blob: cdde8e0670aa19c9b3825a3953b175f5e158ab1b [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 Palix9e395552013-03-02 22:36:26 +010014FLAGS="-very_quiet"
15
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 Palix9e395552013-03-02 22:36:26 +010032 OPTIONS="-dir $KBUILD_EXTMOD -patch $srctree $COCCIINCLUDE"
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050033 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +020034fi
35
Nicolas Palix74425ee2010-06-06 17:15:01 +020036if [ ! -x "$SPATCH" ]; then
37 echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
38 exit 1
39fi
40
41if [ "$MODE" = "" ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +020042 if [ "$ONLINE" = "0" ] ; then
Nicolas Palix2c1160c82010-10-08 21:27:40 +020043 echo 'You have not explicitly specified the mode to use. Using default "chain" mode.'
44 echo 'All available modes will be tried (in that order): patch, report, context, org'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020045 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020046 fi
Nicolas Palix2c1160c82010-10-08 21:27:40 +020047 MODE="chain"
Nicolas Palix03ee0c42010-10-08 21:27:41 +020048elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
Nicolas Palix062c1822010-10-24 23:37:34 +020049 FLAGS="$FLAGS -no_show_diff"
Nicolas Palix74425ee2010-06-06 17:15:01 +020050fi
51
Nicolas Palix1e9dea22010-06-13 09:26:34 +020052if [ "$ONLINE" = "0" ] ; then
53 echo ''
54 echo 'Please check for false positives in the output before submitting a patch.'
55 echo 'When using "patch" mode, carefully review the patch before submitting it.'
56 echo ''
57fi
Nicolas Palix74425ee2010-06-06 17:15:01 +020058
Bernd Schubert53032652013-01-29 17:03:42 +010059run_cmd() {
60 if [ $VERBOSE -ne 0 ] ; then
61 echo "Running: $@"
62 fi
63 eval $@
64}
65
66
Nicolas Palix1e9dea22010-06-13 09:26:34 +020067coccinelle () {
Nicolas Palix74425ee2010-06-06 17:15:01 +020068 COCCI="$1"
Nicolas Palix74425ee2010-06-06 17:15:01 +020069
70 OPT=`grep "Option" $COCCI | cut -d':' -f2`
Nicolas Palix74425ee2010-06-06 17:15:01 +020071
Nicolas Palix062c1822010-10-24 23:37:34 +020072# The option '-parse_cocci' can be used to syntactically check the SmPL files.
Nicolas Palix74425ee2010-06-06 17:15:01 +020073#
Nicolas Palix1e9dea22010-06-13 09:26:34 +020074# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
Nicolas Palix74425ee2010-06-06 17:15:01 +020075
Nicolas Palix35d88a32013-03-02 22:36:25 +010076 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +020077
78 FILE=`echo $COCCI | sed "s|$srctree/||"`
79
Nicolas Palix3c908412010-10-08 21:27:38 +020080 echo "Processing `basename $COCCI`"
81 echo "with option(s) \"$OPT\""
82 echo ''
Nicolas Palix1e9dea22010-06-13 09:26:34 +020083 echo 'Message example to submit a patch:'
84
Nicolas Palix3c908412010-10-08 21:27:38 +020085 sed -ne 's|^///||p' $COCCI
Nicolas Palix1e9dea22010-06-13 09:26:34 +020086
Nicolas Palix062c1822010-10-24 23:37:34 +020087 if [ "$MODE" = "patch" ] ; then
88 echo ' The semantic patch that makes this change is available'
89 elif [ "$MODE" = "report" ] ; then
90 echo ' The semantic patch that makes this report is available'
91 elif [ "$MODE" = "context" ] ; then
92 echo ' The semantic patch that spots this code is available'
93 elif [ "$MODE" = "org" ] ; then
94 echo ' The semantic patch that makes this Org report is available'
95 else
96 echo ' The semantic patch that makes this output is available'
97 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +020098 echo " in $FILE."
99 echo ''
100 echo ' More information about semantic patching is available at'
101 echo ' http://coccinelle.lip6.fr/'
102 echo ''
103
Nicolas Palix3c908412010-10-08 21:27:38 +0200104 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
105 echo 'Semantic patch information:'
106 sed -ne 's|^//#||p' $COCCI
107 echo ''
108 fi
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200109 fi
Nicolas Palix3c908412010-10-08 21:27:38 +0200110
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200111 if [ "$MODE" = "chain" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100112 run_cmd $SPATCH -D patch \
113 $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
114 run_cmd $SPATCH -D report \
115 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
116 run_cmd $SPATCH -D context \
117 $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
118 run_cmd $SPATCH -D org \
119 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
Nicolas Palixc05cd6d2012-09-20 22:30:46 +0200120 elif [ "$MODE" = "rep+ctxt" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100121 run_cmd $SPATCH -D report \
122 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
123 run_cmd $SPATCH -D context \
124 $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200125 else
Bernd Schubert53032652013-01-29 17:03:42 +0100126 run_cmd $SPATCH -D $MODE $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200127 fi
128
Nicolas Palix74425ee2010-06-06 17:15:01 +0200129}
130
131if [ "$COCCI" = "" ] ; then
132 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200133 coccinelle $f
Nicolas Palix74425ee2010-06-06 17:15:01 +0200134 done
135else
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200136 coccinelle $COCCI
Nicolas Palix74425ee2010-06-06 17:15:01 +0200137fi