]>
Commit | Line | Data |
---|---|---|
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 |
8 | SPATCH="`which ${SPATCH:=spatch}`" |
9 | ||
90d06a46 KC |
10 | trap kill_running SIGTERM SIGINT |
11 | declare -a SPATCH_PID | |
12 | ||
26e56720 BS |
13 | # The verbosity may be set by the environmental parameter V= |
14 | # as for example with 'make V=1 coccicheck' | |
15 | ||
16 | if [ -n "$V" -a "$V" != "0" ]; then | |
90d06a46 | 17 | VERBOSE="$V" |
26e56720 BS |
18 | else |
19 | VERBOSE=0 | |
20 | fi | |
21 | ||
90d06a46 KC |
22 | if [ -z "$J" ]; then |
23 | NPROC=$(getconf _NPROCESSORS_ONLN) | |
24 | else | |
25 | NPROC="$J" | |
26 | fi | |
27 | ||
93f14468 | 28 | FLAGS="$SPFLAGS --very-quiet" |
9e395550 NP |
29 | |
30 | # spatch only allows include directories with the syntax "-I include" | |
31 | # while gcc also allows "-Iinclude" and "-include include" | |
32 | COCCIINCLUDE=${LINUXINCLUDE//-I/-I } | |
33 | COCCIINCLUDE=${COCCIINCLUDE//-include/-I} | |
34 | ||
1e9dea2a NP |
35 | if [ "$C" = "1" -o "$C" = "2" ]; then |
36 | ONLINE=1 | |
37 | ||
9e395550 NP |
38 | # Take only the last argument, which is the C file to test |
39 | shift $(( $# - 1 )) | |
40 | OPTIONS="$COCCIINCLUDE $1" | |
1e9dea2a NP |
41 | else |
42 | ONLINE=0 | |
d0bc1fb4 | 43 | if [ "$KBUILD_EXTMOD" = "" ] ; then |
93f14468 | 44 | OPTIONS="--dir $srctree $COCCIINCLUDE" |
d0bc1fb4 | 45 | else |
93f14468 | 46 | OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE" |
d0bc1fb4 | 47 | fi |
1e9dea2a NP |
48 | fi |
49 | ||
bad6a409 | 50 | if [ "$KBUILD_EXTMOD" != "" ] ; then |
93f14468 | 51 | OPTIONS="--patch $srctree $OPTIONS" |
bad6a409 NP |
52 | fi |
53 | ||
74425eee NP |
54 | if [ ! -x "$SPATCH" ]; then |
55 | echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/' | |
56 | exit 1 | |
57 | fi | |
58 | ||
59 | if [ "$MODE" = "" ] ; then | |
1e9dea2a | 60 | if [ "$ONLINE" = "0" ] ; then |
1f0a6742 NP |
61 | echo 'You have not explicitly specified the mode to use. Using default "report" mode.' |
62 | echo 'Available modes are the following: patch, report, context, org' | |
1e9dea2a | 63 | echo 'You can specify the mode with "make coccicheck MODE=<mode>"' |
1f0a6742 NP |
64 | echo 'Note however that some modes are not implemented by some semantic patches.' |
65 | fi | |
66 | MODE="report" | |
67 | fi | |
68 | ||
69 | if [ "$MODE" = "chain" ] ; then | |
70 | if [ "$ONLINE" = "0" ] ; then | |
71 | echo 'You have selected the "chain" mode.' | |
72 | echo 'All available modes will be tried (in that order): patch, report, context, org' | |
1e9dea2a | 73 | fi |
03ee0c42 | 74 | elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then |
93f14468 | 75 | FLAGS="$FLAGS --no-show-diff" |
74425eee NP |
76 | fi |
77 | ||
1e9dea2a NP |
78 | if [ "$ONLINE" = "0" ] ; then |
79 | echo '' | |
80 | echo 'Please check for false positives in the output before submitting a patch.' | |
81 | echo 'When using "patch" mode, carefully review the patch before submitting it.' | |
82 | echo '' | |
83 | fi | |
74425eee | 84 | |
5303265a | 85 | run_cmd() { |
90d06a46 | 86 | local i |
5303265a | 87 | if [ $VERBOSE -ne 0 ] ; then |
90d06a46 | 88 | echo "Running ($NPROC in parallel): $@" |
5303265a | 89 | fi |
90d06a46 | 90 | for i in $(seq 0 $(( NPROC - 1)) ); do |
93f14468 | 91 | eval "$@ --max $NPROC --index $i &" |
90d06a46 KC |
92 | SPATCH_PID[$i]=$! |
93 | if [ $VERBOSE -eq 2 ] ; then | |
94 | echo "${SPATCH_PID[$i]} running" | |
95 | fi | |
96 | done | |
97 | wait | |
5303265a BS |
98 | } |
99 | ||
90d06a46 KC |
100 | kill_running() { |
101 | for i in $(seq $(( NPROC - 1 )) ); do | |
102 | if [ $VERBOSE -eq 2 ] ; then | |
103 | echo "Killing ${SPATCH_PID[$i]}" | |
104 | fi | |
105 | kill ${SPATCH_PID[$i]} 2>/dev/null | |
106 | done | |
107 | } | |
5303265a | 108 | |
1e9dea2a | 109 | coccinelle () { |
74425eee | 110 | COCCI="$1" |
74425eee NP |
111 | |
112 | OPT=`grep "Option" $COCCI | cut -d':' -f2` | |
74425eee | 113 | |
93f14468 | 114 | # The option '--parse-cocci' can be used to syntactically check the SmPL files. |
1e9dea2a NP |
115 | # |
116 | # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null | |
74425eee | 117 | |
35d88a38 | 118 | if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then |
74425eee | 119 | |
1e9dea2a | 120 | FILE=`echo $COCCI | sed "s|$srctree/||"` |
74425eee | 121 | |
3c908417 NP |
122 | echo "Processing `basename $COCCI`" |
123 | echo "with option(s) \"$OPT\"" | |
124 | echo '' | |
1e9dea2a NP |
125 | echo 'Message example to submit a patch:' |
126 | ||
3c908417 | 127 | sed -ne 's|^///||p' $COCCI |
1e9dea2a | 128 | |
062c1825 NP |
129 | if [ "$MODE" = "patch" ] ; then |
130 | echo ' The semantic patch that makes this change is available' | |
131 | elif [ "$MODE" = "report" ] ; then | |
132 | echo ' The semantic patch that makes this report is available' | |
133 | elif [ "$MODE" = "context" ] ; then | |
134 | echo ' The semantic patch that spots this code is available' | |
135 | elif [ "$MODE" = "org" ] ; then | |
136 | echo ' The semantic patch that makes this Org report is available' | |
137 | else | |
138 | echo ' The semantic patch that makes this output is available' | |
139 | fi | |
1e9dea2a NP |
140 | echo " in $FILE." |
141 | echo '' | |
142 | echo ' More information about semantic patching is available at' | |
143 | echo ' http://coccinelle.lip6.fr/' | |
144 | echo '' | |
145 | ||
3c908417 NP |
146 | if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then |
147 | echo 'Semantic patch information:' | |
148 | sed -ne 's|^//#||p' $COCCI | |
149 | echo '' | |
150 | fi | |
2c1160c8 | 151 | fi |
3c908417 | 152 | |
2c1160c8 | 153 | if [ "$MODE" = "chain" ] ; then |
5303265a | 154 | run_cmd $SPATCH -D patch \ |
93f14468 | 155 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
5303265a | 156 | run_cmd $SPATCH -D report \ |
93f14468 | 157 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \ |
5303265a | 158 | run_cmd $SPATCH -D context \ |
93f14468 | 159 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \ |
5303265a | 160 | run_cmd $SPATCH -D org \ |
93f14468 | 161 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1 |
c05cd6dd | 162 | elif [ "$MODE" = "rep+ctxt" ] ; then |
5303265a | 163 | run_cmd $SPATCH -D report \ |
93f14468 | 164 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \ |
5303265a | 165 | run_cmd $SPATCH -D context \ |
93f14468 | 166 | $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 |
1e9dea2a | 167 | else |
93f14468 | 168 | run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1 |
1e9dea2a | 169 | fi |
74425eee | 170 | |
74425eee NP |
171 | } |
172 | ||
173 | if [ "$COCCI" = "" ] ; then | |
174 | for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do | |
1e9dea2a | 175 | coccinelle $f |
74425eee NP |
176 | done |
177 | else | |
1e9dea2a | 178 | coccinelle $COCCI |
74425eee | 179 | fi |