xref: /linux-6.15/scripts/coccicheck (revision c930a1b2)
1#!/bin/bash
2
3#
4# This script requires at least spatch
5# version 1.0.0-rc11.
6#
7
8SPATCH="`which ${SPATCH:=spatch}`"
9
10if [ ! -x "$SPATCH" ]; then
11    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
12    exit 1
13fi
14
15USE_JOBS="no"
16$SPATCH --help | grep "\-\-jobs" > /dev/null && USE_JOBS="yes"
17
18# The verbosity may be set by the environmental parameter V=
19# as for example with 'make V=1 coccicheck'
20
21if [ -n "$V" -a "$V" != "0" ]; then
22	VERBOSE="$V"
23else
24	VERBOSE=0
25fi
26
27if [ -z "$J" ]; then
28	NPROC=$(getconf _NPROCESSORS_ONLN)
29else
30	NPROC="$J"
31fi
32
33FLAGS="--very-quiet"
34
35# spatch only allows include directories with the syntax "-I include"
36# while gcc also allows "-Iinclude" and "-include include"
37COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
38COCCIINCLUDE=${COCCIINCLUDE// -include/ --include}
39
40if [ "$C" = "1" -o "$C" = "2" ]; then
41    ONLINE=1
42
43    # Take only the last argument, which is the C file to test
44    shift $(( $# - 1 ))
45    OPTIONS="$COCCIINCLUDE $1"
46else
47    ONLINE=0
48    if [ "$KBUILD_EXTMOD" = "" ] ; then
49        OPTIONS="--dir $srctree $COCCIINCLUDE"
50    else
51        OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
52    fi
53fi
54
55if [ "$KBUILD_EXTMOD" != "" ] ; then
56    OPTIONS="--patch $srctree $OPTIONS"
57fi
58
59# You can override by using SPFLAGS
60if [ "$USE_JOBS" = "no" ]; then
61	trap kill_running SIGTERM SIGINT
62	declare -a SPATCH_PID
63elif [ "$NPROC" != "1" ]; then
64	# Using 0 should work as well, refer to _SC_NPROCESSORS_ONLN use on
65	# https://github.com/rdicosmo/parmap/blob/master/setcore_stubs.c
66	OPTIONS="$OPTIONS --jobs $NPROC --chunksize 1"
67fi
68
69if [ "$MODE" = "" ] ; then
70    if [ "$ONLINE" = "0" ] ; then
71	echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
72	echo 'Available modes are the following: patch, report, context, org'
73	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
74	echo 'Note however that some modes are not implemented by some semantic patches.'
75    fi
76    MODE="report"
77fi
78
79if [ "$MODE" = "chain" ] ; then
80    if [ "$ONLINE" = "0" ] ; then
81	echo 'You have selected the "chain" mode.'
82	echo 'All available modes will be tried (in that order): patch, report, context, org'
83    fi
84elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
85    FLAGS="--no-show-diff $FLAGS"
86fi
87
88if [ "$ONLINE" = "0" ] ; then
89    echo ''
90    echo 'Please check for false positives in the output before submitting a patch.'
91    echo 'When using "patch" mode, carefully review the patch before submitting it.'
92    echo ''
93fi
94
95run_cmd_parmap() {
96	if [ $VERBOSE -ne 0 ] ; then
97		echo "Running ($NPROC in parallel): $@"
98	fi
99	$@ 2>/dev/null
100	if [[ $? -ne 0 ]]; then
101		echo "coccicheck failed"
102		exit $?
103	fi
104}
105
106run_cmd_old() {
107	local i
108	if [ $VERBOSE -ne 0 ] ; then
109		echo "Running ($NPROC in parallel): $@"
110	fi
111	for i in $(seq 0 $(( NPROC - 1)) ); do
112		eval "$@ --max $NPROC --index $i &"
113		SPATCH_PID[$i]=$!
114		if [ $VERBOSE -eq 2 ] ; then
115			echo "${SPATCH_PID[$i]} running"
116		fi
117	done
118	wait
119}
120
121run_cmd() {
122	if [ "$USE_JOBS" = "yes" ]; then
123		run_cmd_parmap $@
124	else
125		run_cmd_old $@
126	fi
127}
128
129kill_running() {
130	for i in $(seq 0 $(( NPROC - 1 )) ); do
131		if [ $VERBOSE -eq 2 ] ; then
132			echo "Killing ${SPATCH_PID[$i]}"
133		fi
134		kill ${SPATCH_PID[$i]} 2>/dev/null
135	done
136}
137
138# You can override heuristics with SPFLAGS, these must always go last
139OPTIONS="$OPTIONS $SPFLAGS"
140
141coccinelle () {
142    COCCI="$1"
143
144    OPT=`grep "Option" $COCCI | cut -d':' -f2`
145
146#   The option '--parse-cocci' can be used to syntactically check the SmPL files.
147#
148#    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
149
150    if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
151
152	FILE=`echo $COCCI | sed "s|$srctree/||"`
153
154	echo "Processing `basename $COCCI`"
155	echo "with option(s) \"$OPT\""
156	echo ''
157	echo 'Message example to submit a patch:'
158
159	sed -ne 's|^///||p' $COCCI
160
161	if [ "$MODE" = "patch" ] ; then
162	    echo ' The semantic patch that makes this change is available'
163	elif [ "$MODE" = "report" ] ; then
164	    echo ' The semantic patch that makes this report is available'
165	elif [ "$MODE" = "context" ] ; then
166	    echo ' The semantic patch that spots this code is available'
167	elif [ "$MODE" = "org" ] ; then
168	    echo ' The semantic patch that makes this Org report is available'
169	else
170	    echo ' The semantic patch that makes this output is available'
171	fi
172	echo " in $FILE."
173	echo ''
174	echo ' More information about semantic patching is available at'
175	echo ' http://coccinelle.lip6.fr/'
176	echo ''
177
178	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
179	    echo 'Semantic patch information:'
180	    sed -ne 's|^//#||p' $COCCI
181	    echo ''
182	fi
183    fi
184
185    if [ "$MODE" = "chain" ] ; then
186	run_cmd $SPATCH -D patch   \
187		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
188	run_cmd $SPATCH -D report  \
189		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
190	run_cmd $SPATCH -D context \
191		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
192	run_cmd $SPATCH -D org     \
193		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
194    elif [ "$MODE" = "rep+ctxt" ] ; then
195	run_cmd $SPATCH -D report  \
196		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
197	run_cmd $SPATCH -D context \
198		$FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
199    else
200	run_cmd $SPATCH -D $MODE   $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
201    fi
202
203}
204
205if [ "$COCCI" = "" ] ; then
206    for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
207	coccinelle $f
208    done
209else
210    coccinelle $COCCI
211fi
212