Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 35 additions & 33 deletions tools/static-analysis/clang-tidy-builder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ shopt -s extglob || exit $?

retval=0

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

while :; do

Expand All @@ -17,27 +17,26 @@ if [[ -v CLANG_TIDY ]]; then
done
unset arg

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

if [[ ! "$source_file" =~ (^|/)src/[^/]+\.c$ ]] && [[ ! "$source_file" =~ (^|/)wolfhsm/[^/]+\.c$ ]]; then
if [[ -v CLANG_OVERRIDE_CFLAGS ]]; then
read -a CLANG_OVERRIDE_CFLAGS_a < <(echo "${CLANG_OVERRIDE_CFLAGS-}")
# Skip wolfssl/ and .github/ directories for wolfHSM project
if [[ "$source_file" =~ wolfssl/ ]] || [[ "$source_file" =~ \.github/ ]]; then
# Just compile without clang-tidy for these directories
if [[ -n "${CLANG_OVERRIDE_CFLAGS+x}" ]]; then
# shellcheck disable=SC2162 # we want backslashes to be interpreted here.
read -a CLANG_OVERRIDE_CFLAGS_a < <(echo "${CLANG_OVERRIDE_CFLAGS}")
else
CLANG_OVERRIDE_CFLAGS_a=()
fi
exec "$CLANG" "$@" "${CLANG_OVERRIDE_CFLAGS_a[@]}"
fi

if [[ -v CLANG_TIDY_ARGS ]]; then
read -r -a clang_tidy_args_array < <(echo "$CLANG_TIDY_ARGS") || exit $?
else
clang_tidy_args_array=()
fi
read -r -a clang_tidy_args_array < <(echo "$CLANG_TIDY_ARGS") || exit $?

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

if [[ -v per_file_checks ]]; then
if [[ -n "${per_file_checks+x}" ]]; then
declare -i i=0
while [[ $i -lt ${#clang_tidy_args_array[@]} ]]; do
if [[ "${clang_tidy_args_array[i]}" =~ ^-checks ]]; then
Expand All @@ -61,15 +60,15 @@ if [[ -v CLANG_TIDY ]]; then
fi
: $((++i))
done
if [[ ! -v added_to_existing_checks ]]; then
if [[ -z "${added_to_existing_checks+x}" ]]; then
SAVE_IFS="$IFS"
IFS=,
clang_tidy_args_array+=("-checks=${per_file_checks[*]}")
IFS="$SAVE_IFS"
fi
fi

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

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

if [[ -v CLANG_TIDY_EXTRA_ARGS ]]; then
if [[ -n "${CLANG_TIDY_EXTRA_ARGS+x}" ]]; then
read -r -a clang_tidy_extra_args < <(echo "$CLANG_TIDY_EXTRA_ARGS") || exit $?
clang_tidy_args_array+=("${clang_tidy_extra_args[@]}")
fi
Expand All @@ -96,7 +95,7 @@ if [[ -v CLANG_TIDY ]]; then
done
unset arg

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

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

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

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

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

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

IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7])) == "${clang_tidy_line_a[1]}" ]]; then
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
else
echo "$clang_tidy_line" >&2
fi
Expand All @@ -138,7 +137,7 @@ if [[ -v CLANG_TIDY ]]; then

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

IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7])) == "${clang_tidy_line_a[1]}" ]]; then
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
else
echo "$clang_tidy_line" >&2
fi
Expand All @@ -158,7 +157,7 @@ if [[ -v CLANG_TIDY ]]; then

IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7] + clang_tidy_line_a[9])) == "${clang_tidy_line_a[1]}" ]]; then
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
else
echo "$clang_tidy_line" >&2
fi
Expand All @@ -168,7 +167,7 @@ if [[ -v CLANG_TIDY ]]; then

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

IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7])) == "${clang_tidy_line_a[1]}" ]]; then
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
else
echo "$clang_tidy_line" >&2
fi
Expand All @@ -188,7 +187,7 @@ if [[ -v CLANG_TIDY ]]; then

IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7])) == "${clang_tidy_line_a[1]}" ]]; then
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
else
echo "$clang_tidy_line" >&2
fi
Expand All @@ -198,7 +197,7 @@ if [[ -v CLANG_TIDY ]]; then

IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
if [[ $((clang_tidy_line_a[3] + clang_tidy_line_a[7] + clang_tidy_line_a[12])) == "${clang_tidy_line_a[1]}" ]]; then
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
else
echo "$clang_tidy_line" >&2
fi
Expand All @@ -208,7 +207,7 @@ if [[ -v CLANG_TIDY ]]; then

IFS="[( ]" read -r -a clang_tidy_line_a < <(echo "$clang_tidy_line")
if [[ "${clang_tidy_line_a[1]}" == "${clang_tidy_line_a[3]}" ]]; then
[[ -v do_style_restore ]] && echo -n "$text_normal_start" >&2
[[ -n "${do_style_restore+x}" ]] && echo -n "$text_normal_start" >&2
else
echo "$clang_tidy_line" >&2
fi
Expand All @@ -217,24 +216,27 @@ if [[ -v CLANG_TIDY ]]; then
*)

echo "$clang_tidy_line" >&2
retval=1
if [[ "$clang_tidy_line" =~ :[[:space:]]error: ]]; then
retval=1
fi
;;

esac

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

if [[ "$retval" != '0' && -v do_style_restore ]]; then
if [[ "$retval" != '0' && -n "${do_style_restore+x}" ]]; then
echo -n "$text_normal_start" >&2
fi
break
done
fi

if [[ "$retval" != '0' ]]; then
if [[ -v CLANG_TIDY_STATUS_FILE ]]; then
# shellcheck disable=SC2320 # noise
if [[ -n "${CLANG_TIDY_STATUS_FILE+x}" ]]; then
echo "${source_file} ${retval}" >> "$CLANG_TIDY_STATUS_FILE" || exit $?
# Don't exit with error - let build continue and collect all errors
retval=0
else
exit "$retval"
fi
Expand Down
Loading