USB:Update mailing list information in documentation
[linux-2.6/s3c2410-cpufreq.git] / scripts / checkpatch.pl
blob2a7cef9726e4892b3454e7198dca4c529aeed891
1 #!/usr/bin/perl -w
2 # (c) 2001, Dave Jones. <davej@codemonkey.org.uk> (the file handling bit)
3 # (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
4 # (c) 2007, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite, etc)
5 # Licensed under the terms of the GNU GPL License version 2
7 use strict;
9 my $P = $0;
10 $P =~ s@.*/@@g;
12 my $V = '0.15';
14 use Getopt::Long qw(:config no_auto_abbrev);
16 my $quiet = 0;
17 my $tree = 1;
18 my $chk_signoff = 1;
19 my $chk_patch = 1;
20 my $tst_type = 0;
21 my $emacs = 0;
22 my $terse = 0;
23 my $file = 0;
24 my $check = 0;
25 my $summary = 1;
26 my $mailback = 0;
27 my $summary_file = 0;
28 my $root;
29 my %debug;
30 GetOptions(
31 'q|quiet+' => \$quiet,
32 'tree!' => \$tree,
33 'signoff!' => \$chk_signoff,
34 'patch!' => \$chk_patch,
35 'emacs!' => \$emacs,
36 'terse!' => \$terse,
37 'file!' => \$file,
38 'subjective!' => \$check,
39 'strict!' => \$check,
40 'root=s' => \$root,
41 'summary!' => \$summary,
42 'mailback!' => \$mailback,
43 'summary-file!' => \$summary_file,
45 'debug=s' => \%debug,
46 'test-type!' => \$tst_type,
47 ) or exit;
49 my $exit = 0;
51 if ($#ARGV < 0) {
52 print "usage: $P [options] patchfile\n";
53 print "version: $V\n";
54 print "options: -q => quiet\n";
55 print " --no-tree => run without a kernel tree\n";
56 print " --terse => one line per report\n";
57 print " --emacs => emacs compile window format\n";
58 print " --file => check a source file\n";
59 print " --strict => enable more subjective tests\n";
60 print " --root => path to the kernel tree root\n";
61 print " --no-summary => suppress the per-file summary\n";
62 print " --summary-file => include the filename in summary\n";
63 exit(1);
66 my $dbg_values = 0;
67 my $dbg_possible = 0;
68 for my $key (keys %debug) {
69 eval "\${dbg_$key} = '$debug{$key}';"
72 if ($terse) {
73 $emacs = 1;
74 $quiet++;
77 if ($tree) {
78 if (defined $root) {
79 if (!top_of_kernel_tree($root)) {
80 die "$P: $root: --root does not point at a valid tree\n";
82 } else {
83 if (top_of_kernel_tree('.')) {
84 $root = '.';
85 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
86 top_of_kernel_tree($1)) {
87 $root = $1;
91 if (!defined $root) {
92 print "Must be run from the top-level dir. of a kernel tree\n";
93 exit(2);
97 my $emitted_corrupt = 0;
99 our $Ident = qr{[A-Za-z_][A-Za-z\d_]*};
100 our $Storage = qr{extern|static|asmlinkage};
101 our $Sparse = qr{
102 __user|
103 __kernel|
104 __force|
105 __iomem|
106 __must_check|
107 __init_refok|
108 __kprobes
110 our $Attribute = qr{
111 const|
112 __read_mostly|
113 __kprobes|
114 __(?:mem|cpu|dev|)(?:initdata|init)
116 our $Inline = qr{inline|__always_inline|noinline};
117 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
118 our $Lval = qr{$Ident(?:$Member)*};
120 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
121 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
122 our $Operators = qr{
123 <=|>=|==|!=|
124 =>|->|<<|>>|<|>|!|~|
125 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
128 our $NonptrType;
129 our $Type;
130 our $Declare;
132 our @typeList = (
133 qr{void},
134 qr{char},
135 qr{short},
136 qr{int},
137 qr{long},
138 qr{unsigned},
139 qr{float},
140 qr{double},
141 qr{bool},
142 qr{long\s+int},
143 qr{long\s+long},
144 qr{long\s+long\s+int},
145 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
146 qr{struct\s+$Ident},
147 qr{union\s+$Ident},
148 qr{enum\s+$Ident},
149 qr{${Ident}_t},
150 qr{${Ident}_handler},
151 qr{${Ident}_handler_fn},
154 sub build_types {
155 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
156 $NonptrType = qr{
158 (?:const\s+)?
159 (?:unsigned\s+)?
161 $all|
162 (?:typeof|__typeof__)\s*\(\s*\**\s*$Ident\s*\)
164 (?:\s+$Sparse|\s+const)*
167 $Type = qr{
168 \b$NonptrType\b
169 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
170 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
172 $Declare = qr{(?:$Storage\s+)?$Type};
174 build_types();
176 $chk_signoff = 0 if ($file);
178 my @dep_includes = ();
179 my @dep_functions = ();
180 my $removal = "Documentation/feature-removal-schedule.txt";
181 if ($tree && -f "$root/$removal") {
182 open(REMOVE, "<$root/$removal") ||
183 die "$P: $removal: open failed - $!\n";
184 while (<REMOVE>) {
185 if (/^Check:\s+(.*\S)/) {
186 for my $entry (split(/[, ]+/, $1)) {
187 if ($entry =~ m@include/(.*)@) {
188 push(@dep_includes, $1);
190 } elsif ($entry !~ m@/@) {
191 push(@dep_functions, $entry);
198 my @rawlines = ();
199 my @lines = ();
200 my $vname;
201 for my $filename (@ARGV) {
202 if ($file) {
203 open(FILE, "diff -u /dev/null $filename|") ||
204 die "$P: $filename: diff failed - $!\n";
205 } else {
206 open(FILE, "<$filename") ||
207 die "$P: $filename: open failed - $!\n";
209 if ($filename eq '-') {
210 $vname = 'Your patch';
211 } else {
212 $vname = $filename;
214 while (<FILE>) {
215 chomp;
216 push(@rawlines, $_);
218 close(FILE);
219 if (!process($filename)) {
220 $exit = 1;
222 @rawlines = ();
223 @lines = ();
226 exit($exit);
228 sub top_of_kernel_tree {
229 my ($root) = @_;
231 my @tree_check = (
232 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
233 "README", "Documentation", "arch", "include", "drivers",
234 "fs", "init", "ipc", "kernel", "lib", "scripts",
237 foreach my $check (@tree_check) {
238 if (! -e $root . '/' . $check) {
239 return 0;
242 return 1;
245 sub expand_tabs {
246 my ($str) = @_;
248 my $res = '';
249 my $n = 0;
250 for my $c (split(//, $str)) {
251 if ($c eq "\t") {
252 $res .= ' ';
253 $n++;
254 for (; ($n % 8) != 0; $n++) {
255 $res .= ' ';
257 next;
259 $res .= $c;
260 $n++;
263 return $res;
265 sub copy_spacing {
266 my ($str) = @_;
268 my $res = '';
269 for my $c (split(//, $str)) {
270 if ($c eq "\t") {
271 $res .= $c;
272 } else {
273 $res .= ' ';
277 return $res;
280 sub line_stats {
281 my ($line) = @_;
283 # Drop the diff line leader and expand tabs
284 $line =~ s/^.//;
285 $line = expand_tabs($line);
287 # Pick the indent from the front of the line.
288 my ($white) = ($line =~ /^(\s*)/);
290 return (length($line), length($white));
293 sub sanitise_line {
294 my ($line) = @_;
296 my $res = '';
297 my $l = '';
299 my $quote = '';
300 my $qlen = 0;
302 foreach my $c (split(//, $line)) {
303 # The second backslash of a pair is not a "quote".
304 if ($l eq "\\" && $c eq "\\") {
305 $c = 'X';
307 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
308 if ($quote eq '') {
309 $quote = $c;
310 $res .= $c;
311 $l = $c;
312 $qlen = 0;
313 next;
314 } elsif ($quote eq $c) {
315 $quote = '';
318 if ($quote eq "'" && $qlen > 1) {
319 $quote = '';
321 if ($quote && $c ne "\t") {
322 $res .= "X";
323 $qlen++;
324 } else {
325 $res .= $c;
328 $l = $c;
331 # Clear out the comments.
332 while ($res =~ m@(/\*.*?\*/)@g) {
333 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
335 if ($res =~ m@(/\*.*)@) {
336 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
338 if ($res =~ m@^.(.*\*/)@) {
339 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
342 # The pathname on a #include may be surrounded by '<' and '>'.
343 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
344 my $clean = 'X' x length($1);
345 $res =~ s@\<.*\>@<$clean>@;
347 # The whole of a #error is a string.
348 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
349 my $clean = 'X' x length($1);
350 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
353 return $res;
356 sub ctx_statement_block {
357 my ($linenr, $remain, $off) = @_;
358 my $line = $linenr - 1;
359 my $blk = '';
360 my $soff = $off;
361 my $coff = $off - 1;
363 my $loff = 0;
365 my $type = '';
366 my $level = 0;
367 my $p;
368 my $c;
369 my $len = 0;
371 my $remainder;
372 while (1) {
373 #warn "CSB: blk<$blk>\n";
374 # If we are about to drop off the end, pull in more
375 # context.
376 if ($off >= $len) {
377 for (; $remain > 0; $line++) {
378 next if ($lines[$line] =~ /^-/);
379 $remain--;
380 $loff = $len;
381 $blk .= $lines[$line] . "\n";
382 $len = length($blk);
383 $line++;
384 last;
386 # Bail if there is no further context.
387 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
388 if ($off >= $len) {
389 last;
392 $p = $c;
393 $c = substr($blk, $off, 1);
394 $remainder = substr($blk, $off);
396 #warn "CSB: c<$c> type<$type> level<$level>\n";
397 # Statement ends at the ';' or a close '}' at the
398 # outermost level.
399 if ($level == 0 && $c eq ';') {
400 last;
403 # An else is really a conditional as long as its not else if
404 if ($level == 0 && (!defined($p) || $p =~ /(?:\s|\})/) &&
405 $remainder =~ /(else)(?:\s|{)/ &&
406 $remainder !~ /else\s+if\b/) {
407 $coff = $off + length($1);
410 if (($type eq '' || $type eq '(') && $c eq '(') {
411 $level++;
412 $type = '(';
414 if ($type eq '(' && $c eq ')') {
415 $level--;
416 $type = ($level != 0)? '(' : '';
418 if ($level == 0 && $coff < $soff) {
419 $coff = $off;
422 if (($type eq '' || $type eq '{') && $c eq '{') {
423 $level++;
424 $type = '{';
426 if ($type eq '{' && $c eq '}') {
427 $level--;
428 $type = ($level != 0)? '{' : '';
430 if ($level == 0) {
431 last;
434 $off++;
436 if ($off == $len) {
437 $line++;
438 $remain--;
441 my $statement = substr($blk, $soff, $off - $soff + 1);
442 my $condition = substr($blk, $soff, $coff - $soff + 1);
444 #warn "STATEMENT<$statement>\n";
445 #warn "CONDITION<$condition>\n";
447 #print "off<$off> loff<$loff>\n";
449 return ($statement, $condition,
450 $line, $remain + 1, $off - $loff + 1, $level);
453 sub statement_lines {
454 my ($stmt) = @_;
456 # Strip the diff line prefixes and rip blank lines at start and end.
457 $stmt =~ s/(^|\n)./$1/g;
458 $stmt =~ s/^\s*//;
459 $stmt =~ s/\s*$//;
461 my @stmt_lines = ($stmt =~ /\n/g);
463 return $#stmt_lines + 2;
466 sub statement_rawlines {
467 my ($stmt) = @_;
469 my @stmt_lines = ($stmt =~ /\n/g);
471 return $#stmt_lines + 2;
474 sub statement_block_size {
475 my ($stmt) = @_;
477 $stmt =~ s/(^|\n)./$1/g;
478 $stmt =~ s/^\s*{//;
479 $stmt =~ s/}\s*$//;
480 $stmt =~ s/^\s*//;
481 $stmt =~ s/\s*$//;
483 my @stmt_lines = ($stmt =~ /\n/g);
484 my @stmt_statements = ($stmt =~ /;/g);
486 my $stmt_lines = $#stmt_lines + 2;
487 my $stmt_statements = $#stmt_statements + 1;
489 if ($stmt_lines > $stmt_statements) {
490 return $stmt_lines;
491 } else {
492 return $stmt_statements;
496 sub ctx_statement_full {
497 my ($linenr, $remain, $off) = @_;
498 my ($statement, $condition, $level);
500 my (@chunks);
502 # Grab the first conditional/block pair.
503 ($statement, $condition, $linenr, $remain, $off, $level) =
504 ctx_statement_block($linenr, $remain, $off);
505 #print "F: c<$condition> s<$statement>\n";
506 push(@chunks, [ $condition, $statement ]);
507 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
508 return ($level, $linenr, @chunks);
511 # Pull in the following conditional/block pairs and see if they
512 # could continue the statement.
513 for (;;) {
514 ($statement, $condition, $linenr, $remain, $off, $level) =
515 ctx_statement_block($linenr, $remain, $off);
516 #print "C: c<$condition> s<$statement> remain<$remain>\n";
517 last if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:else|do)\b/s));
518 #print "C: push\n";
519 push(@chunks, [ $condition, $statement ]);
522 return ($level, $linenr, @chunks);
525 sub ctx_block_get {
526 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
527 my $line;
528 my $start = $linenr - 1;
529 my $blk = '';
530 my @o;
531 my @c;
532 my @res = ();
534 my $level = 0;
535 for ($line = $start; $remain > 0; $line++) {
536 next if ($rawlines[$line] =~ /^-/);
537 $remain--;
539 $blk .= $rawlines[$line];
540 foreach my $c (split(//, $rawlines[$line])) {
541 ##print "C<$c>L<$level><$open$close>O<$off>\n";
542 if ($off > 0) {
543 $off--;
544 next;
547 if ($c eq $close && $level > 0) {
548 $level--;
549 last if ($level == 0);
550 } elsif ($c eq $open) {
551 $level++;
555 if (!$outer || $level <= 1) {
556 push(@res, $rawlines[$line]);
559 last if ($level == 0);
562 return ($level, @res);
564 sub ctx_block_outer {
565 my ($linenr, $remain) = @_;
567 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
568 return @r;
570 sub ctx_block {
571 my ($linenr, $remain) = @_;
573 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
574 return @r;
576 sub ctx_statement {
577 my ($linenr, $remain, $off) = @_;
579 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
580 return @r;
582 sub ctx_block_level {
583 my ($linenr, $remain) = @_;
585 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
587 sub ctx_statement_level {
588 my ($linenr, $remain, $off) = @_;
590 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
593 sub ctx_locate_comment {
594 my ($first_line, $end_line) = @_;
596 # Catch a comment on the end of the line itself.
597 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
598 return $current_comment if (defined $current_comment);
600 # Look through the context and try and figure out if there is a
601 # comment.
602 my $in_comment = 0;
603 $current_comment = '';
604 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
605 my $line = $rawlines[$linenr - 1];
606 #warn " $line\n";
607 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
608 $in_comment = 1;
610 if ($line =~ m@/\*@) {
611 $in_comment = 1;
613 if (!$in_comment && $current_comment ne '') {
614 $current_comment = '';
616 $current_comment .= $line . "\n" if ($in_comment);
617 if ($line =~ m@\*/@) {
618 $in_comment = 0;
622 chomp($current_comment);
623 return($current_comment);
625 sub ctx_has_comment {
626 my ($first_line, $end_line) = @_;
627 my $cmt = ctx_locate_comment($first_line, $end_line);
629 ##print "LINE: $rawlines[$end_line - 1 ]\n";
630 ##print "CMMT: $cmt\n";
632 return ($cmt ne '');
635 sub cat_vet {
636 my ($vet) = @_;
637 my ($res, $coded);
639 $res = '';
640 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
641 $res .= $1;
642 if ($2 ne '') {
643 $coded = sprintf("^%c", unpack('C', $2) + 64);
644 $res .= $coded;
647 $res =~ s/$/\$/;
649 return $res;
652 my $av_preprocessor = 0;
653 my $av_pending;
654 my @av_paren_type;
656 sub annotate_reset {
657 $av_preprocessor = 0;
658 $av_pending = '_';
659 @av_paren_type = ('E');
662 sub annotate_values {
663 my ($stream, $type) = @_;
665 my $res;
666 my $cur = $stream;
668 print "$stream\n" if ($dbg_values > 1);
670 while (length($cur)) {
671 print " <" . join('', @av_paren_type) .
672 "> <$type> " if ($dbg_values > 1);
673 if ($cur =~ /^(\s+)/o) {
674 print "WS($1)\n" if ($dbg_values > 1);
675 if ($1 =~ /\n/ && $av_preprocessor) {
676 $type = pop(@av_paren_type);
677 $av_preprocessor = 0;
680 } elsif ($cur =~ /^($Type)/) {
681 print "DECLARE($1)\n" if ($dbg_values > 1);
682 $type = 'T';
684 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
685 print "DEFINE($1)\n" if ($dbg_values > 1);
686 $av_preprocessor = 1;
687 $av_pending = 'N';
689 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if))/o) {
690 print "PRE_START($1)\n" if ($dbg_values > 1);
691 $av_preprocessor = 1;
693 push(@av_paren_type, $type);
694 push(@av_paren_type, $type);
695 $type = 'N';
697 } elsif ($cur =~ /^(#\s*(?:else|elif))/o) {
698 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
699 $av_preprocessor = 1;
701 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
703 $type = 'N';
705 } elsif ($cur =~ /^(#\s*(?:endif))/o) {
706 print "PRE_END($1)\n" if ($dbg_values > 1);
708 $av_preprocessor = 1;
710 # Assume all arms of the conditional end as this
711 # one does, and continue as if the #endif was not here.
712 pop(@av_paren_type);
713 push(@av_paren_type, $type);
714 $type = 'N';
716 } elsif ($cur =~ /^(\\\n)/o) {
717 print "PRECONT($1)\n" if ($dbg_values > 1);
719 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
720 print "SIZEOF($1)\n" if ($dbg_values > 1);
721 if (defined $2) {
722 $av_pending = 'V';
724 $type = 'N';
726 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
727 print "COND($1)\n" if ($dbg_values > 1);
728 $av_pending = 'N';
729 $type = 'N';
731 } elsif ($cur =~/^(return|case|else)/o) {
732 print "KEYWORD($1)\n" if ($dbg_values > 1);
733 $type = 'N';
735 } elsif ($cur =~ /^(\()/o) {
736 print "PAREN('$1')\n" if ($dbg_values > 1);
737 push(@av_paren_type, $av_pending);
738 $av_pending = '_';
739 $type = 'N';
741 } elsif ($cur =~ /^(\))/o) {
742 my $new_type = pop(@av_paren_type);
743 if ($new_type ne '_') {
744 $type = $new_type;
745 print "PAREN('$1') -> $type\n"
746 if ($dbg_values > 1);
747 } else {
748 print "PAREN('$1')\n" if ($dbg_values > 1);
751 } elsif ($cur =~ /^($Ident)\(/o) {
752 print "FUNC($1)\n" if ($dbg_values > 1);
753 $av_pending = 'V';
755 } elsif ($cur =~ /^($Ident|$Constant)/o) {
756 print "IDENT($1)\n" if ($dbg_values > 1);
757 $type = 'V';
759 } elsif ($cur =~ /^($Assignment)/o) {
760 print "ASSIGN($1)\n" if ($dbg_values > 1);
761 $type = 'N';
763 } elsif ($cur =~/^(;|{|})/) {
764 print "END($1)\n" if ($dbg_values > 1);
765 $type = 'E';
767 } elsif ($cur =~ /^(;|\?|:|\[)/o) {
768 print "CLOSE($1)\n" if ($dbg_values > 1);
769 $type = 'N';
771 } elsif ($cur =~ /^($Operators)/o) {
772 print "OP($1)\n" if ($dbg_values > 1);
773 if ($1 ne '++' && $1 ne '--') {
774 $type = 'N';
777 } elsif ($cur =~ /(^.)/o) {
778 print "C($1)\n" if ($dbg_values > 1);
780 if (defined $1) {
781 $cur = substr($cur, length($1));
782 $res .= $type x length($1);
786 return $res;
789 sub possible {
790 my ($possible, $line) = @_;
792 #print "CHECK<$possible>\n";
793 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
794 $possible ne 'goto' && $possible ne 'return' &&
795 $possible ne 'struct' && $possible ne 'enum' &&
796 $possible ne 'case' && $possible ne 'else' &&
797 $possible ne 'typedef') {
798 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
799 push(@typeList, $possible);
800 build_types();
804 my $prefix = '';
806 sub report {
807 my $line = $prefix . $_[0];
809 $line = (split('\n', $line))[0] . "\n" if ($terse);
811 push(our @report, $line);
813 sub report_dump {
814 our @report;
816 sub ERROR {
817 report("ERROR: $_[0]\n");
818 our $clean = 0;
819 our $cnt_error++;
821 sub WARN {
822 report("WARNING: $_[0]\n");
823 our $clean = 0;
824 our $cnt_warn++;
826 sub CHK {
827 if ($check) {
828 report("CHECK: $_[0]\n");
829 our $clean = 0;
830 our $cnt_chk++;
834 sub process {
835 my $filename = shift;
837 my $linenr=0;
838 my $prevline="";
839 my $prevrawline="";
840 my $stashline="";
841 my $stashrawline="";
843 my $length;
844 my $indent;
845 my $previndent=0;
846 my $stashindent=0;
848 our $clean = 1;
849 my $signoff = 0;
850 my $is_patch = 0;
852 our @report = ();
853 our $cnt_lines = 0;
854 our $cnt_error = 0;
855 our $cnt_warn = 0;
856 our $cnt_chk = 0;
858 # Trace the real file/line as we go.
859 my $realfile = '';
860 my $realline = 0;
861 my $realcnt = 0;
862 my $here = '';
863 my $in_comment = 0;
864 my $comment_edge = 0;
865 my $first_line = 0;
867 my $prev_values = 'E';
869 # suppression flags
870 my $suppress_ifbraces = 0;
872 # Pre-scan the patch sanitizing the lines.
873 # Pre-scan the patch looking for any __setup documentation.
875 my @setup_docs = ();
876 my $setup_docs = 0;
877 my $line;
878 foreach my $rawline (@rawlines) {
879 # Standardise the strings and chars within the input to
880 # simplify matching.
881 $line = sanitise_line($rawline);
882 push(@lines, $line);
884 ##print "==>$rawline\n";
885 ##print "-->$line\n";
887 if ($line=~/^\+\+\+\s+(\S+)/) {
888 $setup_docs = 0;
889 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
890 $setup_docs = 1;
892 next;
895 if ($setup_docs && $line =~ /^\+/) {
896 push(@setup_docs, $line);
900 $prefix = '';
902 foreach my $line (@lines) {
903 $linenr++;
905 my $rawline = $rawlines[$linenr - 1];
907 #extract the filename as it passes
908 if ($line=~/^\+\+\+\s+(\S+)/) {
909 $realfile=$1;
910 $realfile =~ s@^[^/]*/@@;
911 $in_comment = 0;
912 next;
914 #extract the line range in the file after the patch is applied
915 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
916 $is_patch = 1;
917 $first_line = $linenr + 1;
918 $in_comment = 0;
919 $realline=$1-1;
920 if (defined $2) {
921 $realcnt=$3+1;
922 } else {
923 $realcnt=1+1;
925 annotate_reset();
926 $prev_values = 'E';
928 $suppress_ifbraces = $linenr - 1;
929 next;
932 # track the line number as we move through the hunk, note that
933 # new versions of GNU diff omit the leading space on completely
934 # blank context lines so we need to count that too.
935 if ($line =~ /^( |\+|$)/) {
936 $realline++;
937 $realcnt-- if ($realcnt != 0);
939 # Guestimate if this is a continuing comment. Run
940 # the context looking for a comment "edge". If this
941 # edge is a close comment then we must be in a comment
942 # at context start.
943 if ($linenr == $first_line) {
944 my $edge;
945 for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) {
946 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
947 last if (defined $edge);
949 if (defined $edge && $edge eq '*/') {
950 $in_comment = 1;
954 # Guestimate if this is a continuing comment. If this
955 # is the start of a diff block and this line starts
956 # ' *' then it is very likely a comment.
957 if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) {
958 $in_comment = 1;
961 # Find the last comment edge on _this_ line.
962 $comment_edge = 0;
963 while (($rawline =~ m@(/\*|\*/)@g)) {
964 if ($1 eq '/*') {
965 $in_comment = 1;
966 } else {
967 $in_comment = 0;
969 $comment_edge = 1;
972 # Measure the line length and indent.
973 ($length, $indent) = line_stats($rawline);
975 # Track the previous line.
976 ($prevline, $stashline) = ($stashline, $line);
977 ($previndent, $stashindent) = ($stashindent, $indent);
978 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
980 #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n";
982 } elsif ($realcnt == 1) {
983 $realcnt--;
986 #make up the handle for any error we report on this line
987 $here = "#$linenr: " if (!$file);
988 $here = "#$realline: " if ($file);
989 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
991 my $hereline = "$here\n$rawline\n";
992 my $herecurr = "$here\n$rawline\n";
993 my $hereprev = "$here\n$prevrawline\n$rawline\n";
995 $prefix = "$filename:$realline: " if ($emacs && $file);
996 $prefix = "$filename:$linenr: " if ($emacs && !$file);
997 $cnt_lines++ if ($realcnt != 0);
999 #check the patch for a signoff:
1000 if ($line =~ /^\s*signed-off-by:/i) {
1001 # This is a signoff, if ugly, so do not double report.
1002 $signoff++;
1003 if (!($line =~ /^\s*Signed-off-by:/)) {
1004 WARN("Signed-off-by: is the preferred form\n" .
1005 $herecurr);
1007 if ($line =~ /^\s*signed-off-by:\S/i) {
1008 WARN("need space after Signed-off-by:\n" .
1009 $herecurr);
1013 # Check for wrappage within a valid hunk of the file
1014 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
1015 ERROR("patch seems to be corrupt (line wrapped?)\n" .
1016 $herecurr) if (!$emitted_corrupt++);
1019 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
1020 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
1021 !($rawline =~ m/^(
1022 [\x09\x0A\x0D\x20-\x7E] # ASCII
1023 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1024 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1025 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1026 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1027 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1028 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1029 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1030 )*$/x )) {
1031 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
1034 #ignore lines being removed
1035 if ($line=~/^-/) {next;}
1037 # check we are in a valid source file if not then ignore this hunk
1038 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1040 #trailing whitespace
1041 if ($line =~ /^\+.*\015/) {
1042 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1043 ERROR("DOS line endings\n" . $herevet);
1045 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
1046 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1047 ERROR("trailing whitespace\n" . $herevet);
1049 #80 column limit
1050 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
1051 WARN("line over 80 characters\n" . $herecurr);
1054 # check for adding lines without a newline.
1055 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
1056 WARN("adding a line without newline at end of file\n" . $herecurr);
1059 # check we are in a valid source file *.[hc] if not then ignore this hunk
1060 next if ($realfile !~ /\.[hc]$/);
1062 # at the beginning of a line any tabs must come first and anything
1063 # more than 8 must use tabs.
1064 if ($rawline =~ /^\+\s* \t\s*\S/ ||
1065 $rawline =~ /^\+\s* \s*/) {
1066 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1067 ERROR("use tabs not spaces\n" . $herevet);
1070 # check for RCS/CVS revision markers
1071 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1072 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
1075 # The rest of our checks refer specifically to C style
1076 # only apply those _outside_ comments. Only skip
1077 # lines in the middle of comments.
1078 next if (!$comment_edge && $in_comment);
1080 # Check for potential 'bare' types
1081 if ($realcnt) {
1082 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1083 $s =~ s/\n./ /g;
1084 $s =~ s/{.*$//;
1086 # Ignore goto labels.
1087 if ($s =~ /$Ident:\*$/) {
1089 # Ignore functions being called
1090 } elsif ($s =~ /^.\s*$Ident\s*\(/) {
1092 # definitions in global scope can only start with types
1093 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1094 possible($1, $s);
1096 # declarations always start with types
1097 } elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1098 possible($1, $s);
1101 # any (foo ... *) is a pointer cast, and foo is a type
1102 while ($s =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1103 possible($1, $s);
1106 # Check for any sort of function declaration.
1107 # int foo(something bar, other baz);
1108 # void (*store_gdt)(x86_descr_ptr *);
1109 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
1110 my ($name_len) = length($1);
1112 my $ctx = $s;
1113 substr($ctx, 0, $name_len + 1) = '';
1114 $ctx =~ s/\)[^\)]*$//;
1116 for my $arg (split(/\s*,\s*/, $ctx)) {
1117 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1119 possible($1, $s);
1127 # Checks which may be anchored in the context.
1130 # Check for switch () and associated case and default
1131 # statements should be at the same indent.
1132 if ($line=~/\bswitch\s*\(.*\)/) {
1133 my $err = '';
1134 my $sep = '';
1135 my @ctx = ctx_block_outer($linenr, $realcnt);
1136 shift(@ctx);
1137 for my $ctx (@ctx) {
1138 my ($clen, $cindent) = line_stats($ctx);
1139 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1140 $indent != $cindent) {
1141 $err .= "$sep$ctx\n";
1142 $sep = '';
1143 } else {
1144 $sep = "[...]\n";
1147 if ($err ne '') {
1148 ERROR("switch and case should be at the same indent\n$hereline$err");
1152 # if/while/etc brace do not go on next line, unless defining a do while loop,
1153 # or if that brace on the next line is for something else
1154 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1155 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1156 my $ctx_ln = $linenr + $#ctx + 1;
1157 my $ctx_cnt = $realcnt - $#ctx - 1;
1158 my $ctx = join("\n", @ctx);
1160 # Skip over any removed lines in the context following statement.
1161 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
1162 $ctx_ln++;
1163 $ctx_cnt--;
1165 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
1167 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1168 ERROR("That open brace { should be on the previous line\n" .
1169 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1171 if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
1172 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1173 if ($nindent > $indent) {
1174 WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" .
1175 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1180 # Track the 'values' across context and added lines.
1181 my $opline = $line; $opline =~ s/^./ /;
1182 my $curr_values = annotate_values($opline . "\n", $prev_values);
1183 $curr_values = $prev_values . $curr_values;
1184 if ($dbg_values) {
1185 my $outline = $opline; $outline =~ s/\t/ /g;
1186 print "$linenr > .$outline\n";
1187 print "$linenr > $curr_values\n";
1189 $prev_values = substr($curr_values, -1);
1191 #ignore lines not being added
1192 if ($line=~/^[^\+]/) {next;}
1194 # TEST: allow direct testing of the type matcher.
1195 if ($tst_type && $line =~ /^.$Declare$/) {
1196 ERROR("TEST: is type $Declare\n" . $herecurr);
1197 next;
1200 # check for initialisation to aggregates open brace on the next line
1201 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1202 $line =~ /^.\s*{/) {
1203 ERROR("That open brace { should be on the previous line\n" . $hereprev);
1207 # Checks which are anchored on the added line.
1210 # check for malformed paths in #include statements (uses RAW line)
1211 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1212 my $path = $1;
1213 if ($path =~ m{//}) {
1214 ERROR("malformed #include filename\n" .
1215 $herecurr);
1219 # no C99 // comments
1220 if ($line =~ m{//}) {
1221 ERROR("do not use C99 // comments\n" . $herecurr);
1223 # Remove C99 comments.
1224 $line =~ s@//.*@@;
1225 $opline =~ s@//.*@@;
1227 #EXPORT_SYMBOL should immediately follow its function closing }.
1228 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1229 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1230 my $name = $1;
1231 if (($prevline !~ /^}/) &&
1232 ($prevline !~ /^\+}/) &&
1233 ($prevline !~ /^ }/) &&
1234 ($prevline !~ /^.DECLARE_$Ident\(\Q$name\E\)/) &&
1235 ($prevline !~ /^.LIST_HEAD\(\Q$name\E\)/) &&
1236 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=|\[)/)) {
1237 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1241 # check for external initialisers.
1242 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1243 ERROR("do not initialise externals to 0 or NULL\n" .
1244 $herecurr);
1246 # check for static initialisers.
1247 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
1248 ERROR("do not initialise statics to 0 or NULL\n" .
1249 $herecurr);
1252 # check for new typedefs, only function parameters and sparse annotations
1253 # make sense.
1254 if ($line =~ /\btypedef\s/ &&
1255 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1256 $line !~ /\b__bitwise(?:__|)\b/) {
1257 WARN("do not add new typedefs\n" . $herecurr);
1260 # * goes on variable not on type
1261 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1262 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1263 $herecurr);
1265 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1266 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1267 $herecurr);
1269 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1270 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1271 $herecurr);
1273 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1274 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1275 $herecurr);
1278 # # no BUG() or BUG_ON()
1279 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1280 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1281 # print "$herecurr";
1282 # $clean = 0;
1285 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1286 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1289 # printk should use KERN_* levels. Note that follow on printk's on the
1290 # same line do not need a level, so we use the current block context
1291 # to try and find and validate the current printk. In summary the current
1292 # printk includes all preceeding printk's which have no newline on the end.
1293 # we assume the first bad printk is the one to report.
1294 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1295 my $ok = 0;
1296 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1297 #print "CHECK<$lines[$ln - 1]\n";
1298 # we have a preceeding printk if it ends
1299 # with "\n" ignore it, else it is to blame
1300 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1301 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1302 $ok = 1;
1304 last;
1307 if ($ok == 0) {
1308 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1312 # function brace can't be on same line, except for #defines of do while,
1313 # or if closed on same line
1314 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
1315 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
1316 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1319 # open braces for enum, union and struct go on the same line.
1320 if ($line =~ /^.\s*{/ &&
1321 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1322 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1325 # check for spaces between functions and their parentheses.
1326 while ($line =~ /($Ident)\s+\(/g) {
1327 my $name = $1;
1328 my $ctx = substr($line, 0, $-[1]);
1330 # Ignore those directives where spaces _are_ permitted.
1331 if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) {
1333 # cpp #define statements have non-optional spaces, ie
1334 # if there is a space between the name and the open
1335 # parenthesis it is simply not a parameter group.
1336 } elsif ($ctx =~ /^.\#\s*define\s*$/) {
1338 # If this whole things ends with a type its most
1339 # likely a typedef for a function.
1340 } elsif ("$ctx$name" =~ /$Type$/) {
1342 } else {
1343 WARN("no space between function name and open parenthesis '('\n" . $herecurr);
1346 # Check operator spacing.
1347 if (!($line=~/\#\s*include/)) {
1348 my $ops = qr{
1349 <<=|>>=|<=|>=|==|!=|
1350 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1351 =>|->|<<|>>|<|>|=|!|~|
1352 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1354 my @elements = split(/($ops|;)/, $opline);
1355 my $off = 0;
1357 my $blank = copy_spacing($opline);
1359 for (my $n = 0; $n < $#elements; $n += 2) {
1360 $off += length($elements[$n]);
1362 my $a = '';
1363 $a = 'V' if ($elements[$n] ne '');
1364 $a = 'W' if ($elements[$n] =~ /\s$/);
1365 $a = 'C' if ($elements[$n] =~ /$;$/);
1366 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1367 $a = 'O' if ($elements[$n] eq '');
1368 $a = 'E' if ($elements[$n] eq '' && $n == 0);
1370 my $op = $elements[$n + 1];
1372 my $c = '';
1373 if (defined $elements[$n + 2]) {
1374 $c = 'V' if ($elements[$n + 2] ne '');
1375 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1376 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
1377 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1378 $c = 'O' if ($elements[$n + 2] eq '');
1379 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
1380 } else {
1381 $c = 'E';
1384 # Pick up the preceeding and succeeding characters.
1385 my $ca = substr($opline, 0, $off);
1386 my $cc = '';
1387 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1388 $cc = substr($opline, $off + length($elements[$n + 1]));
1390 my $cb = "$ca$;$cc";
1392 my $ctx = "${a}x${c}";
1394 my $at = "(ctx:$ctx)";
1396 my $ptr = substr($blank, 0, $off) . "^";
1397 my $hereptr = "$hereline$ptr\n";
1399 # Classify operators into binary, unary, or
1400 # definitions (* only) where they have more
1401 # than one mode.
1402 my $op_type = substr($curr_values, $off + 1, 1);
1403 my $op_left = substr($curr_values, $off, 1);
1404 my $is_unary;
1405 if ($op_type eq 'T') {
1406 $is_unary = 2;
1407 } elsif ($op_left eq 'V') {
1408 $is_unary = 0;
1409 } else {
1410 $is_unary = 1;
1412 #if ($op eq '-' || $op eq '&' || $op eq '*') {
1413 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
1416 # Ignore operators passed as parameters.
1417 if ($op_type ne 'V' &&
1418 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1420 # # Ignore comments
1421 # } elsif ($op =~ /^$;+$/) {
1423 # ; should have either the end of line or a space or \ after it
1424 } elsif ($op eq ';') {
1425 if ($ctx !~ /.x[WEBC]/ &&
1426 $cc !~ /^\\/ && $cc !~ /^;/) {
1427 ERROR("need space after that '$op' $at\n" . $hereptr);
1430 # // is a comment
1431 } elsif ($op eq '//') {
1433 # -> should have no spaces
1434 } elsif ($op eq '->') {
1435 if ($ctx =~ /Wx.|.xW/) {
1436 ERROR("no spaces around that '$op' $at\n" . $hereptr);
1439 # , must have a space on the right.
1440 } elsif ($op eq ',') {
1441 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
1442 ERROR("need space after that '$op' $at\n" . $hereptr);
1445 # '*' as part of a type definition -- reported already.
1446 } elsif ($op eq '*' && $is_unary == 2) {
1447 #warn "'*' is part of type\n";
1449 # unary operators should have a space before and
1450 # none after. May be left adjacent to another
1451 # unary operator, or a cast
1452 } elsif ($op eq '!' || $op eq '~' ||
1453 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1454 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1455 ERROR("need space before that '$op' $at\n" . $hereptr);
1457 if ($ctx =~ /.xW/) {
1458 ERROR("no space after that '$op' $at\n" . $hereptr);
1461 # unary ++ and unary -- are allowed no space on one side.
1462 } elsif ($op eq '++' or $op eq '--') {
1463 if ($ctx !~ /[WOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
1464 ERROR("need space one side of that '$op' $at\n" . $hereptr);
1466 if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1467 ERROR("no space before that '$op' $at\n" . $hereptr);
1470 # << and >> may either have or not have spaces both sides
1471 } elsif ($op eq '<<' or $op eq '>>' or
1472 $op eq '&' or $op eq '^' or $op eq '|' or
1473 $op eq '+' or $op eq '-' or
1474 $op eq '*' or $op eq '/' or
1475 $op eq '%')
1477 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO|Cx.|.xC/) {
1478 ERROR("need consistent spacing around '$op' $at\n" .
1479 $hereptr);
1482 # All the others need spaces both sides.
1483 } elsif ($ctx !~ /[EWC]x[CWE]/) {
1484 # Ignore email addresses <foo@bar>
1485 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1486 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1487 ERROR("need spaces around that '$op' $at\n" . $hereptr);
1490 $off += length($elements[$n + 1]);
1494 # check for multiple assignments
1495 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1496 CHK("multiple assignments should be avoided\n" . $herecurr);
1499 ## # check for multiple declarations, allowing for a function declaration
1500 ## # continuation.
1501 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1502 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1504 ## # Remove any bracketed sections to ensure we do not
1505 ## # falsly report the parameters of functions.
1506 ## my $ln = $line;
1507 ## while ($ln =~ s/\([^\(\)]*\)//g) {
1508 ## }
1509 ## if ($ln =~ /,/) {
1510 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1511 ## }
1512 ## }
1514 #need space before brace following if, while, etc
1515 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1516 $line =~ /do{/) {
1517 ERROR("need a space before the open brace '{'\n" . $herecurr);
1520 # closing brace should have a space following it when it has anything
1521 # on the line
1522 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1523 ERROR("need a space after that close brace '}'\n" . $herecurr);
1526 # check spacing on square brackets
1527 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1528 ERROR("no space after that open square bracket '['\n" . $herecurr);
1530 if ($line =~ /\s\]/) {
1531 ERROR("no space before that close square bracket ']'\n" . $herecurr);
1534 # check spacing on paretheses
1535 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1536 $line !~ /for\s*\(\s+;/) {
1537 ERROR("no space after that open parenthesis '('\n" . $herecurr);
1539 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1540 $line !~ /for\s*\(.*;\s+\)/) {
1541 ERROR("no space before that close parenthesis ')'\n" . $herecurr);
1544 #goto labels aren't indented, allow a single space however
1545 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
1546 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1547 WARN("labels should not be indented\n" . $herecurr);
1550 # Need a space before open parenthesis after if, while etc
1551 if ($line=~/\b(if|while|for|switch)\(/) {
1552 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
1555 # Check for illegal assignment in if conditional.
1556 if ($line =~ /\bif\s*\(/) {
1557 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1559 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1560 ERROR("do not use assignment in if condition\n" . $herecurr);
1563 # Find out what is on the end of the line after the
1564 # conditional.
1565 substr($s, 0, length($c)) = '';
1566 $s =~ s/\n.*//g;
1567 $s =~ s/$;//g; # Remove any comments
1568 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) {
1569 ERROR("trailing statements should be on next line\n" . $herecurr);
1573 # Check for bitwise tests written as boolean
1574 if ($line =~ /
1576 (?:\[|\(|\&\&|\|\|)
1577 \s*0[xX][0-9]+\s*
1578 (?:\&\&|\|\|)
1580 (?:\&\&|\|\|)
1581 \s*0[xX][0-9]+\s*
1582 (?:\&\&|\|\||\)|\])
1583 )/x)
1585 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1588 # if and else should not have general statements after it
1589 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1590 my $s = $1;
1591 $s =~ s/$;//g; # Remove any comments
1592 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1593 ERROR("trailing statements should be on next line\n" . $herecurr);
1597 # Check for }<nl>else {, these must be at the same
1598 # indent level to be relevant to each other.
1599 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1600 $previndent == $indent) {
1601 ERROR("else should follow close brace '}'\n" . $hereprev);
1604 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1605 $previndent == $indent) {
1606 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1608 # Find out what is on the end of the line after the
1609 # conditional.
1610 substr($s, 0, length($c)) = '';
1611 $s =~ s/\n.*//g;
1613 if ($s =~ /^\s*;/) {
1614 ERROR("while should follow close brace '}'\n" . $hereprev);
1618 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
1619 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1620 # print "No studly caps, use _\n";
1621 # print "$herecurr";
1622 # $clean = 0;
1625 #no spaces allowed after \ in define
1626 if ($line=~/\#define.*\\\s$/) {
1627 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
1630 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1631 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
1632 my $checkfile = "$root/include/linux/$1.h";
1633 if (-f $checkfile && $1 ne 'irq.h') {
1634 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1635 $herecurr);
1639 # multi-statement macros should be enclosed in a do while loop, grab the
1640 # first statement and ensure its the whole macro if its not enclosed
1641 # in a known good container
1642 if ($prevline =~ /\#define.*\\/ &&
1643 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1644 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1645 $line !~ /^.\s*$Declare\s/) {
1646 # Grab the first statement, if that is the entire macro
1647 # its ok. This may start either on the #define line
1648 # or the one below.
1649 my $ln = $linenr;
1650 my $cnt = $realcnt;
1651 my $off = 0;
1653 # If the macro starts on the define line start
1654 # grabbing the statement after the identifier
1655 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1656 ##print "1<$1> 2<$2>\n";
1657 if (defined $2 && $2 ne '') {
1658 $off = length($1);
1659 $ln--;
1660 $cnt++;
1661 while ($lines[$ln - 1] =~ /^-/) {
1662 $ln--;
1663 $cnt++;
1666 my @ctx = ctx_statement($ln, $cnt, $off);
1667 my $ctx_ln = $ln + $#ctx + 1;
1668 my $ctx = join("\n", @ctx);
1670 # Pull in any empty extension lines.
1671 while ($ctx =~ /\\$/ &&
1672 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1673 $ctx .= $lines[$ctx_ln - 1];
1674 $ctx_ln++;
1677 if ($ctx =~ /\\$/) {
1678 if ($ctx =~ /;/) {
1679 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1680 } else {
1681 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1686 # check for redundant bracing round if etc
1687 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1688 my ($level, $endln, @chunks) =
1689 ctx_statement_full($linenr, $realcnt, 1);
1690 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1691 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
1692 if ($#chunks > 0 && $level == 0) {
1693 my $allowed = 0;
1694 my $seen = 0;
1695 my $herectx = $here . "\n";;
1696 my $ln = $linenr - 1;
1697 for my $chunk (@chunks) {
1698 my ($cond, $block) = @{$chunk};
1700 $herectx .= "$rawlines[$ln]\n[...]\n";
1701 $ln += statement_rawlines($block) - 1;
1703 substr($block, 0, length($cond)) = '';
1705 $seen++ if ($block =~ /^\s*{/);
1707 #print "cond<$cond> block<$block> allowed<$allowed>\n";
1708 if (statement_lines($cond) > 1) {
1709 #print "APW: ALLOWED: cond<$cond>\n";
1710 $allowed = 1;
1712 if ($block =~/\b(?:if|for|while)\b/) {
1713 #print "APW: ALLOWED: block<$block>\n";
1714 $allowed = 1;
1716 if (statement_block_size($block) > 1) {
1717 #print "APW: ALLOWED: lines block<$block>\n";
1718 $allowed = 1;
1721 if ($seen && !$allowed) {
1722 WARN("braces {} are not necessary for any arm of this statement\n" . $herectx);
1724 # Either way we have looked over this whole
1725 # statement and said what needs to be said.
1726 $suppress_ifbraces = $endln;
1729 if ($linenr > $suppress_ifbraces &&
1730 $line =~ /\b(if|while|for|else)\b/) {
1731 my ($level, $endln, @chunks) =
1732 ctx_statement_full($linenr, $realcnt, $-[0]);
1734 my $allowed = 0;
1736 # Check the pre-context.
1737 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
1738 #print "APW: ALLOWED: pre<$1>\n";
1739 $allowed = 1;
1741 # Check the condition.
1742 my ($cond, $block) = @{$chunks[0]};
1743 if (defined $cond) {
1744 substr($block, 0, length($cond)) = '';
1746 if (statement_lines($cond) > 1) {
1747 #print "APW: ALLOWED: cond<$cond>\n";
1748 $allowed = 1;
1750 if ($block =~/\b(?:if|for|while)\b/) {
1751 #print "APW: ALLOWED: block<$block>\n";
1752 $allowed = 1;
1754 if (statement_block_size($block) > 1) {
1755 #print "APW: ALLOWED: lines block<$block>\n";
1756 $allowed = 1;
1758 # Check the post-context.
1759 if (defined $chunks[1]) {
1760 my ($cond, $block) = @{$chunks[1]};
1761 if (defined $cond) {
1762 substr($block, 0, length($cond)) = '';
1764 if ($block =~ /^\s*\{/) {
1765 #print "APW: ALLOWED: chunk-1 block<$block>\n";
1766 $allowed = 1;
1769 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
1770 my $herectx = $here . "\n";;
1771 my $end = $linenr + statement_rawlines($block) - 1;
1773 for (my $ln = $linenr - 1; $ln < $end; $ln++) {
1774 $herectx .= $rawlines[$ln] . "\n";;
1777 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
1781 # don't include deprecated include files (uses RAW line)
1782 for my $inc (@dep_includes) {
1783 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
1784 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1788 # don't use deprecated functions
1789 for my $func (@dep_functions) {
1790 if ($line =~ /\b$func\b/) {
1791 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1795 # no volatiles please
1796 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1797 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
1798 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
1801 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1802 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1803 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1806 # warn about #if 0
1807 if ($line =~ /^.#\s*if\s+0\b/) {
1808 CHK("if this code is redundant consider removing it\n" .
1809 $herecurr);
1812 # check for needless kfree() checks
1813 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1814 my $expr = $1;
1815 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1816 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1820 # warn about #ifdefs in C files
1821 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1822 # print "#ifdef in C files should be avoided\n";
1823 # print "$herecurr";
1824 # $clean = 0;
1827 # warn about spacing in #ifdefs
1828 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1829 ERROR("exactly one space required after that #$1\n" . $herecurr);
1832 # check for spinlock_t definitions without a comment.
1833 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1834 my $which = $1;
1835 if (!ctx_has_comment($first_line, $linenr)) {
1836 CHK("$1 definition without comment\n" . $herecurr);
1839 # check for memory barriers without a comment.
1840 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1841 if (!ctx_has_comment($first_line, $linenr)) {
1842 CHK("memory barrier without comment\n" . $herecurr);
1845 # check of hardware specific defines
1846 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1847 CHK("architecture specific defines should be avoided\n" . $herecurr);
1850 # check the location of the inline attribute, that it is between
1851 # storage class and type.
1852 if ($line =~ /\b$Type\s+$Inline\b/ ||
1853 $line =~ /\b$Inline\s+$Storage\b/) {
1854 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1857 # Check for __inline__ and __inline, prefer inline
1858 if ($line =~ /\b(__inline__|__inline)\b/) {
1859 WARN("plain inline is preferred over $1\n" . $herecurr);
1862 # check for new externs in .c files.
1863 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1864 WARN("externs should be avoided in .c files\n" . $herecurr);
1867 # checks for new __setup's
1868 if ($rawline =~ /\b__setup\("([^"]*)"/) {
1869 my $name = $1;
1871 if (!grep(/$name/, @setup_docs)) {
1872 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1876 # check for pointless casting of kmalloc return
1877 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
1878 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
1881 # check for gcc specific __FUNCTION__
1882 if ($line =~ /__FUNCTION__/) {
1883 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
1887 # If we have no input at all, then there is nothing to report on
1888 # so just keep quiet.
1889 if ($#rawlines == -1) {
1890 exit(0);
1893 # In mailback mode only produce a report in the negative, for
1894 # things that appear to be patches.
1895 if ($mailback && ($clean == 1 || !$is_patch)) {
1896 exit(0);
1899 # This is not a patch, and we are are in 'no-patch' mode so
1900 # just keep quiet.
1901 if (!$chk_patch && !$is_patch) {
1902 exit(0);
1905 if (!$is_patch) {
1906 ERROR("Does not appear to be a unified-diff format patch\n");
1908 if ($is_patch && $chk_signoff && $signoff == 0) {
1909 ERROR("Missing Signed-off-by: line(s)\n");
1912 print report_dump();
1913 if ($summary && !($clean == 1 && $quiet == 1)) {
1914 print "$filename " if ($summary_file);
1915 print "total: $cnt_error errors, $cnt_warn warnings, " .
1916 (($check)? "$cnt_chk checks, " : "") .
1917 "$cnt_lines lines checked\n";
1918 print "\n" if ($quiet == 0);
1921 if ($clean == 1 && $quiet == 0) {
1922 print "$vname has no obvious style problems and is ready for submission.\n"
1924 if ($clean == 0 && $quiet == 0) {
1925 print "$vname has style problems, please review. If any of these errors\n";
1926 print "are false positives report them to the maintainer, see\n";
1927 print "CHECKPATCH in MAINTAINERS.\n";
1930 return $clean;