checkpatch: fix continuation detection when handling spacing on operators
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / scripts / checkpatch.pl
blob67b0c9faa32d9f1da19cee2cd7be38895d0bb789
1 #!/usr/bin/perl -w
2 # (c) 2001, Dave Jones. <davej@redhat.com> (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, Andy Whitcroft <apw@canonical.com>
6 # Licensed under the terms of the GNU GPL License version 2
8 use strict;
10 my $P = $0;
11 $P =~ s@.*/@@g;
13 my $V = '0.25';
15 use Getopt::Long qw(:config no_auto_abbrev);
17 my $quiet = 0;
18 my $tree = 1;
19 my $chk_signoff = 1;
20 my $chk_patch = 1;
21 my $tst_only;
22 my $emacs = 0;
23 my $terse = 0;
24 my $file = 0;
25 my $check = 0;
26 my $summary = 1;
27 my $mailback = 0;
28 my $summary_file = 0;
29 my $root;
30 my %debug;
31 GetOptions(
32 'q|quiet+' => \$quiet,
33 'tree!' => \$tree,
34 'signoff!' => \$chk_signoff,
35 'patch!' => \$chk_patch,
36 'emacs!' => \$emacs,
37 'terse!' => \$terse,
38 'file!' => \$file,
39 'subjective!' => \$check,
40 'strict!' => \$check,
41 'root=s' => \$root,
42 'summary!' => \$summary,
43 'mailback!' => \$mailback,
44 'summary-file!' => \$summary_file,
46 'debug=s' => \%debug,
47 'test-only=s' => \$tst_only,
48 ) or exit;
50 my $exit = 0;
52 if ($#ARGV < 0) {
53 print "usage: $P [options] patchfile\n";
54 print "version: $V\n";
55 print "options: -q => quiet\n";
56 print " --no-tree => run without a kernel tree\n";
57 print " --terse => one line per report\n";
58 print " --emacs => emacs compile window format\n";
59 print " --file => check a source file\n";
60 print " --strict => enable more subjective tests\n";
61 print " --root => path to the kernel tree root\n";
62 print " --no-summary => suppress the per-file summary\n";
63 print " --summary-file => include the filename in summary\n";
64 exit(1);
67 my $dbg_values = 0;
68 my $dbg_possible = 0;
69 my $dbg_type = 0;
70 my $dbg_attr = 0;
71 for my $key (keys %debug) {
72 eval "\${dbg_$key} = '$debug{$key}';"
75 if ($terse) {
76 $emacs = 1;
77 $quiet++;
80 if ($tree) {
81 if (defined $root) {
82 if (!top_of_kernel_tree($root)) {
83 die "$P: $root: --root does not point at a valid tree\n";
85 } else {
86 if (top_of_kernel_tree('.')) {
87 $root = '.';
88 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
89 top_of_kernel_tree($1)) {
90 $root = $1;
94 if (!defined $root) {
95 print "Must be run from the top-level dir. of a kernel tree\n";
96 exit(2);
100 my $emitted_corrupt = 0;
102 our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
103 our $Storage = qr{extern|static|asmlinkage};
104 our $Sparse = qr{
105 __user|
106 __kernel|
107 __force|
108 __iomem|
109 __must_check|
110 __init_refok|
111 __kprobes
113 our $Attribute = qr{
114 const|
115 __read_mostly|
116 __kprobes|
117 __(?:mem|cpu|dev|)(?:initdata|init)|
118 ____cacheline_aligned|
119 ____cacheline_aligned_in_smp|
120 ____cacheline_internodealigned_in_smp|
121 __weak
123 our $Modifier;
124 our $Inline = qr{inline|__always_inline|noinline};
125 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
126 our $Lval = qr{$Ident(?:$Member)*};
128 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
129 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
130 our $Compare = qr{<=|>=|==|!=|<|>};
131 our $Operators = qr{
132 <=|>=|==|!=|
133 =>|->|<<|>>|<|>|!|~|
134 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
137 our $NonptrType;
138 our $Type;
139 our $Declare;
141 our $UTF8 = qr {
142 [\x09\x0A\x0D\x20-\x7E] # ASCII
143 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
144 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
145 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
146 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
147 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
148 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
149 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
152 our $typeTypedefs = qr{(?x:
153 (?:__)?(?:u|s|be|le)(?:\d|\d\d)|
154 atomic_t
157 our @typeList = (
158 qr{void},
159 qr{(?:unsigned\s+)?char},
160 qr{(?:unsigned\s+)?short},
161 qr{(?:unsigned\s+)?int},
162 qr{(?:unsigned\s+)?long},
163 qr{(?:unsigned\s+)?long\s+int},
164 qr{(?:unsigned\s+)?long\s+long},
165 qr{(?:unsigned\s+)?long\s+long\s+int},
166 qr{unsigned},
167 qr{float},
168 qr{double},
169 qr{bool},
170 qr{struct\s+$Ident},
171 qr{union\s+$Ident},
172 qr{enum\s+$Ident},
173 qr{${Ident}_t},
174 qr{${Ident}_handler},
175 qr{${Ident}_handler_fn},
177 our @modifierList = (
178 qr{fastcall},
181 sub build_types {
182 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
183 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
184 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
185 $NonptrType = qr{
186 (?:$Modifier\s+|const\s+)*
188 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)|
189 (?:$typeTypedefs\b)|
190 (?:${all}\b)
192 (?:\s+$Modifier|\s+const)*
194 $Type = qr{
195 $NonptrType
196 (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)?
197 (?:\s+$Inline|\s+$Modifier)*
199 $Declare = qr{(?:$Storage\s+)?$Type};
201 build_types();
203 $chk_signoff = 0 if ($file);
205 my @dep_includes = ();
206 my @dep_functions = ();
207 my $removal = "Documentation/feature-removal-schedule.txt";
208 if ($tree && -f "$root/$removal") {
209 open(REMOVE, "<$root/$removal") ||
210 die "$P: $removal: open failed - $!\n";
211 while (<REMOVE>) {
212 if (/^Check:\s+(.*\S)/) {
213 for my $entry (split(/[, ]+/, $1)) {
214 if ($entry =~ m@include/(.*)@) {
215 push(@dep_includes, $1);
217 } elsif ($entry !~ m@/@) {
218 push(@dep_functions, $entry);
225 my @rawlines = ();
226 my @lines = ();
227 my $vname;
228 for my $filename (@ARGV) {
229 if ($file) {
230 open(FILE, "diff -u /dev/null $filename|") ||
231 die "$P: $filename: diff failed - $!\n";
232 } else {
233 open(FILE, "<$filename") ||
234 die "$P: $filename: open failed - $!\n";
236 if ($filename eq '-') {
237 $vname = 'Your patch';
238 } else {
239 $vname = $filename;
241 while (<FILE>) {
242 chomp;
243 push(@rawlines, $_);
245 close(FILE);
246 if (!process($filename)) {
247 $exit = 1;
249 @rawlines = ();
250 @lines = ();
253 exit($exit);
255 sub top_of_kernel_tree {
256 my ($root) = @_;
258 my @tree_check = (
259 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
260 "README", "Documentation", "arch", "include", "drivers",
261 "fs", "init", "ipc", "kernel", "lib", "scripts",
264 foreach my $check (@tree_check) {
265 if (! -e $root . '/' . $check) {
266 return 0;
269 return 1;
272 sub expand_tabs {
273 my ($str) = @_;
275 my $res = '';
276 my $n = 0;
277 for my $c (split(//, $str)) {
278 if ($c eq "\t") {
279 $res .= ' ';
280 $n++;
281 for (; ($n % 8) != 0; $n++) {
282 $res .= ' ';
284 next;
286 $res .= $c;
287 $n++;
290 return $res;
292 sub copy_spacing {
293 (my $res = shift) =~ tr/\t/ /c;
294 return $res;
297 sub line_stats {
298 my ($line) = @_;
300 # Drop the diff line leader and expand tabs
301 $line =~ s/^.//;
302 $line = expand_tabs($line);
304 # Pick the indent from the front of the line.
305 my ($white) = ($line =~ /^(\s*)/);
307 return (length($line), length($white));
310 my $sanitise_quote = '';
312 sub sanitise_line_reset {
313 my ($in_comment) = @_;
315 if ($in_comment) {
316 $sanitise_quote = '*/';
317 } else {
318 $sanitise_quote = '';
321 sub sanitise_line {
322 my ($line) = @_;
324 my $res = '';
325 my $l = '';
327 my $qlen = 0;
328 my $off = 0;
329 my $c;
331 # Always copy over the diff marker.
332 $res = substr($line, 0, 1);
334 for ($off = 1; $off < length($line); $off++) {
335 $c = substr($line, $off, 1);
337 # Comments we are wacking completly including the begin
338 # and end, all to $;.
339 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
340 $sanitise_quote = '*/';
342 substr($res, $off, 2, "$;$;");
343 $off++;
344 next;
346 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
347 $sanitise_quote = '';
348 substr($res, $off, 2, "$;$;");
349 $off++;
350 next;
353 # A \ in a string means ignore the next character.
354 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
355 $c eq "\\") {
356 substr($res, $off, 2, 'XX');
357 $off++;
358 next;
360 # Regular quotes.
361 if ($c eq "'" || $c eq '"') {
362 if ($sanitise_quote eq '') {
363 $sanitise_quote = $c;
365 substr($res, $off, 1, $c);
366 next;
367 } elsif ($sanitise_quote eq $c) {
368 $sanitise_quote = '';
372 #print "c<$c> SQ<$sanitise_quote>\n";
373 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
374 substr($res, $off, 1, $;);
375 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
376 substr($res, $off, 1, 'X');
377 } else {
378 substr($res, $off, 1, $c);
382 # The pathname on a #include may be surrounded by '<' and '>'.
383 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
384 my $clean = 'X' x length($1);
385 $res =~ s@\<.*\>@<$clean>@;
387 # The whole of a #error is a string.
388 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
389 my $clean = 'X' x length($1);
390 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
393 return $res;
396 sub ctx_statement_block {
397 my ($linenr, $remain, $off) = @_;
398 my $line = $linenr - 1;
399 my $blk = '';
400 my $soff = $off;
401 my $coff = $off - 1;
402 my $coff_set = 0;
404 my $loff = 0;
406 my $type = '';
407 my $level = 0;
408 my $p;
409 my $c;
410 my $len = 0;
412 my $remainder;
413 while (1) {
414 #warn "CSB: blk<$blk> remain<$remain>\n";
415 # If we are about to drop off the end, pull in more
416 # context.
417 if ($off >= $len) {
418 for (; $remain > 0; $line++) {
419 last if (!defined $lines[$line]);
420 next if ($lines[$line] =~ /^-/);
421 $remain--;
422 $loff = $len;
423 $blk .= $lines[$line] . "\n";
424 $len = length($blk);
425 $line++;
426 last;
428 # Bail if there is no further context.
429 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
430 if ($off >= $len) {
431 last;
434 $p = $c;
435 $c = substr($blk, $off, 1);
436 $remainder = substr($blk, $off);
438 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
439 # Statement ends at the ';' or a close '}' at the
440 # outermost level.
441 if ($level == 0 && $c eq ';') {
442 last;
445 # An else is really a conditional as long as its not else if
446 if ($level == 0 && $coff_set == 0 &&
447 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
448 $remainder =~ /^(else)(?:\s|{)/ &&
449 $remainder !~ /^else\s+if\b/) {
450 $coff = $off + length($1) - 1;
451 $coff_set = 1;
452 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
453 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
456 if (($type eq '' || $type eq '(') && $c eq '(') {
457 $level++;
458 $type = '(';
460 if ($type eq '(' && $c eq ')') {
461 $level--;
462 $type = ($level != 0)? '(' : '';
464 if ($level == 0 && $coff < $soff) {
465 $coff = $off;
466 $coff_set = 1;
467 #warn "CSB: mark coff<$coff>\n";
470 if (($type eq '' || $type eq '{') && $c eq '{') {
471 $level++;
472 $type = '{';
474 if ($type eq '{' && $c eq '}') {
475 $level--;
476 $type = ($level != 0)? '{' : '';
478 if ($level == 0) {
479 last;
482 $off++;
484 # We are truly at the end, so shuffle to the next line.
485 if ($off == $len) {
486 $loff = $len + 1;
487 $line++;
488 $remain--;
491 my $statement = substr($blk, $soff, $off - $soff + 1);
492 my $condition = substr($blk, $soff, $coff - $soff + 1);
494 #warn "STATEMENT<$statement>\n";
495 #warn "CONDITION<$condition>\n";
497 #print "coff<$coff> soff<$off> loff<$loff>\n";
499 return ($statement, $condition,
500 $line, $remain + 1, $off - $loff + 1, $level);
503 sub statement_lines {
504 my ($stmt) = @_;
506 # Strip the diff line prefixes and rip blank lines at start and end.
507 $stmt =~ s/(^|\n)./$1/g;
508 $stmt =~ s/^\s*//;
509 $stmt =~ s/\s*$//;
511 my @stmt_lines = ($stmt =~ /\n/g);
513 return $#stmt_lines + 2;
516 sub statement_rawlines {
517 my ($stmt) = @_;
519 my @stmt_lines = ($stmt =~ /\n/g);
521 return $#stmt_lines + 2;
524 sub statement_block_size {
525 my ($stmt) = @_;
527 $stmt =~ s/(^|\n)./$1/g;
528 $stmt =~ s/^\s*{//;
529 $stmt =~ s/}\s*$//;
530 $stmt =~ s/^\s*//;
531 $stmt =~ s/\s*$//;
533 my @stmt_lines = ($stmt =~ /\n/g);
534 my @stmt_statements = ($stmt =~ /;/g);
536 my $stmt_lines = $#stmt_lines + 2;
537 my $stmt_statements = $#stmt_statements + 1;
539 if ($stmt_lines > $stmt_statements) {
540 return $stmt_lines;
541 } else {
542 return $stmt_statements;
546 sub ctx_statement_full {
547 my ($linenr, $remain, $off) = @_;
548 my ($statement, $condition, $level);
550 my (@chunks);
552 # Grab the first conditional/block pair.
553 ($statement, $condition, $linenr, $remain, $off, $level) =
554 ctx_statement_block($linenr, $remain, $off);
555 #print "F: c<$condition> s<$statement> remain<$remain>\n";
556 push(@chunks, [ $condition, $statement ]);
557 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
558 return ($level, $linenr, @chunks);
561 # Pull in the following conditional/block pairs and see if they
562 # could continue the statement.
563 for (;;) {
564 ($statement, $condition, $linenr, $remain, $off, $level) =
565 ctx_statement_block($linenr, $remain, $off);
566 #print "C: c<$condition> s<$statement> remain<$remain>\n";
567 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
568 #print "C: push\n";
569 push(@chunks, [ $condition, $statement ]);
572 return ($level, $linenr, @chunks);
575 sub ctx_block_get {
576 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
577 my $line;
578 my $start = $linenr - 1;
579 my $blk = '';
580 my @o;
581 my @c;
582 my @res = ();
584 my $level = 0;
585 for ($line = $start; $remain > 0; $line++) {
586 next if ($rawlines[$line] =~ /^-/);
587 $remain--;
589 $blk .= $rawlines[$line];
590 foreach my $c (split(//, $rawlines[$line])) {
591 ##print "C<$c>L<$level><$open$close>O<$off>\n";
592 if ($off > 0) {
593 $off--;
594 next;
597 if ($c eq $close && $level > 0) {
598 $level--;
599 last if ($level == 0);
600 } elsif ($c eq $open) {
601 $level++;
605 if (!$outer || $level <= 1) {
606 push(@res, $rawlines[$line]);
609 last if ($level == 0);
612 return ($level, @res);
614 sub ctx_block_outer {
615 my ($linenr, $remain) = @_;
617 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
618 return @r;
620 sub ctx_block {
621 my ($linenr, $remain) = @_;
623 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
624 return @r;
626 sub ctx_statement {
627 my ($linenr, $remain, $off) = @_;
629 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
630 return @r;
632 sub ctx_block_level {
633 my ($linenr, $remain) = @_;
635 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
637 sub ctx_statement_level {
638 my ($linenr, $remain, $off) = @_;
640 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
643 sub ctx_locate_comment {
644 my ($first_line, $end_line) = @_;
646 # Catch a comment on the end of the line itself.
647 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
648 return $current_comment if (defined $current_comment);
650 # Look through the context and try and figure out if there is a
651 # comment.
652 my $in_comment = 0;
653 $current_comment = '';
654 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
655 my $line = $rawlines[$linenr - 1];
656 #warn " $line\n";
657 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
658 $in_comment = 1;
660 if ($line =~ m@/\*@) {
661 $in_comment = 1;
663 if (!$in_comment && $current_comment ne '') {
664 $current_comment = '';
666 $current_comment .= $line . "\n" if ($in_comment);
667 if ($line =~ m@\*/@) {
668 $in_comment = 0;
672 chomp($current_comment);
673 return($current_comment);
675 sub ctx_has_comment {
676 my ($first_line, $end_line) = @_;
677 my $cmt = ctx_locate_comment($first_line, $end_line);
679 ##print "LINE: $rawlines[$end_line - 1 ]\n";
680 ##print "CMMT: $cmt\n";
682 return ($cmt ne '');
685 sub raw_line {
686 my ($linenr, $cnt) = @_;
688 my $offset = $linenr - 1;
689 $cnt++;
691 my $line;
692 while ($cnt) {
693 $line = $rawlines[$offset++];
694 next if (defined($line) && $line =~ /^-/);
695 $cnt--;
698 return $line;
701 sub cat_vet {
702 my ($vet) = @_;
703 my ($res, $coded);
705 $res = '';
706 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
707 $res .= $1;
708 if ($2 ne '') {
709 $coded = sprintf("^%c", unpack('C', $2) + 64);
710 $res .= $coded;
713 $res =~ s/$/\$/;
715 return $res;
718 my $av_preprocessor = 0;
719 my $av_pending;
720 my @av_paren_type;
721 my $av_pend_colon;
723 sub annotate_reset {
724 $av_preprocessor = 0;
725 $av_pending = '_';
726 @av_paren_type = ('E');
727 $av_pend_colon = 'O';
730 sub annotate_values {
731 my ($stream, $type) = @_;
733 my $res;
734 my $var = '_' x length($stream);
735 my $cur = $stream;
737 print "$stream\n" if ($dbg_values > 1);
739 while (length($cur)) {
740 @av_paren_type = ('E') if ($#av_paren_type < 0);
741 print " <" . join('', @av_paren_type) .
742 "> <$type> <$av_pending>" if ($dbg_values > 1);
743 if ($cur =~ /^(\s+)/o) {
744 print "WS($1)\n" if ($dbg_values > 1);
745 if ($1 =~ /\n/ && $av_preprocessor) {
746 $type = pop(@av_paren_type);
747 $av_preprocessor = 0;
750 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\()/) {
751 print "DECLARE($1)\n" if ($dbg_values > 1);
752 $type = 'T';
754 } elsif ($cur =~ /^($Modifier)\s*/) {
755 print "MODIFIER($1)\n" if ($dbg_values > 1);
756 $type = 'T';
758 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
759 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
760 $av_preprocessor = 1;
761 push(@av_paren_type, $type);
762 if ($2 ne '') {
763 $av_pending = 'N';
765 $type = 'E';
767 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
768 print "UNDEF($1)\n" if ($dbg_values > 1);
769 $av_preprocessor = 1;
770 push(@av_paren_type, $type);
772 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
773 print "PRE_START($1)\n" if ($dbg_values > 1);
774 $av_preprocessor = 1;
776 push(@av_paren_type, $type);
777 push(@av_paren_type, $type);
778 $type = 'E';
780 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
781 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
782 $av_preprocessor = 1;
784 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
786 $type = 'E';
788 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
789 print "PRE_END($1)\n" if ($dbg_values > 1);
791 $av_preprocessor = 1;
793 # Assume all arms of the conditional end as this
794 # one does, and continue as if the #endif was not here.
795 pop(@av_paren_type);
796 push(@av_paren_type, $type);
797 $type = 'E';
799 } elsif ($cur =~ /^(\\\n)/o) {
800 print "PRECONT($1)\n" if ($dbg_values > 1);
802 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
803 print "ATTR($1)\n" if ($dbg_values > 1);
804 $av_pending = $type;
805 $type = 'N';
807 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
808 print "SIZEOF($1)\n" if ($dbg_values > 1);
809 if (defined $2) {
810 $av_pending = 'V';
812 $type = 'N';
814 } elsif ($cur =~ /^(if|while|for)\b/o) {
815 print "COND($1)\n" if ($dbg_values > 1);
816 $av_pending = 'E';
817 $type = 'N';
819 } elsif ($cur =~/^(case)/o) {
820 print "CASE($1)\n" if ($dbg_values > 1);
821 $av_pend_colon = 'C';
822 $type = 'N';
824 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
825 print "KEYWORD($1)\n" if ($dbg_values > 1);
826 $type = 'N';
828 } elsif ($cur =~ /^(\()/o) {
829 print "PAREN('$1')\n" if ($dbg_values > 1);
830 push(@av_paren_type, $av_pending);
831 $av_pending = '_';
832 $type = 'N';
834 } elsif ($cur =~ /^(\))/o) {
835 my $new_type = pop(@av_paren_type);
836 if ($new_type ne '_') {
837 $type = $new_type;
838 print "PAREN('$1') -> $type\n"
839 if ($dbg_values > 1);
840 } else {
841 print "PAREN('$1')\n" if ($dbg_values > 1);
844 } elsif ($cur =~ /^($Ident)\s*\(/o) {
845 print "FUNC($1)\n" if ($dbg_values > 1);
846 $type = 'V';
847 $av_pending = 'V';
849 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
850 if (defined $2 && $type eq 'C' || $type eq 'T') {
851 $av_pend_colon = 'B';
852 } elsif ($type eq 'E') {
853 $av_pend_colon = 'L';
855 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
856 $type = 'V';
858 } elsif ($cur =~ /^($Ident|$Constant)/o) {
859 print "IDENT($1)\n" if ($dbg_values > 1);
860 $type = 'V';
862 } elsif ($cur =~ /^($Assignment)/o) {
863 print "ASSIGN($1)\n" if ($dbg_values > 1);
864 $type = 'N';
866 } elsif ($cur =~/^(;|{|})/) {
867 print "END($1)\n" if ($dbg_values > 1);
868 $type = 'E';
869 $av_pend_colon = 'O';
871 } elsif ($cur =~/^(,)/) {
872 print "COMMA($1)\n" if ($dbg_values > 1);
873 $type = 'C';
875 } elsif ($cur =~ /^(\?)/o) {
876 print "QUESTION($1)\n" if ($dbg_values > 1);
877 $type = 'N';
879 } elsif ($cur =~ /^(:)/o) {
880 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
882 substr($var, length($res), 1, $av_pend_colon);
883 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
884 $type = 'E';
885 } else {
886 $type = 'N';
888 $av_pend_colon = 'O';
890 } elsif ($cur =~ /^(\[)/o) {
891 print "CLOSE($1)\n" if ($dbg_values > 1);
892 $type = 'N';
894 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
895 my $variant;
897 print "OPV($1)\n" if ($dbg_values > 1);
898 if ($type eq 'V') {
899 $variant = 'B';
900 } else {
901 $variant = 'U';
904 substr($var, length($res), 1, $variant);
905 $type = 'N';
907 } elsif ($cur =~ /^($Operators)/o) {
908 print "OP($1)\n" if ($dbg_values > 1);
909 if ($1 ne '++' && $1 ne '--') {
910 $type = 'N';
913 } elsif ($cur =~ /(^.)/o) {
914 print "C($1)\n" if ($dbg_values > 1);
916 if (defined $1) {
917 $cur = substr($cur, length($1));
918 $res .= $type x length($1);
922 return ($res, $var);
925 sub possible {
926 my ($possible, $line) = @_;
928 print "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
929 if ($possible !~ /(?:
930 ^(?:
931 $Modifier|
932 $Storage|
933 $Type|
934 DEFINE_\S+|
935 goto|
936 return|
937 case|
938 else|
939 asm|__asm__|
942 ^(?:typedef|struct|enum)\b
943 )/x) {
944 # Check for modifiers.
945 $possible =~ s/\s*$Storage\s*//g;
946 $possible =~ s/\s*$Sparse\s*//g;
947 if ($possible =~ /^\s*$/) {
949 } elsif ($possible =~ /\s/) {
950 $possible =~ s/\s*$Type\s*//g;
951 for my $modifier (split(' ', $possible)) {
952 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
953 push(@modifierList, $modifier);
956 } else {
957 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
958 push(@typeList, $possible);
960 build_types();
961 } else {
962 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
966 my $prefix = '';
968 sub report {
969 if (defined $tst_only && $_[0] !~ /\Q$tst_only\E/) {
970 return 0;
972 my $line = $prefix . $_[0];
974 $line = (split('\n', $line))[0] . "\n" if ($terse);
976 push(our @report, $line);
978 return 1;
980 sub report_dump {
981 our @report;
983 sub ERROR {
984 if (report("ERROR: $_[0]\n")) {
985 our $clean = 0;
986 our $cnt_error++;
989 sub WARN {
990 if (report("WARNING: $_[0]\n")) {
991 our $clean = 0;
992 our $cnt_warn++;
995 sub CHK {
996 if ($check && report("CHECK: $_[0]\n")) {
997 our $clean = 0;
998 our $cnt_chk++;
1002 sub check_absolute_file {
1003 my ($absolute, $herecurr) = @_;
1004 my $file = $absolute;
1006 ##print "absolute<$absolute>\n";
1008 # See if any suffix of this path is a path within the tree.
1009 while ($file =~ s@^[^/]*/@@) {
1010 if (-f "$root/$file") {
1011 ##print "file<$file>\n";
1012 last;
1015 if (! -f _) {
1016 return 0;
1019 # It is, so see if the prefix is acceptable.
1020 my $prefix = $absolute;
1021 substr($prefix, -length($file)) = '';
1023 ##print "prefix<$prefix>\n";
1024 if ($prefix ne ".../") {
1025 WARN("use relative pathname instead of absolute in changelog text\n" . $herecurr);
1029 sub process {
1030 my $filename = shift;
1032 my $linenr=0;
1033 my $prevline="";
1034 my $prevrawline="";
1035 my $stashline="";
1036 my $stashrawline="";
1038 my $length;
1039 my $indent;
1040 my $previndent=0;
1041 my $stashindent=0;
1043 our $clean = 1;
1044 my $signoff = 0;
1045 my $is_patch = 0;
1047 our @report = ();
1048 our $cnt_lines = 0;
1049 our $cnt_error = 0;
1050 our $cnt_warn = 0;
1051 our $cnt_chk = 0;
1053 # Trace the real file/line as we go.
1054 my $realfile = '';
1055 my $realline = 0;
1056 my $realcnt = 0;
1057 my $here = '';
1058 my $in_comment = 0;
1059 my $comment_edge = 0;
1060 my $first_line = 0;
1061 my $p1_prefix = '';
1063 my $prev_values = 'E';
1065 # suppression flags
1066 my %suppress_ifbraces;
1067 my %suppress_whiletrailers;
1069 # Pre-scan the patch sanitizing the lines.
1070 # Pre-scan the patch looking for any __setup documentation.
1072 my @setup_docs = ();
1073 my $setup_docs = 0;
1075 sanitise_line_reset();
1076 my $line;
1077 foreach my $rawline (@rawlines) {
1078 $linenr++;
1079 $line = $rawline;
1081 if ($rawline=~/^\+\+\+\s+(\S+)/) {
1082 $setup_docs = 0;
1083 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
1084 $setup_docs = 1;
1086 #next;
1088 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1089 $realline=$1-1;
1090 if (defined $2) {
1091 $realcnt=$3+1;
1092 } else {
1093 $realcnt=1+1;
1095 $in_comment = 0;
1097 # Guestimate if this is a continuing comment. Run
1098 # the context looking for a comment "edge". If this
1099 # edge is a close comment then we must be in a comment
1100 # at context start.
1101 my $edge;
1102 my $cnt = $realcnt;
1103 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
1104 next if (defined $rawlines[$ln - 1] &&
1105 $rawlines[$ln - 1] =~ /^-/);
1106 $cnt--;
1107 #print "RAW<$rawlines[$ln - 1]>\n";
1108 last if (!defined $rawlines[$ln - 1]);
1109 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
1110 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
1111 ($edge) = $1;
1112 last;
1115 if (defined $edge && $edge eq '*/') {
1116 $in_comment = 1;
1119 # Guestimate if this is a continuing comment. If this
1120 # is the start of a diff block and this line starts
1121 # ' *' then it is very likely a comment.
1122 if (!defined $edge &&
1123 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
1125 $in_comment = 1;
1128 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
1129 sanitise_line_reset($in_comment);
1131 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
1132 # Standardise the strings and chars within the input to
1133 # simplify matching -- only bother with positive lines.
1134 $line = sanitise_line($rawline);
1136 push(@lines, $line);
1138 if ($realcnt > 1) {
1139 $realcnt-- if ($line =~ /^(?:\+| |$)/);
1140 } else {
1141 $realcnt = 0;
1144 #print "==>$rawline\n";
1145 #print "-->$line\n";
1147 if ($setup_docs && $line =~ /^\+/) {
1148 push(@setup_docs, $line);
1152 $prefix = '';
1154 $realcnt = 0;
1155 $linenr = 0;
1156 foreach my $line (@lines) {
1157 $linenr++;
1159 my $rawline = $rawlines[$linenr - 1];
1160 my $hunk_line = ($realcnt != 0);
1162 #extract the line range in the file after the patch is applied
1163 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
1164 $is_patch = 1;
1165 $first_line = $linenr + 1;
1166 $realline=$1-1;
1167 if (defined $2) {
1168 $realcnt=$3+1;
1169 } else {
1170 $realcnt=1+1;
1172 annotate_reset();
1173 $prev_values = 'E';
1175 %suppress_ifbraces = ();
1176 %suppress_whiletrailers = ();
1177 next;
1179 # track the line number as we move through the hunk, note that
1180 # new versions of GNU diff omit the leading space on completely
1181 # blank context lines so we need to count that too.
1182 } elsif ($line =~ /^( |\+|$)/) {
1183 $realline++;
1184 $realcnt-- if ($realcnt != 0);
1186 # Measure the line length and indent.
1187 ($length, $indent) = line_stats($rawline);
1189 # Track the previous line.
1190 ($prevline, $stashline) = ($stashline, $line);
1191 ($previndent, $stashindent) = ($stashindent, $indent);
1192 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
1194 #warn "line<$line>\n";
1196 } elsif ($realcnt == 1) {
1197 $realcnt--;
1200 #make up the handle for any error we report on this line
1201 $prefix = "$filename:$realline: " if ($emacs && $file);
1202 $prefix = "$filename:$linenr: " if ($emacs && !$file);
1204 $here = "#$linenr: " if (!$file);
1205 $here = "#$realline: " if ($file);
1207 # extract the filename as it passes
1208 if ($line=~/^\+\+\+\s+(\S+)/) {
1209 $realfile = $1;
1210 $realfile =~ s@^([^/]*)/@@;
1212 $p1_prefix = $1;
1213 if ($tree && $p1_prefix ne '' && -e "$root/$p1_prefix") {
1214 WARN("patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
1217 if ($realfile =~ m@^include/asm/@) {
1218 ERROR("do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
1220 next;
1223 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
1225 my $hereline = "$here\n$rawline\n";
1226 my $herecurr = "$here\n$rawline\n";
1227 my $hereprev = "$here\n$prevrawline\n$rawline\n";
1229 $cnt_lines++ if ($realcnt != 0);
1231 #check the patch for a signoff:
1232 if ($line =~ /^\s*signed-off-by:/i) {
1233 # This is a signoff, if ugly, so do not double report.
1234 $signoff++;
1235 if (!($line =~ /^\s*Signed-off-by:/)) {
1236 WARN("Signed-off-by: is the preferred form\n" .
1237 $herecurr);
1239 if ($line =~ /^\s*signed-off-by:\S/i) {
1240 WARN("space required after Signed-off-by:\n" .
1241 $herecurr);
1245 # Check for wrappage within a valid hunk of the file
1246 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1247 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1248 $herecurr) if (!$emitted_corrupt++);
1251 # Check for absolute kernel paths.
1252 if ($tree) {
1253 while ($line =~ m{(?:^|\s)(/\S*)}g) {
1254 my $file = $1;
1256 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
1257 check_absolute_file($1, $herecurr)) {
1259 } else {
1260 check_absolute_file($file, $herecurr);
1265 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1266 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1267 $rawline !~ m/^$UTF8*$/) {
1268 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
1270 my $blank = copy_spacing($rawline);
1271 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
1272 my $hereptr = "$hereline$ptr\n";
1274 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
1277 # ignore non-hunk lines and lines being removed
1278 next if (!$hunk_line || $line =~ /^-/);
1280 #trailing whitespace
1281 if ($line =~ /^\+.*\015/) {
1282 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1283 ERROR("DOS line endings\n" . $herevet);
1285 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1286 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1287 ERROR("trailing whitespace\n" . $herevet);
1290 # check we are in a valid source file if not then ignore this hunk
1291 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1293 #80 column limit
1294 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1295 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1296 $line !~ /^\+\s*printk\s*\(\s*(?:KERN_\S+\s*)?"[X\t]*"\s*(?:,|\)\s*;)\s*$/ &&
1297 $length > 80)
1299 WARN("line over 80 characters\n" . $herecurr);
1302 # check for adding lines without a newline.
1303 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1304 WARN("adding a line without newline at end of file\n" . $herecurr);
1307 # check we are in a valid source file C or perl if not then ignore this hunk
1308 next if ($realfile !~ /\.(h|c|pl)$/);
1310 # at the beginning of a line any tabs must come first and anything
1311 # more than 8 must use tabs.
1312 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1313 $rawline =~ /^\+\s* \s*/) {
1314 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1315 ERROR("code indent should use tabs where possible\n" . $herevet);
1318 # check we are in a valid C source file if not then ignore this hunk
1319 next if ($realfile !~ /\.(h|c)$/);
1321 # check for RCS/CVS revision markers
1322 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1323 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1326 # Check for potential 'bare' types
1327 my ($stat, $cond, $line_nr_next, $remain_next, $off_next);
1328 if ($realcnt && $line =~ /.\s*\S/) {
1329 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
1330 ctx_statement_block($linenr, $realcnt, 0);
1331 $stat =~ s/\n./\n /g;
1332 $cond =~ s/\n./\n /g;
1334 my $s = $stat;
1335 $s =~ s/{.*$//s;
1337 # Ignore goto labels.
1338 if ($s =~ /$Ident:\*$/s) {
1340 # Ignore functions being called
1341 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
1343 # declarations always start with types
1344 } 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) {
1345 my $type = $1;
1346 $type =~ s/\s+/ /g;
1347 possible($type, "A:" . $s);
1349 # definitions in global scope can only start with types
1350 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
1351 possible($1, "B:" . $s);
1354 # any (foo ... *) is a pointer cast, and foo is a type
1355 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
1356 possible($1, "C:" . $s);
1359 # Check for any sort of function declaration.
1360 # int foo(something bar, other baz);
1361 # void (*store_gdt)(x86_descr_ptr *);
1362 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
1363 my ($name_len) = length($1);
1365 my $ctx = $s;
1366 substr($ctx, 0, $name_len + 1, '');
1367 $ctx =~ s/\)[^\)]*$//;
1369 for my $arg (split(/\s*,\s*/, $ctx)) {
1370 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
1372 possible($1, "D:" . $s);
1380 # Checks which may be anchored in the context.
1383 # Check for switch () and associated case and default
1384 # statements should be at the same indent.
1385 if ($line=~/\bswitch\s*\(.*\)/) {
1386 my $err = '';
1387 my $sep = '';
1388 my @ctx = ctx_block_outer($linenr, $realcnt);
1389 shift(@ctx);
1390 for my $ctx (@ctx) {
1391 my ($clen, $cindent) = line_stats($ctx);
1392 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1393 $indent != $cindent) {
1394 $err .= "$sep$ctx\n";
1395 $sep = '';
1396 } else {
1397 $sep = "[...]\n";
1400 if ($err ne '') {
1401 ERROR("switch and case should be at the same indent\n$hereline$err");
1405 # if/while/etc brace do not go on next line, unless defining a do while loop,
1406 # or if that brace on the next line is for something else
1407 if ($line =~ /(.*)\b((?:if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
1408 my $pre_ctx = "$1$2";
1410 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1411 my $ctx_cnt = $realcnt - $#ctx - 1;
1412 my $ctx = join("\n", @ctx);
1414 my $ctx_ln = $linenr;
1415 my $ctx_skip = $realcnt;
1417 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
1418 defined $lines[$ctx_ln - 1] &&
1419 $lines[$ctx_ln - 1] =~ /^-/)) {
1420 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
1421 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
1422 $ctx_ln++;
1425 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
1426 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
1428 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln -1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1429 ERROR("that open brace { should be on the previous line\n" .
1430 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1432 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
1433 $ctx =~ /\)\s*\;\s*$/ &&
1434 defined $lines[$ctx_ln - 1])
1436 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1437 if ($nindent > $indent) {
1438 WARN("trailing semicolon indicates no statements, indent implies otherwise\n" .
1439 "$here\n$ctx\n$lines[$ctx_ln - 1]\n");
1444 # Check relative indent for conditionals and blocks.
1445 if ($line =~ /\b(?:(?:if|while|for)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
1446 my ($s, $c) = ($stat, $cond);
1448 substr($s, 0, length($c), '');
1450 # Make sure we remove the line prefixes as we have
1451 # none on the first line, and are going to readd them
1452 # where necessary.
1453 $s =~ s/\n./\n/gs;
1455 # Find out how long the conditional actually is.
1456 my @newlines = ($c =~ /\n/gs);
1457 my $cond_lines = 1 + $#newlines;
1459 # We want to check the first line inside the block
1460 # starting at the end of the conditional, so remove:
1461 # 1) any blank line termination
1462 # 2) any opening brace { on end of the line
1463 # 3) any do (...) {
1464 my $continuation = 0;
1465 my $check = 0;
1466 $s =~ s/^.*\bdo\b//;
1467 $s =~ s/^\s*{//;
1468 if ($s =~ s/^\s*\\//) {
1469 $continuation = 1;
1471 if ($s =~ s/^\s*?\n//) {
1472 $check = 1;
1473 $cond_lines++;
1476 # Also ignore a loop construct at the end of a
1477 # preprocessor statement.
1478 if (($prevline =~ /^.\s*#\s*define\s/ ||
1479 $prevline =~ /\\\s*$/) && $continuation == 0) {
1480 $check = 0;
1483 my $cond_ptr = -1;
1484 $continuation = 0;
1485 while ($cond_ptr != $cond_lines) {
1486 $cond_ptr = $cond_lines;
1488 # If we see an #else/#elif then the code
1489 # is not linear.
1490 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
1491 $check = 0;
1494 # Ignore:
1495 # 1) blank lines, they should be at 0,
1496 # 2) preprocessor lines, and
1497 # 3) labels.
1498 if ($continuation ||
1499 $s =~ /^\s*?\n/ ||
1500 $s =~ /^\s*#\s*?/ ||
1501 $s =~ /^\s*$Ident\s*:/) {
1502 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
1503 $s =~ s/^.*?\n//;
1504 $cond_lines++;
1508 my (undef, $sindent) = line_stats("+" . $s);
1509 my $stat_real = raw_line($linenr, $cond_lines);
1511 # Check if either of these lines are modified, else
1512 # this is not this patch's fault.
1513 if (!defined($stat_real) ||
1514 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
1515 $check = 0;
1517 if (defined($stat_real) && $cond_lines > 1) {
1518 $stat_real = "[...]\n$stat_real";
1521 #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";
1523 if ($check && (($sindent % 8) != 0 ||
1524 ($sindent <= $indent && $s ne ''))) {
1525 WARN("suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
1529 # Track the 'values' across context and added lines.
1530 my $opline = $line; $opline =~ s/^./ /;
1531 my ($curr_values, $curr_vars) =
1532 annotate_values($opline . "\n", $prev_values);
1533 $curr_values = $prev_values . $curr_values;
1534 if ($dbg_values) {
1535 my $outline = $opline; $outline =~ s/\t/ /g;
1536 print "$linenr > .$outline\n";
1537 print "$linenr > $curr_values\n";
1538 print "$linenr > $curr_vars\n";
1540 $prev_values = substr($curr_values, -1);
1542 #ignore lines not being added
1543 if ($line=~/^[^\+]/) {next;}
1545 # TEST: allow direct testing of the type matcher.
1546 if ($dbg_type) {
1547 if ($line =~ /^.\s*$Declare\s*$/) {
1548 ERROR("TEST: is type\n" . $herecurr);
1549 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
1550 ERROR("TEST: is not type ($1 is)\n". $herecurr);
1552 next;
1554 # TEST: allow direct testing of the attribute matcher.
1555 if ($dbg_attr) {
1556 if ($line =~ /^.\s*$Attribute\s*$/) {
1557 ERROR("TEST: is attr\n" . $herecurr);
1558 } elsif ($dbg_attr > 1 && $line =~ /^.+($Attribute)/) {
1559 ERROR("TEST: is not attr ($1 is)\n". $herecurr);
1561 next;
1564 # check for initialisation to aggregates open brace on the next line
1565 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1566 $line =~ /^.\s*{/) {
1567 ERROR("that open brace { should be on the previous line\n" . $hereprev);
1571 # Checks which are anchored on the added line.
1574 # check for malformed paths in #include statements (uses RAW line)
1575 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
1576 my $path = $1;
1577 if ($path =~ m{//}) {
1578 ERROR("malformed #include filename\n" .
1579 $herecurr);
1583 # no C99 // comments
1584 if ($line =~ m{//}) {
1585 ERROR("do not use C99 // comments\n" . $herecurr);
1587 # Remove C99 comments.
1588 $line =~ s@//.*@@;
1589 $opline =~ s@//.*@@;
1591 #EXPORT_SYMBOL should immediately follow its function closing }.
1592 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1593 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1594 my $name = $1;
1595 if ($prevline !~ /(?:
1596 ^.}|
1597 ^.DEFINE_$Ident\(\Q$name\E\)|
1598 ^.DECLARE_$Ident\(\Q$name\E\)|
1599 ^.LIST_HEAD\(\Q$name\E\)|
1600 ^.$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
1601 \b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)
1602 )/x) {
1603 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1607 # check for external initialisers.
1608 if ($line =~ /^.$Type\s*$Ident\s*(?:\s+$Modifier)*\s*=\s*(0|NULL|false)\s*;/) {
1609 ERROR("do not initialise externals to 0 or NULL\n" .
1610 $herecurr);
1612 # check for static initialisers.
1613 if ($line =~ /\s*static\s.*=\s*(0|NULL|false)\s*;/) {
1614 ERROR("do not initialise statics to 0 or NULL\n" .
1615 $herecurr);
1618 # check for new typedefs, only function parameters and sparse annotations
1619 # make sense.
1620 if ($line =~ /\btypedef\s/ &&
1621 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
1622 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
1623 $line !~ /\b$typeTypedefs\b/ &&
1624 $line !~ /\b__bitwise(?:__|)\b/) {
1625 WARN("do not add new typedefs\n" . $herecurr);
1628 # * goes on variable not on type
1629 # (char*[ const])
1630 if ($line =~ m{\($NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)*)\)}) {
1631 my ($from, $to) = ($1, $1);
1633 # Should start with a space.
1634 $to =~ s/^(\S)/ $1/;
1635 # Should not end with a space.
1636 $to =~ s/\s+$//;
1637 # '*'s should not have spaces between.
1638 while ($to =~ s/(.)\s\*/$1\*/) {
1641 #print "from<$from> to<$to>\n";
1642 if ($from ne $to) {
1643 ERROR("\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr);
1645 } elsif ($line =~ m{\b$NonptrType(\s*\*[\s\*]*(?:$Modifier\s*)?)($Ident)}) {
1646 my ($from, $to, $ident) = ($1, $1, $2);
1648 # Should start with a space.
1649 $to =~ s/^(\S)/ $1/;
1650 # Should not end with a space.
1651 $to =~ s/\s+$//;
1652 # '*'s should not have spaces between.
1653 while ($to =~ s/(.)\s\*/$1\*/) {
1655 # Modifiers should have spaces.
1656 $to =~ s/(\b$Modifier$)/$1 /;
1658 #print "from<$from> to<$to>\n";
1659 if ($from ne $to) {
1660 ERROR("\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr);
1664 # # no BUG() or BUG_ON()
1665 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1666 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1667 # print "$herecurr";
1668 # $clean = 0;
1671 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1672 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1675 # printk should use KERN_* levels. Note that follow on printk's on the
1676 # same line do not need a level, so we use the current block context
1677 # to try and find and validate the current printk. In summary the current
1678 # printk includes all preceeding printk's which have no newline on the end.
1679 # we assume the first bad printk is the one to report.
1680 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1681 my $ok = 0;
1682 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1683 #print "CHECK<$lines[$ln - 1]\n";
1684 # we have a preceeding printk if it ends
1685 # with "\n" ignore it, else it is to blame
1686 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1687 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1688 $ok = 1;
1690 last;
1693 if ($ok == 0) {
1694 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1698 # function brace can't be on same line, except for #defines of do while,
1699 # or if closed on same line
1700 if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
1701 !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
1702 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1705 # open braces for enum, union and struct go on the same line.
1706 if ($line =~ /^.\s*{/ &&
1707 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1708 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1711 # check for spacing round square brackets; allowed:
1712 # 1. with a type on the left -- int [] a;
1713 # 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
1714 # 3. inside a curly brace -- = { [0...10] = 5 }
1715 while ($line =~ /(.*?\s)\[/g) {
1716 my ($where, $prefix) = ($-[1], $1);
1717 if ($prefix !~ /$Type\s+$/ &&
1718 ($where != 0 || $prefix !~ /^.\s+$/) &&
1719 $prefix !~ /{\s+$/) {
1720 ERROR("space prohibited before open square bracket '['\n" . $herecurr);
1724 # check for spaces between functions and their parentheses.
1725 while ($line =~ /($Ident)\s+\(/g) {
1726 my $name = $1;
1727 my $ctx_before = substr($line, 0, $-[1]);
1728 my $ctx = "$ctx_before$name";
1730 # Ignore those directives where spaces _are_ permitted.
1731 if ($name =~ /^(?:
1732 if|for|while|switch|return|case|
1733 volatile|__volatile__|
1734 __attribute__|format|__extension__|
1735 asm|__asm__)$/x)
1738 # cpp #define statements have non-optional spaces, ie
1739 # if there is a space between the name and the open
1740 # parenthesis it is simply not a parameter group.
1741 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
1743 # cpp #elif statement condition may start with a (
1744 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
1746 # If this whole things ends with a type its most
1747 # likely a typedef for a function.
1748 } elsif ($ctx =~ /$Type$/) {
1750 } else {
1751 WARN("space prohibited between function name and open parenthesis '('\n" . $herecurr);
1754 # Check operator spacing.
1755 if (!($line=~/\#\s*include/)) {
1756 my $ops = qr{
1757 <<=|>>=|<=|>=|==|!=|
1758 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1759 =>|->|<<|>>|<|>|=|!|~|
1760 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
1761 \?|:
1763 my @elements = split(/($ops|;)/, $opline);
1764 my $off = 0;
1766 my $blank = copy_spacing($opline);
1768 for (my $n = 0; $n < $#elements; $n += 2) {
1769 $off += length($elements[$n]);
1771 # Pick up the preceeding and succeeding characters.
1772 my $ca = substr($opline, 0, $off);
1773 my $cc = '';
1774 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1775 $cc = substr($opline, $off + length($elements[$n + 1]));
1777 my $cb = "$ca$;$cc";
1779 my $a = '';
1780 $a = 'V' if ($elements[$n] ne '');
1781 $a = 'W' if ($elements[$n] =~ /\s$/);
1782 $a = 'C' if ($elements[$n] =~ /$;$/);
1783 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1784 $a = 'O' if ($elements[$n] eq '');
1785 $a = 'E' if ($ca =~ /^\s*$/);
1787 my $op = $elements[$n + 1];
1789 my $c = '';
1790 if (defined $elements[$n + 2]) {
1791 $c = 'V' if ($elements[$n + 2] ne '');
1792 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1793 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1794 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1795 $c = 'O' if ($elements[$n + 2] eq '');
1796 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
1797 } else {
1798 $c = 'E';
1801 my $ctx = "${a}x${c}";
1803 my $at = "(ctx:$ctx)";
1805 my $ptr = substr($blank, 0, $off) . "^";
1806 my $hereptr = "$hereline$ptr\n";
1808 # Pull out the value of this operator.
1809 my $op_type = substr($curr_values, $off + 1, 1);
1811 # Get the full operator variant.
1812 my $opv = $op . substr($curr_vars, $off, 1);
1814 # Ignore operators passed as parameters.
1815 if ($op_type ne 'V' &&
1816 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1818 # # Ignore comments
1819 # } elsif ($op =~ /^$;+$/) {
1821 # ; should have either the end of line or a space or \ after it
1822 } elsif ($op eq ';') {
1823 if ($ctx !~ /.x[WEBC]/ &&
1824 $cc !~ /^\\/ && $cc !~ /^;/) {
1825 ERROR("space required after that '$op' $at\n" . $hereptr);
1828 # // is a comment
1829 } elsif ($op eq '//') {
1831 # No spaces for:
1832 # ->
1833 # : when part of a bitfield
1834 } elsif ($op eq '->' || $opv eq ':B') {
1835 if ($ctx =~ /Wx.|.xW/) {
1836 ERROR("spaces prohibited around that '$op' $at\n" . $hereptr);
1839 # , must have a space on the right.
1840 } elsif ($op eq ',') {
1841 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1842 ERROR("space required after that '$op' $at\n" . $hereptr);
1845 # '*' as part of a type definition -- reported already.
1846 } elsif ($opv eq '*_') {
1847 #warn "'*' is part of type\n";
1849 # unary operators should have a space before and
1850 # none after. May be left adjacent to another
1851 # unary operator, or a cast
1852 } elsif ($op eq '!' || $op eq '~' ||
1853 $opv eq '*U' || $opv eq '-U' ||
1854 $opv eq '&U' || $opv eq '&&U') {
1855 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1856 ERROR("space required before that '$op' $at\n" . $hereptr);
1858 if ($op eq '*' && $cc =~/\s*const\b/) {
1859 # A unary '*' may be const
1861 } elsif ($ctx =~ /.xW/) {
1862 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1865 # unary ++ and unary -- are allowed no space on one side.
1866 } elsif ($op eq '++' or $op eq '--') {
1867 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1868 ERROR("space required one side of that '$op' $at\n" . $hereptr);
1870 if ($ctx =~ /Wx[BE]/ ||
1871 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1872 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1874 if ($ctx =~ /ExW/) {
1875 ERROR("space prohibited after that '$op' $at\n" . $hereptr);
1879 # << and >> may either have or not have spaces both sides
1880 } elsif ($op eq '<<' or $op eq '>>' or
1881 $op eq '&' or $op eq '^' or $op eq '|' or
1882 $op eq '+' or $op eq '-' or
1883 $op eq '*' or $op eq '/' or
1884 $op eq '%')
1886 if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
1887 ERROR("need consistent spacing around '$op' $at\n" .
1888 $hereptr);
1891 # A colon needs no spaces before when it is
1892 # terminating a case value or a label.
1893 } elsif ($opv eq ':C' || $opv eq ':L') {
1894 if ($ctx =~ /Wx./) {
1895 ERROR("space prohibited before that '$op' $at\n" . $hereptr);
1898 # All the others need spaces both sides.
1899 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1900 my $ok = 0;
1902 # Ignore email addresses <foo@bar>
1903 if (($op eq '<' &&
1904 $cc =~ /^\S+\@\S+>/) ||
1905 ($op eq '>' &&
1906 $ca =~ /<\S+\@\S+$/))
1908 $ok = 1;
1911 # Ignore ?:
1912 if (($opv eq ':O' && $ca =~ /\?$/) ||
1913 ($op eq '?' && $cc =~ /^:/)) {
1914 $ok = 1;
1917 if ($ok == 0) {
1918 ERROR("spaces required around that '$op' $at\n" . $hereptr);
1921 $off += length($elements[$n + 1]);
1925 # check for multiple assignments
1926 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1927 CHK("multiple assignments should be avoided\n" . $herecurr);
1930 ## # check for multiple declarations, allowing for a function declaration
1931 ## # continuation.
1932 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1933 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1935 ## # Remove any bracketed sections to ensure we do not
1936 ## # falsly report the parameters of functions.
1937 ## my $ln = $line;
1938 ## while ($ln =~ s/\([^\(\)]*\)//g) {
1939 ## }
1940 ## if ($ln =~ /,/) {
1941 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1942 ## }
1943 ## }
1945 #need space before brace following if, while, etc
1946 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1947 $line =~ /do{/) {
1948 ERROR("space required before the open brace '{'\n" . $herecurr);
1951 # closing brace should have a space following it when it has anything
1952 # on the line
1953 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1954 ERROR("space required after that close brace '}'\n" . $herecurr);
1957 # check spacing on square brackets
1958 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1959 ERROR("space prohibited after that open square bracket '['\n" . $herecurr);
1961 if ($line =~ /\s\]/) {
1962 ERROR("space prohibited before that close square bracket ']'\n" . $herecurr);
1965 # check spacing on parentheses
1966 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1967 $line !~ /for\s*\(\s+;/) {
1968 ERROR("space prohibited after that open parenthesis '('\n" . $herecurr);
1970 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1971 $line !~ /for\s*\(.*;\s+\)/ &&
1972 $line !~ /:\s+\)/) {
1973 ERROR("space prohibited before that close parenthesis ')'\n" . $herecurr);
1976 #goto labels aren't indented, allow a single space however
1977 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
1978 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1979 WARN("labels should not be indented\n" . $herecurr);
1982 # Return is not a function.
1983 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) {
1984 my $spacing = $1;
1985 my $value = $2;
1987 # Flatten any parentheses
1988 $value =~ s/\)\(/\) \(/g;
1989 while ($value !~ /(?:$Ident|-?$Constant)\s*$Compare\s*(?:$Ident|-?$Constant)/ && $value =~ s/\([^\(\)]*\)/1/) {
1992 if ($value =~ /^(?:$Ident|-?$Constant)$/) {
1993 ERROR("return is not a function, parentheses are not required\n" . $herecurr);
1995 } elsif ($spacing !~ /\s+/) {
1996 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2000 # Need a space before open parenthesis after if, while etc
2001 if ($line=~/\b(if|while|for|switch)\(/) {
2002 ERROR("space required before the open parenthesis '('\n" . $herecurr);
2005 # Check for illegal assignment in if conditional -- and check for trailing
2006 # statements after the conditional.
2007 if ($line =~ /do\s*(?!{)/) {
2008 my ($stat_next) = ctx_statement_block($line_nr_next,
2009 $remain_next, $off_next);
2010 $stat_next =~ s/\n./\n /g;
2011 ##print "stat<$stat> stat_next<$stat_next>\n";
2013 if ($stat_next =~ /^\s*while\b/) {
2014 # If the statement carries leading newlines,
2015 # then count those as offsets.
2016 my ($whitespace) =
2017 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
2018 my $offset =
2019 statement_rawlines($whitespace) - 1;
2021 $suppress_whiletrailers{$line_nr_next +
2022 $offset} = 1;
2025 if (!defined $suppress_whiletrailers{$linenr} &&
2026 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
2027 my ($s, $c) = ($stat, $cond);
2029 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
2030 ERROR("do not use assignment in if condition\n" . $herecurr);
2033 # Find out what is on the end of the line after the
2034 # conditional.
2035 substr($s, 0, length($c), '');
2036 $s =~ s/\n.*//g;
2037 $s =~ s/$;//g; # Remove any comments
2038 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
2039 $c !~ /}\s*while\s*/)
2041 # Find out how long the conditional actually is.
2042 my @newlines = ($c =~ /\n/gs);
2043 my $cond_lines = 1 + $#newlines;
2045 my $stat_real = raw_line($linenr, $cond_lines);
2046 if (defined($stat_real) && $cond_lines > 1) {
2047 $stat_real = "[...]\n$stat_real";
2050 ERROR("trailing statements should be on next line\n" . $herecurr . $stat_real);
2054 # Check for bitwise tests written as boolean
2055 if ($line =~ /
2057 (?:\[|\(|\&\&|\|\|)
2058 \s*0[xX][0-9]+\s*
2059 (?:\&\&|\|\|)
2061 (?:\&\&|\|\|)
2062 \s*0[xX][0-9]+\s*
2063 (?:\&\&|\|\||\)|\])
2064 )/x)
2066 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
2069 # if and else should not have general statements after it
2070 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
2071 my $s = $1;
2072 $s =~ s/$;//g; # Remove any comments
2073 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
2074 ERROR("trailing statements should be on next line\n" . $herecurr);
2077 # case and default should not have general statements after them
2078 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
2079 $line !~ /\G(?:
2080 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
2081 \s*return\s+
2082 )/xg)
2084 ERROR("trailing statements should be on next line\n" . $herecurr);
2087 # Check for }<nl>else {, these must be at the same
2088 # indent level to be relevant to each other.
2089 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
2090 $previndent == $indent) {
2091 ERROR("else should follow close brace '}'\n" . $hereprev);
2094 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
2095 $previndent == $indent) {
2096 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
2098 # Find out what is on the end of the line after the
2099 # conditional.
2100 substr($s, 0, length($c), '');
2101 $s =~ s/\n.*//g;
2103 if ($s =~ /^\s*;/) {
2104 ERROR("while should follow close brace '}'\n" . $hereprev);
2108 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
2109 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
2110 # print "No studly caps, use _\n";
2111 # print "$herecurr";
2112 # $clean = 0;
2115 #no spaces allowed after \ in define
2116 if ($line=~/\#\s*define.*\\\s$/) {
2117 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
2120 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
2121 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
2122 my $file = "$1.h";
2123 my $checkfile = "include/linux/$file";
2124 if (-f "$root/$checkfile" &&
2125 $realfile ne $checkfile &&
2126 $1 ne 'irq')
2128 if ($realfile =~ m{^arch/}) {
2129 CHK("Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2130 } else {
2131 WARN("Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
2136 # multi-statement macros should be enclosed in a do while loop, grab the
2137 # first statement and ensure its the whole macro if its not enclosed
2138 # in a known good container
2139 if ($realfile !~ m@/vmlinux.lds.h$@ &&
2140 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
2141 my $ln = $linenr;
2142 my $cnt = $realcnt;
2143 my ($off, $dstat, $dcond, $rest);
2144 my $ctx = '';
2146 my $args = defined($1);
2148 # Find the end of the macro and limit our statement
2149 # search to that.
2150 while ($cnt > 0 && defined $lines[$ln - 1] &&
2151 $lines[$ln - 1] =~ /^(?:-|..*\\$)/)
2153 $ctx .= $rawlines[$ln - 1] . "\n";
2154 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2155 $ln++;
2157 $ctx .= $rawlines[$ln - 1];
2159 ($dstat, $dcond, $ln, $cnt, $off) =
2160 ctx_statement_block($linenr, $ln - $linenr + 1, 0);
2161 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
2162 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
2164 # Extract the remainder of the define (if any) and
2165 # rip off surrounding spaces, and trailing \'s.
2166 $rest = '';
2167 while ($off != 0 || ($cnt > 0 && $rest =~ /\\\s*$/)) {
2168 #print "ADDING cnt<$cnt> $off <" . substr($lines[$ln - 1], $off) . "> rest<$rest>\n";
2169 if ($off != 0 || $lines[$ln - 1] !~ /^-/) {
2170 $rest .= substr($lines[$ln - 1], $off) . "\n";
2171 $cnt--;
2173 $ln++;
2174 $off = 0;
2176 $rest =~ s/\\\n.//g;
2177 $rest =~ s/^\s*//s;
2178 $rest =~ s/\s*$//s;
2180 # Clean up the original statement.
2181 if ($args) {
2182 substr($dstat, 0, length($dcond), '');
2183 } else {
2184 $dstat =~ s/^.\s*\#\s*define\s+$Ident\s*//;
2186 $dstat =~ s/$;//g;
2187 $dstat =~ s/\\\n.//g;
2188 $dstat =~ s/^\s*//s;
2189 $dstat =~ s/\s*$//s;
2191 # Flatten any parentheses and braces
2192 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
2193 $dstat =~ s/\{[^\{\}]*\}/1/ ||
2194 $dstat =~ s/\[[^\{\}]*\]/1/)
2198 my $exceptions = qr{
2199 $Declare|
2200 module_param_named|
2201 MODULE_PARAM_DESC|
2202 DECLARE_PER_CPU|
2203 DEFINE_PER_CPU|
2204 __typeof__\(|
2205 \.$Ident\s*=\s*
2207 #print "REST<$rest> dstat<$dstat>\n";
2208 if ($rest ne '') {
2209 if ($rest !~ /while\s*\(/ &&
2210 $dstat !~ /$exceptions/)
2212 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
2215 } elsif ($ctx !~ /;/) {
2216 if ($dstat ne '' &&
2217 $dstat !~ /^(?:$Ident|-?$Constant)$/ &&
2218 $dstat !~ /$exceptions/ &&
2219 $dstat !~ /^\.$Ident\s*=/ &&
2220 $dstat =~ /$Operators/)
2222 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
2227 # make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
2228 # all assignments may have only one of the following with an assignment:
2230 # ALIGN(...)
2231 # VMLINUX_SYMBOL(...)
2232 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
2233 WARN("vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
2236 # check for redundant bracing round if etc
2237 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
2238 my ($level, $endln, @chunks) =
2239 ctx_statement_full($linenr, $realcnt, 1);
2240 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
2241 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
2242 if ($#chunks > 0 && $level == 0) {
2243 my $allowed = 0;
2244 my $seen = 0;
2245 my $herectx = $here . "\n";
2246 my $ln = $linenr - 1;
2247 for my $chunk (@chunks) {
2248 my ($cond, $block) = @{$chunk};
2250 # If the condition carries leading newlines, then count those as offsets.
2251 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
2252 my $offset = statement_rawlines($whitespace) - 1;
2254 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
2256 # We have looked at and allowed this specific line.
2257 $suppress_ifbraces{$ln + $offset} = 1;
2259 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
2260 $ln += statement_rawlines($block) - 1;
2262 substr($block, 0, length($cond), '');
2264 $seen++ if ($block =~ /^\s*{/);
2266 #print "cond<$cond> block<$block> allowed<$allowed>\n";
2267 if (statement_lines($cond) > 1) {
2268 #print "APW: ALLOWED: cond<$cond>\n";
2269 $allowed = 1;
2271 if ($block =~/\b(?:if|for|while)\b/) {
2272 #print "APW: ALLOWED: block<$block>\n";
2273 $allowed = 1;
2275 if (statement_block_size($block) > 1) {
2276 #print "APW: ALLOWED: lines block<$block>\n";
2277 $allowed = 1;
2280 if ($seen && !$allowed) {
2281 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
2285 if (!defined $suppress_ifbraces{$linenr - 1} &&
2286 $line =~ /\b(if|while|for|else)\b/) {
2287 my $allowed = 0;
2289 # Check the pre-context.
2290 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
2291 #print "APW: ALLOWED: pre<$1>\n";
2292 $allowed = 1;
2295 my ($level, $endln, @chunks) =
2296 ctx_statement_full($linenr, $realcnt, $-[0]);
2298 # Check the condition.
2299 my ($cond, $block) = @{$chunks[0]};
2300 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
2301 if (defined $cond) {
2302 substr($block, 0, length($cond), '');
2304 if (statement_lines($cond) > 1) {
2305 #print "APW: ALLOWED: cond<$cond>\n";
2306 $allowed = 1;
2308 if ($block =~/\b(?:if|for|while)\b/) {
2309 #print "APW: ALLOWED: block<$block>\n";
2310 $allowed = 1;
2312 if (statement_block_size($block) > 1) {
2313 #print "APW: ALLOWED: lines block<$block>\n";
2314 $allowed = 1;
2316 # Check the post-context.
2317 if (defined $chunks[1]) {
2318 my ($cond, $block) = @{$chunks[1]};
2319 if (defined $cond) {
2320 substr($block, 0, length($cond), '');
2322 if ($block =~ /^\s*\{/) {
2323 #print "APW: ALLOWED: chunk-1 block<$block>\n";
2324 $allowed = 1;
2327 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
2328 my $herectx = $here . "\n";;
2329 my $cnt = statement_rawlines($block);
2331 for (my $n = 0; $n < $cnt; $n++) {
2332 $herectx .= raw_line($linenr, $n) . "\n";;
2335 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
2339 # don't include deprecated include files (uses RAW line)
2340 for my $inc (@dep_includes) {
2341 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) {
2342 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2346 # don't use deprecated functions
2347 for my $func (@dep_functions) {
2348 if ($line =~ /\b$func\b/) {
2349 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
2353 # no volatiles please
2354 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
2355 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
2356 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
2359 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
2360 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
2361 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
2364 # warn about #if 0
2365 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
2366 CHK("if this code is redundant consider removing it\n" .
2367 $herecurr);
2370 # check for needless kfree() checks
2371 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2372 my $expr = $1;
2373 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
2374 WARN("kfree(NULL) is safe this check is probably not required\n" . $hereprev);
2377 # check for needless usb_free_urb() checks
2378 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
2379 my $expr = $1;
2380 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
2381 WARN("usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
2385 # warn about #ifdefs in C files
2386 # if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
2387 # print "#ifdef in C files should be avoided\n";
2388 # print "$herecurr";
2389 # $clean = 0;
2392 # warn about spacing in #ifdefs
2393 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
2394 ERROR("exactly one space required after that #$1\n" . $herecurr);
2397 # check for spinlock_t definitions without a comment.
2398 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
2399 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
2400 my $which = $1;
2401 if (!ctx_has_comment($first_line, $linenr)) {
2402 CHK("$1 definition without comment\n" . $herecurr);
2405 # check for memory barriers without a comment.
2406 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
2407 if (!ctx_has_comment($first_line, $linenr)) {
2408 CHK("memory barrier without comment\n" . $herecurr);
2411 # check of hardware specific defines
2412 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
2413 CHK("architecture specific defines should be avoided\n" . $herecurr);
2416 # check the location of the inline attribute, that it is between
2417 # storage class and type.
2418 if ($line =~ /\b$Type\s+$Inline\b/ ||
2419 $line =~ /\b$Inline\s+$Storage\b/) {
2420 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
2423 # Check for __inline__ and __inline, prefer inline
2424 if ($line =~ /\b(__inline__|__inline)\b/) {
2425 WARN("plain inline is preferred over $1\n" . $herecurr);
2428 # check for new externs in .c files.
2429 if ($realfile =~ /\.c$/ && defined $stat &&
2430 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
2432 my $function_name = $1;
2433 my $paren_space = $2;
2435 my $s = $stat;
2436 if (defined $cond) {
2437 substr($s, 0, length($cond), '');
2439 if ($s =~ /^\s*;/ &&
2440 $function_name ne 'uninitialized_var')
2442 WARN("externs should be avoided in .c files\n" . $herecurr);
2445 if ($paren_space =~ /\n/) {
2446 WARN("arguments for function declarations should follow identifier\n" . $herecurr);
2449 } elsif ($realfile =~ /\.c$/ && defined $stat &&
2450 $stat =~ /^.\s*extern\s+/)
2452 WARN("externs should be avoided in .c files\n" . $herecurr);
2455 # checks for new __setup's
2456 if ($rawline =~ /\b__setup\("([^"]*)"/) {
2457 my $name = $1;
2459 if (!grep(/$name/, @setup_docs)) {
2460 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
2464 # check for pointless casting of kmalloc return
2465 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
2466 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
2469 # check for gcc specific __FUNCTION__
2470 if ($line =~ /__FUNCTION__/) {
2471 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
2474 # check for semaphores used as mutexes
2475 if ($line =~ /^.\s*(DECLARE_MUTEX|init_MUTEX)\s*\(/) {
2476 WARN("mutexes are preferred for single holder semaphores\n" . $herecurr);
2478 # check for semaphores used as mutexes
2479 if ($line =~ /^.\s*init_MUTEX_LOCKED\s*\(/) {
2480 WARN("consider using a completion\n" . $herecurr);
2482 # recommend strict_strto* over simple_strto*
2483 if ($line =~ /\bsimple_(strto.*?)\s*\(/) {
2484 WARN("consider using strict_$1 in preference to simple_$1\n" . $herecurr);
2486 # check for __initcall(), use device_initcall() explicitly please
2487 if ($line =~ /^.\s*__initcall\s*\(/) {
2488 WARN("please use device_initcall() instead of __initcall()\n" . $herecurr);
2491 # use of NR_CPUS is usually wrong
2492 # ignore definitions of NR_CPUS and usage to define arrays as likely right
2493 if ($line =~ /\bNR_CPUS\b/ &&
2494 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
2495 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
2496 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
2497 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
2498 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
2500 WARN("usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
2503 # check for %L{u,d,i} in strings
2504 my $string;
2505 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
2506 $string = substr($rawline, $-[1], $+[1] - $-[1]);
2507 $string =~ s/%%/__/g;
2508 if ($string =~ /(?<!%)%L[udi]/) {
2509 WARN("\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
2510 last;
2514 # whine mightly about in_atomic
2515 if ($line =~ /\bin_atomic\s*\(/) {
2516 if ($realfile =~ m@^drivers/@) {
2517 ERROR("do not use in_atomic in drivers\n" . $herecurr);
2518 } else {
2519 WARN("use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
2524 # If we have no input at all, then there is nothing to report on
2525 # so just keep quiet.
2526 if ($#rawlines == -1) {
2527 exit(0);
2530 # In mailback mode only produce a report in the negative, for
2531 # things that appear to be patches.
2532 if ($mailback && ($clean == 1 || !$is_patch)) {
2533 exit(0);
2536 # This is not a patch, and we are are in 'no-patch' mode so
2537 # just keep quiet.
2538 if (!$chk_patch && !$is_patch) {
2539 exit(0);
2542 if (!$is_patch) {
2543 ERROR("Does not appear to be a unified-diff format patch\n");
2545 if ($is_patch && $chk_signoff && $signoff == 0) {
2546 ERROR("Missing Signed-off-by: line(s)\n");
2549 print report_dump();
2550 if ($summary && !($clean == 1 && $quiet == 1)) {
2551 print "$filename " if ($summary_file);
2552 print "total: $cnt_error errors, $cnt_warn warnings, " .
2553 (($check)? "$cnt_chk checks, " : "") .
2554 "$cnt_lines lines checked\n";
2555 print "\n" if ($quiet == 0);
2558 if ($clean == 1 && $quiet == 0) {
2559 print "$vname has no obvious style problems and is ready for submission.\n"
2561 if ($clean == 0 && $quiet == 0) {
2562 print "$vname has style problems, please review. If any of these errors\n";
2563 print "are false positives report them to the maintainer, see\n";
2564 print "CHECKPATCH in MAINTAINERS.\n";
2567 return $clean;