xref: /linux/scripts/coccicheck (revision 35d88a38710583f3b5afc3255d7bc190a50e1af7)
174425eeeSNicolas Palix#!/bin/sh
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
141e9dea2aSNicolas Palixif [ "$C" = "1" -o "$C" = "2" ]; then
151e9dea2aSNicolas Palix    ONLINE=1
161e9dea2aSNicolas Palix
171e9dea2aSNicolas Palix# This requires Coccinelle >= 0.2.3
181e9dea2aSNicolas Palix#    FLAGS="-ignore_unknown_options -very_quiet"
191e9dea2aSNicolas Palix#    OPTIONS=$*
201e9dea2aSNicolas Palix
211e9dea2aSNicolas Palix# Workaround for Coccinelle < 0.2.3
221e9dea2aSNicolas Palix	FLAGS="-I $srctree/include -very_quiet"
231e9dea2aSNicolas Palix	shift $(( $# - 1 ))
241e9dea2aSNicolas Palix	OPTIONS=$1
251e9dea2aSNicolas Palixelse
261e9dea2aSNicolas Palix    ONLINE=0
271e9dea2aSNicolas Palix    FLAGS="-very_quiet"
28d0bc1fb4SGreg Dietsche    if [ "$KBUILD_EXTMOD" = "" ] ; then
292c1160c8SNicolas Palix        OPTIONS="-dir $srctree"
30d0bc1fb4SGreg Dietsche    else
31d0bc1fb4SGreg Dietsche        OPTIONS="-dir $KBUILD_EXTMOD -patch $srctree -I $srctree/include -I $KBUILD_EXTMOD/include"
32d0bc1fb4SGreg Dietsche    fi
331e9dea2aSNicolas Palixfi
341e9dea2aSNicolas Palix
3574425eeeSNicolas Palixif [ ! -x "$SPATCH" ]; then
3674425eeeSNicolas Palix    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
3774425eeeSNicolas Palix    exit 1
3874425eeeSNicolas Palixfi
3974425eeeSNicolas Palix
4074425eeeSNicolas Palixif [ "$MODE" = "" ] ; then
411e9dea2aSNicolas Palix    if [ "$ONLINE" = "0" ] ; then
422c1160c8SNicolas Palix	echo 'You have not explicitly specified the mode to use. Using default "chain" mode.'
432c1160c8SNicolas Palix	echo 'All available modes will be tried (in that order): patch, report, context, org'
4474425eeeSNicolas Palix	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
451e9dea2aSNicolas Palix    fi
462c1160c8SNicolas Palix    MODE="chain"
4703ee0c42SNicolas Palixelif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
4803ee0c42SNicolas Palix    FLAGS="$FLAGS -no_show_diff"
4974425eeeSNicolas Palixfi
5074425eeeSNicolas Palix
511e9dea2aSNicolas Palixif [ "$ONLINE" = "0" ] ; then
5274425eeeSNicolas Palix    echo ''
5374425eeeSNicolas Palix    echo 'Please check for false positives in the output before submitting a patch.'
5474425eeeSNicolas Palix    echo 'When using "patch" mode, carefully review the patch before submitting it.'
5574425eeeSNicolas Palix    echo ''
561e9dea2aSNicolas Palixfi
5774425eeeSNicolas Palix
585303265aSBernd Schubertrun_cmd() {
595303265aSBernd Schubert	if [ $VERBOSE -ne 0 ] ; then
605303265aSBernd Schubert		echo "Running: $@"
615303265aSBernd Schubert	fi
625303265aSBernd Schubert	eval $@
635303265aSBernd Schubert}
645303265aSBernd Schubert
655303265aSBernd Schubert
661e9dea2aSNicolas Palixcoccinelle () {
6774425eeeSNicolas Palix    COCCI="$1"
6874425eeeSNicolas Palix
6974425eeeSNicolas Palix    OPT=`grep "Option" $COCCI | cut -d':' -f2`
701e9dea2aSNicolas Palix
71062c1825SNicolas Palix#   The option '-parse_cocci' can be used to syntactically check the SmPL files.
721e9dea2aSNicolas Palix#
731e9dea2aSNicolas Palix#    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
741e9dea2aSNicolas Palix
75*35d88a38SNicolas Palix    if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
761e9dea2aSNicolas Palix
771e9dea2aSNicolas Palix	FILE=`echo $COCCI | sed "s|$srctree/||"`
7874425eeeSNicolas Palix
793c908417SNicolas Palix	echo "Processing `basename $COCCI`"
803c908417SNicolas Palix	echo "with option(s) \"$OPT\""
813c908417SNicolas Palix	echo ''
8274425eeeSNicolas Palix	echo 'Message example to submit a patch:'
8374425eeeSNicolas Palix
843c908417SNicolas Palix	sed -ne 's|^///||p' $COCCI
8574425eeeSNicolas Palix
86062c1825SNicolas Palix	if [ "$MODE" = "patch" ] ; then
8774425eeeSNicolas Palix	    echo ' The semantic patch that makes this change is available'
88062c1825SNicolas Palix	elif [ "$MODE" = "report" ] ; then
89062c1825SNicolas Palix	    echo ' The semantic patch that makes this report is available'
90062c1825SNicolas Palix	elif [ "$MODE" = "context" ] ; then
91062c1825SNicolas Palix	    echo ' The semantic patch that spots this code is available'
92062c1825SNicolas Palix	elif [ "$MODE" = "org" ] ; then
93062c1825SNicolas Palix	    echo ' The semantic patch that makes this Org report is available'
94062c1825SNicolas Palix	else
95062c1825SNicolas Palix	    echo ' The semantic patch that makes this output is available'
96062c1825SNicolas Palix	fi
9774425eeeSNicolas Palix	echo " in $FILE."
9874425eeeSNicolas Palix	echo ''
9974425eeeSNicolas Palix	echo ' More information about semantic patching is available at'
10074425eeeSNicolas Palix	echo ' http://coccinelle.lip6.fr/'
10174425eeeSNicolas Palix	echo ''
10274425eeeSNicolas Palix
1033c908417SNicolas Palix	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
1043c908417SNicolas Palix	    echo 'Semantic patch information:'
1053c908417SNicolas Palix	    sed -ne 's|^//#||p' $COCCI
1063c908417SNicolas Palix	    echo ''
1073c908417SNicolas Palix	fi
1082c1160c8SNicolas Palix    fi
1093c908417SNicolas Palix
1102c1160c8SNicolas Palix    if [ "$MODE" = "chain" ] ; then
1115303265aSBernd Schubert	run_cmd $SPATCH -D patch   \
1125303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
1135303265aSBernd Schubert	run_cmd $SPATCH -D report  \
1145303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
1155303265aSBernd Schubert	run_cmd $SPATCH -D context \
1165303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
1175303265aSBernd Schubert	run_cmd $SPATCH -D org     \
1185303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
119c05cd6ddSNicolas Palix    elif [ "$MODE" = "rep+ctxt" ] ; then
1205303265aSBernd Schubert	run_cmd $SPATCH -D report  \
1215303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
1225303265aSBernd Schubert	run_cmd $SPATCH -D context \
1235303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1241e9dea2aSNicolas Palix    else
1255303265aSBernd Schubert	run_cmd $SPATCH -D $MODE   $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1261e9dea2aSNicolas Palix    fi
12774425eeeSNicolas Palix
12874425eeeSNicolas Palix}
12974425eeeSNicolas Palix
13074425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then
13174425eeeSNicolas Palix    for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1321e9dea2aSNicolas Palix	coccinelle $f
13374425eeeSNicolas Palix    done
13474425eeeSNicolas Palixelse
1351e9dea2aSNicolas Palix    coccinelle $COCCI
13674425eeeSNicolas Palixfi
137