xref: /linux/scripts/coccicheck (revision 90d06a46835ba73deffb483970fdc2bffa4bb274)
19e395550SNicolas Palix#!/bin/bash
274425eeeSNicolas Palix
374425eeeSNicolas PalixSPATCH="`which ${SPATCH:=spatch}`"
474425eeeSNicolas Palix
5*90d06a46SKees Cooktrap kill_running SIGTERM SIGINT
6*90d06a46SKees Cookdeclare -a SPATCH_PID
7*90d06a46SKees Cook
826e56720SBernd Schubert# The verbosity may be set by the environmental parameter V=
926e56720SBernd Schubert# as for example with 'make V=1 coccicheck'
1026e56720SBernd Schubert
1126e56720SBernd Schubertif [ -n "$V" -a "$V" != "0" ]; then
12*90d06a46SKees Cook	VERBOSE="$V"
1326e56720SBernd Schubertelse
1426e56720SBernd Schubert	VERBOSE=0
1526e56720SBernd Schubertfi
1626e56720SBernd Schubert
17*90d06a46SKees Cookif [ -z "$J" ]; then
18*90d06a46SKees Cook	NPROC=$(getconf _NPROCESSORS_ONLN)
19*90d06a46SKees Cookelse
20*90d06a46SKees Cook	NPROC="$J"
21*90d06a46SKees Cookfi
22*90d06a46SKees Cook
23ed621cc4SNicolas PalixFLAGS="$SPFLAGS -very_quiet"
249e395550SNicolas Palix
259e395550SNicolas Palix# spatch only allows include directories with the syntax "-I include"
269e395550SNicolas Palix# while gcc also allows "-Iinclude" and "-include include"
279e395550SNicolas PalixCOCCIINCLUDE=${LINUXINCLUDE//-I/-I }
289e395550SNicolas PalixCOCCIINCLUDE=${COCCIINCLUDE//-include/-I}
299e395550SNicolas Palix
301e9dea2aSNicolas Palixif [ "$C" = "1" -o "$C" = "2" ]; then
311e9dea2aSNicolas Palix    ONLINE=1
321e9dea2aSNicolas Palix
339e395550SNicolas Palix    # Take only the last argument, which is the C file to test
341e9dea2aSNicolas Palix    shift $(( $# - 1 ))
359e395550SNicolas Palix    OPTIONS="$COCCIINCLUDE $1"
361e9dea2aSNicolas Palixelse
371e9dea2aSNicolas Palix    ONLINE=0
38d0bc1fb4SGreg Dietsche    if [ "$KBUILD_EXTMOD" = "" ] ; then
399e395550SNicolas Palix        OPTIONS="-dir $srctree $COCCIINCLUDE"
40d0bc1fb4SGreg Dietsche    else
41bad6a409SNicolas Palix        OPTIONS="-dir $KBUILD_EXTMOD $COCCIINCLUDE"
42d0bc1fb4SGreg Dietsche    fi
431e9dea2aSNicolas Palixfi
441e9dea2aSNicolas Palix
45bad6a409SNicolas Palixif [ "$KBUILD_EXTMOD" != "" ] ; then
46bad6a409SNicolas Palix    OPTIONS="-patch $srctree $OPTIONS"
47bad6a409SNicolas Palixfi
48bad6a409SNicolas Palix
4974425eeeSNicolas Palixif [ ! -x "$SPATCH" ]; then
5074425eeeSNicolas Palix    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
5174425eeeSNicolas Palix    exit 1
5274425eeeSNicolas Palixfi
5374425eeeSNicolas Palix
5474425eeeSNicolas Palixif [ "$MODE" = "" ] ; then
551e9dea2aSNicolas Palix    if [ "$ONLINE" = "0" ] ; then
561f0a6742SNicolas Palix	echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
571f0a6742SNicolas Palix	echo 'Available modes are the following: patch, report, context, org'
5874425eeeSNicolas Palix	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
591f0a6742SNicolas Palix	echo 'Note however that some modes are not implemented by some semantic patches.'
601e9dea2aSNicolas Palix    fi
611f0a6742SNicolas Palix    MODE="report"
621f0a6742SNicolas Palixfi
631f0a6742SNicolas Palix
641f0a6742SNicolas Palixif [ "$MODE" = "chain" ] ; then
651f0a6742SNicolas Palix    if [ "$ONLINE" = "0" ] ; then
661f0a6742SNicolas Palix	echo 'You have selected the "chain" mode.'
671f0a6742SNicolas Palix	echo 'All available modes will be tried (in that order): patch, report, context, org'
681f0a6742SNicolas Palix    fi
6903ee0c42SNicolas Palixelif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
7003ee0c42SNicolas Palix    FLAGS="$FLAGS -no_show_diff"
7174425eeeSNicolas Palixfi
7274425eeeSNicolas Palix
731e9dea2aSNicolas Palixif [ "$ONLINE" = "0" ] ; then
7474425eeeSNicolas Palix    echo ''
7574425eeeSNicolas Palix    echo 'Please check for false positives in the output before submitting a patch.'
7674425eeeSNicolas Palix    echo 'When using "patch" mode, carefully review the patch before submitting it.'
7774425eeeSNicolas Palix    echo ''
781e9dea2aSNicolas Palixfi
7974425eeeSNicolas Palix
805303265aSBernd Schubertrun_cmd() {
81*90d06a46SKees Cook	local i
825303265aSBernd Schubert	if [ $VERBOSE -ne 0 ] ; then
83*90d06a46SKees Cook		echo "Running ($NPROC in parallel): $@"
845303265aSBernd Schubert	fi
85*90d06a46SKees Cook	for i in $(seq 0 $(( NPROC - 1)) ); do
86*90d06a46SKees Cook		eval "$@ -max $NPROC -index $i &"
87*90d06a46SKees Cook		SPATCH_PID[$i]=$!
88*90d06a46SKees Cook		if [ $VERBOSE -eq 2 ] ; then
89*90d06a46SKees Cook			echo "${SPATCH_PID[$i]} running"
90*90d06a46SKees Cook		fi
91*90d06a46SKees Cook	done
92*90d06a46SKees Cook	wait
935303265aSBernd Schubert}
945303265aSBernd Schubert
95*90d06a46SKees Cookkill_running() {
96*90d06a46SKees Cook	for i in $(seq $(( NPROC - 1 )) ); do
97*90d06a46SKees Cook		if [ $VERBOSE -eq 2 ] ; then
98*90d06a46SKees Cook			echo "Killing ${SPATCH_PID[$i]}"
99*90d06a46SKees Cook		fi
100*90d06a46SKees Cook		kill ${SPATCH_PID[$i]} 2>/dev/null
101*90d06a46SKees Cook	done
102*90d06a46SKees Cook}
1035303265aSBernd Schubert
1041e9dea2aSNicolas Palixcoccinelle () {
10574425eeeSNicolas Palix    COCCI="$1"
10674425eeeSNicolas Palix
10774425eeeSNicolas Palix    OPT=`grep "Option" $COCCI | cut -d':' -f2`
1081e9dea2aSNicolas Palix
109062c1825SNicolas Palix#   The option '-parse_cocci' can be used to syntactically check the SmPL files.
1101e9dea2aSNicolas Palix#
1111e9dea2aSNicolas Palix#    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
1121e9dea2aSNicolas Palix
11335d88a38SNicolas Palix    if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
1141e9dea2aSNicolas Palix
1151e9dea2aSNicolas Palix	FILE=`echo $COCCI | sed "s|$srctree/||"`
11674425eeeSNicolas Palix
1173c908417SNicolas Palix	echo "Processing `basename $COCCI`"
1183c908417SNicolas Palix	echo "with option(s) \"$OPT\""
1193c908417SNicolas Palix	echo ''
12074425eeeSNicolas Palix	echo 'Message example to submit a patch:'
12174425eeeSNicolas Palix
1223c908417SNicolas Palix	sed -ne 's|^///||p' $COCCI
12374425eeeSNicolas Palix
124062c1825SNicolas Palix	if [ "$MODE" = "patch" ] ; then
12574425eeeSNicolas Palix	    echo ' The semantic patch that makes this change is available'
126062c1825SNicolas Palix	elif [ "$MODE" = "report" ] ; then
127062c1825SNicolas Palix	    echo ' The semantic patch that makes this report is available'
128062c1825SNicolas Palix	elif [ "$MODE" = "context" ] ; then
129062c1825SNicolas Palix	    echo ' The semantic patch that spots this code is available'
130062c1825SNicolas Palix	elif [ "$MODE" = "org" ] ; then
131062c1825SNicolas Palix	    echo ' The semantic patch that makes this Org report is available'
132062c1825SNicolas Palix	else
133062c1825SNicolas Palix	    echo ' The semantic patch that makes this output is available'
134062c1825SNicolas Palix	fi
13574425eeeSNicolas Palix	echo " in $FILE."
13674425eeeSNicolas Palix	echo ''
13774425eeeSNicolas Palix	echo ' More information about semantic patching is available at'
13874425eeeSNicolas Palix	echo ' http://coccinelle.lip6.fr/'
13974425eeeSNicolas Palix	echo ''
14074425eeeSNicolas Palix
1413c908417SNicolas Palix	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
1423c908417SNicolas Palix	    echo 'Semantic patch information:'
1433c908417SNicolas Palix	    sed -ne 's|^//#||p' $COCCI
1443c908417SNicolas Palix	    echo ''
1453c908417SNicolas Palix	fi
1462c1160c8SNicolas Palix    fi
1473c908417SNicolas Palix
1482c1160c8SNicolas Palix    if [ "$MODE" = "chain" ] ; then
1495303265aSBernd Schubert	run_cmd $SPATCH -D patch   \
1505303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
1515303265aSBernd Schubert	run_cmd $SPATCH -D report  \
1525303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
1535303265aSBernd Schubert	run_cmd $SPATCH -D context \
1545303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
1555303265aSBernd Schubert	run_cmd $SPATCH -D org     \
1565303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
157c05cd6ddSNicolas Palix    elif [ "$MODE" = "rep+ctxt" ] ; then
1585303265aSBernd Schubert	run_cmd $SPATCH -D report  \
1595303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
1605303265aSBernd Schubert	run_cmd $SPATCH -D context \
1615303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1621e9dea2aSNicolas Palix    else
1635303265aSBernd Schubert	run_cmd $SPATCH -D $MODE   $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1641e9dea2aSNicolas Palix    fi
16574425eeeSNicolas Palix
16674425eeeSNicolas Palix}
16774425eeeSNicolas Palix
16874425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then
16974425eeeSNicolas Palix    for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1701e9dea2aSNicolas Palix	coccinelle $f
17174425eeeSNicolas Palix    done
17274425eeeSNicolas Palixelse
1731e9dea2aSNicolas Palix    coccinelle $COCCI
17474425eeeSNicolas Palixfi
175