static-analysis.sh 2.5 KB

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