Skip to content

Commit 1b3b95a

Browse files
committed
Refine Cppcheck suppression generation
This commit refines the generation of Cppcheck suppression. Previously, all suppression keys were combined into a single comma-separated option. Now, each key is output with its own '--suppress=' flag and stored in an array, making the configuration clearer and easier to update. Additional options, such as '--inline-suppr harness.c', remain unchanged. Change-Id: I09d4c2438120cd5ab36b716c5d271062c42c315e
1 parent 0fc3836 commit 1b3b95a

File tree

2 files changed

+63
-42
lines changed

2 files changed

+63
-42
lines changed

queue.c

-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55
#include "queue.h"
66

7-
/* Notice: sometimes, Cppcheck would find the potential NULL pointer bugs,
8-
* but some of them cannot occur. You can suppress them by adding the
9-
* following line.
10-
* cppcheck-suppress nullPointer
11-
*/
12-
137
/* Create an empty queue */
148
struct list_head *q_new()
159
{

scripts/pre-commit.hook

+63-36
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,68 @@
11
#!/usr/bin/env bash
22

3-
CPPCHECK_unmatched=
4-
for f in *.c; do
5-
CPPCHECK_unmatched="$CPPCHECK_unmatched --suppress=unmatchedSuppression:$f"
6-
done
7-
8-
# We suppress the checkLevelNormal warning for Cppcheck versions 2.11 and above.
9-
# Please refer to issues/153 for more details.
10-
CPPCHECK_suppresses="--inline-suppr harness.c \
11-
--suppress=checkersReport \
12-
--suppress=unmatchedSuppression \
13-
--suppress=normalCheckLevelMaxBranches \
14-
--suppress=missingIncludeSystem \
15-
--suppress=noValidConfiguration \
16-
--suppress=unusedFunction \
17-
--suppress=identicalInnerCondition:log2_lshift16.h \
18-
--suppress=nullPointerRedundantCheck:report.c \
19-
--suppress=nullPointerRedundantCheck:harness.c \
20-
--suppress=nullPointerOutOfMemory:harness.c \
21-
--suppress=staticFunction:harness.c \
22-
--suppress=nullPointerRedundantCheck:queue.c \
23-
--suppress=constParameterPointer:queue.c \
24-
--suppress=memleak:queue.c \
25-
--suppress=nullPointer:queue.c \
26-
--suppress=nullPointer:qtest.c \
27-
--suppress=returnDanglingLifetime:report.c \
28-
--suppress=constParameterCallback:console.c \
29-
--suppress=constParameterPointer:console.c \
30-
--suppress=staticFunction:console.c \
31-
--suppress=checkLevelNormal:log2_lshift16.h \
32-
--suppress=preprocessorErrorDirective:random.h \
33-
--suppress=constVariablePointer:linenoise.c \
34-
--suppress=staticFunction:linenoise.c \
35-
--suppress=nullPointerOutOfMemory:web.c \
36-
--suppress=staticFunction:web.c \
37-
"
38-
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1 --force $CPPCHECK_suppresses $CPPCHECK_unmatched --cppcheck-build-dir=.out ."
3+
# Build unmatched suppressions for each *.c file.
4+
cppcheck_build_unmatched() {
5+
local file suppression=""
6+
for file in *.c; do
7+
suppression+=" --suppress=unmatchedSuppression:$file"
8+
done
9+
echo "$suppression"
10+
}
11+
12+
cppcheck_suppressions() {
13+
# Array of suppression keys (plain elements, without "--suppress=")
14+
local -a suppr_keys=(
15+
"checkersReport"
16+
"unmatchedSuppression"
17+
"normalCheckLevelMaxBranches"
18+
"missingIncludeSystem"
19+
"noValidConfiguration"
20+
"unusedFunction"
21+
"identicalInnerCondition:log2_lshift16.h"
22+
"checkLevelNormal:log2_lshift16.h"
23+
"nullPointerRedundantCheck:report.c"
24+
"returnDanglingLifetime:report.c"
25+
"nullPointerRedundantCheck:harness.c"
26+
"nullPointerOutOfMemory:harness.c"
27+
"staticFunction:harness.c"
28+
"nullPointerRedundantCheck:queue.c"
29+
"constParameterPointer:queue.c"
30+
"memleak:queue.c"
31+
"nullPointer:queue.c"
32+
"nullPointer:qtest.c"
33+
"constParameterCallback:console.c"
34+
"constParameterPointer:console.c"
35+
"staticFunction:console.c"
36+
"preprocessorErrorDirective:random.h"
37+
"constVariablePointer:linenoise.c"
38+
"staticFunction:linenoise.c"
39+
"nullPointerOutOfMemory:web.c"
40+
"staticFunction:web.c"
41+
)
42+
43+
# Array for additional cppcheck options (non-suppressions)
44+
local -a other_flags=(
45+
"--inline-suppr harness.c"
46+
)
47+
48+
local out=""
49+
# Append other flags.
50+
for flag in "${other_flags[@]}"; do
51+
out+="$flag "
52+
done
53+
54+
# Append each suppression flag separately.
55+
for key in "${suppr_keys[@]}"; do
56+
out+="--suppress=$key "
57+
done
58+
59+
# Trim trailing whitespace and output the final string.
60+
printf "%s" "$out" | sed 's/[[:space:]]*$//'
61+
}
62+
63+
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1"
64+
CPPCHECK_OPTS+=" --force $(cppcheck_suppressions) $(cppcheck_build_unmatched)"
65+
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
3966

4067
RETURN=0
4168
CLANG_FORMAT=$(which clang-format)

0 commit comments

Comments
 (0)