coccicheck 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/bash
  2. #
  3. # This script requires at least spatch
  4. # version 1.0.0-rc11.
  5. #
  6. SPATCH="`which ${SPATCH:=spatch}`"
  7. if [ ! -x "$SPATCH" ]; then
  8. echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
  9. exit 1
  10. fi
  11. USE_JOBS="no"
  12. $SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
  13. # The verbosity may be set by the environmental parameter V=
  14. # as for example with 'make V=1 coccicheck'
  15. if [ -n "$V" -a "$V" != "0" ]; then
  16. VERBOSE="$V"
  17. else
  18. VERBOSE=0
  19. fi
  20. if [ -z "$J" ]; then
  21. NPROC=$(getconf _NPROCESSORS_ONLN)
  22. else
  23. NPROC="$J"
  24. fi
  25. FLAGS="--very-quiet"
  26. # You can use SPFLAGS to append extra arguments to coccicheck or override any
  27. # heuristics done in this file as Coccinelle accepts the last options when
  28. # options conflict.
  29. #
  30. # A good example for use of SPFLAGS is if you want to debug your cocci script,
  31. # you can for instance use the following:
  32. #
  33. # $ export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci
  34. # $ make coccicheck MODE=report DEBUG_FILE="all.err" SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c
  35. #
  36. # "--show-trying" should show you what rule is being processed as it goes to
  37. # stdout, you do not need a debug file for that. The profile output will be
  38. # be sent to stdout, if you provide a DEBUG_FILE the profiling data can be
  39. # inspected there.
  40. #
  41. # --profile will not output if --very-quiet is used, so avoid it.
  42. echo $SPFLAGS | egrep -e "--profile|--show-trying" 2>&1 > /dev/null
  43. if [ $? -eq 0 ]; then
  44. FLAGS="--quiet"
  45. fi
  46. # spatch only allows include directories with the syntax "-I include"
  47. # while gcc also allows "-Iinclude" and "-include include"
  48. COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
  49. COCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
  50. if [ "$C" = "1" -o "$C" = "2" ]; then
  51. ONLINE=1
  52. # Take only the last argument, which is the C file to test
  53. shift $(( $# - 1 ))
  54. OPTIONS="$COCCIINCLUDE $1"
  55. else
  56. ONLINE=0
  57. if [ "$KBUILD_EXTMOD" = "" ] ; then
  58. OPTIONS="--dir $srctree $COCCIINCLUDE"
  59. else
  60. OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
  61. fi
  62. fi
  63. if [ "$KBUILD_EXTMOD" != "" ] ; then
  64. OPTIONS="--patch $srctree $OPTIONS"
  65. fi
  66. # You can override by using SPFLAGS
  67. if [ "$USE_JOBS" = "no" ]; then
  68. trap kill_running SIGTERM SIGINT
  69. declare -a SPATCH_PID
  70. elif [ "$NPROC" != "1" ]; then
  71. # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
  72. # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
  73. OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
  74. fi
  75. if [ "$MODE" = "" ] ; then
  76. if [ "$ONLINE" = "0" ] ; then
  77. echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
  78. echo 'Available modes are the following: patch, report, context, org'
  79. echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
  80. echo 'Note however that some modes are not implemented by some semantic patches.'
  81. fi
  82. MODE="report"
  83. fi
  84. if [ "$MODE" = "chain" ] ; then
  85. if [ "$ONLINE" = "0" ] ; then
  86. echo 'You have selected the "chain" mode.'
  87. echo 'All available modes will be tried (in that order): patch, report, context, org'
  88. fi
  89. elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
  90. FLAGS="--no-show-diff $FLAGS"
  91. fi
  92. if [ "$ONLINE" = "0" ] ; then
  93. echo ''
  94. echo 'Please check for false positives in the output before submitting a patch.'
  95. echo 'When using "patch" mode, carefully review the patch before submitting it.'
  96. echo ''
  97. fi
  98. run_cmd_parmap() {
  99. if [ $VERBOSE -ne 0 ] ; then
  100. echo "Running ($NPROC in parallel): $@"
  101. fi
  102. if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then
  103. if [ -f $DEBUG_FILE ]; then
  104. echo "Debug file $DEBUG_FILE exists, bailing"
  105. exit
  106. fi
  107. else
  108. DEBUG_FILE="/dev/null"
  109. fi
  110. $@ 2>$DEBUG_FILE
  111. if [[ $? -ne 0 ]]; then
  112. echo "coccicheck failed"
  113. exit $?
  114. fi
  115. }
  116. run_cmd_old() {
  117. local i
  118. if [ $VERBOSE -ne 0 ] ; then
  119. echo "Running ($NPROC in parallel): $@"
  120. fi
  121. for i in $(seq 0 $(( NPROC - 1)) ); do
  122. eval "$@ --max $NPROC --index $i &"
  123. SPATCH_PID[$i]=$!
  124. if [ $VERBOSE -eq 2 ] ; then
  125. echo "${SPATCH_PID[$i]} running"
  126. fi
  127. done
  128. wait
  129. }
  130. run_cmd() {
  131. if [ "$USE_JOBS" = "yes" ]; then
  132. run_cmd_parmap $@
  133. else
  134. run_cmd_old $@
  135. fi
  136. }
  137. kill_running() {
  138. for i in $(seq 0 $(( NPROC - 1 )) ); do
  139. if [ $VERBOSE -eq 2 ] ; then
  140. echo "Killing ${SPATCH_PID[$i]}"
  141. fi
  142. kill ${SPATCH_PID[$i]} 2>/dev/null
  143. done
  144. }
  145. # You can override heuristics with SPFLAGS, these must always go last
  146. OPTIONS="$OPTIONS $SPFLAGS"
  147. coccinelle () {
  148. COCCI="$1"
  149. OPT=`grep "Option" $COCCI | cut -d':' -f2`
  150. # The option '--parse-cocci' can be used to syntactically check the SmPL files.
  151. #
  152. # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
  153. if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
  154. FILE=`echo $COCCI | sed "s|$srctree/||"`
  155. echo "Processing `basename $COCCI`"
  156. echo "with option(s) \"$OPT\""
  157. echo ''
  158. echo 'Message example to submit a patch:'
  159. sed -ne 's|^///||p' $COCCI
  160. if [ "$MODE" = "patch" ] ; then
  161. echo ' The semantic patch that makes this change is available'
  162. elif [ "$MODE" = "report" ] ; then
  163. echo ' The semantic patch that makes this report is available'
  164. elif [ "$MODE" = "context" ] ; then
  165. echo ' The semantic patch that spots this code is available'
  166. elif [ "$MODE" = "org" ] ; then
  167. echo ' The semantic patch that makes this Org report is available'
  168. else
  169. echo ' The semantic patch that makes this output is available'
  170. fi
  171. echo " in $FILE."
  172. echo ''
  173. echo ' More information about semantic patching is available at'
  174. echo ' http://coccinelle.lip6.fr/'
  175. echo ''
  176. if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
  177. echo 'Semantic patch information:'
  178. sed -ne 's|^//#||p' $COCCI
  179. echo ''
  180. fi
  181. fi
  182. if [ "$MODE" = "chain" ] ; then
  183. run_cmd $SPATCH -D patch \
  184. $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
  185. run_cmd $SPATCH -D report \
  186. $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
  187. run_cmd $SPATCH -D context \
  188. $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
  189. run_cmd $SPATCH -D org \
  190. $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
  191. elif [ "$MODE" = "rep+ctxt" ] ; then
  192. run_cmd $SPATCH -D report \
  193. $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
  194. run_cmd $SPATCH -D context \
  195. $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
  196. else
  197. run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
  198. fi
  199. }
  200. if [ "$COCCI" = "" ] ; then
  201. for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
  202. coccinelle $f
  203. done
  204. else
  205. coccinelle $COCCI
  206. fi