blob: 85d31899ad9869620984a388a09b1eac78bcb084 [file] [log] [blame]
Nicolas Palix74425ee2010-06-06 17:15:01 +02001#!/bin/sh
2
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 Palix1e9dea22010-06-13 09:26:34 +020014if [ "$C" = "1" -o "$C" = "2" ]; then
15 ONLINE=1
16
17# This requires Coccinelle >= 0.2.3
18# FLAGS="-ignore_unknown_options -very_quiet"
19# OPTIONS=$*
20
Greg Dietsche42f1c012012-01-20 17:10:35 -060021# Workaround for Coccinelle < 0.2.3
22 FLAGS="-I $srctree/include -very_quiet"
23 shift $(( $# - 1 ))
24 OPTIONS=$1
Nicolas Palix1e9dea22010-06-13 09:26:34 +020025else
26 ONLINE=0
27 FLAGS="-very_quiet"
Greg Dietsched0bc1fb2011-11-05 20:59:43 -050028 if [ "$KBUILD_EXTMOD" = "" ] ; then
29 OPTIONS="-dir $srctree"
30 else
31 OPTIONS="-dir $KBUILD_EXTMOD -patch $srctree -I $srctree/include -I $KBUILD_EXTMOD/include"
32 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +020033fi
34
Nicolas Palix74425ee2010-06-06 17:15:01 +020035if [ ! -x "$SPATCH" ]; then
36 echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
37 exit 1
38fi
39
40if [ "$MODE" = "" ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +020041 if [ "$ONLINE" = "0" ] ; then
Nicolas Palix2c1160c82010-10-08 21:27:40 +020042 echo 'You have not explicitly specified the mode to use. Using default "chain" mode.'
43 echo 'All available modes will be tried (in that order): patch, report, context, org'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020044 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
Nicolas Palix1e9dea22010-06-13 09:26:34 +020045 fi
Nicolas Palix2c1160c82010-10-08 21:27:40 +020046 MODE="chain"
Nicolas Palix03ee0c42010-10-08 21:27:41 +020047elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
Nicolas Palix062c1822010-10-24 23:37:34 +020048 FLAGS="$FLAGS -no_show_diff"
Nicolas Palix74425ee2010-06-06 17:15:01 +020049fi
50
Nicolas Palix1e9dea22010-06-13 09:26:34 +020051if [ "$ONLINE" = "0" ] ; then
52 echo ''
53 echo 'Please check for false positives in the output before submitting a patch.'
54 echo 'When using "patch" mode, carefully review the patch before submitting it.'
55 echo ''
56fi
Nicolas Palix74425ee2010-06-06 17:15:01 +020057
Bernd Schubert53032652013-01-29 17:03:42 +010058run_cmd() {
59 if [ $VERBOSE -ne 0 ] ; then
60 echo "Running: $@"
61 fi
62 eval $@
63}
64
65
Nicolas Palix1e9dea22010-06-13 09:26:34 +020066coccinelle () {
Nicolas Palix74425ee2010-06-06 17:15:01 +020067 COCCI="$1"
Nicolas Palix74425ee2010-06-06 17:15:01 +020068
69 OPT=`grep "Option" $COCCI | cut -d':' -f2`
Nicolas Palix74425ee2010-06-06 17:15:01 +020070
Nicolas Palix062c1822010-10-24 23:37:34 +020071# The option '-parse_cocci' can be used to syntactically check the SmPL files.
Nicolas Palix74425ee2010-06-06 17:15:01 +020072#
Nicolas Palix1e9dea22010-06-13 09:26:34 +020073# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
Nicolas Palix74425ee2010-06-06 17:15:01 +020074
Bernd Schubert26e56722013-01-29 17:03:37 +010075 if [ $VERBOSE -ne 0 ] ; then
Nicolas Palix1e9dea22010-06-13 09:26:34 +020076
77 FILE=`echo $COCCI | sed "s|$srctree/||"`
78
Nicolas Palix3c908412010-10-08 21:27:38 +020079 echo "Processing `basename $COCCI`"
80 echo "with option(s) \"$OPT\""
81 echo ''
Nicolas Palix1e9dea22010-06-13 09:26:34 +020082 echo 'Message example to submit a patch:'
83
Nicolas Palix3c908412010-10-08 21:27:38 +020084 sed -ne 's|^///||p' $COCCI
Nicolas Palix1e9dea22010-06-13 09:26:34 +020085
Nicolas Palix062c1822010-10-24 23:37:34 +020086 if [ "$MODE" = "patch" ] ; then
87 echo ' The semantic patch that makes this change is available'
88 elif [ "$MODE" = "report" ] ; then
89 echo ' The semantic patch that makes this report is available'
90 elif [ "$MODE" = "context" ] ; then
91 echo ' The semantic patch that spots this code is available'
92 elif [ "$MODE" = "org" ] ; then
93 echo ' The semantic patch that makes this Org report is available'
94 else
95 echo ' The semantic patch that makes this output is available'
96 fi
Nicolas Palix1e9dea22010-06-13 09:26:34 +020097 echo " in $FILE."
98 echo ''
99 echo ' More information about semantic patching is available at'
100 echo ' http://coccinelle.lip6.fr/'
101 echo ''
102
Nicolas Palix3c908412010-10-08 21:27:38 +0200103 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
104 echo 'Semantic patch information:'
105 sed -ne 's|^//#||p' $COCCI
106 echo ''
107 fi
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200108 fi
Nicolas Palix3c908412010-10-08 21:27:38 +0200109
Nicolas Palix2c1160c82010-10-08 21:27:40 +0200110 if [ "$MODE" = "chain" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100111 run_cmd $SPATCH -D patch \
112 $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
113 run_cmd $SPATCH -D report \
114 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
115 run_cmd $SPATCH -D context \
116 $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
117 run_cmd $SPATCH -D org \
118 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
Nicolas Palixc05cd6d2012-09-20 22:30:46 +0200119 elif [ "$MODE" = "rep+ctxt" ] ; then
Bernd Schubert53032652013-01-29 17:03:42 +0100120 run_cmd $SPATCH -D report \
121 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
122 run_cmd $SPATCH -D context \
123 $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200124 else
Bernd Schubert53032652013-01-29 17:03:42 +0100125 run_cmd $SPATCH -D $MODE $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200126 fi
127
Nicolas Palix74425ee2010-06-06 17:15:01 +0200128}
129
130if [ "$COCCI" = "" ] ; then
131 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200132 coccinelle $f
Nicolas Palix74425ee2010-06-06 17:15:01 +0200133 done
134else
Nicolas Palix1e9dea22010-06-13 09:26:34 +0200135 coccinelle $COCCI
Nicolas Palix74425ee2010-06-06 17:15:01 +0200136fi