forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoccicheck
executable file
·243 lines (209 loc) · 6.25 KB
/
coccicheck
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-2.0
#
ZEPHYR_BASE=$( builtin cd "$( dirname "$DIR" )" && pwd ${PWD_OPT})
DIR="$(dirname $(readlink -f $0))/.."
SPATCH="`which ${SPATCH:=spatch}`"
if [ ! -x "$SPATCH" ]; then
echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
exit 1
fi
VERBOSE=0
usage="Usage: ./scripts/coccicheck [OPTIONS]... [DIRECTORY|FILE]...
OPTIONS:
-------
-m= , --mode= specify the mode use {report, patch, org, context, chain}
-v= , --verbose= enable verbose output {1}
-j= , --jobs= number of jobs to use {0 - `nproc`}
-c= , --cocci= specify cocci script to use
-d= , --debug= specify file to store debug log
-f= , --sp-flag= pass additional flag to spatch
-h , --help display help and exit
Default values if any OPTION is not supplied:
--------------------------------------------
mode = report
verbose = 0 (disabled)
jobs = maximum jobs available on the machine
cocci = all cocci scripts available at scripts/coccinelle/*
If no [DIRECTORY|FILE] is supplied, entire codebase is processed.
For detailed documentation refer: doc/guides/coccinelle.rst"
for i in "$@"
do
case $i in
-m=*|--mode=*)
MODE="${i#*=}"
shift # past argument=value
;;
-v=*|--verbose=*)
VERBOSE="${i#*=}"
shift # past argument=value
;;
-j=*|--jobs=*)
J="${i#*=}"
shift
;;
-c=*|--cocci=*)
COCCI="${i#*=}"
shift
;;
-d=*|--debug=*)
DEBUG_FILE="${i#*=}"
shift
;;
-f=*|--sp-flag=*)
SPFLAGS="${i#*=}"
shift
;;
-h|--help)
echo "$usage"
exit 1
;;
*)
FILE="${i#*=}"
if [ ! -e "$FILE" ]; then
echo "unknown option: '${i#*=}'"
echo "$usage"
exit 2
fi
;;
esac
done
FLAGS="--very-quiet"
if [ "$FILE" = "" ] ; then
OPTIONS="--dir $ZEPHYR_BASE"
else
OPTIONS="--dir $FILE"
fi
if [ -z "$J" ]; then
NPROC=$(getconf _NPROCESSORS_ONLN)
else
NPROC="$J"
fi
OPTIONS="--macro-file $ZEPHYR_BASE/scripts/coccinelle/macros.h $OPTIONS"
if [ "$FILE" != "" ] ; then
OPTIONS="--patch $ZEPHYR_BASE $OPTIONS"
fi
if [ "$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
if [ "$MODE" = "" ] ; then
echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
echo 'Available modes are the following: 'patch', 'report', 'context', 'org''
echo 'You can specify the mode with "./scripts/coccicheck --mode=<mode>"'
echo 'Note however that some modes are not implemented by some semantic patches.'
MODE="report"
fi
if [ "$MODE" = "chain" ] ; then
echo 'You have selected the "chain" mode.'
echo 'All available modes will be tried (in that order): patch, report, context, org'
elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
FLAGS="--no-show-diff $FLAGS"
fi
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 ''
run_cmd_parmap() {
if [ $VERBOSE -ne 0 ] ; then
echo "Running ($NPROC in parallel): $@"
fi
echo $@ >>$DEBUG_FILE
$@ 2>>$DEBUG_FILE
err=$?
if [[ $err -ne 0 ]]; then
echo "coccicheck failed"
exit $err
fi
}
# You can override heuristics with SPFLAGS, these must always go last
OPTIONS="$OPTIONS $SPFLAGS"
coccinelle () {
COCCI="$1"
OPT=`grep "Options:" $COCCI | cut -d':' -f2`
VIRTUAL=`grep "virtual" $COCCI | cut -d' ' -f2`
if [[ $VIRTUAL = "" ]]; then
echo "No available modes found in \"$COCCI\" script."
echo "Consider adding virtual rules to the script."
exit 1
elif [[ $VIRTUAL != *"$MODE"* ]]; then
echo "Invalid mode \"$MODE\" supplied!"
echo "Available modes for \"`basename $COCCI`\" are: "$VIRTUAL""
if [[ $VIRTUAL == *report* ]]; then
MODE=report
elif [[ $VIRTUAL == *context* ]]; then
MODE=context
elif [[ $VIRTUAL == *patch* ]]; then
MODE=patch
else
MODE=org
fi
echo "Using random available mode: \"$MODE\""
echo ''
fi
if [ $VERBOSE -ne 0 ] ; then
FILE=${COCCI#$ZEPHYR_BASE/}
echo "Processing `basename $COCCI`"
echo "with option(s) \"$OPT\""
echo ''
echo 'Message example to submit a patch:'
sed -ne 's|^///||p' $COCCI
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
echo " in $FILE."
echo ''
echo ' More information about semantic patching is available at'
echo ' http://coccinelle.lip6.fr/'
echo ''
if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
echo 'Semantic patch information:'
sed -ne 's|^//#||p' $COCCI
echo ''
fi
fi
if [ "$MODE" = "chain" ] ; then
run_cmd_parmap $SPATCH -D patch \
$FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
run_cmd_parmap $SPATCH -D report \
$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
run_cmd_parmap $SPATCH -D context \
$FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
run_cmd_parmap $SPATCH -D org \
$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
elif [ "$MODE" = "rep+ctxt" ] ; then
run_cmd_parmap $SPATCH -D report \
$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
run_cmd_parmap $SPATCH -D context \
$FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
else
run_cmd_parmap $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
fi
MODE=report
}
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
if [ "$COCCI" = "" ] ; then
for f in `find $ZEPHYR_BASE/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
coccinelle $f
echo '-------------------------------------------------------------------------'
echo ''
done
else
coccinelle $COCCI
fi