Coccinelle: Add support to the SPFLAGS variable
[linux-block.git] / scripts / coccicheck
CommitLineData
9e395550 1#!/bin/bash
74425eee
NP
2
3SPATCH="`which ${SPATCH:=spatch}`"
4
26e56720
BS
5# 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
ed621cc4 14FLAGS="$SPFLAGS -very_quiet"
9e395550
NP
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
1e9dea2a
NP
21if [ "$C" = "1" -o "$C" = "2" ]; then
22 ONLINE=1
23
9e395550
NP
24 # Take only the last argument, which is the C file to test
25 shift $(( $# - 1 ))
26 OPTIONS="$COCCIINCLUDE $1"
1e9dea2a
NP
27else
28 ONLINE=0
d0bc1fb4 29 if [ "$KBUILD_EXTMOD" = "" ] ; then
9e395550 30 OPTIONS="-dir $srctree $COCCIINCLUDE"
d0bc1fb4 31 else
9e395550 32 OPTIONS="-dir $KBUILD_EXTMOD -patch $srctree $COCCIINCLUDE"
d0bc1fb4 33 fi
1e9dea2a
NP
34fi
35
74425eee
NP
36if [ ! -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
1e9dea2a 42 if [ "$ONLINE" = "0" ] ; then
2c1160c8
NP
43 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'
1e9dea2a 45 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
1e9dea2a 46 fi
2c1160c8 47 MODE="chain"
03ee0c42 48elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
062c1825 49 FLAGS="$FLAGS -no_show_diff"
74425eee
NP
50fi
51
1e9dea2a
NP
52if [ "$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
74425eee 58
5303265a
BS
59run_cmd() {
60 if [ $VERBOSE -ne 0 ] ; then
61 echo "Running: $@"
62 fi
63 eval $@
64}
65
66
1e9dea2a 67coccinelle () {
74425eee 68 COCCI="$1"
74425eee
NP
69
70 OPT=`grep "Option" $COCCI | cut -d':' -f2`
74425eee 71
062c1825 72# The option '-parse_cocci' can be used to syntactically check the SmPL files.
1e9dea2a
NP
73#
74# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
74425eee 75
35d88a38 76 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
74425eee 77
1e9dea2a 78 FILE=`echo $COCCI | sed "s|$srctree/||"`
74425eee 79
3c908417
NP
80 echo "Processing `basename $COCCI`"
81 echo "with option(s) \"$OPT\""
82 echo ''
1e9dea2a
NP
83 echo 'Message example to submit a patch:'
84
3c908417 85 sed -ne 's|^///||p' $COCCI
1e9dea2a 86
062c1825
NP
87 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
1e9dea2a
NP
98 echo " in $FILE."
99 echo ''
100 echo ' More information about semantic patching is available at'
101 echo ' http://coccinelle.lip6.fr/'
102 echo ''
103
3c908417
NP
104 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
105 echo 'Semantic patch information:'
106 sed -ne 's|^//#||p' $COCCI
107 echo ''
108 fi
2c1160c8 109 fi
3c908417 110
2c1160c8 111 if [ "$MODE" = "chain" ] ; then
5303265a
BS
112 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
c05cd6dd 120 elif [ "$MODE" = "rep+ctxt" ] ; then
5303265a
BS
121 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
1e9dea2a 125 else
5303265a 126 run_cmd $SPATCH -D $MODE $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1e9dea2a 127 fi
74425eee 128
74425eee
NP
129}
130
131if [ "$COCCI" = "" ] ; then
132 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1e9dea2a 133 coccinelle $f
74425eee
NP
134 done
135else
1e9dea2a 136 coccinelle $COCCI
74425eee 137fi