Initial commit
[wrt350n-kernel.git] / scripts / checkpatch.pl
blob2086a856400a9c7258958b96fe0f3d53d3d70ff8
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.14';
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|
109 fastcall
111 our $Attribute = qr{
112 const|
113 __read_mostly|
114 __kprobes|
115 __(?:mem|cpu|dev|)(?:initdata|init)
117 our $Inline = qr{inline|__always_inline|noinline};
118 our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
119 our $Lval = qr{$Ident(?:$Member)*};
121 our $Constant = qr{(?:[0-9]+|0x[0-9a-fA-F]+)[UL]*};
122 our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
123 our $Operators = qr{
124 <=|>=|==|!=|
125 =>|->|<<|>>|<|>|!|~|
126 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
129 our $NonptrType;
130 our $Type;
131 our $Declare;
133 our @typeList = (
134 qr{void},
135 qr{char},
136 qr{short},
137 qr{int},
138 qr{long},
139 qr{unsigned},
140 qr{float},
141 qr{double},
142 qr{bool},
143 qr{long\s+int},
144 qr{long\s+long},
145 qr{long\s+long\s+int},
146 qr{(?:__)?(?:u|s|be|le)(?:8|16|32|64)},
147 qr{struct\s+$Ident},
148 qr{union\s+$Ident},
149 qr{enum\s+$Ident},
150 qr{${Ident}_t},
151 qr{${Ident}_handler},
152 qr{${Ident}_handler_fn},
155 sub build_types {
156 my $all = "(?: \n" . join("|\n ", @typeList) . "\n)";
157 $NonptrType = qr{
159 (?:const\s+)?
160 (?:unsigned\s+)?
161 $all
162 (?:\s+$Sparse|\s+const)*
165 $Type = qr{
166 \b$NonptrType\b
167 (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
168 (?:\s+$Inline|\s+$Sparse|\s+$Attribute)*
170 $Declare = qr{(?:$Storage\s+)?$Type};
172 build_types();
174 $chk_signoff = 0 if ($file);
176 my @dep_includes = ();
177 my @dep_functions = ();
178 my $removal = "Documentation/feature-removal-schedule.txt";
179 if ($tree && -f "$root/$removal") {
180 open(REMOVE, "<$root/$removal") ||
181 die "$P: $removal: open failed - $!\n";
182 while (<REMOVE>) {
183 if (/^Check:\s+(.*\S)/) {
184 for my $entry (split(/[, ]+/, $1)) {
185 if ($entry =~ m@include/(.*)@) {
186 push(@dep_includes, $1);
188 } elsif ($entry !~ m@/@) {
189 push(@dep_functions, $entry);
196 my @rawlines = ();
197 my @lines = ();
198 my $vname;
199 for my $filename (@ARGV) {
200 if ($file) {
201 open(FILE, "diff -u /dev/null $filename|") ||
202 die "$P: $filename: diff failed - $!\n";
203 } else {
204 open(FILE, "<$filename") ||
205 die "$P: $filename: open failed - $!\n";
207 if ($filename eq '-') {
208 $vname = 'Your patch';
209 } else {
210 $vname = $filename;
212 while (<FILE>) {
213 chomp;
214 push(@rawlines, $_);
216 close(FILE);
217 if (!process($filename)) {
218 $exit = 1;
220 @rawlines = ();
221 @lines = ();
224 exit($exit);
226 sub top_of_kernel_tree {
227 my ($root) = @_;
229 my @tree_check = (
230 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
231 "README", "Documentation", "arch", "include", "drivers",
232 "fs", "init", "ipc", "kernel", "lib", "scripts",
235 foreach my $check (@tree_check) {
236 if (! -e $root . '/' . $check) {
237 return 0;
240 return 1;
243 sub expand_tabs {
244 my ($str) = @_;
246 my $res = '';
247 my $n = 0;
248 for my $c (split(//, $str)) {
249 if ($c eq "\t") {
250 $res .= ' ';
251 $n++;
252 for (; ($n % 8) != 0; $n++) {
253 $res .= ' ';
255 next;
257 $res .= $c;
258 $n++;
261 return $res;
263 sub copy_spacing {
264 my ($str) = @_;
266 my $res = '';
267 for my $c (split(//, $str)) {
268 if ($c eq "\t") {
269 $res .= $c;
270 } else {
271 $res .= ' ';
275 return $res;
278 sub line_stats {
279 my ($line) = @_;
281 # Drop the diff line leader and expand tabs
282 $line =~ s/^.//;
283 $line = expand_tabs($line);
285 # Pick the indent from the front of the line.
286 my ($white) = ($line =~ /^(\s*)/);
288 return (length($line), length($white));
291 sub sanitise_line {
292 my ($line) = @_;
294 my $res = '';
295 my $l = '';
297 my $quote = '';
298 my $qlen = 0;
300 foreach my $c (split(//, $line)) {
301 # The second backslash of a pair is not a "quote".
302 if ($l eq "\\" && $c eq "\\") {
303 $c = 'X';
305 if ($l ne "\\" && ($c eq "'" || $c eq '"')) {
306 if ($quote eq '') {
307 $quote = $c;
308 $res .= $c;
309 $l = $c;
310 $qlen = 0;
311 next;
312 } elsif ($quote eq $c) {
313 $quote = '';
316 if ($quote eq "'" && $qlen > 1) {
317 $quote = '';
319 if ($quote && $c ne "\t") {
320 $res .= "X";
321 $qlen++;
322 } else {
323 $res .= $c;
326 $l = $c;
329 # Clear out the comments.
330 while ($res =~ m@(/\*.*?\*/)@g) {
331 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
333 if ($res =~ m@(/\*.*)@) {
334 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
336 if ($res =~ m@^.(.*\*/)@) {
337 substr($res, $-[1], $+[1] - $-[1]) = $; x ($+[1] - $-[1]);
340 # The pathname on a #include may be surrounded by '<' and '>'.
341 if ($res =~ /^.#\s*include\s+\<(.*)\>/) {
342 my $clean = 'X' x length($1);
343 $res =~ s@\<.*\>@<$clean>@;
345 # The whole of a #error is a string.
346 } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) {
347 my $clean = 'X' x length($1);
348 $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@;
351 return $res;
354 sub ctx_statement_block {
355 my ($linenr, $remain, $off) = @_;
356 my $line = $linenr - 1;
357 my $blk = '';
358 my $soff = $off;
359 my $coff = $off - 1;
361 my $loff = 0;
363 my $type = '';
364 my $level = 0;
365 my $c;
366 my $len = 0;
368 my $remainder;
369 while (1) {
370 #warn "CSB: blk<$blk>\n";
371 # If we are about to drop off the end, pull in more
372 # context.
373 if ($off >= $len) {
374 for (; $remain > 0; $line++) {
375 next if ($lines[$line] =~ /^-/);
376 $remain--;
377 $loff = $len;
378 $blk .= $lines[$line] . "\n";
379 $len = length($blk);
380 $line++;
381 last;
383 # Bail if there is no further context.
384 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
385 if ($off >= $len) {
386 last;
389 $c = substr($blk, $off, 1);
390 $remainder = substr($blk, $off);
392 #warn "CSB: c<$c> type<$type> level<$level>\n";
393 # Statement ends at the ';' or a close '}' at the
394 # outermost level.
395 if ($level == 0 && $c eq ';') {
396 last;
399 # An else is really a conditional as long as its not else if
400 if ($level == 0 && $remainder =~ /(\s+else)(?:\s|{)/ &&
401 $remainder !~ /\s+else\s+if\b/) {
402 $coff = $off + length($1);
405 if (($type eq '' || $type eq '(') && $c eq '(') {
406 $level++;
407 $type = '(';
409 if ($type eq '(' && $c eq ')') {
410 $level--;
411 $type = ($level != 0)? '(' : '';
413 if ($level == 0 && $coff < $soff) {
414 $coff = $off;
417 if (($type eq '' || $type eq '{') && $c eq '{') {
418 $level++;
419 $type = '{';
421 if ($type eq '{' && $c eq '}') {
422 $level--;
423 $type = ($level != 0)? '{' : '';
425 if ($level == 0) {
426 last;
429 $off++;
431 if ($off == $len) {
432 $line++;
433 $remain--;
436 my $statement = substr($blk, $soff, $off - $soff + 1);
437 my $condition = substr($blk, $soff, $coff - $soff + 1);
439 #warn "STATEMENT<$statement>\n";
440 #warn "CONDITION<$condition>\n";
442 #print "off<$off> loff<$loff>\n";
444 return ($statement, $condition,
445 $line, $remain + 1, $off - $loff + 1, $level);
448 sub ctx_statement_full {
449 my ($linenr, $remain, $off) = @_;
450 my ($statement, $condition, $level);
452 my (@chunks);
454 ($statement, $condition, $linenr, $remain, $off, $level) =
455 ctx_statement_block($linenr, $remain, $off);
456 #print "F: c<$condition> s<$statement>\n";
457 for (;;) {
458 push(@chunks, [ $condition, $statement ]);
459 last if (!($remain > 0 && $condition =~ /^.\s*(?:if|else|do)/));
460 ($statement, $condition, $linenr, $remain, $off, $level) =
461 ctx_statement_block($linenr, $remain, $off);
462 #print "C: c<$condition> s<$statement>\n";
465 return ($level, $linenr, @chunks);
468 sub ctx_block_get {
469 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
470 my $line;
471 my $start = $linenr - 1;
472 my $blk = '';
473 my @o;
474 my @c;
475 my @res = ();
477 my $level = 0;
478 for ($line = $start; $remain > 0; $line++) {
479 next if ($rawlines[$line] =~ /^-/);
480 $remain--;
482 $blk .= $rawlines[$line];
483 foreach my $c (split(//, $rawlines[$line])) {
484 ##print "C<$c>L<$level><$open$close>O<$off>\n";
485 if ($off > 0) {
486 $off--;
487 next;
490 if ($c eq $close && $level > 0) {
491 $level--;
492 last if ($level == 0);
493 } elsif ($c eq $open) {
494 $level++;
498 if (!$outer || $level <= 1) {
499 push(@res, $rawlines[$line]);
502 last if ($level == 0);
505 return ($level, @res);
507 sub ctx_block_outer {
508 my ($linenr, $remain) = @_;
510 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
511 return @r;
513 sub ctx_block {
514 my ($linenr, $remain) = @_;
516 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
517 return @r;
519 sub ctx_statement {
520 my ($linenr, $remain, $off) = @_;
522 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
523 return @r;
525 sub ctx_block_level {
526 my ($linenr, $remain) = @_;
528 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
530 sub ctx_statement_level {
531 my ($linenr, $remain, $off) = @_;
533 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
536 sub ctx_locate_comment {
537 my ($first_line, $end_line) = @_;
539 # Catch a comment on the end of the line itself.
540 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*$@);
541 return $current_comment if (defined $current_comment);
543 # Look through the context and try and figure out if there is a
544 # comment.
545 my $in_comment = 0;
546 $current_comment = '';
547 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
548 my $line = $rawlines[$linenr - 1];
549 #warn " $line\n";
550 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
551 $in_comment = 1;
553 if ($line =~ m@/\*@) {
554 $in_comment = 1;
556 if (!$in_comment && $current_comment ne '') {
557 $current_comment = '';
559 $current_comment .= $line . "\n" if ($in_comment);
560 if ($line =~ m@\*/@) {
561 $in_comment = 0;
565 chomp($current_comment);
566 return($current_comment);
568 sub ctx_has_comment {
569 my ($first_line, $end_line) = @_;
570 my $cmt = ctx_locate_comment($first_line, $end_line);
572 ##print "LINE: $rawlines[$end_line - 1 ]\n";
573 ##print "CMMT: $cmt\n";
575 return ($cmt ne '');
578 sub cat_vet {
579 my ($vet) = @_;
580 my ($res, $coded);
582 $res = '';
583 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
584 $res .= $1;
585 if ($2 ne '') {
586 $coded = sprintf("^%c", unpack('C', $2) + 64);
587 $res .= $coded;
590 $res =~ s/$/\$/;
592 return $res;
595 my $av_preprocessor = 0;
596 my $av_paren = 0;
597 my @av_paren_type;
599 sub annotate_reset {
600 $av_preprocessor = 0;
601 $av_paren = 0;
602 @av_paren_type = ();
605 sub annotate_values {
606 my ($stream, $type) = @_;
608 my $res;
609 my $cur = $stream;
611 print "$stream\n" if ($dbg_values > 1);
613 while (length($cur)) {
614 print " <$type> " if ($dbg_values > 1);
615 if ($cur =~ /^(\s+)/o) {
616 print "WS($1)\n" if ($dbg_values > 1);
617 if ($1 =~ /\n/ && $av_preprocessor) {
618 $av_preprocessor = 0;
619 $type = 'N';
622 } elsif ($cur =~ /^($Type)/) {
623 print "DECLARE($1)\n" if ($dbg_values > 1);
624 $type = 'T';
626 } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) {
627 print "DEFINE($1)\n" if ($dbg_values > 1);
628 $av_preprocessor = 1;
629 $av_paren_type[$av_paren] = 'N';
631 } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|elif|endif))/o) {
632 print "PRE($1)\n" if ($dbg_values > 1);
633 $av_preprocessor = 1;
634 $type = 'N';
636 } elsif ($cur =~ /^(\\\n)/o) {
637 print "PRECONT($1)\n" if ($dbg_values > 1);
639 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
640 print "SIZEOF($1)\n" if ($dbg_values > 1);
641 if (defined $2) {
642 $av_paren_type[$av_paren] = 'V';
644 $type = 'N';
646 } elsif ($cur =~ /^(if|while|typeof|__typeof__|for)\b/o) {
647 print "COND($1)\n" if ($dbg_values > 1);
648 $av_paren_type[$av_paren] = 'N';
649 $type = 'N';
651 } elsif ($cur =~/^(return|case|else)/o) {
652 print "KEYWORD($1)\n" if ($dbg_values > 1);
653 $type = 'N';
655 } elsif ($cur =~ /^(\()/o) {
656 print "PAREN('$1')\n" if ($dbg_values > 1);
657 $av_paren++;
658 $type = 'N';
660 } elsif ($cur =~ /^(\))/o) {
661 $av_paren-- if ($av_paren > 0);
662 if (defined $av_paren_type[$av_paren]) {
663 $type = $av_paren_type[$av_paren];
664 undef $av_paren_type[$av_paren];
665 print "PAREN('$1') -> $type\n"
666 if ($dbg_values > 1);
667 } else {
668 print "PAREN('$1')\n" if ($dbg_values > 1);
671 } elsif ($cur =~ /^($Ident)\(/o) {
672 print "FUNC($1)\n" if ($dbg_values > 1);
673 $av_paren_type[$av_paren] = 'V';
675 } elsif ($cur =~ /^($Ident|$Constant)/o) {
676 print "IDENT($1)\n" if ($dbg_values > 1);
677 $type = 'V';
679 } elsif ($cur =~ /^($Assignment)/o) {
680 print "ASSIGN($1)\n" if ($dbg_values > 1);
681 $type = 'N';
683 } elsif ($cur =~/^(;)/) {
684 print "END($1)\n" if ($dbg_values > 1);
685 $type = 'E';
687 } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) {
688 print "CLOSE($1)\n" if ($dbg_values > 1);
689 $type = 'N';
691 } elsif ($cur =~ /^($Operators)/o) {
692 print "OP($1)\n" if ($dbg_values > 1);
693 if ($1 ne '++' && $1 ne '--') {
694 $type = 'N';
697 } elsif ($cur =~ /(^.)/o) {
698 print "C($1)\n" if ($dbg_values > 1);
700 if (defined $1) {
701 $cur = substr($cur, length($1));
702 $res .= $type x length($1);
706 return $res;
709 sub possible {
710 my ($possible, $line) = @_;
712 #print "CHECK<$possible>\n";
713 if ($possible !~ /^(?:$Storage|$Type|DEFINE_\S+)$/ &&
714 $possible ne 'goto' && $possible ne 'return' &&
715 $possible ne 'struct' && $possible ne 'enum' &&
716 $possible ne 'case' && $possible ne 'else' &&
717 $possible ne 'typedef') {
718 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
719 push(@typeList, $possible);
720 build_types();
724 my $prefix = '';
726 sub report {
727 my $line = $prefix . $_[0];
729 $line = (split('\n', $line))[0] . "\n" if ($terse);
731 push(our @report, $line);
733 sub report_dump {
734 our @report;
736 sub ERROR {
737 report("ERROR: $_[0]\n");
738 our $clean = 0;
739 our $cnt_error++;
741 sub WARN {
742 report("WARNING: $_[0]\n");
743 our $clean = 0;
744 our $cnt_warn++;
746 sub CHK {
747 if ($check) {
748 report("CHECK: $_[0]\n");
749 our $clean = 0;
750 our $cnt_chk++;
754 sub process {
755 my $filename = shift;
757 my $linenr=0;
758 my $prevline="";
759 my $prevrawline="";
760 my $stashline="";
761 my $stashrawline="";
763 my $length;
764 my $indent;
765 my $previndent=0;
766 my $stashindent=0;
768 our $clean = 1;
769 my $signoff = 0;
770 my $is_patch = 0;
772 our @report = ();
773 our $cnt_lines = 0;
774 our $cnt_error = 0;
775 our $cnt_warn = 0;
776 our $cnt_chk = 0;
778 # Trace the real file/line as we go.
779 my $realfile = '';
780 my $realline = 0;
781 my $realcnt = 0;
782 my $here = '';
783 my $in_comment = 0;
784 my $comment_edge = 0;
785 my $first_line = 0;
787 my $prev_values = 'E';
789 # suppression flags
790 my $suppress_ifbraces = 0;
792 # Pre-scan the patch sanitizing the lines.
793 # Pre-scan the patch looking for any __setup documentation.
795 my @setup_docs = ();
796 my $setup_docs = 0;
797 my $line;
798 foreach my $rawline (@rawlines) {
799 # Standardise the strings and chars within the input to
800 # simplify matching.
801 $line = sanitise_line($rawline);
802 push(@lines, $line);
804 ##print "==>$rawline\n";
805 ##print "-->$line\n";
807 if ($line=~/^\+\+\+\s+(\S+)/) {
808 $setup_docs = 0;
809 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
810 $setup_docs = 1;
812 next;
815 if ($setup_docs && $line =~ /^\+/) {
816 push(@setup_docs, $line);
820 $prefix = '';
822 foreach my $line (@lines) {
823 $linenr++;
825 my $rawline = $rawlines[$linenr - 1];
827 #extract the filename as it passes
828 if ($line=~/^\+\+\+\s+(\S+)/) {
829 $realfile=$1;
830 $realfile =~ s@^[^/]*/@@;
831 $in_comment = 0;
832 next;
834 #extract the line range in the file after the patch is applied
835 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
836 $is_patch = 1;
837 $first_line = $linenr + 1;
838 $in_comment = 0;
839 $realline=$1-1;
840 if (defined $2) {
841 $realcnt=$3+1;
842 } else {
843 $realcnt=1+1;
845 annotate_reset();
846 $prev_values = 'E';
848 $suppress_ifbraces = $linenr - 1;
849 next;
852 # track the line number as we move through the hunk, note that
853 # new versions of GNU diff omit the leading space on completely
854 # blank context lines so we need to count that too.
855 if ($line =~ /^( |\+|$)/) {
856 $realline++;
857 $realcnt-- if ($realcnt != 0);
859 # Guestimate if this is a continuing comment. Run
860 # the context looking for a comment "edge". If this
861 # edge is a close comment then we must be in a comment
862 # at context start.
863 if ($linenr == $first_line) {
864 my $edge;
865 for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) {
866 ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@);
867 last if (defined $edge);
869 if (defined $edge && $edge eq '*/') {
870 $in_comment = 1;
874 # Guestimate if this is a continuing comment. If this
875 # is the start of a diff block and this line starts
876 # ' *' then it is very likely a comment.
877 if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) {
878 $in_comment = 1;
881 # Find the last comment edge on _this_ line.
882 $comment_edge = 0;
883 while (($rawline =~ m@(/\*|\*/)@g)) {
884 if ($1 eq '/*') {
885 $in_comment = 1;
886 } else {
887 $in_comment = 0;
889 $comment_edge = 1;
892 # Measure the line length and indent.
893 ($length, $indent) = line_stats($rawline);
895 # Track the previous line.
896 ($prevline, $stashline) = ($stashline, $line);
897 ($previndent, $stashindent) = ($stashindent, $indent);
898 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
900 #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n";
902 } elsif ($realcnt == 1) {
903 $realcnt--;
906 #make up the handle for any error we report on this line
907 $here = "#$linenr: " if (!$file);
908 $here = "#$realline: " if ($file);
909 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
911 my $hereline = "$here\n$rawline\n";
912 my $herecurr = "$here\n$rawline\n";
913 my $hereprev = "$here\n$prevrawline\n$rawline\n";
915 $prefix = "$filename:$realline: " if ($emacs && $file);
916 $prefix = "$filename:$linenr: " if ($emacs && !$file);
917 $cnt_lines++ if ($realcnt != 0);
919 #check the patch for a signoff:
920 if ($line =~ /^\s*signed-off-by:/i) {
921 # This is a signoff, if ugly, so do not double report.
922 $signoff++;
923 if (!($line =~ /^\s*Signed-off-by:/)) {
924 WARN("Signed-off-by: is the preferred form\n" .
925 $herecurr);
927 if ($line =~ /^\s*signed-off-by:\S/i) {
928 WARN("need space after Signed-off-by:\n" .
929 $herecurr);
933 # Check for wrappage within a valid hunk of the file
934 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
935 ERROR("patch seems to be corrupt (line wrapped?)\n" .
936 $herecurr) if (!$emitted_corrupt++);
939 # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
940 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
941 !($rawline =~ m/^(
942 [\x09\x0A\x0D\x20-\x7E] # ASCII
943 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
944 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
945 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
946 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
947 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
948 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
949 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
950 )*$/x )) {
951 ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr);
954 #ignore lines being removed
955 if ($line=~/^-/) {next;}
957 # check we are in a valid source file if not then ignore this hunk
958 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
960 #trailing whitespace
961 if ($line =~ /^\+.*\015/) {
962 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
963 ERROR("DOS line endings\n" . $herevet);
965 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
966 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
967 ERROR("trailing whitespace\n" . $herevet);
969 #80 column limit
970 if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) {
971 WARN("line over 80 characters\n" . $herecurr);
974 # check for adding lines without a newline.
975 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
976 WARN("adding a line without newline at end of file\n" . $herecurr);
979 # check we are in a valid source file *.[hc] if not then ignore this hunk
980 next if ($realfile !~ /\.[hc]$/);
982 # at the beginning of a line any tabs must come first and anything
983 # more than 8 must use tabs.
984 if ($rawline =~ /^\+\s* \t\s*\S/ ||
985 $rawline =~ /^\+\s* \s*/) {
986 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
987 ERROR("use tabs not spaces\n" . $herevet);
990 # check for RCS/CVS revision markers
991 if ($rawline =~ /\$(Revision|Log|Id)(?:\$|)/) {
992 WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr);
995 # The rest of our checks refer specifically to C style
996 # only apply those _outside_ comments. Only skip
997 # lines in the middle of comments.
998 next if (!$comment_edge && $in_comment);
1000 # Check for potential 'bare' types
1001 if ($realcnt) {
1002 # Ignore goto labels.
1003 if ($line =~ /$Ident:\*$/) {
1005 # Ignore functions being called
1006 } elsif ($line =~ /^.\s*$Ident\s*\(/) {
1008 # definitions in global scope can only start with types
1009 } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) {
1010 possible($1, $line);
1012 # declarations always start with types
1013 } elsif ($prev_values eq 'E' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=|,)/) {
1014 possible($1);
1017 # any (foo ... *) is a pointer cast, and foo is a type
1018 while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) {
1019 possible($1, $line);
1022 # Check for any sort of function declaration.
1023 # int foo(something bar, other baz);
1024 # void (*store_gdt)(x86_descr_ptr *);
1025 if ($prev_values eq 'E' && $line =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) {
1026 my ($name_len) = length($1);
1027 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len);
1028 my $ctx = join("\n", @ctx);
1030 $ctx =~ s/\n.//;
1031 substr($ctx, 0, $name_len + 1) = '';
1032 $ctx =~ s/\)[^\)]*$//;
1033 for my $arg (split(/\s*,\s*/, $ctx)) {
1034 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/ || $arg =~ /^($Ident)$/) {
1036 possible($1, $line);
1044 # Checks which may be anchored in the context.
1047 # Check for switch () and associated case and default
1048 # statements should be at the same indent.
1049 if ($line=~/\bswitch\s*\(.*\)/) {
1050 my $err = '';
1051 my $sep = '';
1052 my @ctx = ctx_block_outer($linenr, $realcnt);
1053 shift(@ctx);
1054 for my $ctx (@ctx) {
1055 my ($clen, $cindent) = line_stats($ctx);
1056 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
1057 $indent != $cindent) {
1058 $err .= "$sep$ctx\n";
1059 $sep = '';
1060 } else {
1061 $sep = "[...]\n";
1064 if ($err ne '') {
1065 ERROR("switch and case should be at the same indent\n$hereline$err");
1069 # if/while/etc brace do not go on next line, unless defining a do while loop,
1070 # or if that brace on the next line is for something else
1071 if ($line =~ /\b(?:(if|while|for|switch)\s*\(|do\b|else\b)/ && $line !~ /^.#/) {
1072 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
1073 my $ctx_ln = $linenr + $#ctx + 1;
1074 my $ctx_cnt = $realcnt - $#ctx - 1;
1075 my $ctx = join("\n", @ctx);
1077 # Skip over any removed lines in the context following statement.
1078 while ($ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^-/) {
1079 $ctx_ln++;
1080 $ctx_cnt--;
1082 ##warn "line<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>";
1084 if ($ctx !~ /{\s*/ && $ctx_cnt > 0 && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
1085 ERROR("That open brace { should be on the previous line\n" .
1086 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1088 if ($level == 0 && $ctx =~ /\)\s*\;\s*$/ && defined $lines[$ctx_ln - 1]) {
1089 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
1090 if ($nindent > $indent) {
1091 WARN("Trailing semicolon indicates no statements, indent implies otherwise\n" .
1092 "$here\n$ctx\n$lines[$ctx_ln - 1]");
1097 # Track the 'values' across context and added lines.
1098 my $opline = $line; $opline =~ s/^./ /;
1099 my $curr_values = annotate_values($opline . "\n", $prev_values);
1100 $curr_values = $prev_values . $curr_values;
1101 if ($dbg_values) {
1102 my $outline = $opline; $outline =~ s/\t/ /g;
1103 warn "--> .$outline\n";
1104 warn "--> $curr_values\n";
1106 $prev_values = substr($curr_values, -1);
1108 #ignore lines not being added
1109 if ($line=~/^[^\+]/) {next;}
1111 # TEST: allow direct testing of the type matcher.
1112 if ($tst_type && $line =~ /^.$Declare$/) {
1113 ERROR("TEST: is type $Declare\n" . $herecurr);
1114 next;
1117 # check for initialisation to aggregates open brace on the next line
1118 if ($prevline =~ /$Declare\s*$Ident\s*=\s*$/ &&
1119 $line =~ /^.\s*{/) {
1120 ERROR("That open brace { should be on the previous line\n" . $hereprev);
1124 # Checks which are anchored on the added line.
1127 # check for malformed paths in #include statements (uses RAW line)
1128 if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) {
1129 my $path = $1;
1130 if ($path =~ m{//}) {
1131 ERROR("malformed #include filename\n" .
1132 $herecurr);
1136 # no C99 // comments
1137 if ($line =~ m{//}) {
1138 ERROR("do not use C99 // comments\n" . $herecurr);
1140 # Remove C99 comments.
1141 $line =~ s@//.*@@;
1142 $opline =~ s@//.*@@;
1144 #EXPORT_SYMBOL should immediately follow its function closing }.
1145 if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) ||
1146 ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
1147 my $name = $1;
1148 if (($prevline !~ /^}/) &&
1149 ($prevline !~ /^\+}/) &&
1150 ($prevline !~ /^ }/) &&
1151 ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
1152 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
1156 # check for external initialisers.
1157 if ($line =~ /^.$Type\s*$Ident\s*=\s*(0|NULL);/) {
1158 ERROR("do not initialise externals to 0 or NULL\n" .
1159 $herecurr);
1161 # check for static initialisers.
1162 if ($line =~ /\s*static\s.*=\s*(0|NULL);/) {
1163 ERROR("do not initialise statics to 0 or NULL\n" .
1164 $herecurr);
1167 # check for new typedefs, only function parameters and sparse annotations
1168 # make sense.
1169 if ($line =~ /\btypedef\s/ &&
1170 $line !~ /\btypedef\s+$Type\s+\(\s*\*?$Ident\s*\)\s*\(/ &&
1171 $line !~ /\b__bitwise(?:__|)\b/) {
1172 WARN("do not add new typedefs\n" . $herecurr);
1175 # * goes on variable not on type
1176 if ($line =~ m{\($NonptrType(\*+)(?:\s+const)?\)}) {
1177 ERROR("\"(foo$1)\" should be \"(foo $1)\"\n" .
1178 $herecurr);
1180 } elsif ($line =~ m{\($NonptrType\s+(\*+)(?!\s+const)\s+\)}) {
1181 ERROR("\"(foo $1 )\" should be \"(foo $1)\"\n" .
1182 $herecurr);
1184 } elsif ($line =~ m{$NonptrType(\*+)(?:\s+(?:$Attribute|$Sparse))?\s+[A-Za-z\d_]+}) {
1185 ERROR("\"foo$1 bar\" should be \"foo $1bar\"\n" .
1186 $herecurr);
1188 } elsif ($line =~ m{$NonptrType\s+(\*+)(?!\s+(?:$Attribute|$Sparse))\s+[A-Za-z\d_]+}) {
1189 ERROR("\"foo $1 bar\" should be \"foo $1bar\"\n" .
1190 $herecurr);
1193 # # no BUG() or BUG_ON()
1194 # if ($line =~ /\b(BUG|BUG_ON)\b/) {
1195 # print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n";
1196 # print "$herecurr";
1197 # $clean = 0;
1200 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
1201 WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
1204 # printk should use KERN_* levels. Note that follow on printk's on the
1205 # same line do not need a level, so we use the current block context
1206 # to try and find and validate the current printk. In summary the current
1207 # printk includes all preceeding printk's which have no newline on the end.
1208 # we assume the first bad printk is the one to report.
1209 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
1210 my $ok = 0;
1211 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
1212 #print "CHECK<$lines[$ln - 1]\n";
1213 # we have a preceeding printk if it ends
1214 # with "\n" ignore it, else it is to blame
1215 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
1216 if ($rawlines[$ln - 1] !~ m{\\n"}) {
1217 $ok = 1;
1219 last;
1222 if ($ok == 0) {
1223 WARN("printk() should include KERN_ facility level\n" . $herecurr);
1227 # function brace can't be on same line, except for #defines of do while,
1228 # or if closed on same line
1229 if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and
1230 !($line=~/\#define.*do\s{/) and !($line=~/}/)) {
1231 ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr);
1234 # open braces for enum, union and struct go on the same line.
1235 if ($line =~ /^.\s*{/ &&
1236 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
1237 ERROR("open brace '{' following $1 go on the same line\n" . $hereprev);
1240 # check for spaces between functions and their parentheses.
1241 while ($line =~ /($Ident)\s+\(/g) {
1242 my $name = $1;
1243 my $ctx = substr($line, 0, $-[1]);
1245 # Ignore those directives where spaces _are_ permitted.
1246 if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case|__asm__)$/) {
1248 # cpp #define statements have non-optional spaces, ie
1249 # if there is a space between the name and the open
1250 # parenthesis it is simply not a parameter group.
1251 } elsif ($ctx =~ /^.\#\s*define\s*$/) {
1253 # If this whole things ends with a type its most
1254 # likely a typedef for a function.
1255 } elsif ("$ctx$name" =~ /$Type$/) {
1257 } else {
1258 WARN("no space between function name and open parenthesis '('\n" . $herecurr);
1261 # Check operator spacing.
1262 if (!($line=~/\#\s*include/)) {
1263 my $ops = qr{
1264 <<=|>>=|<=|>=|==|!=|
1265 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
1266 =>|->|<<|>>|<|>|=|!|~|
1267 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%
1269 my @elements = split(/($;+|$ops|;)/, $opline);
1270 my $off = 0;
1272 my $blank = copy_spacing($opline);
1274 for (my $n = 0; $n < $#elements; $n += 2) {
1275 $off += length($elements[$n]);
1277 my $a = '';
1278 $a = 'V' if ($elements[$n] ne '');
1279 $a = 'W' if ($elements[$n] =~ /\s$/);
1280 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
1281 $a = 'O' if ($elements[$n] eq '');
1282 $a = 'E' if ($elements[$n] eq '' && $n == 0);
1284 my $op = $elements[$n + 1];
1286 my $c = '';
1287 if (defined $elements[$n + 2]) {
1288 $c = 'V' if ($elements[$n + 2] ne '');
1289 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
1290 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
1291 $c = 'O' if ($elements[$n + 2] eq '');
1292 $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
1293 } else {
1294 $c = 'E';
1297 # Pick up the preceeding and succeeding characters.
1298 my $ca = substr($opline, 0, $off);
1299 my $cc = '';
1300 if (length($opline) >= ($off + length($elements[$n + 1]))) {
1301 $cc = substr($opline, $off + length($elements[$n + 1]));
1303 my $cb = "$ca$;$cc";
1305 my $ctx = "${a}x${c}";
1307 my $at = "(ctx:$ctx)";
1309 my $ptr = substr($blank, 0, $off) . "^";
1310 my $hereptr = "$hereline$ptr\n";
1312 # Classify operators into binary, unary, or
1313 # definitions (* only) where they have more
1314 # than one mode.
1315 my $op_type = substr($curr_values, $off + 1, 1);
1316 my $op_left = substr($curr_values, $off, 1);
1317 my $is_unary;
1318 if ($op_type eq 'T') {
1319 $is_unary = 2;
1320 } elsif ($op_left eq 'V') {
1321 $is_unary = 0;
1322 } else {
1323 $is_unary = 1;
1325 #if ($op eq '-' || $op eq '&' || $op eq '*') {
1326 # print "UNARY: <$op_left$op_type $is_unary $a:$op:$c> <$ca:$op:$cc> <$unary_ctx>\n";
1329 # Ignore operators passed as parameters.
1330 if ($op_type ne 'V' &&
1331 $ca =~ /\s$/ && $cc =~ /^\s*,/) {
1333 # Ignore comments
1334 } elsif ($op =~ /^$;+$/) {
1336 # ; should have either the end of line or a space or \ after it
1337 } elsif ($op eq ';') {
1338 if ($ctx !~ /.x[WEB]/ && $cc !~ /^\\/ &&
1339 $cc !~ /^;/) {
1340 ERROR("need space after that '$op' $at\n" . $hereptr);
1343 # // is a comment
1344 } elsif ($op eq '//') {
1346 # -> should have no spaces
1347 } elsif ($op eq '->') {
1348 if ($ctx =~ /Wx.|.xW/) {
1349 ERROR("no spaces around that '$op' $at\n" . $hereptr);
1352 # , must have a space on the right.
1353 } elsif ($op eq ',') {
1354 if ($ctx !~ /.xW|.xE/ && $cc !~ /^}/) {
1355 ERROR("need space after that '$op' $at\n" . $hereptr);
1358 # '*' as part of a type definition -- reported already.
1359 } elsif ($op eq '*' && $is_unary == 2) {
1360 #warn "'*' is part of type\n";
1362 # unary operators should have a space before and
1363 # none after. May be left adjacent to another
1364 # unary operator, or a cast
1365 } elsif ($op eq '!' || $op eq '~' ||
1366 ($is_unary && ($op eq '*' || $op eq '-' || $op eq '&'))) {
1367 if ($ctx !~ /[WEB]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
1368 ERROR("need space before that '$op' $at\n" . $hereptr);
1370 if ($ctx =~ /.xW/) {
1371 ERROR("no space after that '$op' $at\n" . $hereptr);
1374 # unary ++ and unary -- are allowed no space on one side.
1375 } elsif ($op eq '++' or $op eq '--') {
1376 if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOBE]/) {
1377 ERROR("need space one side of that '$op' $at\n" . $hereptr);
1379 if ($ctx =~ /WxB/ || ($ctx =~ /Wx./ && $cc =~ /^;/)) {
1380 ERROR("no space before that '$op' $at\n" . $hereptr);
1383 # << and >> may either have or not have spaces both sides
1384 } elsif ($op eq '<<' or $op eq '>>' or
1385 $op eq '&' or $op eq '^' or $op eq '|' or
1386 $op eq '+' or $op eq '-' or
1387 $op eq '*' or $op eq '/' or
1388 $op eq '%')
1390 if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) {
1391 ERROR("need consistent spacing around '$op' $at\n" .
1392 $hereptr);
1395 # All the others need spaces both sides.
1396 } elsif ($ctx !~ /[EW]x[WE]/) {
1397 # Ignore email addresses <foo@bar>
1398 if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
1399 !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
1400 ERROR("need spaces around that '$op' $at\n" . $hereptr);
1403 $off += length($elements[$n + 1]);
1407 # check for multiple assignments
1408 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
1409 CHK("multiple assignments should be avoided\n" . $herecurr);
1412 ## # check for multiple declarations, allowing for a function declaration
1413 ## # continuation.
1414 ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
1415 ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
1417 ## # Remove any bracketed sections to ensure we do not
1418 ## # falsly report the parameters of functions.
1419 ## my $ln = $line;
1420 ## while ($ln =~ s/\([^\(\)]*\)//g) {
1421 ## }
1422 ## if ($ln =~ /,/) {
1423 ## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
1424 ## }
1425 ## }
1427 #need space before brace following if, while, etc
1428 if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
1429 $line =~ /do{/) {
1430 ERROR("need a space before the open brace '{'\n" . $herecurr);
1433 # closing brace should have a space following it when it has anything
1434 # on the line
1435 if ($line =~ /}(?!(?:,|;|\)))\S/) {
1436 ERROR("need a space after that close brace '}'\n" . $herecurr);
1439 # check spacing on square brackets
1440 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
1441 ERROR("no space after that open square bracket '['\n" . $herecurr);
1443 if ($line =~ /\s\]/) {
1444 ERROR("no space before that close square bracket ']'\n" . $herecurr);
1447 # check spacing on paretheses
1448 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
1449 $line !~ /for\s*\(\s+;/) {
1450 ERROR("no space after that open parenthesis '('\n" . $herecurr);
1452 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
1453 $line !~ /for\s*\(.*;\s+\)/) {
1454 ERROR("no space before that close parenthesis ')'\n" . $herecurr);
1457 #goto labels aren't indented, allow a single space however
1458 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
1459 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
1460 WARN("labels should not be indented\n" . $herecurr);
1463 # Need a space before open parenthesis after if, while etc
1464 if ($line=~/\b(if|while|for|switch)\(/) {
1465 ERROR("need a space before the open parenthesis '('\n" . $herecurr);
1468 # Check for illegal assignment in if conditional.
1469 if ($line =~ /\bif\s*\(/) {
1470 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1472 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) {
1473 ERROR("do not use assignment in if condition\n" . $herecurr);
1476 # Find out what is on the end of the line after the
1477 # conditional.
1478 substr($s, 0, length($c)) = '';
1479 $s =~ s/\n.*//g;
1480 $s =~ s/$;//g; # Remove any comments
1481 if (length($c) && $s !~ /^\s*({|;|)\s*\\*\s*$/) {
1482 ERROR("trailing statements should be on next line\n" . $herecurr);
1486 # Check for bitwise tests written as boolean
1487 if ($line =~ /
1489 (?:\[|\(|\&\&|\|\|)
1490 \s*0[xX][0-9]+\s*
1491 (?:\&\&|\|\|)
1493 (?:\&\&|\|\|)
1494 \s*0[xX][0-9]+\s*
1495 (?:\&\&|\|\||\)|\])
1496 )/x)
1498 WARN("boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
1501 # if and else should not have general statements after it
1502 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
1503 my $s = $1;
1504 $s =~ s/$;//g; # Remove any comments
1505 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
1506 ERROR("trailing statements should be on next line\n" . $herecurr);
1510 # Check for }<nl>else {, these must be at the same
1511 # indent level to be relevant to each other.
1512 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ and
1513 $previndent == $indent) {
1514 ERROR("else should follow close brace '}'\n" . $hereprev);
1517 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and
1518 $previndent == $indent) {
1519 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
1521 # Find out what is on the end of the line after the
1522 # conditional.
1523 substr($s, 0, length($c)) = '';
1524 $s =~ s/\n.*//g;
1526 if ($s =~ /^\s*;/) {
1527 ERROR("while should follow close brace '}'\n" . $hereprev);
1531 #studly caps, commented out until figure out how to distinguish between use of existing and adding new
1532 # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) {
1533 # print "No studly caps, use _\n";
1534 # print "$herecurr";
1535 # $clean = 0;
1538 #no spaces allowed after \ in define
1539 if ($line=~/\#define.*\\\s$/) {
1540 WARN("Whitepspace after \\ makes next lines useless\n" . $herecurr);
1543 #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line)
1544 if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) {
1545 my $checkfile = "$root/include/linux/$1.h";
1546 if (-f $checkfile && $1 ne 'irq.h') {
1547 CHK("Use #include <linux/$1.h> instead of <asm/$1.h>\n" .
1548 $herecurr);
1552 # multi-statement macros should be enclosed in a do while loop, grab the
1553 # first statement and ensure its the whole macro if its not enclosed
1554 # in a known goot container
1555 if ($prevline =~ /\#define.*\\/ &&
1556 $prevline !~/(?:do\s+{|\(\{|\{)/ &&
1557 $line !~ /(?:do\s+{|\(\{|\{)/ &&
1558 $line !~ /^.\s*$Declare\s/) {
1559 # Grab the first statement, if that is the entire macro
1560 # its ok. This may start either on the #define line
1561 # or the one below.
1562 my $ln = $linenr;
1563 my $cnt = $realcnt;
1564 my $off = 0;
1566 # If the macro starts on the define line start
1567 # grabbing the statement after the identifier
1568 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
1569 ##print "1<$1> 2<$2>\n";
1570 if (defined $2 && $2 ne '') {
1571 $off = length($1);
1572 $ln--;
1573 $cnt++;
1574 while ($lines[$ln - 1] =~ /^-/) {
1575 $ln--;
1576 $cnt++;
1579 my @ctx = ctx_statement($ln, $cnt, $off);
1580 my $ctx_ln = $ln + $#ctx + 1;
1581 my $ctx = join("\n", @ctx);
1583 # Pull in any empty extension lines.
1584 while ($ctx =~ /\\$/ &&
1585 $lines[$ctx_ln - 1] =~ /^.\s*(?:\\)?$/) {
1586 $ctx .= $lines[$ctx_ln - 1];
1587 $ctx_ln++;
1590 if ($ctx =~ /\\$/) {
1591 if ($ctx =~ /;/) {
1592 ERROR("Macros with multiple statements should be enclosed in a do - while loop\n" . "$here\n$ctx\n");
1593 } else {
1594 ERROR("Macros with complex values should be enclosed in parenthesis\n" . "$here\n$ctx\n");
1599 # check for redundant bracing round if etc
1600 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
1601 my ($level, $endln, @chunks) =
1602 ctx_statement_full($linenr, $realcnt, 0);
1603 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
1604 if ($#chunks > 1 && $level == 0) {
1605 my $allowed = 0;
1606 my $seen = 0;
1607 for my $chunk (@chunks) {
1608 my ($cond, $block) = @{$chunk};
1610 substr($block, 0, length($cond)) = '';
1612 $seen++ if ($block =~ /^\s*{/);
1614 $block =~ s/(^|\n)./$1/g;
1615 $block =~ s/^\s*{//;
1616 $block =~ s/}\s*$//;
1617 $block =~ s/^\s*//;
1618 $block =~ s/\s*$//;
1620 my @lines = ($block =~ /\n/g);
1621 my @statements = ($block =~ /;/g);
1623 #print "cond<$cond> block<$block> lines<" . scalar(@lines) . "> statements<" . scalar(@statements) . "> seen<$seen> allowed<$allowed>\n";
1624 if (scalar(@lines) != 0) {
1625 $allowed = 1;
1627 if ($block =~/\b(?:if|for|while)\b/) {
1628 $allowed = 1;
1630 if (scalar(@statements) > 1) {
1631 $allowed = 1;
1634 if ($seen && !$allowed) {
1635 WARN("braces {} are not necessary for any arm of this statement\n" . $herecurr);
1636 $suppress_ifbraces = $endln;
1640 if ($linenr > $suppress_ifbraces &&
1641 $line =~ /\b(if|while|for|else)\b/) {
1642 # Locate the end of the opening statement.
1643 my @control = ctx_statement($linenr, $realcnt, 0);
1644 my $nr = $linenr + (scalar(@control) - 1);
1645 my $cnt = $realcnt - (scalar(@control) - 1);
1647 my $off = $realcnt - $cnt;
1648 #print "$off: line<$line>end<" . $lines[$nr - 1] . ">\n";
1650 # If this is is a braced statement group check it
1651 if ($lines[$nr - 1] =~ /{\s*$/) {
1652 my ($lvl, @block) = ctx_block_level($nr, $cnt);
1654 my $stmt = join("\n", @block);
1655 # Drop the diff line leader.
1656 $stmt =~ s/\n./\n/g;
1657 # Drop the code outside the block.
1658 $stmt =~ s/(^[^{]*){\s*//;
1659 my $before = $1;
1660 $stmt =~ s/\s*}([^}]*$)//;
1661 my $after = $1;
1663 #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
1664 #print "before<$before> stmt<$stmt> after<$after>\n\n";
1666 # Count the newlines, if there is only one
1667 # then the block should not have {}'s.
1668 my @lines = ($stmt =~ /\n/g);
1669 my @statements = ($stmt =~ /;/g);
1670 #print "lines<" . scalar(@lines) . ">\n";
1671 #print "statements<" . scalar(@statements) . ">\n";
1672 if ($lvl == 0 && scalar(@lines) == 0 &&
1673 scalar(@statements) < 2 &&
1674 $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
1675 $before !~ /}/ && $after !~ /{/) {
1676 my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
1677 shift(@block);
1678 WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
1683 # don't include deprecated include files (uses RAW line)
1684 for my $inc (@dep_includes) {
1685 if ($rawline =~ m@\#\s*include\s*\<$inc>@) {
1686 ERROR("Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1690 # don't use deprecated functions
1691 for my $func (@dep_functions) {
1692 if ($line =~ /\b$func\b/) {
1693 ERROR("Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
1697 # no volatiles please
1698 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
1699 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
1700 WARN("Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
1703 # SPIN_LOCK_UNLOCKED & RW_LOCK_UNLOCKED are deprecated
1704 if ($line =~ /\b(SPIN_LOCK_UNLOCKED|RW_LOCK_UNLOCKED)/) {
1705 ERROR("Use of $1 is deprecated: see Documentation/spinlocks.txt\n" . $herecurr);
1708 # warn about #if 0
1709 if ($line =~ /^.#\s*if\s+0\b/) {
1710 CHK("if this code is redundant consider removing it\n" .
1711 $herecurr);
1714 # check for needless kfree() checks
1715 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
1716 my $expr = $1;
1717 if ($line =~ /\bkfree\(\Q$expr\E\);/) {
1718 WARN("kfree(NULL) is safe this check is probabally not required\n" . $hereprev);
1722 # warn about #ifdefs in C files
1723 # if ($line =~ /^.#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
1724 # print "#ifdef in C files should be avoided\n";
1725 # print "$herecurr";
1726 # $clean = 0;
1729 # warn about spacing in #ifdefs
1730 if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
1731 ERROR("exactly one space required after that #$1\n" . $herecurr);
1734 # check for spinlock_t definitions without a comment.
1735 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1736 my $which = $1;
1737 if (!ctx_has_comment($first_line, $linenr)) {
1738 CHK("$1 definition without comment\n" . $herecurr);
1741 # check for memory barriers without a comment.
1742 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
1743 if (!ctx_has_comment($first_line, $linenr)) {
1744 CHK("memory barrier without comment\n" . $herecurr);
1747 # check of hardware specific defines
1748 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1749 CHK("architecture specific defines should be avoided\n" . $herecurr);
1752 # check the location of the inline attribute, that it is between
1753 # storage class and type.
1754 if ($line =~ /\b$Type\s+$Inline\b/ ||
1755 $line =~ /\b$Inline\s+$Storage\b/) {
1756 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1759 # Check for __inline__ and __inline, prefer inline
1760 if ($line =~ /\b(__inline__|__inline)\b/) {
1761 WARN("plain inline is preferred over $1\n" . $herecurr);
1764 # check for new externs in .c files.
1765 if ($line =~ /^.\s*extern\s/ && ($realfile =~ /\.c$/)) {
1766 WARN("externs should be avoided in .c files\n" . $herecurr);
1769 # checks for new __setup's
1770 if ($rawline =~ /\b__setup\("([^"]*)"/) {
1771 my $name = $1;
1773 if (!grep(/$name/, @setup_docs)) {
1774 CHK("__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
1778 # check for pointless casting of kmalloc return
1779 if ($line =~ /\*\s*\)\s*k[czm]alloc\b/) {
1780 WARN("unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
1783 # check for gcc specific __FUNCTION__
1784 if ($line =~ /__FUNCTION__/) {
1785 WARN("__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr);
1789 # If we have no input at all, then there is nothing to report on
1790 # so just keep quiet.
1791 if ($#rawlines == -1) {
1792 exit(0);
1795 # In mailback mode only produce a report in the negative, for
1796 # things that appear to be patches.
1797 if ($mailback && ($clean == 1 || !$is_patch)) {
1798 exit(0);
1801 # This is not a patch, and we are are in 'no-patch' mode so
1802 # just keep quiet.
1803 if (!$chk_patch && !$is_patch) {
1804 exit(0);
1807 if (!$is_patch) {
1808 ERROR("Does not appear to be a unified-diff format patch\n");
1810 if ($is_patch && $chk_signoff && $signoff == 0) {
1811 ERROR("Missing Signed-off-by: line(s)\n");
1814 print report_dump();
1815 if ($summary && !($clean == 1 && $quiet == 1)) {
1816 print "$filename " if ($summary_file);
1817 print "total: $cnt_error errors, $cnt_warn warnings, " .
1818 (($check)? "$cnt_chk checks, " : "") .
1819 "$cnt_lines lines checked\n";
1820 print "\n" if ($quiet == 0);
1823 if ($clean == 1 && $quiet == 0) {
1824 print "$vname has no obvious style problems and is ready for submission.\n"
1826 if ($clean == 0 && $quiet == 0) {
1827 print "$vname has style problems, please review. If any of these errors\n";
1828 print "are false positives report them to the maintainer, see\n";
1829 print "CHECKPATCH in MAINTAINERS.\n";
1831 print <<EOL if ($file == 1 && $quiet == 0);
1833 WARNING: Using --file mode. Please do not send patches to linux-kernel
1834 that change whole existing files if you did not significantly change most
1835 of the the file for other reasons anyways or just wrote the file newly
1836 from scratch. Pure code style patches have a significant cost in a
1837 quickly changing code base like Linux because they cause rejects
1838 with other changes.
1841 return $clean;