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