devtools: forbid variable declaration inside for
[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,\
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,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) [-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/master..").
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 *\\((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         # svg figures must be included with wildcard extension
81         # because of png conversion for pdf docs
82         awk -v FOLDERS='doc' \
83                 -v EXPRESSIONS='::[[:space:]]*[^[:space:]]*\\.svg' \
84                 -v RET_ON_FAIL=1 \
85                 -v MESSAGE='Using explicit .svg extension instead of .*' \
86                 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
87                 "$1" || res=1
88
89         # links must prefer https over http
90         awk -v FOLDERS='doc' \
91                 -v EXPRESSIONS='http://.*dpdk.org' \
92                 -v RET_ON_FAIL=1 \
93                 -v MESSAGE='Using non https link to dpdk.org' \
94                 -f $(dirname $(readlink -f $0))/check-forbidden-tokens.awk \
95                 "$1" || res=1
96
97         return $res
98 }
99
100 check_experimental_tags() { # <patch>
101         res=0
102
103         cat "$1" |awk '
104         BEGIN {
105                 current_file = "";
106                 ret = 0;
107         }
108         /^+++ b\// {
109                 current_file = $2;
110         }
111         /^+.*__rte_experimental/ {
112                 if (current_file ~ ".c$" ) {
113                         print "Please only put __rte_experimental tags in " \
114                                 "headers ("current_file")";
115                         ret = 1;
116                 }
117                 if ($1 != "+__rte_experimental" || $2 != "") {
118                         print "__rte_experimental must appear alone on the line" \
119                                 " immediately preceding the return type of a function."
120                         ret = 1;
121                 }
122         }
123         END {
124                 exit ret;
125         }' || res=1
126
127         return $res
128 }
129
130 check_internal_tags() { # <patch>
131         res=0
132
133         cat "$1" |awk '
134         BEGIN {
135                 current_file = "";
136                 ret = 0;
137         }
138         /^+++ b\// {
139                 current_file = $2;
140         }
141         /^+.*__rte_internal/ {
142                 if (current_file ~ ".c$" ) {
143                         print "Please only put __rte_internal tags in " \
144                                 "headers ("current_file")";
145                         ret = 1;
146                 }
147                 if ($1 != "+__rte_internal" || $2 != "") {
148                         print "__rte_internal must appear alone on the line" \
149                                 " immediately preceding the return type of" \
150                                 " a function."
151                         ret = 1;
152                 }
153         }
154         END {
155                 exit ret;
156         }' || res=1
157
158         return $res
159 }
160
161 number=0
162 range='origin/master..'
163 quiet=false
164 verbose=false
165 while getopts hn:qr:v ARG ; do
166         case $ARG in
167                 n ) number=$OPTARG ;;
168                 q ) quiet=true ;;
169                 r ) range=$OPTARG ;;
170                 v ) verbose=true ;;
171                 h ) print_usage ; exit 0 ;;
172                 ? ) print_usage ; exit 1 ;;
173         esac
174 done
175 shift $(($OPTIND - 1))
176
177 if [ ! -f "$DPDK_CHECKPATCH_PATH" ] || [ ! -x "$DPDK_CHECKPATCH_PATH" ] ; then
178         print_usage >&2
179         echo
180         echo 'Cannot execute DPDK_CHECKPATCH_PATH' >&2
181         exit 1
182 fi
183
184 print_headline() { # <title>
185         printf '\n### %s\n\n' "$1"
186         headline_printed=true
187 }
188
189 total=0
190 status=0
191
192 check () { # <patch> <commit> <title>
193         local ret=0
194         headline_printed=false
195
196         total=$(($total + 1))
197         ! $verbose || print_headline "$3"
198         if [ -n "$1" ] ; then
199                 tmpinput=$1
200         else
201                 tmpinput=$(mktemp -t dpdk.checkpatches.XXXXXX)
202                 trap "rm -f '$tmpinput'" INT
203
204                 if [ -n "$2" ] ; then
205                         git format-patch --find-renames \
206                         --no-stat --stdout -1 $commit > "$tmpinput"
207                 else
208                         cat > "$tmpinput"
209                 fi
210         fi
211
212         ! $verbose || printf 'Running checkpatch.pl:\n'
213         report=$($DPDK_CHECKPATCH_PATH $options "$tmpinput" 2>/dev/null)
214         if [ $? -ne 0 ] ; then
215                 $headline_printed || print_headline "$3"
216                 printf '%s\n' "$report" | sed -n '1,/^total:.*lines checked$/p'
217                 ret=1
218         fi
219
220         ! $verbose || printf '\nChecking API additions/removals:\n'
221         report=$($VALIDATE_NEW_API "$tmpinput")
222         if [ $? -ne 0 ] ; then
223                 $headline_printed || print_headline "$3"
224                 printf '%s\n' "$report"
225                 ret=1
226         fi
227
228         ! $verbose || printf '\nChecking forbidden tokens additions:\n'
229         report=$(check_forbidden_additions "$tmpinput")
230         if [ $? -ne 0 ] ; then
231                 $headline_printed || print_headline "$3"
232                 printf '%s\n' "$report"
233                 ret=1
234         fi
235
236         ! $verbose || printf '\nChecking __rte_experimental tags:\n'
237         report=$(check_experimental_tags "$tmpinput")
238         if [ $? -ne 0 ] ; then
239                 $headline_printed || print_headline "$3"
240                 printf '%s\n' "$report"
241                 ret=1
242         fi
243
244         ! $verbose || printf '\nChecking __rte_internal tags:\n'
245         report=$(check_internal_tags "$tmpinput")
246         if [ $? -ne 0 ] ; then
247                 $headline_printed || print_headline "$3"
248                 printf '%s\n' "$report"
249                 ret=1
250         fi
251
252         if [ "$tmpinput" != "$1" ]; then
253                 rm -f "$tmpinput"
254                 trap - INT
255         fi
256         [ $ret -eq 0 ] && return 0
257
258         status=$(($status + 1))
259 }
260
261 if [ -n "$1" ] ; then
262         for patch in "$@" ; do
263                 # Subject can be on 2 lines
264                 subject=$(sed '/^Subject: */!d;s///;N;s,\n[[:space:]]\+, ,;s,\n.*,,;q' "$patch")
265                 check "$patch" '' "$subject"
266         done
267 elif [ ! -t 0 ] ; then # stdin
268         subject=$(while read header value ; do
269                 if [ "$header" = 'Subject:' ] ; then
270                         IFS= read next
271                         continuation=$(echo "$next" | sed -n 's,^[[:space:]]\+, ,p')
272                         echo $value$continuation
273                         break
274                 fi
275         done)
276         check '' '' "$subject"
277 else
278         if [ $number -eq 0 ] ; then
279                 commits=$(git rev-list --reverse $range)
280         else
281                 commits=$(git rev-list --reverse --max-count=$number HEAD)
282         fi
283         for commit in $commits ; do
284                 subject=$(git log --format='%s' -1 $commit)
285                 check '' $commit "$subject"
286         done
287 fi
288 pass=$(($total - $status))
289 $quiet || printf '\n%d/%d valid patch' $pass $total
290 $quiet || [ $pass -le 1 ] || printf 'es'
291 $quiet || printf '\n'
292 exit $status