blob: 28d71b3733c1bc6fd22f6329ed219a71291d1c20 [file] [log] [blame]
Kamil Rytarowskib7d5a9c2017-04-26 15:16:04 +02001#!/usr/bin/env perl
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002# (c) 2001, Dave Jones. (the file handling bit)
3# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
5# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
6# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Kamil Rytarowskib7d5a9c2017-04-26 15:16:04 +02009use warnings;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000010
11my $P = $0;
12$P =~ s@.*/@@g;
13
14my $V = '0.31';
15
16use Getopt::Long qw(:config no_auto_abbrev);
17
18my $quiet = 0;
19my $tree = 1;
20my $chk_signoff = 1;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010021my $chk_patch = undef;
22my $chk_branch = undef;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000023my $tst_only;
24my $emacs = 0;
25my $terse = 0;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010026my $file = undef;
Paolo Bonzini141de882016-08-09 17:47:44 +020027my $no_warnings = 0;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000028my $summary = 1;
29my $mailback = 0;
30my $summary_file = 0;
31my $root;
32my %debug;
33my $help = 0;
34
35sub help {
36 my ($exitcode) = @_;
37
38 print << "EOM";
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010039Usage:
40
41 $P [OPTION]... [FILE]...
42 $P [OPTION]... [GIT-REV-LIST]
43
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000044Version: $V
45
46Options:
47 -q, --quiet quiet
48 --no-tree run without a kernel tree
49 --no-signoff do not check for 'Signed-off-by' line
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010050 --patch treat FILE as patchfile
51 --branch treat args as GIT revision list
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000052 --emacs emacs compile window format
53 --terse one line per report
54 -f, --file treat FILE as regular source file
Paolo Bonzini141de882016-08-09 17:47:44 +020055 --strict fail if only warnings are found
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000056 --root=PATH PATH to the kernel tree root
57 --no-summary suppress the per-file summary
58 --mailback only produce a report in case of warnings/errors
59 --summary-file include the filename in summary
60 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
61 'values', 'possible', 'type', and 'attr' (default
62 is all off)
63 --test-only=WORD report only warnings/errors containing WORD
64 literally
65 -h, --help, --version display this help and exit
66
67When FILE is - read standard input.
68EOM
69
70 exit($exitcode);
71}
72
73GetOptions(
74 'q|quiet+' => \$quiet,
75 'tree!' => \$tree,
76 'signoff!' => \$chk_signoff,
77 'patch!' => \$chk_patch,
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +010078 'branch!' => \$chk_branch,
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000079 'emacs!' => \$emacs,
80 'terse!' => \$terse,
81 'f|file!' => \$file,
Paolo Bonzini141de882016-08-09 17:47:44 +020082 'strict!' => \$no_warnings,
Blue Swirl1ec3f6f2011-01-20 20:54:26 +000083 'root=s' => \$root,
84 'summary!' => \$summary,
85 'mailback!' => \$mailback,
86 'summary-file!' => \$summary_file,
87
88 'debug=s' => \%debug,
89 'test-only=s' => \$tst_only,
90 'h|help' => \$help,
91 'version' => \$help
92) or help(1);
93
94help(0) if ($help);
95
96my $exit = 0;
97
98if ($#ARGV < 0) {
99 print "$P: no input files\n";
100 exit(1);
101}
102
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100103if (!defined $chk_branch && !defined $chk_patch && !defined $file) {
104 $chk_branch = $ARGV[0] =~ /\.\./ ? 1 : 0;
105 $chk_patch = $chk_branch ? 0 :
106 $ARGV[0] =~ /\.patch$/ || $ARGV[0] eq "-" ? 1 : 0;
107 $file = $chk_branch || $chk_patch ? 0 : 1;
108} elsif (!defined $chk_branch && !defined $chk_patch) {
109 if ($file) {
110 $chk_branch = $chk_patch = 0;
111 } else {
112 $chk_branch = $ARGV[0] =~ /\.\./ ? 1 : 0;
113 $chk_patch = $chk_branch ? 0 : 1;
114 }
115} elsif (!defined $chk_branch && !defined $file) {
116 if ($chk_patch) {
117 $chk_branch = $file = 0;
118 } else {
119 $chk_branch = $ARGV[0] =~ /\.\./ ? 1 : 0;
120 $file = $chk_branch ? 0 : 1;
121 }
122} elsif (!defined $chk_patch && !defined $file) {
123 if ($chk_branch) {
124 $chk_patch = $file = 0;
125 } else {
126 $chk_patch = $ARGV[0] =~ /\.patch$/ || $ARGV[0] eq "-" ? 1 : 0;
127 $file = $chk_patch ? 0 : 1;
128 }
129} elsif (!defined $chk_branch) {
130 $chk_branch = $chk_patch || $file ? 0 : 1;
131} elsif (!defined $chk_patch) {
132 $chk_patch = $chk_branch || $file ? 0 : 1;
133} elsif (!defined $file) {
134 $file = $chk_patch || $chk_branch ? 0 : 1;
135}
136
137if (($chk_patch && $chk_branch) ||
138 ($chk_patch && $file) ||
139 ($chk_branch && $file)) {
140 die "Only one of --file, --branch, --patch is permitted\n";
141}
142if (!$chk_patch && !$chk_branch && !$file) {
143 die "One of --file, --branch, --patch is required\n";
144}
145
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000146my $dbg_values = 0;
147my $dbg_possible = 0;
148my $dbg_type = 0;
149my $dbg_attr = 0;
Don Slutza99ac042012-09-02 19:22:35 -0400150my $dbg_adv_dcs = 0;
Don Slutz54243022012-09-02 19:22:36 -0400151my $dbg_adv_checking = 0;
Don Slutz69402a62012-09-02 19:22:37 -0400152my $dbg_adv_apw = 0;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000153for my $key (keys %debug) {
154 ## no critic
155 eval "\${dbg_$key} = '$debug{$key}';";
156 die "$@" if ($@);
157}
158
159my $rpt_cleaners = 0;
160
161if ($terse) {
162 $emacs = 1;
163 $quiet++;
164}
165
166if ($tree) {
167 if (defined $root) {
168 if (!top_of_kernel_tree($root)) {
169 die "$P: $root: --root does not point at a valid tree\n";
170 }
171 } else {
172 if (top_of_kernel_tree('.')) {
173 $root = '.';
174 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
175 top_of_kernel_tree($1)) {
176 $root = $1;
177 }
178 }
179
180 if (!defined $root) {
181 print "Must be run from the top-level dir. of a kernel tree\n";
182 exit(2);
183 }
184}
185
186my $emitted_corrupt = 0;
187
188our $Ident = qr{
189 [A-Za-z_][A-Za-z\d_]*
190 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
191 }x;
192our $Storage = qr{extern|static|asmlinkage};
193our $Sparse = qr{
Paolo Bonzinif1e155b2015-08-16 23:01:19 +0200194 __force
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000195 }x;
196
197# Notes to $Attribute:
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000198our $Attribute = qr{
199 const|
Paolo Bonzini71c47b02015-08-16 23:15:46 +0200200 volatile|
201 QEMU_NORETURN|
202 QEMU_WARN_UNUSED_RESULT|
203 QEMU_SENTINEL|
204 QEMU_ARTIFICIAL|
205 QEMU_PACKED|
206 GCC_FMT_ATTR
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000207 }x;
208our $Modifier;
Paolo Bonzini71c47b02015-08-16 23:15:46 +0200209our $Inline = qr{inline};
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000210our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
211our $Lval = qr{$Ident(?:$Member)*};
212
213our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
214our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
215our $Compare = qr{<=|>=|==|!=|<|>};
216our $Operators = qr{
217 <=|>=|==|!=|
218 =>|->|<<|>>|<|>|!|~|
219 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
220 }x;
221
222our $NonptrType;
223our $Type;
224our $Declare;
225
226our $UTF8 = qr {
227 [\x09\x0A\x0D\x20-\x7E] # ASCII
228 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
229 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
230 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
231 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
232 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
233 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
234 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
235}x;
236
Paolo Bonzinia6859de2014-06-10 10:52:02 +0200237# There are still some false positives, but this catches most
238# common cases.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000239our $typeTypedefs = qr{(?x:
Paolo Bonzinia6859de2014-06-10 10:52:02 +0200240 [A-Z][A-Z\d_]*[a-z][A-Za-z\d_]* # camelcase
241 | [A-Z][A-Z\d_]*AIOCB # all uppercase
242 | [A-Z][A-Z\d_]*CPU # all uppercase
243 | QEMUBH # all uppercase
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000244)};
245
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000246our @typeList = (
247 qr{void},
248 qr{(?:unsigned\s+)?char},
249 qr{(?:unsigned\s+)?short},
250 qr{(?:unsigned\s+)?int},
251 qr{(?:unsigned\s+)?long},
252 qr{(?:unsigned\s+)?long\s+int},
253 qr{(?:unsigned\s+)?long\s+long},
254 qr{(?:unsigned\s+)?long\s+long\s+int},
255 qr{unsigned},
256 qr{float},
257 qr{double},
258 qr{bool},
259 qr{struct\s+$Ident},
260 qr{union\s+$Ident},
261 qr{enum\s+$Ident},
262 qr{${Ident}_t},
263 qr{${Ident}_handler},
264 qr{${Ident}_handler_fn},
Cédric Le Goaterf0707d22016-04-01 11:40:06 +0200265 qr{target_(?:u)?long},
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000266);
Paolo Bonzinif1e155b2015-08-16 23:01:19 +0200267
268# This can be modified by sub possible. Since it can be empty, be careful
269# about regexes that always match, because they can cause infinite loops.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000270our @modifierList = (
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000271);
272
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000273sub build_types {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000274 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
Paolo Bonzinif1e155b2015-08-16 23:01:19 +0200275 if (@modifierList > 0) {
276 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
277 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
278 } else {
279 $Modifier = qr{(?:$Attribute|$Sparse)};
280 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000281 $NonptrType = qr{
282 (?:$Modifier\s+|const\s+)*
283 (?:
284 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
285 (?:$typeTypedefs\b)|
286 (?:${all}\b)
287 )
288 (?:\s+$Modifier|\s+const)*
289 }x;
290 $Type = qr{
291 $NonptrType
292 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
293 (?:\s+$Inline|\s+$Modifier)*
294 }x;
295 $Declare = qr{(?:$Storage\s+)?$Type};
296}
297build_types();
298
299$chk_signoff = 0 if ($file);
300
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000301my @rawlines = ();
302my @lines = ();
303my $vname;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100304if ($chk_branch) {
305 my @patches;
306 my $HASH;
307 open($HASH, "-|", "git", "log", "--format=%H", $ARGV[0]) ||
308 die "$P: git log --format=%H $ARGV[0] failed - $!\n";
309
310 while (<$HASH>) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000311 chomp;
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100312 push @patches, $_;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000313 }
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100314
315 close $HASH;
316
317 die "$P: no revisions returned for revlist '$chk_branch'\n"
318 unless @patches;
319
320 for my $hash (@patches) {
321 my $FILE;
322 open($FILE, '-|', "git", "show", $hash) ||
323 die "$P: git show $hash - $!\n";
324 $vname = $hash;
325 while (<$FILE>) {
326 chomp;
327 push(@rawlines, $_);
328 }
329 close($FILE);
330 if (!process($hash)) {
331 $exit = 1;
332 }
333 @rawlines = ();
334 @lines = ();
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000335 }
Daniel P. Berrange8e1fe172017-09-13 10:10:00 +0100336} else {
337 for my $filename (@ARGV) {
338 my $FILE;
339 if ($file) {
340 open($FILE, '-|', "diff -u /dev/null $filename") ||
341 die "$P: $filename: diff failed - $!\n";
342 } elsif ($filename eq '-') {
343 open($FILE, '<&STDIN');
344 } else {
345 open($FILE, '<', "$filename") ||
346 die "$P: $filename: open failed - $!\n";
347 }
348 if ($filename eq '-') {
349 $vname = 'Your patch';
350 } else {
351 $vname = $filename;
352 }
353 while (<$FILE>) {
354 chomp;
355 push(@rawlines, $_);
356 }
357 close($FILE);
358 if (!process($filename)) {
359 $exit = 1;
360 }
361 @rawlines = ();
362 @lines = ();
363 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000364}
365
366exit($exit);
367
368sub top_of_kernel_tree {
369 my ($root) = @_;
370
371 my @tree_check = (
Blue Swirlb6469682011-01-20 20:58:56 +0000372 "COPYING", "MAINTAINERS", "Makefile",
373 "README", "docs", "VERSION",
374 "vl.c"
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000375 );
376
377 foreach my $check (@tree_check) {
378 if (! -e $root . '/' . $check) {
379 return 0;
380 }
381 }
382 return 1;
383}
384
385sub expand_tabs {
386 my ($str) = @_;
387
388 my $res = '';
389 my $n = 0;
390 for my $c (split(//, $str)) {
391 if ($c eq "\t") {
392 $res .= ' ';
393 $n++;
394 for (; ($n % 8) != 0; $n++) {
395 $res .= ' ';
396 }
397 next;
398 }
399 $res .= $c;
400 $n++;
401 }
402
403 return $res;
404}
405sub copy_spacing {
406 (my $res = shift) =~ tr/\t/ /c;
407 return $res;
408}
409
410sub line_stats {
411 my ($line) = @_;
412
413 # Drop the diff line leader and expand tabs
414 $line =~ s/^.//;
415 $line = expand_tabs($line);
416
417 # Pick the indent from the front of the line.
418 my ($white) = ($line =~ /^(\s*)/);
419
420 return (length($line), length($white));
421}
422
423my $sanitise_quote = '';
424
425sub sanitise_line_reset {
426 my ($in_comment) = @_;
427
428 if ($in_comment) {
429 $sanitise_quote = '*/';
430 } else {
431 $sanitise_quote = '';
432 }
433}
434sub sanitise_line {
435 my ($line) = @_;
436
437 my $res = '';
438 my $l = '';
439
440 my $qlen = 0;
441 my $off = 0;
442 my $c;
443
444 # Always copy over the diff marker.
445 $res = substr($line, 0, 1);
446
447 for ($off = 1; $off < length($line); $off++) {
448 $c = substr($line, $off, 1);
449
Stefan Weilcb8d4c82016-03-23 15:59:57 +0100450 # Comments we are wacking completely including the begin
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000451 # and end, all to $;.
452 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
453 $sanitise_quote = '*/';
454
455 substr($res, $off, 2, "$;$;");
456 $off++;
457 next;
458 }
459 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
460 $sanitise_quote = '';
461 substr($res, $off, 2, "$;$;");
462 $off++;
463 next;
464 }
465 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
466 $sanitise_quote = '//';
467
468 substr($res, $off, 2, $sanitise_quote);
469 $off++;
470 next;
471 }
472
473 # A \ in a string means ignore the next character.
474 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
475 $c eq "\\") {
476 substr($res, $off, 2, 'XX');
477 $off++;
478 next;
479 }
480 # Regular quotes.
481 if ($c eq "'" || $c eq '"') {
482 if ($sanitise_quote eq '') {
483 $sanitise_quote = $c;
484
485 substr($res, $off, 1, $c);
486 next;
487 } elsif ($sanitise_quote eq $c) {
488 $sanitise_quote = '';
489 }
490 }
491
492 #print "c<$c> SQ<$sanitise_quote>\n";
493 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
494 substr($res, $off, 1, $;);
495 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
496 substr($res, $off, 1, $;);
497 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
498 substr($res, $off, 1, 'X');
499 } else {
500 substr($res, $off, 1, $c);
501 }
502 }
503
504 if ($sanitise_quote eq '//') {
505 $sanitise_quote = '';
506 }
507
508 # The pathname on a #include may be surrounded by '<' and '>'.
509 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
510 my $clean = 'X' x length($1);
511 $res =~ s@\<.*\>@<$clean>@;
512
513 # The whole of a #error is a string.
514 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
515 my $clean = 'X' x length($1);
516 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
517 }
518
519 return $res;
520}
521
522sub ctx_statement_block {
523 my ($linenr, $remain, $off) = @_;
524 my $line = $linenr - 1;
525 my $blk = '';
526 my $soff = $off;
527 my $coff = $off - 1;
528 my $coff_set = 0;
529
530 my $loff = 0;
531
532 my $type = '';
533 my $level = 0;
534 my @stack = ();
535 my $p;
536 my $c;
537 my $len = 0;
538
539 my $remainder;
540 while (1) {
541 @stack = (['', 0]) if ($#stack == -1);
542
543 #warn "CSB: blk<$blk> remain<$remain>\n";
544 # If we are about to drop off the end, pull in more
545 # context.
546 if ($off >= $len) {
547 for (; $remain > 0; $line++) {
548 last if (!defined $lines[$line]);
549 next if ($lines[$line] =~ /^-/);
550 $remain--;
551 $loff = $len;
552 $blk .= $lines[$line] . "\n";
553 $len = length($blk);
554 $line++;
555 last;
556 }
557 # Bail if there is no further context.
558 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
559 if ($off >= $len) {
560 last;
561 }
562 }
563 $p = $c;
564 $c = substr($blk, $off, 1);
565 $remainder = substr($blk, $off);
566
567 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
568
569 # Handle nested #if/#else.
570 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
571 push(@stack, [ $type, $level ]);
572 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
573 ($type, $level) = @{$stack[$#stack - 1]};
574 } elsif ($remainder =~ /^#\s*endif\b/) {
575 ($type, $level) = @{pop(@stack)};
576 }
577
578 # Statement ends at the ';' or a close '}' at the
579 # outermost level.
580 if ($level == 0 && $c eq ';') {
581 last;
582 }
583
584 # An else is really a conditional as long as its not else if
585 if ($level == 0 && $coff_set == 0 &&
586 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
587 $remainder =~ /^(else)(?:\s|{)/ &&
588 $remainder !~ /^else\s+if\b/) {
589 $coff = $off + length($1) - 1;
590 $coff_set = 1;
591 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
592 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
593 }
594
595 if (($type eq '' || $type eq '(') && $c eq '(') {
596 $level++;
597 $type = '(';
598 }
599 if ($type eq '(' && $c eq ')') {
600 $level--;
601 $type = ($level != 0)? '(' : '';
602
603 if ($level == 0 && $coff < $soff) {
604 $coff = $off;
605 $coff_set = 1;
606 #warn "CSB: mark coff<$coff>\n";
607 }
608 }
609 if (($type eq '' || $type eq '{') && $c eq '{') {
610 $level++;
611 $type = '{';
612 }
613 if ($type eq '{' && $c eq '}') {
614 $level--;
615 $type = ($level != 0)? '{' : '';
616
617 if ($level == 0) {
618 if (substr($blk, $off + 1, 1) eq ';') {
619 $off++;
620 }
621 last;
622 }
623 }
624 $off++;
625 }
626 # We are truly at the end, so shuffle to the next line.
627 if ($off == $len) {
628 $loff = $len + 1;
629 $line++;
630 $remain--;
631 }
632
633 my $statement = substr($blk, $soff, $off - $soff + 1);
634 my $condition = substr($blk, $soff, $coff - $soff + 1);
635
636 #warn "STATEMENT<$statement>\n";
637 #warn "CONDITION<$condition>\n";
638
639 #print "coff<$coff> soff<$off> loff<$loff>\n";
640
641 return ($statement, $condition,
642 $line, $remain + 1, $off - $loff + 1, $level);
643}
644
645sub statement_lines {
646 my ($stmt) = @_;
647
648 # Strip the diff line prefixes and rip blank lines at start and end.
649 $stmt =~ s/(^|\n)./$1/g;
650 $stmt =~ s/^\s*//;
651 $stmt =~ s/\s*$//;
652
653 my @stmt_lines = ($stmt =~ /\n/g);
654
655 return $#stmt_lines + 2;
656}
657
658sub statement_rawlines {
659 my ($stmt) = @_;
660
661 my @stmt_lines = ($stmt =~ /\n/g);
662
663 return $#stmt_lines + 2;
664}
665
666sub statement_block_size {
667 my ($stmt) = @_;
668
669 $stmt =~ s/(^|\n)./$1/g;
Fam Zheng04f25622015-09-11 19:07:36 +0800670 $stmt =~ s/^\s*\{//;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000671 $stmt =~ s/}\s*$//;
672 $stmt =~ s/^\s*//;
673 $stmt =~ s/\s*$//;
674
675 my @stmt_lines = ($stmt =~ /\n/g);
676 my @stmt_statements = ($stmt =~ /;/g);
677
678 my $stmt_lines = $#stmt_lines + 2;
679 my $stmt_statements = $#stmt_statements + 1;
680
681 if ($stmt_lines > $stmt_statements) {
682 return $stmt_lines;
683 } else {
684 return $stmt_statements;
685 }
686}
687
688sub ctx_statement_full {
689 my ($linenr, $remain, $off) = @_;
690 my ($statement, $condition, $level);
691
692 my (@chunks);
693
694 # Grab the first conditional/block pair.
695 ($statement, $condition, $linenr, $remain, $off, $level) =
696 ctx_statement_block($linenr, $remain, $off);
697 #print "F: c<$condition> s<$statement> remain<$remain>\n";
698 push(@chunks, [ $condition, $statement ]);
699 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
700 return ($level, $linenr, @chunks);
701 }
702
703 # Pull in the following conditional/block pairs and see if they
704 # could continue the statement.
705 for (;;) {
706 ($statement, $condition, $linenr, $remain, $off, $level) =
707 ctx_statement_block($linenr, $remain, $off);
708 #print "C: c<$condition> s<$statement> remain<$remain>\n";
709 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
710 #print "C: push\n";
711 push(@chunks, [ $condition, $statement ]);
712 }
713
714 return ($level, $linenr, @chunks);
715}
716
717sub ctx_block_get {
718 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
719 my $line;
720 my $start = $linenr - 1;
721 my $blk = '';
722 my @o;
723 my @c;
724 my @res = ();
725
726 my $level = 0;
727 my @stack = ($level);
728 for ($line = $start; $remain > 0; $line++) {
729 next if ($rawlines[$line] =~ /^-/);
730 $remain--;
731
732 $blk .= $rawlines[$line];
733
734 # Handle nested #if/#else.
735 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
736 push(@stack, $level);
737 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
738 $level = $stack[$#stack - 1];
739 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
740 $level = pop(@stack);
741 }
742
743 foreach my $c (split(//, $lines[$line])) {
744 ##print "C<$c>L<$level><$open$close>O<$off>\n";
745 if ($off > 0) {
746 $off--;
747 next;
748 }
749
750 if ($c eq $close && $level > 0) {
751 $level--;
752 last if ($level == 0);
753 } elsif ($c eq $open) {
754 $level++;
755 }
756 }
757
758 if (!$outer || $level <= 1) {
759 push(@res, $rawlines[$line]);
760 }
761
762 last if ($level == 0);
763 }
764
765 return ($level, @res);
766}
767sub ctx_block_outer {
768 my ($linenr, $remain) = @_;
769
770 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
771 return @r;
772}
773sub ctx_block {
774 my ($linenr, $remain) = @_;
775
776 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
777 return @r;
778}
779sub ctx_statement {
780 my ($linenr, $remain, $off) = @_;
781
782 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
783 return @r;
784}
785sub ctx_block_level {
786 my ($linenr, $remain) = @_;
787
788 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
789}
790sub ctx_statement_level {
791 my ($linenr, $remain, $off) = @_;
792
793 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
794}
795
796sub ctx_locate_comment {
797 my ($first_line, $end_line) = @_;
798
799 # Catch a comment on the end of the line itself.
800 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
801 return $current_comment if (defined $current_comment);
802
803 # Look through the context and try and figure out if there is a
804 # comment.
805 my $in_comment = 0;
806 $current_comment = '';
807 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
808 my $line = $rawlines[$linenr - 1];
809 #warn " $line\n";
810 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
811 $in_comment = 1;
812 }
813 if ($line =~ m@/\*@) {
814 $in_comment = 1;
815 }
816 if (!$in_comment && $current_comment ne '') {
817 $current_comment = '';
818 }
819 $current_comment .= $line . "\n" if ($in_comment);
820 if ($line =~ m@\*/@) {
821 $in_comment = 0;
822 }
823 }
824
825 chomp($current_comment);
826 return($current_comment);
827}
828sub ctx_has_comment {
829 my ($first_line, $end_line) = @_;
830 my $cmt = ctx_locate_comment($first_line, $end_line);
831
832 ##print "LINE: $rawlines[$end_line - 1 ]\n";
833 ##print "CMMT: $cmt\n";
834
835 return ($cmt ne '');
836}
837
838sub raw_line {
839 my ($linenr, $cnt) = @_;
840
841 my $offset = $linenr - 1;
842 $cnt++;
843
844 my $line;
845 while ($cnt) {
846 $line = $rawlines[$offset++];
847 next if (defined($line) && $line =~ /^-/);
848 $cnt--;
849 }
850
851 return $line;
852}
853
854sub cat_vet {
855 my ($vet) = @_;
856 my ($res, $coded);
857
858 $res = '';
859 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
860 $res .= $1;
861 if ($2 ne '') {
862 $coded = sprintf("^%c", unpack('C', $2) + 64);
863 $res .= $coded;
864 }
865 }
866 $res =~ s/$/\$/;
867
868 return $res;
869}
870
871my $av_preprocessor = 0;
872my $av_pending;
873my @av_paren_type;
874my $av_pend_colon;
875
876sub annotate_reset {
877 $av_preprocessor = 0;
878 $av_pending = '_';
879 @av_paren_type = ('E');
880 $av_pend_colon = 'O';
881}
882
883sub annotate_values {
884 my ($stream, $type) = @_;
885
886 my $res;
887 my $var = '_' x length($stream);
888 my $cur = $stream;
889
890 print "$stream\n" if ($dbg_values > 1);
891
892 while (length($cur)) {
893 @av_paren_type = ('E') if ($#av_paren_type < 0);
894 print " <" . join('', @av_paren_type) .
895 "> <$type> <$av_pending>" if ($dbg_values > 1);
896 if ($cur =~ /^(\s+)/o) {
897 print "WS($1)\n" if ($dbg_values > 1);
898 if ($1 =~ /\n/ && $av_preprocessor) {
899 $type = pop(@av_paren_type);
900 $av_preprocessor = 0;
901 }
902
Florian Mickler61669f92011-11-25 10:24:16 +0100903 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +0000904 print "CAST($1)\n" if ($dbg_values > 1);
905 push(@av_paren_type, $type);
906 $type = 'C';
907
908 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
909 print "DECLARE($1)\n" if ($dbg_values > 1);
910 $type = 'T';
911
912 } elsif ($cur =~ /^($Modifier)\s*/) {
913 print "MODIFIER($1)\n" if ($dbg_values > 1);
914 $type = 'T';
915
916 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
917 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
918 $av_preprocessor = 1;
919 push(@av_paren_type, $type);
920 if ($2 ne '') {
921 $av_pending = 'N';
922 }
923 $type = 'E';
924
925 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
926 print "UNDEF($1)\n" if ($dbg_values > 1);
927 $av_preprocessor = 1;
928 push(@av_paren_type, $type);
929
930 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
931 print "PRE_START($1)\n" if ($dbg_values > 1);
932 $av_preprocessor = 1;
933
934 push(@av_paren_type, $type);
935 push(@av_paren_type, $type);
936 $type = 'E';
937
938 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
939 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
940 $av_preprocessor = 1;
941
942 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
943
944 $type = 'E';
945
946 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
947 print "PRE_END($1)\n" if ($dbg_values > 1);
948
949 $av_preprocessor = 1;
950
951 # Assume all arms of the conditional end as this
952 # one does, and continue as if the #endif was not here.
953 pop(@av_paren_type);
954 push(@av_paren_type, $type);
955 $type = 'E';
956
957 } elsif ($cur =~ /^(\\\n)/o) {
958 print "PRECONT($1)\n" if ($dbg_values > 1);
959
960 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
961 print "ATTR($1)\n" if ($dbg_values > 1);
962 $av_pending = $type;
963 $type = 'N';
964
965 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
966 print "SIZEOF($1)\n" if ($dbg_values > 1);
967 if (defined $2) {
968 $av_pending = 'V';
969 }
970 $type = 'N';
971
972 } elsif ($cur =~ /^(if|while|for)\b/o) {
973 print "COND($1)\n" if ($dbg_values > 1);
974 $av_pending = 'E';
975 $type = 'N';
976
977 } elsif ($cur =~/^(case)/o) {
978 print "CASE($1)\n" if ($dbg_values > 1);
979 $av_pend_colon = 'C';
980 $type = 'N';
981
982 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
983 print "KEYWORD($1)\n" if ($dbg_values > 1);
984 $type = 'N';
985
986 } elsif ($cur =~ /^(\()/o) {
987 print "PAREN('$1')\n" if ($dbg_values > 1);
988 push(@av_paren_type, $av_pending);
989 $av_pending = '_';
990 $type = 'N';
991
992 } elsif ($cur =~ /^(\))/o) {
993 my $new_type = pop(@av_paren_type);
994 if ($new_type ne '_') {
995 $type = $new_type;
996 print "PAREN('$1') -> $type\n"
997 if ($dbg_values > 1);
998 } else {
999 print "PAREN('$1')\n" if ($dbg_values > 1);
1000 }
1001
1002 } elsif ($cur =~ /^($Ident)\s*\(/o) {
1003 print "FUNC($1)\n" if ($dbg_values > 1);
1004 $type = 'V';
1005 $av_pending = 'V';
1006
1007 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1008 if (defined $2 && $type eq 'C' || $type eq 'T') {
1009 $av_pend_colon = 'B';
1010 } elsif ($type eq 'E') {
1011 $av_pend_colon = 'L';
1012 }
1013 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1014 $type = 'V';
1015
1016 } elsif ($cur =~ /^($Ident|$Constant)/o) {
1017 print "IDENT($1)\n" if ($dbg_values > 1);
1018 $type = 'V';
1019
1020 } elsif ($cur =~ /^($Assignment)/o) {
1021 print "ASSIGN($1)\n" if ($dbg_values > 1);
1022 $type = 'N';
1023
1024 } elsif ($cur =~/^(;|{|})/) {
1025 print "END($1)\n" if ($dbg_values > 1);
1026 $type = 'E';
1027 $av_pend_colon = 'O';
1028
1029 } elsif ($cur =~/^(,)/) {
1030 print "COMMA($1)\n" if ($dbg_values > 1);
1031 $type = 'C';
1032
1033 } elsif ($cur =~ /^(\?)/o) {
1034 print "QUESTION($1)\n" if ($dbg_values > 1);
1035 $type = 'N';
1036
1037 } elsif ($cur =~ /^(:)/o) {
1038 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1039
1040 substr($var, length($res), 1, $av_pend_colon);
1041 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1042 $type = 'E';
1043 } else {
1044 $type = 'N';
1045 }
1046 $av_pend_colon = 'O';
1047
1048 } elsif ($cur =~ /^(\[)/o) {
1049 print "CLOSE($1)\n" if ($dbg_values > 1);
1050 $type = 'N';
1051
1052 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
1053 my $variant;
1054
1055 print "OPV($1)\n" if ($dbg_values > 1);
1056 if ($type eq 'V') {
1057 $variant = 'B';
1058 } else {
1059 $variant = 'U';
1060 }
1061
1062 substr($var, length($res), 1, $variant);
1063 $type = 'N';
1064
1065 } elsif ($cur =~ /^($Operators)/o) {
1066 print "OP($1)\n" if ($dbg_values > 1);
1067 if ($1 ne '++' && $1 ne '--') {
1068 $type = 'N';
1069 }
1070
1071 } elsif ($cur =~ /(^.)/o) {
1072 print "C($1)\n" if ($dbg_values > 1);
1073 }
1074 if (defined $1) {
1075 $cur = substr($cur, length($1));
1076 $res .= $type x length($1);
1077 }
1078 }
1079
1080 return ($res, $var);
1081}
1082
1083sub possible {
1084 my ($possible, $line) = @_;
1085 my $notPermitted = qr{(?:
1086 ^(?:
1087 $Modifier|
1088 $Storage|
1089 $Type|
1090 DEFINE_\S+
1091 )$|
1092 ^(?:
1093 goto|
1094 return|
1095 case|
1096 else|
1097 asm|__asm__|
Andy Whitcroft3e5385f2015-10-08 10:05:24 +02001098 do|
1099 \#|
1100 \#\#
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001101 )(?:\s|$)|
1102 ^(?:typedef|struct|enum)\b
1103 )}x;
1104 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1105 if ($possible !~ $notPermitted) {
1106 # Check for modifiers.
1107 $possible =~ s/\s*$Storage\s*//g;
1108 $possible =~ s/\s*$Sparse\s*//g;
1109 if ($possible =~ /^\s*$/) {
1110
1111 } elsif ($possible =~ /\s/) {
1112 $possible =~ s/\s*$Type\s*//g;
1113 for my $modifier (split(' ', $possible)) {
1114 if ($modifier !~ $notPermitted) {
1115 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1116 push(@modifierList, $modifier);
1117 }
1118 }
1119
1120 } else {
1121 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1122 push(@typeList, $possible);
1123 }
1124 build_types();
1125 } else {
1126 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
1127 }
1128}
1129
1130my $prefix = '';
1131
1132sub report {
1133 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
1134 return 0;
1135 }
1136 my $line = $prefix . $_[0];
1137
1138 $line = (split('\n', $line))[0] . "\n" if ($terse);
1139
1140 push(our @report, $line);
1141
1142 return 1;
1143}
1144sub report_dump {
1145 our @report;
1146}
1147sub ERROR {
1148 if (report("ERROR: $_[0]\n")) {
1149 our $clean = 0;
1150 our $cnt_error++;
1151 }
1152}
1153sub WARN {
1154 if (report("WARNING: $_[0]\n")) {
1155 our $clean = 0;
1156 our $cnt_warn++;
1157 }
1158}
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001159
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001160sub process {
1161 my $filename = shift;
1162
1163 my $linenr=0;
1164 my $prevline="";
1165 my $prevrawline="";
1166 my $stashline="";
1167 my $stashrawline="";
1168
1169 my $length;
1170 my $indent;
1171 my $previndent=0;
1172 my $stashindent=0;
1173
1174 our $clean = 1;
1175 my $signoff = 0;
1176 my $is_patch = 0;
1177
1178 our @report = ();
1179 our $cnt_lines = 0;
1180 our $cnt_error = 0;
1181 our $cnt_warn = 0;
1182 our $cnt_chk = 0;
1183
1184 # Trace the real file/line as we go.
1185 my $realfile = '';
1186 my $realline = 0;
1187 my $realcnt = 0;
1188 my $here = '';
1189 my $in_comment = 0;
1190 my $comment_edge = 0;
1191 my $first_line = 0;
1192 my $p1_prefix = '';
1193
1194 my $prev_values = 'E';
1195
1196 # suppression flags
1197 my %suppress_ifbraces;
1198 my %suppress_whiletrailers;
1199 my %suppress_export;
1200
1201 # Pre-scan the patch sanitizing the lines.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001202
1203 sanitise_line_reset();
1204 my $line;
1205 foreach my $rawline (@rawlines) {
1206 $linenr++;
1207 $line = $rawline;
1208
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001209 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1210 $realline=$1-1;
1211 if (defined $2) {
1212 $realcnt=$3+1;
1213 } else {
1214 $realcnt=1+1;
1215 }
1216 $in_comment = 0;
1217
1218 # Guestimate if this is a continuing comment. Run
1219 # the context looking for a comment "edge". If this
1220 # edge is a close comment then we must be in a comment
1221 # at context start.
1222 my $edge;
1223 my $cnt = $realcnt;
1224 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1225 next if (defined $rawlines[$ln - 1] &&
1226 $rawlines[$ln - 1] =~ /^-/);
1227 $cnt--;
1228 #print "RAW<$rawlines[$ln - 1]>\n";
1229 last if (!defined $rawlines[$ln - 1]);
1230 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1231 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1232 ($edge) = $1;
1233 last;
1234 }
1235 }
1236 if (defined $edge && $edge eq '*/') {
1237 $in_comment = 1;
1238 }
1239
1240 # Guestimate if this is a continuing comment. If this
1241 # is the start of a diff block and this line starts
1242 # ' *' then it is very likely a comment.
1243 if (!defined $edge &&
1244 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1245 {
1246 $in_comment = 1;
1247 }
1248
1249 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1250 sanitise_line_reset($in_comment);
1251
1252 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1253 # Standardise the strings and chars within the input to
1254 # simplify matching -- only bother with positive lines.
1255 $line = sanitise_line($rawline);
1256 }
1257 push(@lines, $line);
1258
1259 if ($realcnt > 1) {
1260 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1261 } else {
1262 $realcnt = 0;
1263 }
1264
1265 #print "==>$rawline\n";
1266 #print "-->$line\n";
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001267 }
1268
1269 $prefix = '';
1270
1271 $realcnt = 0;
1272 $linenr = 0;
1273 foreach my $line (@lines) {
1274 $linenr++;
1275
1276 my $rawline = $rawlines[$linenr - 1];
1277
1278#extract the line range in the file after the patch is applied
1279 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1280 $is_patch = 1;
1281 $first_line = $linenr + 1;
1282 $realline=$1-1;
1283 if (defined $2) {
1284 $realcnt=$3+1;
1285 } else {
1286 $realcnt=1+1;
1287 }
1288 annotate_reset();
1289 $prev_values = 'E';
1290
1291 %suppress_ifbraces = ();
1292 %suppress_whiletrailers = ();
1293 %suppress_export = ();
1294 next;
1295
1296# track the line number as we move through the hunk, note that
1297# new versions of GNU diff omit the leading space on completely
1298# blank context lines so we need to count that too.
1299 } elsif ($line =~ /^( |\+|$)/) {
1300 $realline++;
1301 $realcnt-- if ($realcnt != 0);
1302
1303 # Measure the line length and indent.
1304 ($length, $indent) = line_stats($rawline);
1305
1306 # Track the previous line.
1307 ($prevline, $stashline) = ($stashline, $line);
1308 ($previndent, $stashindent) = ($stashindent, $indent);
1309 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1310
1311 #warn "line<$line>\n";
1312
1313 } elsif ($realcnt == 1) {
1314 $realcnt--;
1315 }
1316
1317 my $hunk_line = ($realcnt != 0);
1318
1319#make up the handle for any error we report on this line
1320 $prefix = "$filename:$realline: " if ($emacs && $file);
1321 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1322
1323 $here = "#$linenr: " if (!$file);
1324 $here = "#$realline: " if ($file);
1325
1326 # extract the filename as it passes
1327 if ($line =~ /^diff --git.*?(\S+)$/) {
1328 $realfile = $1;
1329 $realfile =~ s@^([^/]*)/@@;
1330
1331 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1332 $realfile = $1;
1333 $realfile =~ s@^([^/]*)/@@;
1334
1335 $p1_prefix = $1;
1336 if (!$file && $tree && $p1_prefix ne '' &&
1337 -e "$root/$p1_prefix") {
1338 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1339 }
1340
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001341 next;
1342 }
1343
1344 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1345
1346 my $hereline = "$here\n$rawline\n";
1347 my $herecurr = "$here\n$rawline\n";
1348 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1349
1350 $cnt_lines++ if ($realcnt != 0);
1351
1352# Check for incorrect file permissions
1353 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
1354 my $permhere = $here . "FILE: $realfile\n";
Paolo Bonzini71c47b02015-08-16 23:15:46 +02001355 if ($realfile =~ /(\bMakefile(?:\.objs)?|\.c|\.cc|\.cpp|\.h|\.mak|\.[sS])$/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001356 ERROR("do not set execute permissions for source files\n" . $permhere);
1357 }
1358 }
1359
Stefan Hajnoczif8dccbb2016-07-15 10:46:54 +01001360# Accept git diff extended headers as valid patches
1361 if ($line =~ /^(?:rename|copy) (?:from|to) [\w\/\.\-]+\s*$/) {
1362 $is_patch = 1;
1363 }
1364
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001365#check the patch for a signoff:
1366 if ($line =~ /^\s*signed-off-by:/i) {
1367 # This is a signoff, if ugly, so do not double report.
1368 $signoff++;
1369 if (!($line =~ /^\s*Signed-off-by:/)) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001370 ERROR("The correct form is \"Signed-off-by\"\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001371 $herecurr);
1372 }
1373 if ($line =~ /^\s*signed-off-by:\S/i) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001374 ERROR("space required after Signed-off-by:\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001375 $herecurr);
1376 }
1377 }
1378
1379# Check for wrappage within a valid hunk of the file
1380 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1381 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1382 $herecurr) if (!$emitted_corrupt++);
1383 }
1384
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001385# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1386 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1387 $rawline !~ m/^$UTF8*$/) {
1388 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1389
1390 my $blank = copy_spacing($rawline);
1391 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1392 my $hereptr = "$hereline$ptr\n";
1393
1394 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1395 }
1396
1397# ignore non-hunk lines and lines being removed
1398 next if (!$hunk_line || $line =~ /^-/);
1399
Radim Krčmář93bf13c2016-08-09 19:38:41 +02001400# ignore files that are being periodically imported from Linux
1401 next if ($realfile =~ /^(linux-headers|include\/standard-headers)\//);
1402
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001403#trailing whitespace
1404 if ($line =~ /^\+.*\015/) {
1405 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1406 ERROR("DOS line endings\n" . $herevet);
1407
Lluís Vilanova0cebabd2016-09-07 14:49:04 +02001408 } elsif ($realfile =~ /^docs\/.+\.txt/ ||
1409 $realfile =~ /^docs\/.+\.md/) {
1410 if ($rawline =~ /^\+\s+$/ && $rawline !~ /^\+ {4}$/) {
1411 # TODO: properly check we're in a code block
1412 # (surrounding text is 4-column aligned)
1413 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1414 ERROR("code blocks in documentation should have " .
1415 "empty lines with exactly 4 columns of " .
1416 "whitespace\n" . $herevet);
1417 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001418 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1419 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1420 ERROR("trailing whitespace\n" . $herevet);
1421 $rpt_cleaners = 1;
1422 }
1423
Vladimir Sementsov-Ogievskiyc3e58752017-07-31 19:01:34 +03001424# checks for trace-events files
1425 if ($realfile =~ /trace-events$/ && $line =~ /^\+/) {
1426 if ($rawline =~ /%[-+ 0]*#/) {
1427 ERROR("Don't use '#' flag of printf format ('%#') in " .
1428 "trace-events, use '0x' prefix instead\n" . $herecurr);
1429 } else {
1430 my $hex =
1431 qr/%[-+ *.0-9]*([hljztL]|ll|hh)?(x|X|"\s*PRI[xX][^"]*"?)/;
1432
1433 # don't consider groups splitted by [.:/ ], like 2A.20:12ab
1434 my $tmpline = $rawline =~ s/($hex[.:\/ ])+$hex//gr;
1435
1436 if ($tmpline =~ /(?<!0x)$hex/) {
1437 ERROR("Hex numbers must be prefixed with '0x'\n" .
1438 $herecurr);
1439 }
1440 }
1441 }
1442
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001443# check we are in a valid source file if not then ignore this hunk
Paolo Bonzini906fb132016-08-09 17:47:42 +02001444 next if ($realfile !~ /\.(h|c|cpp|s|S|pl|py|sh)$/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001445
Paolo Bonzini8fbe3d12015-09-07 11:53:02 +02001446#90 column limit
Paolo Bonzinif1e155b2015-08-16 23:01:19 +02001447 if ($line =~ /^\+/ &&
1448 !($line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001449 $length > 80)
1450 {
Paolo Bonzini8fbe3d12015-09-07 11:53:02 +02001451 if ($length > 90) {
1452 ERROR("line over 90 characters\n" . $herecurr);
1453 } else {
1454 WARN("line over 80 characters\n" . $herecurr);
1455 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001456 }
1457
1458# check for spaces before a quoted newline
1459 if ($rawline =~ /^.*\".*\s\\n/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001460 ERROR("unnecessary whitespace before a quoted newline\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001461 }
1462
1463# check for adding lines without a newline.
1464 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001465 ERROR("adding a line without newline at end of file\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001466 }
1467
Paolo Bonzini93eb8e32016-08-10 10:05:03 +02001468# check for RCS/CVS revision markers
1469 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|\b)/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001470 ERROR("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Paolo Bonzini93eb8e32016-08-10 10:05:03 +02001471 }
1472
Paolo Bonzini906fb132016-08-09 17:47:42 +02001473# tabs are only allowed in assembly source code, and in
1474# some scripts we imported from other projects.
1475 next if ($realfile =~ /\.(s|S)$/);
1476 next if ($realfile =~ /(checkpatch|get_maintainer|texi2pod)\.pl$/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001477
Blue Swirlad36ce82011-02-05 13:18:20 +00001478 if ($rawline =~ /^\+.*\t/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001479 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Blue Swirlb6469682011-01-20 20:58:56 +00001480 ERROR("code indent should never use tabs\n" . $herevet);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001481 $rpt_cleaners = 1;
1482 }
1483
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001484# check we are in a valid C source file if not then ignore this hunk
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04001485 next if ($realfile !~ /\.(h|c|cpp)$/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001486
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001487# Check for potential 'bare' types
1488 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
1489 $realline_next);
1490 if ($realcnt && $line =~ /.\s*\S/) {
1491 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1492 ctx_statement_block($linenr, $realcnt, 0);
1493 $stat =~ s/\n./\n /g;
1494 $cond =~ s/\n./\n /g;
1495
1496 # Find the real next line.
1497 $realline_next = $line_nr_next;
1498 if (defined $realline_next &&
1499 (!defined $lines[$realline_next - 1] ||
1500 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
1501 $realline_next++;
1502 }
1503
1504 my $s = $stat;
1505 $s =~ s/{.*$//s;
1506
1507 # Ignore goto labels.
1508 if ($s =~ /$Ident:\*$/s) {
1509
1510 # Ignore functions being called
1511 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1512
1513 } elsif ($s =~ /^.\s*else\b/s) {
1514
1515 # declarations always start with types
1516 } 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) {
1517 my $type = $1;
1518 $type =~ s/\s+/ /g;
1519 possible($type, "A:" . $s);
1520
1521 # definitions in global scope can only start with types
1522 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1523 possible($1, "B:" . $s);
1524 }
1525
1526 # any (foo ... *) is a pointer cast, and foo is a type
1527 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1528 possible($1, "C:" . $s);
1529 }
1530
1531 # Check for any sort of function declaration.
1532 # int foo(something bar, other baz);
1533 # void (*store_gdt)(x86_descr_ptr *);
1534 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1535 my ($name_len) = length($1);
1536
1537 my $ctx = $s;
1538 substr($ctx, 0, $name_len + 1, '');
1539 $ctx =~ s/\)[^\)]*$//;
1540
1541 for my $arg (split(/\s*,\s*/, $ctx)) {
1542 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1543
1544 possible($1, "D:" . $s);
1545 }
1546 }
1547 }
1548
1549 }
1550
1551#
1552# Checks which may be anchored in the context.
1553#
1554
1555# Check for switch () and associated case and default
1556# statements should be at the same indent.
1557 if ($line=~/\bswitch\s*\(.*\)/) {
1558 my $err = '';
1559 my $sep = '';
1560 my @ctx = ctx_block_outer($linenr, $realcnt);
1561 shift(@ctx);
1562 for my $ctx (@ctx) {
1563 my ($clen, $cindent) = line_stats($ctx);
1564 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1565 $indent != $cindent) {
1566 $err .= "$sep$ctx\n";
1567 $sep = '';
1568 } else {
1569 $sep = "[...]\n";
1570 }
1571 }
1572 if ($err ne '') {
1573 ERROR("switch and case should be at the same indent\n$hereline$err");
1574 }
1575 }
1576
1577# if/while/etc brace do not go on next line, unless defining a do while loop,
1578# or if that brace on the next line is for something else
1579 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1580 my $pre_ctx = "$1$2";
1581
1582 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1583 my $ctx_cnt = $realcnt - $#ctx - 1;
1584 my $ctx = join("\n", @ctx);
1585
1586 my $ctx_ln = $linenr;
1587 my $ctx_skip = $realcnt;
1588
1589 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1590 defined $lines[$ctx_ln - 1] &&
1591 $lines[$ctx_ln - 1] =~ /^-/)) {
1592 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1593 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1594 $ctx_ln++;
1595 }
1596
1597 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1598 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1599
Max Reitza97ceca2014-11-26 17:20:24 +01001600 # The length of the "previous line" is checked against 80 because it
1601 # includes the + at the beginning of the line (if the actual line has
1602 # 79 or 80 characters, it is no longer possible to add a space and an
1603 # opening brace there)
1604 if ($#ctx == 0 && $ctx !~ /{\s*/ &&
Fam Zheng04f25622015-09-11 19:07:36 +08001605 defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*\{/ &&
Max Reitza97ceca2014-11-26 17:20:24 +01001606 defined($lines[$ctx_ln - 2]) && length($lines[$ctx_ln - 2]) < 80) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001607 ERROR("that open brace { should be on the previous line\n" .
1608 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1609 }
1610 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1611 $ctx =~ /\)\s*\;\s*$/ &&
1612 defined $lines[$ctx_ln - 1])
1613 {
1614 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1615 if ($nindent > $indent) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001616 ERROR("trailing semicolon indicates no statements, indent implies otherwise\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001617 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
1618 }
1619 }
1620 }
1621
1622# Check relative indent for conditionals and blocks.
1623 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1624 my ($s, $c) = ($stat, $cond);
1625
1626 substr($s, 0, length($c), '');
1627
1628 # Make sure we remove the line prefixes as we have
1629 # none on the first line, and are going to readd them
1630 # where necessary.
1631 $s =~ s/\n./\n/gs;
1632
1633 # Find out how long the conditional actually is.
1634 my @newlines = ($c =~ /\n/gs);
1635 my $cond_lines = 1 + $#newlines;
1636
1637 # We want to check the first line inside the block
1638 # starting at the end of the conditional, so remove:
1639 # 1) any blank line termination
1640 # 2) any opening brace { on end of the line
1641 # 3) any do (...) {
1642 my $continuation = 0;
1643 my $check = 0;
1644 $s =~ s/^.*\bdo\b//;
Fam Zheng04f25622015-09-11 19:07:36 +08001645 $s =~ s/^\s*\{//;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001646 if ($s =~ s/^\s*\\//) {
1647 $continuation = 1;
1648 }
1649 if ($s =~ s/^\s*?\n//) {
1650 $check = 1;
1651 $cond_lines++;
1652 }
1653
1654 # Also ignore a loop construct at the end of a
1655 # preprocessor statement.
1656 if (($prevline =~ /^.\s*#\s*define\s/ ||
1657 $prevline =~ /\\\s*$/) && $continuation == 0) {
1658 $check = 0;
1659 }
1660
1661 my $cond_ptr = -1;
1662 $continuation = 0;
1663 while ($cond_ptr != $cond_lines) {
1664 $cond_ptr = $cond_lines;
1665
1666 # If we see an #else/#elif then the code
1667 # is not linear.
1668 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1669 $check = 0;
1670 }
1671
1672 # Ignore:
1673 # 1) blank lines, they should be at 0,
1674 # 2) preprocessor lines, and
1675 # 3) labels.
1676 if ($continuation ||
1677 $s =~ /^\s*?\n/ ||
1678 $s =~ /^\s*#\s*?/ ||
1679 $s =~ /^\s*$Ident\s*:/) {
1680 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1681 if ($s =~ s/^.*?\n//) {
1682 $cond_lines++;
1683 }
1684 }
1685 }
1686
1687 my (undef, $sindent) = line_stats("+" . $s);
1688 my $stat_real = raw_line($linenr, $cond_lines);
1689
1690 # Check if either of these lines are modified, else
1691 # this is not this patch's fault.
1692 if (!defined($stat_real) ||
1693 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1694 $check = 0;
1695 }
1696 if (defined($stat_real) && $cond_lines > 1) {
1697 $stat_real = "[...]\n$stat_real";
1698 }
1699
1700 #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";
1701
Blue Swirlb6469682011-01-20 20:58:56 +00001702 if ($check && (($sindent % 4) != 0 ||
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001703 ($sindent <= $indent && $s ne ''))) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001704 ERROR("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001705 }
1706 }
1707
1708 # Track the 'values' across context and added lines.
1709 my $opline = $line; $opline =~ s/^./ /;
1710 my ($curr_values, $curr_vars) =
1711 annotate_values($opline . "\n", $prev_values);
1712 $curr_values = $prev_values . $curr_values;
1713 if ($dbg_values) {
1714 my $outline = $opline; $outline =~ s/\t/ /g;
1715 print "$linenr > .$outline\n";
1716 print "$linenr > $curr_values\n";
1717 print "$linenr > $curr_vars\n";
1718 }
1719 $prev_values = substr($curr_values, -1);
1720
1721#ignore lines not being added
1722 if ($line=~/^[^\+]/) {next;}
1723
1724# TEST: allow direct testing of the type matcher.
1725 if ($dbg_type) {
1726 if ($line =~ /^.\s*$Declare\s*$/) {
1727 ERROR("TEST: is type\n" . $herecurr);
1728 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1729 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1730 }
1731 next;
1732 }
1733# TEST: allow direct testing of the attribute matcher.
1734 if ($dbg_attr) {
1735 if ($line =~ /^.\s*$Modifier\s*$/) {
1736 ERROR("TEST: is attr\n" . $herecurr);
1737 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
1738 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1739 }
1740 next;
1741 }
1742
1743# check for initialisation to aggregates open brace on the next line
Fam Zheng04f25622015-09-11 19:07:36 +08001744 if ($line =~ /^.\s*\{/ &&
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001745 $prevline =~ /(?:^|[^=])=\s*$/) {
1746 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1747 }
1748
1749#
1750# Checks which are anchored on the added line.
1751#
1752
1753# check for malformed paths in #include statements (uses RAW line)
1754 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1755 my $path = $1;
1756 if ($path =~ m{//}) {
1757 ERROR("malformed #include filename\n" .
1758 $herecurr);
1759 }
1760 }
1761
1762# no C99 // comments
1763 if ($line =~ m{//}) {
1764 ERROR("do not use C99 // comments\n" . $herecurr);
1765 }
1766 # Remove C99 comments.
1767 $line =~ s@//.*@@;
1768 $opline =~ s@//.*@@;
1769
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001770# check for global initialisers.
1771 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1772 ERROR("do not initialise globals to 0 or NULL\n" .
1773 $herecurr);
1774 }
1775# check for static initialisers.
1776 if ($line =~ /\bstatic\s.*=\s*(0|NULL|false)\s*;/) {
1777 ERROR("do not initialise statics to 0 or NULL\n" .
1778 $herecurr);
1779 }
1780
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001781# * goes on variable not on type
1782 # (char*[ const])
1783 if ($line =~ m{\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\)}) {
1784 my ($from, $to) = ($1, $1);
1785
1786 # Should start with a space.
1787 $to =~ s/^(\S)/ $1/;
1788 # Should not end with a space.
1789 $to =~ s/\s+$//;
1790 # '*'s should not have spaces between.
1791 while ($to =~ s/\*\s+\*/\*\*/) {
1792 }
1793
1794 #print "from<$from> to<$to>\n";
1795 if ($from ne $to) {
1796 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1797 }
1798 } elsif ($line =~ m{\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident)}) {
1799 my ($from, $to, $ident) = ($1, $1, $2);
1800
1801 # Should start with a space.
1802 $to =~ s/^(\S)/ $1/;
1803 # Should not end with a space.
1804 $to =~ s/\s+$//;
1805 # '*'s should not have spaces between.
1806 while ($to =~ s/\*\s+\*/\*\*/) {
1807 }
1808 # Modifiers should have spaces.
1809 $to =~ s/(\b$Modifier$)/$1 /;
1810
1811 #print "from<$from> to<$to> ident<$ident>\n";
1812 if ($from ne $to && $ident !~ /^$Modifier$/) {
1813 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1814 }
1815 }
1816
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001817# function brace can't be on same line, except for #defines of do while,
1818# or if closed on same line
Fam Zheng04f25622015-09-11 19:07:36 +08001819 if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
1820 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001821 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1822 }
1823
1824# open braces for enum, union and struct go on the same line.
Fam Zheng04f25622015-09-11 19:07:36 +08001825 if ($line =~ /^.\s*\{/ &&
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001826 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1827 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1828 }
1829
1830# missing space after union, struct or enum definition
1831 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?(?:\s+$Ident)?[=\{]/) {
Paolo Bonzini71c47b02015-08-16 23:15:46 +02001832 ERROR("missing space after $1 definition\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001833 }
1834
1835# check for spacing round square brackets; allowed:
1836# 1. with a type on the left -- int [] a;
1837# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1838# 3. inside a curly brace -- = { [0...10] = 5 }
Leonid Bloch409db6e2015-10-29 11:48:37 +02001839# 4. after a comma -- [1] = 5, [2] = 6
Leonid Bloch8800cf02015-10-29 11:48:38 +02001840# 5. in a macro definition -- #define abc(x) [x] = y
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001841 while ($line =~ /(.*?\s)\[/g) {
1842 my ($where, $prefix) = ($-[1], $1);
1843 if ($prefix !~ /$Type\s+$/ &&
1844 ($where != 0 || $prefix !~ /^.\s+$/) &&
Leonid Bloch409db6e2015-10-29 11:48:37 +02001845 $prefix !~ /{\s+$/ &&
Leonid Bloch8800cf02015-10-29 11:48:38 +02001846 $prefix !~ /\#\s*define[^(]*\([^)]*\)\s+$/ &&
Leonid Bloch409db6e2015-10-29 11:48:37 +02001847 $prefix !~ /,\s+$/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001848 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1849 }
1850 }
1851
1852# check for spaces between functions and their parentheses.
1853 while ($line =~ /($Ident)\s+\(/g) {
1854 my $name = $1;
1855 my $ctx_before = substr($line, 0, $-[1]);
1856 my $ctx = "$ctx_before$name";
1857
1858 # Ignore those directives where spaces _are_ permitted.
1859 if ($name =~ /^(?:
1860 if|for|while|switch|return|case|
Jeff Cody000980c2016-11-01 10:38:35 -04001861 volatile|__volatile__|coroutine_fn|
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001862 __attribute__|format|__extension__|
1863 asm|__asm__)$/x)
1864 {
1865
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04001866 # Ignore 'catch (...)' in C++
1867 } elsif ($name =~ /^catch$/ && $realfile =~ /(\.cpp|\.h)$/) {
1868
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001869 # cpp #define statements have non-optional spaces, ie
1870 # if there is a space between the name and the open
1871 # parenthesis it is simply not a parameter group.
1872 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1873
1874 # cpp #elif statement condition may start with a (
1875 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1876
1877 # If this whole things ends with a type its most
1878 # likely a typedef for a function.
1879 } elsif ($ctx =~ /$Type$/) {
1880
1881 } else {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02001882 ERROR("space prohibited between function name and open parenthesis '('\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001883 }
1884 }
1885# Check operator spacing.
1886 if (!($line=~/\#\s*include/)) {
1887 my $ops = qr{
1888 <<=|>>=|<=|>=|==|!=|
1889 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1890 =>|->|<<|>>|<|>|=|!|~|
1891 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04001892 \?|::|:
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001893 }x;
1894 my @elements = split(/($ops|;)/, $opline);
1895 my $off = 0;
1896
1897 my $blank = copy_spacing($opline);
1898
1899 for (my $n = 0; $n < $#elements; $n += 2) {
1900 $off += length($elements[$n]);
1901
Stefan Weile7d81002011-12-10 00:19:46 +01001902 # Pick up the preceding and succeeding characters.
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001903 my $ca = substr($opline, 0, $off);
1904 my $cc = '';
1905 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1906 $cc = substr($opline, $off + length($elements[$n + 1]));
1907 }
1908 my $cb = "$ca$;$cc";
1909
1910 my $a = '';
1911 $a = 'V' if ($elements[$n] ne '');
1912 $a = 'W' if ($elements[$n] =~ /\s$/);
1913 $a = 'C' if ($elements[$n] =~ /$;$/);
1914 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1915 $a = 'O' if ($elements[$n] eq '');
1916 $a = 'E' if ($ca =~ /^\s*$/);
1917
1918 my $op = $elements[$n + 1];
1919
1920 my $c = '';
1921 if (defined $elements[$n + 2]) {
1922 $c = 'V' if ($elements[$n + 2] ne '');
1923 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1924 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1925 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1926 $c = 'O' if ($elements[$n + 2] eq '');
1927 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1928 } else {
1929 $c = 'E';
1930 }
1931
1932 my $ctx = "${a}x${c}";
1933
1934 my $at = "(ctx:$ctx)";
1935
1936 my $ptr = substr($blank, 0, $off) . "^";
1937 my $hereptr = "$hereline$ptr\n";
1938
1939 # Pull out the value of this operator.
1940 my $op_type = substr($curr_values, $off + 1, 1);
1941
1942 # Get the full operator variant.
1943 my $opv = $op . substr($curr_vars, $off, 1);
1944
1945 # Ignore operators passed as parameters.
1946 if ($op_type ne 'V' &&
1947 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1948
1949# # Ignore comments
1950# } elsif ($op =~ /^$;+$/) {
1951
1952 # ; should have either the end of line or a space or \ after it
1953 } elsif ($op eq ';') {
1954 if ($ctx !~ /.x[WEBC]/ &&
1955 $cc !~ /^\\/ && $cc !~ /^;/) {
1956 ERROR("space required after that '$op' $at\n" . $hereptr);
1957 }
1958
1959 # // is a comment
1960 } elsif ($op eq '//') {
1961
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04001962 # Ignore : used in class declaration in C++
1963 } elsif ($opv eq ':B' && $ctx =~ /Wx[WE]/ &&
1964 $line =~ /class/ && $realfile =~ /(\.cpp|\.h)$/) {
1965
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001966 # No spaces for:
1967 # ->
1968 # : when part of a bitfield
1969 } elsif ($op eq '->' || $opv eq ':B') {
1970 if ($ctx =~ /Wx.|.xW/) {
1971 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
1972 }
1973
1974 # , must have a space on the right.
Alexander Graf9fbe4782011-06-29 08:04:27 +02001975 # not required when having a single },{ on one line
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001976 } elsif ($op eq ',') {
Alexander Graf9fbe4782011-06-29 08:04:27 +02001977 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/ &&
Fam Zheng04f25622015-09-11 19:07:36 +08001978 ($elements[$n] . $elements[$n + 2]) !~ " *}\\{") {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001979 ERROR("space required after that '$op' $at\n" . $hereptr);
1980 }
1981
1982 # '*' as part of a type definition -- reported already.
1983 } elsif ($opv eq '*_') {
1984 #warn "'*' is part of type\n";
1985
1986 # unary operators should have a space before and
1987 # none after. May be left adjacent to another
1988 # unary operator, or a cast
1989 } elsif ($op eq '!' || $op eq '~' ||
1990 $opv eq '*U' || $opv eq '-U' ||
1991 $opv eq '&U' || $opv eq '&&U') {
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04001992 if ($op eq '~' && $ca =~ /::$/ && $realfile =~ /(\.cpp|\.h)$/) {
1993 # '~' used as a name of Destructor
1994
1995 } elsif ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00001996 ERROR("space required before that '$op' $at\n" . $hereptr);
1997 }
1998 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
1999 # A unary '*' may be const
2000
2001 } elsif ($ctx =~ /.xW/) {
2002 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2003 }
2004
2005 # unary ++ and unary -- are allowed no space on one side.
2006 } elsif ($op eq '++' or $op eq '--') {
2007 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
2008 ERROR("space required one side of that '$op' $at\n" . $hereptr);
2009 }
2010 if ($ctx =~ /Wx[BE]/ ||
2011 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
2012 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2013 }
2014 if ($ctx =~ /ExW/) {
2015 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
2016 }
2017
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002018 # A colon needs no spaces before when it is
2019 # terminating a case value or a label.
2020 } elsif ($opv eq ':C' || $opv eq ':L') {
2021 if ($ctx =~ /Wx./) {
2022 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
2023 }
2024
2025 # All the others need spaces both sides.
2026 } elsif ($ctx !~ /[EWC]x[CWE]/) {
2027 my $ok = 0;
2028
Tomoki Sekiyama69d5d212013-08-07 11:39:50 -04002029 if ($realfile =~ /\.cpp|\.h$/) {
2030 # Ignore template arguments <...> in C++
2031 if (($op eq '<' || $op eq '>') && $line =~ /<.*>/) {
2032 $ok = 1;
2033 }
2034
2035 # Ignore :: in C++
2036 if ($op eq '::') {
2037 $ok = 1;
2038 }
2039 }
2040
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002041 # Ignore email addresses <foo@bar>
2042 if (($op eq '<' &&
2043 $cc =~ /^\S+\@\S+>/) ||
2044 ($op eq '>' &&
2045 $ca =~ /<\S+\@\S+$/))
2046 {
2047 $ok = 1;
2048 }
2049
2050 # Ignore ?:
2051 if (($opv eq ':O' && $ca =~ /\?$/) ||
2052 ($op eq '?' && $cc =~ /^:/)) {
2053 $ok = 1;
2054 }
2055
2056 if ($ok == 0) {
2057 ERROR("spaces required around that '$op' $at\n" . $hereptr);
2058 }
2059 }
2060 $off += length($elements[$n + 1]);
2061 }
2062 }
2063
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002064#need space before brace following if, while, etc
Fam Zheng04f25622015-09-11 19:07:36 +08002065 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
2066 $line =~ /do\{/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002067 ERROR("space required before the open brace '{'\n" . $herecurr);
2068 }
2069
2070# closing brace should have a space following it when it has anything
2071# on the line
2072 if ($line =~ /}(?!(?:,|;|\)))\S/) {
2073 ERROR("space required after that close brace '}'\n" . $herecurr);
2074 }
2075
2076# check spacing on square brackets
2077 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
2078 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
2079 }
2080 if ($line =~ /\s\]/) {
2081 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
2082 }
2083
2084# check spacing on parentheses
2085 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
2086 $line !~ /for\s*\(\s+;/) {
2087 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
2088 }
2089 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
2090 $line !~ /for\s*\(.*;\s+\)/ &&
2091 $line !~ /:\s+\)/) {
2092 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
2093 }
2094
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002095# Return is not a function.
2096 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
2097 my $spacing = $1;
2098 my $value = $2;
2099
2100 # Flatten any parentheses
2101 $value =~ s/\(/ \(/g;
2102 $value =~ s/\)/\) /g;
2103 while ($value =~ s/\[[^\{\}]*\]/1/ ||
2104 $value !~ /(?:$Ident|-?$Constant)\s*
2105 $Compare\s*
2106 (?:$Ident|-?$Constant)/x &&
2107 $value =~ s/\([^\(\)]*\)/1/) {
2108 }
2109#print "value<$value>\n";
2110 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
2111 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
2112
2113 } elsif ($spacing !~ /\s+/) {
2114 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2115 }
2116 }
2117# Return of what appears to be an errno should normally be -'ve
2118 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
2119 my $name = $1;
2120 if ($name ne 'EOF' && $name ne 'ERROR') {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002121 ERROR("return of an errno should typically be -ve (return -$1)\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002122 }
2123 }
2124
2125# Need a space before open parenthesis after if, while etc
2126 if ($line=~/\b(if|while|for|switch)\(/) {
2127 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2128 }
2129
2130# Check for illegal assignment in if conditional -- and check for trailing
2131# statements after the conditional.
2132 if ($line =~ /do\s*(?!{)/) {
2133 my ($stat_next) = ctx_statement_block($line_nr_next,
2134 $remain_next, $off_next);
2135 $stat_next =~ s/\n./\n /g;
2136 ##print "stat<$stat> stat_next<$stat_next>\n";
2137
2138 if ($stat_next =~ /^\s*while\b/) {
2139 # If the statement carries leading newlines,
2140 # then count those as offsets.
2141 my ($whitespace) =
2142 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2143 my $offset =
2144 statement_rawlines($whitespace) - 1;
2145
2146 $suppress_whiletrailers{$line_nr_next +
2147 $offset} = 1;
2148 }
2149 }
2150 if (!defined $suppress_whiletrailers{$linenr} &&
2151 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2152 my ($s, $c) = ($stat, $cond);
2153
2154 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
2155 ERROR("do not use assignment in if condition\n" . $herecurr);
2156 }
2157
2158 # Find out what is on the end of the line after the
2159 # conditional.
2160 substr($s, 0, length($c), '');
2161 $s =~ s/\n.*//g;
2162 $s =~ s/$;//g; # Remove any comments
2163 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2164 $c !~ /}\s*while\s*/)
2165 {
2166 # Find out how long the conditional actually is.
2167 my @newlines = ($c =~ /\n/gs);
2168 my $cond_lines = 1 + $#newlines;
2169 my $stat_real = '';
2170
2171 $stat_real = raw_line($linenr, $cond_lines)
2172 . "\n" if ($cond_lines);
2173 if (defined($stat_real) && $cond_lines > 1) {
2174 $stat_real = "[...]\n$stat_real";
2175 }
2176
2177 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2178 }
2179 }
2180
2181# Check for bitwise tests written as boolean
2182 if ($line =~ /
2183 (?:
2184 (?:\[|\(|\&\&|\|\|)
2185 \s*0[xX][0-9]+\s*
2186 (?:\&\&|\|\|)
2187 |
2188 (?:\&\&|\|\|)
2189 \s*0[xX][0-9]+\s*
2190 (?:\&\&|\|\||\)|\])
2191 )/x)
2192 {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002193 ERROR("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002194 }
2195
2196# if and else should not have general statements after it
2197 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2198 my $s = $1;
2199 $s =~ s/$;//g; # Remove any comments
2200 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2201 ERROR("trailing statements should be on next line\n" . $herecurr);
2202 }
2203 }
2204# if should not continue a brace
2205 if ($line =~ /}\s*if\b/) {
2206 ERROR("trailing statements should be on next line\n" .
2207 $herecurr);
2208 }
2209# case and default should not have general statements after them
2210 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2211 $line !~ /\G(?:
2212 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2213 \s*return\s+
2214 )/xg)
2215 {
2216 ERROR("trailing statements should be on next line\n" . $herecurr);
2217 }
2218
2219 # Check for }<nl>else {, these must be at the same
2220 # indent level to be relevant to each other.
2221 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2222 $previndent == $indent) {
2223 ERROR("else should follow close brace '}'\n" . $hereprev);
2224 }
2225
2226 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2227 $previndent == $indent) {
2228 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2229
2230 # Find out what is on the end of the line after the
2231 # conditional.
2232 substr($s, 0, length($c), '');
2233 $s =~ s/\n.*//g;
2234
2235 if ($s =~ /^\s*;/) {
2236 ERROR("while should follow close brace '}'\n" . $hereprev);
2237 }
2238 }
2239
2240#studly caps, commented out until figure out how to distinguish between use of existing and adding new
2241# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2242# print "No studly caps, use _\n";
2243# print "$herecurr";
2244# $clean = 0;
2245# }
2246
2247#no spaces allowed after \ in define
2248 if ($line=~/\#\s*define.*\\\s$/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002249 ERROR("Whitespace after \\ makes next lines useless\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002250 }
2251
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002252# multi-statement macros should be enclosed in a do while loop, grab the
2253# first statement and ensure its the whole macro if its not enclosed
2254# in a known good container
2255 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2256 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2257 my $ln = $linenr;
2258 my $cnt = $realcnt;
2259 my ($off, $dstat, $dcond, $rest);
2260 my $ctx = '';
2261
2262 my $args = defined($1);
2263
2264 # Find the end of the macro and limit our statement
2265 # search to that.
2266 while ($cnt > 0 && defined $lines[$ln - 1] &&
2267 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2268 {
2269 $ctx .= $rawlines[$ln - 1] . "\n";
2270 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2271 $ln++;
2272 }
2273 $ctx .= $rawlines[$ln - 1];
2274
2275 ($dstat, $dcond, $ln, $cnt, $off) =
2276 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2277 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2278 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2279
2280 # Extract the remainder of the define (if any) and
2281 # rip off surrounding spaces, and trailing \'s.
2282 $rest = '';
2283 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2284 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2285 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2286 $rest .= substr($lines[$ln - 1], $off) . "\n";
2287 $cnt--;
2288 }
2289 $ln++;
2290 $off = 0;
2291 }
2292 $rest =~ s/\\\n.//g;
2293 $rest =~ s/^\s*//s;
2294 $rest =~ s/\s*$//s;
2295
2296 # Clean up the original statement.
2297 if ($args) {
2298 substr($dstat, 0, length($dcond), '');
2299 } else {
2300 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2301 }
2302 $dstat =~ s/$;//g;
2303 $dstat =~ s/\\\n.//g;
2304 $dstat =~ s/^\s*//s;
2305 $dstat =~ s/\s*$//s;
2306
2307 # Flatten any parentheses and braces
2308 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2309 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2310 $dstat =~ s/\[[^\{\}]*\]/1/)
2311 {
2312 }
2313
2314 my $exceptions = qr{
2315 $Declare|
2316 module_param_named|
2317 MODULE_PARAM_DESC|
2318 DECLARE_PER_CPU|
2319 DEFINE_PER_CPU|
2320 __typeof__\(|
2321 union|
2322 struct|
2323 \.$Ident\s*=\s*|
2324 ^\"|\"$
2325 }x;
2326 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
2327 if ($rest ne '' && $rest ne ',') {
2328 if ($rest !~ /while\s*\(/ &&
2329 $dstat !~ /$exceptions/)
2330 {
2331 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2332 }
2333
2334 } elsif ($ctx !~ /;/) {
2335 if ($dstat ne '' &&
2336 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2337 $dstat !~ /$exceptions/ &&
2338 $dstat !~ /^\.$Ident\s*=/ &&
2339 $dstat =~ /$Operators/)
2340 {
2341 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2342 }
2343 }
2344 }
2345
Blue Swirlb6469682011-01-20 20:58:56 +00002346# check for missing bracing round if etc
2347 if ($line =~ /(^.*)\bif\b/ && $line !~ /\#\s*if/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002348 my ($level, $endln, @chunks) =
2349 ctx_statement_full($linenr, $realcnt, 1);
Don Slutz69402a62012-09-02 19:22:37 -04002350 if ($dbg_adv_apw) {
2351 print "APW: chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2352 print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"
2353 if $#chunks >= 1;
2354 }
Blue Swirlb6469682011-01-20 20:58:56 +00002355 if ($#chunks >= 0 && $level == 0) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002356 my $allowed = 0;
2357 my $seen = 0;
2358 my $herectx = $here . "\n";
2359 my $ln = $linenr - 1;
2360 for my $chunk (@chunks) {
2361 my ($cond, $block) = @{$chunk};
2362
2363 # If the condition carries leading newlines, then count those as offsets.
2364 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2365 my $offset = statement_rawlines($whitespace) - 1;
2366
2367 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2368
2369 # We have looked at and allowed this specific line.
2370 $suppress_ifbraces{$ln + $offset} = 1;
2371
2372 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2373 $ln += statement_rawlines($block) - 1;
2374
2375 substr($block, 0, length($cond), '');
2376
Max Reitza97ceca2014-11-26 17:20:24 +01002377 my $spaced_block = $block;
2378 $spaced_block =~ s/\n\+/ /g;
2379
Fam Zheng04f25622015-09-11 19:07:36 +08002380 $seen++ if ($spaced_block =~ /^\s*\{/);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002381
Don Slutz69402a62012-09-02 19:22:37 -04002382 print "APW: cond<$cond> block<$block> allowed<$allowed>\n"
2383 if $dbg_adv_apw;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002384 if (statement_lines($cond) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002385 print "APW: ALLOWED: cond<$cond>\n"
2386 if $dbg_adv_apw;
2387 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002388 }
2389 if ($block =~/\b(?:if|for|while)\b/) {
Don Slutz69402a62012-09-02 19:22:37 -04002390 print "APW: ALLOWED: block<$block>\n"
2391 if $dbg_adv_apw;
2392 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002393 }
2394 if (statement_block_size($block) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002395 print "APW: ALLOWED: lines block<$block>\n"
2396 if $dbg_adv_apw;
2397 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002398 }
2399 }
Pavel Borzenkov01c43302011-08-26 17:34:37 +04002400 if ($seen != ($#chunks + 1)) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002401 ERROR("braces {} are necessary for all arms of this statement\n" . $herectx);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002402 }
2403 }
2404 }
2405 if (!defined $suppress_ifbraces{$linenr - 1} &&
Jan Kiszka789f88d2011-01-21 18:19:40 +01002406 $line =~ /\b(if|while|for|else)\b/ &&
Blue Swirld0510af2011-07-15 20:09:10 +00002407 $line !~ /\#\s*if/ &&
Jan Kiszka789f88d2011-01-21 18:19:40 +01002408 $line !~ /\#\s*else/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002409 my $allowed = 0;
2410
Don Slutzdfe70532012-09-02 19:22:38 -04002411 # Check the pre-context.
2412 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2413 my $pre = $1;
2414
2415 if ($line !~ /else/) {
2416 print "APW: ALLOWED: pre<$pre> line<$line>\n"
2417 if $dbg_adv_apw;
2418 $allowed = 1;
2419 }
2420 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002421
2422 my ($level, $endln, @chunks) =
2423 ctx_statement_full($linenr, $realcnt, $-[0]);
2424
2425 # Check the condition.
2426 my ($cond, $block) = @{$chunks[0]};
Don Slutz54243022012-09-02 19:22:36 -04002427 print "CHECKING<$linenr> cond<$cond> block<$block>\n"
2428 if $dbg_adv_checking;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002429 if (defined $cond) {
2430 substr($block, 0, length($cond), '');
2431 }
2432 if (statement_lines($cond) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002433 print "APW: ALLOWED: cond<$cond>\n"
2434 if $dbg_adv_apw;
2435 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002436 }
2437 if ($block =~/\b(?:if|for|while)\b/) {
Don Slutz69402a62012-09-02 19:22:37 -04002438 print "APW: ALLOWED: block<$block>\n"
2439 if $dbg_adv_apw;
2440 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002441 }
2442 if (statement_block_size($block) > 1) {
Don Slutz69402a62012-09-02 19:22:37 -04002443 print "APW: ALLOWED: lines block<$block>\n"
2444 if $dbg_adv_apw;
2445 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002446 }
2447 # Check the post-context.
2448 if (defined $chunks[1]) {
2449 my ($cond, $block) = @{$chunks[1]};
2450 if (defined $cond) {
2451 substr($block, 0, length($cond), '');
2452 }
2453 if ($block =~ /^\s*\{/) {
Don Slutz69402a62012-09-02 19:22:37 -04002454 print "APW: ALLOWED: chunk-1 block<$block>\n"
2455 if $dbg_adv_apw;
2456 $allowed = 1;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002457 }
2458 }
Don Slutza99ac042012-09-02 19:22:35 -04002459 print "DCS: level=$level block<$block> allowed=$allowed\n"
2460 if $dbg_adv_dcs;
Blue Swirlb6469682011-01-20 20:58:56 +00002461 if ($level == 0 && $block !~ /^\s*\{/ && !$allowed) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002462 my $herectx = $here . "\n";;
2463 my $cnt = statement_rawlines($block);
2464
2465 for (my $n = 0; $n < $cnt; $n++) {
2466 $herectx .= raw_line($linenr, $n) . "\n";;
2467 }
2468
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002469 ERROR("braces {} are necessary even for single statement blocks\n" . $herectx);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002470 }
2471 }
2472
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002473# no volatiles please
2474 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2475 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002476 ERROR("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002477 }
2478
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002479# warn about #if 0
2480 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002481 ERROR("if this code is redundant consider removing it\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002482 $herecurr);
2483 }
2484
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002485# check for needless g_free() checks
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002486 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2487 my $expr = $1;
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002488 if ($line =~ /\bg_free\(\Q$expr\E\);/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002489 ERROR("g_free(NULL) is safe this check is probably not required\n" . $hereprev);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002490 }
2491 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002492
2493# warn about #ifdefs in C files
2494# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2495# print "#ifdef in C files should be avoided\n";
2496# print "$herecurr";
2497# $clean = 0;
2498# }
2499
2500# warn about spacing in #ifdefs
2501 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2502 ERROR("exactly one space required after that #$1\n" . $herecurr);
2503 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002504# check for memory barriers without a comment.
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002505 if ($line =~ /\b(smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002506 if (!ctx_has_comment($first_line, $linenr)) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002507 ERROR("memory barrier without comment\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002508 }
2509 }
2510# check of hardware specific defines
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002511# we have e.g. CONFIG_LINUX and CONFIG_WIN32 for common cases
2512# where they might be necessary.
2513 if ($line =~ m@^.\s*\#\s*if.*\b__@) {
Paolo Bonzini63ae8b92016-09-21 18:49:07 +02002514 WARN("architecture specific defines should be avoided\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002515 }
2516
2517# Check that the storage class is at the beginning of a declaration
2518 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002519 ERROR("storage class should be at the beginning of the declaration\n" . $herecurr)
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002520 }
2521
2522# check the location of the inline attribute, that it is between
2523# storage class and type.
2524 if ($line =~ /\b$Type\s+$Inline\b/ ||
2525 $line =~ /\b$Inline\s+$Storage\b/) {
2526 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2527 }
2528
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002529# check for sizeof(&)
2530 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002531 ERROR("sizeof(& should be avoided\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002532 }
2533
2534# check for new externs in .c files.
2535 if ($realfile =~ /\.c$/ && defined $stat &&
2536 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2537 {
2538 my $function_name = $1;
2539 my $paren_space = $2;
2540
2541 my $s = $stat;
2542 if (defined $cond) {
2543 substr($s, 0, length($cond), '');
2544 }
2545 if ($s =~ /^\s*;/ &&
2546 $function_name ne 'uninitialized_var')
2547 {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002548 ERROR("externs should be avoided in .c files\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002549 }
2550
2551 if ($paren_space =~ /\n/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002552 ERROR("arguments for function declarations should follow identifier\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002553 }
2554
2555 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2556 $stat =~ /^.\s*extern\s+/)
2557 {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002558 ERROR("externs should be avoided in .c files\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002559 }
2560
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002561# check for pointless casting of g_malloc return
2562 if ($line =~ /\*\s*\)\s*g_(try)?(m|re)alloc(0?)(_n)?\b/) {
2563 if ($2 == 'm') {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002564 ERROR("unnecessary cast may hide bugs, use g_$1new$3 instead\n" . $herecurr);
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002565 } else {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002566 ERROR("unnecessary cast may hide bugs, use g_$1renew$3 instead\n" . $herecurr);
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002567 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002568 }
2569
2570# check for gcc specific __FUNCTION__
2571 if ($line =~ /__FUNCTION__/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002572 ERROR("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002573 }
2574
Paolo Bonzini5e43efb2015-09-16 18:35:09 +02002575# recommend qemu_strto* over strto* for numeric conversions
Eric Blake01fb8e12016-06-09 20:48:07 -06002576 if ($line =~ /\b(strto[^kd].*?)\s*\(/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002577 ERROR("consider using qemu_$1 in preference to $1\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002578 }
Paolo Bonzinie8c20912017-07-04 11:26:37 +02002579# recommend sigaction over signal for portability, when establishing a handler
2580 if ($line =~ /\bsignal\s*\(/ && !($line =~ /SIG_(?:IGN|DFL)/)) {
2581 ERROR("use sigaction to establish signal handlers; signal is not portable\n" . $herecurr);
2582 }
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002583# check for module_init(), use category-specific init macros explicitly please
2584 if ($line =~ /^module_init\s*\(/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002585 ERROR("please use block_init(), type_init() etc. instead of module_init()\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002586 }
2587# check for various ops structs, ensure they are const.
Paolo Bonzini71c47b02015-08-16 23:15:46 +02002588 my $struct_ops = qr{AIOCBInfo|
2589 BdrvActionOps|
2590 BlockDevOps|
2591 BlockJobDriver|
2592 DisplayChangeListenerOps|
2593 GraphicHwOps|
2594 IDEDMAOps|
2595 KVMCapabilityInfo|
2596 MemoryRegionIOMMUOps|
2597 MemoryRegionOps|
2598 MemoryRegionPortio|
2599 QEMUFileOps|
2600 SCSIBusInfo|
2601 SCSIReqOps|
2602 Spice[A-Z][a-zA-Z0-9]*Interface|
2603 TPMDriverOps|
2604 USBDesc[A-Z][a-zA-Z0-9]*|
2605 VhostOps|
2606 VMStateDescription|
2607 VMStateInfo}x;
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002608 if ($line !~ /\bconst\b/ &&
Paolo Bonzinie20e7182016-10-14 10:45:45 +02002609 $line =~ /\b($struct_ops)\b.*=/) {
2610 ERROR("initializer for struct $1 should normally be const\n" .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002611 $herecurr);
2612 }
2613
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002614# check for %L{u,d,i} in strings
2615 my $string;
2616 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2617 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2618 $string =~ s/%%/__/g;
2619 if ($string =~ /(?<!%)%L[udi]/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002620 ERROR("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002621 last;
2622 }
2623 }
2624
Stefan Weil9964d8f2012-06-17 06:57:41 +02002625# QEMU specific tests
2626 if ($rawline =~ /\b(?:Qemu|QEmu)\b/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002627 ERROR("use QEMU instead of Qemu or QEmu\n" . $herecurr);
Stefan Weil9964d8f2012-06-17 06:57:41 +02002628 }
Stefan Hajnoczi8b6ee9a2015-03-23 15:29:31 +00002629
Jason J. Herne5d596c22015-12-11 13:30:42 -05002630# Qemu error function tests
2631
2632 # Find newlines in error messages
2633 my $qemu_error_funcs = qr{error_setg|
2634 error_setg_errno|
2635 error_setg_win32|
Markus Armbrustera47eb012016-08-03 13:37:52 +02002636 error_setg_file_open|
Jason J. Herne5d596c22015-12-11 13:30:42 -05002637 error_set|
Markus Armbrustera47eb012016-08-03 13:37:52 +02002638 error_prepend|
Alistair Francise43ead12017-07-12 06:57:52 -07002639 warn_reportf_err|
Markus Armbrustera47eb012016-08-03 13:37:52 +02002640 error_reportf_err|
Jason J. Herne5d596c22015-12-11 13:30:42 -05002641 error_vreport|
Alistair Francis97f40302017-07-12 06:57:36 -07002642 warn_vreport|
2643 info_vreport|
2644 error_report|
2645 warn_report|
2646 info_report}x;
Jason J. Herne5d596c22015-12-11 13:30:42 -05002647
Markus Armbrustera47eb012016-08-03 13:37:52 +02002648 if ($rawline =~ /\b(?:$qemu_error_funcs)\s*\(.*\".*\\n/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002649 ERROR("Error messages should not contain newlines\n" . $herecurr);
Jason J. Herne5d596c22015-12-11 13:30:42 -05002650 }
2651
2652 # Continue checking for error messages that contains newlines. This
2653 # check handles cases where string literals are spread over multiple lines.
2654 # Example:
2655 # error_report("Error msg line #1"
2656 # "Error msg line #2\n");
2657 my $quoted_newline_regex = qr{\+\s*\".*\\n.*\"};
2658 my $continued_str_literal = qr{\+\s*\".*\"};
2659
2660 if ($rawline =~ /$quoted_newline_regex/) {
2661 # Backtrack to first line that does not contain only a quoted literal
2662 # and assume that it is the start of the statement.
2663 my $i = $linenr - 2;
2664
2665 while (($i >= 0) & $rawlines[$i] =~ /$continued_str_literal/) {
2666 $i--;
2667 }
2668
2669 if ($rawlines[$i] =~ /\b(?:$qemu_error_funcs)\s*\(/) {
Paolo Bonzinic2df8782016-08-09 17:47:43 +02002670 ERROR("Error messages should not contain newlines\n" . $herecurr);
Jason J. Herne5d596c22015-12-11 13:30:42 -05002671 }
2672 }
2673
Paolo Bonzini3f822cf2016-07-21 11:03:11 +02002674# check for non-portable libc calls that have portable alternatives in QEMU
Stefan Hajnoczi8b6ee9a2015-03-23 15:29:31 +00002675 if ($line =~ /\bffs\(/) {
2676 ERROR("use ctz32() instead of ffs()\n" . $herecurr);
2677 }
2678 if ($line =~ /\bffsl\(/) {
2679 ERROR("use ctz32() or ctz64() instead of ffsl()\n" . $herecurr);
2680 }
2681 if ($line =~ /\bffsll\(/) {
2682 ERROR("use ctz64() instead of ffsll()\n" . $herecurr);
2683 }
Paolo Bonzini3f822cf2016-07-21 11:03:11 +02002684 if ($line =~ /\bbzero\(/) {
2685 ERROR("use memset() instead of bzero()\n" . $herecurr);
2686 }
Dr. David Alan Gilbert6e938952017-04-27 17:55:26 +01002687 my $non_exit_glib_asserts = qr{g_assert_cmpstr|
2688 g_assert_cmpint|
2689 g_assert_cmpuint|
2690 g_assert_cmphex|
2691 g_assert_cmpfloat|
2692 g_assert_true|
2693 g_assert_false|
2694 g_assert_nonnull|
2695 g_assert_null|
2696 g_assert_no_error|
2697 g_assert_error|
2698 g_test_assert_expected_messages|
2699 g_test_trap_assert_passed|
2700 g_test_trap_assert_stdout|
2701 g_test_trap_assert_stdout_unmatched|
2702 g_test_trap_assert_stderr|
2703 g_test_trap_assert_stderr_unmatched}x;
2704 if ($realfile !~ /^tests\// &&
2705 $line =~ /\b(?:$non_exit_glib_asserts)\(/) {
2706 ERROR("Use g_assert or g_assert_not_reached\n". $herecurr);
2707 }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002708 }
2709
2710 # If we have no input at all, then there is nothing to report on
2711 # so just keep quiet.
2712 if ($#rawlines == -1) {
2713 exit(0);
2714 }
2715
2716 # In mailback mode only produce a report in the negative, for
2717 # things that appear to be patches.
2718 if ($mailback && ($clean == 1 || !$is_patch)) {
2719 exit(0);
2720 }
2721
2722 # This is not a patch, and we are are in 'no-patch' mode so
2723 # just keep quiet.
2724 if (!$chk_patch && !$is_patch) {
2725 exit(0);
2726 }
2727
2728 if (!$is_patch) {
2729 ERROR("Does not appear to be a unified-diff format patch\n");
2730 }
2731 if ($is_patch && $chk_signoff && $signoff == 0) {
2732 ERROR("Missing Signed-off-by: line(s)\n");
2733 }
2734
2735 print report_dump();
2736 if ($summary && !($clean == 1 && $quiet == 1)) {
2737 print "$filename " if ($summary_file);
2738 print "total: $cnt_error errors, $cnt_warn warnings, " .
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002739 "$cnt_lines lines checked\n";
2740 print "\n" if ($quiet == 0);
2741 }
2742
2743 if ($quiet == 0) {
2744 # If there were whitespace errors which cleanpatch can fix
2745 # then suggest that.
Blue Swirlb6469682011-01-20 20:58:56 +00002746# if ($rpt_cleaners) {
2747# print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
2748# print " scripts/cleanfile\n\n";
2749# }
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002750 }
2751
2752 if ($clean == 1 && $quiet == 0) {
2753 print "$vname has no obvious style problems and is ready for submission.\n"
2754 }
2755 if ($clean == 0 && $quiet == 0) {
2756 print "$vname has style problems, please review. If any of these errors\n";
2757 print "are false positives report them to the maintainer, see\n";
2758 print "CHECKPATCH in MAINTAINERS.\n";
2759 }
2760
Paolo Bonzini141de882016-08-09 17:47:44 +02002761 return ($no_warnings ? $clean : $cnt_error == 0);
Blue Swirl1ec3f6f2011-01-20 20:54:26 +00002762}