blob: ad8e8ffd3c7ec7c6d32008196f2323836b3877dd [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
Kees Cook90d06a42013-06-18 14:49:29 -07005trap kill_running SIGTERM SIGINT
6declare -a SPATCH_PID
7
Bernd Schubert26e56722013-01-29 17:03:37 +01008# The verbosity may be set by the environmental parameter V=
9# as for example with 'make V=1 coccicheck'
10
11if [ -n "$V" -a "$V" != "0" ]; then
Kees Cook90d06a42013-06-18 14:49:29 -070012 VERBOSE="$V"
Bernd Schubert26e56722013-01-29 17:03:37 +010013else
14 VERBOSE=0
15fi
16
Kees Cook90d06a42013-06-18 14:49:29 -070017if [ -z "$J" ]; then
18 NPROC=$(getconf _NPROCESSORS_ONLN)
19else
20 NPROC="$J"
21fi
22
Nicolas Palixed621cc2013-03-02 22:36:27 +010023FLAGS="$SPFLAGS -very_quiet"
Nicolas Palix9e395552013-03-02 22:36:26 +010024
25# spatch only allows include directories with the syntax "-I include"
26# while gcc also allows "-Iinclude" and "-include include"
27COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
28COCCIINCLUDE=${COCCIINCLUDE//-include/-I}
29
Nicolas Palix1e9dea22010-06-13 09:26:34 +020030if [ "$C" = "1" -o "$C" = "2" ]; then
31 ONLINE=1
32
Nicolas Palix9e395552013-03-02 22:36:26 +010033 # Take only the last argument, which is the C file to test
34 shift $(( $# - 1 ))
35 OPTIONS="$COCCIINCLUDE $1"
Nicolas Palix1e9dea22010-06-13 09:26:34 +020036else
37 ONLINE=0
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050038 if [ "$KBUILD_EXTMOD" = "" ] ; then
Nicolas Palix9e395552013-03-02 22:36:26 +010039 OPTIONS="-dir $srctree $COCCIINCLUDE"
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050040 else
Nicolas Palixbad6a402013-03-02 22:36:28 +010041 OPTIONS="-dir $KBUILD_EXTMOD $COCCIINCLUDE"
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050042 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +020043fi
44
Nicolas Palixbad6a402013-03-02 22:36:28 +010045if [ "$KBUILD_EXTMOD" != "" ] ; then
46 OPTIONS="-patch $srctree $OPTIONS"
47fi
48
Nicolas Palix74425ee2010-06-06 17:15:01 +020049if [ ! -x "$SPATCH" ]; then
50 echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
51 exit 1
52fi
53
54if [ "$MODE" = "" ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +020055 if [ "$ONLINE" = "0" ] ; then
Nicolas Palix1f0a6742013-06-06 23:39:52 +020056 echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
57 echo 'Available modes are the following: patch, report, context, org'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020058 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
Nicolas Palix1f0a6742013-06-06 23:39:52 +020059 echo 'Note however that some modes are not implemented by some semantic patches.'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020060 fi
Nicolas Palix1f0a6742013-06-06 23:39:52 +020061 MODE="report"
62fi
63
64if [ "$MODE" = "chain" ] ; then
65 if [ "$ONLINE" = "0" ] ; then
66 echo 'You have selected the "chain" mode.'
67 echo 'All available modes will be tried (in that order): patch, report, context, org'
68 fi
Nicolas Palix03ee0c42010-10-08 21:27:41 +020069elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
Nicolas Palix062c1822010-10-24 23:37:34 +020070 FLAGS="$FLAGS -no_show_diff"
Nicolas Palix74425ee2010-06-06 17:15:01 +020071fi
72
Nicolas Palix1e9dea22010-06-13 09:26:34 +020073if [ "$ONLINE" = "0" ] ; then
74 echo ''
75 echo 'Please check for false positives in the output before submitting a patch.'
76 echo 'When using "patch" mode, carefully review the patch before submitting it.'
77 echo ''
78fi
Nicolas Palix74425ee2010-06-06 17:15:01 +020079
Bernd Schubert53032652013-01-29 17:03:42 +010080run_cmd() {
Kees Cook90d06a42013-06-18 14:49:29 -070081 local i
Bernd Schubert53032652013-01-29 17:03:42 +010082 if [ $VERBOSE -ne 0 ] ; then
Kees Cook90d06a42013-06-18 14:49:29 -070083 echo "Running ($NPROC in parallel): $@"
Bernd Schubert53032652013-01-29 17:03:42 +010084 fi
Kees Cook90d06a42013-06-18 14:49:29 -070085 for i in $(seq 0 $(( NPROC - 1)) ); do
86 eval "$@ -max $NPROC -index $i &"
87 SPATCH_PID[$i]=$!
88 if [ $VERBOSE -eq 2 ] ; then
89 echo "${SPATCH_PID[$i]} running"
90 fi
91 done
92 wait
Bernd Schubert53032652013-01-29 17:03:42 +010093}
94
Kees Cook90d06a42013-06-18 14:49:29 -070095kill_running() {
96 for i in $(seq $(( NPROC - 1 )) ); do
97 if [ $VERBOSE -eq 2 ] ; then
98 echo "Killing ${SPATCH_PID[$i]}"
99 fi
100 kill ${SPATCH_PID[$i]} 2>/dev/null
101 done
102}
Bernd Schubert53032652013-01-29 17:03:42 +0100103
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200104coccinelle () {
Nicolas Palix74425ee2010-06-06 17:15:01 +0200105 COCCI="$1"
Nicolas Palix74425ee2010-06-06 17:15:01 +0200106
107 OPT=`grep "Option" $COCCI | cut -d':' -f2`
Nicolas Palix74425ee2010-06-06 17:15:01 +0200108
Nicolas Palix062c1822010-10-24 23:37:34 +0200109# The option '-parse_cocci' can be used to syntactically check the SmPL files.
Nicolas Palix74425ee2010-06-06 17:15:01 +0200110#
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200111# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
Nicolas Palix74425ee2010-06-06 17:15:01 +0200112
Nicolas Palix35d88a32013-03-02 22:36:25 +0100113 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200114
115 FILE=`echo $COCCI | sed "s|$srctree/||"`
116
Nicolas Palix3c908412010-10-08 21:27:38 +0200117 echo "Processing `basename $COCCI`"
118 echo "with option(s) \"$OPT\""
119 echo ''
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200120 echo 'Message example to submit a patch:'
121
Nicolas Palix3c908412010-10-08 21:27:38 +0200122 sed -ne 's|^///||p' $COCCI
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200123
Nicolas Palix062c1822010-10-24 23:37:34 +0200124 if [ "$MODE" = "patch" ] ; then
125 echo ' The semantic patch that makes this change is available'
126 elif [ "$MODE" = "report" ] ; then
127 echo ' The semantic patch that makes this report is available'
128 elif [ "$MODE" = "context" ] ; then
129 echo ' The semantic patch that spots this code is available'
130 elif [ "$MODE" = "org" ] ; then
131 echo ' The semantic patch that makes this Org report is available'
132 else
133 echo ' The semantic patch that makes this output is available'
134 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200135 echo " in $FILE."
136 echo ''
137 echo ' More information about semantic patching is available at'
138 echo ' http://coccinelle.lip6.fr/'
139 echo ''
140
Nicolas Palix3c908412010-10-08 21:27:38 +0200141 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
142 echo 'Semantic patch information:'
143 sed -ne 's|^//#||p' $COCCI
144 echo ''
145 fi
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200146 fi
Nicolas Palix3c908412010-10-08 21:27:38 +0200147
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200148 if [ "$MODE" = "chain" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100149 run_cmd $SPATCH -D patch \
150 $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
151 run_cmd $SPATCH -D report \
152 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
153 run_cmd $SPATCH -D context \
154 $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
155 run_cmd $SPATCH -D org \
156 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
Nicolas Palixc05cd6d2012-09-20 22:30:46 +0200157 elif [ "$MODE" = "rep+ctxt" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100158 run_cmd $SPATCH -D report \
159 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
160 run_cmd $SPATCH -D context \
161 $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200162 else
Bernd Schubert53032652013-01-29 17:03:42 +0100163 run_cmd $SPATCH -D $MODE $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200164 fi
165
Nicolas Palix74425ee2010-06-06 17:15:01 +0200166}
167
168if [ "$COCCI" = "" ] ; then
169 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200170 coccinelle $f
Nicolas Palix74425ee2010-06-06 17:15:01 +0200171 done
172else
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200173 coccinelle $COCCI
Nicolas Palix74425ee2010-06-06 17:15:01 +0200174fi