Skip to content

Commit 7cf1dc6

Browse files
authored
Merge pull request #237 from wolfSSL/clang-tidy-fix
Clang-tidy: Mimic wolfSSL clang-tidy config
2 parents 6436b5c + 6133c62 commit 7cf1dc6

File tree

2 files changed

+162
-65
lines changed

2 files changed

+162
-65
lines changed

tools/static-analysis/clang-tidy-builder.sh

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ shopt -s extglob || exit $?
55

66
retval=0
77

8-
if [[ -v CLANG_TIDY ]]; then
8+
if [[ -n "${CLANG_TIDY+x}" ]]; then
99

1010
while :; do
1111

@@ -17,27 +17,26 @@ if [[ -v CLANG_TIDY ]]; then
1717
done
1818
unset arg
1919

20-
if [[ ! -v source_file ]]; then
20+
if [[ -z "${source_file+x}" ]]; then
2121
retval=0
2222
break
2323
fi
2424

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-}")
25+
# Skip wolfssl/ and .github/ directories for wolfHSM project
26+
if [[ "$source_file" =~ wolfssl/ ]] || [[ "$source_file" =~ \.github/ ]]; then
27+
# Just compile without clang-tidy for these directories
28+
if [[ -n "${CLANG_OVERRIDE_CFLAGS+x}" ]]; then
29+
# shellcheck disable=SC2162 # we want backslashes to be interpreted here.
30+
read -a CLANG_OVERRIDE_CFLAGS_a < <(echo "${CLANG_OVERRIDE_CFLAGS}")
2831
else
2932
CLANG_OVERRIDE_CFLAGS_a=()
3033
fi
3134
exec "$CLANG" "$@" "${CLANG_OVERRIDE_CFLAGS_a[@]}"
3235
fi
3336

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
37+
read -r -a clang_tidy_args_array < <(echo "$CLANG_TIDY_ARGS") || exit $?
3938

40-
if [[ -v CLANG_TIDY_PER_FILE_CHECKS ]]; then
39+
if [[ -n "${CLANG_TIDY_PER_FILE_CHECKS+x}" ]]; then
4140
per_file_checks=()
4241
read -r -a clang_tidy_per_file_checks < <(echo "$CLANG_TIDY_PER_FILE_CHECKS") || exit $?
4342
for check in "${clang_tidy_per_file_checks[@]}"; do
@@ -48,7 +47,7 @@ if [[ -v CLANG_TIDY ]]; then
4847
unset check
4948
fi
5049

51-
if [[ -v per_file_checks ]]; then
50+
if [[ -n "${per_file_checks+x}" ]]; then
5251
declare -i i=0
5352
while [[ $i -lt ${#clang_tidy_args_array[@]} ]]; do
5453
if [[ "${clang_tidy_args_array[i]}" =~ ^-checks ]]; then
@@ -61,15 +60,15 @@ if [[ -v CLANG_TIDY ]]; then
6160
fi
6261
: $((++i))
6362
done
64-
if [[ ! -v added_to_existing_checks ]]; then
63+
if [[ -z "${added_to_existing_checks+x}" ]]; then
6564
SAVE_IFS="$IFS"
6665
IFS=,
6766
clang_tidy_args_array+=("-checks=${per_file_checks[*]}")
6867
IFS="$SAVE_IFS"
6968
fi
7069
fi
7170

72-
if [[ -v CLANG_TIDY_PER_FILE_ARGS ]]; then
71+
if [[ -n "${CLANG_TIDY_PER_FILE_ARGS+x}" ]]; then
7372
read -r -a clang_tidy_per_file_args < <(echo "$CLANG_TIDY_PER_FILE_ARGS") || exit $?
7473
for arg in "${clang_tidy_per_file_args[@]}"; do
7574
if [[ "$source_file" =~ ${arg%:*} ]]; then
@@ -79,11 +78,11 @@ if [[ -v CLANG_TIDY ]]; then
7978
unset arg
8079
fi
8180

82-
if [[ -v CLANG_TIDY_CONFIG ]]; then
81+
if [[ -n "${CLANG_TIDY_CONFIG+x}" ]]; then
8382
clang_tidy_args_array+=("-config=${CLANG_TIDY_CONFIG}")
8483
fi
8584

86-
if [[ -v CLANG_TIDY_EXTRA_ARGS ]]; then
85+
if [[ -n "${CLANG_TIDY_EXTRA_ARGS+x}" ]]; then
8786
read -r -a clang_tidy_extra_args < <(echo "$CLANG_TIDY_EXTRA_ARGS") || exit $?
8887
clang_tidy_args_array+=("${clang_tidy_extra_args[@]}")
8988
fi
@@ -96,7 +95,7 @@ if [[ -v CLANG_TIDY ]]; then
9695
done
9796
unset arg
9897

99-
if [[ -v use_color ]]; then
98+
if [[ -n "${use_color+x}" ]]; then
10099
if text_normal_start="$(tput sgr0)"; then
101100
do_style_restore=
102101
fi
@@ -106,19 +105,19 @@ if [[ -v CLANG_TIDY ]]; then
106105
case "$clang_tidy_line" in
107106
Use\ -header-filter=.*\ to\ display\ errors\ from\ all\ non-system\ headers.\ Use\ -system-headers\ to\ display\ errors\ from\ system\ headers\ as\ well.)
108107

109-
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
108+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
110109
;;
111110

112111
+([0-9])\ warning?(s)\ generated.)
113112

114-
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
113+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
115114
;;
116115

117116
Suppressed\ +([0-9])\ warnings\ \(+([0-9])\ NOLINT\).)
118117

