coccicheck: add support for DEBUG_FILE
[linux-2.6-block.git] / scripts / coccicheck
CommitLineData
9e395550 1#!/bin/bash
74425eee 2
ec97946e
NP
3#
4# This script requires at least spatch
5# version 1.0.0-rc11.
6#
7
74425eee
NP
8SPATCH="`which ${SPATCH:=spatch}`"
9
13d94865
LR
10if [ ! -x "$SPATCH" ]; then
11 echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
12 exit 1
13fi
14
c930a1b2
LR
15USE_JOBS="no"
16$SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
90d06a46 17
26e56720
BS
18# The verbosity may be set by the environmental parameter V=
19# as for example with 'make V=1 coccicheck'
20
21if [ -n "$V" -a "$V" != "0" ]; then
90d06a46 22 VERBOSE="$V"
26e56720
BS
23else
24 VERBOSE=0
25fi
26
90d06a46
KC
27if [ -z "$J" ]; then
28 NPROC=$(getconf _NPROCESSORS_ONLN)
29else
30 NPROC="$J"
31fi
32
8e826ad5 33FLAGS="--very-quiet"
9e395550
NP
34
35# spatch only allows include directories with the syntax "-I include"
36# while gcc also allows "-Iinclude" and "-include include"
37COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
5b169108 38COCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
9e395550 39
1e9dea2a
NP
40if [ "$C" = "1" -o "$C" = "2" ]; then
41 ONLINE=1
42
9e395550
NP
43 # Take only the last argument, which is the C file to test
44 shift $(( $# - 1 ))
45 OPTIONS="$COCCIINCLUDE $1"
1e9dea2a
NP
46else
47 ONLINE=0
d0bc1fb4 48 if [ "$KBUILD_EXTMOD" = "" ] ; then
93f14468 49 OPTIONS="--dir $srctree $COCCIINCLUDE"
d0bc1fb4 50 else
93f14468 51 OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
d0bc1fb4 52 fi
1e9dea2a
NP
53fi
54
bad6a409 55if [ "$KBUILD_EXTMOD" != "" ] ; then
93f14468 56 OPTIONS="--patch $srctree $OPTIONS"
bad6a409
NP
57fi
58
c930a1b2
LR
59# You can override by using SPFLAGS
60if [ "$USE_JOBS" = "no" ]; then
61 trap kill_running SIGTERM SIGINT
62 declare -a SPATCH_PID
63elif [ "$NPROC" != "1" ]; then
64 # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
65 # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
66 OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
67fi
68
74425eee 69if [ "$MODE" = "" ] ; then
1e9dea2a 70 if [ "$ONLINE" = "0" ] ; then
1f0a6742
NP
71 echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
72 echo 'Available modes are the following: patch, report, context, org'
1e9dea2a 73 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
1f0a6742
NP
74 echo 'Note however that some modes are not implemented by some semantic patches.'
75 fi
76 MODE="report"
77fi
78
79if [ "$MODE" = "chain" ] ; then
80 if [ "$ONLINE" = "0" ] ; then
81 echo 'You have selected the "chain" mode.'
82 echo 'All available modes will be tried (in that order): patch, report, context, org'
1e9dea2a 83 fi
03ee0c42 84elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
7a2358b3 85 FLAGS="--no-show-diff $FLAGS"
74425eee
NP
86fi
87
1e9dea2a
NP
88if [ "$ONLINE" = "0" ] ; then
89 echo ''
90 echo 'Please check for false positives in the output before submitting a patch.'
91 echo 'When using "patch" mode, carefully review the patch before submitting it.'
92 echo ''
93fi
74425eee 94
c930a1b2
LR
95run_cmd_parmap() {
96 if [ $VERBOSE -ne 0 ] ; then
97 echo "Running ($NPROC in parallel): $@"
98 fi
be1fa900
LR
99 if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then
100 if [ -f $DEBUG_FILE ]; then
101 echo "Debug file $DEBUG_FILE exists, bailing"
102 exit
103 fi
104 else
105 DEBUG_FILE="/dev/null"
106 fi
107 $@ 2>$DEBUG_FILE
c930a1b2
LR
108 if [[ $? -ne 0 ]]; then
109 echo "coccicheck failed"
110 exit $?
111 fi
112}
113
114run_cmd_old() {
90d06a46 115 local i
5303265a 116 if [ $VERBOSE -ne 0 ] ; then
90d06a46 117 echo "Running ($NPROC in parallel): $@"
5303265a 118 fi
90d06a46 119 for i in $(seq 0 $(( NPROC - 1)) ); do
93f14468 120 eval "$@ --max $NPROC --index $i &"
90d06a46
KC
121 SPATCH_PID[$i]=$!
122 if [ $VERBOSE -eq 2 ] ; then
123 echo "${SPATCH_PID[$i]} running"
124 fi
125 done
126 wait
5303265a
BS
127}
128
c930a1b2
LR
129run_cmd() {
130 if [ "$USE_JOBS" = "yes" ]; then
131 run_cmd_parmap $@
132 else
133 run_cmd_old $@
134 fi
135}
136
90d06a46 137kill_running() {
2552a39f 138 for i in $(seq 0 $(( NPROC - 1 )) ); do
90d06a46
KC
139 if [ $VERBOSE -eq 2 ] ; then
140 echo "Killing ${SPATCH_PID[$i]}"
141 fi
142 kill ${SPATCH_PID[$i]} 2>/dev/null
143 done
144}
5303265a 145
8e826ad5
LR
146# You can override heuristics with SPFLAGS, these must always go last
147OPTIONS="$OPTIONS $SPFLAGS"
148
1e9dea2a 149coccinelle () {
74425eee 150 COCCI="$1"
74425eee
NP
151
152 OPT=`grep "Option" $COCCI | cut -d':' -f2`
74425eee 153
93f14468 154# The option '--parse-cocci' can be used to syntactically check the SmPL files.
1e9dea2a
NP
155#
156# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
74425eee 157
35d88a38 158 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
74425eee 159
1e9dea2a 160 FILE=`echo $COCCI | sed "s|$srctree/||"`
74425eee 161
3c908417
NP
162 echo "Processing `basename $COCCI`"
163 echo "with option(s) \"$OPT\""
164 echo ''
1e9dea2a
NP
165 echo 'Message example to submit a patch:'
166
3c908417 167 sed -ne 's|^///||p' $COCCI
1e9dea2a 168
062c1825
NP
169 if [ "$MODE" = "patch" ] ; then
170 echo ' The semantic patch that makes this change is available'
171 elif [ "$MODE" = "report" ] ; then
172 echo ' The semantic patch that makes this report is available'
173 elif [ "$MODE" = "context" ] ; then
174 echo ' The semantic patch that spots this code is available'
175 elif [ "$MODE" = "org" ] ; then
176 echo ' The semantic patch that makes this Org report is available'
177 else
178 echo ' The semantic patch that makes this output is available'
179 fi
1e9dea2a
NP
180 echo " in $FILE."
181 echo ''
182 echo ' More information about semantic patching is available at'
183 echo ' http://coccinelle.lip6.fr/'
184 echo ''
185
3c908417
NP
186 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
187 echo 'Semantic patch information:'
188 sed -ne 's|^//#||p' $COCCI
189 echo ''
190 fi
2c1160c8 191 fi
3c908417 192
2c1160c8 193 if [ "$MODE" = "chain" ] ; then
5303265a 194 run_cmd $SPATCH -D patch \
93f14468 195 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
5303265a 196 run_cmd $SPATCH -D report \
93f14468 197 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
5303265a 198 run_cmd $SPATCH -D context \
93f14468 199 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
5303265a 200 run_cmd $SPATCH -D org \
93f14468 201 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
c05cd6dd 202 elif [ "$MODE" = "rep+ctxt" ] ; then
5303265a 203 run_cmd $SPATCH -D report \
93f14468 204 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
5303265a 205 run_cmd $SPATCH -D context \
93f14468 206 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
1e9dea2a 207 else
93f14468 208 run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
1e9dea2a 209 fi
74425eee 210
74425eee
NP
211}
212
213if [ "$COCCI" = "" ] ; then
214 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1e9dea2a 215 coccinelle $f
74425eee
NP
216 done
217else
1e9dea2a 218 coccinelle $COCCI
74425eee 219fi