560e6ce93c6333215981baa6ebcb67e0ca7cfaa2
[dpdk.git] / devtools / checkpatches.sh
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 -e $0))/load-devel-config
11
12 VALIDATE_NEW_API=$(dirname $(readlink -e $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.
17 codespell=${DPDK_CHECKPATCH_CODESPELL:-enable}
18 length=${DPDK_CHECKPATCH_LINE_LENGTH:-80}
19
20 # override default Linux options
21 options="--no-tree"
22 if [ "$codespell" = "enable" ] ; then
23     options="$options --codespell"
24 elif [ -f "$codespell" ] ; then
25     options="$options --codespell"
26     options="$options --codespellfile $codespell"
27 fi
28 options="$options --max-line-length=$length"
29 options="$options --show-types"
30 options="$options --ignore=LINUX_VERSION_CODE,\
31 FILE_PATH_CHANGES,MAINTAINERS_STYLE,SPDX_LICENSE_TAG,\
32 VOLATILE,PREFER_PACKED,PREFER_ALIGNED,PREFER_PRINTF,\
33 PREFER_KERNEL_TYPES,BIT_MACRO,CONST_STRUCT,\
34 SPLIT_STRING,LONG_LINE_STRING,\
35 LINE_SPACING,PARENTHESIS_ALIGNMENT,NETWORKING_BLOCK_COMMENT_STYLE,\
36 NEW_TYPEDEFS,COMPARISON_TO_NULL"
37 options="$options $DPDK_CHECKPATCH_OPTIONS"
38
39 clean_tmp_files() {
40         if echo $tmpinput | grep -q '^checkpatches\.' ; then
41                 rm -f "$tmpinput"
42         fi
43 }
44
45 trap "clean_tmp_files" INT
46
47 print_usage () {
48         cat <<- END_OF_HELP
49         usage: $(basename $0) [-q] [-v] [-nX|-r range|patch1 [patch2] ...]]
50
51         Run Linux kernel checkpatch.pl with DPDK options.
52         The environment variable DPDK_CHECKPATCH_PATH must be set.
53
54         The patches to check can be from stdin, files specified on the command line,
55         latest git commits limited with -n option, or commits in the git range
56         specified with -r option (default: "origin/master..").
57         END_OF_HELP
58 }
59
60 check_forbidden_additions() { # <patch>
61         res=0
62
63         # refrain from new additions of rte_panic() and rte_exit()
64         # multiple folders and expressions are separated by spaces
65         awk -v FOLDERS="lib drivers" \
66                 -v EXPRESSIONS="rte_panic\\\( rte_exit\\\(" \
67                 -v RET_ON_FAIL=1 \
68                 -v MESSAGE='Using rte_panic/rte_exit' \
69                 -f $(dirname $(readlink -e $0))/check-forbidden-tokens.awk \
70                 "$1" || res=1
71
72         # svg figures must be included with wildcard extension
73         # because of png conversion for pdf docs
74         awk -v FOLDERS='doc' \
75                 -v EXPRESSIONS='::[[:space:]]*[^[:space:]]*\\.svg' \
76                 -v RET_ON_FAIL=1 \
77                 -v MESSAGE='Using explicit .svg extension instead of .*' \
78                 -f $(dirname $(readlink -e $0))/check-forbidden-tokens.awk \
79                 "$1" || res=1
80
81         return $res
82 }
83
84 check_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
114 number=0
115 range='origin/master..'
116 quiet=false
117 verbose=false
118 while 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
127 done
128 shift $(($OPTIND - 1))
129
130 if [ ! -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
135 fi
136
137 print_headline() { # <title>
138         printf '\n### %s\n\n' "$1"
139         headline_printed=true
140 }
141
142 total=0
143 status=0
144
145 check () { # <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         elif [ -n "$2" ] ; then
154                 tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
155                 git format-patch --find-renames \
156                 --no-stat --stdout -1 $commit > "$tmpinput"
157         else
158                 tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
159                 cat > "$tmpinput"
160         fi
161
162         ! $verbose || printf 'Running checkpatch.pl:\n'
163         report=$($DPDK_CHECKPATCH_PATH $options "$tmpinput" 2>/dev/null)
164         if [ $? -ne 0 ] ; then
165                 $headline_printed || print_headline "$3"
166                 printf '%s\n' "$report" | sed -n '1,/^total:.*lines checked$/p'
167                 ret=1
168         fi
169
170         ! $verbose || printf '\nChecking API additions/removals:\n'
171         report=$($VALIDATE_NEW_API "$tmpinput")
172         if [ $? -ne 0 ] ; then
173                 $headline_printed || print_headline "$3"
174                 printf '%s\n' "$report"
175                 ret=1
176         fi
177
178         ! $verbose || printf '\nChecking forbidden tokens additions:\n'
179         report=$(check_forbidden_additions "$tmpinput")
180         if [ $? -ne 0 ] ; then
181                 $headline_printed || print_headline "$3"
182                 printf '%s\n' "$report"
183                 ret=1
184         fi
185
186         ! $verbose || printf '\nChecking __rte_experimental tags:\n'
187         report=$(check_experimental_tags "$tmpinput")
188         if [ $? -ne 0 ] ; then
189                 $headline_printed || print_headline "$3"
190                 printf '%s\n' "$report"
191                 ret=1
192         fi
193
194         clean_tmp_files
195         [ $ret -eq 0 ] && return 0
196
197         status=$(($status + 1))
198 }
199
200 if [ -n "$1" ] ; then
201         for patch in "$@" ; do
202                 # Subject can be on 2 lines
203                 subject=$(sed '/^Subject: */!d;s///;N;s,\n[[:space:]]\+, ,;s,\n.*,,;q' "$patch")
204                 check "$patch" '' "$subject"
205         done
206 elif [ ! -t 0 ] ; then # stdin
207         subject=$(while read header value ; do
208                 if [ "$header" = 'Subject:' ] ; then
209                         IFS= read next
210                         continuation=$(echo "$next" | sed -n 's,^[[:space:]]\+, ,p')
211                         echo $value$continuation
212                         break
213                 fi
214         done)
215         check '' '' "$subject"
216 else
217         if [ $number -eq 0 ] ; then
218                 commits=$(git rev-list --reverse $range)
219         else
220                 commits=$(git rev-list --reverse --max-count=$number HEAD)
221         fi
222         for commit in $commits ; do
223                 subject=$(git log --format='%s' -1 $commit)
224                 check '' $commit "$subject"
225         done
226 fi
227 pass=$(($total - $status))
228 $quiet || printf '\n%d/%d valid patch' $pass $total
229 $quiet || [ $pass -le 1 ] || printf 'es'
230 $quiet || printf '\n'
231 exit $status