xref: /linux-6.15/scripts/coccicheck (revision 26e56720)
174425eeeSNicolas Palix#!/bin/sh
274425eeeSNicolas Palix
374425eeeSNicolas PalixSPATCH="`which ${SPATCH:=spatch}`"
474425eeeSNicolas Palix
5*26e56720SBernd Schubert# The verbosity may be set by the environmental parameter V=
6*26e56720SBernd Schubert# as for example with 'make V=1 coccicheck'
7*26e56720SBernd Schubert
8*26e56720SBernd Schubertif [ -n "$V" -a "$V" != "0" ]; then
9*26e56720SBernd Schubert	VERBOSE=1
10*26e56720SBernd Schubertelse
11*26e56720SBernd Schubert	VERBOSE=0
12*26e56720SBernd Schubertfi
13*26e56720SBernd 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
581e9dea2aSNicolas Palixcoccinelle () {
5974425eeeSNicolas Palix    COCCI="$1"
6074425eeeSNicolas Palix
6174425eeeSNicolas Palix    OPT=`grep "Option" $COCCI | cut -d':' -f2`
621e9dea2aSNicolas Palix
63062c1825SNicolas Palix#   The option '-parse_cocci' can be used to syntactically check the SmPL files.
641e9dea2aSNicolas Palix#
651e9dea2aSNicolas Palix#    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
661e9dea2aSNicolas Palix
67*26e56720SBernd Schubert    if [ $VERBOSE -ne 0 ] ; then
681e9dea2aSNicolas Palix
691e9dea2aSNicolas Palix	FILE=`echo $COCCI | sed "s|$srctree/||"`
7074425eeeSNicolas Palix
713c908417SNicolas Palix	echo "Processing `basename $COCCI`"
723c908417SNicolas Palix	echo "with option(s) \"$OPT\""
733c908417SNicolas Palix	echo ''
7474425eeeSNicolas Palix	echo 'Message example to submit a patch:'
7574425eeeSNicolas Palix
763c908417SNicolas Palix	sed -ne 's|^///||p' $COCCI
7774425eeeSNicolas Palix
78062c1825SNicolas Palix	if [ "$MODE" = "patch" ] ; then
7974425eeeSNicolas Palix	    echo ' The semantic patch that makes this change is available'
80062c1825SNicolas Palix	elif [ "$MODE" = "report" ] ; then
81062c1825SNicolas Palix	    echo ' The semantic patch that makes this report is available'
82062c1825SNicolas Palix	elif [ "$MODE" = "context" ] ; then
83062c1825SNicolas Palix	    echo ' The semantic patch that spots this code is available'
84062c1825SNicolas Palix	elif [ "$MODE" = "org" ] ; then
85062c1825SNicolas Palix	    echo ' The semantic patch that makes this Org report is available'
86062c1825SNicolas Palix	else
87062c1825SNicolas Palix	    echo ' The semantic patch that makes this output is available'
88062c1825SNicolas Palix	fi
8974425eeeSNicolas Palix	echo " in $FILE."
9074425eeeSNicolas Palix	echo ''
9174425eeeSNicolas Palix	echo ' More information about semantic patching is available at'
9274425eeeSNicolas Palix	echo ' http://coccinelle.lip6.fr/'
9374425eeeSNicolas Palix	echo ''
9474425eeeSNicolas Palix
953c908417SNicolas Palix	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
963c908417SNicolas Palix	    echo 'Semantic patch information:'
973c908417SNicolas Palix	    sed -ne 's|^//#||p' $COCCI
983c908417SNicolas Palix	    echo ''
993c908417SNicolas Palix	fi
1002c1160c8SNicolas Palix    fi
1013c908417SNicolas Palix
1022c1160c8SNicolas Palix    if [ "$MODE" = "chain" ] ; then
1032c1160c8SNicolas Palix	$SPATCH -D patch   $FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
10403ee0c42SNicolas Palix	$SPATCH -D report  $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
1052c1160c8SNicolas Palix	$SPATCH -D context $FLAGS -sp_file $COCCI $OPT $OPTIONS               || \
10603ee0c42SNicolas Palix	$SPATCH -D org     $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
107c05cd6ddSNicolas Palix    elif [ "$MODE" = "rep+ctxt" ] ; then
108c05cd6ddSNicolas Palix	$SPATCH -D report  $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
109c05cd6ddSNicolas Palix	$SPATCH -D context $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1101e9dea2aSNicolas Palix    else
1111e9dea2aSNicolas Palix	$SPATCH -D $MODE   $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1121e9dea2aSNicolas Palix    fi
11374425eeeSNicolas Palix
11474425eeeSNicolas Palix}
11574425eeeSNicolas Palix
11674425eeeSNicolas Palixif [ "$COCCI" = "" ] ; then
11774425eeeSNicolas Palix    for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1181e9dea2aSNicolas Palix	coccinelle $f
11974425eeeSNicolas Palix    done
12074425eeeSNicolas Palixelse
1211e9dea2aSNicolas Palix    coccinelle $COCCI
12274425eeeSNicolas Palixfi
123