blob: 9d8780cdbcd43e82555cf97b8afadfce5a3ea75d [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 Palix1f0a6742013-06-06 23:39:52 +020047 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 Palix1e9dea22010-06-13 09:26:34 +020049 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
Nicolas Palix1f0a6742013-06-06 23:39:52 +020050 echo 'Note however that some modes are not implemented by some semantic patches.'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020051 fi
Nicolas Palix1f0a6742013-06-06 23:39:52 +020052 MODE="report"
53fi
54
55if [ "$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 Palix03ee0c42010-10-08 21:27:41 +020060elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
Nicolas Palix062c1822010-10-24 23:37:34 +020061 FLAGS="$FLAGS -no_show_diff"
Nicolas Palix74425ee2010-06-06 17:15:01 +020062fi
63
Nicolas Palix1e9dea22010-06-13 09:26:34 +020064if [ "$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 ''
69fi
Nicolas Palix74425ee2010-06-06 17:15:01 +020070
Bernd Schubert53032652013-01-29 17:03:42 +010071run_cmd() {
72 if [ $VERBOSE -ne 0 ] ; then
73 echo "Running: $@"
74 fi
75 eval $@
76}
77
78
Nicolas Palix1e9dea22010-06-13 09:26:34 +020079coccinelle () {
Nicolas Palix74425ee2010-06-06 17:15:01 +020080 COCCI="$1"
Nicolas Palix74425ee2010-06-06 17:15:01 +020081
82 OPT=`grep "Option" $COCCI | cut -d':' -f2`
Nicolas Palix74425ee2010-06-06 17:15:01 +020083
Nicolas Palix062c1822010-10-24 23:37:34 +020084# The option '-parse_cocci' can be used to syntactically check the SmPL files.
Nicolas Palix74425ee2010-06-06 17:15:01 +020085#
Nicolas Palix1e9dea22010-06-13 09:26:34 +020086# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
Nicolas Palix74425ee2010-06-06 17:15:01 +020087
Nicolas Palix35d88a32013-03-02 22:36:25 +010088 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +020089
90 FILE=`echo $COCCI | sed "s|$srctree/||"`
91
Nicolas Palix3c908412010-10-08 21:27:38 +020092 echo "Processing `basename $COCCI`"
93 echo "with option(s) \"$OPT\""
94 echo ''
Nicolas Palix1e9dea22010-06-13 09:26:34 +020095 echo 'Message example to submit a patch:'
96
Nicolas Palix3c908412010-10-08 21:27:38 +020097 sed -ne 's|^///||p' $COCCI
Nicolas Palix1e9dea22010-06-13 09:26:34 +020098
Nicolas Palix062c1822010-10-24 23:37:34 +020099 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 Palix1e9dea22010-06-13 09:26:34 +0200110 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 Palix3c908412010-10-08 21:27:38 +0200116 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
117 echo 'Semantic patch information:'
118 sed -ne 's|^//#||p' $COCCI
119 echo ''
120 fi
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200121 fi
Nicolas Palix3c908412010-10-08 21:27:38 +0200122
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200123 if [ "$MODE" = "chain" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100124 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 Palixc05cd6d2012-09-20 22:30:46 +0200132 elif [ "$MODE" = "rep+ctxt" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100133 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 Palix1e9dea22010-06-13 09:26:34 +0200137 else
Bernd Schubert53032652013-01-29 17:03:42 +0100138 run_cmd $SPATCH -D $MODE $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200139 fi
140
Nicolas Palix74425ee2010-06-06 17:15:01 +0200141}
142
143if [ "$COCCI" = "" ] ; then
144 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200145 coccinelle $f
Nicolas Palix74425ee2010-06-06 17:15:01 +0200146 done
147else
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200148 coccinelle $COCCI
Nicolas Palix74425ee2010-06-06 17:15:01 +0200149fi