xref: /dpdk/devtools/checkpatches.sh (revision 3d4b2afb73f7f0988f8e66ba1b37f2a446e33868)
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,\
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	# svg figures must be included with wildcard extension
73	# because of png conversion for pdf docs
74	awk -v FOLDERS='doc' \
75		-v EXPRESSIONS='::[[:space:]]*[^[:space:]]*\\.svg' \
76		-v RET_ON_FAIL=1 \
77		-v MESSAGE='Using explicit .svg extension instead of .*' \
78		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
79		"$1" || res=1
80
81	# links must prefer https over http
82	awk -v FOLDERS='doc' \
83		-v EXPRESSIONS='http://.*dpdk.org' \
84		-v RET_ON_FAIL=1 \
85		-v MESSAGE='Using non https link to dpdk.org' \
86		-f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
87		"$1" || res=1
88
89	return $res
90}
91
92check_experimental_tags() { # <patch>
93	res=0
94
95	cat "$1" |awk '
96	BEGIN {
97		current_file = "";
98		ret = 0;
99	}
100	/^+++ b\// {
101		current_file = $2;
102	}
103	/^+.*__rte_experimental/ {
104		if (current_file ~ ".c$" ) {
105			print "Please only put __rte_experimental tags in " \
106				"headers ("current_file")";
107			ret = 1;
108		}
109		if ($1 != "+__rte_experimental" || $2 != "") {
110			print "__rte_experimental must appear alone on the line" \
111				" immediately preceding the return type of a function."
112			ret = 1;
113		}
114	}
115	END {
116		exit ret;
117	}' || res=1
118
119	return $res
120}
121
122check_internal_tags() { # <patch>
123	res=0
124
125	cat "$1" |awk '
126	BEGIN {
127		current_file = "";
128		ret = 0;
129	}
130	/^+++ b\// {
131		current_file = $2;
132	}
133	/^+.*__rte_internal/ {
134		if (current_file ~ ".c$" ) {
135			print "Please only put __rte_internal tags in " \
136				"headers ("current_file")";
137			ret = 1;
138		}
139		if ($1 != "+__rte_internal" || $2 != "") {
140			print "__rte_internal must appear alone on the line" \
141				" immediately preceding the return type of" \
142				" a function."
143			ret = 1;
144		}
145	}
146	END {
147		exit ret;
148	}' || res=1
149
150	return $res
151}
152
153number=0
154range='origin/master..'
155quiet=false
156verbose=false
157while getopts hn:qr:v ARG ; do
158	case $ARG in
159		n ) number=$OPTARG ;;
160		q ) quiet=true ;;
161		r ) range=$OPTARG ;;
162		v ) verbose=true ;;
163		h ) print_usage ; exit 0 ;;
164		? ) print_usage ; exit 1 ;;
165	esac
166done
167shift $(($OPTIND - 1))
168
169if [ ! -f "$DPDK_CHECKPATCH_PATH" ] || [ ! -x "$DPDK_CHECKPATCH_PATH" ] ; then
170	print_usage >&2
171	echo
172	echo 'Cannot execute DPDK_CHECKPATCH_PATH' >&2
173	exit 1
174fi
175
176print_headline() { # <title>
177	printf '\n### %s\n\n' "$1"
178	headline_printed=true
179}
180
181total=0
182status=0
183
184check () { # <patch> <commit> <title>
185	local ret=0
186	headline_printed=false
187
188	total=$(($total + 1))
189	! $verbose || print_headline "$3"
190	if [ -n "$1" ] ; then
191		tmpinput=$1
192	else
193		tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
194		trap "rm -f '$tmpinput'" INT
195
196		if [ -n "$2" ] ; then
197			git format-patch --find-renames \
198			--no-stat --stdout -1 $commit > "$tmpinput"
199		else
200			cat > "$tmpinput"
201		fi
202	fi
203
204	! $verbose || printf 'Running checkpatch.pl:\n'
205	report=$($DPDK_CHECKPATCH_PATH $options "$tmpinput" 2>/dev/null)
206	if [ $? -ne 0 ] ; then
207		$headline_printed || print_headline "$3"
208		printf '%s\n' "$report" | sed -n '1,/^total:.*lines checked$/p'
209		ret=1
210	fi
211
212	! $verbose || printf '\nChecking API additions/removals:\n'
213	report=$($VALIDATE_NEW_API "$tmpinput")
214	if [ $? -ne 0 ] ; then
215		$headline_printed || print_headline "$3"
216		printf '%s\n' "$report"
217		ret=1
218	fi
219
220	! $verbose || printf '\nChecking forbidden tokens additions:\n'
221	report=$(check_forbidden_additions "$tmpinput")
222	if [ $? -ne 0 ] ; then
223		$headline_printed || print_headline "$3"
224		printf '%s\n' "$report"
225		ret=1
226	fi
227
228	! $verbose || printf '\nChecking __rte_experimental tags:\n'
229	report=$(check_experimental_tags "$tmpinput")
230	if [ $? -ne 0 ] ; then
231		$headline_printed || print_headline "$3"
232		printf '%s\n' "$report"
233		ret=1
234	fi
235
236	! $verbose || printf '\nChecking __rte_internal tags:\n'
237	report=$(check_internal_tags "$tmpinput")
238	if [ $? -ne 0 ] ; then
239		$headline_printed || print_headline "$3"
240		printf '%s\n' "$report"
241		ret=1
242	fi
243
244	if [ "$tmpinput" != "$1" ]; then
245		rm -f "$tmpinput"
246		trap - INT
247	fi
248	[ $ret -eq 0 ] && return 0
249
250	status=$(($status + 1))
251}
252
253if [ -n "$1" ] ; then
254	for patch in "$@" ; do
255		# Subject can be on 2 lines
256		subject=$(sed '/^Subject: */!d;s///;N;s,\n[[:space:]]\+, ,;s,\n.*,,;q' "$patch")
257		check "$patch" '' "$subject"
258	done
259elif [ ! -t 0 ] ; then # stdin
260	subject=$(while read header value ; do
261		if [ "$header" = 'Subject:' ] ; then
262			IFS= read next
263			continuation=$(echo "$next" | sed -n 's,^[[:space:]]\+, ,p')
264			echo $value$continuation
265			break
266		fi
267	done)
268	check '' '' "$subject"
269else
270	if [ $number -eq 0 ] ; then
271		commits=$(git rev-list --reverse $range)
272	else
273		commits=$(git rev-list --reverse --max-count=$number HEAD)
274	fi
275	for commit in $commits ; do
276		subject=$(git log --format='%s' -1 $commit)
277		check '' $commit "$subject"
278	done
279fi
280pass=$(($total - $status))
281$quiet || printf '\n%d/%d valid patch' $pass $total
282$quiet || [ $pass -le 1 ] || printf 'es'
283$quiet || printf '\n'
284exit $status
285