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