xref: /linux-6.15/scripts/coccicheck (revision 9e395550)
1*9e395550SNicolas 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
14*9e395550SNicolas PalixFLAGS="-very_quiet"
15*9e395550SNicolas Palix
16*9e395550SNicolas Palix# spatch only allows include directories with the syntax "-I include"
17*9e395550SNicolas Palix# while gcc also allows "-Iinclude" and "-include include"
18*9e395550SNicolas PalixCOCCIINCLUDE=${LINUXINCLUDE//-I/-I }
19*9e395550SNicolas PalixCOCCIINCLUDE=${COCCIINCLUDE//-include/-I}
20*9e395550SNicolas Palix
211e9dea2aSNicolas Palixif [ "$C" = "1" -o "$C" = "2" ]; then
221e9dea2aSNicolas Palix    ONLINE=1
231e9dea2aSNicolas Palix
24*9e395550SNicolas Palix    # Take only the last argument, which is the C file to test
251e9dea2aSNicolas Palix    shift $(( $# - 1 ))
26*9e395550SNicolas Palix    OPTIONS="$COCCIINCLUDE $1"
271e9dea2aSNicolas Palixelse
281e9dea2aSNicolas Palix    ONLINE=0
29d0bc1fb4SGreg Dietsche    if [ "$KBUILD_EXTMOD" = "" ] ; then
30*9e395550SNicolas Palix        OPTIONS="-dir $srctree $COCCIINCLUDE"
31d0bc1fb4SGreg Dietsche    else
32*9e395550SNicolas Palix        OPTIONS="-dir $KBUILD_EXTMOD -patch $srctree $COCCIINCLUDE"
33d0bc1fb4SGreg Dietsche    fi
341e9dea2aSNicolas Palixfi
351e9dea2aSNicolas Palix
3674425eeeSNicolas Palixif [ ! -x "$SPATCH" ]; then
3774425eeeSNicolas Palix    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
3874425eeeSNicolas Palix    exit 1
3974425eeeSNicolas Palixfi
4074425eeeSNicolas Palix
4174425eeeSNicolas Palixif [ "$MODE" = "" ] ; then
421e9dea2aSNicolas Palix    if [ "$ONLINE" = "0" ] ; then
432c1160c8SNicolas Palix	echo 'You have not explicitly specified the mode to use. Using default "chain" mode.'
442c1160c8SNicolas Palix	echo 'All available modes will be tried (in that order): patch, report, context, org'
4574425eeeSNicolas Palix	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
461e9dea2aSNicolas Palix    fi
472c1160c8SNicolas Palix    MODE="chain"
4803ee0c42SNicolas Palixelif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
4903ee0c42SNicolas Palix    FLAGS="$FLAGS -no_show_diff"
5074425eeeSNicolas Palixfi
5174425eeeSNicolas Palix
521e9dea2aSNicolas Palixif [ "$ONLINE" = "0" ] ; then
5374425eeeSNicolas Palix    echo ''
5474425eeeSNicolas Palix    echo 'Please check for false positives in the output before submitting a patch.'
5574425eeeSNicolas Palix    echo 'When using "patch" mode, carefully review the patch before submitting it.'
5674425eeeSNicolas Palix    echo ''
571e9dea2aSNicolas Palixfi
5874425eeeSNicolas Palix
595303265aSBernd Schubertrun_cmd() {
605303265aSBernd Schubert	if [ $VERBOSE -ne 0 ] ; then
615303265aSBernd Schubert		echo "Running: $@"
625303265aSBernd Schubert	fi
635303265aSBernd Schubert	eval $@
645303265aSBernd Schubert}
655303265aSBernd Schubert
665303265aSBernd Schubert
671e9dea2aSNicolas Palixcoccinelle () {
6874425eeeSNicolas Palix    COCCI="$1"
6974425eeeSNicolas Palix
7074425eeeSNicolas Palix    OPT=`grep "Option" $COCCI | cut -d':' -f2`
711e9dea2aSNicolas Palix
72062c1825SNicolas Palix#   The option '-parse_cocci' can be used to syntactically check the SmPL files.
731e9dea2aSNicolas Palix#
741e9dea2aSNicolas Palix#    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
751e9dea2aSNicolas Palix
7635d88a38SNicolas Palix    if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
771e9dea2aSNicolas Palix
781e9dea2aSNicolas Palix	FILE=`echo $COCCI | sed "s|$srctree/||"`
7974425eeeSNicolas Palix
803c908417SNicolas Palix	echo "Processing `basename $COCCI`"
813c908417SNicolas Palix	echo "with option(s) \"$OPT\""
823c908417SNicolas Palix	echo ''
8374425eeeSNicolas Palix	echo 'Message example to submit a patch:'
8474425eeeSNicolas Palix
853c908417SNicolas Palix	sed -ne 's|^///||p' $COCCI
8674425eeeSNicolas Palix
87062c1825SNicolas Palix	if [ "$MODE" = "patch" ] ; then
8874425eeeSNicolas Palix	    echo ' The semantic patch that makes this change is available'
89062c1825SNicolas Palix	elif [ "$MODE" = "report" ] ; then
90062c1825SNicolas Palix	    echo ' The semantic patch that makes this report is available'
91062c1825SNicolas Palix	elif [ "$MODE" = "context" ] ; then
92062c1825SNicolas Palix	    echo ' The semantic patch that spots this code is available'
93062c1825SNicolas Palix	elif [ "$MODE" = "org" ] ; then
94062c1825SNicolas Palix	    echo ' The semantic patch that makes this Org report is available'
95062c1825SNicolas Palix	else
96062c1825SNicolas Palix	    echo ' The semantic patch that makes this output is available'
97062c1825SNicolas Palix	fi
9874425eeeSNicolas Palix	echo " in $FILE."
9974425eeeSNicolas Palix	echo ''
10074425eeeSNicolas Palix	echo ' More information about semantic patching is available at'
10174425eeeSNicolas Palix	echo ' http://coccinelle.lip6.fr/'
10274425eeeSNicolas Palix	echo ''
10374425eeeSNicolas Palix
1043c908417SNicolas Palix	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
1053c908417SNicolas Palix	    echo 'Semantic patch information:'
1063c908417SNicolas Palix	    sed -ne 's|^//#||p' $COCCI
1073c908417SNicolas Palix	    echo ''
1083c908417SNicolas Palix	fi
1092c1160c8SNicolas Palix    fi
1103c908417SNicolas Palix
1112c1160c8SNicolas Palix    if [ "$MODE" = "chain" ] ; then
1125303265aSBernd Schubert	run_cmd $SPATCH -D patch   \
1135303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
1145303265aSBernd Schubert	run_cmd $SPATCH -D report  \
1155303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
1165303265aSBernd Schubert	run_cmd $SPATCH -D context \
1175303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
1185303265aSBernd Schubert	run_cmd $SPATCH -D org     \
1195303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
120c05cd6ddSNicolas Palix    elif [ "$MODE" = "rep+ctxt" ] ; then
1215303265aSBernd Schubert	run_cmd $SPATCH -D report  \
1225303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
1235303265aSBernd Schubert	run_cmd $SPATCH -D context \
1245303265aSBernd Schubert		$FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1251e9dea2aSNicolas Palix    else
1265303265aSBernd Schubert	run_cmd $SPATCH -D $MODE   $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1271e9dea2aSNicolas Palix    fi
12874425eeeSNicolas Palix
12974425eeeSNicolas Palix}
13074425eeeSNicolas Palix
13174425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then
13274425eeeSNicolas Palix    for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1331e9dea2aSNicolas Palix	coccinelle $f
13474425eeeSNicolas Palix    done
13574425eeeSNicolas Palixelse
1361e9dea2aSNicolas Palix    coccinelle $COCCI
13774425eeeSNicolas Palixfi
138