Blame view
scripts/coccicheck
4.65 KB
9e3955507
|
1 |
#!/bin/bash |
74425eee7
|
2 |
|
ec97946ed
|
3 4 5 6 |
# # This script requires at least spatch # version 1.0.0-rc11. # |
74425eee7
|
7 |
SPATCH="`which ${SPATCH:=spatch}`" |
90d06a468
|
8 9 |
trap kill_running SIGTERM SIGINT declare -a SPATCH_PID |
26e567209
|
10 11 12 13 |
# 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
|
14 |
VERBOSE="$V" |
26e567209
|
15 16 17 |
else VERBOSE=0 fi |
90d06a468
|
18 19 20 21 22 |
if [ -z "$J" ]; then NPROC=$(getconf _NPROCESSORS_ONLN) else NPROC="$J" fi |
93f144684
|
23 |
FLAGS="$SPFLAGS --very-quiet" |
9e3955507
|
24 25 26 27 28 |
# spatch only allows include directories with the syntax "-I include" # while gcc also allows "-Iinclude" and "-include include" COCCIINCLUDE=${LINUXINCLUDE//-I/-I } COCCIINCLUDE=${COCCIINCLUDE//-include/-I} |
1e9dea2a6
|
29 30 |
if [ "$C" = "1" -o "$C" = "2" ]; then ONLINE=1 |
9e3955507
|
31 32 33 |
# Take only the last argument, which is the C file to test shift $(( $# - 1 )) OPTIONS="$COCCIINCLUDE $1" |
1e9dea2a6
|
34 35 |
else ONLINE=0 |
d0bc1fb46
|
36 |
if [ "$KBUILD_EXTMOD" = "" ] ; then |
93f144684
|
37 |
OPTIONS="--dir $srctree $COCCIINCLUDE" |
d0bc1fb46
|
38 |
else |
93f144684
|
39 |
OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" |
d0bc1fb46
|
40 |
fi |
1e9dea2a6
|
41 |
fi |
bad6a4092
|
42 |
if [ "$KBUILD_EXTMOD" != "" ] ; then |
93f144684
|
43 |
OPTIONS="--patch $srctree $OPTIONS" |
bad6a4092
|
44 |
fi |
74425eee7
|
45 46 47 48 49 50 |
if [ ! -x "$SPATCH" ]; then echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/' exit 1 fi if [ "$MODE" = "" ] ; then |
1e9dea2a6
|
51 |
if [ "$ONLINE" = "0" ] ; then |
1f0a6742d
|
52 53 |
echo 'You have not explicitly specified the mode to use. Using default "report" mode.' echo 'Available modes are the following: patch, report, context, org' |
1e9dea2a6
|
54 |
echo 'You can specify the mode with "make coccicheck MODE=<mode>"' |
1f0a6742d
|
55 56 57 58 59 60 61 62 63 |
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
|
64 |
fi |
03ee0c42a
|
65 |
elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then |
93f144684
|
66 |
FLAGS="$FLAGS --no-show-diff" |
74425eee7
|
67 |
fi |
1e9dea2a6
|
68 69 70 71 72 73 |
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
|
74 |
|
5303265a4
|
75 |
run_cmd() { |
90d06a468
|
76 |
local i |
5303265a4
|
77 |
if [ $VERBOSE -ne 0 ] ; then |
90d06a468
|
78 |
echo "Running ($NPROC in parallel): $@" |
5303265a4
|
79 |
fi |
90d06a468
|
80 |
for i in $(seq 0 $(( NPROC - 1)) ); do |
93f144684
|
81 |
eval "$@ --max $NPROC --index $i &" |
90d06a468
|
82 83 84 85 86 87 |
SPATCH_PID[$i]=$! if [ $VERBOSE -eq 2 ] ; then echo "${SPATCH_PID[$i]} running" fi done wait |
5303265a4
|
88 |
} |
90d06a468
|
89 90 91 92 93 94 95 96 |
kill_running() { for i in $(seq $(( NPROC - 1 )) ); do if [ $VERBOSE -eq 2 ] ; then echo "Killing ${SPATCH_PID[$i]}" fi kill ${SPATCH_PID[$i]} 2>/dev/null done } |
5303265a4
|
97 |
|
1e9dea2a6
|
98 |
coccinelle () { |
74425eee7
|
99 |
COCCI="$1" |
74425eee7
|
100 101 |
OPT=`grep "Option" $COCCI | cut -d':' -f2` |
74425eee7
|
102 |
|
93f144684
|
103 |
# The option '--parse-cocci' can be used to syntactically check the SmPL files. |
1e9dea2a6
|
104 105 |
# # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null |
74425eee7
|
106 |
|
35d88a387
|
107 |
if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then |
74425eee7
|
108 |
|
1e9dea2a6
|
109 |
FILE=`echo $COCCI | sed "s|$srctree/||"` |
74425eee7
|
110 |
|
3c9084176
|
111 112 113 |
echo "Processing `basename $COCCI`" echo "with option(s) \"$OPT\"" echo '' |
1e9dea2a6
|
114 |
echo 'Message example to submit a patch:' |
3c9084176
|
115 |
sed -ne 's|^///||p' $COCCI |
1e9dea2a6
|
116 |
|
062c1825a
|
117 118 119 120 121 122 123 124 125 126 127 |
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
|
128 129 130 131 132 |
echo " in $FILE." echo '' echo ' More information about semantic patching is available at' echo ' http://coccinelle.lip6.fr/' echo '' |
3c9084176
|
133 134 135 136 137 |
if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then echo 'Semantic patch information:' sed -ne 's|^//#||p' $COCCI echo '' fi |
2c1160c87
|
138 |
fi |
3c9084176
|
139 |
|
2c1160c87
|
140 |
if [ "$MODE" = "chain" ] ; then |
5303265a4
|
141 |
run_cmd $SPATCH -D patch \ |
93f144684
|
142 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
5303265a4
|
143 |
run_cmd $SPATCH -D report \ |
93f144684
|
144 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \ |
5303265a4
|
145 |
run_cmd $SPATCH -D context \ |
93f144684
|
146 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
5303265a4
|
147 |
run_cmd $SPATCH -D org \ |
93f144684
|
148 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1 |
c05cd6ddb
|
149 |
elif [ "$MODE" = "rep+ctxt" ] ; then |
5303265a4
|
150 |
run_cmd $SPATCH -D report \ |
93f144684
|
151 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \ |
5303265a4
|
152 |
run_cmd $SPATCH -D context \ |
93f144684
|
153 |
$FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 |
1e9dea2a6
|
154 |
else |
93f144684
|
155 |
run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 |
1e9dea2a6
|
156 |
fi |
74425eee7
|
157 |
|
74425eee7
|
158 159 160 161 |
} if [ "$COCCI" = "" ] ; then for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do |
1e9dea2a6
|
162 |
coccinelle $f |
74425eee7
|
163 164 |
done else |
1e9dea2a6
|
165 |
coccinelle $COCCI |
74425eee7
|
166 |
fi |