ublk_drv: add device parameter UBLK_PARAM_TYPE_DEVT
[linux-block.git] / scripts / coccicheck
CommitLineData
9e395550 1#!/bin/bash
b2441318 2# SPDX-License-Identifier: GPL-2.0
c100d537
LR
3# Linux kernel coccicheck
4#
1e01892e 5# Read Documentation/dev-tools/coccinelle.rst
ec97946e
NP
6#
7# This script requires at least spatch
8# version 1.0.0-rc11.
ec97946e 9
a9e064c0 10DIR="$(dirname $(readlink -f $0))/.."
74425eee
NP
11SPATCH="`which ${SPATCH:=spatch}`"
12
13d94865
LR
13if [ ! -x "$SPATCH" ]; then
14 echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
15 exit 1
16fi
17
a9e064c0 18SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
a9e064c0 19
c930a1b2
LR
20USE_JOBS="no"
21$SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
90d06a46 22
26e56720
BS
23# The verbosity may be set by the environmental parameter V=
24# as for example with 'make V=1 coccicheck'
25
26if [ -n "$V" -a "$V" != "0" ]; then
90d06a46 27 VERBOSE="$V"
26e56720
BS
28else
29 VERBOSE=0
30fi
31
8e826ad5 32FLAGS="--very-quiet"
9e395550 33
5c384dba
LR
34# You can use SPFLAGS to append extra arguments to coccicheck or override any
35# heuristics done in this file as Coccinelle accepts the last options when
36# options conflict.
37#
38# A good example for use of SPFLAGS is if you want to debug your cocci script,
39# you can for instance use the following:
40#
41# $ export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci
42# $ make coccicheck MODE=report DEBUG_FILE="all.err" SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c
43#
44# "--show-trying" should show you what rule is being processed as it goes to
45# stdout, you do not need a debug file for that. The profile output will be
46# be sent to stdout, if you provide a DEBUG_FILE the profiling data can be
47# inspected there.
48#
49# --profile will not output if --very-quiet is used, so avoid it.
2d63e6a3 50echo $SPFLAGS | grep -E -e "--profile|--show-trying" 2>&1 > /dev/null
5c384dba
LR
51if [ $? -eq 0 ]; then
52 FLAGS="--quiet"
53fi
54
9e395550
NP
55# spatch only allows include directories with the syntax "-I include"
56# while gcc also allows "-Iinclude" and "-include include"
57COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
5b169108 58COCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
9e395550 59
1e9dea2a
NP
60if [ "$C" = "1" -o "$C" = "2" ]; then
61 ONLINE=1
62
d8f6e5c6
SP
63 if [[ $# -le 0 ]]; then
64 echo ''
65 echo 'Specifying both the variable "C" and rule "coccicheck" in the make
66command results in a shift count error.'
67 echo ''
68 echo 'Try specifying "scripts/coccicheck" as a value for the CHECK variable instead.'
69 echo ''
70 echo 'Example: make C=2 CHECK=scripts/coccicheck drivers/net/ethernet/ethoc.o'
71 echo ''
72 exit 1
73 fi
74
9e395550
NP
75 # Take only the last argument, which is the C file to test
76 shift $(( $# - 1 ))
77 OPTIONS="$COCCIINCLUDE $1"
d7059ca0
MY
78
79 # No need to parallelize Coccinelle since this mode takes one input file.
80 NPROC=1
1e9dea2a
NP
81else
82 ONLINE=0
d0bc1fb4 83 if [ "$KBUILD_EXTMOD" = "" ] ; then
93f14468 84 OPTIONS="--dir $srctree $COCCIINCLUDE"
d0bc1fb4 85 else
93f14468 86 OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
d0bc1fb4 87 fi
d7059ca0 88
e16a7c47 89 # Use only one thread per core by default if hyperthreading is enabled
f5b3553b 90 THREADS_PER_CORE=$(LANG=C lscpu | grep "Thread(s) per core: " | tr -cd "[:digit:]")
d7059ca0
MY
91 if [ -z "$J" ]; then
92 NPROC=$(getconf _NPROCESSORS_ONLN)
c5864560 93 if [ $THREADS_PER_CORE -gt 1 -a $NPROC -gt 4 ] ; then
e16a7c47
SP
94 NPROC=$((NPROC/2))
95 fi
d7059ca0
MY
96 else
97 NPROC="$J"
98 fi
1e9dea2a
NP
99fi
100
bad6a409 101if [ "$KBUILD_EXTMOD" != "" ] ; then
93f14468 102 OPTIONS="--patch $srctree $OPTIONS"
bad6a409
NP
103fi
104
c930a1b2
LR
105# You can override by using SPFLAGS
106if [ "$USE_JOBS" = "no" ]; then
107 trap kill_running SIGTERM SIGINT
108 declare -a SPATCH_PID
109elif [ "$NPROC" != "1" ]; then
110 # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
111 # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
112 OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
113fi
114
74425eee 115if [ "$MODE" = "" ] ; then
1e9dea2a 116 if [ "$ONLINE" = "0" ] ; then
1f0a6742 117 echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
4b2bd20c 118 echo 'Available modes are the following: patch, report, context, org, chain'
1e9dea2a 119 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
1f0a6742
NP
120 echo 'Note however that some modes are not implemented by some semantic patches.'
121 fi
122 MODE="report"
123fi
124
125if [ "$MODE" = "chain" ] ; then
126 if [ "$ONLINE" = "0" ] ; then
127 echo 'You have selected the "chain" mode.'
128 echo 'All available modes will be tried (in that order): patch, report, context, org'
1e9dea2a 129 fi
03ee0c42 130elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
7a2358b3 131 FLAGS="--no-show-diff $FLAGS"
74425eee
NP
132fi
133
1e9dea2a
NP
134if [ "$ONLINE" = "0" ] ; then
135 echo ''
136 echo 'Please check for false positives in the output before submitting a patch.'
137 echo 'When using "patch" mode, carefully review the patch before submitting it.'
138 echo ''
139fi
74425eee 140
c930a1b2
LR
141run_cmd_parmap() {
142 if [ $VERBOSE -ne 0 ] ; then
143 echo "Running ($NPROC in parallel): $@"
144 fi
a19d1358
SP
145 if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then
146 echo $@>>$DEBUG_FILE
147 $@ 2>>$DEBUG_FILE
148 else
149 echo $@
150 $@ 2>&1
151 fi
152
512ddf7d
DE
153 err=$?
154 if [[ $err -ne 0 ]]; then
c930a1b2 155 echo "coccicheck failed"
512ddf7d 156 exit $err
c930a1b2
LR
157 fi
158}
159
160run_cmd_old() {
90d06a46 161 local i
5303265a 162 if [ $VERBOSE -ne 0 ] ; then
90d06a46 163 echo "Running ($NPROC in parallel): $@"
5303265a 164 fi
90d06a46 165 for i in $(seq 0 $(( NPROC - 1)) ); do
93f14468 166 eval "$@ --max $NPROC --index $i &"
90d06a46
KC
167 SPATCH_PID[$i]=$!
168 if [ $VERBOSE -eq 2 ] ; then
169 echo "${SPATCH_PID[$i]} running"
170 fi
171 done
172 wait
5303265a
BS
173}
174
c930a1b2
LR
175run_cmd() {
176 if [ "$USE_JOBS" = "yes" ]; then
177 run_cmd_parmap $@
178 else
179 run_cmd_old $@
180 fi
181}
182
90d06a46 183kill_running() {
2552a39f 184 for i in $(seq 0 $(( NPROC - 1 )) ); do
90d06a46
KC
185 if [ $VERBOSE -eq 2 ] ; then
186 echo "Killing ${SPATCH_PID[$i]}"
187 fi
188 kill ${SPATCH_PID[$i]} 2>/dev/null
189 done
190}
5303265a 191
8e826ad5
LR
192# You can override heuristics with SPFLAGS, these must always go last
193OPTIONS="$OPTIONS $SPFLAGS"
194
1e9dea2a 195coccinelle () {
74425eee 196 COCCI="$1"
74425eee 197
e0be348e
MY
198 OPT=`grep "Options:" $COCCI | cut -d':' -f2`
199 REQ=`grep "Requires:" $COCCI | cut -d':' -f2 | sed "s| ||"`
33114c43
MY
200 if [ -n "$REQ" ] && ! { echo "$REQ"; echo "$SPATCH_VERSION"; } | sort -CV ; then
201 echo "Skipping coccinelle SmPL patch: $COCCI"
202 echo "You have coccinelle: $SPATCH_VERSION"
203 echo "This SmPL patch requires: $REQ"
204 return
a9e064c0 205 fi
74425eee 206
93f14468 207# The option '--parse-cocci' can be used to syntactically check the SmPL files.
1e9dea2a
NP
208#
209# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
74425eee 210
35d88a38 211 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
74425eee 212
cd1af7cf 213 FILE=${COCCI#$srctree/}
74425eee 214
3c908417
NP
215 echo "Processing `basename $COCCI`"
216 echo "with option(s) \"$OPT\""
217 echo ''
1e9dea2a
NP
218 echo 'Message example to submit a patch:'
219
3c908417 220 sed -ne 's|^///||p' $COCCI
1e9dea2a 221
062c1825
NP
222 if [ "$MODE" = "patch" ] ; then
223 echo ' The semantic patch that makes this change is available'
224 elif [ "$MODE" = "report" ] ; then
225 echo ' The semantic patch that makes this report is available'
226 elif [ "$MODE" = "context" ] ; then
227 echo ' The semantic patch that spots this code is available'
228 elif [ "$MODE" = "org" ] ; then
229 echo ' The semantic patch that makes this Org report is available'
230 else
231 echo ' The semantic patch that makes this output is available'
232 fi
1e9dea2a
NP
233 echo " in $FILE."
234 echo ''
235 echo ' More information about semantic patching is available at'
236 echo ' http://coccinelle.lip6.fr/'
237 echo ''
238
3c908417
NP
239 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
240 echo 'Semantic patch information:'
241 sed -ne 's|^//#||p' $COCCI
242 echo ''
243 fi
2c1160c8 244 fi
3c908417 245
2c1160c8 246 if [ "$MODE" = "chain" ] ; then
5303265a 247 run_cmd $SPATCH -D patch \
93f14468 248 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
5303265a 249 run_cmd $SPATCH -D report \
93f14468 250 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
5303265a 251 run_cmd $SPATCH -D context \
93f14468 252 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
5303265a 253 run_cmd $SPATCH -D org \
93f14468 254 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
c05cd6dd 255 elif [ "$MODE" = "rep+ctxt" ] ; then
5303265a 256 run_cmd $SPATCH -D report \
93f14468 257 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
5303265a 258 run_cmd $SPATCH -D context \
93f14468 259 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
1e9dea2a 260 else
93f14468 261 run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
1e9dea2a 262 fi
74425eee 263
74425eee
NP
264}
265
9ed07ada
JL
266if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then
267 if [ -f $DEBUG_FILE ]; then
268 echo "Debug file $DEBUG_FILE exists, bailing"
269 exit
270 fi
271else
272 DEBUG_FILE="/dev/null"
273fi
274
74425eee
NP
275if [ "$COCCI" = "" ] ; then
276 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1e9dea2a 277 coccinelle $f
74425eee
NP
278 done
279else
1e9dea2a 280 coccinelle $COCCI
74425eee 281fi