Skip to content

Commit 5070fcd

Browse files
authored
CI: Run static analysis with Cppcheck (#105)
Cppcheck[1] is integrated into CI pipeline for running static analysis. However, Cppcheck is known to report false-positive, and we have to suppress some warnings in advance. [1] https://cppcheck.sourceforge.io/
1 parent 55b77fa commit 5070fcd

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

.ci/static-analysis.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
3+
SOURCES=$(find $(git rev-parse --show-toplevel) | egrep "\.(cpp|cc|c|h)\$")
4+
5+
CPPCHECK=$(which cppcheck)
6+
if [ $? -ne 0 ]; then
7+
echo "[!] cppcheck not installed. Failed to run static analysis the source code." >&2
8+
exit 1
9+
fi
10+
11+
## Suppression list ##
12+
# This list will explain the detail of suppressed warnings.
13+
# The prototype of the item should be like:
14+
# "- [{file}] {spec}: {reason}"
15+
#
16+
# - [hello-1.c] unusedFunction: False positive of init_module and cleanup_module.
17+
# - [*.c] missingIncludeSystem: Focus on the example code, not the kernel headers.
18+
19+
OPTS=" --enable=warning,style,performance,information
20+
--suppress=unusedFunction:hello-1.c
21+
--suppress=missingIncludeSystem
22+
--std=c89 "
23+
24+
$CPPCHECK $OPTS --xml ${SOURCES} 2> cppcheck.xml
25+
ERROR_COUNT=$(cat cppcheck.xml | egrep -c "</error>" )
26+
27+
if [ $ERROR_COUNT -gt 0 ]; then
28+
echo "Cppcheck failed: error count is $ERROR_COUNT"
29+
cat cppcheck.xml
30+
exit 1
31+
fi
32+
exit 0

.github/workflows/generate_doc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ jobs:
4444
- name: validate coding style and functionality
4545
run: |
4646
sudo apt-get install -q -y clang-format-11
47+
sudo apt-get install -q -y cppcheck
4748
.ci/check-format.sh
49+
.ci/static-analysis.sh
4850
.ci/build-n-run.sh
4951
shell: bash

examples/example_atomic.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
99
#define BYTE_TO_BINARY(byte) \
10-
(byte & 0x80 ? '1' : '0'), (byte & 0x40 ? '1' : '0'), \
11-
(byte & 0x20 ? '1' : '0'), (byte & 0x10 ? '1' : '0'), \
12-
(byte & 0x08 ? '1' : '0'), (byte & 0x04 ? '1' : '0'), \
13-
(byte & 0x02 ? '1' : '0'), (byte & 0x01 ? '1' : '0')
10+
((byte & 0x80) ? '1' : '0'), ((byte & 0x40) ? '1' : '0'), \
11+
((byte & 0x20) ? '1' : '0'), ((byte & 0x10) ? '1' : '0'), \
12+
((byte & 0x08) ? '1' : '0'), ((byte & 0x04) ? '1' : '0'), \
13+
((byte & 0x02) ? '1' : '0'), ((byte & 0x01) ? '1' : '0')
1414

1515
static void atomic_add_subtract(void)
1616
{

examples/ioctl.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <linux/uaccess.h>
1111

1212
struct ioctl_arg {
13-
unsigned int reg;
1413
unsigned int val;
1514
};
1615

0 commit comments

Comments
 (0)