blob: 06fcb3333247358c29b15f4b222b2f654d0e1465 [file] [log] [blame]
Nicolas Palix9e395552013-03-02 22:36:26 +01001#!/bin/bash
Nicolas Palix74425ee2010-06-06 17:15:01 +02002
3SPATCH="`which ${SPATCH:=spatch}`"
4
Bernd Schubert26e56722013-01-29 17:03:37 +01005# The verbosity may be set by the environmental parameter V=
6# as for example with 'make V=1 coccicheck'
7
8if [ -n "$V" -a "$V" != "0" ]; then
9 VERBOSE=1
10else
11 VERBOSE=0
12fi
13
Nicolas Palixed621cc2013-03-02 22:36:27 +010014FLAGS="$SPFLAGS -very_quiet"
Nicolas Palix9e395552013-03-02 22:36:26 +010015
16# spatch only allows include directories with the syntax "-I include"
17# while gcc also allows "-Iinclude" and "-include include"
18COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
19COCCIINCLUDE=${COCCIINCLUDE//-include/-I}
20
Nicolas Palix1e9dea22010-06-13 09:26:34 +020021if [ "$C" = "1" -o "$C" = "2" ]; then
22 ONLINE=1
23
Nicolas Palix9e395552013-03-02 22:36:26 +010024 # Take only the last argument, which is the C file to test
25 shift $(( $# - 1 ))
26 OPTIONS="$COCCIINCLUDE $1"
Nicolas Palix1e9dea22010-06-13 09:26:34 +020027else
28 ONLINE=0
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050029 if [ "$KBUILD_EXTMOD" = "" ] ; then
Nicolas Palix9e395552013-03-02 22:36:26 +010030 OPTIONS="-dir $srctree $COCCIINCLUDE"
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050031 else
Nicolas Palixbad6a402013-03-02 22:36:28 +010032 OPTIONS="-dir $KBUILD_EXTMOD $COCCIINCLUDE"
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050033 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +020034fi
35
Nicolas Palixbad6a402013-03-02 22:36:28 +010036if [ "$KBUILD_EXTMOD" != "" ] ; then
37 OPTIONS="-patch $srctree $OPTIONS"
38fi
39
Nicolas Palix74425ee2010-06-06 17:15:01 +020040if [ ! -x "$SPATCH" ]; then
41 echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
42 exit 1
43fi
44
45if [ "$MODE" = "" ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +020046 if [ "$ONLINE" = "0" ] ; then
Nicolas Palix2c1160c82010-10-08 21:27:40 +020047 echo 'You have not explicitly specified the mode to use. Using default "chain" mode.'
48 echo 'All available modes will be tried (in that order): patch, report, context, org'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020049 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020050 fi
Nicolas Palix2c1160c82010-10-08 21:27:40 +020051 MODE="chain"
Nicolas Palix03ee0c42010-10-08 21:27:41 +020052elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
Nicolas Palix062c1822010-10-24 23:37:34 +020053 FLAGS="$FLAGS -no_show_diff"
Nicolas Palix74425ee2010-06-06 17:15:01 +020054fi
55
Nicolas Palix1e9dea22010-06-13 09:26:34 +020056if [ "$ONLINE" = "0" ] ; then
57 echo ''
58 echo 'Please check for false positives in the output before submitting a patch.'
59 echo 'When using "patch" mode, carefully review the patch before submitting it.'
60 echo ''
61fi
Nicolas Palix74425ee2010-06-06 17:15:01 +020062
Bernd Schubert53032652013-01-29 17:03:42 +010063run_cmd() {
64 if [ $VERBOSE -ne 0 ] ; then
65 echo "Running: $@"
66 fi
67 eval $@
68}
69
70
Nicolas Palix1e9dea22010-06-13 09:26:34 +020071coccinelle () {
Nicolas Palix74425ee2010-06-06 17:15:01 +020072 COCCI="$1"
Nicolas Palix74425ee2010-06-06 17:15:01 +020073
74 OPT=`grep "Option" $COCCI | cut -d':' -f2`
Nicolas Palix74425ee2010-06-06 17:15:01 +020075
Nicolas Palix062c1822010-10-24 23:37:34 +020076# The option '-parse_cocci' can be used to syntactically check the SmPL files.
Nicolas Palix74425ee2010-06-06 17:15:01 +020077#
Nicolas Palix1e9dea22010-06-13 09:26:34 +020078# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
Nicolas Palix74425ee2010-06-06 17:15:01 +020079
Nicolas Palix35d88a32013-03-02 22:36:25 +010080 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +020081
82 FILE=`echo $COCCI | sed "s|$srctree/||"`
83
Nicolas Palix3c908412010-10-08 21:27:38 +020084 echo "Processing `basename $COCCI`"
85 echo "with option(s) \"$OPT\""
86 echo ''
Nicolas Palix1e9dea22010-06-13 09:26:34 +020087 echo 'Message example to submit a patch:'
88
Nicolas Palix3c908412010-10-08 21:27:38 +020089 sed -ne 's|^///||p' $COCCI
Nicolas Palix1e9dea22010-06-13 09:26:34 +020090
Nicolas Palix062c1822010-10-24 23:37:34 +020091 if [ "$MODE" = "patch" ] ; then
92 echo ' The semantic patch that makes this change is available'
93 elif [ "$MODE" = "report" ] ; then
94 echo ' The semantic patch that makes this report is available'
95 elif [ "$MODE" = "context" ] ; then
96 echo ' The semantic patch that spots this code is available'
97 elif [ "$MODE" = "org" ] ; then
98 echo ' The semantic patch that makes this Org report is available'
99 else
100 echo ' The semantic patch that makes this output is available'
101 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200102 echo " in $FILE."
103 echo ''
104 echo ' More information about semantic patching is available at'
105 echo ' http://coccinelle.lip6.fr/'
106 echo ''
107
Nicolas Palix3c908412010-10-08 21:27:38 +0200108 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
109 echo 'Semantic patch information:'
110 sed -ne 's|^//#||p' $COCCI
111 echo ''
112 fi
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200113 fi
Nicolas Palix3c908412010-10-08 21:27:38 +0200114
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200115 if [ "$MODE" = "chain" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100116 run_cmd $SPATCH -D patch \
117 $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
118 run_cmd $SPATCH -D report \
119 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
120 run_cmd $SPATCH -D context \
121 $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
122 run_cmd $SPATCH -D org \
123 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
Nicolas Palixc05cd6d2012-09-20 22:30:46 +0200124 elif [ "$MODE" = "rep+ctxt" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100125 run_cmd $SPATCH -D report \
126 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
127 run_cmd $SPATCH -D context \
128 $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200129 else
Bernd Schubert53032652013-01-29 17:03:42 +0100130 run_cmd $SPATCH -D $MODE $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200131 fi
132
Nicolas Palix74425ee2010-06-06 17:15:01 +0200133}
134
135if [ "$COCCI" = "" ] ; then
136 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200137 coccinelle $f
Nicolas Palix74425ee2010-06-06 17:15:01 +0200138 done
139else
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200140 coccinelle $COCCI
Nicolas Palix74425ee2010-06-06 17:15:01 +0200141fi