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