Skip to content

Commit 267d42f

Browse files
committed
Refactor compiler macro detection into a function
Refactored the detection of compiler type and standard version into a Bash function. This function determines the compiler in use, extracts the standard version, and constructs the necessary macro definitions for static analysis tools. The detected macros are then passed to cppcheck to ensure proper environment simulation. - Improve code organization and clarity - Ensure static analysis tools receive accurate macro definitions - Support both GCC and Clang detection Change-Id: I5e37ece6fddb27eeb2eff2dc582aa0f6949db945
1 parent a3bd3df commit 267d42f

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

scripts/pre-commit.hook

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,25 @@ 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
71+
# Check compiler type and __STDC_VERSION__
72+
get_compiler_macros() {
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+
}
8587

8688
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1"
87-
CPPCHECK_OPTS+=" $EXTRA_DEFINES"
89+
CPPCHECK_OPTS+=" $(get_compiler_macros)"
8890
CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)"
8991
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
9092

0 commit comments

Comments
 (0)