xref: /dpdk/devtools/checkpatches.sh (revision 4d4c612e)
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	# refrain from new additions of rte_panic() and rte_exit()
48	# multiple folders and expressions are separated by spaces
49	awk -v FOLDERS="lib drivers" \
50		-v EXPRESSIONS="rte_panic\\\( rte_exit\\\(" \
51		-v RET_ON_FAIL=1 \
52		-f $(dirname $(readlink -e $0))/check-forbidden-tokens.awk \
53		"$1"
54	# svg figures must be included with wildcard extension
55	# because of png conversion for pdf docs
56	awk -v FOLDERS='doc' \
57		-v EXPRESSIONS='::[[:space:]]*[^[:space:]]*\\.svg' \
58		-v RET_ON_FAIL=1 \
59		-f $(dirname $(readlink -e $0))/check-forbidden-tokens.awk \
60		"$1"
61}
62
63number=0
64quiet=false
65verbose=false
66while getopts hn:qv ARG ; do
67	case $ARG in
68		n ) number=$OPTARG ;;
69		q ) quiet=true ;;
70		v ) verbose=true ;;
71		h ) print_usage ; exit 0 ;;
72		? ) print_usage ; exit 1 ;;
73	esac
74done
75shift $(($OPTIND - 1))
76
77if [ ! -f "$DPDK_CHECKPATCH_PATH" ] || [ ! -x "$DPDK_CHECKPATCH_PATH" ] ; then
78	print_usage >&2
79	echo
80	echo 'Cannot execute DPDK_CHECKPATCH_PATH' >&2
81	exit 1
82fi
83
84print_headline() { # <title>
85	printf '\n### %s\n\n' "$1"
86	headline_printed=true
87}
88
89total=0
90status=0
91
92check () { # <patch> <commit> <title>
93	local ret=0
94	headline_printed=false
95
96	total=$(($total + 1))
97	! $verbose || print_headline "$3"
98	if [ -n "$1" ] ; then
99		tmpinput=$1
100	elif [ -n "$2" ] ; then
101		tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
102		git format-patch --find-renames \
103		--no-stat --stdout -1 $commit > "$tmpinput"
104	else
105		tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
106		cat > "$tmpinput"
107	fi
108
109	! $verbose || printf 'Running checkpatch.pl:\n'
110	report=$($DPDK_CHECKPATCH_PATH $options "$tmpinput" 2>/dev/null)
111	if [ $? -ne 0 ] ; then
112		$headline_printed || print_headline "$3"
113		printf '%s\n' "$report" | sed -n '1,/^total:.*lines checked$/p'
114		ret=1
115	fi
116
117	! $verbose || printf '\nChecking API additions/removals:\n'
118	report=$($VALIDATE_NEW_API "$tmpinput")
119	if [ $? -ne 0 ] ; then
120		$headline_printed || print_headline "$3"
121		printf '%s\n' "$report"
122		ret=1
123	fi
124
125	! $verbose || printf '\nChecking forbidden tokens additions:\n'
126	report=$(check_forbidden_additions "$tmpinput")
127	if [ $? -ne 0 ] ; then
128		$headline_printed || print_headline "$3"
129		printf '%s\n' "$report"
130		ret=1
131	fi
132
133	clean_tmp_files
134	[ $ret -eq 0 ] && return 0
135
136	status=$(($status + 1))
137}
138
139if [ -n "$1" ] ; then
140	for patch in "$@" ; do
141		# Subject can be on 2 lines
142		subject=$(sed '/^Subject: */!d;s///;N;s,\n[[:space:]]\+, ,;s,\n.*,,;q' "$patch")
143		check "$patch" '' "$subject"
144	done
145elif [ ! -t 0 ] ; then # stdin
146	subject=$(while read header value ; do
147		if [ "$header" = 'Subject:' ] ; then
148			IFS= read next
149			continuation=$(echo "$next" | sed -n 's,^[[:space:]]\+, ,p')
150			echo $value$continuation
151			break
152		fi
153	done)
154	check '' '' "$subject"
155else
156	if [ $number -eq 0 ] ; then
157		commits=$(git rev-list --reverse origin/master..)
158	else
159		commits=$(git rev-list --reverse --max-count=$number HEAD)
160	fi
161	for commit in $commits ; do
162		subject=$(git log --format='%s' -1 $commit)
163		check '' $commit "$subject"
164	done
165fi
166pass=$(($total - $status))
167$quiet || printf '\n%d/%d valid patch' $pass $total
168$quiet || [ $pass -le 1 ] || printf 'es'
169$quiet || printf '\n'
170exit $status
171