diff options
-rwxr-xr-x | scripts/checkpatch.pl | 294 |
1 files changed, 198 insertions, 96 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 579f50fa838c..545471a99eea 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -9,7 +9,7 @@ use strict; | |||
9 | my $P = $0; | 9 | my $P = $0; |
10 | $P =~ s@.*/@@g; | 10 | $P =~ s@.*/@@g; |
11 | 11 | ||
12 | my $V = '0.12'; | 12 | my $V = '0.13'; |
13 | 13 | ||
14 | use Getopt::Long qw(:config no_auto_abbrev); | 14 | use Getopt::Long qw(:config no_auto_abbrev); |
15 | 15 | ||
@@ -25,6 +25,7 @@ my $check = 0; | |||
25 | my $summary = 1; | 25 | my $summary = 1; |
26 | my $mailback = 0; | 26 | my $mailback = 0; |
27 | my $root; | 27 | my $root; |
28 | my %debug; | ||
28 | GetOptions( | 29 | GetOptions( |
29 | 'q|quiet+' => \$quiet, | 30 | 'q|quiet+' => \$quiet, |
30 | 'tree!' => \$tree, | 31 | 'tree!' => \$tree, |
@@ -39,6 +40,7 @@ GetOptions( | |||
39 | 'root=s' => \$root, | 40 | 'root=s' => \$root, |
40 | 'summary!' => \$summary, | 41 | 'summary!' => \$summary, |
41 | 'mailback!' => \$mailback, | 42 | 'mailback!' => \$mailback, |
43 | 'debug=s' => \%debug, | ||
42 | ) or exit; | 44 | ) or exit; |
43 | 45 | ||
44 | my $exit = 0; | 46 | my $exit = 0; |
@@ -56,6 +58,12 @@ if ($#ARGV < 0) { | |||
56 | exit(1); | 58 | exit(1); |
57 | } | 59 | } |
58 | 60 | ||
61 | my $dbg_values = 0; | ||
62 | my $dbg_possible = 0; | ||
63 | for my $key (keys %debug) { | ||
64 | eval "\${dbg_$key} = '$debug{$key}';" | ||
65 | } | ||
66 | |||
59 | if ($terse) { | 67 | if ($terse) { |
60 | $emacs = 1; | 68 | $emacs = 1; |
61 | $quiet++; | 69 | $quiet++; |
@@ -110,7 +118,7 @@ our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; | |||
110 | our $Operators = qr{ | 118 | our $Operators = qr{ |
111 | <=|>=|==|!=| | 119 | <=|>=|==|!=| |
112 | =>|->|<<|>>|<|>|!|~| | 120 | =>|->|<<|>>|<|>|!|~| |
113 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/ | 121 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% |
114 | }x; | 122 | }x; |
115 | 123 | ||
116 | our $NonptrType; | 124 | our $NonptrType; |
@@ -152,7 +160,7 @@ sub build_types { | |||
152 | $Type = qr{ | 160 | $Type = qr{ |
153 | \b$NonptrType\b | 161 | \b$NonptrType\b |
154 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? | 162 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? |
155 | (?:\s+$Sparse|\s+$Attribute)* | 163 | (?:\s+$Inline|\s+$Sparse|\s+$Attribute)* |
156 | }x; | 164 | }x; |
157 | $Declare = qr{(?:$Storage\s+)?$Type}; | 165 | $Declare = qr{(?:$Storage\s+)?$Type}; |
158 | } | 166 | } |
@@ -181,6 +189,8 @@ if ($tree && -f "$root/$removal") { | |||
181 | } | 189 | } |
182 | 190 | ||
183 | my @rawlines = (); | 191 | my @rawlines = (); |
192 | my @lines = (); | ||
193 | my $vname; | ||
184 | for my $filename (@ARGV) { | 194 | for my $filename (@ARGV) { |
185 | if ($file) { | 195 | if ($file) { |
186 | open(FILE, "diff -u /dev/null $filename|") || | 196 | open(FILE, "diff -u /dev/null $filename|") || |
@@ -189,12 +199,17 @@ for my $filename (@ARGV) { | |||
189 | open(FILE, "<$filename") || | 199 | open(FILE, "<$filename") || |
190 | die "$P: $filename: open failed - $!\n"; | 200 | die "$P: $filename: open failed - $!\n"; |
191 | } | 201 | } |
202 | if ($filename eq '-') { | ||
203 | $vname = 'Your patch'; | ||
204 | } else { | ||
205 | $vname = $filename; | ||
206 | } | ||
192 | while (<FILE>) { | 207 | while (<FILE>) { |
193 | chomp; | 208 | chomp; |
194 | push(@rawlines, $_); | 209 | push(@rawlines, $_); |
195 | } | 210 | } |
196 | close(FILE); | 211 | close(FILE); |
197 | if (!process($filename, @rawlines)) { | 212 | if (!process($filename)) { |
198 | $exit = 1; | 213 | $exit = 1; |
199 | } | 214 | } |
200 | @rawlines = (); | 215 | @rawlines = (); |
@@ -274,20 +289,30 @@ sub sanitise_line { | |||
274 | my $l = ''; | 289 | my $l = ''; |
275 | 290 | ||
276 | my $quote = ''; | 291 | my $quote = ''; |
292 | my $qlen = 0; | ||
277 | 293 | ||
278 | foreach my $c (split(//, $line)) { | 294 | foreach my $c (split(//, $line)) { |
295 | # The second backslash of a pair is not a "quote". | ||
296 | if ($l eq "\\" && $c eq "\\") { | ||
297 | $c = 'X'; | ||
298 | } | ||
279 | if ($l ne "\\" && ($c eq "'" || $c eq '"')) { | 299 | if ($l ne "\\" && ($c eq "'" || $c eq '"')) { |
280 | if ($quote eq '') { | 300 | if ($quote eq '') { |
281 | $quote = $c; | 301 | $quote = $c; |
282 | $res .= $c; | 302 | $res .= $c; |
283 | $l = $c; | 303 | $l = $c; |
304 | $qlen = 0; | ||
284 | next; | 305 | next; |
285 | } elsif ($quote eq $c) { | 306 | } elsif ($quote eq $c) { |
286 | $quote = ''; | 307 | $quote = ''; |
287 | } | 308 | } |
288 | } | 309 | } |
310 | if ($quote eq "'" && $qlen > 1) { | ||
311 | $quote = ''; | ||
312 | } | ||
289 | if ($quote && $c ne "\t") { | 313 | if ($quote && $c ne "\t") { |
290 | $res .= "X"; | 314 | $res .= "X"; |
315 | $qlen++; | ||
291 | } else { | 316 | } else { |
292 | $res .= $c; | 317 | $res .= $c; |
293 | } | 318 | } |
@@ -295,6 +320,28 @@ sub sanitise_line { | |||
295 | $l = $c; | 320 | $l = $c; |
296 | } | 321 | } |
297 | 322 | ||
323 | # Clear out the comments. | ||
324 | while ($res =~ m@(/\*.*?\*/)@) { | ||
325 | substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); | ||
326 | } | ||
327 | if ($res =~ m@(/\*.*)@) { | ||
328 | substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); | ||
329 | } | ||
330 | if ($res =~ m@^.(.*\*/)@) { | ||
331 | substr($res, $-[1], $+[1] - $-[1]) = ' ' x ($+[1] - $-[1]); | ||
332 | } | ||
333 | |||
334 | # The pathname on a #include may be surrounded by '<' and '>'. | ||
335 | if ($res =~ /^.#\s*include\s+\<(.*)\>/) { | ||
336 | my $clean = 'X' x length($1); | ||
337 | $res =~ s@\<.*\>@<$clean>@; | ||
338 | |||
339 | # The whole of a #error is a string. | ||
340 | } elsif ($res =~ /^.#\s*(?:error|warning)\s+(.*)\b/) { | ||
341 | my $clean = 'X' x length($1); | ||
342 | $res =~ s@(#\s*(?:error|warning)\s+).*@$1$clean@; | ||
343 | } | ||
344 | |||
298 | return $res; | 345 | return $res; |
299 | } | 346 | } |
300 | 347 | ||
@@ -315,9 +362,9 @@ sub ctx_statement_block { | |||
315 | # context. | 362 | # context. |
316 | if ($off >= $len) { | 363 | if ($off >= $len) { |
317 | for (; $remain > 0; $line++) { | 364 | for (; $remain > 0; $line++) { |
318 | next if ($rawlines[$line] =~ /^-/); | 365 | next if ($lines[$line] =~ /^-/); |
319 | $remain--; | 366 | $remain--; |
320 | $blk .= sanitise_line($rawlines[$line]) . "\n"; | 367 | $blk .= $lines[$line] . "\n"; |
321 | $len = length($blk); | 368 | $len = length($blk); |
322 | $line++; | 369 | $line++; |
323 | last; | 370 | last; |
@@ -500,103 +547,106 @@ sub cat_vet { | |||
500 | return $res; | 547 | return $res; |
501 | } | 548 | } |
502 | 549 | ||
550 | my $av_preprocessor = 0; | ||
551 | my $av_paren = 0; | ||
552 | my @av_paren_type; | ||
553 | |||
554 | sub annotate_reset { | ||
555 | $av_preprocessor = 0; | ||
556 | $av_paren = 0; | ||
557 | @av_paren_type = (); | ||
558 | } | ||
559 | |||
503 | sub annotate_values { | 560 | sub annotate_values { |
504 | my ($stream, $type) = @_; | 561 | my ($stream, $type) = @_; |
505 | 562 | ||
506 | my $res; | 563 | my $res; |
507 | my $cur = $stream; | 564 | my $cur = $stream; |
508 | 565 | ||
509 | my $debug = 0; | 566 | print "$stream\n" if ($dbg_values > 1); |
510 | |||
511 | print "$stream\n" if ($debug); | ||
512 | |||
513 | ##my $type = 'N'; | ||
514 | my $pos = 0; | ||
515 | my $preprocessor = 0; | ||
516 | my $paren = 0; | ||
517 | my @paren_type; | ||
518 | 567 | ||
519 | while (length($cur)) { | 568 | while (length($cur)) { |
520 | print " <$type> " if ($debug); | 569 | print " <$type> " if ($dbg_values > 1); |
521 | if ($cur =~ /^(\s+)/o) { | 570 | if ($cur =~ /^(\s+)/o) { |
522 | print "WS($1)\n" if ($debug); | 571 | print "WS($1)\n" if ($dbg_values > 1); |
523 | if ($1 =~ /\n/ && $preprocessor) { | 572 | if ($1 =~ /\n/ && $av_preprocessor) { |
524 | $preprocessor = 0; | 573 | $av_preprocessor = 0; |
525 | $type = 'N'; | 574 | $type = 'N'; |
526 | } | 575 | } |
527 | 576 | ||
528 | } elsif ($cur =~ /^($Type)/) { | 577 | } elsif ($cur =~ /^($Type)/) { |
529 | print "DECLARE($1)\n" if ($debug); | 578 | print "DECLARE($1)\n" if ($dbg_values > 1); |
530 | $type = 'T'; | 579 | $type = 'T'; |
531 | 580 | ||
532 | } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) { | 581 | } elsif ($cur =~ /^(#\s*define\s*$Ident)(\(?)/o) { |
533 | print "DEFINE($1)\n" if ($debug); | 582 | print "DEFINE($1)\n" if ($dbg_values > 1); |
534 | $preprocessor = 1; | 583 | $av_preprocessor = 1; |
535 | $paren_type[$paren] = 'N'; | 584 | $av_paren_type[$av_paren] = 'N'; |
536 | 585 | ||
537 | } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|endif))/o) { | 586 | } elsif ($cur =~ /^(#\s*(?:ifdef|ifndef|if|else|elif|endif))/o) { |
538 | print "PRE($1)\n" if ($debug); | 587 | print "PRE($1)\n" if ($dbg_values > 1); |
539 | $preprocessor = 1; | 588 | $av_preprocessor = 1; |
540 | $type = 'N'; | 589 | $type = 'N'; |
541 | 590 | ||
542 | } elsif ($cur =~ /^(\\\n)/o) { | 591 | } elsif ($cur =~ /^(\\\n)/o) { |
543 | print "PRECONT($1)\n" if ($debug); | 592 | print "PRECONT($1)\n" if ($dbg_values > 1); |
544 | 593 | ||
545 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { | 594 | } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { |
546 | print "SIZEOF($1)\n" if ($debug); | 595 | print "SIZEOF($1)\n" if ($dbg_values > 1); |
547 | if (defined $2) { | 596 | if (defined $2) { |
548 | $paren_type[$paren] = 'V'; | 597 | $av_paren_type[$av_paren] = 'V'; |
549 | } | 598 | } |
550 | $type = 'N'; | 599 | $type = 'N'; |
551 | 600 | ||
552 | } elsif ($cur =~ /^(if|while|typeof|for)\b/o) { | 601 | } elsif ($cur =~ /^(if|while|typeof|for)\b/o) { |
553 | print "COND($1)\n" if ($debug); | 602 | print "COND($1)\n" if ($dbg_values > 1); |
554 | $paren_type[$paren] = 'N'; | 603 | $av_paren_type[$av_paren] = 'N'; |
555 | $type = 'N'; | 604 | $type = 'N'; |
556 | 605 | ||
557 | } elsif ($cur =~/^(return|case|else)/o) { | 606 | } elsif ($cur =~/^(return|case|else)/o) { |
558 | print "KEYWORD($1)\n" if ($debug); | 607 | print "KEYWORD($1)\n" if ($dbg_values > 1); |
559 | $type = 'N'; | 608 | $type = 'N'; |
560 | 609 | ||
561 | } elsif ($cur =~ /^(\()/o) { | 610 | } elsif ($cur =~ /^(\()/o) { |
562 | print "PAREN('$1')\n" if ($debug); | 611 | print "PAREN('$1')\n" if ($dbg_values > 1); |
563 | $paren++; | 612 | $av_paren++; |
564 | $type = 'N'; | 613 | $type = 'N'; |
565 | 614 | ||
566 | } elsif ($cur =~ /^(\))/o) { | 615 | } elsif ($cur =~ /^(\))/o) { |
567 | $paren-- if ($paren > 0); | 616 | $av_paren-- if ($av_paren > 0); |
568 | if (defined $paren_type[$paren]) { | 617 | if (defined $av_paren_type[$av_paren]) { |
569 | $type = $paren_type[$paren]; | 618 | $type = $av_paren_type[$av_paren]; |
570 | undef $paren_type[$paren]; | 619 | undef $av_paren_type[$av_paren]; |
571 | print "PAREN('$1') -> $type\n" if ($debug); | 620 | print "PAREN('$1') -> $type\n" |
621 | if ($dbg_values > 1); | ||
572 | } else { | 622 | } else { |
573 | print "PAREN('$1')\n" if ($debug); | 623 | print "PAREN('$1')\n" if ($dbg_values > 1); |
574 | } | 624 | } |
575 | 625 | ||
576 | } elsif ($cur =~ /^($Ident)\(/o) { | 626 | } elsif ($cur =~ /^($Ident)\(/o) { |
577 | print "FUNC($1)\n" if ($debug); | 627 | print "FUNC($1)\n" if ($dbg_values > 1); |
578 | $paren_type[$paren] = 'V'; | 628 | $av_paren_type[$av_paren] = 'V'; |
579 | 629 | ||
580 | } elsif ($cur =~ /^($Ident|$Constant)/o) { | 630 | } elsif ($cur =~ /^($Ident|$Constant)/o) { |
581 | print "IDENT($1)\n" if ($debug); | 631 | print "IDENT($1)\n" if ($dbg_values > 1); |
582 | $type = 'V'; | 632 | $type = 'V'; |
583 | 633 | ||
584 | } elsif ($cur =~ /^($Assignment)/o) { | 634 | } elsif ($cur =~ /^($Assignment)/o) { |
585 | print "ASSIGN($1)\n" if ($debug); | 635 | print "ASSIGN($1)\n" if ($dbg_values > 1); |
586 | $type = 'N'; | 636 | $type = 'N'; |
587 | 637 | ||
588 | } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) { | 638 | } elsif ($cur =~ /^(;|{|}|\?|:|\[)/o) { |
589 | print "END($1)\n" if ($debug); | 639 | print "END($1)\n" if ($dbg_values > 1); |
590 | $type = 'N'; | 640 | $type = 'N'; |
591 | 641 | ||
592 | } elsif ($cur =~ /^($Operators)/o) { | 642 | } elsif ($cur =~ /^($Operators)/o) { |
593 | print "OP($1)\n" if ($debug); | 643 | print "OP($1)\n" if ($dbg_values > 1); |
594 | if ($1 ne '++' && $1 ne '--') { | 644 | if ($1 ne '++' && $1 ne '--') { |
595 | $type = 'N'; | 645 | $type = 'N'; |
596 | } | 646 | } |
597 | 647 | ||
598 | } elsif ($cur =~ /(^.)/o) { | 648 | } elsif ($cur =~ /(^.)/o) { |
599 | print "C($1)\n" if ($debug); | 649 | print "C($1)\n" if ($dbg_values > 1); |
600 | } | 650 | } |
601 | if (defined $1) { | 651 | if (defined $1) { |
602 | $cur = substr($cur, length($1)); | 652 | $cur = substr($cur, length($1)); |
@@ -616,7 +666,7 @@ sub possible { | |||
616 | $possible ne 'struct' && $possible ne 'enum' && | 666 | $possible ne 'struct' && $possible ne 'enum' && |
617 | $possible ne 'case' && $possible ne 'else' && | 667 | $possible ne 'case' && $possible ne 'else' && |
618 | $possible ne 'typedef') { | 668 | $possible ne 'typedef') { |
619 | #print "POSSIBLE<$possible>\n"; | 669 | warn "POSSIBLE: $possible\n" if ($dbg_possible); |
620 | push(@typeList, $possible); | 670 | push(@typeList, $possible); |
621 | build_types(); | 671 | build_types(); |
622 | } | 672 | } |
@@ -655,11 +705,12 @@ sub CHK { | |||
655 | 705 | ||
656 | sub process { | 706 | sub process { |
657 | my $filename = shift; | 707 | my $filename = shift; |
658 | my @lines = @_; | ||
659 | 708 | ||
660 | my $linenr=0; | 709 | my $linenr=0; |
661 | my $prevline=""; | 710 | my $prevline=""; |
711 | my $prevrawline=""; | ||
662 | my $stashline=""; | 712 | my $stashline=""; |
713 | my $stashrawline=""; | ||
663 | 714 | ||
664 | my $length; | 715 | my $length; |
665 | my $indent; | 716 | my $indent; |
@@ -681,14 +732,26 @@ sub process { | |||
681 | my $realcnt = 0; | 732 | my $realcnt = 0; |
682 | my $here = ''; | 733 | my $here = ''; |
683 | my $in_comment = 0; | 734 | my $in_comment = 0; |
735 | my $comment_edge = 0; | ||
684 | my $first_line = 0; | 736 | my $first_line = 0; |
685 | 737 | ||
686 | my $prev_values = 'N'; | 738 | my $prev_values = 'N'; |
687 | 739 | ||
740 | # Pre-scan the patch sanitizing the lines. | ||
688 | # Pre-scan the patch looking for any __setup documentation. | 741 | # Pre-scan the patch looking for any __setup documentation. |
742 | # | ||
689 | my @setup_docs = (); | 743 | my @setup_docs = (); |
690 | my $setup_docs = 0; | 744 | my $setup_docs = 0; |
691 | foreach my $line (@lines) { | 745 | my $line; |
746 | foreach my $rawline (@rawlines) { | ||
747 | # Standardise the strings and chars within the input to | ||
748 | # simplify matching. | ||
749 | $line = sanitise_line($rawline); | ||
750 | push(@lines, $line); | ||
751 | |||
752 | ##print "==>$rawline\n"; | ||
753 | ##print "-->$line\n"; | ||
754 | |||
692 | if ($line=~/^\+\+\+\s+(\S+)/) { | 755 | if ($line=~/^\+\+\+\s+(\S+)/) { |
693 | $setup_docs = 0; | 756 | $setup_docs = 0; |
694 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { | 757 | if ($1 =~ m@Documentation/kernel-parameters.txt$@) { |
@@ -707,8 +770,7 @@ sub process { | |||
707 | foreach my $line (@lines) { | 770 | foreach my $line (@lines) { |
708 | $linenr++; | 771 | $linenr++; |
709 | 772 | ||
710 | my $rawline = $line; | 773 | my $rawline = $rawlines[$linenr - 1]; |
711 | |||
712 | 774 | ||
713 | #extract the filename as it passes | 775 | #extract the filename as it passes |
714 | if ($line=~/^\+\+\+\s+(\S+)/) { | 776 | if ($line=~/^\+\+\+\s+(\S+)/) { |
@@ -728,6 +790,7 @@ sub process { | |||
728 | } else { | 790 | } else { |
729 | $realcnt=1+1; | 791 | $realcnt=1+1; |
730 | } | 792 | } |
793 | annotate_reset(); | ||
731 | $prev_values = 'N'; | 794 | $prev_values = 'N'; |
732 | next; | 795 | next; |
733 | } | 796 | } |
@@ -746,7 +809,7 @@ sub process { | |||
746 | if ($linenr == $first_line) { | 809 | if ($linenr == $first_line) { |
747 | my $edge; | 810 | my $edge; |
748 | for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) { | 811 | for (my $ln = $first_line; $ln < ($linenr + $realcnt); $ln++) { |
749 | ($edge) = ($lines[$ln - 1] =~ m@(/\*|\*/)@); | 812 | ($edge) = ($rawlines[$ln - 1] =~ m@(/\*|\*/)@); |
750 | last if (defined $edge); | 813 | last if (defined $edge); |
751 | } | 814 | } |
752 | if (defined $edge && $edge eq '*/') { | 815 | if (defined $edge && $edge eq '*/') { |
@@ -757,25 +820,30 @@ sub process { | |||
757 | # Guestimate if this is a continuing comment. If this | 820 | # Guestimate if this is a continuing comment. If this |
758 | # is the start of a diff block and this line starts | 821 | # is the start of a diff block and this line starts |
759 | # ' *' then it is very likely a comment. | 822 | # ' *' then it is very likely a comment. |
760 | if ($linenr == $first_line and $line =~ m@^.\s*\*@) { | 823 | if ($linenr == $first_line and $rawline =~ m@^.\s* \*(?:\s|$)@) { |
761 | $in_comment = 1; | 824 | $in_comment = 1; |
762 | } | 825 | } |
763 | 826 | ||
764 | # Find the last comment edge on _this_ line. | 827 | # Find the last comment edge on _this_ line. |
765 | while (($line =~ m@(/\*|\*/)@g)) { | 828 | $comment_edge = 0; |
829 | while (($rawline =~ m@(/\*|\*/)@g)) { | ||
766 | if ($1 eq '/*') { | 830 | if ($1 eq '/*') { |
767 | $in_comment = 1; | 831 | $in_comment = 1; |
768 | } else { | 832 | } else { |
769 | $in_comment = 0; | 833 | $in_comment = 0; |
770 | } | 834 | } |
835 | $comment_edge = 1; | ||
771 | } | 836 | } |
772 | 837 | ||
773 | # Measure the line length and indent. | 838 | # Measure the line length and indent. |
774 | ($length, $indent) = line_stats($line); | 839 | ($length, $indent) = line_stats($rawline); |
775 | 840 | ||
776 | # Track the previous line. | 841 | # Track the previous line. |
777 | ($prevline, $stashline) = ($stashline, $line); | 842 | ($prevline, $stashline) = ($stashline, $line); |
778 | ($previndent, $stashindent) = ($stashindent, $indent); | 843 | ($previndent, $stashindent) = ($stashindent, $indent); |
844 | ($prevrawline, $stashrawline) = ($stashrawline, $rawline); | ||
845 | |||
846 | #warn "ic<$in_comment> ce<$comment_edge> line<$line>\n"; | ||
779 | 847 | ||
780 | } elsif ($realcnt == 1) { | 848 | } elsif ($realcnt == 1) { |
781 | $realcnt--; | 849 | $realcnt--; |
@@ -786,9 +854,9 @@ sub process { | |||
786 | $here = "#$realline: " if ($file); | 854 | $here = "#$realline: " if ($file); |
787 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); | 855 | $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); |
788 | 856 | ||
789 | my $hereline = "$here\n$line\n"; | 857 | my $hereline = "$here\n$rawline\n"; |
790 | my $herecurr = "$here\n$line\n"; | 858 | my $herecurr = "$here\n$rawline\n"; |
791 | my $hereprev = "$here\n$prevline\n$line\n"; | 859 | my $hereprev = "$here\n$prevrawline\n$rawline\n"; |
792 | 860 | ||
793 | $prefix = "$filename:$realline: " if ($emacs && $file); | 861 | $prefix = "$filename:$realline: " if ($emacs && $file); |
794 | $prefix = "$filename:$linenr: " if ($emacs && !$file); | 862 | $prefix = "$filename:$linenr: " if ($emacs && !$file); |
@@ -816,7 +884,7 @@ sub process { | |||
816 | 884 | ||
817 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php | 885 | # UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php |
818 | if (($realfile =~ /^$/ || $line =~ /^\+/) && | 886 | if (($realfile =~ /^$/ || $line =~ /^\+/) && |
819 | !($line =~ m/^( | 887 | !($rawline =~ m/^( |
820 | [\x09\x0A\x0D\x20-\x7E] # ASCII | 888 | [\x09\x0A\x0D\x20-\x7E] # ASCII |
821 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | 889 | | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte |
822 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | 890 | | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs |
@@ -826,7 +894,7 @@ sub process { | |||
826 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | 894 | | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 |
827 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 | 895 | | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 |
828 | )*$/x )) { | 896 | )*$/x )) { |
829 | ERROR("Invalid UTF-8\n" . $herecurr); | 897 | ERROR("Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $herecurr); |
830 | } | 898 | } |
831 | 899 | ||
832 | #ignore lines being removed | 900 | #ignore lines being removed |
@@ -837,15 +905,15 @@ sub process { | |||
837 | 905 | ||
838 | #trailing whitespace | 906 | #trailing whitespace |
839 | if ($line =~ /^\+.*\015/) { | 907 | if ($line =~ /^\+.*\015/) { |
840 | my $herevet = "$here\n" . cat_vet($line) . "\n"; | 908 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
841 | ERROR("DOS line endings\n" . $herevet); | 909 | ERROR("DOS line endings\n" . $herevet); |
842 | 910 | ||
843 | } elsif ($line =~ /^\+.*\S\s+$/ || $line =~ /^\+\s+$/) { | 911 | } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { |
844 | my $herevet = "$here\n" . cat_vet($line) . "\n"; | 912 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
845 | ERROR("trailing whitespace\n" . $herevet); | 913 | ERROR("trailing whitespace\n" . $herevet); |
846 | } | 914 | } |
847 | #80 column limit | 915 | #80 column limit |
848 | if ($line =~ /^\+/ && !($prevline=~/\/\*\*/) && $length > 80) { | 916 | if ($line =~ /^\+/ && !($prevrawline=~/\/\*\*/) && $length > 80) { |
849 | WARN("line over 80 characters\n" . $herecurr); | 917 | WARN("line over 80 characters\n" . $herecurr); |
850 | } | 918 | } |
851 | 919 | ||
@@ -859,46 +927,48 @@ sub process { | |||
859 | 927 | ||
860 | # at the beginning of a line any tabs must come first and anything | 928 | # at the beginning of a line any tabs must come first and anything |
861 | # more than 8 must use tabs. | 929 | # more than 8 must use tabs. |
862 | if ($line=~/^\+\s* \t\s*\S/ or $line=~/^\+\s* \s*/) { | 930 | if ($rawline =~ /^\+\s* \t\s*\S/ || |
863 | my $herevet = "$here\n" . cat_vet($line) . "\n"; | 931 | $rawline =~ /^\+\s* \s*/) { |
932 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | ||
864 | ERROR("use tabs not spaces\n" . $herevet); | 933 | ERROR("use tabs not spaces\n" . $herevet); |
865 | } | 934 | } |
866 | 935 | ||
867 | # Remove comments from the line before processing. | 936 | # check for RCS/CVS revision markers |
868 | my $comment_edge = ($line =~ s@/\*.*\*/@@g) + | 937 | if ($rawline =~ /\$(Revision|Log|Id)(?:\$|)/) { |
869 | ($line =~ s@/\*.*@@) + | 938 | WARN("CVS style keyword markers, these will _not_ be updated\n". $herecurr); |
870 | ($line =~ s@^(.).*\*/@$1@); | 939 | } |
871 | 940 | ||
872 | # The rest of our checks refer specifically to C style | 941 | # The rest of our checks refer specifically to C style |
873 | # only apply those _outside_ comments. Only skip | 942 | # only apply those _outside_ comments. Only skip |
874 | # lines in the middle of comments. | 943 | # lines in the middle of comments. |
875 | next if (!$comment_edge && $in_comment); | 944 | next if (!$comment_edge && $in_comment); |
876 | 945 | ||
877 | # Standardise the strings and chars within the input to simplify matching. | ||
878 | $line = sanitise_line($line); | ||
879 | |||
880 | # Check for potential 'bare' types | 946 | # Check for potential 'bare' types |
881 | if ($realcnt && | 947 | if ($realcnt) { |
882 | $line !~ /$Ident:\s*$/ && | 948 | # Ignore goto labels. |
883 | ($line =~ /^.\s*$Ident\s*\(\*+\s*$Ident\)\s*\(/ || | 949 | if ($line =~ /$Ident:\*$/) { |
884 | $line !~ /^.\s*$Ident\s*\(/)) { | 950 | |
951 | # Ignore functions being called | ||
952 | } elsif ($line =~ /^.\s*$Ident\s*\(/) { | ||
953 | |||
885 | # definitions in global scope can only start with types | 954 | # definitions in global scope can only start with types |
886 | if ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) { | 955 | } elsif ($line =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b/) { |
887 | possible($1); | 956 | possible($1); |
888 | 957 | ||
889 | # declarations always start with types | 958 | # declarations always start with types |
890 | } elsif ($prev_values eq 'N' && $line =~ /^.\s*(?:$Storage\s+)?($Ident)\b\s*\**\s*$Ident\s*(?:;|=)/) { | 959 | } elsif ($prev_values eq 'N' && $line =~ /^.\s*(?:$Storage\s+)?(?:const\s+)?($Ident)\b(:?\s+$Sparse)?\s*\**\s*$Ident\s*(?:;|=)/) { |
891 | possible($1); | 960 | possible($1); |
961 | } | ||
892 | 962 | ||
893 | # any (foo ... *) is a pointer cast, and foo is a type | 963 | # any (foo ... *) is a pointer cast, and foo is a type |
894 | } elsif ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/) { | 964 | while ($line =~ /\(($Ident)(?:\s+$Sparse)*\s*\*+\s*\)/g) { |
895 | possible($1); | 965 | possible($1); |
896 | } | 966 | } |
897 | 967 | ||
898 | # Check for any sort of function declaration. | 968 | # Check for any sort of function declaration. |
899 | # int foo(something bar, other baz); | 969 | # int foo(something bar, other baz); |
900 | # void (*store_gdt)(x86_descr_ptr *); | 970 | # void (*store_gdt)(x86_descr_ptr *); |
901 | if ($prev_values eq 'N' && $line =~ /^(.(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) { | 971 | if ($prev_values eq 'N' && $line =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/) { |
902 | my ($name_len) = length($1); | 972 | my ($name_len) = length($1); |
903 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len); | 973 | my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, $name_len); |
904 | my $ctx = join("\n", @ctx); | 974 | my $ctx = join("\n", @ctx); |
@@ -974,8 +1044,11 @@ sub process { | |||
974 | my $opline = $line; $opline =~ s/^./ /; | 1044 | my $opline = $line; $opline =~ s/^./ /; |
975 | my $curr_values = annotate_values($opline . "\n", $prev_values); | 1045 | my $curr_values = annotate_values($opline . "\n", $prev_values); |
976 | $curr_values = $prev_values . $curr_values; | 1046 | $curr_values = $prev_values . $curr_values; |
977 | #warn "--> $opline\n"; | 1047 | if ($dbg_values) { |
978 | #warn "--> $curr_values ($prev_values)\n"; | 1048 | my $outline = $opline; $outline =~ s/\t/ /g; |
1049 | warn "--> .$outline\n"; | ||
1050 | warn "--> $curr_values\n"; | ||
1051 | } | ||
979 | $prev_values = substr($curr_values, -1); | 1052 | $prev_values = substr($curr_values, -1); |
980 | 1053 | ||
981 | #ignore lines not being added | 1054 | #ignore lines not being added |
@@ -1004,9 +1077,6 @@ sub process { | |||
1004 | ERROR("malformed #include filename\n" . | 1077 | ERROR("malformed #include filename\n" . |
1005 | $herecurr); | 1078 | $herecurr); |
1006 | } | 1079 | } |
1007 | # Sanitise this special form of string. | ||
1008 | $path = 'X' x length($path); | ||
1009 | $line =~ s{\<.*\>}{<$path>}; | ||
1010 | } | 1080 | } |
1011 | 1081 | ||
1012 | # no C99 // comments | 1082 | # no C99 // comments |
@@ -1074,7 +1144,7 @@ sub process { | |||
1074 | # } | 1144 | # } |
1075 | 1145 | ||
1076 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { | 1146 | if ($line =~ /\bLINUX_VERSION_CODE\b/) { |
1077 | WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged" . $herecurr); | 1147 | WARN("LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); |
1078 | } | 1148 | } |
1079 | 1149 | ||
1080 | # printk should use KERN_* levels. Note that follow on printk's on the | 1150 | # printk should use KERN_* levels. Note that follow on printk's on the |
@@ -1102,7 +1172,7 @@ sub process { | |||
1102 | 1172 | ||
1103 | # function brace can't be on same line, except for #defines of do while, | 1173 | # function brace can't be on same line, except for #defines of do while, |
1104 | # or if closed on same line | 1174 | # or if closed on same line |
1105 | if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).* {/) and | 1175 | if (($line=~/$Type\s*[A-Za-z\d_]+\(.*\).*\s{/) and |
1106 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { | 1176 | !($line=~/\#define.*do\s{/) and !($line=~/}/)) { |
1107 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); | 1177 | ERROR("open brace '{' following function declarations go on the next line\n" . $herecurr); |
1108 | } | 1178 | } |
@@ -1115,8 +1185,22 @@ sub process { | |||
1115 | 1185 | ||
1116 | # check for spaces between functions and their parentheses. | 1186 | # check for spaces between functions and their parentheses. |
1117 | while ($line =~ /($Ident)\s+\(/g) { | 1187 | while ($line =~ /($Ident)\s+\(/g) { |
1118 | if ($1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/ && | 1188 | my $name = $1; |
1119 | $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) { | 1189 | my $ctx = substr($line, 0, $-[1]); |
1190 | |||
1191 | # Ignore those directives where spaces _are_ permitted. | ||
1192 | if ($name =~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright|case)$/) { | ||
1193 | |||
1194 | # cpp #define statements have non-optional spaces, ie | ||
1195 | # if there is a space between the name and the open | ||
1196 | # parenthesis it is simply not a parameter group. | ||
1197 | } elsif ($ctx =~ /^.\#\s*define\s*$/) { | ||
1198 | |||
1199 | # If this whole things ends with a type its most | ||
1200 | # likely a typedef for a function. | ||
1201 | } elsif ("$ctx$name" =~ /$Type$/) { | ||
1202 | |||
1203 | } else { | ||
1120 | WARN("no space between function name and open parenthesis '('\n" . $herecurr); | 1204 | WARN("no space between function name and open parenthesis '('\n" . $herecurr); |
1121 | } | 1205 | } |
1122 | } | 1206 | } |
@@ -1126,7 +1210,7 @@ sub process { | |||
1126 | <<=|>>=|<=|>=|==|!=| | 1210 | <<=|>>=|<=|>=|==|!=| |
1127 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| | 1211 | \+=|-=|\*=|\/=|%=|\^=|\|=|&=| |
1128 | =>|->|<<|>>|<|>|=|!|~| | 1212 | =>|->|<<|>>|<|>|=|!|~| |
1129 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/ | 1213 | &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|% |
1130 | }x; | 1214 | }x; |
1131 | my @elements = split(/($ops|;)/, $opline); | 1215 | my @elements = split(/($ops|;)/, $opline); |
1132 | my $off = 0; | 1216 | my $off = 0; |
@@ -1239,7 +1323,8 @@ sub process { | |||
1239 | } elsif ($op eq '<<' or $op eq '>>' or | 1323 | } elsif ($op eq '<<' or $op eq '>>' or |
1240 | $op eq '&' or $op eq '^' or $op eq '|' or | 1324 | $op eq '&' or $op eq '^' or $op eq '|' or |
1241 | $op eq '+' or $op eq '-' or | 1325 | $op eq '+' or $op eq '-' or |
1242 | $op eq '*' or $op eq '/') | 1326 | $op eq '*' or $op eq '/' or |
1327 | $op eq '%') | ||
1243 | { | 1328 | { |
1244 | if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) { | 1329 | if ($ctx !~ /VxV|WxW|VxE|WxE|VxO/) { |
1245 | ERROR("need consistent spacing around '$op' $at\n" . | 1330 | ERROR("need consistent spacing around '$op' $at\n" . |
@@ -1324,7 +1409,7 @@ sub process { | |||
1324 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); | 1409 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); |
1325 | 1410 | ||
1326 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) { | 1411 | if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/) { |
1327 | ERROR("do not use assignment in if condition ($c)\n" . $herecurr); | 1412 | ERROR("do not use assignment in if condition\n" . $herecurr); |
1328 | } | 1413 | } |
1329 | 1414 | ||
1330 | # Find out what is on the end of the line after the | 1415 | # Find out what is on the end of the line after the |
@@ -1350,6 +1435,20 @@ sub process { | |||
1350 | ERROR("else should follow close brace '}'\n" . $hereprev); | 1435 | ERROR("else should follow close brace '}'\n" . $hereprev); |
1351 | } | 1436 | } |
1352 | 1437 | ||
1438 | if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ and | ||
1439 | $previndent == $indent) { | ||
1440 | my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); | ||
1441 | |||
1442 | # Find out what is on the end of the line after the | ||
1443 | # conditional. | ||
1444 | substr($s, 0, length($c)) = ''; | ||
1445 | $s =~ s/\n.*//g; | ||
1446 | |||
1447 | if ($s =~ /^\s*;/) { | ||
1448 | ERROR("while should follow close brace '}'\n" . $hereprev); | ||
1449 | } | ||
1450 | } | ||
1451 | |||
1353 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new | 1452 | #studly caps, commented out until figure out how to distinguish between use of existing and adding new |
1354 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { | 1453 | # if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { |
1355 | # print "No studly caps, use _\n"; | 1454 | # print "No studly caps, use _\n"; |
@@ -1447,8 +1546,11 @@ sub process { | |||
1447 | # Count the newlines, if there is only one | 1546 | # Count the newlines, if there is only one |
1448 | # then the block should not have {}'s. | 1547 | # then the block should not have {}'s. |
1449 | my @lines = ($stmt =~ /\n/g); | 1548 | my @lines = ($stmt =~ /\n/g); |
1549 | my @statements = ($stmt =~ /;/g); | ||
1450 | #print "lines<" . scalar(@lines) . ">\n"; | 1550 | #print "lines<" . scalar(@lines) . ">\n"; |
1551 | #print "statements<" . scalar(@statements) . ">\n"; | ||
1451 | if ($lvl == 0 && scalar(@lines) == 0 && | 1552 | if ($lvl == 0 && scalar(@lines) == 0 && |
1553 | scalar(@statements) < 2 && | ||
1452 | $stmt !~ /{/ && $stmt !~ /\bif\b/ && | 1554 | $stmt !~ /{/ && $stmt !~ /\bif\b/ && |
1453 | $before !~ /}/ && $after !~ /{/) { | 1555 | $before !~ /}/ && $after !~ /{/) { |
1454 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; | 1556 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; |
@@ -1587,10 +1689,10 @@ sub process { | |||
1587 | } | 1689 | } |
1588 | 1690 | ||
1589 | if ($clean == 1 && $quiet == 0) { | 1691 | if ($clean == 1 && $quiet == 0) { |
1590 | print "Your patch has no obvious style problems and is ready for submission.\n" | 1692 | print "$vname has no obvious style problems and is ready for submission.\n" |
1591 | } | 1693 | } |
1592 | if ($clean == 0 && $quiet == 0) { | 1694 | if ($clean == 0 && $quiet == 0) { |
1593 | print "Your patch has style problems, please review. If any of these errors\n"; | 1695 | print "$vname has style problems, please review. If any of these errors\n"; |
1594 | print "are false positives report them to the maintainer, see\n"; | 1696 | print "are false positives report them to the maintainer, see\n"; |
1595 | print "CHECKPATCH in MAINTAINERS.\n"; | 1697 | print "CHECKPATCH in MAINTAINERS.\n"; |
1596 | } | 1698 | } |