Skip to content

Commit c1dd57b

Browse files
committed
Improve compiler macro handling in static analysis
Refactored compiler macro detection into a function and ensured predefined macros are correctly passed to cppcheck. This prevents false positives caused by incorrect environment detection in macro evaluations. - Encapsulated the generation of standard compliance into a function - Passed compiler macros to cppcheck via -D option Change-Id: I0c8cd7507f081a03f1c1ffec648aed544a0e6466
1 parent 1cdb288 commit c1dd57b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

scripts/pre-commit.hook

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,25 @@ cppcheck_suppressions() {
6868
printf "%s" "$out" | sed 's/[[:space:]]*$//'
6969
}
7070

71+
# Generation of standard compliance for GCC/Clang
72+
detect_cc_std() {
73+
local STDC_VERSION=""
74+
local EXTRA_DEFINES=""
75+
if command -v gcc >/dev/null 2>&1; then
76+
STDC_VERSION=$(gcc -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}')
77+
EXTRA_DEFINES="-D__GNUC__=1"
78+
elif command -v clang >/dev/null 2>&1; then
79+
STDC_VERSION=$(clang -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}')
80+
EXTRA_DEFINES="-D__clang__=1"
81+
fi
82+
if [ -n "$STDC_VERSION" ]; then
83+
EXTRA_DEFINES+=" -D__STDC__=1 -D__STDC_VERSION__=${STDC_VERSION}"
84+
fi
85+
echo "$EXTRA_DEFINES"
86+
}
87+
7188
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1"
89+
CPPCHECK_OPTS+=" $(detect_cc_std)"
7290
CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)"
7391
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
7492

0 commit comments

Comments
 (0)