Skip to content

Commit 25d82ec

Browse files
committed
CI: Integrate the Sparse into the CI pipeline
Sparse[1] is a semantic parser for C language, it can find out the potential problem of the example code. [1] https://www.kernel.org/doc/html/latest/dev-tools/sparse.html
1 parent 5070fcd commit 25d82ec

File tree

1 file changed

+62
-29
lines changed

1 file changed

+62
-29
lines changed

.ci/static-analysis.sh

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,65 @@
11
#!/usr/bin/env bash
22

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
3+
function do_cppcheck()
4+
{
5+
local SOURCES=$(find $(git rev-parse --show-toplevel) | egrep "\.(cpp|cc|c|h)\$")
6+
7+
local CPPCHECK=$(which cppcheck)
8+
if [ $? -ne 0 ]; then
9+
echo "[!] cppcheck not installed. Failed to run static analysis the source code." >&2
10+
exit 1
11+
fi
12+
13+
## Suppression list ##
14+
# This list will explain the detail of suppressed warnings.
15+
# The prototype of the item should be like:
16+
# "- [{file}] {spec}: {reason}"
17+
#
18+
# - [hello-1.c] unusedFunction: False positive of init_module and cleanup_module.
19+
# - [*.c] missingIncludeSystem: Focus on the example code, not the kernel headers.
20+
21+
local OPTS="
22+
--enable=warning,style,performance,information
23+
--suppress=unusedFunction:hello-1.c
24+
--suppress=missingIncludeSystem
25+
--std=c89 "
26+
27+
$CPPCHECK $OPTS --xml ${SOURCES} 2> cppcheck.xml
28+
local ERROR_COUNT=$(cat cppcheck.xml | egrep -c "</error>" )
29+
30+
if [ $ERROR_COUNT -gt 0 ]; then
31+
echo "Cppcheck failed: $ERROR_COUNT error(s)"
32+
cat cppcheck.xml
33+
exit 1
34+
fi
35+
}
36+
37+
function do_sparse()
38+
{
39+
wget -q http://www.kernel.org/pub/software/devel/sparse/dist/sparse-latest.tar.gz
40+
if [ $? -ne 0 ]; then
41+
echo "Failed to download sparse."
42+
exit 1
43+
fi
44+
tar -xzf sparse-latest.tar.gz
45+
pushd sparse-*/
46+
make sparse || exit 1
47+
sudo make INST_PROGRAMS=sparse PREFIX=/usr install || exit 1
48+
popd
49+
50+
make -C examples C=2 2> sparse.log
51+
52+
local WARNING_COUNT=$(cat sparse.log | egrep -c " warning:" )
53+
local ERROR_COUNT=$(cat sparse.log | egrep -c " error:" )
54+
local COUNT=`expr $WARNING_COUNT + $ERROR_COUNT`
55+
if [ $COUNT -gt 0 ]; then
56+
echo "Sparse failed: $WARNING_COUNT warning(s), $ERROR_COUNT error(s)"
57+
cat sparse.log
58+
exit 1
59+
fi
60+
make -C examples clean
61+
}
62+
63+
do_cppcheck
64+
do_sparse
3265
exit 0

0 commit comments

Comments
 (0)