xref: /dpdk/devtools/checkpatches.sh (revision cb440babbd45a80c059f8bc80e87c48d09086fd7)
1#! /bin/sh
2# SPDX-License-Identifier: BSD-3-Clause
3# Copyright 2015 6WIND S.A.
4
5# Load config options:
6# - DPDK_CHECKPATCH_PATH
7# - DPDK_CHECKPATCH_CODESPELL
8# - DPDK_CHECKPATCH_LINE_LENGTH
9# - DPDK_CHECKPATCH_OPTIONS
10. $(dirname $(readlink -f $0))/load-devel-config
11
12VALIDATE_NEW_API=$(dirname $(readlink -f $0))/check-symbol-change.sh
13
14# Enable codespell by default. This can be overwritten from a config file.
15# Codespell can also be enabled by setting DPDK_CHECKPATCH_CODESPELL to a valid path
16# to a dictionary.txt file if dictionary.txt is not in the default location.
17codespell=${DPDK_CHECKPATCH_CODESPELL:-enable}
18length=${DPDK_CHECKPATCH_LINE_LENGTH:-80}
19
20# override default Linux options
21options="--no-tree"
22if [ "$codespell" = "enable" ] ; then
23    options="$options --codespell"
24elif [ -f "$codespell" ] ; then
25    options="$options --codespell"
26    options="$options --codespellfile $codespell"
27fi
28options="$options --max-line-length=$length"
29options="$options --show-types"
30options="$options --ignore=LINUX_VERSION_CODE,ENOSYS,\
31FILE_PATH_CHANGES,MAINTAINERS_STYLE,SPDX_LICENSE_TAG,\
32VOLATILE,PREFER_PACKED,PREFER_ALIGNED,PREFER_PRINTF,\
33PREFER_KERNEL_TYPES,BIT_MACRO,CONST_STRUCT,\
34SPLIT_STRING,LONG_LINE_STRING,C99_COMMENT_TOLERANCE,\
35LINE_SPACING,PARENTHESIS_ALIGNMENT,NETWORKING_BLOCK_COMMENT_STYLE,\
36NEW_TYPEDEFS,COMPARISON_TO_NULL"
37options="$options $DPDK_CHECKPATCH_OPTIONS"
38
39print_usage () {
40	cat <<- END_OF_HELP
41	usage: $(basename $0) [-q] [-v] [-nX|-r range|patch1 [patch2] ...]]
42
43	Run Linux kernel checkpatch.pl with DPDK options.
44	The environment variable DPDK_CHECKPATCH_PATH must be set.
45
46	The patches to check can be from stdin, files specified on the command line,
47	latest git commits limited with -n option, or commits in the git range
48	specified with -r option (default: "origin/master..").
49	END_OF_HELP
50}
51
52check_forbidden_additions() { # <patch>
53	res=0
54
55	# refrain from new additions of rte_panic() and rte_exit()
56	# multiple folders and expressions are separated by spaces
57	awk -v FOLDERS="lib drivers" \
58		-v EXPRESSIONS="rte_panic\\\( rte_exit\\\(" \
59		-v RET_ON_FAIL=1 \
60		-v MESSAGE='Using rte_panic/rte_exit' \
61		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
62		"$1" || res=1
63
64	# refrain from using compiler attribute without defining a common macro
65	awk -v FOLDERS="lib drivers app examples" \
66		-v EXPRESSIONS="__attribute__" \
67		-v RET_ON_FAIL=1 \
68		-v MESSAGE='Using compiler attribute directly' \
69		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
70		"$1" || res=1
71
72	# forbid variable declaration inside "for" loop
73	awk -v FOLDERS='.' \
74		-v EXPRESSIONS='for[[:space:]]*\\((char|u?int|unsigned|s?size_t)' \
75		-v RET_ON_FAIL=1 \
76		-v MESSAGE='Declaring a variable inside for()' \
77		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
78		"$1" || res=1
79
80	# refrain from new additions of 16/32/64 bits rte_atomicNN_xxx()
81	awk -v FOLDERS="lib drivers app examples" \
82		-v EXPRESSIONS="rte_atomic[0-9][0-9]_.*\\\(" \
83		-v RET_ON_FAIL=1 \
84		-v MESSAGE='Using rte_atomicNN_xxx' \
85		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
86		"$1" || res=1
87
88	# refrain from new additions of rte_smp_[r/w]mb()
89	awk -v FOLDERS="lib drivers app examples" \
90		-v EXPRESSIONS="rte_smp_(r|w)?mb\\\(" \
91		-v RET_ON_FAIL=1 \
92		-v MESSAGE='Using rte_smp_[r/w]mb' \
93		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
94		"$1" || res=1
95
96	# refrain from using compiler __sync_xxx builtins
97	awk -v FOLDERS="lib drivers app examples" \
98		-v EXPRESSIONS="__sync_.*\\\(" \
99		-v RET_ON_FAIL=1 \
100		-v MESSAGE='Using __sync_xxx builtins' \
101		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
102		"$1" || res=1
103
104	# refrain from using compiler __atomic_thread_fence()
105	# It should be avoided on x86 for SMP case.
106	awk -v FOLDERS="lib drivers app examples" \
107		-v EXPRESSIONS="__atomic_thread_fence\\\(" \
108		-v RET_ON_FAIL=1 \
109		-v MESSAGE='Using __atomic_thread_fence' \
110		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
111		"$1" || res=1
112
113	# svg figures must be included with wildcard extension
114	# because of png conversion for pdf docs
115	awk -v FOLDERS='doc' \
116		-v EXPRESSIONS='::[[:space:]]*[^[:space:]]*\\.svg' \
117		-v RET_ON_FAIL=1 \
118		-v MESSAGE='Using explicit .svg extension instead of .*' \
119		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
120		"$1" || res=1
121
122	# links must prefer https over http
123	awk -v FOLDERS='doc' \
124		-v EXPRESSIONS='http://.*dpdk.org' \
125		-v RET_ON_FAIL=1 \
126		-v MESSAGE='Using non https link to dpdk.org' \
127		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
128		"$1" || res=1
129
130	return $res
131}
132
133check_experimental_tags() { # <patch>
134	res=0
135
136	cat "$1" |awk '
137	BEGIN {
138		current_file = "";
139		ret = 0;
140	}
141	/^+++ b\// {
142		current_file = $2;
143	}
144	/^+.*__rte_experimental/ {
145		if (current_file ~ ".c$" ) {
146			print "Please only put __rte_experimental tags in " \
147				"headers ("current_file")";
148			ret = 1;
149		}
150		if ($1 != "+__rte_experimental" || $2 != "") {
151			print "__rte_experimental must appear alone on the line" \
152				" immediately preceding the return type of a function."
153			ret = 1;
154		}
155	}
156	END {
157		exit ret;
158	}' || res=1
159
160	return $res
161}
162
163check_internal_tags() { # <patch>
164	res=0
165
166	cat "$1" |awk '
167	BEGIN {
168		current_file = "";
169		ret = 0;
170	}
171	/^+++ b\// {
172		current_file = $2;
173	}
174	/^+.*__rte_internal/ {
175		if (current_file ~ ".c$" ) {
176			print "Please only put __rte_internal tags in " \
177				"headers ("current_file")";
178			ret = 1;
179		}
180		if ($1 != "+__rte_internal" || $2 != "") {
181			print "__rte_internal must appear alone on the line" \
182				" immediately preceding the return type of" \
183				" a function."
184			ret = 1;
185		}
186	}
187	END {
188		exit ret;
189	}' || res=1
190
191	return $res
192}
193
194number=0
195range='origin/master..'
196quiet=false
197verbose=false
198while getopts hn:qr:v ARG ; do
199	case $ARG in
200		n ) number=$OPTARG ;;
201		q ) quiet=true ;;
202		r ) range=$OPTARG ;;
203		v ) verbose=true ;;
204		h ) print_usage ; exit 0 ;;
205		? ) print_usage ; exit 1 ;;
206	esac
207done
208shift $(($OPTIND - 1))
209
210if [ ! -f "$DPDK_CHECKPATCH_PATH" ] || [ ! -x "$DPDK_CHECKPATCH_PATH" ] ; then
211	print_usage >&2
212	echo
213	echo 'Cannot execute DPDK_CHECKPATCH_PATH' >&2
214	exit 1
215fi
216
217print_headline() { # <title>
218	printf '\n### %s\n\n' "$1"
219	headline_printed=true
220}
221
222total=0
223status=0
224
225check () { # <patch> <commit> <title>
226	local ret=0
227	headline_printed=false
228
229	total=$(($total + 1))
230	! $verbose || print_headline "$3"
231	if [ -n "$1" ] ; then
232		tmpinput=$1
233	else
234		tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
235		trap "rm -f '$tmpinput'" INT
236
237		if [ -n "$2" ] ; then
238			git format-patch --find-renames \
239			--no-stat --stdout -1 $commit > "$tmpinput"
240		else
241			cat > "$tmpinput"
242		fi
243	fi
244
245	! $verbose || printf 'Running checkpatch.pl:\n'
246	report=$($DPDK_CHECKPATCH_PATH $options "$tmpinput" 2>/dev/null)
247	if [ $? -ne 0 ] ; then
248		$headline_printed || print_headline "$3"
249		printf '%s\n' "$report" | sed -n '1,/^total:.*lines checked$/p'
250		ret=1
251	fi
252
253	! $verbose || printf '\nChecking API additions/removals:\n'
254	report=$($VALIDATE_NEW_API "$tmpinput")
255	if [ $? -ne 0 ] ; then
256		$headline_printed || print_headline "$3"
257		printf '%s\n' "$report"
258		ret=1
259	fi
260
261	! $verbose || printf '\nChecking forbidden tokens additions:\n'
262	report=$(check_forbidden_additions "$tmpinput")
263	if [ $? -ne 0 ] ; then
264		$headline_printed || print_headline "$3"
265		printf '%s\n' "$report"
266		ret=1
267	fi
268
269	! $verbose || printf '\nChecking __rte_experimental tags:\n'
270	report=$(check_experimental_tags "$tmpinput")
271	if [ $? -ne 0 ] ; then
272		$headline_printed || print_headline "$3"
273		printf '%s\n' "$report"
274		ret=1
275	fi
276
277	! $verbose || printf '\nChecking __rte_internal tags:\n'
278	report=$(check_internal_tags "$tmpinput")
279	if [ $? -ne 0 ] ; then
280		$headline_printed || print_headline "$3"
281		printf '%s\n' "$report"
282		ret=1
283	fi
284
285	if [ "$tmpinput" != "$1" ]; then
286		rm -f "$tmpinput"
287		trap - INT
288	fi
289	[ $ret -eq 0 ] && return 0
290
291	status=$(($status + 1))
292}
293
294if [ -n "$1" ] ; then
295	for patch in "$@" ; do
296		# Subject can be on 2 lines
297		subject=$(sed '/^Subject: */!d;s///;N;s,\n[[:space:]]\+, ,;s,\n.*,,;q' "$patch")
298		check "$patch" '' "$subject"
299	done
300elif [ ! -t 0 ] ; then # stdin
301	subject=$(while read header value ; do
302		if [ "$header" = 'Subject:' ] ; then
303			IFS= read next
304			continuation=$(echo "$next" | sed -n 's,^[[:space:]]\+, ,p')
305			echo $value$continuation
306			break
307		fi
308	done)
309	check '' '' "$subject"
310else
311	if [ $number -eq 0 ] ; then
312		commits=$(git rev-list --reverse $range)
313	else
314		commits=$(git rev-list --reverse --max-count=$number HEAD)
315	fi
316	for commit in $commits ; do
317		subject=$(git log --format='%s' -1 $commit)
318		check '' $commit "$subject"
319	done
320fi
321pass=$(($total - $status))
322$quiet || printf '\n%d/%d valid patch' $pass $total
323$quiet || [ $pass -le 1 ] || printf 'es'
324$quiet || printf '\n'
325exit $status
326