4 # This script requires at least spatch
8 SPATCH="`which ${SPATCH:=spatch}`"
10 if [ ! -x "$SPATCH" ]; then
11 echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
16 $SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
18 # The verbosity may be set by the environmental parameter V=
19 # as for example with 'make V=1 coccicheck'
21 if [ -n "$V" -a "$V" != "0" ]; then
28 NPROC=$(getconf _NPROCESSORS_ONLN)
35 # spatch only allows include directories with the syntax "-I include"
36 # while gcc also allows "-Iinclude" and "-include include"
37 COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
38 COCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
40 if [ "$C" = "1" -o "$C" = "2" ]; then
43 # Take only the last argument, which is the C file to test
45 OPTIONS="$COCCIINCLUDE $1"
48 if [ "$KBUILD_EXTMOD" = "" ] ; then
49 OPTIONS="--dir $srctree $COCCIINCLUDE"
51 OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
55 if [ "$KBUILD_EXTMOD" != "" ] ; then
56 OPTIONS="--patch $srctree $OPTIONS"
59 # You can override by using SPFLAGS
60 if [ "$USE_JOBS" = "no" ]; then
61 trap kill_running SIGTERM SIGINT
63 elif [ "$NPROC" != "1" ]; then
64 # Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
65 # https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
66 OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
69 if [ "$MODE" = "" ] ; then
70 if [ "$ONLINE" = "0" ] ; then
71 echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
72 echo 'Available modes are the following: patch, report, context, org'
73 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
74 echo 'Note however that some modes are not implemented by some semantic patches.'
79 if [ "$MODE" = "chain" ] ; then
80 if [ "$ONLINE" = "0" ] ; then
81 echo 'You have selected the "chain" mode.'
82 echo 'All available modes will be tried (in that order): patch, report, context, org'
84 elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
85 FLAGS="--no-show-diff $FLAGS"
88 if [ "$ONLINE" = "0" ] ; then
90 echo 'Please check for false positives in the output before submitting a patch.'
91 echo 'When using "patch" mode, carefully review the patch before submitting it.'
96 if [ $VERBOSE -ne 0 ] ; then
97 echo "Running ($NPROC in parallel): $@"
100 if [[ $? -ne 0 ]]; then
101 echo "coccicheck failed"
108 if [ $VERBOSE -ne 0 ] ; then
109 echo "Running ($NPROC in parallel): $@"
111 for i in $(seq 0 $(( NPROC - 1)) ); do
112 eval "$@ --max $NPROC --index $i &"
114 if [ $VERBOSE -eq 2 ] ; then
115 echo "${SPATCH_PID[$i]} running"
122 if [ "$USE_JOBS" = "yes" ]; then
130 for i in $(seq 0 $(( NPROC - 1 )) ); do
131 if [ $VERBOSE -eq 2 ] ; then
132 echo "Killing ${SPATCH_PID[$i]}"
134 kill ${SPATCH_PID[$i]} 2>/dev/null
138 # You can override heuristics with SPFLAGS, these must always go last
139 OPTIONS="$OPTIONS $SPFLAGS"
144 OPT=`grep "Option" $COCCI | cut -d':' -f2`
146 # The option '--parse-cocci' can be used to syntactically check the SmPL files.
148 # $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
150 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
152 FILE=`echo $COCCI | sed "s|$srctree/||"`
154 echo "Processing `basename $COCCI`"
155 echo "with option(s) \"$OPT\""
157 echo 'Message example to submit a patch:'
159 sed -ne 's|^///||p' $COCCI
161 if [ "$MODE" = "patch" ] ; then
162 echo ' The semantic patch that makes this change is available'
163 elif [ "$MODE" = "report" ] ; then
164 echo ' The semantic patch that makes this report is available'
165 elif [ "$MODE" = "context" ] ; then
166 echo ' The semantic patch that spots this code is available'
167 elif [ "$MODE" = "org" ] ; then
168 echo ' The semantic patch that makes this Org report is available'
170 echo ' The semantic patch that makes this output is available'
174 echo ' More information about semantic patching is available at'
175 echo ' http://coccinelle.lip6.fr/'
178 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
179 echo 'Semantic patch information:'
180 sed -ne 's|^//#||p' $COCCI
185 if [ "$MODE" = "chain" ] ; then
186 run_cmd $SPATCH -D patch \
187 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
188 run_cmd $SPATCH -D report \
189 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
190 run_cmd $SPATCH -D context \
191 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || \
192 run_cmd $SPATCH -D org \
193 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
194 elif [ "$MODE" = "rep+ctxt" ] ; then
195 run_cmd $SPATCH -D report \
196 $FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
197 run_cmd $SPATCH -D context \
198 $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
200 run_cmd $SPATCH -D $MODE $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
205 if [ "$COCCI" = "" ] ; then
206 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do