From f8bbfee119f2f758446900c07b95b31de3dc6fc1 Mon Sep 17 00:00:00 2001 From: willy-liu Date: Fri, 7 Mar 2025 21:31:19 +0800 Subject: [PATCH] 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 --- scripts/pre-commit.hook | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/scripts/pre-commit.hook b/scripts/pre-commit.hook index b610848cc..964144b95 100755 --- a/scripts/pre-commit.hook +++ b/scripts/pre-commit.hook @@ -68,7 +68,27 @@ cppcheck_suppressions() { printf "%s" "$out" | sed 's/[[:space:]]*$//' } +# Generation of standard compliance for GCC/Clang +detect_cc_std() { + local STDC_VERSION="" + local EXTRA_DEFINES="" + if command -v cc >/dev/null 2>&1; then + if cc --version 2>/dev/null | grep -q "clang"; then + STDC_VERSION=$(cc -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}') + EXTRA_DEFINES="-D__clang__=1" + elif cc --version 2>/dev/null | grep -q "Free Software Foundation"; then + STDC_VERSION=$(cc -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}') + EXTRA_DEFINES="-D__GNUC__=1" + fi + fi + if [ -n "$STDC_VERSION" ]; then + EXTRA_DEFINES+=" -D__STDC__=1 -D__STDC_VERSION__=${STDC_VERSION}" + fi + echo "$EXTRA_DEFINES" +} + CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1" +CPPCHECK_OPTS+=" $(detect_cc_std)" CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)" CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."