blob: f6627863fdc38a5b3ce0edc6e23993f96e8b4dd9 [file] [log] [blame]
Nicolas Palix9e395552013-03-02 22:36:26 +01001#!/bin/bash
Nicolas Palix74425ee2010-06-06 17:15:01 +02002
Nicolas Palixec979462013-07-03 16:41:01 +02003#
4# This script requires at least spatch
5# version 1.0.0-rc11.
6#
7
Nicolas Palix74425ee2010-06-06 17:15:01 +02008SPATCH="`which ${SPATCH:=spatch}`"
9
Kees Cook90d06a42013-06-18 14:49:29 -070010trap kill_running SIGTERM SIGINT
11declare -a SPATCH_PID
12
Bernd Schubert26e56722013-01-29 17:03:37 +010013# The verbosity may be set by the environmental parameter V=
14# as for example with 'make V=1 coccicheck'
15
16if [ -n "$V" -a "$V" != "0" ]; then
Kees Cook90d06a42013-06-18 14:49:29 -070017 VERBOSE="$V"
Bernd Schubert26e56722013-01-29 17:03:37 +010018else
19 VERBOSE=0
20fi
21
Kees Cook90d06a42013-06-18 14:49:29 -070022if [ -z "$J" ]; then
23 NPROC=$(getconf _NPROCESSORS_ONLN)
24else
25 NPROC="$J"
26fi
27
Deepa Dinamani7a2358b2016-06-12 12:04:39 -070028FLAGS="--very-quiet $SPFLAGS"
Nicolas Palix9e395552013-03-02 22:36:26 +010029
30# spatch only allows include directories with the syntax "-I include"
31# while gcc also allows "-Iinclude" and "-include include"
32COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
Andrzej Hajda5b169102015-09-22 15:15:30 +020033COCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
Nicolas Palix9e395552013-03-02 22:36:26 +010034
Nicolas Palix1e9dea22010-06-13 09:26:34 +020035if [ "$C" = "1" -o "$C" = "2" ]; then
36 ONLINE=1
37
Nicolas Palix9e395552013-03-02 22:36:26 +010038 # Take only the last argument, which is the C file to test
39 shift $(( $# - 1 ))
40 OPTIONS="$COCCIINCLUDE $1"
Nicolas Palix1e9dea22010-06-13 09:26:34 +020041else
42 ONLINE=0
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050043 if [ "$KBUILD_EXTMOD" = "" ] ; then
Nicolas Palix93f14462013-06-20 13:10:56 +020044 OPTIONS="--dir $srctree $COCCIINCLUDE"
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050045 else
Nicolas Palix93f14462013-06-20 13:10:56 +020046 OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050047 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +020048fi
49
Nicolas Palixbad6a402013-03-02 22:36:28 +010050if [ "$KBUILD_EXTMOD" != "" ] ; then
Nicolas Palix93f14462013-06-20 13:10:56 +020051 OPTIONS="--patch $srctree $OPTIONS"
Nicolas Palixbad6a402013-03-02 22:36:28 +010052fi
53
Nicolas Palix74425ee2010-06-06 17:15:01 +020054if [ ! -x "$SPATCH" ]; then
55 echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
56 exit 1
57fi
58
59if [ "$MODE" = "" ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +020060 if [ "$ONLINE" = "0" ] ; then
Nicolas Palix1f0a6742013-06-06 23:39:52 +020061 echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
62 echo 'Available modes are the following: patch, report, context, org'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020063 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
Nicolas Palix1f0a6742013-06-06 23:39:52 +020064 echo 'Note however that some modes are not implemented by some semantic patches.'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020065 fi
Nicolas Palix1f0a6742013-06-06 23:39:52 +020066 MODE="report"
67fi
68
69if [ "$MODE" = "chain" ] ; then
70 if [ "$ONLINE" = "0" ] ; then
71 echo 'You have selected the "chain" mode.'
72 echo 'All available modes will be tried (in that order): patch, report, context, org'
73 fi
Nicolas Palix03ee0c42010-10-08 21:27:41 +020074elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
Deepa Dinamani7a2358b2016-06-12 12:04:39 -070075 FLAGS="--no-show-diff $FLAGS"
Nicolas Palix74425ee2010-06-06 17:15:01 +020076fi
77
Nicolas Palix1e9dea22010-06-13 09:26:34 +020078if [ "$ONLINE" = "0" ] ; then
79 echo ''
80 echo 'Please check for false positives in the output before submitting a patch.'
81 echo 'When using "patch" mode, carefully review the patch before submitting it.'
82 echo ''
83fi
Nicolas Palix74425ee2010-06-06 17:15:01 +020084
Bernd Schubert53032652013-01-29 17:03:42 +010085run_cmd() {
Kees Cook90d06a42013-06-18 14:49:29 -070086 local i
Bernd Schubert53032652013-01-29 17:03:42 +010087 if [ $VERBOSE -ne 0 ] ; then
Kees Cook90d06a42013-06-18 14:49:29 -070088 echo "Running ($NPROC in parallel): $@"
Bernd Schubert53032652013-01-29 17:03:42 +010089 fi
Kees Cook90d06a42013-06-18 14:49:29 -070090 for i in $(seq 0 $(( NPROC - 1)) ); do
Nicolas Palix93f14462013-06-20 13:10:56 +020091 eval "$@ --max $NPROC --index $i &"
Kees Cook90d06a42013-06-18 14:49:29 -070092 SPATCH_PID[$i]=$!
93 if [ $VERBOSE -eq 2 ] ; then
94 echo "${SPATCH_PID[$i]} running"
95 fi
96 done
97 wait
Bernd Schubert53032652013-01-29 17:03:42 +010098}
99
Kees Cook90d06a42013-06-18 14:49:29 -0700100kill_running() {
Kees Cook2552a392016-05-16 05:55:58 -0700101 for i in $(seq 0 $(( NPROC - 1 )) ); do
Kees Cook90d06a42013-06-18 14:49:29 -0700102 if [ $VERBOSE -eq 2 ] ; then
103 echo "Killing ${SPATCH_PID[$i]}"
104 fi
105 kill ${SPATCH_PID[$i]} 2>/dev/null
106 done
107}
Bernd Schubert53032652013-01-29 17:03:42 +0100108
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200109coccinelle () {
Nicolas Palix74425ee2010-06-06 17:15:01 +0200110 COCCI="$1"
Nicolas Palix74425ee2010-06-06 17:15:01 +0200111
112 OPT=`grep "Option" $COCCI | cut -d':' -f2`
Nicolas Palix74425ee2010-06-06 17:15:01 +0200113
Nicolas Palix93f14462013-06-20 13:10:56 +0200114# The option '--parse-cocci' can be used to syntactically check the SmPL files.
Nicolas Palix74425ee2010-06-06 17:15:01 +0200115#
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200116# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
Nicolas Palix74425ee2010-06-06 17:15:01 +0200117
Nicolas Palix35d88a32013-03-02 22:36:25 +0100118 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200119
120 FILE=`echo $COCCI | sed "s|$srctree/||"`
121
Nicolas Palix3c908412010-10-08 21:27:38 +0200122 echo "Processing `basename $COCCI`"
123 echo "with option(s) \"$OPT\""
124 echo ''
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200125 echo 'Message example to submit a patch:'
126
Nicolas Palix3c908412010-10-08 21:27:38 +0200127 sed -ne 's|^///||p' $COCCI
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200128
Nicolas Palix062c1822010-10-24 23:37:34 +0200129 if [ "$MODE" = "patch" ] ; then
130 echo ' The semantic patch that makes this change is available'
131 elif [ "$MODE" = "report" ] ; then
132 echo ' The semantic patch that makes this report is available'
133 elif [ "$MODE" = "context" ] ; then
134 echo ' The semantic patch that spots this code is available'
135 elif [ "$MODE" = "org" ] ; then
136 echo ' The semantic patch that makes this Org report is available'
137 else
138 echo ' The semantic patch that makes this output is available'
139 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200140 echo " in $FILE."
141 echo ''
142 echo ' More information about semantic patching is available at'
143 echo ' http://coccinelle.lip6.fr/'
144 echo ''
145
Nicolas Palix3c908412010-10-08 21:27:38 +0200146 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
147 echo 'Semantic patch information:'
148 sed -ne 's|^//#||p' $COCCI
149 echo ''
150 fi
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200151 fi
Nicolas Palix3c908412010-10-08 21:27:38 +0200152
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200153 if [ "$MODE" = "chain" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100154 run_cmd $SPATCH -D patch \
Nicolas Palix93f14462013-06-20 13:10:56 +0200155 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
Bernd Schubert53032652013-01-29 17:03:42 +0100156 run_cmd $SPATCH -D report \
Nicolas Palix93f14462013-06-20 13:10:56 +0200157 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
Bernd Schubert53032652013-01-29 17:03:42 +0100158 run_cmd $SPATCH -D context \
Nicolas Palix93f14462013-06-20 13:10:56 +0200159 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
Bernd Schubert53032652013-01-29 17:03:42 +0100160 run_cmd $SPATCH -D org \
Nicolas Palix93f14462013-06-20 13:10:56 +0200161 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
Nicolas Palixc05cd6d2012-09-20 22:30:46 +0200162 elif [ "$MODE" = "rep+ctxt" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100163 run_cmd $SPATCH -D report \
Nicolas Palix93f14462013-06-20 13:10:56 +0200164 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
Bernd Schubert53032652013-01-29 17:03:42 +0100165 run_cmd $SPATCH -D context \
Nicolas Palix93f14462013-06-20 13:10:56 +0200166 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200167 else
Nicolas Palix93f14462013-06-20 13:10:56 +0200168 run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200169 fi
170
Nicolas Palix74425ee2010-06-06 17:15:01 +0200171}
172
173if [ "$COCCI" = "" ] ; then
174 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200175 coccinelle $f
Nicolas Palix74425ee2010-06-06 17:15:01 +0200176 done
177else
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200178 coccinelle $COCCI
Nicolas Palix74425ee2010-06-06 17:15:01 +0200179fi