]>
Commit | Line | Data |
---|---|---|
b7d5a9c2 | 1 | #!/usr/bin/env perl |
1ec3f6f9 BS |
2 | # (c) 2001, Dave Jones. (the file handling bit) |
3 | # (c) 2005, Joel Schopp <[email protected]> (the ugly bit) | |
4 | # (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite) | |
5 | # (c) 2008-2010 Andy Whitcroft <[email protected]> | |
6 | # Licensed under the terms of the GNU GPL License version 2 | |
7 | ||
8 | use strict; | |
b7d5a9c2 | 9 | use warnings; |
1db4269f | 10 | use Term::ANSIColor qw(:constants); |
1ec3f6f9 BS |
11 | |
12 | my $P = $0; | |
13 | $P =~ s@.*/@@g; | |
14 | ||
777d05ba PB |
15 | our $SrcFile = qr{\.(?:h|c|cpp|s|S|pl|py|sh)$}; |
16 | ||
1ec3f6f9 BS |
17 | my $V = '0.31'; |
18 | ||
19 | use Getopt::Long qw(:config no_auto_abbrev); | |
20 | ||
21 | my $quiet = 0; | |
22 | my $tree = 1; | |
23 | my $chk_signoff = 1; | |
8e1fe175 DB |
24 | my $chk_patch = undef; |
25 | my $chk_branch = undef; | |
1ec3f6f9 BS |
26 | my $tst_only; |
27 | my $emacs = 0; | |
28 | my $terse = 0; | |
8e1fe175 | 29 | my $file = undef; |
1db4269f | 30 | my $color = "auto"; |
141de886 | 31 | my $no_warnings = 0; |
1ec3f6f9 BS |
32 | my $summary = 1; |
33 | my $mailback = 0; | |
34 | my $summary_file = 0; | |
35 | my $root; | |
36 | my %debug; | |
37 | my $help = 0; | |
38 | ||
39 | sub help { | |
40 | my ($exitcode) = @_; | |
41 | ||
42 | print << "EOM"; | |
8e1fe175 DB |
43 | Usage: |
44 | ||
45 | $P [OPTION]... [FILE]... | |
46 | $P [OPTION]... [GIT-REV-LIST] | |
47 | ||
1ec3f6f9 BS |
48 | Version: $V |
49 | ||
50 | Options: | |
51 | -q, --quiet quiet | |
52 | --no-tree run without a kernel tree | |
53 | --no-signoff do not check for 'Signed-off-by' line | |
8e1fe175 DB |
54 | --patch treat FILE as patchfile |
55 | --branch treat args as GIT revision list | |
1ec3f6f9 BS |
56 | --emacs emacs compile window format |
57 | --terse one line per report | |
58 | -f, --file treat FILE as regular source file | |
141de886 | 59 | --strict fail if only warnings are found |
1ec3f6f9 BS |
60 | --root=PATH PATH to the kernel tree root |
61 | --no-summary suppress the per-file summary | |
62 | --mailback only produce a report in case of warnings/errors | |
63 | --summary-file include the filename in summary | |
64 | --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of | |
65 | 'values', 'possible', 'type', and 'attr' (default | |
66 | is all off) | |
67 | --test-only=WORD report only warnings/errors containing WORD | |
68 | literally | |
1db4269f PB |
69 | --color[=WHEN] Use colors 'always', 'never', or only when output |
70 | is a terminal ('auto'). Default is 'auto'. | |
1ec3f6f9 BS |
71 | -h, --help, --version display this help and exit |
72 | ||
73 | When FILE is - read standard input. | |
74 | EOM | |
75 | ||
76 | exit($exitcode); | |
77 | } | |
78 | ||
1db4269f PB |
79 | # Perl's Getopt::Long allows options to take optional arguments after a space. |
80 | # Prevent --color by itself from consuming other arguments | |
81 | foreach (@ARGV) { | |
82 | if ($_ eq "--color" || $_ eq "-color") { | |
83 | $_ = "--color=$color"; | |
84 | } | |
85 | } | |
86 | ||
1ec3f6f9 BS |
87 | GetOptions( |
88 | 'q|quiet+' => \$quiet, | |
89 | 'tree!' => \$tree, | |
90 | 'signoff!' => \$chk_signoff, | |
91 | 'patch!' => \$chk_patch, | |
8e1fe175 | 92 | 'branch!' => \$chk_branch, |
1ec3f6f9 BS |
93 | 'emacs!' => \$emacs, |
94 | 'terse!' => \$terse, | |
95 | 'f|file!' => \$file, | |
141de886 | 96 | 'strict!' => \$no_warnings, |
1ec3f6f9 BS |
97 | 'root=s' => \$root, |
98 | 'summary!' => \$summary, | |
99 | 'mailback!' => \$mailback, | |
100 | 'summary-file!' => \$summary_file, | |
101 | ||
102 | 'debug=s' => \%debug, | |
103 | 'test-only=s' => \$tst_only, | |
1db4269f PB |
104 | 'color=s' => \$color, |
105 | 'no-color' => sub { $color = 'never'; }, | |
1ec3f6f9 BS |
106 | 'h|help' => \$help, |
107 | 'version' => \$help | |
108 | ) or help(1); | |
109 | ||
110 | help(0) if ($help); | |
111 | ||
112 | my $exit = 0; | |
113 | ||
114 | if ($#ARGV < 0) { | |
115 | print "$P: no input files\n"; | |
116 | exit(1); | |
117 | } | |
118 | ||
8e1fe175 | 119 | if (!defined $chk_branch && !defined $chk_patch && !defined $file) { |
777d05ba PB |
120 | $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0; |
121 | $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0; | |
122 | $chk_patch = $chk_branch || $file ? 0 : 1; | |
8e1fe175 DB |
123 | } elsif (!defined $chk_branch && !defined $chk_patch) { |
124 | if ($file) { | |
125 | $chk_branch = $chk_patch = 0; | |
126 | } else { | |
777d05ba | 127 | $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0; |
8e1fe175 DB |
128 | $chk_patch = $chk_branch ? 0 : 1; |
129 | } | |
130 | } elsif (!defined $chk_branch && !defined $file) { | |
131 | if ($chk_patch) { | |
132 | $chk_branch = $file = 0; | |
133 | } else { | |
777d05ba | 134 | $chk_branch = $ARGV[0] =~ /.\.\./ ? 1 : 0; |
8e1fe175 DB |
135 | $file = $chk_branch ? 0 : 1; |
136 | } | |
137 | } elsif (!defined $chk_patch && !defined $file) { | |
138 | if ($chk_branch) { | |
139 | $chk_patch = $file = 0; | |
140 | } else { | |
777d05ba PB |
141 | $file = $ARGV[0] =~ /$SrcFile/ ? 1 : 0; |
142 | $chk_patch = $file ? 0 : 1; | |
8e1fe175 DB |
143 | } |
144 | } elsif (!defined $chk_branch) { | |
145 | $chk_branch = $chk_patch || $file ? 0 : 1; | |
146 | } elsif (!defined $chk_patch) { | |
147 | $chk_patch = $chk_branch || $file ? 0 : 1; | |
148 | } elsif (!defined $file) { | |
149 | $file = $chk_patch || $chk_branch ? 0 : 1; | |
150 | } | |
151 | ||
152 | if (($chk_patch && $chk_branch) || | |
153 | ($chk_patch && $file) || | |
154 | ($chk_branch && $file)) { | |
155 | die "Only one of --file, --branch, --patch is permitted\n"; | |
156 | } | |
157 | if (!$chk_patch && !$chk_branch && !$file) { | |
158 | die "One of --file, --branch, --patch is required\n"; | |
159 | } | |
160 | ||
1db4269f PB |
161 | if ($color =~ /^always$/i) { |
162 | $color = 1; | |
163 | } elsif ($color =~ /^never$/i) { | |
164 | $color = 0; | |
165 | } elsif ($color =~ /^auto$/i) { | |
166 | $color = (-t STDOUT); | |
167 | } else { | |
168 | die "Invalid color mode: $color\n"; | |
169 | } | |
170 | ||
1ec3f6f9 BS |
171 | my $dbg_values = 0; |
172 | my $dbg_possible = 0; | |
173 | my $dbg_type = 0; | |
174 | my $dbg_attr = 0; | |
a99ac041 | 175 | my $dbg_adv_dcs = 0; |
5424302e | 176 | my $dbg_adv_checking = 0; |
69402a69 | 177 | my $dbg_adv_apw = 0; |
1ec3f6f9 BS |
178 | for my $key (keys %debug) { |
179 | ## no critic | |
180 | eval "\${dbg_$key} = '$debug{$key}';"; | |
181 | die "$@" if ($@); | |
182 | } | |
183 | ||
184 | my $rpt_cleaners = 0; | |
185 | ||
186 | if ($terse) { | |
187 | $emacs = 1; | |
188 | $quiet++; | |
189 | } | |
190 | ||
191 | if ($tree) { | |
192 | if (defined $root) { | |
193 | if (!top_of_kernel_tree($root)) { | |
194 | die "$P: $root: --root does not point at a valid tree\n"; | |
195 | } | |
196 | } else { | |
197 | if (top_of_kernel_tree('.')) { | |
198 | $root = '.'; | |
199 | } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && | |
200 | top_of_kernel_tree($1)) { | |
201 | $root = $1; | |
202 | } | |
203 | } | |
204 | ||
205 | if (!defined $root) { | |
206 | print "Must be run from the top-level dir. of a kernel tree\n"; | |
207 | exit(2); | |
208 | } | |
209 | } | |
210 | ||
211 | my $emitted_corrupt = 0; | |
212 | ||
213 | our $Ident = qr{ | |
214 | [A-Za-z_][A-Za-z\d_]* | |
215 | (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* | |
216 | }x; | |
217 | our $Storage = qr{extern|static|asmlinkage}; | |
218 | our $Sparse = qr{ | |
f1e155bb | 219 | __force |
1ec3f6f9 BS |
220 | }x; |
221 | ||
222 | # Notes to $Attribute: | |
1ec3f6f9 BS |
223 | our $Attribute = qr{ |
224 | const| | |
71c47b01 PB |
225 | volatile| |
226 | QEMU_NORETURN| | |
227 | QEMU_WARN_UNUSED_RESULT| | |
228 | QEMU_SENTINEL| | |
71c47b01 PB |
229 | QEMU_PACKED| |
230 | GCC_FMT_ATTR | |
1ec3f6f9 BS |
231 | }x; |
232 | our $Modifier; | |
71c47b01 | 233 | our $Inline = qr{inline}; |
1ec3f6f9 BS |
234 | our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; |
235 | our $Lval = qr{$Ident(?:$Member)*}; | |
236 | ||
237 | our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*}; | |
238 | our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; | |
239 | our $Compare = qr{<=|>=|==|!=|<|>}; | |
240 | our $Operators = qr{ | |
241 | <=|>=|==|!=| | |
242 | =>|->|<<|>>|<|>|!|~| | |
243 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% | |
244 | }x; | |
245 | ||
246 | our $NonptrType; | |
247 | our $Type; | |
248 | our $Declare; | |
249 | ||
b37c4aac JP |
250 | our $NON_ASCII_UTF8 = qr{ |
251 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | |
1ec3f6f9 BS |
252 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
253 | | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte | |
254 | | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates | |
255 | | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | |
256 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | |
257 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 | |
258 | }x; | |
259 | ||
b37c4aac JP |
260 | our $UTF8 = qr{ |
261 | [\x09\x0A\x0D\x20-\x7E] # ASCII | |
262 | | $NON_ASCII_UTF8 | |
263 | }x; | |
264 | ||
874acb6f PB |
265 | # some readers default to ISO-8859-1 when showing email source. detect |
266 | # when UTF-8 is incorrectly interpreted as ISO-8859-1 and reencoded back. | |
267 | # False positives are possible but very unlikely. | |
268 | our $UTF8_MOJIBAKE = qr{ | |
269 | \xC3[\x82-\x9F] \xC2[\x80-\xBF] # c2-df 80-bf | |
270 | | \xC3\xA0 \xC2[\xA0-\xBF] \xC2[\x80-\xBF] # e0 a0-bf 80-bf | |
271 | | \xC3[\xA1-\xAC\xAE\xAF] (?: \xC2[\x80-\xBF]){2} # e1-ec/ee/ef 80-bf 80-bf | |
272 | | \xC3\xAD \xC2[\x80-\x9F] \xC2[\x80-\xBF] # ed 80-9f 80-bf | |
273 | | \xC3\xB0 \xC2[\x90-\xBF] (?: \xC2[\x80-\xBF]){2} # f0 90-bf 80-bf 80-bf | |
274 | | \xC3[\xB1-\xB3] (?: \xC2[\x80-\xBF]){3} # f1-f3 80-bf 80-bf 80-bf | |
275 | | \xC3\xB4 \xC2[\x80-\x8F] (?: \xC2[\x80-\xBF]){2} # f4 80-b8 80-bf 80-bf | |
276 | }x; | |
277 | ||
a6859deb PB |
278 | # There are still some false positives, but this catches most |
279 | # common cases. | |
1ec3f6f9 | 280 | our $typeTypedefs = qr{(?x: |
5fa96cad | 281 | (?![KMGTPE]iB) # IEC binary prefix (do not match) |
a6859deb PB |
282 | [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]* # camelcase |
283 | | [A-Z][A-Z\d_]*AIOCB # all uppercase | |
284 | | [A-Z][A-Z\d_]*CPU # all uppercase | |
285 | | QEMUBH # all uppercase | |
1ec3f6f9 BS |
286 | )}; |
287 | ||
1ec3f6f9 BS |
288 | our @typeList = ( |
289 | qr{void}, | |
290 | qr{(?:unsigned\s+)?char}, | |
291 | qr{(?:unsigned\s+)?short}, | |
292 | qr{(?:unsigned\s+)?int}, | |
293 | qr{(?:unsigned\s+)?long}, | |
294 | qr{(?:unsigned\s+)?long\s+int}, | |
295 | qr{(?:unsigned\s+)?long\s+long}, | |
296 | qr{(?:unsigned\s+)?long\s+long\s+int}, | |
297 | qr{unsigned}, | |
298 | qr{float}, | |
299 | qr{double}, | |
300 | qr{bool}, | |
301 | qr{struct\s+$Ident}, | |
302 | qr{union\s+$Ident}, | |
303 | qr{enum\s+$Ident}, | |
304 | qr{${Ident}_t}, | |
305 | qr{${Ident}_handler}, | |
306 | qr{${Ident}_handler_fn}, | |
f0707d2e | 307 | qr{target_(?:u)?long}, |
825bfa00 | 308 | qr{hwaddr}, |
82870f3c | 309 | # external libraries |
ed279a06 | 310 | qr{xml${Ident}}, |
8d0f08e9 | 311 | qr{xen\w+_handle}, |
82870f3c PX |
312 | # Glib definitions |
313 | qr{gchar}, | |
314 | qr{gshort}, | |
315 | qr{glong}, | |
316 | qr{gint}, | |
317 | qr{gboolean}, | |
318 | qr{guchar}, | |
319 | qr{gushort}, | |
320 | qr{gulong}, | |
321 | qr{guint}, | |
322 | qr{gfloat}, | |
323 | qr{gdouble}, | |
324 | qr{gpointer}, | |
325 | qr{gconstpointer}, | |
326 | qr{gint8}, | |
327 | qr{guint8}, | |
328 | qr{gint16}, | |
329 | qr{guint16}, | |
330 | qr{gint32}, | |
331 | qr{guint32}, | |
332 | qr{gint64}, | |
333 | qr{guint64}, | |
334 | qr{gsize}, | |
335 | qr{gssize}, | |
336 | qr{goffset}, | |
337 | qr{gintptr}, | |
338 | qr{guintptr}, | |
1ec3f6f9 | 339 | ); |
f1e155bb PB |
340 | |
341 | # This can be modified by sub possible. Since it can be empty, be careful | |
342 | # about regexes that always match, because they can cause infinite loops. | |
1ec3f6f9 | 343 | our @modifierList = ( |
1ec3f6f9 BS |
344 | ); |
345 | ||
1ec3f6f9 | 346 | sub build_types { |
1ec3f6f9 | 347 | my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; |
f1e155bb PB |
348 | if (@modifierList > 0) { |
349 | my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; | |
350 | $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; | |
351 | } else { | |
352 | $Modifier = qr{(?:$Attribute|$Sparse)}; | |
353 | } | |
1ec3f6f9 BS |
354 | $NonptrType = qr{ |
355 | (?:$Modifier\s+|const\s+)* | |
356 | (?: | |
357 | (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)| | |
358 | (?:$typeTypedefs\b)| | |
359 | (?:${all}\b) | |
360 | ) | |
361 | (?:\s+$Modifier|\s+const)* | |
362 | }x; | |
363 | $Type = qr{ | |
364 | $NonptrType | |
365 | (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? | |
366 | (?:\s+$Inline|\s+$Modifier)* | |
367 | }x; | |
368 | $Declare = qr{(?:$Storage\s+)?$Type}; | |
369 | } | |
370 | build_types(); | |
371 | ||
372 | $chk_signoff = 0 if ($file); | |
373 | ||
1ec3f6f9 BS |
374 | my @rawlines = (); |
375 | my @lines = (); | |
376 | my $vname; | |
8e1fe175 DB |
377 | if ($chk_branch) { |
378 | my @patches; | |
c182b619 | 379 | my %git_commits = (); |
8e1fe175 | 380 | my $HASH; |
c182b619 PB |
381 | open($HASH, "-|", "git", "log", "--reverse", "--no-merges", "--format=%H %s", $ARGV[0]) || |
382 | die "$P: git log --reverse --no-merges --format='%H %s' $ARGV[0] failed - $!\n"; | |
383 | ||
384 | for my $line (<$HASH>) { | |
385 | $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/; | |
386 | next if (!defined($1) || !defined($2)); | |
387 | my $sha1 = $1; | |
388 | my $subject = $2; | |
389 | push(@patches, $sha1); | |
390 | $git_commits{$sha1} = $subject; | |
1ec3f6f9 | 391 | } |
8e1fe175 DB |
392 | |
393 | close $HASH; | |
394 | ||
395 | die "$P: no revisions returned for revlist '$chk_branch'\n" | |
396 | unless @patches; | |
397 | ||
c182b619 PB |
398 | my $i = 1; |
399 | my $num_patches = @patches; | |
8e1fe175 DB |
400 | for my $hash (@patches) { |
401 | my $FILE; | |
402 | open($FILE, '-|', "git", "show", $hash) || | |
403 | die "$P: git show $hash - $!\n"; | |
8e1fe175 DB |
404 | while (<$FILE>) { |
405 | chomp; | |
406 | push(@rawlines, $_); | |
407 | } | |
408 | close($FILE); | |
c182b619 PB |
409 | $vname = substr($hash, 0, 12) . ' (' . $git_commits{$hash} . ')'; |
410 | if ($num_patches > 1 && $quiet == 0) { | |
1db4269f PB |
411 | my $prefix = "$i/$num_patches"; |
412 | $prefix = BLUE . BOLD . $prefix . RESET if $color; | |
413 | print "$prefix Checking commit $vname\n"; | |
c182b619 PB |
414 | $vname = "Patch $i/$num_patches"; |
415 | } else { | |
416 | $vname = "Commit " . $vname; | |
417 | } | |
8e1fe175 DB |
418 | if (!process($hash)) { |
419 | $exit = 1; | |
c182b619 | 420 | print "\n" if ($num_patches > 1 && $quiet == 0); |
8e1fe175 DB |
421 | } |
422 | @rawlines = (); | |
423 | @lines = (); | |
c182b619 | 424 | $i++; |
8e1fe175 DB |
425 | } |
426 | } else { | |
427 | for my $filename (@ARGV) { | |
428 | my $FILE; | |
429 | if ($file) { | |
430 | open($FILE, '-|', "diff -u /dev/null $filename") || | |
431 | die "$P: $filename: diff failed - $!\n"; | |
432 | } elsif ($filename eq '-') { | |
433 | open($FILE, '<&STDIN'); | |
434 | } else { | |
435 | open($FILE, '<', "$filename") || | |
436 | die "$P: $filename: open failed - $!\n"; | |
437 | } | |
438 | if ($filename eq '-') { | |
439 | $vname = 'Your patch'; | |
440 | } else { | |
441 | $vname = $filename; | |
442 | } | |
c182b619 | 443 | print "Checking $filename...\n" if @ARGV > 1 && $quiet == 0; |
8e1fe175 DB |
444 | while (<$FILE>) { |
445 | chomp; | |
446 | push(@rawlines, $_); | |
447 | } | |
448 | close($FILE); | |
449 | if (!process($filename)) { | |
450 | $exit = 1; | |
451 | } | |
452 | @rawlines = (); | |
453 | @lines = (); | |
1ec3f6f9 | 454 | } |
1ec3f6f9 BS |
455 | } |
456 | ||
457 | exit($exit); | |
458 | ||
459 | sub top_of_kernel_tree { | |
460 | my ($root) = @_; | |
461 | ||
462 | my @tree_check = ( | |
b6469683 BS |
463 | "COPYING", "MAINTAINERS", "Makefile", |
464 | "README", "docs", "VERSION", | |
465 | "vl.c" | |
1ec3f6f9 BS |
466 | ); |
467 | ||
468 | foreach my $check (@tree_check) { | |
469 | if (! -e $root . '/' . $check) { | |
470 | return 0; | |
471 | } | |
472 | } | |
473 | return 1; | |
474 | } | |
475 | ||
476 | sub expand_tabs { | |
477 | my ($str) = @_; | |
478 | ||
479 | my $res = ''; | |
480 | my $n = 0; | |
481 | for my $c (split(//, $str)) { | |
482 | if ($c eq "\t") { | |
483 | $res .= ' '; | |
484 | $n++; | |
485 | for (; ($n % 8) != 0; $n++) { | |
486 | $res .= ' '; | |
487 | } | |
488 | next; | |
489 | } | |
490 | $res .= $c; | |
491 | $n++; | |
492 | } | |
493 | ||
494 | return $res; | |
495 | } | |
496 | sub copy_spacing { | |
497 | (my $res = shift) =~ tr/\t/ /c; | |
498 | return $res; | |
499 | } | |
500 | ||
501 | sub line_stats { | |
502 | my ($line) = @_; | |
503 | ||
504 | # Drop the diff line leader and expand tabs | |
505 | $line =~ s/^.//; | |
506 | $line = expand_tabs($line); | |
507 | ||
508 | # Pick the indent from the front of the line. | |
509 | my ($white) = ($line =~ /^(\s*)/); | |
510 | ||
511 | return (length($line), length($white)); | |
512 | } | |
513 | ||
514 | my $sanitise_quote = ''; | |
515 | ||
516 | sub sanitise_line_reset { | |
517 | my ($in_comment) = @_; | |
518 | ||
519 | if ($in_comment) { | |
520 | $sanitise_quote = '*/'; | |
521 | } else { | |
522 | $sanitise_quote = ''; | |
523 | } | |
524 | } | |
525 | sub sanitise_line { | |
526 | my ($line) = @_; | |
527 | ||
528 | my $res = ''; | |
529 | my $l = ''; | |
530 | ||
531 | my $qlen = 0; | |
532 | my $off = 0; | |
533 | my $c; | |
534 | ||
535 | # Always copy over the diff marker. | |
536 | $res = substr($line, 0, 1); | |
537 | ||
538 | for ($off = 1; $off < length($line); $off++) { | |
539 | $c = substr($line, $off, 1); | |
540 | ||
cb8d4c8f | 541 | # Comments we are wacking completely including the begin |
1ec3f6f9 BS |
542 | # and end, all to $;. |
543 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { | |
544 | $sanitise_quote = '*/'; | |
545 | ||
546 | substr($res, $off, 2, "$;$;"); | |
547 | $off++; | |
548 | next; | |
549 | } | |
550 | if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { | |
551 | $sanitise_quote = ''; | |
552 | substr($res, $off, 2, "$;$;"); | |
553 | $off++; | |
554 | next; | |
555 | } | |
556 | if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { | |
557 | $sanitise_quote = '//'; | |
558 | ||
559 | substr($res, $off, 2, $sanitise_quote); | |
560 | $off++; | |
561 | next; | |
562 | } | |
563 | ||
564 | # A \ in a string means ignore the next character. | |
565 | if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && | |
566 | $c eq "\\") { | |
567 | substr($res, $off, 2, 'XX'); | |
568 | $off++; | |
569 | next; | |
570 | } | |
571 | # Regular quotes. | |
572 | if ($c eq "'" || $c eq '"') { | |
573 | if ($sanitise_quote eq '') { | |
574 | $sanitise_quote = $c; | |
575 | ||
576 | substr($res, $off, 1, $c); | |
577 | next; | |
578 | } elsif ($sanitise_quote eq $c) { | |
579 | $sanitise_quote = ''; | |
580 | } | |
581 | } | |
582 | ||
583 | #print "c<$c> SQ<$sanitise_quote>\n"; | |
584 | if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { | |
585 | substr($res, $off, 1, $;); | |
586 | } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { | |
587 | substr($res, $off, 1, $;); | |
588 | } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { | |
589 | substr($res, $off, 1, 'X'); | |
590 | } else { | |
591 | substr($res, $off, 1, $c); | |
592 | } | |
593 | } | |
594 | ||
595 | if ($sanitise_quote eq '//') { | |
596 | $sanitise_quote = ''; | |
597 | } | |
598 | ||
599 | # The pathname on a #include may be surrounded by '<' and '>'. | |
600 | if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { | |
601 | my $clean = 'X' x length($1); | |
602 | $res =~ s@\<.*\>@<$clean>@; | |
603 | ||
604 | # The whole of a #error is a string. | |
605 | } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { | |
606 | my $clean = 'X' x length($1); | |
607 | $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; | |
608 | } | |
609 | ||
610 | return $res; | |
611 | } | |
612 | ||
613 | sub ctx_statement_block { | |
614 | my ($linenr, $remain, $off) = @_; | |
615 | my $line = $linenr - 1; | |
616 | my $blk = ''; | |
617 | my $soff = $off; | |
618 | my $coff = $off - 1; | |
619 | my $coff_set = 0; | |
620 | ||
621 | my $loff = 0; | |
622 | ||
623 | my $type = ''; | |
624 | my $level = 0; | |
625 | my @stack = (); | |
626 | my $p; | |
627 | my $c; | |
628 | my $len = 0; | |
629 | ||
630 | my $remainder; | |
631 | while (1) { | |
632 | @stack = (['', 0]) if ($#stack == -1); | |
633 | ||
634 | #warn "CSB: blk<$blk> remain<$remain>\n"; | |
635 | # If we are about to drop off the end, pull in more | |
636 | # context. | |
637 | if ($off >= $len) { | |
638 | for (; $remain > 0; $line++) { | |
639 | last if (!defined $lines[$line]); | |
640 | next if ($lines[$line] =~ /^-/); | |
641 | $remain--; | |
642 | $loff = $len; | |
643 | $blk .= $lines[$line] . "\n"; | |
644 | $len = length($blk); | |
645 | $line++; | |
646 | last; | |
647 | } | |
648 | # Bail if there is no further context. | |
649 | #warn "CSB: blk<$blk> off<$off> len<$len>\n"; | |
650 | if ($off >= $len) { | |
651 | last; | |
652 | } | |
653 | } | |
654 | $p = $c; | |
655 | $c = substr($blk, $off, 1); | |
656 | $remainder = substr($blk, $off); | |
657 | ||
658 | #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; | |
659 | ||
660 | # Handle nested #if/#else. | |
661 | if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { | |
662 | push(@stack, [ $type, $level ]); | |
663 | } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { | |
664 | ($type, $level) = @{$stack[$#stack - 1]}; | |
665 | } elsif ($remainder =~ /^#\s*endif\b/) { | |
666 | ($type, $level) = @{pop(@stack)}; | |
667 | } | |
668 | ||
669 | # Statement ends at the ';' or a close '}' at the | |
670 | # outermost level. | |
671 | if ($level == 0 && $c eq ';') { | |
672 | last; | |
673 | } | |
674 | ||
675 | # An else is really a conditional as long as its not else if | |
676 | if ($level == 0 && $coff_set == 0 && | |
677 | (!defined($p) || $p =~ /(?:\s|\}|\+)/) && | |
678 | $remainder =~ /^(else)(?:\s|{)/ && | |
679 | $remainder !~ /^else\s+if\b/) { | |
680 | $coff = $off + length($1) - 1; | |
681 | $coff_set = 1; | |
682 | #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; | |
683 | #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; | |
684 | } | |
685 | ||
686 | if (($type eq '' || $type eq '(') && $c eq '(') { | |
687 | $level++; | |
688 | $type = '('; | |
689 | } | |
690 | if ($type eq '(' && $c eq ')') { | |
691 | $level--; | |
692 | $type = ($level != 0)? '(' : ''; | |
693 | ||
694 | if ($level == 0 && $coff < $soff) { | |
695 | $coff = $off; | |
696 | $coff_set = 1; | |
697 | #warn "CSB: mark coff<$coff>\n"; | |
698 | } | |
699 | } | |
700 | if (($type eq '' || $type eq '{') && $c eq '{') { | |
701 | $level++; | |
702 | $type = '{'; | |
703 | } | |
704 | if ($type eq '{' && $c eq '}') { | |
705 | $level--; | |
706 | $type = ($level != 0)? '{' : ''; | |
707 | ||
708 | if ($level == 0) { | |
709 | if (substr($blk, $off + 1, 1) eq ';') { | |
710 | $off++; | |
711 | } | |
712 | last; | |
713 | } | |
714 | } | |
715 | $off++; | |
716 | } | |
717 | # We are truly at the end, so shuffle to the next line. | |
718 | if ($off == $len) { | |
719 | $loff = $len + 1; | |
720 | $line++; | |
721 | $remain--; | |
722 | } | |
723 | ||
724 | my $statement = substr($blk, $soff, $off - $soff + 1); | |
725 | my $condition = substr($blk, $soff, $coff - $soff + 1); | |
726 | ||
727 | #warn "STATEMENT<$statement>\n"; | |
728 | #warn "CONDITION<$condition>\n"; | |
729 | ||
730 | #print "coff<$coff> soff<$off> loff<$loff>\n"; | |
731 | ||
732 | return ($statement, $condition, | |
733 | $line, $remain + 1, $off - $loff + 1, $level); | |
734 | } | |
735 | ||
736 | sub statement_lines { | |
737 | my ($stmt) = @_; | |
738 | ||
739 | # Strip the diff line prefixes and rip blank lines at start and end. | |
740 | $stmt =~ s/(^|\n)./$1/g; | |
741 | $stmt =~ s/^\s*//; | |
742 | $stmt =~ s/\s*$//; | |
743 | ||
744 | my @stmt_lines = ($stmt =~ /\n/g); | |
745 | ||
746 | return $#stmt_lines + 2; | |
747 | } | |
748 | ||
749 | sub statement_rawlines { | |
750 | my ($stmt) = @_; | |
751 | ||
752 | my @stmt_lines = ($stmt =~ /\n/g); | |
753 | ||
754 | return $#stmt_lines + 2; | |
755 | } | |
756 | ||
757 | sub statement_block_size { | |
758 | my ($stmt) = @_; | |
759 | ||
760 | $stmt =~ s/(^|\n)./$1/g; | |
04f2562f | 761 | $stmt =~ s/^\s*\{//; |
1ec3f6f9 BS |
762 | $stmt =~ s/}\s*$//; |
763 | $stmt =~ s/^\s*//; | |
764 | $stmt =~ s/\s*$//; | |
765 | ||
766 | my @stmt_lines = ($stmt =~ /\n/g); | |
767 | my @stmt_statements = ($stmt =~ /;/g); | |
768 | ||
769 | my $stmt_lines = $#stmt_lines + 2; | |
770 | my $stmt_statements = $#stmt_statements + 1; | |
771 | ||
772 | if ($stmt_lines > $stmt_statements) { | |
773 | return $stmt_lines; | |
774 | } else { | |
775 | return $stmt_statements; | |
776 | } | |
777 | } | |
778 | ||
779 | sub ctx_statement_full { | |
780 | my ($linenr, $remain, $off) = @_; | |
781 | my ($statement, $condition, $level); | |
782 | ||
783 | my (@chunks); | |
784 | ||
785 | # Grab the first conditional/block pair. | |
786 | ($statement, $condition, $linenr, $remain, $off, $level) = | |
787 | ctx_statement_block($linenr, $remain, $off); | |
788 | #print "F: c<$condition> s<$statement> remain<$remain>\n"; | |
789 | push(@chunks, [ $condition, $statement ]); | |
790 | if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { | |
791 | return ($level, $linenr, @chunks); | |
792 | } | |
793 | ||
794 | # Pull in the following conditional/block pairs and see if they | |
795 | # could continue the statement. | |
796 | for (;;) { | |
797 | ($statement, $condition, $linenr, $remain, $off, $level) = | |
798 | ctx_statement_block($linenr, $remain, $off); | |
799 | #print "C: c<$condition> s<$statement> remain<$remain>\n"; | |
800 | last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); | |
801 | #print "C: push\n"; | |
802 | push(@chunks, [ $condition, $statement ]); | |
803 | } | |
804 | ||
805 | return ($level, $linenr, @chunks); | |
806 | } | |
807 | ||
808 | sub ctx_block_get { | |
809 | my ($linenr, $remain, $outer, $open, $close, $off) = @_; | |
810 | my $line; | |
811 | my $start = $linenr - 1; | |
812 | my $blk = ''; | |
813 | my @o; | |
814 | my @c; | |
815 | my @res = (); | |
816 | ||
817 | my $level = 0; | |
818 | my @stack = ($level); | |
819 | for ($line = $start; $remain > 0; $line++) { | |
820 | next if ($rawlines[$line] =~ /^-/); | |
821 | $remain--; | |
822 | ||
823 | $blk .= $rawlines[$line]; | |
824 | ||
825 | # Handle nested #if/#else. | |
826 | if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { | |
827 | push(@stack, $level); | |
828 | } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { | |
829 | $level = $stack[$#stack - 1]; | |
830 | } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { | |
831 | $level = pop(@stack); | |
832 | } | |
833 | ||
834 | foreach my $c (split(//, $lines[$line])) { | |
835 | ##print "C<$c>L<$level><$open$close>O<$off>\n"; | |
836 | if ($off > 0) { | |
837 | $off--; | |
838 | next; | |
839 | } | |
840 | ||
841 | if ($c eq $close && $level > 0) { | |
842 | $level--; | |
843 | last if ($level == 0); | |
844 | } elsif ($c eq $open) { | |
845 | $level++; | |
846 | } | |
847 | } | |
848 | ||
849 | if (!$outer || $level <= 1) { | |
850 | push(@res, $rawlines[$line]); | |
851 | } | |
852 | ||
853 | last if ($level == 0); | |
854 | } | |
855 | ||
856 | return ($level, @res); | |
857 | } | |
858 | sub ctx_block_outer { | |
859 | my ($linenr, $remain) = @_; | |
860 | ||
861 | my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); | |
862 | return @r; | |
863 | } | |
864 | sub ctx_block { | |
865 | my ($linenr, $remain) = @_; | |
866 | ||
867 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); | |
868 | return @r; | |
869 | } | |
870 | sub ctx_statement { | |
871 | my ($linenr, $remain, $off) = @_; | |
872 | ||
873 | my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); | |
874 | return @r; | |
875 | } | |
876 | sub ctx_block_level { | |
877 | my ($linenr, $remain) = @_; | |
878 | ||
879 | return ctx_block_get($linenr, $remain, 0, '{', '}', 0); | |
880 | } | |
881 | sub ctx_statement_level { | |
882 | my ($linenr, $remain, $off) = @_; | |
883 | ||
884 | return ctx_block_get($linenr, $remain, 0, '(', ')', $off); | |
885 | } | |
886 | ||
887 | sub ctx_locate_comment { | |
888 | my ($first_line, $end_line) = @_; | |
889 | ||
890 | # Catch a comment on the end of the line itself. | |
891 | my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); | |
892 | return $current_comment if (defined $current_comment); | |
893 | ||
894 | # Look through the context and try and figure out if there is a | |
895 | # comment. | |
896 | my $in_comment = 0; | |
897 | $current_comment = ''; | |
898 | for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { | |
899 | my $line = $rawlines[$linenr - 1]; | |
900 | #warn " $line\n"; | |
901 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { | |
902 | $in_comment = 1; | |
903 | } | |
904 | if ($line =~ m@/\*@) { | |
905 | $in_comment = 1; | |
906 | } | |
907 | if (!$in_comment && $current_comment ne '') { | |
908 | $current_comment = ''; | |
909 | } | |
910 | $current_comment .= $line . "\n" if ($in_comment); | |
911 | if ($line =~ m@\*/@) { | |
912 | $in_comment = 0; | |
913 | } | |
914 | } | |
915 | ||
916 | chomp($current_comment); | |
917 | return($current_comment); | |
918 | } | |
919 | sub ctx_has_comment { | |
920 | my ($first_line, $end_line) = @_; | |
921 | my $cmt = ctx_locate_comment($first_line, $end_line); | |
922 | ||
923 | ##print "LINE: $rawlines[$end_line - 1 ]\n"; | |
924 | ##print "CMMT: $cmt\n"; | |
925 | ||
926 | return ($cmt ne ''); | |
927 | } | |
928 | ||
929 | sub raw_line { | |
930 | my ($linenr, $cnt) = @_; | |
931 | ||
932 | my $offset = $linenr - 1; | |
933 | $cnt++; | |
934 | ||
935 | my $line; | |
936 | while ($cnt) { | |
937 | $line = $rawlines[$offset++]; | |
938 | next if (defined($line) && $line =~ /^-/); | |
939 | $cnt--; | |
940 | } | |
941 | ||
942 | return $line; | |
943 | } | |
944 | ||
945 | sub cat_vet { | |
946 | my ($vet) = @_; | |
947 | my ($res, $coded); | |
948 | ||
949 | $res = ''; | |
950 | while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { | |
951 | $res .= $1; | |
952 | if ($2 ne '') { | |
953 | $coded = sprintf("^%c", unpack('C', $2) + 64); | |
954 | $res .= $coded; | |
955 | } | |
956 | } | |
957 | $res =~ s/$/\$/; | |
958 | ||
959 | return $res; | |
960 | } | |
961 | ||
962 | my $av_preprocessor = 0; | |
963 | my $av_pending; | |
964 | my @av_paren_type; | |
965 | my $av_pend_colon; | |
966 | ||
967 | sub annotate_reset { | |
968 | $av_preprocessor = 0; | |
969 | $av_pending = '_'; | |
970 | @av_paren_type = ('E'); | |
971 | $av_pend_colon = 'O'; | |
972 | } | |
973 | ||
974 | sub annotate_values { | |
975 | my ($stream, $type) = @_; | |
976 | ||
977 | my $res; | |
978 | my $var = '_' x length($stream); | |
979 | my $cur = $stream; | |
980 | ||
981 | print "$stream\n" if ($dbg_values > 1); | |
982 | ||
983 | while (length($cur)) { | |
984 | @av_paren_type = ('E') if ($#av_paren_type < 0); | |
985 | print " <" . join('', @av_paren_type) . | |
986 | "> <$type> <$av_pending>" if ($dbg_values > 1); | |
987 | if ($cur =~ /^(\s+)/o) { | |
988 | print "WS($1)\n" if ($dbg_values > 1); | |
989 | if ($1 =~ /\n/ && $av_preprocessor) { | |
990 | $type = pop(@av_paren_type); | |
991 | $av_preprocessor = 0; | |
992 | } | |
993 | ||
61669f9a | 994 | } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { |
1ec3f6f9 BS |
995 | print "CAST($1)\n" if ($dbg_values > 1); |
996 | push(@av_paren_type, $type); | |
997 | $type = 'C'; | |
998 | ||
999 | } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { | |
1000 | print "DECLARE($1)\n" if ($dbg_values > 1); | |
1001 | $type = 'T'; | |
1002 | ||
1003 | } elsif ($cur =~ /^($Modifier)\s*/) { | |
1004 | print "MODIFIER($1)\n" if ($dbg_values > 1); | |
1005 | $type = 'T'; | |
1006 | ||
1007 | } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { | |
1008 | print "DEFINE($1,$2)\n" if ($dbg_values > 1); | |
1009 | $av_preprocessor = 1; | |
1010 | push(@av_paren_type, $type); | |
1011 | if ($2 ne '') { | |
1012 | $av_pending = 'N'; | |
1013 | } | |
1014 | $type = 'E'; | |
1015 | ||
1016 | } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { | |
1017 | print "UNDEF($1)\n" if ($dbg_values > 1); | |
1018 | $av_preprocessor = 1; | |
1019 | push(@av_paren_type, $type); | |
1020 | ||
1021 | } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { | |
1022 | print "PRE_START($1)\n" if ($dbg_values > 1); | |
1023 | $av_preprocessor = 1; | |
1024 | ||
1025 | push(@av_paren_type, $type); | |
1026 | push(@av_paren_type, $type); | |
1027 | $type = 'E'; | |
1028 | ||
1029 | } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { | |
1030 | print "PRE_RESTART($1)\n" if ($dbg_values > 1); | |
1031 | $av_preprocessor = 1; | |
1032 | ||
1033 | push(@av_paren_type, $av_paren_type[$#av_paren_type]); | |
1034 | ||
1035 | $type = 'E'; | |
1036 | ||
1037 | } elsif ($cur =~ /^(\#\s*(?:endif))/o) { | |
1038 | print "PRE_END($1)\n" if ($dbg_values > 1); | |
1039 | ||
1040 | $av_preprocessor = 1; | |
1041 | ||
1042 | # Assume all arms of the conditional end as this | |
1043 | # one does, and continue as if the #endif was not here. | |
1044 | pop(@av_paren_type); | |
1045 | push(@av_paren_type, $type); | |
1046 | $type = 'E'; | |
1047 | ||
1048 | } elsif ($cur =~ /^(\\\n)/o) { | |
1049 | print "PRECONT($1)\n" if ($dbg_values > 1); | |
1050 | ||
1051 | } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { | |
1052 | print "ATTR($1)\n" if ($dbg_values > 1); | |
1053 | $av_pending = $type; | |
1054 | $type = 'N'; | |
1055 | ||
1056 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { | |
1057 | print "SIZEOF($1)\n" if ($dbg_values > 1); | |
1058 | if (defined $2) { | |
1059 | $av_pending = 'V'; | |
1060 | } | |
1061 | $type = 'N'; | |
1062 | ||
1063 | } elsif ($cur =~ /^(if|while|for)\b/o) { | |
1064 | print "COND($1)\n" if ($dbg_values > 1); | |
1065 | $av_pending = 'E'; | |
1066 | $type = 'N'; | |
1067 | ||
1068 | } elsif ($cur =~/^(case)/o) { | |
1069 | print "CASE($1)\n" if ($dbg_values > 1); | |
1070 | $av_pend_colon = 'C'; | |
1071 | $type = 'N'; | |
1072 | ||
1073 | } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { | |
1074 | print "KEYWORD($1)\n" if ($dbg_values > 1); | |
1075 | $type = 'N'; | |
1076 | ||
1077 | } elsif ($cur =~ /^(\()/o) { | |
1078 | print "PAREN('$1')\n" if ($dbg_values > 1); | |
1079 | push(@av_paren_type, $av_pending); | |
1080 | $av_pending = '_'; | |
1081 | $type = 'N'; | |
1082 | ||
1083 | } elsif ($cur =~ /^(\))/o) { | |
1084 | my $new_type = pop(@av_paren_type); | |
1085 | if ($new_type ne '_') { | |
1086 | $type = $new_type; | |
1087 | print "PAREN('$1') -> $type\n" | |
1088 | if ($dbg_values > 1); | |
1089 | } else { | |
1090 | print "PAREN('$1')\n" if ($dbg_values > 1); | |
1091 | } | |
1092 | ||
1093 | } elsif ($cur =~ /^($Ident)\s*\(/o) { | |
1094 | print "FUNC($1)\n" if ($dbg_values > 1); | |
1095 | $type = 'V'; | |
1096 | $av_pending = 'V'; | |
1097 | ||
1098 | } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { | |
1099 | if (defined $2 && $type eq 'C' || $type eq 'T') { | |
1100 | $av_pend_colon = 'B'; | |
1101 | } elsif ($type eq 'E') { | |
1102 | $av_pend_colon = 'L'; | |
1103 | } | |
1104 | print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); | |
1105 | $type = 'V'; | |
1106 | ||
1107 | } elsif ($cur =~ /^($Ident|$Constant)/o) { | |
1108 | print "IDENT($1)\n" if ($dbg_values > 1); | |
1109 | $type = 'V'; | |
1110 | ||
1111 | } elsif ($cur =~ /^($Assignment)/o) { | |
1112 | print "ASSIGN($1)\n" if ($dbg_values > 1); | |
1113 | $type = 'N'; | |
1114 | ||
1115 | } elsif ($cur =~/^(;|{|})/) { | |
1116 | print "END($1)\n" if ($dbg_values > 1); | |
1117 | $type = 'E'; | |
1118 | $av_pend_colon = 'O'; | |
1119 | ||
1120 | } elsif ($cur =~/^(,)/) { | |
1121 | print "COMMA($1)\n" if ($dbg_values > 1); | |
1122 | $type = 'C'; | |
1123 | ||
1124 | } elsif ($cur =~ /^(\?)/o) { | |
1125 | print "QUESTION($1)\n" if ($dbg_values > 1); | |
1126 | $type = 'N'; | |
1127 | ||
1128 | } elsif ($cur =~ /^(:)/o) { | |
1129 | print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); | |
1130 | ||
1131 | substr($var, length($res), 1, $av_pend_colon); | |
1132 | if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { | |
1133 | $type = 'E'; | |
1134 | } else { | |
1135 | $type = 'N'; | |
1136 | } | |
1137 | $av_pend_colon = 'O'; | |
1138 | ||
1139 | } elsif ($cur =~ /^(\[)/o) { | |
1140 | print "CLOSE($1)\n" if ($dbg_values > 1); | |
1141 | $type = 'N'; | |
1142 | ||
1143 | } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { | |
1144 | my $variant; | |
1145 | ||
1146 | print "OPV($1)\n" if ($dbg_values > 1); | |
1147 | if ($type eq 'V') { | |
1148 | $variant = 'B'; | |
1149 | } else { | |
1150 | $variant = 'U'; | |
1151 | } | |
1152 | ||
1153 | substr($var, length($res), 1, $variant); | |
1154 | $type = 'N'; | |
1155 | ||
1156 | } elsif ($cur =~ /^($Operators)/o) { | |
1157 | print "OP($1)\n" if ($dbg_values > 1); | |
1158 | if ($1 ne '++' && $1 ne '--') { | |
1159 | $type = 'N'; | |
1160 | } | |
1161 | ||
1162 | } elsif ($cur =~ /(^.)/o) { | |
1163 | print "C($1)\n" if ($dbg_values > 1); | |
1164 | } | |
1165 | if (defined $1) { | |
1166 | $cur = substr($cur, length($1)); | |
1167 | $res .= $type x length($1); | |
1168 | } | |
1169 | } | |
1170 | ||
1171 | return ($res, $var); | |
1172 | } | |
1173 | ||
1174 | sub possible { | |
1175 | my ($possible, $line) = @_; | |
1176 | my $notPermitted = qr{(?: | |
1177 | ^(?: | |
1178 | $Modifier| | |
1179 | $Storage| | |
1180 | $Type| | |
1181 | DEFINE_\S+ | |
1182 | )$| | |
1183 | ^(?: | |
1184 | goto| | |
1185 | return| | |
1186 | case| | |
1187 | else| | |
1188 | asm|__asm__| | |
e20122ff | 1189 | do |
1ec3f6f9 | 1190 | )(?:\s|$)| |
e20122ff PB |
1191 | ^(?:typedef|struct|enum)\b| |
1192 | ^\# | |
1ec3f6f9 BS |
1193 | )}x; |
1194 | warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); | |
1195 | if ($possible !~ $notPermitted) { | |
1196 | # Check for modifiers. | |
1197 | $possible =~ s/\s*$Storage\s*//g; | |
1198 | $possible =~ s/\s*$Sparse\s*//g; | |
1199 | if ($possible =~ /^\s*$/) { | |
1200 | ||
1201 | } elsif ($possible =~ /\s/) { | |
e20122ff | 1202 | $possible =~ s/\s*(?:$Type|\#\#)\s*//g; |
1ec3f6f9 BS |
1203 | for my $modifier (split(' ', $possible)) { |
1204 | if ($modifier !~ $notPermitted) { | |
1205 | warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); | |
1206 | push(@modifierList, $modifier); | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | } else { | |
1211 | warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); | |
1212 | push(@typeList, $possible); | |
1213 | } | |
1214 | build_types(); | |
1215 | } else { | |
1216 | warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | my $prefix = ''; | |
1221 | ||
1222 | sub report { | |
1db4269f PB |
1223 | my ($level, $msg) = @_; |
1224 | if (defined $tst_only && $msg !~ /\Q$tst_only\E/) { | |
1ec3f6f9 BS |
1225 | return 0; |
1226 | } | |
1ec3f6f9 | 1227 | |
1db4269f PB |
1228 | my $output = ''; |
1229 | $output .= BOLD if $color; | |
1230 | $output .= $prefix; | |
1231 | $output .= RED if $color && $level eq 'ERROR'; | |
1232 | $output .= MAGENTA if $color && $level eq 'WARNING'; | |
1233 | $output .= $level . ':'; | |
1234 | $output .= RESET if $color; | |
1235 | $output .= ' ' . $msg . "\n"; | |
1236 | ||
1237 | $output = (split('\n', $output))[0] . "\n" if ($terse); | |
1ec3f6f9 | 1238 | |
1db4269f | 1239 | push(our @report, $output); |
1ec3f6f9 BS |
1240 | |
1241 | return 1; | |
1242 | } | |
1243 | sub report_dump { | |
1244 | our @report; | |
1245 | } | |
1246 | sub ERROR { | |
1db4269f | 1247 | if (report("ERROR", $_[0])) { |
1ec3f6f9 BS |
1248 | our $clean = 0; |
1249 | our $cnt_error++; | |
1250 | } | |
1251 | } | |
1252 | sub WARN { | |
1db4269f | 1253 | if (report("WARNING", $_[0])) { |
1ec3f6f9 BS |
1254 | our $clean = 0; |
1255 | our $cnt_warn++; | |
1256 | } | |
1257 | } | |
1ec3f6f9 | 1258 | |
1ec3f6f9 BS |
1259 | sub process { |
1260 | my $filename = shift; | |
1261 | ||
1262 | my $linenr=0; | |
1263 | my $prevline=""; | |
1264 | my $prevrawline=""; | |
1265 | my $stashline=""; | |
1266 | my $stashrawline=""; | |
1267 | ||
1268 | my $length; | |
1269 | my $indent; | |
1270 | my $previndent=0; | |
1271 | my $stashindent=0; | |
1272 | ||
1273 | our $clean = 1; | |
1274 | my $signoff = 0; | |
1275 | my $is_patch = 0; | |
1276 | ||
5fc7e404 | 1277 | my $in_header_lines = $file ? 0 : 1; |
b37c4aac | 1278 | my $in_commit_log = 0; #Scanning lines before patch |
4be6131e | 1279 | my $reported_maintainer_file = 0; |
bf139a06 PS |
1280 | my $non_utf8_charset = 0; |
1281 | ||
1ec3f6f9 BS |
1282 | our @report = (); |
1283 | our $cnt_lines = 0; | |
1284 | our $cnt_error = 0; | |
1285 | our $cnt_warn = 0; | |
1286 | our $cnt_chk = 0; | |
1287 | ||
1288 | # Trace the real file/line as we go. | |
1289 | my $realfile = ''; | |
1290 | my $realline = 0; | |
1291 | my $realcnt = 0; | |
1292 | my $here = ''; | |
1293 | my $in_comment = 0; | |
1294 | my $comment_edge = 0; | |
1295 | my $first_line = 0; | |
1296 | my $p1_prefix = ''; | |
1297 | ||
1298 | my $prev_values = 'E'; | |
1299 | ||
1300 | # suppression flags | |
1301 | my %suppress_ifbraces; | |
1302 | my %suppress_whiletrailers; | |
1303 | my %suppress_export; | |
1304 | ||
1305 | # Pre-scan the patch sanitizing the lines. | |
1ec3f6f9 BS |
1306 | |
1307 | sanitise_line_reset(); | |
1308 | my $line; | |
1309 | foreach my $rawline (@rawlines) { | |
1310 | $linenr++; | |
1311 | $line = $rawline; | |
1312 | ||
1ec3f6f9 BS |
1313 | if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { |
1314 | $realline=$1-1; | |
1315 | if (defined $2) { | |
1316 | $realcnt=$3+1; | |
1317 | } else { | |
1318 | $realcnt=1+1; | |
1319 | } | |
1320 | $in_comment = 0; | |
1321 | ||
1322 | # Guestimate if this is a continuing comment. Run | |
1323 | # the context looking for a comment "edge". If this | |
1324 | # edge is a close comment then we must be in a comment | |
1325 | # at context start. | |
1326 | my $edge; | |
1327 | my $cnt = $realcnt; | |
1328 | for (my $ln = $linenr + 1; $cnt > 0; $ln++) { | |
1329 | next if (defined $rawlines[$ln - 1] && | |
1330 | $rawlines[$ln - 1] =~ /^-/); | |
1331 | $cnt--; | |
1332 | #print "RAW<$rawlines[$ln - 1]>\n"; | |
1333 | last if (!defined $rawlines[$ln - 1]); | |
1334 | if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && | |
1335 | $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { | |
1336 | ($edge) = $1; | |
1337 | last; | |
1338 | } | |
1339 | } | |
1340 | if (defined $edge && $edge eq '*/') { | |
1341 | $in_comment = 1; | |
1342 | } | |
1343 | ||
1344 | # Guestimate if this is a continuing comment. If this | |
1345 | # is the start of a diff block and this line starts | |
1346 | # ' *' then it is very likely a comment. | |
1347 | if (!defined $edge && | |
1348 | $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) | |
1349 | { | |
1350 | $in_comment = 1; | |
1351 | } | |
1352 | ||
1353 | ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; | |
1354 | sanitise_line_reset($in_comment); | |
1355 | ||
1356 | } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { | |
1357 | # Standardise the strings and chars within the input to | |
1358 | # simplify matching -- only bother with positive lines. | |
1359 | $line = sanitise_line($rawline); | |
1360 | } | |
1361 | push(@lines, $line); | |
1362 | ||
1363 | if ($realcnt > 1) { | |
1364 | $realcnt-- if ($line =~ /^(?:\+| |$)/); | |
1365 | } else { | |
1366 | $realcnt = 0; | |
1367 | } | |
1368 | ||
1369 | #print "==>$rawline\n"; | |
1370 | #print "-->$line\n"; | |
1ec3f6f9 BS |
1371 | } |
1372 | ||
1373 | $prefix = ''; | |
1374 | ||
1375 | $realcnt = 0; | |
1376 | $linenr = 0; | |
1377 | foreach my $line (@lines) { | |
1378 | $linenr++; | |
1379 | ||
1380 | my $rawline = $rawlines[$linenr - 1]; | |
1381 | ||
1382 | #extract the line range in the file after the patch is applied | |
1383 | if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { | |
1384 | $is_patch = 1; | |
1385 | $first_line = $linenr + 1; | |
1386 | $realline=$1-1; | |
1387 | if (defined $2) { | |
1388 | $realcnt=$3+1; | |
1389 | } else { | |
1390 | $realcnt=1+1; | |
1391 | } | |
1392 | annotate_reset(); | |
1393 | $prev_values = 'E'; | |
1394 | ||
1395 | %suppress_ifbraces = (); | |
1396 | %suppress_whiletrailers = (); | |
1397 | %suppress_export = (); | |
1398 | next; | |
1399 | ||
1400 | # track the line number as we move through the hunk, note that | |
1401 | # new versions of GNU diff omit the leading space on completely | |
1402 | # blank context lines so we need to count that too. | |
1403 | } elsif ($line =~ /^( |\+|$)/) { | |
1404 | $realline++; | |
1405 | $realcnt-- if ($realcnt != 0); | |
1406 | ||
1407 | # Measure the line length and indent. | |
1408 | ($length, $indent) = line_stats($rawline); | |
1409 | ||
1410 | # Track the previous line. | |
1411 | ($prevline, $stashline) = ($stashline, $line); | |
1412 | ($previndent, $stashindent) = ($stashindent, $indent); | |
1413 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); | |
1414 | ||
1415 | #warn "line<$line>\n"; | |
1416 | ||
1417 | } elsif ($realcnt == 1) { | |
1418 | $realcnt--; | |
1419 | } | |
1420 | ||
1421 | my $hunk_line = ($realcnt != 0); | |
1422 | ||
1423 | #make up the handle for any error we report on this line | |
1424 | $prefix = "$filename:$realline: " if ($emacs && $file); | |
1425 | $prefix = "$filename:$linenr: " if ($emacs && !$file); | |
1426 | ||
1427 | $here = "#$linenr: " if (!$file); | |
1428 | $here = "#$realline: " if ($file); | |
1429 | ||
1430 | # extract the filename as it passes | |
1431 | if ($line =~ /^diff --git.*?(\S+)$/) { | |
1432 | $realfile = $1; | |
1a5c63ce | 1433 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
1ec3f6f9 BS |
1434 | } elsif ($line =~ /^\+\+\+\s+(\S+)/) { |
1435 | $realfile = $1; | |
1a5c63ce | 1436 | $realfile =~ s@^([^/]*)/@@ if (!$file); |
1ec3f6f9 BS |
1437 | |
1438 | $p1_prefix = $1; | |
1439 | if (!$file && $tree && $p1_prefix ne '' && | |
1440 | -e "$root/$p1_prefix") { | |
1441 | WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); | |
1442 | } | |
1443 | ||
1ec3f6f9 BS |
1444 | next; |
1445 | } | |
1446 | ||
1447 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); | |
1448 | ||
1449 | my $hereline = "$here\n$rawline\n"; | |
1450 | my $herecurr = "$here\n$rawline\n"; | |
1451 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; | |
1452 | ||
1453 | $cnt_lines++ if ($realcnt != 0); | |
1454 | ||
1455 | # Check for incorrect file permissions | |
1456 | if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { | |
1457 | my $permhere = $here . "FILE: $realfile\n"; | |
71c47b01 | 1458 | if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) { |
1ec3f6f9 BS |
1459 | ERROR("do not set execute permissions for source files\n" . $permhere); |
1460 | } | |
1461 | } | |
1462 | ||
f8dccbb6 SH |
1463 | # Accept git diff extended headers as valid patches |
1464 | if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) { | |
1465 | $is_patch = 1; | |
1466 | } | |
1467 | ||
f5177798 DB |
1468 | if ($line =~ /^Author: .*via Qemu-devel.*<qemu-devel\@nongnu.org>/) { |
1469 | ERROR("Author email address is mangled by the mailing list\n" . $herecurr); | |
1470 | } | |
1471 | ||
1ec3f6f9 BS |
1472 | #check the patch for a signoff: |
1473 | if ($line =~ /^\s*signed-off-by:/i) { | |
1474 | # This is a signoff, if ugly, so do not double report. | |
1475 | $signoff++; | |
b37c4aac JP |
1476 | $in_commit_log = 0; |
1477 | ||
1ec3f6f9 | 1478 | if (!($line =~ /^\s*Signed-off-by:/)) { |
c2df8783 | 1479 | ERROR("The correct form is \"Signed-off-by\"\n" . |
1ec3f6f9 BS |
1480 | $herecurr); |
1481 | } | |
1482 | if ($line =~ /^\s*signed-off-by:\S/i) { | |
c2df8783 | 1483 | ERROR("space required after Signed-off-by:\n" . |
1ec3f6f9 BS |
1484 | $herecurr); |
1485 | } | |
1486 | } | |
1487 | ||
1a6fad0c JP |
1488 | # Check if MAINTAINERS is being updated. If so, there's probably no need to |
1489 | # emit the "does MAINTAINERS need updating?" message on file add/move/delete | |
1490 | if ($line =~ /^\s*MAINTAINERS\s*\|/) { | |
1491 | $reported_maintainer_file = 1; | |
1492 | } | |
1493 | ||
4be6131e JP |
1494 | # Check for added, moved or deleted files |
1495 | if (!$reported_maintainer_file && !$in_commit_log && | |
1496 | ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || | |
1497 | $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || | |
1498 | ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && | |
1499 | (defined($1) || defined($2))))) { | |
1500 | $reported_maintainer_file = 1; | |
1501 | WARN("added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); | |
1502 | } | |
1503 | ||
1ec3f6f9 BS |
1504 | # Check for wrappage within a valid hunk of the file |
1505 | if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { | |
1506 | ERROR("patch seems to be corrupt (line wrapped?)\n" . | |
1507 | $herecurr) if (!$emitted_corrupt++); | |
1508 | } | |
1509 | ||
1ec3f6f9 BS |
1510 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
1511 | if (($realfile =~ /^$/ || $line =~ /^\+/) && | |
1512 | $rawline !~ m/^$UTF8*$/) { | |
1513 | my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); | |
1514 | ||
1515 | my $blank = copy_spacing($rawline); | |
1516 | my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; | |
1517 | my $hereptr = "$hereline$ptr\n"; | |
1518 | ||
1519 | ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); | |
1520 | } | |
1521 | ||
874acb6f PB |
1522 | if ($rawline =~ m/$UTF8_MOJIBAKE/) { |
1523 | ERROR("Doubly-encoded UTF-8\n" . $herecurr); | |
1524 | } | |
b37c4aac JP |
1525 | # Check if it's the start of a commit log |
1526 | # (not a header line and we haven't seen the patch filename) | |
1527 | if ($in_header_lines && $realfile =~ /^$/ && | |
5fc7e404 JP |
1528 | !($rawline =~ /^\s+\S/ || |
1529 | $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) { | |
b37c4aac JP |
1530 | $in_header_lines = 0; |
1531 | $in_commit_log = 1; | |
1532 | } | |
1533 | ||
bf139a06 PS |
1534 | # Check if there is UTF-8 in a commit log when a mail header has explicitly |
1535 | # declined it, i.e defined some charset where it is missing. | |
1536 | if ($in_header_lines && | |
1537 | $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && | |
1538 | $1 !~ /utf-8/i) { | |
1539 | $non_utf8_charset = 1; | |
1540 | } | |
1541 | ||
1542 | if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && | |
b37c4aac JP |
1543 | $rawline =~ /$NON_ASCII_UTF8/) { |
1544 | WARN("8-bit UTF-8 used in possible commit log\n" . $herecurr); | |
1545 | } | |
1546 | ||
1ec3f6f9 BS |
1547 | # ignore non-hunk lines and lines being removed |
1548 | next if (!$hunk_line || $line =~ /^-/); | |
1549 | ||
93bf13c6 RK |
1550 | # ignore files that are being periodically imported from Linux |
1551 | next if ($realfile =~ /^(linux-headers|include\/standard-headers)\//); | |
1552 | ||
1ec3f6f9 BS |
1553 | #trailing whitespace |
1554 | if ($line =~ /^\+.*\015/) { | |
1555 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | |
1556 | ERROR("DOS line endings\n" . $herevet); | |
1557 | ||
0cebabd5 LV |
1558 | } elsif ($realfile =~ /^docs\/.+\.txt/ || |
1559 | $realfile =~ /^docs\/.+\.md/) { | |
1560 | if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) { | |
1561 | # TODO: properly check we're in a code block | |
1562 | # (surrounding text is 4-column aligned) | |
1563 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | |
1564 | ERROR("code blocks in documentation should have " . | |
1565 | "empty lines with exactly 4 columns of " . | |
1566 | "whitespace\n" . $herevet); | |
1567 | } | |
1ec3f6f9 BS |
1568 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
1569 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | |
1570 | ERROR("trailing whitespace\n" . $herevet); | |
1571 | $rpt_cleaners = 1; | |
1572 | } | |
1573 | ||
c3e5875a VSO |
1574 | # checks for trace-events files |
1575 | if ($realfile =~ /trace-events$/ && $line =~ /^\+/) { | |
1576 | if ($rawline =~ /%[-+ 0]*#/) { | |
1577 | ERROR("Don't use '#' flag of printf format ('%#') in " . | |
1578 | "trace-events, use '0x' prefix instead\n" . $herecurr); | |
1579 | } else { | |
1580 | my $hex = | |
1581 | qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/; | |
1582 | ||
1583 | # don't consider groups splitted by [.:/ ], like 2A.20:12ab | |
45042732 VSO |
1584 | my $tmpline = $rawline; |
1585 | $tmpline =~ s/($hex[.:\/ ])+$hex//g; | |
c3e5875a VSO |
1586 | |
1587 | if ($tmpline =~ /(?<!0x)$hex/) { | |
1588 | ERROR("Hex numbers must be prefixed with '0x'\n" . | |
1589 | $herecurr); | |
1590 | } | |
1591 | } | |
1592 | } | |
1593 | ||
1ec3f6f9 | 1594 | # check we are in a valid source file if not then ignore this hunk |
777d05ba | 1595 | next if ($realfile !~ /$SrcFile/); |
1ec3f6f9 | 1596 | |
205f31a8 | 1597 | #90 column limit; exempt URLs, if no other words on line |
f1e155bb PB |
1598 | if ($line =~ /^\+/ && |
1599 | !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && | |
205f31a8 | 1600 | !($rawline =~ /^[^[:alnum:]]*https?:\S*$/) && |
1ec3f6f9 BS |
1601 | $length > 80) |
1602 | { | |
8fbe3d1f PB |
1603 | if ($length > 90) { |
1604 | ERROR("line over 90 characters\n" . $herecurr); | |
1605 | } else { | |
1606 | WARN("line over 80 characters\n" . $herecurr); | |
1607 | } | |
1ec3f6f9 BS |
1608 | } |
1609 | ||
1610 | # check for spaces before a quoted newline | |
1611 | if ($rawline =~ /^.*\".*\s\\n/) { | |
c2df8783 | 1612 | ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr); |
1ec3f6f9 BS |
1613 | } |
1614 | ||
1615 | # check for adding lines without a newline. | |
1616 | if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { | |
c2df8783 | 1617 | ERROR("adding a line without newline at end of file\n" . $herecurr); |
1ec3f6f9 BS |
1618 | } |
1619 | ||
93eb8e31 PB |
1620 | # check for RCS/CVS revision markers |
1621 | if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) { | |
c2df8783 | 1622 | ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
93eb8e31 PB |
1623 | } |
1624 | ||
906fb135 PB |
1625 | # tabs are only allowed in assembly source code, and in |
1626 | # some scripts we imported from other projects. | |
1627 | next if ($realfile =~ /\.(s|S)$/); | |
1628 | next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/); | |
1ec3f6f9 | 1629 | |
ad36ce8b | 1630 | if ($rawline =~ /^\+.*\t/) { |
1ec3f6f9 | 1631 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
b6469683 | 1632 | ERROR("code indent should never use tabs\n" . $herevet); |
1ec3f6f9 BS |
1633 | $rpt_cleaners = 1; |
1634 | } | |
1635 | ||
1ec3f6f9 | 1636 | # check we are in a valid C source file if not then ignore this hunk |
69d5d21f | 1637 | next if ($realfile !~ /\.(h|c|cpp)$/); |
1ec3f6f9 | 1638 | |
8c06fbdf PM |
1639 | # Block comment styles |
1640 | ||
1641 | # Block comments use /* on a line of its own | |
1642 | if ($rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ | |
b94e809d | 1643 | $rawline =~ m@^\+.*/\*\*?+[ \t]*[^ \t]@) { # /* or /** non-blank |
8c06fbdf PM |
1644 | WARN("Block comments use a leading /* on a separate line\n" . $herecurr); |
1645 | } | |
1646 | ||
1647 | # Block comments use * on subsequent lines | |
1648 | if ($prevline =~ /$;[ \t]*$/ && #ends in comment | |
1649 | $prevrawline =~ /^\+.*?\/\*/ && #starting /* | |
1650 | $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ | |
1651 | $rawline =~ /^\+/ && #line is new | |
1652 | $rawline !~ /^\+[ \t]*\*/) { #no leading * | |
1653 | WARN("Block comments use * on subsequent lines\n" . $hereprev); | |
1654 | } | |
1655 | ||
1656 | # Block comments use */ on trailing lines | |
1657 | if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ | |
1658 | $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ | |
1659 | $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ | |
1660 | $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ | |
1661 | WARN("Block comments use a trailing */ on a separate line\n" . $herecurr); | |
1662 | } | |
1663 | ||
1664 | # Block comment * alignment | |
1665 | if ($prevline =~ /$;[ \t]*$/ && #ends in comment | |
1666 | $line =~ /^\+[ \t]*$;/ && #leading comment | |
1667 | $rawline =~ /^\+[ \t]*\*/ && #leading * | |
1668 | (($prevrawline =~ /^\+.*?\/\*/ && #leading /* | |
1669 | $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */ | |
1670 | $prevrawline =~ /^\+[ \t]*\*/)) { #leading * | |
1671 | my $oldindent; | |
1672 | $prevrawline =~ m@^\+([ \t]*/?)\*@; | |
1673 | if (defined($1)) { | |
1674 | $oldindent = expand_tabs($1); | |
1675 | } else { | |
1676 | $prevrawline =~ m@^\+(.*/?)\*@; | |
1677 | $oldindent = expand_tabs($1); | |
1678 | } | |
1679 | $rawline =~ m@^\+([ \t]*)\*@; | |
1680 | my $newindent = $1; | |
1681 | $newindent = expand_tabs($newindent); | |
1682 | if (length($oldindent) ne length($newindent)) { | |
1683 | WARN("Block comments should align the * on each line\n" . $hereprev); | |
1684 | } | |
1685 | } | |
1686 | ||
1ec3f6f9 BS |
1687 | # Check for potential 'bare' types |
1688 | my ($stat, $cond, $line_nr_next, $remain_next, $off_next, | |
1689 | $realline_next); | |
1690 | if ($realcnt && $line =~ /.\s*\S/) { | |
1691 | ($stat, $cond, $line_nr_next, $remain_next, $off_next) = | |
1692 | ctx_statement_block($linenr, $realcnt, 0); | |
1693 | $stat =~ s/\n./\n /g; | |
1694 | $cond =~ s/\n./\n /g; | |
1695 | ||
1696 | # Find the real next line. | |
1697 | $realline_next = $line_nr_next; | |
1698 | if (defined $realline_next && | |
1699 | (!defined $lines[$realline_next - 1] || | |
1700 | substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { | |
1701 | $realline_next++; | |
1702 | } | |
1703 | ||
1704 | my $s = $stat; | |
1705 | $s =~ s/{.*$//s; | |
1706 | ||
1707 | # Ignore goto labels. | |
1708 | if ($s =~ /$Ident:\*$/s) { | |
1709 | ||
1710 | # Ignore functions being called | |
1711 | } elsif ($s =~ /^.\s*$Ident\s*\(/s) { | |
1712 | ||
1713 | } elsif ($s =~ /^.\s*else\b/s) { | |
1714 | ||
1715 | # declarations always start with types | |
1716 | } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) { | |
1717 | my $type = $1; | |
1718 | $type =~ s/\s+/ /g; | |
1719 | possible($type, "A:" . $s); | |
1720 | ||
1721 | # definitions in global scope can only start with types | |
1722 | } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { | |
1723 | possible($1, "B:" . $s); | |
1724 | } | |
1725 | ||
1726 | # any (foo ... *) is a pointer cast, and foo is a type | |
1727 | while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { | |
1728 | possible($1, "C:" . $s); | |
1729 | } | |
1730 | ||
1731 | # Check for any sort of function declaration. | |
1732 | # int foo(something bar, other baz); | |
1733 | # void (*store_gdt)(x86_descr_ptr *); | |
1734 | if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { | |
1735 | my ($name_len) = length($1); | |
1736 | ||
1737 | my $ctx = $s; | |
1738 | substr($ctx, 0, $name_len + 1, ''); | |
1739 | $ctx =~ s/\)[^\)]*$//; | |
1740 | ||
1741 | for my $arg (split(/\s*,\s*/, $ctx)) { | |
1742 | if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { | |
1743 | ||
1744 | possible($1, "D:" . $s); | |
1745 | } | |
1746 | } | |
1747 | } | |
1748 | ||
1749 | } | |
1750 | ||
1751 | # | |
1752 | # Checks which may be anchored in the context. | |
1753 | # | |
1754 | ||
1755 | # Check for switch () and associated case and default | |
1756 | # statements should be at the same indent. | |
1757 | if ($line=~/\bswitch\s*\(.*\)/) { | |
1758 | my $err = ''; | |
1759 | my $sep = ''; | |
1760 | my @ctx = ctx_block_outer($linenr, $realcnt); | |
1761 | shift(@ctx); | |
1762 | for my $ctx (@ctx) { | |
1763 | my ($clen, $cindent) = line_stats($ctx); | |
1764 | if ($ctx =~ /^\+\s*(case\s+|default:)/ && | |
1765 | $indent != $cindent) { | |
1766 | $err .= "$sep$ctx\n"; | |
1767 | $sep = ''; | |
1768 | } else { | |
1769 | $sep = "[...]\n"; | |
1770 | } | |
1771 | } | |
1772 | if ($err ne '') { | |
1773 | ERROR("switch and case should be at the same indent\n$hereline$err"); | |
1774 | } | |
1775 | } | |
1776 | ||
1777 | # if/while/etc brace do not go on next line, unless defining a do while loop, | |
1778 | # or if that brace on the next line is for something else | |
1779 | if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { | |
1780 | my $pre_ctx = "$1$2"; | |
1781 | ||
1782 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); | |
1783 | my $ctx_cnt = $realcnt - $#ctx - 1; | |
1784 | my $ctx = join("\n", @ctx); | |
1785 | ||
1786 | my $ctx_ln = $linenr; | |
1787 | my $ctx_skip = $realcnt; | |
1788 | ||
1789 | while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && | |
1790 | defined $lines[$ctx_ln - 1] && | |
1791 | $lines[$ctx_ln - 1] =~ /^-/)) { | |
1792 | ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; | |
1793 | $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); | |
1794 | $ctx_ln++; | |
1795 | } | |
1796 | ||
1797 | #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; | |
1798 | #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; | |
1799 | ||
a97ceca5 HR |
1800 | # The length of the "previous line" is checked against 80 because it |
1801 | # includes the + at the beginning of the line (if the actual line has | |
1802 | # 79 or 80 characters, it is no longer possible to add a space and an | |
1803 | # opening brace there) | |
1804 | if ($#ctx == 0 && $ctx !~ /{\s*/ && | |
04f2562f | 1805 | defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ && |
a97ceca5 | 1806 | defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) { |
1ec3f6f9 BS |
1807 | ERROR("that open brace { should be on the previous line\n" . |
1808 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); | |
1809 | } | |
1810 | if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && | |
1811 | $ctx =~ /\)\s*\;\s*$/ && | |
1812 | defined $lines[$ctx_ln - 1]) | |
1813 | { | |
1814 | my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); | |
1815 | if ($nindent > $indent) { | |
c2df8783 | 1816 | ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" . |
1ec3f6f9 BS |
1817 | "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); |
1818 | } | |
1819 | } | |
1820 | } | |
1821 | ||
f4bdc13e EB |
1822 | # 'do ... while (0/false)' only makes sense in macros, without trailing ';' |
1823 | if ($line =~ /while\s*\((0|false)\);/) { | |
1824 | ERROR("suspicious ; after while (0)\n" . $herecurr); | |
1825 | } | |
1826 | ||
1ec3f6f9 BS |
1827 | # Check relative indent for conditionals and blocks. |
1828 | if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { | |
1829 | my ($s, $c) = ($stat, $cond); | |
1830 | ||
1831 | substr($s, 0, length($c), ''); | |
1832 | ||
1833 | # Make sure we remove the line prefixes as we have | |
1834 | # none on the first line, and are going to readd them | |
1835 | # where necessary. | |
1836 | $s =~ s/\n./\n/gs; | |
1837 | ||
1838 | # Find out how long the conditional actually is. | |
1839 | my @newlines = ($c =~ /\n/gs); | |
1840 | my $cond_lines = 1 + $#newlines; | |
1841 | ||
1842 | # We want to check the first line inside the block | |
1843 | # starting at the end of the conditional, so remove: | |
1844 | # 1) any blank line termination | |
1845 | # 2) any opening brace { on end of the line | |
1846 | # 3) any do (...) { | |
1847 | my $continuation = 0; | |
1848 | my $check = 0; | |
1849 | $s =~ s/^.*\bdo\b//; | |
04f2562f | 1850 | $s =~ s/^\s*\{//; |
1ec3f6f9 BS |
1851 | if ($s =~ s/^\s*\\//) { |
1852 | $continuation = 1; | |
1853 | } | |
1854 | if ($s =~ s/^\s*?\n//) { | |
1855 | $check = 1; | |
1856 | $cond_lines++; | |
1857 | } | |
1858 | ||
1859 | # Also ignore a loop construct at the end of a | |
1860 | # preprocessor statement. | |
1861 | if (($prevline =~ /^.\s*#\s*define\s/ || | |
1862 | $prevline =~ /\\\s*$/) && $continuation == 0) { | |
1863 | $check = 0; | |
1864 | } | |
1865 | ||
1866 | my $cond_ptr = -1; | |
1867 | $continuation = 0; | |
1868 | while ($cond_ptr != $cond_lines) { | |
1869 | $cond_ptr = $cond_lines; | |
1870 | ||
1871 | # If we see an #else/#elif then the code | |
1872 | # is not linear. | |
1873 | if ($s =~ /^\s*\#\s*(?:else|elif)/) { | |
1874 | $check = 0; | |
1875 | } | |
1876 | ||
1877 | # Ignore: | |
1878 | # 1) blank lines, they should be at 0, | |
1879 | # 2) preprocessor lines, and | |
1880 | # 3) labels. | |
1881 | if ($continuation || | |
1882 | $s =~ /^\s*?\n/ || | |
1883 | $s =~ /^\s*#\s*?/ || | |
1884 | $s =~ /^\s*$Ident\s*:/) { | |
1885 | $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; | |
1886 | if ($s =~ s/^.*?\n//) { | |
1887 | $cond_lines++; | |
1888 | } | |
1889 | } | |
1890 | } | |
1891 | ||
1892 | my (undef, $sindent) = line_stats("+" . $s); | |
1893 | my $stat_real = raw_line($linenr, $cond_lines); | |
1894 | ||
1895 | # Check if either of these lines are modified, else | |
1896 | # this is not this patch's fault. | |
1897 | if (!defined($stat_real) || | |
1898 | $stat !~ /^\+/ && $stat_real !~ /^\+/) { | |
1899 | $check = 0; | |
1900 | } | |
1901 | if (defined($stat_real) && $cond_lines > 1) { | |
1902 | $stat_real = "[...]\n$stat_real"; | |
1903 | } | |
1904 | ||
1905 | #print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n"; | |
1906 | ||
b6469683 | 1907 | if ($check && (($sindent % 4) != 0 || |
1ec3f6f9 | 1908 | ($sindent <= $indent && $s ne ''))) { |
c2df8783 | 1909 | ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); |
1ec3f6f9 BS |
1910 | } |
1911 | } | |
1912 | ||
1913 | # Track the 'values' across context and added lines. | |
1914 | my $opline = $line; $opline =~ s/^./ /; | |
1915 | my ($curr_values, $curr_vars) = | |
1916 | annotate_values($opline . "\n", $prev_values); | |
1917 | $curr_values = $prev_values . $curr_values; | |
1918 | if ($dbg_values) { | |
1919 | my $outline = $opline; $outline =~ s/\t/ /g; | |
1920 | print "$linenr > .$outline\n"; | |
1921 | print "$linenr > $curr_values\n"; | |
1922 | print "$linenr > $curr_vars\n"; | |
1923 | } | |
1924 | $prev_values = substr($curr_values, -1); | |
1925 | ||
1926 | #ignore lines not being added | |
1927 | if ($line=~/^[^\+]/) {next;} | |
1928 | ||
1929 | # TEST: allow direct testing of the type matcher. | |
1930 | if ($dbg_type) { | |
1931 | if ($line =~ /^.\s*$Declare\s*$/) { | |
1932 | ERROR("TEST: is type\n" . $herecurr); | |
1933 | } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { | |
1934 | ERROR("TEST: is not type ($1 is)\n". $herecurr); | |
1935 | } | |
1936 | next; | |
1937 | } | |
1938 | # TEST: allow direct testing of the attribute matcher. | |
1939 | if ($dbg_attr) { | |
1940 | if ($line =~ /^.\s*$Modifier\s*$/) { | |
1941 | ERROR("TEST: is attr\n" . $herecurr); | |
1942 | } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { | |
1943 | ERROR("TEST: is not attr ($1 is)\n". $herecurr); | |
1944 | } | |
1945 | next; | |
1946 | } | |
1947 | ||
1948 | # check for initialisation to aggregates open brace on the next line | |
04f2562f | 1949 | if ($line =~ /^.\s*\{/ && |
1ec3f6f9 BS |
1950 | $prevline =~ /(?:^|[^=])=\s*$/) { |
1951 | ERROR("that open brace { should be on the previous line\n" . $hereprev); | |
1952 | } | |
1953 | ||
1954 | # | |
1955 | # Checks which are anchored on the added line. | |
1956 | # | |
1957 | ||
1958 | # check for malformed paths in #include statements (uses RAW line) | |
1959 | if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { | |
1960 | my $path = $1; | |
1961 | if ($path =~ m{//}) { | |
1962 | ERROR("malformed #include filename\n" . | |
1963 | $herecurr); | |
1964 | } | |
1965 | } | |
1966 | ||
1967 | # no C99 // comments | |
8d061278 PX |
1968 | if ($line =~ m{//} && |
1969 | $rawline !~ m{// SPDX-License-Identifier: }) { | |
1ec3f6f9 BS |
1970 | ERROR("do not use C99 // comments\n" . $herecurr); |
1971 | } | |
1972 | # Remove C99 comments. | |
1973 | $line =~ s@//.*@@; | |
1974 | $opline =~ s@//.*@@; | |
1975 | ||
1ec3f6f9 BS |
1976 | # check for global initialisers. |
1977 | if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) { | |
1978 | ERROR("do not initialise globals to 0 or NULL\n" . | |
1979 | $herecurr); | |
1980 | } | |
1981 | # check for static initialisers. | |
1982 | if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) { | |
1983 | ERROR("do not initialise statics to 0 or NULL\n" . | |
1984 | $herecurr); | |
1985 | } | |
1986 | ||
1ec3f6f9 BS |
1987 | # * goes on variable not on type |
1988 | # (char*[ const]) | |
1989 | if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) { | |
1990 | my ($from, $to) = ($1, $1); | |
1991 | ||
1992 | # Should start with a space. | |
1993 | $to =~ s/^(\S)/ $1/; | |
1994 | # Should not end with a space. | |
1995 | $to =~ s/\s+$//; | |
1996 | # '*'s should not have spaces between. | |
1997 | while ($to =~ s/\*\s+\*/\*\*/) { | |
1998 | } | |
1999 | ||
2000 | #print "from<$from> to<$to>\n"; | |
2001 | if ($from ne $to) { | |
2002 | ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr); | |
2003 | } | |
2004 | } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) { | |
2005 | my ($from, $to, $ident) = ($1, $1, $2); | |
2006 | ||
2007 | # Should start with a space. | |
2008 | $to =~ s/^(\S)/ $1/; | |
2009 | # Should not end with a space. | |
2010 | $to =~ s/\s+$//; | |
2011 | # '*'s should not have spaces between. | |
2012 | while ($to =~ s/\*\s+\*/\*\*/) { | |
2013 | } | |
2014 | # Modifiers should have spaces. | |
2015 | $to =~ s/(\b$Modifier$)/$1 /; | |
2016 | ||
2017 | #print "from<$from> to<$to> ident<$ident>\n"; | |
2018 | if ($from ne $to && $ident !~ /^$Modifier$/) { | |
2019 | ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr); | |
2020 | } | |
2021 | } | |
2022 | ||
1ec3f6f9 BS |
2023 | # function brace can't be on same line, except for #defines of do while, |
2024 | # or if closed on same line | |
04f2562f FZ |
2025 | if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and |
2026 | !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) { | |
1ec3f6f9 BS |
2027 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); |
2028 | } | |
2029 | ||
2030 | # open braces for enum, union and struct go on the same line. | |
04f2562f | 2031 | if ($line =~ /^.\s*\{/ && |
1ec3f6f9 BS |
2032 | $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { |
2033 | ERROR("open brace '{' following $1 go on the same line\n" . $hereprev); | |
2034 | } | |
2035 | ||
2036 | # missing space after union, struct or enum definition | |
2037 | if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) { | |
71c47b01 | 2038 | ERROR("missing space after $1 definition\n" . $herecurr); |
1ec3f6f9 BS |
2039 | } |
2040 | ||
2041 | # check for spacing round square brackets; allowed: | |
2042 | # 1. with a type on the left -- int [] a; | |
2043 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, | |
2044 | # 3. inside a curly brace -- = { [0...10] = 5 } | |
409db6eb | 2045 | # 4. after a comma -- [1] = 5, [2] = 6 |
8800cf0a | 2046 | # 5. in a macro definition -- #define abc(x) [x] = y |
1ec3f6f9 BS |
2047 | while ($line =~ /(.*?\s)\[/g) { |
2048 | my ($where, $prefix) = ($-[1], $1); | |
2049 | if ($prefix !~ /$Type\s+$/ && | |
2050 | ($where != 0 || $prefix !~ /^.\s+$/) && | |
8800cf0a | 2051 | $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ && |
66e9d20e | 2052 | $prefix !~ /[,{:]\s+$/) { |
1ec3f6f9 BS |
2053 | ERROR("space prohibited before open square bracket '['\n" . $herecurr); |
2054 | } | |
2055 | } | |
2056 | ||
2057 | # check for spaces between functions and their parentheses. | |
2058 | while ($line =~ /($Ident)\s+\(/g) { | |
2059 | my $name = $1; | |
2060 | my $ctx_before = substr($line, 0, $-[1]); | |
2061 | my $ctx = "$ctx_before$name"; | |
2062 | ||
2063 | # Ignore those directives where spaces _are_ permitted. | |
2064 | if ($name =~ /^(?: | |
2065 | if|for|while|switch|return|case| | |
000980cb | 2066 | volatile|__volatile__|coroutine_fn| |
1ec3f6f9 BS |
2067 | __attribute__|format|__extension__| |
2068 | asm|__asm__)$/x) | |
2069 | { | |
2070 | ||
69d5d21f TS |
2071 | # Ignore 'catch (...)' in C++ |
2072 | } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) { | |
2073 | ||
1ec3f6f9 BS |
2074 | # cpp #define statements have non-optional spaces, ie |
2075 | # if there is a space between the name and the open | |
2076 | # parenthesis it is simply not a parameter group. | |
2077 | } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { | |
2078 | ||
2079 | # cpp #elif statement condition may start with a ( | |
2080 | } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { | |
2081 | ||
2082 | # If this whole things ends with a type its most | |
2083 | # likely a typedef for a function. | |
2084 | } elsif ($ctx =~ /$Type$/) { | |
2085 | ||
2086 | } else { | |
c2df8783 | 2087 | ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr); |
1ec3f6f9 BS |
2088 | } |
2089 | } | |
2090 | # Check operator spacing. | |
2091 | if (!($line=~/\#\s*include/)) { | |
2092 | my $ops = qr{ | |
2093 | <<=|>>=|<=|>=|==|!=| | |
2094 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| | |
2095 | =>|->|<<|>>|<|>|=|!|~| | |
2096 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| | |
69d5d21f | 2097 | \?|::|: |
1ec3f6f9 BS |
2098 | }x; |
2099 | my @elements = split(/($ops|;)/, $opline); | |
2100 | my $off = 0; | |
2101 | ||
2102 | my $blank = copy_spacing($opline); | |
2103 | ||
2104 | for (my $n = 0; $n < $#elements; $n += 2) { | |
2105 | $off += length($elements[$n]); | |
2106 | ||
e7d81004 | 2107 | # Pick up the preceding and succeeding characters. |
1ec3f6f9 BS |
2108 | my $ca = substr($opline, 0, $off); |
2109 | my $cc = ''; | |
2110 | if (length($opline) >= ($off + length($elements[$n + 1]))) { | |
2111 | $cc = substr($opline, $off + length($elements[$n + 1])); | |
2112 | } | |
2113 | my $cb = "$ca$;$cc"; | |
2114 | ||
2115 | my $a = ''; | |
2116 | $a = 'V' if ($elements[$n] ne ''); | |
2117 | $a = 'W' if ($elements[$n] =~ /\s$/); | |
2118 | $a = 'C' if ($elements[$n] =~ /$;$/); | |
2119 | $a = 'B' if ($elements[$n] =~ /(\[|\()$/); | |
2120 | $a = 'O' if ($elements[$n] eq ''); | |
2121 | $a = 'E' if ($ca =~ /^\s*$/); | |
2122 | ||
2123 | my $op = $elements[$n + 1]; | |
2124 | ||
2125 | my $c = ''; | |
2126 | if (defined $elements[$n + 2]) { | |
2127 | $c = 'V' if ($elements[$n + 2] ne ''); | |
2128 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); | |
2129 | $c = 'C' if ($elements[$n + 2] =~ /^$;/); | |
2130 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); | |
2131 | $c = 'O' if ($elements[$n + 2] eq ''); | |
2132 | $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); | |
2133 | } else { | |
2134 | $c = 'E'; | |
2135 | } | |
2136 | ||
2137 | my $ctx = "${a}x${c}"; | |
2138 | ||
2139 | my $at = "(ctx:$ctx)"; | |
2140 | ||
2141 | my $ptr = substr($blank, 0, $off) . "^"; | |
2142 | my $hereptr = "$hereline$ptr\n"; | |
2143 | ||
2144 | # Pull out the value of this operator. | |
2145 | my $op_type = substr($curr_values, $off + 1, 1); | |
2146 | ||
2147 | # Get the full operator variant. | |
2148 | my $opv = $op . substr($curr_vars, $off, 1); | |
2149 | ||
2150 | # Ignore operators passed as parameters. | |
2151 | if ($op_type ne 'V' && | |
2152 | $ca =~ /\s$/ && $cc =~ /^\s*,/) { | |
2153 | ||
2154 | # # Ignore comments | |
2155 | # } elsif ($op =~ /^$;+$/) { | |
2156 | ||
2157 | # ; should have either the end of line or a space or \ after it | |
2158 | } elsif ($op eq ';') { | |
2159 | if ($ctx !~ /.x[WEBC]/ && | |
2160 | $cc !~ /^\\/ && $cc !~ /^;/) { | |
2161 | ERROR("space required after that '$op' $at\n" . $hereptr); | |
2162 | } | |
2163 | ||
2164 | # // is a comment | |
2165 | } elsif ($op eq '//') { | |
2166 | ||
69d5d21f TS |
2167 | # Ignore : used in class declaration in C++ |
2168 | } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ && | |
2169 | $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) { | |
2170 | ||
1ec3f6f9 BS |
2171 | # No spaces for: |
2172 | # -> | |
2173 | # : when part of a bitfield | |
2174 | } elsif ($op eq '->' || $opv eq ':B') { | |
2175 | if ($ctx =~ /Wx.|.xW/) { | |
2176 | ERROR("spaces prohibited around that '$op' $at\n" . $hereptr); | |
2177 | } | |
2178 | ||
2179 | # , must have a space on the right. | |
9fbe4784 | 2180 | # not required when having a single },{ on one line |
1ec3f6f9 | 2181 | } elsif ($op eq ',') { |
9fbe4784 | 2182 | if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ && |
04f2562f | 2183 | ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") { |
1ec3f6f9 BS |
2184 | ERROR("space required after that '$op' $at\n" . $hereptr); |
2185 | } | |
2186 | ||
2187 | # '*' as part of a type definition -- reported already. | |
2188 | } elsif ($opv eq '*_') { | |
2189 | #warn "'*' is part of type\n"; | |
2190 | ||
2191 | # unary operators should have a space before and | |
2192 | # none after. May be left adjacent to another | |
2193 | # unary operator, or a cast | |
2194 | } elsif ($op eq '!' || $op eq '~' || | |
2195 | $opv eq '*U' || $opv eq '-U' || | |
2196 | $opv eq '&U' || $opv eq '&&U') { | |
69d5d21f TS |
2197 | if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) { |
2198 | # '~' used as a name of Destructor | |
2199 | ||
2200 | } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { | |
1ec3f6f9 BS |
2201 | ERROR("space required before that '$op' $at\n" . $hereptr); |
2202 | } | |
2203 | if ($op eq '*' && $cc =~/\s*$Modifier\b/) { | |
2204 | # A unary '*' may be const | |
2205 | ||
2206 | } elsif ($ctx =~ /.xW/) { | |
2207 | ERROR("space prohibited after that '$op' $at\n" . $hereptr); | |
2208 | } | |
2209 | ||
2210 | # unary ++ and unary -- are allowed no space on one side. | |
2211 | } elsif ($op eq '++' or $op eq '--') { | |
2212 | if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { | |
2213 | ERROR("space required one side of that '$op' $at\n" . $hereptr); | |
2214 | } | |
2215 | if ($ctx =~ /Wx[BE]/ || | |
2216 | ($ctx =~ /Wx./ && $cc =~ /^;/)) { | |
2217 | ERROR("space prohibited before that '$op' $at\n" . $hereptr); | |
2218 | } | |
2219 | if ($ctx =~ /ExW/) { | |
2220 | ERROR("space prohibited after that '$op' $at\n" . $hereptr); | |
2221 | } | |
2222 | ||
1ec3f6f9 BS |
2223 | # A colon needs no spaces before when it is |
2224 | # terminating a case value or a label. | |
2225 | } elsif ($opv eq ':C' || $opv eq ':L') { | |
2226 | if ($ctx =~ /Wx./) { | |
2227 | ERROR("space prohibited before that '$op' $at\n" . $hereptr); | |
2228 | } | |
2229 | ||
2230 | # All the others need spaces both sides. | |
2231 | } elsif ($ctx !~ /[EWC]x[CWE]/) { | |
2232 | my $ok = 0; | |
2233 | ||
69d5d21f TS |
2234 | if ($realfile =~ /\.cpp|\.h$/) { |
2235 | # Ignore template arguments <...> in C++ | |
2236 | if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) { | |
2237 | $ok = 1; | |
2238 | } | |
2239 | ||
2240 | # Ignore :: in C++ | |
2241 | if ($op eq '::') { | |
2242 | $ok = 1; | |
2243 | } | |
2244 | } | |
2245 | ||
1ec3f6f9 BS |
2246 | # Ignore email addresses <foo@bar> |
2247 | if (($op eq '<' && | |
2248 | $cc =~ /^\S+\@\S+>/) || | |
2249 | ($op eq '>' && | |
2250 | $ca =~ /<\S+\@\S+$/)) | |
2251 | { | |
2252 | $ok = 1; | |
2253 | } | |
2254 | ||
2255 | # Ignore ?: | |
2256 | if (($opv eq ':O' && $ca =~ /\?$/) || | |
2257 | ($op eq '?' && $cc =~ /^:/)) { | |
2258 | $ok = 1; | |
2259 | } | |
2260 | ||
2261 | if ($ok == 0) { | |
2262 | ERROR("spaces required around that '$op' $at\n" . $hereptr); | |
2263 | } | |
2264 | } | |
2265 | $off += length($elements[$n + 1]); | |
2266 | } | |
2267 | } | |
2268 | ||
1ec3f6f9 | 2269 | #need space before brace following if, while, etc |
04f2562f FZ |
2270 | if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || |
2271 | $line =~ /do\{/) { | |
1ec3f6f9 BS |
2272 | ERROR("space required before the open brace '{'\n" . $herecurr); |
2273 | } | |
2274 | ||
2275 | # closing brace should have a space following it when it has anything | |
2276 | # on the line | |
2277 | if ($line =~ /}(?!(?:,|;|\)))\S/) { | |
2278 | ERROR("space required after that close brace '}'\n" . $herecurr); | |
2279 | } | |
2280 | ||
2281 | # check spacing on square brackets | |
2282 | if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { | |
2283 | ERROR("space prohibited after that open square bracket '['\n" . $herecurr); | |
2284 | } | |
2285 | if ($line =~ /\s\]/) { | |
2286 | ERROR("space prohibited before that close square bracket ']'\n" . $herecurr); | |
2287 | } | |
2288 | ||
2289 | # check spacing on parentheses | |
2290 | if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && | |
2291 | $line !~ /for\s*\(\s+;/) { | |
2292 | ERROR("space prohibited after that open parenthesis '('\n" . $herecurr); | |
2293 | } | |
2294 | if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && | |
2295 | $line !~ /for\s*\(.*;\s+\)/ && | |
2296 | $line !~ /:\s+\)/) { | |
2297 | ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr); | |
2298 | } | |
2299 | ||
1ec3f6f9 BS |
2300 | # Return is not a function. |
2301 | if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { | |
2302 | my $spacing = $1; | |
2303 | my $value = $2; | |
2304 | ||
2305 | # Flatten any parentheses | |
2306 | $value =~ s/\(/ \(/g; | |
2307 | $value =~ s/\)/\) /g; | |
2308 | while ($value =~ s/\[[^\{\}]*\]/1/ || | |
2309 | $value !~ /(?:$Ident|-?$Constant)\s* | |
2310 | $Compare\s* | |
2311 | (?:$Ident|-?$Constant)/x && | |
2312 | $value =~ s/\([^\(\)]*\)/1/) { | |
2313 | } | |
2314 | #print "value<$value>\n"; | |
c20b1396 PB |
2315 | if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/ && |
2316 | $line =~ /;$/) { | |
1ec3f6f9 BS |
2317 | ERROR("return is not a function, parentheses are not required\n" . $herecurr); |
2318 | ||
2319 | } elsif ($spacing !~ /\s+/) { | |
2320 | ERROR("space required before the open parenthesis '('\n" . $herecurr); | |
2321 | } | |
2322 | } | |
2323 | # Return of what appears to be an errno should normally be -'ve | |
2324 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { | |
2325 | my $name = $1; | |
2326 | if ($name ne 'EOF' && $name ne 'ERROR') { | |
c2df8783 | 2327 | ERROR("return of an errno should typically be -ve (return -$1)\n" . $herecurr); |
1ec3f6f9 BS |
2328 | } |
2329 | } | |
2330 | ||
50db69a1 PB |
2331 | if ($line =~ /^.\s*(Q(?:S?LIST|SIMPLEQ|TAILQ)_HEAD)\s*\(\s*[^,]/ && |
2332 | $line !~ /^.typedef/) { | |
2333 | ERROR("named $1 should be typedefed separately\n" . $herecurr); | |
2334 | } | |
2335 | ||
1ec3f6f9 BS |
2336 | # Need a space before open parenthesis after if, while etc |
2337 | if ($line=~/\b(if|while|for|switch)\(/) { | |
2338 | ERROR("space required before the open parenthesis '('\n" . $herecurr); | |
2339 | } | |
2340 | ||
2341 | # Check for illegal assignment in if conditional -- and check for trailing | |
2342 | # statements after the conditional. | |
2343 | if ($line =~ /do\s*(?!{)/) { | |
2344 | my ($stat_next) = ctx_statement_block($line_nr_next, | |
2345 | $remain_next, $off_next); | |
2346 | $stat_next =~ s/\n./\n /g; | |
2347 | ##print "stat<$stat> stat_next<$stat_next>\n"; | |
2348 | ||
2349 | if ($stat_next =~ /^\s*while\b/) { | |
2350 | # If the statement carries leading newlines, | |
2351 | # then count those as offsets. | |
2352 | my ($whitespace) = | |
2353 | ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); | |
2354 | my $offset = | |
2355 | statement_rawlines($whitespace) - 1; | |
2356 | ||
2357 | $suppress_whiletrailers{$line_nr_next + | |
2358 | $offset} = 1; | |
2359 | } | |
2360 | } | |
2361 | if (!defined $suppress_whiletrailers{$linenr} && | |
2362 | $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { | |
2363 | my ($s, $c) = ($stat, $cond); | |
2364 | ||
2365 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { | |
2366 | ERROR("do not use assignment in if condition\n" . $herecurr); | |
2367 | } | |
2368 | ||
2369 | # Find out what is on the end of the line after the | |
2370 | # conditional. | |
2371 | substr($s, 0, length($c), ''); | |
2372 | $s =~ s/\n.*//g; | |
2373 | $s =~ s/$;//g; # Remove any comments | |
2374 | if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && | |
2375 | $c !~ /}\s*while\s*/) | |
2376 | { | |
2377 | # Find out how long the conditional actually is. | |
2378 | my @newlines = ($c =~ /\n/gs); | |
2379 | my $cond_lines = 1 + $#newlines; | |
2380 | my $stat_real = ''; | |
2381 | ||
2382 | $stat_real = raw_line($linenr, $cond_lines) | |
2383 | . "\n" if ($cond_lines); | |
2384 | if (defined($stat_real) && $cond_lines > 1) { | |
2385 | $stat_real = "[...]\n$stat_real"; | |
2386 | } | |
2387 | ||
2388 | ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real); | |
2389 | } | |
2390 | } | |
2391 | ||
2392 | # Check for bitwise tests written as boolean | |
2393 | if ($line =~ / | |
2394 | (?: | |
2395 | (?:\[|\(|\&\&|\|\|) | |
2396 | \s*0[xX][0-9]+\s* | |
2397 | (?:\&\&|\|\|) | |
2398 | | | |
2399 | (?:\&\&|\|\|) | |
2400 | \s*0[xX][0-9]+\s* | |
2401 | (?:\&\&|\|\||\)|\]) | |
2402 | )/x) | |
2403 | { | |
c2df8783 | 2404 | ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); |
1ec3f6f9 BS |
2405 | } |
2406 | ||
2407 | # if and else should not have general statements after it | |
2408 | if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { | |
2409 | my $s = $1; | |
2410 | $s =~ s/$;//g; # Remove any comments | |
2411 | if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { | |
2412 | ERROR("trailing statements should be on next line\n" . $herecurr); | |
2413 | } | |
2414 | } | |
2415 | # if should not continue a brace | |
2416 | if ($line =~ /}\s*if\b/) { | |
2417 | ERROR("trailing statements should be on next line\n" . | |
2418 | $herecurr); | |
2419 | } | |
2420 | # case and default should not have general statements after them | |
2421 | if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && | |
2422 | $line !~ /\G(?: | |
2423 | (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| | |
2424 | \s*return\s+ | |
2425 | )/xg) | |
2426 | { | |
2427 | ERROR("trailing statements should be on next line\n" . $herecurr); | |
2428 | } | |
2429 | ||
2430 | # Check for }<nl>else {, these must be at the same | |
2431 | # indent level to be relevant to each other. | |
2432 | if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and | |
2433 | $previndent == $indent) { | |
2434 | ERROR("else should follow close brace '}'\n" . $hereprev); | |
2435 | } | |
2436 | ||
2437 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and | |
2438 | $previndent == $indent) { | |
2439 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); | |
2440 | ||
2441 | # Find out what is on the end of the line after the | |
2442 | # conditional. | |
2443 | substr($s, 0, length($c), ''); | |
2444 | $s =~ s/\n.*//g; | |
2445 | ||
2446 | if ($s =~ /^\s*;/) { | |
2447 | ERROR("while should follow close brace '}'\n" . $hereprev); | |
2448 | } | |
2449 | } | |
2450 | ||
2451 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new | |
2452 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { | |
2453 | # print "No studly caps, use _\n"; | |
2454 | # print "$herecurr"; | |
2455 | # $clean = 0; | |
2456 | # } | |
2457 | ||
2458 | #no spaces allowed after \ in define | |
2459 | if ($line=~/\#\s*define.*\\\s$/) { | |
c2df8783 | 2460 | ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr); |
1ec3f6f9 BS |
2461 | } |
2462 | ||
1ec3f6f9 BS |
2463 | # multi-statement macros should be enclosed in a do while loop, grab the |
2464 | # first statement and ensure its the whole macro if its not enclosed | |
2465 | # in a known good container | |
2466 | if ($realfile !~ m@/vmlinux.lds.h$@ && | |
2467 | $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { | |
2468 | my $ln = $linenr; | |
2469 | my $cnt = $realcnt; | |
2470 | my ($off, $dstat, $dcond, $rest); | |
2471 | my $ctx = ''; | |
2472 | ||
2473 | my $args = defined($1); | |
2474 | ||
2475 | # Find the end of the macro and limit our statement | |
2476 | # search to that. | |
2477 | while ($cnt > 0 && defined $lines[$ln - 1] && | |
2478 | $lines[$ln - 1] =~ /^(?:-|..*\\$)/) | |
2479 | { | |
2480 | $ctx .= $rawlines[$ln - 1] . "\n"; | |
2481 | $cnt-- if ($lines[$ln - 1] !~ /^-/); | |
2482 | $ln++; | |
2483 | } | |
2484 | $ctx .= $rawlines[$ln - 1]; | |
2485 | ||
2486 | ($dstat, $dcond, $ln, $cnt, $off) = | |
2487 | ctx_statement_block($linenr, $ln - $linenr + 1, 0); | |
2488 | #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; | |
2489 | #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; | |
2490 | ||
2491 | # Extract the remainder of the define (if any) and | |
2492 | # rip off surrounding spaces, and trailing \'s. | |
2493 | $rest = ''; | |
2494 | while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) { | |
2495 | #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n"; | |
2496 | if ($off != 0 || $lines[$ln - 1] !~ /^-/) { | |
2497 | $rest .= substr($lines[$ln - 1], $off) . "\n"; | |
2498 | $cnt--; | |
2499 | } | |
2500 | $ln++; | |
2501 | $off = 0; | |
2502 | } | |
2503 | $rest =~ s/\\\n.//g; | |
2504 | $rest =~ s/^\s*//s; | |
2505 | $rest =~ s/\s*$//s; | |
2506 | ||
2507 | # Clean up the original statement. | |
2508 | if ($args) { | |
2509 | substr($dstat, 0, length($dcond), ''); | |
2510 | } else { | |
2511 | $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//; | |
2512 | } | |
2513 | $dstat =~ s/$;//g; | |
2514 | $dstat =~ s/\\\n.//g; | |
2515 | $dstat =~ s/^\s*//s; | |
2516 | $dstat =~ s/\s*$//s; | |
2517 | ||
2518 | # Flatten any parentheses and braces | |
2519 | while ($dstat =~ s/\([^\(\)]*\)/1/ || | |
2520 | $dstat =~ s/\{[^\{\}]*\}/1/ || | |
2521 | $dstat =~ s/\[[^\{\}]*\]/1/) | |
2522 | { | |
2523 | } | |
2524 | ||
2525 | my $exceptions = qr{ | |
2526 | $Declare| | |
2527 | module_param_named| | |
2528 | MODULE_PARAM_DESC| | |
2529 | DECLARE_PER_CPU| | |
2530 | DEFINE_PER_CPU| | |
2531 | __typeof__\(| | |
2532 | union| | |
2533 | struct| | |
2534 | \.$Ident\s*=\s*| | |
2535 | ^\"|\"$ | |
2536 | }x; | |
2537 | #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; | |
2538 | if ($rest ne '' && $rest ne ',') { | |
2539 | if ($rest !~ /while\s*\(/ && | |
2540 | $dstat !~ /$exceptions/) | |
2541 | { | |
2542 | ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n"); | |
2543 | } | |
2544 | ||
2545 | } elsif ($ctx !~ /;/) { | |
2546 | if ($dstat ne '' && | |
2547 | $dstat !~ /^(?:$Ident|-?$Constant)$/ && | |
2548 | $dstat !~ /$exceptions/ && | |
2549 | $dstat !~ /^\.$Ident\s*=/ && | |
2550 | $dstat =~ /$Operators/) | |
2551 | { | |
2552 | ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n"); | |
2553 | } | |
2554 | } | |
2555 | } | |
2556 | ||
2b9aef6f SH |
2557 | # check for missing bracing around if etc |
2558 | if ($line =~ /(^.*)\b(?:if|while|for)\b/ && | |
2559 | $line !~ /\#\s*if/) { | |
053e45d2 SH |
2560 | my $allowed = 0; |
2561 | ||
2562 | # Check the pre-context. | |
2563 | if ($line =~ /(\}.*?)$/) { | |
2564 | my $pre = $1; | |
2565 | ||
2566 | if ($line !~ /else/) { | |
2567 | print "APW: ALLOWED: pre<$pre> line<$line>\n" | |
2568 | if $dbg_adv_apw; | |
2569 | $allowed = 1; | |
2570 | } | |
2571 | } | |
1ec3f6f9 BS |
2572 | my ($level, $endln, @chunks) = |
2573 | ctx_statement_full($linenr, $realcnt, 1); | |
69402a69 DS |
2574 | if ($dbg_adv_apw) { |
2575 | print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; | |
2576 | print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n" | |
2577 | if $#chunks >= 1; | |
2578 | } | |
b6469683 | 2579 | if ($#chunks >= 0 && $level == 0) { |
1ec3f6f9 BS |
2580 | my $seen = 0; |
2581 | my $herectx = $here . "\n"; | |
2582 | my $ln = $linenr - 1; | |
2583 | for my $chunk (@chunks) { | |
2584 | my ($cond, $block) = @{$chunk}; | |
2585 | ||
2586 | # If the condition carries leading newlines, then count those as offsets. | |
2587 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); | |
2588 | my $offset = statement_rawlines($whitespace) - 1; | |
2589 | ||
2590 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; | |
2591 | ||
2592 | # We have looked at and allowed this specific line. | |
2593 | $suppress_ifbraces{$ln + $offset} = 1; | |
2594 | ||
2595 | $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; | |
2596 | $ln += statement_rawlines($block) - 1; | |
2597 | ||
2598 | substr($block, 0, length($cond), ''); | |
2599 | ||
a97ceca5 HR |
2600 | my $spaced_block = $block; |
2601 | $spaced_block =~ s/\n\+/ /g; | |
2602 | ||
04f2562f | 2603 | $seen++ if ($spaced_block =~ /^\s*\{/); |
1ec3f6f9 | 2604 | |
69402a69 DS |
2605 | print "APW: cond<$cond> block<$block> allowed<$allowed>\n" |
2606 | if $dbg_adv_apw; | |
1ec3f6f9 | 2607 | if (statement_lines($cond) > 1) { |
69402a69 DS |
2608 | print "APW: ALLOWED: cond<$cond>\n" |
2609 | if $dbg_adv_apw; | |
2610 | $allowed = 1; | |
1ec3f6f9 BS |
2611 | } |
2612 | if ($block =~/\b(?:if|for|while)\b/) { | |
69402a69 DS |
2613 | print "APW: ALLOWED: block<$block>\n" |
2614 | if $dbg_adv_apw; | |
2615 | $allowed = 1; | |
1ec3f6f9 BS |
2616 | } |
2617 | if (statement_block_size($block) > 1) { | |
69402a69 DS |
2618 | print "APW: ALLOWED: lines block<$block>\n" |
2619 | if $dbg_adv_apw; | |
2620 | $allowed = 1; | |
1ec3f6f9 BS |
2621 | } |
2622 | } | |
053e45d2 | 2623 | if ($seen != ($#chunks + 1) && !$allowed) { |
c2df8783 | 2624 | ERROR("braces {} are necessary for all arms of this statement\n" . $herectx); |
1ec3f6f9 BS |
2625 | } |
2626 | } | |
2627 | } | |
2628 | if (!defined $suppress_ifbraces{$linenr - 1} && | |
789f88d0 | 2629 | $line =~ /\b(if|while|for|else)\b/ && |
d0510af2 | 2630 | $line !~ /\#\s*if/ && |
789f88d0 | 2631 | $line !~ /\#\s*else/) { |
1ec3f6f9 BS |
2632 | my $allowed = 0; |
2633 | ||
dfe7053a DS |
2634 | # Check the pre-context. |
2635 | if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { | |
2636 | my $pre = $1; | |
2637 | ||
2638 | if ($line !~ /else/) { | |
2639 | print "APW: ALLOWED: pre<$pre> line<$line>\n" | |
2640 | if $dbg_adv_apw; | |
2641 | $allowed = 1; | |
2642 | } | |
2643 | } | |
1ec3f6f9 BS |
2644 | |
2645 | my ($level, $endln, @chunks) = | |
2646 | ctx_statement_full($linenr, $realcnt, $-[0]); | |
2647 | ||
2648 | # Check the condition. | |
2649 | my ($cond, $block) = @{$chunks[0]}; | |
5424302e DS |
2650 | print "CHECKING<$linenr> cond<$cond> block<$block>\n" |
2651 | if $dbg_adv_checking; | |
1ec3f6f9 BS |
2652 | if (defined $cond) { |
2653 | substr($block, 0, length($cond), ''); | |
2654 | } | |
2655 | if (statement_lines($cond) > 1) { | |
69402a69 DS |
2656 | print "APW: ALLOWED: cond<$cond>\n" |
2657 | if $dbg_adv_apw; | |
2658 | $allowed = 1; | |
1ec3f6f9 BS |
2659 | } |
2660 | if ($block =~/\b(?:if|for|while)\b/) { | |
69402a69 DS |
2661 | print "APW: ALLOWED: block<$block>\n" |
2662 | if $dbg_adv_apw; | |
2663 | $allowed = 1; | |
1ec3f6f9 BS |
2664 | } |
2665 | if (statement_block_size($block) > 1) { | |
69402a69 DS |
2666 | print "APW: ALLOWED: lines block<$block>\n" |
2667 | if $dbg_adv_apw; | |
2668 | $allowed = 1; | |
1ec3f6f9 BS |
2669 | } |
2670 | # Check the post-context. | |
2671 | if (defined $chunks[1]) { | |
2672 | my ($cond, $block) = @{$chunks[1]}; | |
2673 | if (defined $cond) { | |
2674 | substr($block, 0, length($cond), ''); | |
2675 | } | |
2676 | if ($block =~ /^\s*\{/) { | |
69402a69 DS |
2677 | print "APW: ALLOWED: chunk-1 block<$block>\n" |
2678 | if $dbg_adv_apw; | |
2679 | $allowed = 1; | |
1ec3f6f9 BS |
2680 | } |
2681 | } | |
a99ac041 DS |
2682 | print "DCS: level=$level block<$block> allowed=$allowed\n" |
2683 | if $dbg_adv_dcs; | |
b6469683 | 2684 | if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) { |
1ec3f6f9 BS |
2685 | my $herectx = $here . "\n";; |
2686 | my $cnt = statement_rawlines($block); | |
2687 | ||
2688 | for (my $n = 0; $n < $cnt; $n++) { | |
2689 | $herectx .= raw_line($linenr, $n) . "\n";; | |
2690 | } | |
2691 | ||
c2df8783 | 2692 | ERROR("braces {} are necessary even for single statement blocks\n" . $herectx); |
1ec3f6f9 BS |
2693 | } |
2694 | } | |
2695 | ||
1ec3f6f9 BS |
2696 | # no volatiles please |
2697 | my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; | |
6b012d23 MAL |
2698 | if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/ && |
2699 | $line !~ /sig_atomic_t/ && | |
2700 | !ctx_has_comment($first_line, $linenr)) { | |
2701 | my $msg = "Use of volatile is usually wrong, please add a comment\n" . $herecurr; | |
2702 | ERROR($msg); | |
1ec3f6f9 BS |
2703 | } |
2704 | ||
1ec3f6f9 BS |
2705 | # warn about #if 0 |
2706 | if ($line =~ /^.\s*\#\s*if\s+0\b/) { | |
c2df8783 | 2707 | ERROR("if this code is redundant consider removing it\n" . |
1ec3f6f9 BS |
2708 | $herecurr); |
2709 | } | |
2710 | ||
71c47b01 | 2711 | # check for needless g_free() checks |
1ec3f6f9 BS |
2712 | if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { |
2713 | my $expr = $1; | |
71c47b01 | 2714 | if ($line =~ /\bg_free\(\Q$expr\E\);/) { |
c2df8783 | 2715 | ERROR("g_free(NULL) is safe this check is probably not required\n" . $hereprev); |
1ec3f6f9 BS |
2716 | } |
2717 | } | |
1ec3f6f9 BS |
2718 | |
2719 | # warn about #ifdefs in C files | |
2720 | # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { | |
2721 | # print "#ifdef in C files should be avoided\n"; | |
2722 | # print "$herecurr"; | |
2723 | # $clean = 0; | |
2724 | # } | |
2725 | ||
2726 | # warn about spacing in #ifdefs | |
2727 | if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { | |
2728 | ERROR("exactly one space required after that #$1\n" . $herecurr); | |
2729 | } | |
1ec3f6f9 | 2730 | # check for memory barriers without a comment. |
71c47b01 | 2731 | if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { |
1ec3f6f9 | 2732 | if (!ctx_has_comment($first_line, $linenr)) { |
c2df8783 | 2733 | ERROR("memory barrier without comment\n" . $herecurr); |
1ec3f6f9 BS |
2734 | } |
2735 | } | |
2736 | # check of hardware specific defines | |
71c47b01 PB |
2737 | # we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases |
2738 | # where they might be necessary. | |
2739 | if ($line =~ m@^.\s*\#\s*if.*\b__@) { | |
63ae8b94 | 2740 | WARN("architecture specific defines should be avoided\n" . $herecurr); |
1ec3f6f9 BS |
2741 | } |
2742 | ||
2743 | # Check that the storage class is at the beginning of a declaration | |
2744 | if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { | |
c2df8783 | 2745 | ERROR("storage class should be at the beginning of the declaration\n" . $herecurr) |
1ec3f6f9 BS |
2746 | } |
2747 | ||
2748 | # check the location of the inline attribute, that it is between | |
2749 | # storage class and type. | |
2750 | if ($line =~ /\b$Type\s+$Inline\b/ || | |
2751 | $line =~ /\b$Inline\s+$Storage\b/) { | |
2752 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); | |
2753 | } | |
2754 | ||
1ec3f6f9 BS |
2755 | # check for sizeof(&) |
2756 | if ($line =~ /\bsizeof\s*\(\s*\&/) { | |
c2df8783 | 2757 | ERROR("sizeof(& should be avoided\n" . $herecurr); |
1ec3f6f9 BS |
2758 | } |
2759 | ||
2760 | # check for new externs in .c files. | |
2761 | if ($realfile =~ /\.c$/ && defined $stat && | |
2762 | $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) | |
2763 | { | |
2764 | my $function_name = $1; | |
2765 | my $paren_space = $2; | |
2766 | ||
2767 | my $s = $stat; | |
2768 | if (defined $cond) { | |
2769 | substr($s, 0, length($cond), ''); | |
2770 | } | |
2771 | if ($s =~ /^\s*;/ && | |
2772 | $function_name ne 'uninitialized_var') | |
2773 | { | |
c2df8783 | 2774 | ERROR("externs should be avoided in .c files\n" . $herecurr); |
1ec3f6f9 BS |
2775 | } |
2776 | ||
2777 | if ($paren_space =~ /\n/) { | |
c2df8783 | 2778 | ERROR("arguments for function declarations should follow identifier\n" . $herecurr); |
1ec3f6f9 BS |
2779 | } |
2780 | ||
2781 | } elsif ($realfile =~ /\.c$/ && defined $stat && | |
2782 | $stat =~ /^.\s*extern\s+/) | |
2783 | { | |
c2df8783 | 2784 | ERROR("externs should be avoided in .c files\n" . $herecurr); |
1ec3f6f9 BS |
2785 | } |
2786 | ||
71c47b01 PB |
2787 | # check for pointless casting of g_malloc return |
2788 | if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) { | |
2789 | if ($2 == 'm') { | |
c2df8783 | 2790 | ERROR("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr); |
71c47b01 | 2791 | } else { |
c2df8783 | 2792 | ERROR("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr); |
71c47b01 | 2793 | } |
1ec3f6f9 BS |
2794 | } |
2795 | ||
2796 | # check for gcc specific __FUNCTION__ | |
2797 | if ($line =~ /__FUNCTION__/) { | |
c2df8783 | 2798 | ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); |
1ec3f6f9 BS |
2799 | } |
2800 | ||
fb8446d9 JS |
2801 | # recommend g_path_get_* over g_strdup(basename/dirname(...)) |
2802 | if ($line =~ /\bg_strdup\s*\(\s*(basename|dirname)\s*\(/) { | |
2803 | WARN("consider using g_path_get_$1() in preference to g_strdup($1())\n" . $herecurr); | |
2804 | } | |
2805 | ||
5e43efb2 | 2806 | # recommend qemu_strto* over strto* for numeric conversions |
01fb8e19 | 2807 | if ($line =~ /\b(strto[^kd].*?)\s*\(/) { |
c2df8783 | 2808 | ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr); |
1ec3f6f9 | 2809 | } |
e8c2091d PB |
2810 | # recommend sigaction over signal for portability, when establishing a handler |
2811 | if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) { | |
2812 | ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr); | |
2813 | } | |
71c47b01 PB |
2814 | # check for module_init(), use category-specific init macros explicitly please |
2815 | if ($line =~ /^module_init\s*\(/) { | |
c2df8783 | 2816 | ERROR("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr); |
1ec3f6f9 BS |
2817 | } |
2818 | # check for various ops structs, ensure they are const. | |
71c47b01 PB |
2819 | my $struct_ops = qr{AIOCBInfo| |
2820 | BdrvActionOps| | |
2821 | BlockDevOps| | |
2822 | BlockJobDriver| | |
2823 | DisplayChangeListenerOps| | |
2824 | GraphicHwOps| | |
2825 | IDEDMAOps| | |
2826 | KVMCapabilityInfo| | |
2827 | MemoryRegionIOMMUOps| | |
2828 | MemoryRegionOps| | |
2829 | MemoryRegionPortio| | |
2830 | QEMUFileOps| | |
2831 | SCSIBusInfo| | |
2832 | SCSIReqOps| | |
2833 | Spice[A-Z][a-zA-Z0-9]*Interface| | |
71c47b01 PB |
2834 | USBDesc[A-Z][a-zA-Z0-9]*| |
2835 | VhostOps| | |
2836 | VMStateDescription| | |
2837 | VMStateInfo}x; | |
1ec3f6f9 | 2838 | if ($line !~ /\bconst\b/ && |
e20e718c PB |
2839 | $line =~ /\b($struct_ops)\b.*=/) { |
2840 | ERROR("initializer for struct $1 should normally be const\n" . | |
1ec3f6f9 BS |
2841 | $herecurr); |
2842 | } | |
2843 | ||
1ec3f6f9 BS |
2844 | # check for %L{u,d,i} in strings |
2845 | my $string; | |
2846 | while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { | |
2847 | $string = substr($rawline, $-[1], $+[1] - $-[1]); | |
2848 | $string =~ s/%%/__/g; | |
2849 | if ($string =~ /(?<!%)%L[udi]/) { | |
c2df8783 | 2850 | ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); |
1ec3f6f9 BS |
2851 | last; |
2852 | } | |
2853 | } | |
2854 | ||
9964d8f9 SW |
2855 | # QEMU specific tests |
2856 | if ($rawline =~ /\b(?:Qemu|QEmu)\b/) { | |
c2df8783 | 2857 | ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr); |
9964d8f9 | 2858 | } |
8b6ee9ae | 2859 | |
5d596c24 JH |
2860 | # Qemu error function tests |
2861 | ||
2862 | # Find newlines in error messages | |
2863 | my $qemu_error_funcs = qr{error_setg| | |
2864 | error_setg_errno| | |
2865 | error_setg_win32| | |
a47eb010 | 2866 | error_setg_file_open| |
5d596c24 | 2867 | error_set| |
a47eb010 | 2868 | error_prepend| |
e43ead1d | 2869 | warn_reportf_err| |
a47eb010 | 2870 | error_reportf_err| |
5d596c24 | 2871 | error_vreport| |
97f40301 AF |
2872 | warn_vreport| |
2873 | info_vreport| | |
2874 | error_report| | |
2875 | warn_report| | |
f1e35acf PB |
2876 | info_report| |
2877 | g_test_message}x; | |
5d596c24 | 2878 | |
a47eb010 | 2879 | if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) { |
c2df8783 | 2880 | ERROR("Error messages should not contain newlines\n" . $herecurr); |
5d596c24 JH |
2881 | } |
2882 | ||
2883 | # Continue checking for error messages that contains newlines. This | |
2884 | # check handles cases where string literals are spread over multiple lines. | |
2885 | # Example: | |
2886 | # error_report("Error msg line #1" | |
2887 | # "Error msg line #2\n"); | |
2888 | my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"}; | |
2889 | my $continued_str_literal = qr{\+\s*\".*\"}; | |
2890 | ||
2891 | if ($rawline =~ /$quoted_newline_regex/) { | |
2892 | # Backtrack to first line that does not contain only a quoted literal | |
2893 | # and assume that it is the start of the statement. | |
2894 | my $i = $linenr - 2; | |
2895 | ||
2896 | while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) { | |
2897 | $i--; | |
2898 | } | |
2899 | ||
2900 | if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) { | |
c2df8783 | 2901 | ERROR("Error messages should not contain newlines\n" . $herecurr); |
5d596c24 JH |
2902 | } |
2903 | } | |
2904 | ||
3f822cff | 2905 | # check for non-portable libc calls that have portable alternatives in QEMU |
8b6ee9ae SH |
2906 | if ($line =~ /\bffs\(/) { |
2907 | ERROR("use ctz32() instead of ffs()\n" . $herecurr); | |
2908 | } | |
2909 | if ($line =~ /\bffsl\(/) { | |
2910 | ERROR("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr); | |
2911 | } | |
2912 | if ($line =~ /\bffsll\(/) { | |
2913 | ERROR("use ctz64() instead of ffsll()\n" . $herecurr); | |
2914 | } | |
3f822cff PB |
2915 | if ($line =~ /\bbzero\(/) { |
2916 | ERROR("use memset() instead of bzero()\n" . $herecurr); | |
2917 | } | |
6e938956 DDAG |
2918 | my $non_exit_glib_asserts = qr{g_assert_cmpstr| |
2919 | g_assert_cmpint| | |
2920 | g_assert_cmpuint| | |
2921 | g_assert_cmphex| | |
2922 | g_assert_cmpfloat| | |
2923 | g_assert_true| | |
2924 | g_assert_false| | |
2925 | g_assert_nonnull| | |
2926 | g_assert_null| | |
2927 | g_assert_no_error| | |
2928 | g_assert_error| | |
2929 | g_test_assert_expected_messages| | |
2930 | g_test_trap_assert_passed| | |
2931 | g_test_trap_assert_stdout| | |
2932 | g_test_trap_assert_stdout_unmatched| | |
2933 | g_test_trap_assert_stderr| | |
2934 | g_test_trap_assert_stderr_unmatched}x; | |
2935 | if ($realfile !~ /^tests\// && | |
2936 | $line =~ /\b(?:$non_exit_glib_asserts)\(/) { | |
2937 | ERROR("Use g_assert or g_assert_not_reached\n". $herecurr); | |
2938 | } | |
1ec3f6f9 BS |
2939 | } |
2940 | ||
fd9c0cfe PB |
2941 | if ($is_patch && $chk_signoff && $signoff == 0) { |
2942 | ERROR("Missing Signed-off-by: line(s)\n"); | |
2943 | } | |
2944 | ||
1ec3f6f9 BS |
2945 | # If we have no input at all, then there is nothing to report on |
2946 | # so just keep quiet. | |
2947 | if ($#rawlines == -1) { | |
1ff7ebf3 | 2948 | return 1; |
1ec3f6f9 BS |
2949 | } |
2950 | ||
2951 | # In mailback mode only produce a report in the negative, for | |
2952 | # things that appear to be patches. | |
2953 | if ($mailback && ($clean == 1 || !$is_patch)) { | |
1ff7ebf3 | 2954 | return 1; |
1ec3f6f9 BS |
2955 | } |
2956 | ||
2957 | # This is not a patch, and we are are in 'no-patch' mode so | |
2958 | # just keep quiet. | |
2959 | if (!$chk_patch && !$is_patch) { | |
1ff7ebf3 | 2960 | return 1; |
1ec3f6f9 BS |
2961 | } |
2962 | ||
2963 | if (!$is_patch) { | |
2964 | ERROR("Does not appear to be a unified-diff format patch\n"); | |
2965 | } | |
1ec3f6f9 BS |
2966 | |
2967 | print report_dump(); | |
2968 | if ($summary && !($clean == 1 && $quiet == 1)) { | |
2969 | print "$filename " if ($summary_file); | |
2970 | print "total: $cnt_error errors, $cnt_warn warnings, " . | |
1ec3f6f9 BS |
2971 | "$cnt_lines lines checked\n"; |
2972 | print "\n" if ($quiet == 0); | |
2973 | } | |
2974 | ||
2975 | if ($quiet == 0) { | |
2976 | # If there were whitespace errors which cleanpatch can fix | |
2977 | # then suggest that. | |
b6469683 BS |
2978 | # if ($rpt_cleaners) { |
2979 | # print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; | |
2980 | # print " scripts/cleanfile\n\n"; | |
2981 | # } | |
1ec3f6f9 BS |
2982 | } |
2983 | ||
2984 | if ($clean == 1 && $quiet == 0) { | |
2985 | print "$vname has no obvious style problems and is ready for submission.\n" | |
2986 | } | |
2987 | if ($clean == 0 && $quiet == 0) { | |
2988 | print "$vname has style problems, please review. If any of these errors\n"; | |
2989 | print "are false positives report them to the maintainer, see\n"; | |
2990 | print "CHECKPATCH in MAINTAINERS.\n"; | |
2991 | } | |
2992 | ||
141de886 | 2993 | return ($no_warnings ? $clean : $cnt_error == 0); |
1ec3f6f9 | 2994 | } |