coccicheck 3.6 KB

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