coccicheck 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/bin/bash
  2. SPATCH="`which ${SPATCH:=spatch}`"
  3. # The verbosity may be set by the environmental parameter V=
  4. # as for example with 'make V=1 coccicheck'
  5. if [ -n "$V" -a "$V" != "0" ]; then
  6. VERBOSE=1
  7. else
  8. VERBOSE=0
  9. fi
  10. FLAGS="$SPFLAGS -very_quiet"
  11. # spatch only allows include directories with the syntax "-I include"
  12. # while gcc also allows "-Iinclude" and "-include include"
  13. COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
  14. COCCIINCLUDE=${COCCIINCLUDE//-include/-I}
  15. if [ "$C" = "1" -o "$C" = "2" ]; then
  16. ONLINE=1
  17. # Take only the last argument, which is the C file to test
  18. shift $(( $# - 1 ))
  19. OPTIONS="$COCCIINCLUDE $1"
  20. else
  21. ONLINE=0
  22. if [ "$KBUILD_EXTMOD" = "" ] ; then
  23. OPTIONS="-dir $srctree $COCCIINCLUDE"
  24. else
  25. OPTIONS="-dir $KBUILD_EXTMOD $COCCIINCLUDE"
  26. fi
  27. fi
  28. if [ "$KBUILD_EXTMOD" != "" ] ; then
  29. OPTIONS="-patch $srctree $OPTIONS"
  30. fi
  31. if [ ! -x "$SPATCH" ]; then
  32. echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
  33. exit 1
  34. fi
  35. if [ "$MODE" = "" ] ; then
  36. if [ "$ONLINE" = "0" ] ; then
  37. echo 'You have not explicitly specified the mode to use. Using default "chain" mode.'
  38. echo 'All available modes will be tried (in that order): patch, report, context, org'
  39. echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
  40. fi
  41. MODE="chain"
  42. elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
  43. FLAGS="$FLAGS -no_show_diff"
  44. fi
  45. if [ "$ONLINE" = "0" ] ; then
  46. echo ''
  47. echo 'Please check for false positives in the output before submitting a patch.'
  48. echo 'When using "patch" mode, carefully review the patch before submitting it.'
  49. echo ''
  50. fi
  51. run_cmd() {
  52. if [ $VERBOSE -ne 0 ] ; then
  53. echo "Running: $@"
  54. fi
  55. eval $@
  56. }
  57. coccinelle () {
  58. COCCI="$1"
  59. OPT=`grep "Option" $COCCI | cut -d':' -f2`
  60. # The option '-parse_cocci' can be used to syntactically check the SmPL files.
  61. #
  62. # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
  63. if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
  64. FILE=`echo $COCCI | sed "s|$srctree/||"`
  65. echo "Processing `basename $COCCI`"
  66. echo "with option(s) \"$OPT\""
  67. echo ''
  68. echo 'Message example to submit a patch:'
  69. sed -ne 's|^///||p' $COCCI
  70. if [ "$MODE" = "patch" ] ; then
  71. echo ' The semantic patch that makes this change is available'
  72. elif [ "$MODE" = "report" ] ; then
  73. echo ' The semantic patch that makes this report is available'
  74. elif [ "$MODE" = "context" ] ; then
  75. echo ' The semantic patch that spots this code is available'
  76. elif [ "$MODE" = "org" ] ; then
  77. echo ' The semantic patch that makes this Org report is available'
  78. else
  79. echo ' The semantic patch that makes this output is available'
  80. fi
  81. echo " in $FILE."
  82. echo ''
  83. echo ' More information about semantic patching is available at'
  84. echo ' http://coccinelle.lip6.fr/'
  85. echo ''
  86. if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
  87. echo 'Semantic patch information:'
  88. sed -ne 's|^//#||p' $COCCI
  89. echo ''
  90. fi
  91. fi
  92. if [ "$MODE" = "chain" ] ; then
  93. run_cmd $SPATCH -D patch \
  94. $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
  95. run_cmd $SPATCH -D report \
  96. $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
  97. run_cmd $SPATCH -D context \
  98. $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
  99. run_cmd $SPATCH -D org \
  100. $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
  101. elif [ "$MODE" = "rep+ctxt" ] ; then
  102. run_cmd $SPATCH -D report \
  103. $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
  104. run_cmd $SPATCH -D context \
  105. $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
  106. else
  107. run_cmd $SPATCH -D $MODE $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
  108. fi
  109. }
  110. if [ "$COCCI" = "" ] ; then
  111. for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
  112. coccinelle $f
  113. done
  114. else
  115. coccinelle $COCCI
  116. fi