119118
IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
120119
if [[ "${clang_tidy_line_a[3]}" == "${clang_tidy_line_a[1]}" ]]; then
121-
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
120+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
122121
else
123122
echo "$clang_tidy_line" >&2
124123
fi
@@ -128,7 +127,7 @@ if [[ -v CLANG_TIDY ]]; then
128127

129128
IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
130129
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
130+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
132131
else
133132
echo "$clang_tidy_line" >&2
134133
fi
@@ -138,7 +137,7 @@ if [[ -v CLANG_TIDY ]]; then
138137

139138
IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
140139
if [[ "${clang_tidy_line_a[3]}" == "${clang_tidy_line_a[1]}" ]]; then
141-
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
140+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
142141
else
143142
echo "$clang_tidy_line" >&2
144143
fi
@@ -148,7 +147,7 @@ if [[ -v CLANG_TIDY ]]; then
148147

149148
IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
150149
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
150+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
152151
else
153152
echo "$clang_tidy_line" >&2
154153
fi
@@ -158,7 +157,7 @@ if [[ -v CLANG_TIDY ]]; then
158157

159158
IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
160159
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
160+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
162161
else
163162
echo "$clang_tidy_line" >&2
164163
fi
@@ -168,7 +167,7 @@ if [[ -v CLANG_TIDY ]]; then
168167

169168
IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
170169
if [[ "${clang_tidy_line_a[3]}" == "${clang_tidy_line_a[1]}" ]]; then
171-
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
170+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
172171
else
173172
echo "$clang_tidy_line" >&2
174173
fi
@@ -178,7 +177,7 @@ if [[ -v CLANG_TIDY ]]; then
178177

179178
IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
180179
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
180+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
182181
else
183182
echo "$clang_tidy_line" >&2
184183
fi
@@ -188,7 +187,7 @@ if [[ -v CLANG_TIDY ]]; then
188187

189188
IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
190189
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
190+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
192191
else
193192
echo "$clang_tidy_line" >&2
194193
fi
@@ -198,7 +197,7 @@ if [[ -v CLANG_TIDY ]]; then
198197

199198
IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
200199
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
200+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
202201
else
203202
echo "$clang_tidy_line" >&2
204203
fi
@@ -208,7 +207,7 @@ if [[ -v CLANG_TIDY ]]; then
208207

209208
IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
210209
if [[ "${clang_tidy_line_a[1]}" == "${clang_tidy_line_a[3]}" ]]; then
211-
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
210+
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
212211
else
213212
echo "$clang_tidy_line" >&2
214213
fi
@@ -217,24 +216,27 @@ if [[ -v CLANG_TIDY ]]; then
217216
*)
218217

219218
echo "$clang_tidy_line" >&2
220-
retval=1
219+
if [[ "$clang_tidy_line" =~ :[[:space:]]error: ]]; then
220+
retval=1
221+
fi
221222
;;
222223

223224
esac
224225

225226
done < <("$CLANG_TIDY" "${clang_tidy_args_array[@]}" "$source_file" -- "$@" 2>&1)
226227

227-
if [[ "$retval" != '0' && -v do_style_restore ]]; then
228+
if [[ "$retval" != '0' && -n "${do_style_restore+x}" ]]; then
228229
echo -n "$text_normal_start" >&2
229230
fi
230231
break
231232
done
232233
fi
233234

234235
if [[ "$retval" != '0' ]]; then
235-
if [[ -v CLANG_TIDY_STATUS_FILE ]]; then
236-
# shellcheck disable=SC2320 # noise
236+
if [[ -n "${CLANG_TIDY_STATUS_FILE+x}" ]]; then
237237
echo "${source_file} ${retval}" >> "$CLANG_TIDY_STATUS_FILE" || exit $?
238+
# Don't exit with error - let build continue and collect all errors
239+
retval=0
238240
else
239241
exit "$retval"
240242
fi

0 commit comments

Comments
 (0)