From 25d82ec47f1ed1872c1f30624d69a4a52bf386ee Mon Sep 17 00:00:00 2001 From: linD026 Date: Wed, 15 Sep 2021 08:19:43 +0800 Subject: [PATCH] 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 --- .ci/static-analysis.sh | 81 +++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/.ci/static-analysis.sh b/.ci/static-analysis.sh index 8604616..1079b59 100755 --- a/.ci/static-analysis.sh +++ b/.ci/static-analysis.sh @@ -1,32 +1,65 @@ #!/usr/bin/env bash -SOURCES=$(find $(git rev-parse --show-toplevel) | egrep "\.(cpp|cc|c|h)\$") +function do_cppcheck() +{ + local SOURCES=$(find $(git rev-parse --show-toplevel) | egrep "\.(cpp|cc|c|h)\$") -CPPCHECK=$(which cppcheck) -if [ $? -ne 0 ]; then - echo "[!] cppcheck not installed. Failed to run static analysis the source code." >&2 - exit 1 -fi + local CPPCHECK=$(which cppcheck) + if [ $? -ne 0 ]; then + echo "[!] cppcheck not installed. Failed to run static analysis the source code." >&2 + exit 1 + fi -## Suppression list ## -# This list will explain the detail of suppressed warnings. -# The prototype of the item should be like: -# "- [{file}] {spec}: {reason}" -# -# - [hello-1.c] unusedFunction: False positive of init_module and cleanup_module. -# - [*.c] missingIncludeSystem: Focus on the example code, not the kernel headers. + ## Suppression list ## + # This list will explain the detail of suppressed warnings. + # The prototype of the item should be like: + # "- [{file}] {spec}: {reason}" + # + # - [hello-1.c] unusedFunction: False positive of init_module and cleanup_module. + # - [*.c] missingIncludeSystem: Focus on the example code, not the kernel headers. -OPTS=" --enable=warning,style,performance,information - --suppress=unusedFunction:hello-1.c - --suppress=missingIncludeSystem - --std=c89 " + local OPTS=" + --enable=warning,style,performance,information + --suppress=unusedFunction:hello-1.c + --suppress=missingIncludeSystem + --std=c89 " -$CPPCHECK $OPTS --xml ${SOURCES} 2> cppcheck.xml -ERROR_COUNT=$(cat cppcheck.xml | egrep -c "" ) + $CPPCHECK $OPTS --xml ${SOURCES} 2> cppcheck.xml + local ERROR_COUNT=$(cat cppcheck.xml | egrep -c "" ) -if [ $ERROR_COUNT -gt 0 ]; then - echo "Cppcheck failed: error count is $ERROR_COUNT" - cat cppcheck.xml - exit 1 -fi + if [ $ERROR_COUNT -gt 0 ]; then + echo "Cppcheck failed: $ERROR_COUNT error(s)" + cat cppcheck.xml + exit 1 + fi +} + +function do_sparse() +{ + wget -q http://www.kernel.org/pub/software/devel/sparse/dist/sparse-latest.tar.gz + if [ $? -ne 0 ]; then + echo "Failed to download sparse." + exit 1 + fi + tar -xzf sparse-latest.tar.gz + pushd sparse-*/ + make sparse || exit 1 + sudo make INST_PROGRAMS=sparse PREFIX=/usr install || exit 1 + popd + + make -C examples C=2 2> sparse.log + + local WARNING_COUNT=$(cat sparse.log | egrep -c " warning:" ) + local ERROR_COUNT=$(cat sparse.log | egrep -c " error:" ) + local COUNT=`expr $WARNING_COUNT + $ERROR_COUNT` + if [ $COUNT -gt 0 ]; then + echo "Sparse failed: $WARNING_COUNT warning(s), $ERROR_COUNT error(s)" + cat sparse.log + exit 1 + fi + make -C examples clean +} + +do_cppcheck +do_sparse exit 0