xref: /linux/scripts/coccicheck (revision 7a2358b3818691521c7df531415d1ea4d0398520)
19e395550SNicolas Palix#!/bin/bash
274425eeeSNicolas Palix
3ec97946eSNicolas Palix#
4ec97946eSNicolas Palix# This script requires at least spatch
5ec97946eSNicolas Palix# version 1.0.0-rc11.
6ec97946eSNicolas Palix#
7ec97946eSNicolas Palix
874425eeeSNicolas PalixSPATCH="`which ${SPATCH:=spatch}`"
974425eeeSNicolas Palix
1090d06a46SKees Cooktrap kill_running SIGTERM SIGINT
1190d06a46SKees Cookdeclare -a SPATCH_PID
1290d06a46SKees Cook
1326e56720SBernd Schubert# The verbosity may be set by the environmental parameter V=
1426e56720SBernd Schubert# as for example with 'make V=1 coccicheck'
1526e56720SBernd Schubert
1626e56720SBernd Schubertif [ -n "$V" -a "$V" != "0" ]; then
1790d06a46SKees Cook	VERBOSE="$V"
1826e56720SBernd Schubertelse
1926e56720SBernd Schubert	VERBOSE=0
2026e56720SBernd Schubertfi
2126e56720SBernd Schubert
2290d06a46SKees Cookif [ -z "$J" ]; then
2390d06a46SKees Cook	NPROC=$(getconf _NPROCESSORS_ONLN)
2490d06a46SKees Cookelse
2590d06a46SKees Cook	NPROC="$J"
2690d06a46SKees Cookfi
2790d06a46SKees Cook
28*7a2358b3SDeepa DinamaniFLAGS="--very-quiet $SPFLAGS"
299e395550SNicolas Palix
309e395550SNicolas Palix# spatch only allows include directories with the syntax "-I include"
319e395550SNicolas Palix# while gcc also allows "-Iinclude" and "-include include"
329e395550SNicolas PalixCOCCIINCLUDE=${LINUXINCLUDE//-I/-I }
335b169108SAndrzej HajdaCOCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
349e395550SNicolas Palix
351e9dea2aSNicolas Palixif [ "$C" = "1" -o "$C" = "2" ]; then
361e9dea2aSNicolas Palix    ONLINE=1
371e9dea2aSNicolas Palix
389e395550SNicolas Palix    # Take only the last argument, which is the C file to test
391e9dea2aSNicolas Palix    shift $(( $# - 1 ))
409e395550SNicolas Palix    OPTIONS="$COCCIINCLUDE $1"
411e9dea2aSNicolas Palixelse
421e9dea2aSNicolas Palix    ONLINE=0
43d0bc1fb4SGreg Dietsche    if [ "$KBUILD_EXTMOD" = "" ] ; then
4493f14468SNicolas Palix        OPTIONS="--dir $srctree $COCCIINCLUDE"
45d0bc1fb4SGreg Dietsche    else
4693f14468SNicolas Palix        OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
47d0bc1fb4SGreg Dietsche    fi
481e9dea2aSNicolas Palixfi
491e9dea2aSNicolas Palix
50bad6a409SNicolas Palixif [ "$KBUILD_EXTMOD" != "" ] ; then
5193f14468SNicolas Palix    OPTIONS="--patch $srctree $OPTIONS"
52bad6a409SNicolas Palixfi
53bad6a409SNicolas Palix
5474425eeeSNicolas Palixif [ ! -x "$SPATCH" ]; then
5574425eeeSNicolas Palix    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
5674425eeeSNicolas Palix    exit 1
5774425eeeSNicolas Palixfi
5874425eeeSNicolas Palix
5974425eeeSNicolas Palixif [ "$MODE" = "" ] ; then
601e9dea2aSNicolas Palix    if [ "$ONLINE" = "0" ] ; then
611f0a6742SNicolas Palix	echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
621f0a6742SNicolas Palix	echo 'Available modes are the following: patch, report, context, org'
6374425eeeSNicolas Palix	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
641f0a6742SNicolas Palix	echo 'Note however that some modes are not implemented by some semantic patches.'
651e9dea2aSNicolas Palix    fi
661f0a6742SNicolas Palix    MODE="report"
671f0a6742SNicolas Palixfi
681f0a6742SNicolas Palix
691f0a6742SNicolas Palixif [ "$MODE" = "chain" ] ; then
701f0a6742SNicolas Palix    if [ "$ONLINE" = "0" ] ; then
711f0a6742SNicolas Palix	echo 'You have selected the "chain" mode.'
721f0a6742SNicolas Palix	echo 'All available modes will be tried (in that order): patch, report, context, org'
731f0a6742SNicolas Palix    fi
7403ee0c42SNicolas Palixelif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
75*7a2358b3SDeepa Dinamani    FLAGS="--no-show-diff $FLAGS"
7674425eeeSNicolas Palixfi
7774425eeeSNicolas Palix
781e9dea2aSNicolas Palixif [ "$ONLINE" = "0" ] ; then
7974425eeeSNicolas Palix    echo ''
8074425eeeSNicolas Palix    echo 'Please check for false positives in the output before submitting a patch.'
8174425eeeSNicolas Palix    echo 'When using "patch" mode, carefully review the patch before submitting it.'
8274425eeeSNicolas Palix    echo ''
831e9dea2aSNicolas Palixfi
8474425eeeSNicolas Palix
855303265aSBernd Schubertrun_cmd() {
8690d06a46SKees Cook	local i
875303265aSBernd Schubert	if [ $VERBOSE -ne 0 ] ; then
8890d06a46SKees Cook		echo "Running ($NPROC in parallel): $@"
895303265aSBernd Schubert	fi
9090d06a46SKees Cook	for i in $(seq 0 $(( NPROC - 1)) ); do
9193f14468SNicolas Palix		eval "$@ --max $NPROC --index $i &"
9290d06a46SKees Cook		SPATCH_PID[$i]=$!
9390d06a46SKees Cook		if [ $VERBOSE -eq 2 ] ; then
9490d06a46SKees Cook			echo "${SPATCH_PID[$i]} running"
9590d06a46SKees Cook		fi
9690d06a46SKees Cook	done
9790d06a46SKees Cook	wait
985303265aSBernd Schubert}
995303265aSBernd Schubert
10090d06a46SKees Cookkill_running() {
1012552a39fSKees Cook	for i in $(seq 0 $(( NPROC - 1 )) ); do
10290d06a46SKees Cook		if [ $VERBOSE -eq 2 ] ; then
10390d06a46SKees Cook			echo "Killing ${SPATCH_PID[$i]}"
10490d06a46SKees Cook		fi
10590d06a46SKees Cook		kill ${SPATCH_PID[$i]} 2>/dev/null
10690d06a46SKees Cook	done
10790d06a46SKees Cook}
1085303265aSBernd Schubert
1091e9dea2aSNicolas Palixcoccinelle () {
11074425eeeSNicolas Palix    COCCI="$1"
11174425eeeSNicolas Palix
11274425eeeSNicolas Palix    OPT=`grep "Option" $COCCI | cut -d':' -f2`
1131e9dea2aSNicolas Palix
11493f14468SNicolas Palix#   The option '--parse-cocci' can be used to syntactically check the SmPL files.
1151e9dea2aSNicolas Palix#
1161e9dea2aSNicolas Palix#    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
1171e9dea2aSNicolas Palix
11835d88a38SNicolas Palix    if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
1191e9dea2aSNicolas Palix
1201e9dea2aSNicolas Palix	FILE=`echo $COCCI | sed "s|$srctree/||"`
12174425eeeSNicolas Palix
1223c908417SNicolas Palix	echo "Processing `basename $COCCI`"
1233c908417SNicolas Palix	echo "with option(s) \"$OPT\""
1243c908417SNicolas Palix	echo ''
12574425eeeSNicolas Palix	echo 'Message example to submit a patch:'
12674425eeeSNicolas Palix
1273c908417SNicolas Palix	sed -ne 's|^///||p' $COCCI
12874425eeeSNicolas Palix
129062c1825SNicolas Palix	if [ "$MODE" = "patch" ] ; then
13074425eeeSNicolas Palix	    echo ' The semantic patch that makes this change is available'
131062c1825SNicolas Palix	elif [ "$MODE" = "report" ] ; then
132062c1825SNicolas Palix	    echo ' The semantic patch that makes this report is available'
133062c1825SNicolas Palix	elif [ "$MODE" = "context" ] ; then
134062c1825SNicolas Palix	    echo ' The semantic patch that spots this code is available'
135062c1825SNicolas Palix	elif [ "$MODE" = "org" ] ; then
136062c1825SNicolas Palix	    echo ' The semantic patch that makes this Org report is available'
137062c1825SNicolas Palix	else
138062c1825SNicolas Palix	    echo ' The semantic patch that makes this output is available'
139062c1825SNicolas Palix	fi
14074425eeeSNicolas Palix	echo " in $FILE."
14174425eeeSNicolas Palix	echo ''
14274425eeeSNicolas Palix	echo ' More information about semantic patching is available at'
14374425eeeSNicolas Palix	echo ' http://coccinelle.lip6.fr/'
14474425eeeSNicolas Palix	echo ''
14574425eeeSNicolas Palix
1463c908417SNicolas Palix	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
1473c908417SNicolas Palix	    echo 'Semantic patch information:'
1483c908417SNicolas Palix	    sed -ne 's|^//#||p' $COCCI
1493c908417SNicolas Palix	    echo ''
1503c908417SNicolas Palix	fi
1512c1160c8SNicolas Palix    fi
1523c908417SNicolas Palix
1532c1160c8SNicolas Palix    if [ "$MODE" = "chain" ] ; then
1545303265aSBernd Schubert	run_cmd $SPATCH -D patch   \
15593f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
1565303265aSBernd Schubert	run_cmd $SPATCH -D report  \
15793f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
1585303265aSBernd Schubert	run_cmd $SPATCH -D context \
15993f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
1605303265aSBernd Schubert	run_cmd $SPATCH -D org     \
16193f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
162c05cd6ddSNicolas Palix    elif [ "$MODE" = "rep+ctxt" ] ; then
1635303265aSBernd Schubert	run_cmd $SPATCH -D report  \
16493f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
1655303265aSBernd Schubert	run_cmd $SPATCH -D context \
16693f14468SNicolas Palix		$FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
1671e9dea2aSNicolas Palix    else
16893f14468SNicolas Palix	run_cmd $SPATCH -D $MODE   $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
1691e9dea2aSNicolas Palix    fi
17074425eeeSNicolas Palix
17174425eeeSNicolas Palix}
17274425eeeSNicolas Palix
17374425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then
17474425eeeSNicolas Palix    for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1751e9dea2aSNicolas Palix	coccinelle $f
17674425eeeSNicolas Palix    done
17774425eeeSNicolas Palixelse
1781e9dea2aSNicolas Palix    coccinelle $COCCI
17974425eeeSNicolas Palixfi
180