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