coccicheck 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "report" mode.'
  38. echo 'Available modes are the following: patch, report, context, org'
  39. echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
  40. echo 'Note however that some modes are not implemented by some semantic patches.'
  41. fi
  42. MODE="report"
  43. fi
  44. if [ "$MODE" = "chain" ] ; then
  45. if [ "$ONLINE" = "0" ] ; then
  46. echo 'You have selected the "chain" mode.'
  47. echo 'All available modes will be tried (in that order): patch, report, context, org'
  48. fi
  49. elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
  50. FLAGS="$FLAGS -no_show_diff"
  51. fi
  52. if [ "$ONLINE" = "0" ] ; then
  53. echo ''
  54. echo 'Please check for false positives in the output before submitting a patch.'
  55. echo 'When using "patch" mode, carefully review the patch before submitting it.'
  56. echo ''
  57. fi
  58. run_cmd() {
  59. if [ $VERBOSE -ne 0 ] ; then
  60. echo "Running: $@"
  61. fi
  62. eval $@
  63. }
  64. coccinelle () {
  65. COCCI="$1"
  66. OPT=`grep "Option" $COCCI | cut -d':' -f2`
  67. # The option '-parse_cocci' can be used to syntactically check the SmPL files.
  68. #
  69. # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
  70. if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
  71. FILE=`echo $COCCI | sed "s|$srctree/||"`
  72. echo "Processing `basename $COCCI`"
  73. echo "with option(s) \"$OPT\""
  74. echo ''
  75. echo 'Message example to submit a patch:'
  76. sed -ne 's|^///||p' $COCCI
  77. if [ "$MODE" = "patch" ] ; then
  78. echo ' The semantic patch that makes this change is available'
  79. elif [ "$MODE" = "report" ] ; then
  80. echo ' The semantic patch that makes this report is available'
  81. elif [ "$MODE" = "context" ] ; then
  82. echo ' The semantic patch that spots this code is available'
  83. elif [ "$MODE" = "org" ] ; then
  84. echo ' The semantic patch that makes this Org report is available'
  85. else
  86. echo ' The semantic patch that makes this output is available'
  87. fi
  88. echo " in $FILE."
  89. echo ''
  90. echo ' More information about semantic patching is available at'
  91. echo ' http://coccinelle.lip6.fr/'
  92. echo ''
  93. if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
  94. echo 'Semantic patch information:'
  95. sed -ne 's|^//#||p' $COCCI
  96. echo ''
  97. fi
  98. fi
  99. if [ "$MODE" = "chain" ] ; then
  100. run_cmd $SPATCH -D patch \
  101. $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
  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 || \
  106. run_cmd $SPATCH -D org \
  107. $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
  108. elif [ "$MODE" = "rep+ctxt" ] ; then
  109. run_cmd $SPATCH -D report \
  110. $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
  111. run_cmd $SPATCH -D context \
  112. $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
  113. else
  114. run_cmd $SPATCH -D $MODE $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
  115. fi
  116. }
  117. if [ "$COCCI" = "" ] ; then
  118. for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
  119. coccinelle $f
  120. done
  121. else
  122. coccinelle $COCCI
  123. fi