coccicheck 4.6 KB

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