Skip to content

Commit bccc966

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 bccc966

File tree

2 files changed

+54
-42
lines changed

2 files changed

+54
-42
lines changed

queue.c

Lines changed: 0 additions & 6 deletions
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

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,59 @@
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+
cppcheck_suppressions() {
4+
# Array of suppression keys (plain elements, without "--suppress=")
5+
local -a suppr_keys=(
6+
"checkersReport"
7+
"unmatchedSuppression"
8+
"normalCheckLevelMaxBranches"
9+
"missingIncludeSystem"
10+
"noValidConfiguration"
11+
"unusedFunction"
12+
"identicalInnerCondition:log2_lshift16.h"
13+
"checkLevelNormal:log2_lshift16.h"
14+
"nullPointerRedundantCheck:report.c"
15+
"returnDanglingLifetime:report.c"
16+
"nullPointerRedundantCheck:harness.c"
17+
"nullPointerOutOfMemory:harness.c"
18+
"staticFunction:harness.c"
19+
"nullPointerRedundantCheck:queue.c"
20+
"constParameterPointer:queue.c"
21+
"memleak:queue.c"
22+
"nullPointer:queue.c"
23+
"nullPointer:qtest.c"
24+
"constParameterCallback:console.c"
25+
"constParameterPointer:console.c"
26+
"staticFunction:console.c"
27+
"preprocessorErrorDirective:random.h"
28+
"constVariablePointer:linenoise.c"
29+
"staticFunction:linenoise.c"
30+
"nullPointerOutOfMemory:web.c"
31+
"staticFunction:web.c"
32+
)
33+
34+
# Array for additional cppcheck options (non-suppressions)
35+
local -a other_flags=(
36+
"--inline-suppr harness.c"
37+
)
38+
39+
local out=""
40+
# Append other flags.
41+
for flag in "${other_flags[@]}"; do
42+
out+="$flag "
43+
done
44+
45+
# Append each suppression flag separately.
46+
for key in "${suppr_keys[@]}"; do
47+
out+="--suppress=$key "
48+
done
49+
50+
# Trim trailing whitespace and output the final string.
51+
printf "%s" "$out" | sed 's/[[:space:]]*$//'
52+
}
53+
54+
CPPCHECK_OPTS="-I. --enable=all --error-exitcode=1"
55+
CPPCHECK_OPTS+=" --force $(cppcheck_suppressions)"
56+
CPPCHECK_OPTS+=" --cppcheck-build-dir=.out ."
3957

4058
RETURN=0
4159
CLANG_FORMAT=$(which clang-format)

0 commit comments

Comments
 (0)