xref: /linux-6.15/scripts/coccicheck (revision bad6a409)
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
32*bad6a409SNicolas Palix        OPTIONS="-dir $KBUILD_EXTMOD $COCCIINCLUDE"
33d0bc1fb4SGreg Dietsche    fi
341e9dea2aSNicolas Palixfi
351e9dea2aSNicolas Palix
36*bad6a409SNicolas Palixif [ "$KBUILD_EXTMOD" != "" ] ; then
37*bad6a409SNicolas Palix    OPTIONS="-patch $srctree $OPTIONS"
38*bad6a409SNicolas Palixfi
39*bad6a409SNicolas 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
472c1160c8SNicolas Palix	echo 'You have not explicitly specified the mode to use. Using default "chain" mode.'
482c1160c8SNicolas Palix	echo 'All available modes will be tried (in that order): patch, report, context, org'
4974425eeeSNicolas Palix	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
501e9dea2aSNicolas Palix    fi
512c1160c8SNicolas Palix    MODE="chain"
5203ee0c42SNicolas Palixelif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
5303ee0c42SNicolas Palix    FLAGS="$FLAGS -no_show_diff"
5474425eeeSNicolas Palixfi
5574425eeeSNicolas Palix
561e9dea2aSNicolas Palixif [ "$ONLINE" = "0" ] ; then
5774425eeeSNicolas Palix    echo ''
5874425eeeSNicolas Palix    echo 'Please check for false positives in the output before submitting a patch.'
5974425eeeSNicolas Palix    echo 'When using "patch" mode, carefully review the patch before submitting it.'
6074425eeeSNicolas Palix    echo ''
611e9dea2aSNicolas Palixfi
6274425eeeSNicolas Palix
635303265aSBernd Schubertrun_cmd() {
645303265aSBernd Schubert	if [ $VERBOSE -ne 0 ] ; then
655303265aSBernd Schubert		echo "Running: $@"
665303265aSBernd Schubert	fi
675303265aSBernd Schubert	eval $@
685303265aSBernd Schubert}
695303265aSBernd Schubert
705303265aSBernd Schubert
711e9dea2aSNicolas Palixcoccinelle () {
7274425eeeSNicolas Palix    COCCI="$1"
7374425eeeSNicolas Palix
7474425eeeSNicolas Palix    OPT=`grep "Option" $COCCI | cut -d':' -f2`
751e9dea2aSNicolas Palix
76062c1825SNicolas Palix#   The option '-parse_cocci' can be used to syntactically check the SmPL files.
771e9dea2aSNicolas Palix#
781e9dea2aSNicolas Palix#    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
791e9dea2aSNicolas Palix
8035d88a38SNicolas Palix    if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
811e9dea2aSNicolas Palix
821e9dea2aSNicolas Palix	FILE=`echo $COCCI | sed "s|$srctree/||"`
8374425eeeSNicolas Palix
843c908417SNicolas Palix	echo "Processing `basename $COCCI`"
853c908417SNicolas Palix	echo "with option(s) \"$OPT\""
863c908417SNicolas Palix	echo ''
8774425eeeSNicolas Palix	echo 'Message example to submit a patch:'
8874425eeeSNicolas Palix
893c908417SNicolas Palix	sed -ne 's|^///||p' $COCCI
9074425eeeSNicolas Palix
91062c1825SNicolas Palix	if [ "$MODE" = "patch" ] ; then
9274425eeeSNicolas Palix	    echo ' The semantic patch that makes this change is available'
93062c1825SNicolas Palix	elif [ "$MODE" = "report" ] ; then
94062c1825SNicolas Palix	    echo ' The semantic patch that makes this report is available'
95062c1825SNicolas Palix	elif [ "$MODE" = "context" ] ; then
96062c1825SNicolas Palix	    echo ' The semantic patch that spots this code is available'
97062c1825SNicolas Palix	elif [ "$MODE" = "org" ] ; then
98062c1825SNicolas Palix	    echo ' The semantic patch that makes this Org report is available'
99062c1825SNicolas Palix	else
100062c1825SNicolas Palix	    echo ' The semantic patch that makes this output is available'
101062c1825SNicolas Palix	fi
10274425eeeSNicolas Palix	echo " in $FILE."
10374425eeeSNicolas Palix	echo ''
10474425eeeSNicolas Palix	echo ' More information about semantic patching is available at'
10574425eeeSNicolas Palix	echo ' http://coccinelle.lip6.fr/'
10674425eeeSNicolas Palix	echo ''
10774425eeeSNicolas Palix
1083c908417SNicolas Palix	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
1093c908417SNicolas Palix	    echo 'Semantic patch information:'
1103c908417SNicolas Palix	    sed -ne 's|^//#||p' $COCCI
1113c908417SNicolas Palix	    echo ''
1123c908417SNicolas Palix	fi
1132c1160c8SNicolas Palix    fi
1143c908417SNicolas Palix
1152c1160c8SNicolas Palix    if [ "$MODE" = "chain" ] ; then
1165303265aSBernd Schubert	run_cmd $SPATCH -D patch   \
1175303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
1185303265aSBernd Schubert	run_cmd $SPATCH -D report  \
1195303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
1205303265aSBernd Schubert	run_cmd $SPATCH -D context \
1215303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
1225303265aSBernd Schubert	run_cmd $SPATCH -D org     \
1235303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
124c05cd6ddSNicolas Palix    elif [ "$MODE" = "rep+ctxt" ] ; then
1255303265aSBernd Schubert	run_cmd $SPATCH -D report  \
1265303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
1275303265aSBernd Schubert	run_cmd $SPATCH -D context \
1285303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1291e9dea2aSNicolas Palix    else
1305303265aSBernd Schubert	run_cmd $SPATCH -D $MODE   $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1311e9dea2aSNicolas Palix    fi
13274425eeeSNicolas Palix
13374425eeeSNicolas Palix}
13474425eeeSNicolas Palix
13574425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then
13674425eeeSNicolas Palix    for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1371e9dea2aSNicolas Palix	coccinelle $f
13874425eeeSNicolas Palix    done
13974425eeeSNicolas Palixelse
1401e9dea2aSNicolas Palix    coccinelle $COCCI
14174425eeeSNicolas Palixfi
142