devtools: warn about old release notes updates
[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 -f $0))/load-devel-config
11
12 VALIDATE_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.
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,ENOSYS,\
31 FILE_PATH_CHANGES,MAINTAINERS_STYLE,SPDX_LICENSE_TAG,\
32 VOLATILE,PREFER_PACKED,PREFER_ALIGNED,PREFER_PRINTF,\
33 PREFER_KERNEL_TYPES,PREFER_FALLTHROUGH,BIT_MACRO,CONST_STRUCT,\
34 SPLIT_STRING,LONG_LINE_STRING,C99_COMMENT_TOLERANCE,\
35 LINE_SPACING,PARENTHESIS_ALIGNMENT,NETWORKING_BLOCK_COMMENT_STYLE,\
36 NEW_TYPEDEFS,COMPARISON_TO_NULL"
37 options="$options $DPDK_CHECKPATCH_OPTIONS"
38
39 print_usage () {
40         cat <<- END_OF_HELP
41         usage: $(basename $0) [-h] [-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/main..").
49         END_OF_HELP
50 }
51
52 check_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         # refrain from using compiler attribute without defining a common macro
65         awk -v FOLDERS="lib drivers app examples" \
66                 -v EXPRESSIONS="__attribute__" \
67                 -v RET_ON_FAIL=1 \
68                 -v MESSAGE='Using compiler attribute directly' \
69                 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
70                 "$1" || res=1
71
72         # forbid variable declaration inside "for" loop
73         awk -v FOLDERS='.' \
74                 -v EXPRESSIONS='for[[:space:]]*\\((char|u?int|unsigned|s?size_t)' \
75                 -v RET_ON_FAIL=1 \
76                 -v MESSAGE='Declaring a variable inside for()' \
77                 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
78                 "$1" || res=1
79
80         # refrain from new additions of 16/32/64 bits rte_atomicNN_xxx()
81         awk -v FOLDERS="lib drivers app examples" \
82                 -v EXPRESSIONS="rte_atomic[0-9][0-9]_.*\\\(" \
83                 -v RET_ON_FAIL=1 \
84                 -v MESSAGE='Using rte_atomicNN_xxx' \
85                 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
86                 "$1" || res=1
87
88         # refrain from new additions of rte_smp_[r/w]mb()
89         awk -v FOLDERS="lib drivers app examples" \
90                 -v EXPRESSIONS="rte_smp_(r|w)?mb\\\(" \
91                 -v RET_ON_FAIL=1 \
92                 -v MESSAGE='Using rte_smp_[r/w]mb' \
93                 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
94                 "$1" || res=1
95
96         # refrain from using compiler __sync_xxx builtins
97         awk -v FOLDERS="lib drivers app examples" \
98                 -v EXPRESSIONS="__sync_.*\\\(" \
99                 -v RET_ON_FAIL=1 \
100                 -v MESSAGE='Using __sync_xxx builtins' \
101                 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
102                 "$1" || res=1
103
104         # refrain from using compiler __atomic_thread_fence()
105         # It should be avoided on x86 for SMP case.
106         awk -v FOLDERS="lib drivers app examples" \
107                 -v EXPRESSIONS="__atomic_thread_fence\\\(" \
108                 -v RET_ON_FAIL=1 \
109                 -v MESSAGE='Using __atomic_thread_fence' \
110                 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
111                 "$1" || res=1
112
113         # forbid use of experimental build flag except in examples
114         awk -v FOLDERS='lib drivers app' \
115                 -v EXPRESSIONS='-DALLOW_EXPERIMENTAL_API allow_experimental_apis' \
116                 -v RET_ON_FAIL=1 \
117                 -v MESSAGE='Using experimental build flag for in-tree compilation' \
118                 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
119                 "$1" || res=1
120
121         # SVG must be included with wildcard extension to allow conversion
122         awk -v FOLDERS='doc' \
123                 -v EXPRESSIONS='::[[:space:]]*[^[:space:]]*\\.svg' \
124                 -v RET_ON_FAIL=1 \
125                 -v MESSAGE='Using explicit .svg extension instead of .*' \
126                 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
127                 "$1" || res=1
128
129         # links must prefer https over http
130         awk -v FOLDERS='doc' \
131                 -v EXPRESSIONS='http://.*dpdk.org' \
132                 -v RET_ON_FAIL=1 \
133                 -v MESSAGE='Using non https link to dpdk.org' \
134                 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
135                 "$1" || res=1
136
137         return $res
138 }
139
140 check_experimental_tags() { # <patch>
141         res=0
142
143         cat "$1" |awk '
144         BEGIN {
145                 current_file = "";
146                 ret = 0;
147         }
148         /^+++ b\// {
149                 current_file = $2;
150         }
151         /^+.*__rte_experimental/ {
152                 if (current_file ~ ".c$" ) {
153                         print "Please only put __rte_experimental tags in " \
154                                 "headers ("current_file")";
155                         ret = 1;
156                 }
157                 if ($1 != "+__rte_experimental" || $2 != "") {
158                         print "__rte_experimental must appear alone on the line" \
159                                 " immediately preceding the return type of a function."
160                         ret = 1;
161                 }
162         }
163         END {
164                 exit ret;
165         }' || res=1
166
167         return $res
168 }
169
170 check_internal_tags() { # <patch>
171         res=0
172
173         cat "$1" |awk '
174         BEGIN {
175                 current_file = "";
176                 ret = 0;
177         }
178         /^+++ b\// {
179                 current_file = $2;
180         }
181         /^+.*__rte_internal/ {
182                 if (current_file ~ ".c$" ) {
183                         print "Please only put __rte_internal tags in " \
184                                 "headers ("current_file")";
185                         ret = 1;
186                 }
187                 if ($1 != "+__rte_internal" || $2 != "") {
188                         print "__rte_internal must appear alone on the line" \
189                                 " immediately preceding the return type of" \
190                                 " a function."
191                         ret = 1;
192                 }
193         }
194         END {
195                 exit ret;
196         }' || res=1
197
198         return $res
199 }
200
201 check_release_notes() { # <patch>
202         rel_notes_prefix=doc/guides/rel_notes/release_
203         IFS=. read year month release < VERSION
204         current_rel_notes=${rel_notes_prefix}${year}_${month}.rst
205
206         ! grep -e '^--- a/'$rel_notes_prefix -e '^+++ b/'$rel_notes_prefix "$1" |
207                 grep -v $current_rel_notes
208 }
209
210 number=0
211 range='origin/main..'
212 quiet=false
213 verbose=false
214 while getopts hn:qr:v ARG ; do
215         case $ARG in
216                 n ) number=$OPTARG ;;
217                 q ) quiet=true ;;
218                 r ) range=$OPTARG ;;
219                 v ) verbose=true ;;
220                 h ) print_usage ; exit 0 ;;
221                 ? ) print_usage ; exit 1 ;;
222         esac
223 done
224 shift $(($OPTIND - 1))
225
226 if [ ! -f "$DPDK_CHECKPATCH_PATH" ] || [ ! -x "$DPDK_CHECKPATCH_PATH" ] ; then
227         print_usage >&2
228         echo
229         echo 'Cannot execute DPDK_CHECKPATCH_PATH' >&2
230         exit 1
231 fi
232
233 print_headline() { # <title>
234         printf '\n### %s\n\n' "$1"
235         headline_printed=true
236 }
237
238 total=0
239 status=0
240
241 check () { # <patch> <commit> <title>
242         local ret=0
243         headline_printed=false
244
245         total=$(($total + 1))
246         ! $verbose || print_headline "$3"
247         if [ -n "$1" ] ; then
248                 tmpinput=$1
249         else
250                 tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
251                 trap "rm -f '$tmpinput'" INT
252
253                 if [ -n "$2" ] ; then
254                         git format-patch --find-renames \
255                         --no-stat --stdout -1 $commit > "$tmpinput"
256                 else
257                         cat > "$tmpinput"
258                 fi
259         fi
260
261         ! $verbose || printf 'Running checkpatch.pl:\n'
262         report=$($DPDK_CHECKPATCH_PATH $options "$tmpinput" 2>/dev/null)
263         if [ $? -ne 0 ] ; then
264                 $headline_printed || print_headline "$3"
265                 printf '%s\n' "$report" | sed -n '1,/^total:.*lines checked$/p'
266                 ret=1
267         fi
268
269         ! $verbose || printf '\nChecking API additions/removals:\n'
270         report=$($VALIDATE_NEW_API "$tmpinput")
271         if [ $? -ne 0 ] ; then
272                 $headline_printed || print_headline "$3"
273                 printf '%s\n' "$report"
274                 ret=1
275         fi
276
277         ! $verbose || printf '\nChecking forbidden tokens additions:\n'
278         report=$(check_forbidden_additions "$tmpinput")
279         if [ $? -ne 0 ] ; then
280                 $headline_printed || print_headline "$3"
281                 printf '%s\n' "$report"
282                 ret=1
283         fi
284
285         ! $verbose || printf '\nChecking __rte_experimental tags:\n'
286         report=$(check_experimental_tags "$tmpinput")
287         if [ $? -ne 0 ] ; then
288                 $headline_printed || print_headline "$3"
289                 printf '%s\n' "$report"
290                 ret=1
291         fi
292
293         ! $verbose || printf '\nChecking __rte_internal tags:\n'
294         report=$(check_internal_tags "$tmpinput")
295         if [ $? -ne 0 ] ; then
296                 $headline_printed || print_headline "$3"
297                 printf '%s\n' "$report"
298                 ret=1
299         fi
300
301         ! $verbose || printf '\nChecking release notes updates:\n'
302         report=$(check_release_notes "$tmpinput")
303         if [ $? -ne 0 ] ; then
304                 $headline_printed || print_headline "$3"
305                 printf '%s\n' "$report"
306                 ret=1
307         fi
308
309         if [ "$tmpinput" != "$1" ]; then
310                 rm -f "$tmpinput"
311                 trap - INT
312         fi
313         [ $ret -eq 0 ] && return 0
314
315         status=$(($status + 1))
316 }
317
318 if [ -n "$1" ] ; then
319         for patch in "$@" ; do
320                 # Subject can be on 2 lines
321                 subject=$(sed '/^Subject: */!d;s///;N;s,\n[[:space:]]\+, ,;s,\n.*,,;q' "$patch")
322                 check "$patch" '' "$subject"
323         done
324 elif [ ! -t 0 ] ; then # stdin
325         subject=$(while read header value ; do
326                 if [ "$header" = 'Subject:' ] ; then
327                         IFS= read next
328                         continuation=$(echo "$next" | sed -n 's,^[[:space:]]\+, ,p')
329                         echo $value$continuation
330                         break
331                 fi
332         done)
333         check '' '' "$subject"
334 else
335         if [ $number -eq 0 ] ; then
336                 commits=$(git rev-list --reverse $range)
337         else
338                 commits=$(git rev-list --reverse --max-count=$number HEAD)
339         fi
340         for commit in $commits ; do
341                 subject=$(git log --format='%s' -1 $commit)
342                 check '' $commit "$subject"
343         done
344 fi
345 pass=$(($total - $status))
346 $quiet || printf '\n%d/%d valid patch' $pass $total
347 $quiet || [ $pass -le 1 ] || printf 'es'
348 $quiet || printf '\n'
349 exit $status