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