New upstream version 18.08
[deb_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_LINE_LENGTH
8 . $(dirname $(readlink -e $0))/load-devel-config
9
10 VALIDATE_NEW_API=$(dirname $(readlink -e $0))/check-symbol-change.sh
11
12 length=${DPDK_CHECKPATCH_LINE_LENGTH:-80}
13
14 # override default Linux options
15 options="--no-tree"
16 options="$options --max-line-length=$length"
17 options="$options --show-types"
18 options="$options --ignore=LINUX_VERSION_CODE,\
19 FILE_PATH_CHANGES,MAINTAINERS_STYLE,SPDX_LICENSE_TAG,\
20 VOLATILE,PREFER_PACKED,PREFER_ALIGNED,PREFER_PRINTF,\
21 PREFER_KERNEL_TYPES,BIT_MACRO,CONST_STRUCT,\
22 SPLIT_STRING,LONG_LINE_STRING,\
23 LINE_SPACING,PARENTHESIS_ALIGNMENT,NETWORKING_BLOCK_COMMENT_STYLE,\
24 NEW_TYPEDEFS,COMPARISON_TO_NULL"
25
26 clean_tmp_files() {
27         if echo $tmpinput | grep -q '^checkpatches\.' ; then
28                 rm -f "$tmpinput"
29         fi
30 }
31
32 trap "clean_tmp_files" INT
33
34 print_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
46 check_forbidden_additions() {
47     # This awk script receives a list of expressions to monitor
48     # and a list of folders to search these expressions in
49     # - No search is done inside comments
50     # - Both additions and removals of the expressions are checked
51     #   A positive balance of additions fails the check
52         read -d '' awk_script << 'EOF'
53         BEGIN {
54                 split(FOLDERS,deny_folders," ");
55                 split(EXPRESSIONS,deny_expr," ");
56                 in_file=0;
57                 in_comment=0;
58                 count=0;
59                 comment_start="/*"
60                 comment_end="*/"
61         }
62         # search for add/remove instances in current file
63         # state machine assumes the comments structure is enforced by
64         # checkpatches.pl
65         (in_file) {
66                 # comment start
67                 if (index($0,comment_start) > 0) {
68                         in_comment = 1
69                 }
70                 # non comment code
71                 if (in_comment == 0) {
72                         for (i in deny_expr) {
73                                 forbidden_added = "^\+.*" deny_expr[i];
74                                 forbidden_removed="^-.*" deny_expr[i];
75                                 current = expressions[deny_expr[i]]
76                                 if ($0 ~ forbidden_added) {
77                                         count = count + 1;
78                                         expressions[deny_expr[i]] = current + 1
79                                 }
80                                 if ($0 ~ forbidden_removed) {
81                                         count = count - 1;
82                                         expressions[deny_expr[i]] = current - 1
83                                 }
84                         }
85                 }
86                 # comment end
87                 if (index($0,comment_end) > 0) {
88                         in_comment = 0
89                 }
90         }
91         # switch to next file , check if the balance of add/remove
92         # of previous filehad new additions
93         ($0 ~ "^\+\+\+ b/") {
94                 in_file = 0;
95                 if (count > 0) {
96                         exit;
97                 }
98                 for (i in deny_folders) {
99                         re = "^\+\+\+ b/" deny_folders[i];
100                         if ($0 ~ deny_folders[i]) {
101                                 in_file = 1
102                                 last_file = $0
103                         }
104                 }
105         }
106         END {
107                 if (count > 0) {
108                         print "Warning in " substr(last_file,6) ":"
109                         print "are you sure you want to add the following:"
110                         for (key in expressions) {
111                                 if (expressions[key] > 0) {
112                                         print key
113                                 }
114                         }
115                         exit RET_ON_FAIL
116                 }
117         }
118 EOF
119         # ---------------------------------
120         # refrain from new additions of rte_panic() and rte_exit()
121         # multiple folders and expressions are separated by spaces
122         awk -v FOLDERS="lib drivers" \
123                 -v EXPRESSIONS="rte_panic\\\( rte_exit\\\(" \
124                 -v RET_ON_FAIL=1 \
125                 "$awk_script" -
126 }
127
128 number=0
129 quiet=false
130 verbose=false
131 while getopts hn:qv ARG ; do
132         case $ARG in
133                 n ) number=$OPTARG ;;
134                 q ) quiet=true ;;
135                 v ) verbose=true ;;
136                 h ) print_usage ; exit 0 ;;
137                 ? ) print_usage ; exit 1 ;;
138         esac
139 done
140 shift $(($OPTIND - 1))
141
142 if [ ! -f "$DPDK_CHECKPATCH_PATH" ] || [ ! -x "$DPDK_CHECKPATCH_PATH" ] ; then
143         print_usage >&2
144         echo
145         echo 'Cannot execute DPDK_CHECKPATCH_PATH' >&2
146         exit 1
147 fi
148
149 total=0
150 status=0
151
152 check () { # <patch> <commit> <title>
153         local ret=0
154
155         total=$(($total + 1))
156         ! $verbose || printf '\n### %s\n\n' "$3"
157         if [ -n "$1" ] ; then
158                 tmpinput=$1
159         elif [ -n "$2" ] ; then
160                 tmpinput=$(mktemp checkpatches.XXXXXX)
161                 git format-patch --find-renames \
162                 --no-stat --stdout -1 $commit > "$tmpinput"
163         else
164                 tmpinput=$(mktemp checkpatches.XXXXXX)
165                 cat > "$tmpinput"
166         fi
167
168         report=$($DPDK_CHECKPATCH_PATH $options "$tmpinput" 2>/dev/null)
169         if [ $? -ne 0 ] ; then
170                 $verbose || printf '\n### %s\n\n' "$3"
171                 printf '%s\n' "$report" | sed -n '1,/^total:.*lines checked$/p'
172                 ret=1
173         fi
174
175         ! $verbose || printf '\nChecking API additions/removals:\n'
176         report=$($VALIDATE_NEW_API "$tmpinput")
177         if [ $? -ne 0 ] ; then
178                 printf '%s\n' "$report"
179                 ret=1
180         fi
181
182         ! $verbose || printf '\nChecking forbidden tokens additions:\n'
183         report=$(check_forbidden_additions <"$tmpinput")
184         if [ $? -ne 0 ] ; then
185                 printf '%s\n' "$report"
186                 ret=1
187         fi
188
189         clean_tmp_files
190         [ $ret -eq 0 ] && return 0
191
192         status=$(($status + 1))
193 }
194
195 if [ -n "$1" ] ; then
196         for patch in "$@" ; do
197                 # Subject can be on 2 lines
198                 subject=$(sed '/^Subject: */!d;s///;N;s,\n[[:space:]]\+, ,;s,\n.*,,;q' "$patch")
199                 check "$patch" '' "$subject"
200         done
201 elif [ ! -t 0 ] ; then # stdin
202         subject=$(while read header value ; do
203                 if [ "$header" = 'Subject:' ] ; then
204                         IFS= read next
205                         continuation=$(echo "$next" | sed -n 's,^[[:space:]]\+, ,p')
206                         echo $value$continuation
207                         break
208                 fi
209         done)
210         check '' '' "$subject"
211 else
212         if [ $number -eq 0 ] ; then
213                 commits=$(git rev-list --reverse origin/master..)
214         else
215                 commits=$(git rev-list --reverse --max-count=$number HEAD)
216         fi
217         for commit in $commits ; do
218                 subject=$(git log --format='%s' -1 $commit)
219                 check '' $commit "$subject"
220         done
221 fi
222 pass=$(($total - $status))
223 $quiet || printf '\n%d/%d valid patch' $pass $total
224 $quiet || [ $pass -le 1 ] || printf 'es'
225 $quiet || printf '\n'
226 exit $status