Skip to content

Commit b479ac9

Browse files
committed
Run Cppcheck only when source files change
Static analysis is essential for C/C++ projects, but running 'fmtscan' can significantly slow down Cppcheck. The git pre-commit hook detects staged files and run Cppcheck only if C/C++ files have been modified, resulting in a noticeable reduction in analysis time. Change-Id: I872c0b6fc9008cc2b3c96283e458b25cf506eb16
1 parent 407d43e commit b479ac9

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

scripts/pre-commit.hook

+10-8
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ detect_cc_std() {
9191
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1"
9292
CPPCHECK_OPTS+=" $(detect_cc_std)"
9393
CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)"
94-
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
94+
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out"
9595

9696
set_colors
9797

@@ -250,13 +250,15 @@ if [ -n "$C_FILES" ]; then
250250
fi
251251

252252
# static analysis
253-
echo "Running static analysis..."
254-
$CPPCHECK $CPPCHECK_OPTS >/dev/null
255-
if [ $? -ne 0 ]; then
256-
RETURN=1
257-
echo "" >&2
258-
echo "Fail to pass static analysis." >&2
259-
echo
253+
if [ -n "$C_FILES" ]; then
254+
echo "Running static analysis..."
255+
$CPPCHECK $CPPCHECK_OPTS $C_FILES >/dev/null
256+
if [ $? -ne 0 ]; then
257+
RETURN=1
258+
echo "" >&2
259+
echo "Fail to pass static analysis." >&2
260+
echo
261+
fi
260262
fi
261263

262264
# non-ASCII filenames are not allowed.

0 commit comments

Comments
 (0)