coccicheck: make SPFLAGS more useful
[cascardo/linux.git] / scripts / coccicheck
1 #!/bin/bash
2
3 #
4 # This script requires at least spatch
5 # version 1.0.0-rc11.
6 #
7
8 SPATCH="`which ${SPATCH:=spatch}`"
9
10 if [ ! -x "$SPATCH" ]; then
11     echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
12     exit 1
13 fi
14
15 trap kill_running SIGTERM SIGINT
16 declare -a SPATCH_PID
17
18 # The verbosity may be set by the environmental parameter V=
19 # as for example with 'make V=1 coccicheck'
20
21 if [ -n "$V" -a "$V" != "0" ]; then
22         VERBOSE="$V"
23 else
24         VERBOSE=0
25 fi
26
27 if [ -z "$J" ]; then
28         NPROC=$(getconf _NPROCESSORS_ONLN)
29 else
30         NPROC="$J"
31 fi
32
33 FLAGS="--very-quiet"
34
35 # spatch only allows include directories with the syntax "-I include"
36 # while gcc also allows "-Iinclude" and "-include include"
37 COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
38 COCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
39
40 if [ "$C" = "1" -o "$C" = "2" ]; then
41     ONLINE=1
42
43     # Take only the last argument, which is the C file to test
44     shift $(( $# - 1 ))
45     OPTIONS="$COCCIINCLUDE $1"
46 else
47     ONLINE=0
48     if [ "$KBUILD_EXTMOD" = "" ] ; then
49         OPTIONS="--dir $srctree $COCCIINCLUDE"
50     else
51         OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
52     fi
53 fi
54
55 if [ "$KBUILD_EXTMOD" != "" ] ; then
56     OPTIONS="--patch $srctree $OPTIONS"
57 fi
58
59 if [ "$MODE" = "" ] ; then
60     if [ "$ONLINE" = "0" ] ; then
61         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'
63         echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
64         echo 'Note however that some modes are not implemented by some semantic patches.'
65     fi
66     MODE="report"
67 fi
68
69 if [ "$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
74 elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
75     FLAGS="--no-show-diff $FLAGS"
76 fi
77
78 if [ "$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 ''
83 fi
84
85 run_cmd() {
86         local i
87         if [ $VERBOSE -ne 0 ] ; then
88                 echo "Running ($NPROC in parallel): $@"
89         fi
90         for i in $(seq 0 $(( NPROC - 1)) ); do
91                 eval "$@ --max $NPROC --index $i &"
92                 SPATCH_PID[$i]=$!
93                 if [ $VERBOSE -eq 2 ] ; then
94                         echo "${SPATCH_PID[$i]} running"
95                 fi
96         done
97         wait
98 }
99
100 kill_running() {
101         for i in $(seq 0 $(( NPROC - 1 )) ); do
102                 if [ $VERBOSE -eq 2 ] ; then
103                         echo "Killing ${SPATCH_PID[$i]}"
104                 fi
105                 kill ${SPATCH_PID[$i]} 2>/dev/null
106         done
107 }
108
109 # You can override heuristics with SPFLAGS, these must always go last
110 OPTIONS="$OPTIONS $SPFLAGS"
111
112 coccinelle () {
113     COCCI="$1"
114
115     OPT=`grep "Option" $COCCI | cut -d':' -f2`
116
117 #   The option '--parse-cocci' can be used to syntactically check the SmPL files.
118 #
119 #    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
120
121     if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
122
123         FILE=`echo $COCCI | sed "s|$srctree/||"`
124
125         echo "Processing `basename $COCCI`"
126         echo "with option(s) \"$OPT\""
127         echo ''
128         echo 'Message example to submit a patch:'
129
130         sed -ne 's|^///||p' $COCCI
131
132         if [ "$MODE" = "patch" ] ; then
133             echo ' The semantic patch that makes this change is available'
134         elif [ "$MODE" = "report" ] ; then
135             echo ' The semantic patch that makes this report is available'
136         elif [ "$MODE" = "context" ] ; then
137             echo ' The semantic patch that spots this code is available'
138         elif [ "$MODE" = "org" ] ; then
139             echo ' The semantic patch that makes this Org report is available'
140         else
141             echo ' The semantic patch that makes this output is available'
142         fi
143         echo " in $FILE."
144         echo ''
145         echo ' More information about semantic patching is available at'
146         echo ' http://coccinelle.lip6.fr/'
147         echo ''
148
149         if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
150             echo 'Semantic patch information:'
151             sed -ne 's|^//#||p' $COCCI
152             echo ''
153         fi
154     fi
155
156     if [ "$MODE" = "chain" ] ; then
157         run_cmd $SPATCH -D patch   \
158                 $FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
159         run_cmd $SPATCH -D report  \
160                 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
161         run_cmd $SPATCH -D context \
162                 $FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
163         run_cmd $SPATCH -D org     \
164                 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
165     elif [ "$MODE" = "rep+ctxt" ] ; then
166         run_cmd $SPATCH -D report  \
167                 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
168         run_cmd $SPATCH -D context \
169                 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
170     else
171         run_cmd $SPATCH -D $MODE   $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
172     fi
173
174 }
175
176 if [ "$COCCI" = "" ] ; then
177     for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
178         coccinelle $f
179     done
180 else
181     coccinelle $COCCI
182 fi