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