Skip to content

Commit f8bbfee

Browse files
committed
Fix missing compiler macros in cppcheck analysis
Added a function to determine the compiler type (GCC or Clang) and extract the standard version. This ensures that predefined macros are correctly passed to cppcheck, preventing false positives caused by incorrect environment detection in macro evaluations. Change-Id: I0c8cd7507f081a03f1c1ffec648aed544a0e6466
1 parent 1cdb288 commit f8bbfee

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

scripts/pre-commit.hook

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,27 @@ 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 cc >/dev/null 2>&1; then
76+
if cc --version 2>/dev/null | grep -q "clang"; then
77+
STDC_VERSION=$(cc -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}')
78+
EXTRA_DEFINES="-D__clang__=1"
79+
elif cc --version 2>/dev/null | grep -q "Free Software Foundation"; then
80+
STDC_VERSION=$(cc -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}')
81+
EXTRA_DEFINES="-D__GNUC__=1"
82+
fi
83+
fi
84+
if [ -n "$STDC_VERSION" ]; then
85+
EXTRA_DEFINES+=" -D__STDC__=1 -D__STDC_VERSION__=${STDC_VERSION}"
86+
fi
87+
echo "$EXTRA_DEFINES"
88+
}
89+
7190
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1"
91+
CPPCHECK_OPTS+=" $(detect_cc_std)"
7292
CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)"
7393
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
7494

0 commit comments

Comments
 (0)