Blame view
scripts/coccicheck
7.64 KB
9e3955507 Coccinelle: Clean... |
1 |
#!/bin/bash |
b24413180 License cleanup: ... |
2 |
# SPDX-License-Identifier: GPL-2.0 |
c100d537b coccicheck: refer... |
3 4 |
# Linux kernel coccicheck # |
1e01892e7 scripts/coccichec... |
5 |
# Read Documentation/dev-tools/coccinelle.rst |
ec97946ed Coccinelle: Updat... |
6 7 8 |
# # This script requires at least spatch # version 1.0.0-rc11. |
ec97946ed Coccinelle: Updat... |
9 |
|
a9e064c00 coccicheck: add s... |
10 |
DIR="$(dirname $(readlink -f $0))/.." |
74425eee7 Add a target to u... |
11 |
SPATCH="`which ${SPATCH:=spatch}`" |
13d948653 coccicheck: move ... |
12 13 14 15 |
if [ ! -x "$SPATCH" ]; then echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/' exit 1 fi |
a9e064c00 coccicheck: add s... |
16 17 |
SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}') SPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh) |
c930a1b23 coccicheck: enabl... |
18 19 |
USE_JOBS="no" $SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes" |
90d06a468 coccicheck: span ... |
20 |
|
26e567209 coccicheck: Allow... |
21 22 23 24 |
# The verbosity may be set by the environmental parameter V= # as for example with 'make V=1 coccicheck' if [ -n "$V" -a "$V" != "0" ]; then |
90d06a468 coccicheck: span ... |
25 |
VERBOSE="$V" |
26e567209 coccicheck: Allow... |
26 27 28 |
else VERBOSE=0 fi |
8e826ad52 coccicheck: make ... |
29 |
FLAGS="--very-quiet" |
9e3955507 Coccinelle: Clean... |
30 |
|
5c384dba9 coccicheck: repla... |
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# You can use SPFLAGS to append extra arguments to coccicheck or override any # heuristics done in this file as Coccinelle accepts the last options when # options conflict. # # A good example for use of SPFLAGS is if you want to debug your cocci script, # you can for instance use the following: # # $ export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci # $ make coccicheck MODE=report DEBUG_FILE="all.err" SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c # # "--show-trying" should show you what rule is being processed as it goes to # stdout, you do not need a debug file for that. The profile output will be # be sent to stdout, if you provide a DEBUG_FILE the profiling data can be # inspected there. # # --profile will not output if --very-quiet is used, so avoid it. echo $SPFLAGS | egrep -e "--profile|--show-trying" 2>&1 > /dev/null if [ $? -eq 0 ]; then FLAGS="--quiet" fi |
9e3955507 Coccinelle: Clean... |
51 52 53 |
# spatch only allows include directories with the syntax "-I include" # while gcc also allows "-Iinclude" and "-include include" COCCIINCLUDE=${LINUXINCLUDE//-I/-I } |
5b1691085 Coccinelle: fix i... |
54 |
COCCIINCLUDE=${COCCIINCLUDE// -include/ --include} |
9e3955507 Coccinelle: Clean... |
55 |
|
1e9dea2a6 Add support for t... |
56 57 |
if [ "$C" = "1" -o "$C" = "2" ]; then ONLINE=1 |
9e3955507 Coccinelle: Clean... |
58 59 60 |
# Take only the last argument, which is the C file to test shift $(( $# - 1 )) OPTIONS="$COCCIINCLUDE $1" |
d7059ca01 coccinelle: fix p... |
61 62 63 |
# No need to parallelize Coccinelle since this mode takes one input file. NPROC=1 |
1e9dea2a6 Add support for t... |
64 65 |
else ONLINE=0 |
d0bc1fb46 coccicheck: add M... |
66 |
if [ "$KBUILD_EXTMOD" = "" ] ; then |
93f144684 Coccinelle: Updat... |
67 |
OPTIONS="--dir $srctree $COCCIINCLUDE" |
d0bc1fb46 coccicheck: add M... |
68 |
else |
93f144684 Coccinelle: Updat... |
69 |
OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" |
d0bc1fb46 coccicheck: add M... |
70 |
fi |
d7059ca01 coccinelle: fix p... |
71 |
|
e16a7c47d scripts: cocciche... |
72 |
# Use only one thread per core by default if hyperthreading is enabled |
2f324dd8a scripts: cocciche... |
73 |
THREADS_PER_CORE=$(lscpu | grep "Thread(s) per core: " | tr -cd "[:digit:]") |
d7059ca01 coccinelle: fix p... |
74 75 |
if [ -z "$J" ]; then NPROC=$(getconf _NPROCESSORS_ONLN) |
c5864560d scripts: cocciche... |
76 |
if [ $THREADS_PER_CORE -gt 1 -a $NPROC -gt 4 ] ; then |
e16a7c47d scripts: cocciche... |
77 78 |
NPROC=$((NPROC/2)) fi |
d7059ca01 coccinelle: fix p... |
79 80 81 |
else NPROC="$J" fi |
1e9dea2a6 Add support for t... |
82 |
fi |
bad6a4092 Coccinelle: Fix p... |
83 |
if [ "$KBUILD_EXTMOD" != "" ] ; then |
93f144684 Coccinelle: Updat... |
84 |
OPTIONS="--patch $srctree $OPTIONS" |
bad6a4092 Coccinelle: Fix p... |
85 |
fi |
c930a1b23 coccicheck: enabl... |
86 87 88 89 90 91 92 93 94 |
# You can override by using SPFLAGS if [ "$USE_JOBS" = "no" ]; then trap kill_running SIGTERM SIGINT declare -a SPATCH_PID elif [ "$NPROC" != "1" ]; then # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1" fi |
74425eee7 Add a target to u... |
95 |
if [ "$MODE" = "" ] ; then |
1e9dea2a6 Add support for t... |
96 |
if [ "$ONLINE" = "0" ] ; then |
1f0a6742d Coccinelle: Make ... |
97 |
echo 'You have not explicitly specified the mode to use. Using default "report" mode.' |
4b2bd20c3 scripts: cocciche... |
98 |
echo 'Available modes are the following: patch, report, context, org, chain' |
1e9dea2a6 Add support for t... |
99 |
echo 'You can specify the mode with "make coccicheck MODE=<mode>"' |
1f0a6742d Coccinelle: Make ... |
100 101 102 103 104 105 106 107 108 |
echo 'Note however that some modes are not implemented by some semantic patches.' fi MODE="report" fi if [ "$MODE" = "chain" ] ; then if [ "$ONLINE" = "0" ] ; then echo 'You have selected the "chain" mode.' echo 'All available modes will be tried (in that order): patch, report, context, org' |
1e9dea2a6 Add support for t... |
109 |
fi |
03ee0c42a Coccinelle: Use t... |
110 |
elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then |
7a2358b38 coccicheck: Allow... |
111 |
FLAGS="--no-show-diff $FLAGS" |
74425eee7 Add a target to u... |
112 |
fi |
1e9dea2a6 Add support for t... |
113 114 115 116 117 118 |
if [ "$ONLINE" = "0" ] ; then echo '' echo 'Please check for false positives in the output before submitting a patch.' echo 'When using "patch" mode, carefully review the patch before submitting it.' echo '' fi |
74425eee7 Add a target to u... |
119 |
|
c930a1b23 coccicheck: enabl... |
120 121 122 123 |
run_cmd_parmap() { if [ $VERBOSE -ne 0 ] ; then echo "Running ($NPROC in parallel): $@" fi |
a19d13583 scripts: cocciche... |
124 125 126 127 128 129 130 |
if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then echo $@>>$DEBUG_FILE $@ 2>>$DEBUG_FILE else echo $@ $@ 2>&1 fi |
512ddf7d7 coccicheck: retur... |
131 132 |
err=$? if [[ $err -ne 0 ]]; then |
c930a1b23 coccicheck: enabl... |
133 |
echo "coccicheck failed" |
512ddf7d7 coccicheck: retur... |
134 |
exit $err |
c930a1b23 coccicheck: enabl... |
135 136 137 138 |
fi } run_cmd_old() { |
90d06a468 coccicheck: span ... |
139 |
local i |
5303265a4 coccicheck: Allow... |
140 |
if [ $VERBOSE -ne 0 ] ; then |
90d06a468 coccicheck: span ... |
141 |
echo "Running ($NPROC in parallel): $@" |
5303265a4 coccicheck: Allow... |
142 |
fi |
90d06a468 coccicheck: span ... |
143 |
for i in $(seq 0 $(( NPROC - 1)) ); do |
93f144684 Coccinelle: Updat... |
144 |
eval "$@ --max $NPROC --index $i &" |
90d06a468 coccicheck: span ... |
145 146 147 148 149 150 |
SPATCH_PID[$i]=$! if [ $VERBOSE -eq 2 ] ; then echo "${SPATCH_PID[$i]} running" fi done wait |
5303265a4 coccicheck: Allow... |
151 |
} |
c930a1b23 coccicheck: enabl... |
152 153 154 155 156 157 158 |
run_cmd() { if [ "$USE_JOBS" = "yes" ]; then run_cmd_parmap $@ else run_cmd_old $@ fi } |
90d06a468 coccicheck: span ... |
159 |
kill_running() { |
2552a39fa coccicheck: Fix m... |
160 |
for i in $(seq 0 $(( NPROC - 1 )) ); do |
90d06a468 coccicheck: span ... |
161 162 163 164 165 166 |
if [ $VERBOSE -eq 2 ] ; then echo "Killing ${SPATCH_PID[$i]}" fi kill ${SPATCH_PID[$i]} 2>/dev/null done } |
5303265a4 coccicheck: Allow... |
167 |
|
8e826ad52 coccicheck: make ... |
168 169 |
# You can override heuristics with SPFLAGS, these must always go last OPTIONS="$OPTIONS $SPFLAGS" |
1e9dea2a6 Add support for t... |
170 |
coccinelle () { |
74425eee7 Add a target to u... |
171 |
COCCI="$1" |
74425eee7 Add a target to u... |
172 |
|
e0be348e4 coccinelle: grep ... |
173 174 |
OPT=`grep "Options:" $COCCI | cut -d':' -f2` REQ=`grep "Requires:" $COCCI | cut -d':' -f2 | sed "s| ||"` |
a9e064c00 coccicheck: add s... |
175 176 177 |
REQ_NUM=$(echo $REQ | ${DIR}/scripts/ld-version.sh) if [ "$REQ_NUM" != "0" ] ; then if [ "$SPATCH_VERSION_NUM" -lt "$REQ_NUM" ] ; then |
1640eea35 Coccinelle: cocci... |
178 |
echo "Skipping coccinelle SmPL patch: $COCCI" |
a9e064c00 coccicheck: add s... |
179 180 181 182 183 |
echo "You have coccinelle: $SPATCH_VERSION" echo "This SmPL patch requires: $REQ" return fi fi |
74425eee7 Add a target to u... |
184 |
|
93f144684 Coccinelle: Updat... |
185 |
# The option '--parse-cocci' can be used to syntactically check the SmPL files. |
1e9dea2a6 Add support for t... |
186 187 |
# # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null |
74425eee7 Add a target to u... |
188 |
|
35d88a387 Coccinelle: Resto... |
189 |
if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then |
74425eee7 Add a target to u... |
190 |
|
cd1af7cfb coccinelle: fix v... |
191 |
FILE=${COCCI#$srctree/} |
74425eee7 Add a target to u... |
192 |
|
3c9084176 Coccinelle: Impro... |
193 194 195 |
echo "Processing `basename $COCCI`" echo "with option(s) \"$OPT\"" echo '' |
1e9dea2a6 Add support for t... |
196 |
echo 'Message example to submit a patch:' |
3c9084176 Coccinelle: Impro... |
197 |
sed -ne 's|^///||p' $COCCI |
1e9dea2a6 Add support for t... |
198 |
|
062c1825a Coccinelle: Add c... |
199 200 201 202 203 204 205 206 207 208 209 |
if [ "$MODE" = "patch" ] ; then echo ' The semantic patch that makes this change is available' elif [ "$MODE" = "report" ] ; then echo ' The semantic patch that makes this report is available' elif [ "$MODE" = "context" ] ; then echo ' The semantic patch that spots this code is available' elif [ "$MODE" = "org" ] ; then echo ' The semantic patch that makes this Org report is available' else echo ' The semantic patch that makes this output is available' fi |
1e9dea2a6 Add support for t... |
210 211 212 213 214 |
echo " in $FILE." echo '' echo ' More information about semantic patching is available at' echo ' http://coccinelle.lip6.fr/' echo '' |
3c9084176 Coccinelle: Impro... |
215 216 217 218 219 |
if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then echo 'Semantic patch information:' sed -ne 's|^//#||p' $COCCI echo '' fi |
2c1160c87 Coccinelle: Add a... |
220 |
fi |
3c9084176 Coccinelle: Impro... |
221 |
|
2c1160c87 Coccinelle: Add a... |
222 |
if [ "$MODE" = "chain" ] ; then |
5303265a4 coccicheck: Allow... |
223 |
run_cmd $SPATCH -D patch \ |
93f144684 Coccinelle: Updat... |
224 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
5303265a4 coccicheck: Allow... |
225 |
run_cmd $SPATCH -D report \ |
93f144684 Coccinelle: Updat... |
226 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \ |
5303265a4 coccicheck: Allow... |
227 |
run_cmd $SPATCH -D context \ |
93f144684 Coccinelle: Updat... |
228 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
5303265a4 coccicheck: Allow... |
229 |
run_cmd $SPATCH -D org \ |
93f144684 Coccinelle: Updat... |
230 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1 |
c05cd6ddb coccicheck: Add t... |
231 |
elif [ "$MODE" = "rep+ctxt" ] ; then |
5303265a4 coccicheck: Allow... |
232 |
run_cmd $SPATCH -D report \ |
93f144684 Coccinelle: Updat... |
233 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \ |
5303265a4 coccicheck: Allow... |
234 |
run_cmd $SPATCH -D context \ |
93f144684 Coccinelle: Updat... |
235 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 |
1e9dea2a6 Add support for t... |
236 |
else |
93f144684 Coccinelle: Updat... |
237 |
run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 |
1e9dea2a6 Add support for t... |
238 |
fi |
74425eee7 Add a target to u... |
239 |
|
74425eee7 Add a target to u... |
240 |
} |
9ed07ada0 Coccinelle: make ... |
241 242 243 244 245 246 247 248 |
if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then if [ -f $DEBUG_FILE ]; then echo "Debug file $DEBUG_FILE exists, bailing" exit fi else DEBUG_FILE="/dev/null" fi |
74425eee7 Add a target to u... |
249 250 |
if [ "$COCCI" = "" ] ; then for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do |
1e9dea2a6 Add support for t... |
251 |
coccinelle $f |
74425eee7 Add a target to u... |
252 253 |
done else |
1e9dea2a6 Add support for t... |
254 |
coccinelle $COCCI |
74425eee7 Add a target to u... |
255 |
fi |