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