xref: /linux/scripts/coccicheck (revision 1f0a6742dd900b866c046e29d287c28f0df9bd51)
19e395550SNicolas Palix#!/bin/bash
274425eeeSNicolas Palix
374425eeeSNicolas PalixSPATCH="`which ${SPATCH:=spatch}`"
474425eeeSNicolas Palix
526e56720SBernd Schubert# The verbosity may be set by the environmental parameter V=
626e56720SBernd Schubert# as for example with 'make V=1 coccicheck'
726e56720SBernd Schubert
826e56720SBernd Schubertif [ -n "$V" -a "$V" != "0" ]; then
926e56720SBernd Schubert	VERBOSE=1
1026e56720SBernd Schubertelse
1126e56720SBernd Schubert	VERBOSE=0
1226e56720SBernd Schubertfi
1326e56720SBernd Schubert
14ed621cc4SNicolas PalixFLAGS="$SPFLAGS -very_quiet"
159e395550SNicolas Palix
169e395550SNicolas Palix# spatch only allows include directories with the syntax "-I include"
179e395550SNicolas Palix# while gcc also allows "-Iinclude" and "-include include"
189e395550SNicolas PalixCOCCIINCLUDE=${LINUXINCLUDE//-I/-I }
199e395550SNicolas PalixCOCCIINCLUDE=${COCCIINCLUDE//-include/-I}
209e395550SNicolas Palix
211e9dea2aSNicolas Palixif [ "$C" = "1" -o "$C" = "2" ]; then
221e9dea2aSNicolas Palix    ONLINE=1
231e9dea2aSNicolas Palix
249e395550SNicolas Palix    # Take only the last argument, which is the C file to test
251e9dea2aSNicolas Palix    shift $(( $# - 1 ))
269e395550SNicolas Palix    OPTIONS="$COCCIINCLUDE $1"
271e9dea2aSNicolas Palixelse
281e9dea2aSNicolas Palix    ONLINE=0
29d0bc1fb4SGreg Dietsche    if [ "$KBUILD_EXTMOD" = "" ] ; then
309e395550SNicolas Palix        OPTIONS="-dir $srctree $COCCIINCLUDE"
31d0bc1fb4SGreg Dietsche    else
32bad6a409SNicolas Palix        OPTIONS="-dir $KBUILD_EXTMOD $COCCIINCLUDE"
33d0bc1fb4SGreg Dietsche    fi
341e9dea2aSNicolas Palixfi
351e9dea2aSNicolas Palix
36bad6a409SNicolas Palixif [ "$KBUILD_EXTMOD" != "" ] ; then
37bad6a409SNicolas Palix    OPTIONS="-patch $srctree $OPTIONS"
38bad6a409SNicolas Palixfi
39bad6a409SNicolas Palix
4074425eeeSNicolas Palixif [ ! -x "$SPATCH" ]; then
4174425eeeSNicolas Palix    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
4274425eeeSNicolas Palix    exit 1
4374425eeeSNicolas Palixfi
4474425eeeSNicolas Palix
4574425eeeSNicolas Palixif [ "$MODE" = "" ] ; then
461e9dea2aSNicolas Palix    if [ "$ONLINE" = "0" ] ; then
47*1f0a6742SNicolas Palix	echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
48*1f0a6742SNicolas Palix	echo 'Available modes are the following: patch, report, context, org'
4974425eeeSNicolas Palix	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
50*1f0a6742SNicolas Palix	echo 'Note however that some modes are not implemented by some semantic patches.'
511e9dea2aSNicolas Palix    fi
52*1f0a6742SNicolas Palix    MODE="report"
53*1f0a6742SNicolas Palixfi
54*1f0a6742SNicolas Palix
55*1f0a6742SNicolas Palixif [ "$MODE" = "chain" ] ; then
56*1f0a6742SNicolas Palix    if [ "$ONLINE" = "0" ] ; then
57*1f0a6742SNicolas Palix	echo 'You have selected the "chain" mode.'
58*1f0a6742SNicolas Palix	echo 'All available modes will be tried (in that order): patch, report, context, org'
59*1f0a6742SNicolas Palix    fi
6003ee0c42SNicolas Palixelif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
6103ee0c42SNicolas Palix    FLAGS="$FLAGS -no_show_diff"
6274425eeeSNicolas Palixfi
6374425eeeSNicolas Palix
641e9dea2aSNicolas Palixif [ "$ONLINE" = "0" ] ; then
6574425eeeSNicolas Palix    echo ''
6674425eeeSNicolas Palix    echo 'Please check for false positives in the output before submitting a patch.'
6774425eeeSNicolas Palix    echo 'When using "patch" mode, carefully review the patch before submitting it.'
6874425eeeSNicolas Palix    echo ''
691e9dea2aSNicolas Palixfi
7074425eeeSNicolas Palix
715303265aSBernd Schubertrun_cmd() {
725303265aSBernd Schubert	if [ $VERBOSE -ne 0 ] ; then
735303265aSBernd Schubert		echo "Running: $@"
745303265aSBernd Schubert	fi
755303265aSBernd Schubert	eval $@
765303265aSBernd Schubert}
775303265aSBernd Schubert
785303265aSBernd Schubert
791e9dea2aSNicolas Palixcoccinelle () {
8074425eeeSNicolas Palix    COCCI="$1"
8174425eeeSNicolas Palix
8274425eeeSNicolas Palix    OPT=`grep "Option" $COCCI | cut -d':' -f2`
831e9dea2aSNicolas Palix
84062c1825SNicolas Palix#   The option '-parse_cocci' can be used to syntactically check the SmPL files.
851e9dea2aSNicolas Palix#
861e9dea2aSNicolas Palix#    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
871e9dea2aSNicolas Palix
8835d88a38SNicolas Palix    if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
891e9dea2aSNicolas Palix
901e9dea2aSNicolas Palix	FILE=`echo $COCCI | sed "s|$srctree/||"`
9174425eeeSNicolas Palix
923c908417SNicolas Palix	echo "Processing `basename $COCCI`"
933c908417SNicolas Palix	echo "with option(s) \"$OPT\""
943c908417SNicolas Palix	echo ''
9574425eeeSNicolas Palix	echo 'Message example to submit a patch:'
9674425eeeSNicolas Palix
973c908417SNicolas Palix	sed -ne 's|^///||p' $COCCI
9874425eeeSNicolas Palix
99062c1825SNicolas Palix	if [ "$MODE" = "patch" ] ; then
10074425eeeSNicolas Palix	    echo ' The semantic patch that makes this change is available'
101062c1825SNicolas Palix	elif [ "$MODE" = "report" ] ; then
102062c1825SNicolas Palix	    echo ' The semantic patch that makes this report is available'
103062c1825SNicolas Palix	elif [ "$MODE" = "context" ] ; then
104062c1825SNicolas Palix	    echo ' The semantic patch that spots this code is available'
105062c1825SNicolas Palix	elif [ "$MODE" = "org" ] ; then
106062c1825SNicolas Palix	    echo ' The semantic patch that makes this Org report is available'
107062c1825SNicolas Palix	else
108062c1825SNicolas Palix	    echo ' The semantic patch that makes this output is available'
109062c1825SNicolas Palix	fi
11074425eeeSNicolas Palix	echo " in $FILE."
11174425eeeSNicolas Palix	echo ''
11274425eeeSNicolas Palix	echo ' More information about semantic patching is available at'
11374425eeeSNicolas Palix	echo ' http://coccinelle.lip6.fr/'
11474425eeeSNicolas Palix	echo ''
11574425eeeSNicolas Palix
1163c908417SNicolas Palix	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
1173c908417SNicolas Palix	    echo 'Semantic patch information:'
1183c908417SNicolas Palix	    sed -ne 's|^//#||p' $COCCI
1193c908417SNicolas Palix	    echo ''
1203c908417SNicolas Palix	fi
1212c1160c8SNicolas Palix    fi
1223c908417SNicolas Palix
1232c1160c8SNicolas Palix    if [ "$MODE" = "chain" ] ; then
1245303265aSBernd Schubert	run_cmd $SPATCH -D patch   \
1255303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
1265303265aSBernd Schubert	run_cmd $SPATCH -D report  \
1275303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
1285303265aSBernd Schubert	run_cmd $SPATCH -D context \
1295303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
1305303265aSBernd Schubert	run_cmd $SPATCH -D org     \
1315303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
132c05cd6ddSNicolas Palix    elif [ "$MODE" = "rep+ctxt" ] ; then
1335303265aSBernd Schubert	run_cmd $SPATCH -D report  \
1345303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
1355303265aSBernd Schubert	run_cmd $SPATCH -D context \
1365303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1371e9dea2aSNicolas Palix    else
1385303265aSBernd Schubert	run_cmd $SPATCH -D $MODE   $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1391e9dea2aSNicolas Palix    fi
14074425eeeSNicolas Palix
14174425eeeSNicolas Palix}
14274425eeeSNicolas Palix
14374425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then
14474425eeeSNicolas Palix    for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1451e9dea2aSNicolas Palix	coccinelle $f
14674425eeeSNicolas Palix    done
14774425eeeSNicolas Palixelse
1481e9dea2aSNicolas Palix    coccinelle $COCCI
14974425eeeSNicolas Palixfi
150