|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -o noclobber -o nounset || exit $? |
| 4 | +shopt -s extglob || exit $? |
| 5 | + |
| 6 | +retval=0 |
| 7 | + |
| 8 | +if [[ -v CLANG_TIDY ]]; then |
| 9 | + |
| 10 | + while :; do |
| 11 | + |
| 12 | + for arg in "$@"; do |
| 13 | + case "$arg" in |
| 14 | + *.c) source_file="$arg" |
| 15 | + ;; |
| 16 | + esac |
| 17 | + done |
| 18 | + unset arg |
| 19 | + |
| 20 | + if [[ ! -v source_file ]]; then |
| 21 | + retval=0 |
| 22 | + break |
| 23 | + fi |
| 24 | + |
| 25 | + if [[ ! "$source_file" =~ (^|/)src/[^/]+\.c$ ]] && [[ ! "$source_file" =~ (^|/)wolfhsm/[^/]+\.c$ ]]; then |
| 26 | + if [[ -v CLANG_OVERRIDE_CFLAGS ]]; then |
| 27 | + read -a CLANG_OVERRIDE_CFLAGS_a < <(echo "${CLANG_OVERRIDE_CFLAGS-}") |
| 28 | + else |
| 29 | + CLANG_OVERRIDE_CFLAGS_a=() |
| 30 | + fi |
| 31 | + exec "$CLANG" "$@" "${CLANG_OVERRIDE_CFLAGS_a[@]}" |
| 32 | + fi |
| 33 | + |
| 34 | + if [[ -v CLANG_TIDY_ARGS ]]; then |
| 35 | + read -r -a clang_tidy_args_array < <(echo "$CLANG_TIDY_ARGS") || exit $? |
| 36 | + else |
| 37 | + clang_tidy_args_array=() |
| 38 | + fi |
| 39 | + |
| 40 | + if [[ -v CLANG_TIDY_PER_FILE_CHECKS ]]; then |
| 41 | + per_file_checks=() |
| 42 | + read -r -a clang_tidy_per_file_checks < <(echo "$CLANG_TIDY_PER_FILE_CHECKS") || exit $? |
| 43 | + for check in "${clang_tidy_per_file_checks[@]}"; do |
| 44 | + if [[ "$source_file" =~ ${check%:*} ]]; then |
| 45 | + per_file_checks+=("${check#*:}") |
| 46 | + fi |
| 47 | + done |
| 48 | + unset check |
| 49 | + fi |
| 50 | + |
| 51 | + if [[ -v per_file_checks ]]; then |
| 52 | + declare -i i=0 |
| 53 | + while [[ $i -lt ${#clang_tidy_args_array[@]} ]]; do |
| 54 | + if [[ "${clang_tidy_args_array[i]}" =~ ^-checks ]]; then |
| 55 | + SAVE_IFS="$IFS" |
| 56 | + IFS=, |
| 57 | + clang_tidy_args_array[i]="${clang_tidy_args_array[i]},${per_file_checks[*]}" |
| 58 | + IFS="$SAVE_IFS" |
| 59 | + added_to_existing_checks= |
| 60 | + break |
| 61 | + fi |
| 62 | + : $((++i)) |
| 63 | + done |
| 64 | + if [[ ! -v added_to_existing_checks ]]; then |
| 65 | + SAVE_IFS="$IFS" |
| 66 | + IFS=, |
| 67 | + clang_tidy_args_array+=("-checks=${per_file_checks[*]}") |
| 68 | + IFS="$SAVE_IFS" |
| 69 | + fi |
| 70 | + fi |
| 71 | + |
| 72 | + if [[ -v CLANG_TIDY_PER_FILE_ARGS ]]; then |
| 73 | + read -r -a clang_tidy_per_file_args < <(echo "$CLANG_TIDY_PER_FILE_ARGS") || exit $? |
| 74 | + for arg in "${clang_tidy_per_file_args[@]}"; do |
| 75 | + if [[ "$source_file" =~ ${arg%:*} ]]; then |
| 76 | + clang_tidy_args_array+=("${arg#*:}") |
| 77 | + fi |
| 78 | + done |
| 79 | + unset arg |
| 80 | + fi |
| 81 | + |
| 82 | + if [[ -v CLANG_TIDY_CONFIG ]]; then |
| 83 | + clang_tidy_args_array+=("-config=${CLANG_TIDY_CONFIG}") |
| 84 | + fi |
| 85 | + |
| 86 | + if [[ -v CLANG_TIDY_EXTRA_ARGS ]]; then |
| 87 | + read -r -a clang_tidy_extra_args < <(echo "$CLANG_TIDY_EXTRA_ARGS") || exit $? |
| 88 | + clang_tidy_args_array+=("${clang_tidy_extra_args[@]}") |
| 89 | + fi |
| 90 | + |
| 91 | + for arg in "${clang_tidy_args_array[@]}"; do |
| 92 | + case "$arg" in |
| 93 | + --use-color) use_color= |
| 94 | + ;; |
| 95 | + esac |
| 96 | + done |
| 97 | + unset arg |
| 98 | + |
| 99 | + if [[ -v use_color ]]; then |
| 100 | + if text_normal_start="$(tput sgr0)"; then |
| 101 | + do_style_restore= |
| 102 | + fi |
| 103 | + fi |
| 104 | + |
| 105 | + while read -r clang_tidy_line; do |
| 106 | + case "$clang_tidy_line" in |
| 107 | + Use\ -header-filter=.*\ to\ display\ errors\ from\ all\ non-system\ headers.\ Use\ -system-headers\ to\ display\ errors\ from\ system\ headers\ as\ well.) |
| 108 | + |
| 109 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 110 | + ;; |
| 111 | + |
| 112 | + +([0-9])\ warning?(s)\ generated.) |
| 113 | + |
| 114 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 115 | + ;; |
| 116 | + |
| 117 | + Suppressed\ +([0-9])\ warnings\ \(+([0-9])\ NOLINT\).) |
| 118 | + |
| 119 | + IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line") |
| 120 | + if [[ "${clang_tidy_line_a[3]}" == "${clang_tidy_line_a[1]}" ]]; then |
| 121 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 122 | + else |
| 123 | + echo "$clang_tidy_line" >&2 |
| 124 | + fi |
| 125 | + ;; |
| 126 | + |
| 127 | + Suppressed\ +([0-9])\ warnings\ \(+([0-9])\ in\ non-user\ code,\ +([0-9])\ NOLINT\).) |
| 128 | + |
| 129 | + IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line") |
| 130 | + if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7])) == "${clang_tidy_line_a[1]}" ]]; then |
| 131 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 132 | + else |
| 133 | + echo "$clang_tidy_line" >&2 |
| 134 | + fi |
| 135 | + ;; |
| 136 | + |
| 137 | + Suppressed\ +([0-9])\ warnings\ \(+([0-9])\ with\ check\ filters\).) |
| 138 | + |
| 139 | + IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line") |
| 140 | + if [[ "${clang_tidy_line_a[3]}" == "${clang_tidy_line_a[1]}" ]]; then |
| 141 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 142 | + else |
| 143 | + echo "$clang_tidy_line" >&2 |
| 144 | + fi |
| 145 | + ;; |
| 146 | + |
| 147 | + Suppressed\ +([0-9])\ warnings\ \(+([0-9])\ in\ non-user\ code,\ +([0-9])\ with\ check\ filters\).) |
| 148 | + |
| 149 | + IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line") |
| 150 | + if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7])) == "${clang_tidy_line_a[1]}" ]]; then |
| 151 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 152 | + else |
| 153 | + echo "$clang_tidy_line" >&2 |
| 154 | + fi |
| 155 | + ;; |
| 156 | + |
| 157 | + Suppressed\ +([0-9])\ warnings\ \(+([0-9])\ in\ non-user\ code,\ +([0-9])\ NOLINT,\ +([0-9])\ with\ check\ filters\).) |
| 158 | + |
| 159 | + IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line") |
| 160 | + if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7] + clang_tidy_line_a[9])) == "${clang_tidy_line_a[1]}" ]]; then |
| 161 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 162 | + else |
| 163 | + echo "$clang_tidy_line" >&2 |
| 164 | + fi |
| 165 | + ;; |
| 166 | + |
| 167 | + Suppressed\ +([0-9])\ warnings\ \(+([0-9])\ due\ to\ line\ filter\).) |
| 168 | + |
| 169 | + IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line") |
| 170 | + if [[ "${clang_tidy_line_a[3]}" == "${clang_tidy_line_a[1]}" ]]; then |
| 171 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 172 | + else |
| 173 | + echo "$clang_tidy_line" >&2 |
| 174 | + fi |
| 175 | + ;; |
| 176 | + |
| 177 | + Suppressed\ +([0-9])\ warnings\ \(+([0-9])\ in\ non-user\ code,\ +([0-9])\ due\ to\ line\ filter\).) |
| 178 | + |
| 179 | + IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line") |
| 180 | + if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7])) == "${clang_tidy_line_a[1]}" ]]; then |
| 181 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 182 | + else |
| 183 | + echo "$clang_tidy_line" >&2 |
| 184 | + fi |
| 185 | + ;; |
| 186 | + |
| 187 | + Suppressed\ +([0-9])\ warnings\ \(+([0-9])\ due\ to\ line\ filter,\ +([0-9])\ NOLINT\).) |
| 188 | + |
| 189 | + IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line") |
| 190 | + if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7])) == "${clang_tidy_line_a[1]}" ]]; then |
| 191 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 192 | + else |
| 193 | + echo "$clang_tidy_line" >&2 |
| 194 | + fi |
| 195 | + ;; |
| 196 | + |
| 197 | + Suppressed\ +([0-9])\ warnings\ \(+([0-9])\ in\ non-user\ code,\ +([0-9])\ due\ to\ line\ filter,\ +([0-9])\ NOLINT\).) |
| 198 | + |
| 199 | + IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line") |
| 200 | + if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7] + clang_tidy_line_a[12])) == "${clang_tidy_line_a[1]}" ]]; then |
| 201 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 202 | + else |
| 203 | + echo "$clang_tidy_line" >&2 |
| 204 | + fi |
| 205 | + ;; |
| 206 | + |
| 207 | + Suppressed\ +([0-9])\ warnings\ \(+([0-9])\ in\ non-user\ code\).) |
| 208 | + |
| 209 | + IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line") |
| 210 | + if [[ "${clang_tidy_line_a[1]}" == "${clang_tidy_line_a[3]}" ]]; then |
| 211 | + [[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2 |
| 212 | + else |
| 213 | + echo "$clang_tidy_line" >&2 |
| 214 | + fi |
| 215 | + ;; |
| 216 | + |
| 217 | + *) |
| 218 | + |
| 219 | + echo "$clang_tidy_line" >&2 |
| 220 | + retval=1 |
| 221 | + ;; |
| 222 | + |
| 223 | + esac |
| 224 | + |
| 225 | + done < <("$CLANG_TIDY" "${clang_tidy_args_array[@]}" "$source_file" -- "$@" 2>&1) |
| 226 | + |
| 227 | + if [[ "$retval" != '0' && -v do_style_restore ]]; then |
| 228 | + echo -n "$text_normal_start" >&2 |
| 229 | + fi |
| 230 | + break |
| 231 | + done |
| 232 | +fi |
| 233 | + |
| 234 | +if [[ "$retval" != '0' ]]; then |
| 235 | + if [[ -v CLANG_TIDY_STATUS_FILE ]]; then |
| 236 | + # shellcheck disable=SC2320 # noise |
| 237 | + echo "${source_file} ${retval}" >> "$CLANG_TIDY_STATUS_FILE" || exit $? |
| 238 | + else |
| 239 | + exit "$retval" |
| 240 | + fi |
| 241 | +fi |
| 242 | + |
| 243 | +# shellcheck disable=SC2162 # we want backslashes to be interpreted here. |
| 244 | +read -a CLANG_OVERRIDE_CFLAGS_a < <(echo "${CLANG_OVERRIDE_CFLAGS-}") |
| 245 | + |
| 246 | +exec "$CLANG" "$@" "${CLANG_OVERRIDE_CFLAGS_a[@]}" |
0 commit comments