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