Skip to content

Commit a3bd3df

Browse files
committed
Fix missing compiler macros in cppcheck analysis
Static analysis with cppcheck fails because predefined compiler macros are not available during analysis. This causes the environment detection in the list macro to fall back to an invalid code branch, triggering false errors such as unused labels. Although this fallback branch is not used in supported environments, it is still parsed by cppcheck unless the proper macro definitions are provided. This commit resolves the issue by passing the necessary compiler environment macros to cppcheck through the -D option, ensuring correct evaluation of conditional logic in macro definitions. - Prevent static analysis false positives in list macros - Ensure the environment is correctly simulated during checks Change-Id: If8b51c44f194f19f109bc4aaf91a464f850e7fdc
1 parent 1cdb288 commit a3bd3df

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

scripts/pre-commit.hook

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

71+
# Check complier and __STDC_VERSION__
72+
EXTRA_DEFINES=""
73+
if command -v gcc >/dev/null 2>&1; then
74+
STDC_VERSION=$(gcc -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}')
75+
EXTRA_DEFINES="-D__GNUC__=1"
76+
elif command -v clang >/dev/null 2>&1; then
77+
STDC_VERSION=$(clang -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}')
78+
EXTRA_DEFINES="-D__clang__=1"
79+
else
80+
STDC_VERSION=""
81+
fi
82+
if [ -n "$STDC_VERSION" ]; then
83+
EXTRA_DEFINES+=" -D__STDC__=1 -D__STDC_VERSION__=${STDC_VERSION}"
84+
fi
85+
7186
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1"
87+
CPPCHECK_OPTS+=" $EXTRA_DEFINES"
7288
CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)"
7389
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
7490

0 commit comments

Comments
 (0)