xref: /dpdk/devtools/checkpatches.sh (revision 200bc52e5aa0d72e70464c9cd22b55cf536ed13c)
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. $(dirname $(readlink -e $0))/load-devel-config
10
11VALIDATE_NEW_API=$(dirname $(readlink -e $0))/check-symbol-change.sh
12
13# Enable codespell by default. This can be overwritten from a config file.
14# Codespell can also be enabled by setting DPDK_CHECKPATCH_CODESPELL to a valid path
15# to a dictionary.txt file if dictionary.txt is not in the default location.
16codespell=${DPDK_CHECKPATCH_CODESPELL:-enable}
17length=${DPDK_CHECKPATCH_LINE_LENGTH:-80}
18
19# override default Linux options
20options="--no-tree"
21if [ "$codespell" = "enable" ] ; then
22    options="$options --codespell"
23elif [ -f "$codespell" ] ; then
24    options="$options --codespell"
25    options="$options --codespellfile $codespell"
26fi
27options="$options --max-line-length=$length"
28options="$options --show-types"
29options="$options --ignore=LINUX_VERSION_CODE,\
30FILE_PATH_CHANGES,MAINTAINERS_STYLE,SPDX_LICENSE_TAG,\
31VOLATILE,PREFER_PACKED,PREFER_ALIGNED,PREFER_PRINTF,\
32PREFER_KERNEL_TYPES,BIT_MACRO,CONST_STRUCT,\
33SPLIT_STRING,LONG_LINE_STRING,\
34LINE_SPACING,PARENTHESIS_ALIGNMENT,NETWORKING_BLOCK_COMMENT_STYLE,\
35NEW_TYPEDEFS,COMPARISON_TO_NULL"
36
37clean_tmp_files() {
38	if echo $tmpinput | grep -q '^checkpatches\.' ; then
39		rm -f "$tmpinput"
40	fi
41}
42
43trap "clean_tmp_files" INT
44
45print_usage () {
46	cat <<- END_OF_HELP
47	usage: $(basename $0) [-q] [-v] [-nX|-r range|patch1 [patch2] ...]]
48
49	Run Linux kernel checkpatch.pl with DPDK options.
50	The environment variable DPDK_CHECKPATCH_PATH must be set.
51
52	The patches to check can be from stdin, files specified on the command line,
53	latest git commits limited with -n option, or commits in the git range
54	specified with -r option (default: "origin/master..").
55	END_OF_HELP
56}
57
58check_forbidden_additions() { # <patch>
59	res=0
60
61	# refrain from new additions of rte_panic() and rte_exit()
62	# multiple folders and expressions are separated by spaces
63	awk -v FOLDERS="lib drivers" \
64		-v EXPRESSIONS="rte_panic\\\( rte_exit\\\(" \
65		-v RET_ON_FAIL=1 \
66		-v MESSAGE='Using rte_panic/rte_exit' \
67		-f $(dirname $(readlink -e $0))/check-forbidden-tokens.awk \
68		"$1" || res=1
69
70	# svg figures must be included with wildcard extension
71	# because of png conversion for pdf docs
72	awk -v FOLDERS='doc' \
73		-v EXPRESSIONS='::[[:space:]]*[^[:space:]]*\\.svg' \
74		-v RET_ON_FAIL=1 \
75		-v MESSAGE='Using explicit .svg extension instead of .*' \
76		-f $(dirname $(readlink -e $0))/check-forbidden-tokens.awk \
77		"$1" || res=1
78
79	return $res
80}
81
82number=0
83range='origin/master..'
84quiet=false
85verbose=false
86while getopts hn:qr:v ARG ; do
87	case $ARG in
88		n ) number=$OPTARG ;;
89		q ) quiet=true ;;
90		r ) range=$OPTARG ;;
91		v ) verbose=true ;;
92		h ) print_usage ; exit 0 ;;
93		? ) print_usage ; exit 1 ;;
94	esac
95done
96shift $(($OPTIND - 1))
97
98if [ ! -f "$DPDK_CHECKPATCH_PATH" ] || [ ! -x "$DPDK_CHECKPATCH_PATH" ] ; then
99	print_usage >&2
100	echo
101	echo 'Cannot execute DPDK_CHECKPATCH_PATH' >&2
102	exit 1
103fi
104
105print_headline() { # <title>
106	printf '\n### %s\n\n' "$1"
107	headline_printed=true
108}
109
110total=0
111status=0
112
113check () { # <patch> <commit> <title>
114	local ret=0
115	headline_printed=false
116
117	total=$(($total + 1))
118	! $verbose || print_headline "$3"
119	if [ -n "$1" ] ; then
120		tmpinput=$1
121	elif [ -n "$2" ] ; then
122		tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
123		git format-patch --find-renames \
124		--no-stat --stdout -1 $commit > "$tmpinput"
125	else
126		tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
127		cat > "$tmpinput"
128	fi
129
130	! $verbose || printf 'Running checkpatch.pl:\n'
131	report=$($DPDK_CHECKPATCH_PATH $options "$tmpinput" 2>/dev/null)
132	if [ $? -ne 0 ] ; then
133		$headline_printed || print_headline "$3"
134		printf '%s\n' "$report" | sed -n '1,/^total:.*lines checked$/p'
135		ret=1
136	fi
137
138	! $verbose || printf '\nChecking API additions/removals:\n'
139	report=$($VALIDATE_NEW_API "$tmpinput")
140	if [ $? -ne 0 ] ; then
141		$headline_printed || print_headline "$3"
142		printf '%s\n' "$report"
143		ret=1
144	fi
145
146	! $verbose || printf '\nChecking forbidden tokens additions:\n'
147	report=$(check_forbidden_additions "$tmpinput")
148	if [ $? -ne 0 ] ; then
149		$headline_printed || print_headline "$3"
150		printf '%s\n' "$report"
151		ret=1
152	fi
153
154	clean_tmp_files
155	[ $ret -eq 0 ] && return 0
156
157	status=$(($status + 1))
158}
159
160if [ -n "$1" ] ; then
161	for patch in "$@" ; do
162		# Subject can be on 2 lines
163		subject=$(sed '/^Subject: */!d;s///;N;s,\n[[:space:]]\+, ,;s,\n.*,,;q' "$patch")
164		check "$patch" '' "$subject"
165	done
166elif [ ! -t 0 ] ; then # stdin
167	subject=$(while read header value ; do
168		if [ "$header" = 'Subject:' ] ; then
169			IFS= read next
170			continuation=$(echo "$next" | sed -n 's,^[[:space:]]\+, ,p')
171			echo $value$continuation
172			break
173		fi
174	done)
175	check '' '' "$subject"
176else
177	if [ $number -eq 0 ] ; then
178		commits=$(git rev-list --reverse $range)
179	else
180		commits=$(git rev-list --reverse --max-count=$number HEAD)
181	fi
182	for commit in $commits ; do
183		subject=$(git log --format='%s' -1 $commit)
184		check '' $commit "$subject"
185	done
186fi
187pass=$(($total - $status))
188$quiet || printf '\n%d/%d valid patch' $pass $total
189$quiet || [ $pass -le 1 ] || printf 'es'
190$quiet || printf '\n'
191exit $status
192