xref: /linux/scripts/coccicheck (revision b24413180f5600bcb3bb70fbed5cf186b60864bd)
19e395550SNicolas Palix#!/bin/bash
2*b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
3c100d537SLuis R. Rodriguez# Linux kernel coccicheck
4c100d537SLuis R. Rodriguez#
51e01892eSMarkus Elfring# Read Documentation/dev-tools/coccinelle.rst
6ec97946eSNicolas Palix#
7ec97946eSNicolas Palix# This script requires at least spatch
8ec97946eSNicolas Palix# version 1.0.0-rc11.
9ec97946eSNicolas Palix
10a9e064c0SLuis R. RodriguezDIR="$(dirname $(readlink -f $0))/.."
1174425eeeSNicolas PalixSPATCH="`which ${SPATCH:=spatch}`"
1274425eeeSNicolas Palix
1313d94865SLuis R. Rodriguezif [ ! -x "$SPATCH" ]; then
1413d94865SLuis R. Rodriguez    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
1513d94865SLuis R. Rodriguez    exit 1
1613d94865SLuis R. Rodriguezfi
1713d94865SLuis R. Rodriguez
18a9e064c0SLuis R. RodriguezSPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}')
19a9e064c0SLuis R. RodriguezSPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh)
20a9e064c0SLuis R. Rodriguez
21c930a1b2SLuis R. RodriguezUSE_JOBS="no"
22c930a1b2SLuis R. Rodriguez$SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
2390d06a46SKees Cook
2426e56720SBernd Schubert# The verbosity may be set by the environmental parameter V=
2526e56720SBernd Schubert# as for example with 'make V=1 coccicheck'
2626e56720SBernd Schubert
2726e56720SBernd Schubertif [ -n "$V" -a "$V" != "0" ]; then
2890d06a46SKees Cook	VERBOSE="$V"
2926e56720SBernd Schubertelse
3026e56720SBernd Schubert	VERBOSE=0
3126e56720SBernd Schubertfi
3226e56720SBernd Schubert
3390d06a46SKees Cookif [ -z "$J" ]; then
3490d06a46SKees Cook	NPROC=$(getconf _NPROCESSORS_ONLN)
3590d06a46SKees Cookelse
3690d06a46SKees Cook	NPROC="$J"
3790d06a46SKees Cookfi
3890d06a46SKees Cook
398e826ad5SLuis R. RodriguezFLAGS="--very-quiet"
409e395550SNicolas Palix
415c384dbaSLuis R. Rodriguez# You can use SPFLAGS to append extra arguments to coccicheck or override any
425c384dbaSLuis R. Rodriguez# heuristics done in this file as Coccinelle accepts the last options when
435c384dbaSLuis R. Rodriguez# options conflict.
445c384dbaSLuis R. Rodriguez#
455c384dbaSLuis R. Rodriguez# A good example for use of SPFLAGS is if you want to debug your cocci script,
465c384dbaSLuis R. Rodriguez# you can for instance use the following:
475c384dbaSLuis R. Rodriguez#
485c384dbaSLuis R. Rodriguez# $ export COCCI=scripts/coccinelle/misc/irqf_oneshot.cocci
495c384dbaSLuis R. Rodriguez# $ make coccicheck MODE=report DEBUG_FILE="all.err" SPFLAGS="--profile --show-trying" M=./drivers/mfd/arizona-irq.c
505c384dbaSLuis R. Rodriguez#
515c384dbaSLuis R. Rodriguez# "--show-trying" should show you what rule is being processed as it goes to
525c384dbaSLuis R. Rodriguez# stdout, you do not need a debug file for that. The profile output will be
535c384dbaSLuis R. Rodriguez# be sent to stdout, if you provide a DEBUG_FILE the profiling data can be
545c384dbaSLuis R. Rodriguez# inspected there.
555c384dbaSLuis R. Rodriguez#
565c384dbaSLuis R. Rodriguez# --profile will not output if --very-quiet is used, so avoid it.
575c384dbaSLuis R. Rodriguezecho $SPFLAGS | egrep -e "--profile|--show-trying" 2>&1 > /dev/null
585c384dbaSLuis R. Rodriguezif [ $? -eq 0 ]; then
595c384dbaSLuis R. Rodriguez	FLAGS="--quiet"
605c384dbaSLuis R. Rodriguezfi
615c384dbaSLuis R. Rodriguez
629e395550SNicolas Palix# spatch only allows include directories with the syntax "-I include"
639e395550SNicolas Palix# while gcc also allows "-Iinclude" and "-include include"
649e395550SNicolas PalixCOCCIINCLUDE=${LINUXINCLUDE//-I/-I }
655b169108SAndrzej HajdaCOCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
669e395550SNicolas Palix
671e9dea2aSNicolas Palixif [ "$C" = "1" -o "$C" = "2" ]; then
681e9dea2aSNicolas Palix    ONLINE=1
691e9dea2aSNicolas Palix
709e395550SNicolas Palix    # Take only the last argument, which is the C file to test
711e9dea2aSNicolas Palix    shift $(( $# - 1 ))
729e395550SNicolas Palix    OPTIONS="$COCCIINCLUDE $1"
731e9dea2aSNicolas Palixelse
741e9dea2aSNicolas Palix    ONLINE=0
75d0bc1fb4SGreg Dietsche    if [ "$KBUILD_EXTMOD" = "" ] ; then
7693f14468SNicolas Palix        OPTIONS="--dir $srctree $COCCIINCLUDE"
77d0bc1fb4SGreg Dietsche    else
7893f14468SNicolas Palix        OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
79d0bc1fb4SGreg Dietsche    fi
801e9dea2aSNicolas Palixfi
811e9dea2aSNicolas Palix
82bad6a409SNicolas Palixif [ "$KBUILD_EXTMOD" != "" ] ; then
8393f14468SNicolas Palix    OPTIONS="--patch $srctree $OPTIONS"
84bad6a409SNicolas Palixfi
85bad6a409SNicolas Palix
86c930a1b2SLuis R. Rodriguez# You can override by using SPFLAGS
87c930a1b2SLuis R. Rodriguezif [ "$USE_JOBS" = "no" ]; then
88c930a1b2SLuis R. Rodriguez	trap kill_running SIGTERM SIGINT
89c930a1b2SLuis R. Rodriguez	declare -a SPATCH_PID
90c930a1b2SLuis R. Rodriguezelif [ "$NPROC" != "1" ]; then
91c930a1b2SLuis R. Rodriguez	# Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
92c930a1b2SLuis R. Rodriguez	# https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
93c930a1b2SLuis R. Rodriguez	OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
94c930a1b2SLuis R. Rodriguezfi
95c930a1b2SLuis R. Rodriguez
9674425eeeSNicolas Palixif [ "$MODE" = "" ] ; then
971e9dea2aSNicolas Palix    if [ "$ONLINE" = "0" ] ; then
981f0a6742SNicolas Palix	echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
991f0a6742SNicolas Palix	echo 'Available modes are the following: patch, report, context, org'
10074425eeeSNicolas Palix	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
1011f0a6742SNicolas Palix	echo 'Note however that some modes are not implemented by some semantic patches.'
1021e9dea2aSNicolas Palix    fi
1031f0a6742SNicolas Palix    MODE="report"
1041f0a6742SNicolas Palixfi
1051f0a6742SNicolas Palix
1061f0a6742SNicolas Palixif [ "$MODE" = "chain" ] ; then
1071f0a6742SNicolas Palix    if [ "$ONLINE" = "0" ] ; then
1081f0a6742SNicolas Palix	echo 'You have selected the "chain" mode.'
1091f0a6742SNicolas Palix	echo 'All available modes will be tried (in that order): patch, report, context, org'
1101f0a6742SNicolas Palix    fi
11103ee0c42SNicolas Palixelif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
1127a2358b3SDeepa Dinamani    FLAGS="--no-show-diff $FLAGS"
11374425eeeSNicolas Palixfi
11474425eeeSNicolas Palix
1151e9dea2aSNicolas Palixif [ "$ONLINE" = "0" ] ; then
11674425eeeSNicolas Palix    echo ''
11774425eeeSNicolas Palix    echo 'Please check for false positives in the output before submitting a patch.'
11874425eeeSNicolas Palix    echo 'When using "patch" mode, carefully review the patch before submitting it.'
11974425eeeSNicolas Palix    echo ''
1201e9dea2aSNicolas Palixfi
12174425eeeSNicolas Palix
122c930a1b2SLuis R. Rodriguezrun_cmd_parmap() {
123c930a1b2SLuis R. Rodriguez	if [ $VERBOSE -ne 0 ] ; then
124c930a1b2SLuis R. Rodriguez		echo "Running ($NPROC in parallel): $@"
125c930a1b2SLuis R. Rodriguez	fi
126be1fa900SLuis R. Rodriguez	if [ "$DEBUG_FILE" != "/dev/null" -a "$DEBUG_FILE" != "" ]; then
127be1fa900SLuis R. Rodriguez		if [ -f $DEBUG_FILE ]; then
128be1fa900SLuis R. Rodriguez			echo "Debug file $DEBUG_FILE exists, bailing"
129be1fa900SLuis R. Rodriguez			exit
130be1fa900SLuis R. Rodriguez		fi
131be1fa900SLuis R. Rodriguez	else
132be1fa900SLuis R. Rodriguez		DEBUG_FILE="/dev/null"
133be1fa900SLuis R. Rodriguez	fi
134be1fa900SLuis R. Rodriguez	$@ 2>$DEBUG_FILE
135c930a1b2SLuis R. Rodriguez	if [[ $? -ne 0 ]]; then
136c930a1b2SLuis R. Rodriguez		echo "coccicheck failed"
137c930a1b2SLuis R. Rodriguez		exit $?
138c930a1b2SLuis R. Rodriguez	fi
139c930a1b2SLuis R. Rodriguez}
140c930a1b2SLuis R. Rodriguez
141c930a1b2SLuis R. Rodriguezrun_cmd_old() {
14290d06a46SKees Cook	local i
1435303265aSBernd Schubert	if [ $VERBOSE -ne 0 ] ; then
14490d06a46SKees Cook		echo "Running ($NPROC in parallel): $@"
1455303265aSBernd Schubert	fi
14690d06a46SKees Cook	for i in $(seq 0 $(( NPROC - 1)) ); do
14793f14468SNicolas Palix		eval "$@ --max $NPROC --index $i &"
14890d06a46SKees Cook		SPATCH_PID[$i]=$!
14990d06a46SKees Cook		if [ $VERBOSE -eq 2 ] ; then
15090d06a46SKees Cook			echo "${SPATCH_PID[$i]} running"
15190d06a46SKees Cook		fi
15290d06a46SKees Cook	done
15390d06a46SKees Cook	wait
1545303265aSBernd Schubert}
1555303265aSBernd Schubert
156c930a1b2SLuis R. Rodriguezrun_cmd() {
157c930a1b2SLuis R. Rodriguez	if [ "$USE_JOBS" = "yes" ]; then
158c930a1b2SLuis R. Rodriguez		run_cmd_parmap $@
159c930a1b2SLuis R. Rodriguez	else
160c930a1b2SLuis R. Rodriguez		run_cmd_old $@
161c930a1b2SLuis R. Rodriguez	fi
162c930a1b2SLuis R. Rodriguez}
163c930a1b2SLuis R. Rodriguez
16490d06a46SKees Cookkill_running() {
1652552a39fSKees Cook	for i in $(seq 0 $(( NPROC - 1 )) ); do
16690d06a46SKees Cook		if [ $VERBOSE -eq 2 ] ; then
16790d06a46SKees Cook			echo "Killing ${SPATCH_PID[$i]}"
16890d06a46SKees Cook		fi
16990d06a46SKees Cook		kill ${SPATCH_PID[$i]} 2>/dev/null
17090d06a46SKees Cook	done
17190d06a46SKees Cook}
1725303265aSBernd Schubert
1738e826ad5SLuis R. Rodriguez# You can override heuristics with SPFLAGS, these must always go last
1748e826ad5SLuis R. RodriguezOPTIONS="$OPTIONS $SPFLAGS"
1758e826ad5SLuis R. Rodriguez
1761e9dea2aSNicolas Palixcoccinelle () {
17774425eeeSNicolas Palix    COCCI="$1"
17874425eeeSNicolas Palix
17974425eeeSNicolas Palix    OPT=`grep "Option" $COCCI | cut -d':' -f2`
180a9e064c0SLuis R. Rodriguez    REQ=`grep "Requires" $COCCI | cut -d':' -f2 | sed "s| ||"`
181a9e064c0SLuis R. Rodriguez    REQ_NUM=$(echo $REQ | ${DIR}/scripts/ld-version.sh)
182a9e064c0SLuis R. Rodriguez    if [ "$REQ_NUM" != "0" ] ; then
183a9e064c0SLuis R. Rodriguez	    if [ "$SPATCH_VERSION_NUM" -lt "$REQ_NUM" ] ; then
184a9e064c0SLuis R. Rodriguez		    echo "Skipping coccinele SmPL patch: $COCCI"
185a9e064c0SLuis R. Rodriguez		    echo "You have coccinelle:           $SPATCH_VERSION"
186a9e064c0SLuis R. Rodriguez		    echo "This SmPL patch requires:      $REQ"
187a9e064c0SLuis R. Rodriguez		    return
188a9e064c0SLuis R. Rodriguez	    fi
189a9e064c0SLuis R. Rodriguez    fi
1901e9dea2aSNicolas Palix
19193f14468SNicolas Palix#   The option '--parse-cocci' can be used to syntactically check the SmPL files.
1921e9dea2aSNicolas Palix#
1931e9dea2aSNicolas Palix#    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
1941e9dea2aSNicolas Palix
19535d88a38SNicolas Palix    if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
1961e9dea2aSNicolas Palix
1971e9dea2aSNicolas Palix	FILE=`echo $COCCI | sed "s|$srctree/||"`
19874425eeeSNicolas Palix
1993c908417SNicolas Palix	echo "Processing `basename $COCCI`"
2003c908417SNicolas Palix	echo "with option(s) \"$OPT\""
2013c908417SNicolas Palix	echo ''
20274425eeeSNicolas Palix	echo 'Message example to submit a patch:'
20374425eeeSNicolas Palix
2043c908417SNicolas Palix	sed -ne 's|^///||p' $COCCI
20574425eeeSNicolas Palix
206062c1825SNicolas Palix	if [ "$MODE" = "patch" ] ; then
20774425eeeSNicolas Palix	    echo ' The semantic patch that makes this change is available'
208062c1825SNicolas Palix	elif [ "$MODE" = "report" ] ; then
209062c1825SNicolas Palix	    echo ' The semantic patch that makes this report is available'
210062c1825SNicolas Palix	elif [ "$MODE" = "context" ] ; then
211062c1825SNicolas Palix	    echo ' The semantic patch that spots this code is available'
212062c1825SNicolas Palix	elif [ "$MODE" = "org" ] ; then
213062c1825SNicolas Palix	    echo ' The semantic patch that makes this Org report is available'
214062c1825SNicolas Palix	else
215062c1825SNicolas Palix	    echo ' The semantic patch that makes this output is available'
216062c1825SNicolas Palix	fi
21774425eeeSNicolas Palix	echo " in $FILE."
21874425eeeSNicolas Palix	echo ''
21974425eeeSNicolas Palix	echo ' More information about semantic patching is available at'
22074425eeeSNicolas Palix	echo ' http://coccinelle.lip6.fr/'
22174425eeeSNicolas Palix	echo ''
22274425eeeSNicolas Palix
2233c908417SNicolas Palix	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
2243c908417SNicolas Palix	    echo 'Semantic patch information:'
2253c908417SNicolas Palix	    sed -ne 's|^//#||p' $COCCI
2263c908417SNicolas Palix	    echo ''
2273c908417SNicolas Palix	fi
2282c1160c8SNicolas Palix    fi
2293c908417SNicolas Palix
2302c1160c8SNicolas Palix    if [ "$MODE" = "chain" ] ; then
2315303265aSBernd Schubert	run_cmd $SPATCH -D patch   \
23293f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
2335303265aSBernd Schubert	run_cmd $SPATCH -D report  \
23493f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
2355303265aSBernd Schubert	run_cmd $SPATCH -D context \
23693f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
2375303265aSBernd Schubert	run_cmd $SPATCH -D org     \
23893f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
239c05cd6ddSNicolas Palix    elif [ "$MODE" = "rep+ctxt" ] ; then
2405303265aSBernd Schubert	run_cmd $SPATCH -D report  \
24193f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
2425303265aSBernd Schubert	run_cmd $SPATCH -D context \
24393f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
2441e9dea2aSNicolas Palix    else
24593f14468SNicolas Palix	run_cmd $SPATCH -D $MODE   $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
2461e9dea2aSNicolas Palix    fi
24774425eeeSNicolas Palix
24874425eeeSNicolas Palix}
24974425eeeSNicolas Palix
25074425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then
25174425eeeSNicolas Palix    for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
2521e9dea2aSNicolas Palix	coccinelle $f
25374425eeeSNicolas Palix    done
25474425eeeSNicolas Palixelse
2551e9dea2aSNicolas Palix    coccinelle $COCCI
25674425eeeSNicolas Palixfi
257