2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright 2015 6WIND S.A.
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
12 VALIDATE_NEW_API=$(dirname $(readlink -f $0))/check-symbol-change.sh
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}
20 # override default Linux options
22 if [ "$codespell" = "enable" ] ; then
23 options="$options --codespell"
24 elif [ -f "$codespell" ] ; then
25 options="$options --codespell"
26 options="$options --codespellfile $codespell"
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"
41 usage: $(basename $0) [-h] [-q] [-v] [-nX|-r range|patch1 [patch2] ...]
43 Run Linux kernel checkpatch.pl with DPDK options.
44 The environment variable DPDK_CHECKPATCH_PATH must be set.
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..").
52 check_forbidden_additions() { # <patch>
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\\\(" \
60 -v MESSAGE='Using rte_panic/rte_exit' \
61 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
64 # refrain from using compiler attribute without defining a common macro
65 awk -v FOLDERS="lib drivers app examples" \
66 -v EXPRESSIONS="__attribute__" \
68 -v MESSAGE='Using compiler attribute directly' \
69 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
72 # check %l or %ll format specifier
73 awk -v FOLDERS='lib drivers app examples' \
74 -v EXPRESSIONS='%ll*[xud]' \
76 -v MESSAGE='Using %l format, prefer %PRI*64 if type is [u]int64_t' \
77 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
80 # forbid variable declaration inside "for" loop
82 -v EXPRESSIONS='for[[:space:]]*\\((char|u?int|unsigned|s?size_t)' \
84 -v MESSAGE='Declaring a variable inside for()' \
85 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
88 # refrain from new additions of 16/32/64 bits rte_atomicNN_xxx()
89 awk -v FOLDERS="lib drivers app examples" \
90 -v EXPRESSIONS="rte_atomic[0-9][0-9]_.*\\\(" \
92 -v MESSAGE='Using rte_atomicNN_xxx' \
93 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
96 # refrain from new additions of rte_smp_[r/w]mb()
97 awk -v FOLDERS="lib drivers app examples" \
98 -v EXPRESSIONS="rte_smp_(r|w)?mb\\\(" \
100 -v MESSAGE='Using rte_smp_[r/w]mb' \
101 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
104 # refrain from using compiler __sync_xxx builtins
105 awk -v FOLDERS="lib drivers app examples" \
106 -v EXPRESSIONS="__sync_.*\\\(" \
108 -v MESSAGE='Using __sync_xxx builtins' \
109 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
112 # refrain from using compiler __atomic_thread_fence()
113 # It should be avoided on x86 for SMP case.
114 awk -v FOLDERS="lib drivers app examples" \
115 -v EXPRESSIONS="__atomic_thread_fence\\\(" \
117 -v MESSAGE='Using __atomic_thread_fence' \
118 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
121 # forbid use of experimental build flag except in examples
122 awk -v FOLDERS='lib drivers app' \
123 -v EXPRESSIONS='-DALLOW_EXPERIMENTAL_API allow_experimental_apis' \
125 -v MESSAGE='Using experimental build flag for in-tree compilation' \
126 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
129 # refrain from using RTE_LOG_REGISTER for drivers and libs
130 awk -v FOLDERS='lib drivers' \
131 -v EXPRESSIONS='\\<RTE_LOG_REGISTER\\>' \
133 -v MESSAGE='Using RTE_LOG_REGISTER, prefer RTE_LOG_REGISTER_(DEFAULT|SUFFIX)' \
134 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
137 # SVG must be included with wildcard extension to allow conversion
138 awk -v FOLDERS='doc' \
139 -v EXPRESSIONS='::[[:space:]]*[^[:space:]]*\\.svg' \
141 -v MESSAGE='Using explicit .svg extension instead of .*' \
142 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
145 # links must prefer https over http
146 awk -v FOLDERS='doc' \
147 -v EXPRESSIONS='http://.*dpdk.org' \
149 -v MESSAGE='Using non https link to dpdk.org' \
150 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
156 check_experimental_tags() { # <patch>
167 /^+.*__rte_experimental/ {
168 if (current_file ~ ".c$" ) {
169 print "Please only put __rte_experimental tags in " \
170 "headers ("current_file")";
173 if ($1 != "+__rte_experimental" || $2 != "") {
174 print "__rte_experimental must appear alone on the line" \
175 " immediately preceding the return type of a function."
186 check_internal_tags() { # <patch>
197 /^+.*__rte_internal/ {
198 if (current_file ~ ".c$" ) {
199 print "Please only put __rte_internal tags in " \
200 "headers ("current_file")";
203 if ($1 != "+__rte_internal" || $2 != "") {
204 print "__rte_internal must appear alone on the line" \
205 " immediately preceding the return type of" \
217 check_release_notes() { # <patch>
218 rel_notes_prefix=doc/guides/rel_notes/release_
219 IFS=. read year month release < VERSION
220 current_rel_notes=${rel_notes_prefix}${year}_${month}.rst
222 ! grep -e '^--- a/'$rel_notes_prefix -e '^+++ b/'$rel_notes_prefix "$1" |
223 grep -v $current_rel_notes
227 range='origin/main..'
230 while getopts hn:qr:v ARG ; do
232 n ) number=$OPTARG ;;
236 h ) print_usage ; exit 0 ;;
237 ? ) print_usage ; exit 1 ;;
240 shift $(($OPTIND - 1))
242 if [ ! -f "$DPDK_CHECKPATCH_PATH" ] || [ ! -x "$DPDK_CHECKPATCH_PATH" ] ; then
245 echo 'Cannot execute DPDK_CHECKPATCH_PATH' >&2
249 print_headline() { # <title>
250 printf '\n### %s\n\n' "$1"
251 headline_printed=true
257 check () { # <patch> <commit> <title>
259 headline_printed=false
261 total=$(($total + 1))
262 ! $verbose || print_headline "$3"
263 if [ -n "$1" ] ; then
266 tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
267 trap "rm -f '$tmpinput'" INT
269 if [ -n "$2" ] ; then
270 git format-patch --find-renames \
271 --no-stat --stdout -1 $commit > "$tmpinput"
277 ! $verbose || printf 'Running checkpatch.pl:\n'
278 report=$($DPDK_CHECKPATCH_PATH $options "$tmpinput" 2>/dev/null)
279 if [ $? -ne 0 ] ; then
280 $headline_printed || print_headline "$3"
281 printf '%s\n' "$report" | sed -n '1,/^total:.*lines checked$/p'
285 ! $verbose || printf '\nChecking API additions/removals:\n'
286 report=$($VALIDATE_NEW_API "$tmpinput")
287 if [ $? -ne 0 ] ; then
288 $headline_printed || print_headline "$3"
289 printf '%s\n' "$report"
293 ! $verbose || printf '\nChecking forbidden tokens additions:\n'
294 report=$(check_forbidden_additions "$tmpinput")
295 if [ $? -ne 0 ] ; then
296 $headline_printed || print_headline "$3"
297 printf '%s\n' "$report"
301 ! $verbose || printf '\nChecking __rte_experimental tags:\n'
302 report=$(check_experimental_tags "$tmpinput")
303 if [ $? -ne 0 ] ; then
304 $headline_printed || print_headline "$3"
305 printf '%s\n' "$report"
309 ! $verbose || printf '\nChecking __rte_internal tags:\n'
310 report=$(check_internal_tags "$tmpinput")
311 if [ $? -ne 0 ] ; then
312 $headline_printed || print_headline "$3"
313 printf '%s\n' "$report"
317 ! $verbose || printf '\nChecking release notes updates:\n'
318 report=$(check_release_notes "$tmpinput")
319 if [ $? -ne 0 ] ; then
320 $headline_printed || print_headline "$3"
321 printf '%s\n' "$report"
325 if [ "$tmpinput" != "$1" ]; then
329 [ $ret -eq 0 ] && return 0
331 status=$(($status + 1))
334 if [ -n "$1" ] ; then
335 for patch in "$@" ; do
336 # Subject can be on 2 lines
337 subject=$(sed '/^Subject: */!d;s///;N;s,\n[[:space:]]\+, ,;s,\n.*,,;q' "$patch")
338 check "$patch" '' "$subject"
340 elif [ ! -t 0 ] ; then # stdin
341 subject=$(while read header value ; do
342 if [ "$header" = 'Subject:' ] ; then
344 continuation=$(echo "$next" | sed -n 's,^[[:space:]]\+, ,p')
345 echo $value$continuation
349 check '' '' "$subject"
351 if [ $number -eq 0 ] ; then
352 commits=$(git rev-list --reverse $range)
354 commits=$(git rev-list --reverse --max-count=$number HEAD)
356 for commit in $commits ; do
357 subject=$(git log --format='%s' -1 $commit)
358 check '' $commit "$subject"
361 pass=$(($total - $status))
362 $quiet || printf '\n%d/%d valid patch' $pass $total
363 $quiet || [ $pass -le 1 ] || printf 'es'
364 $quiet || printf '\n'