update checkpatch.pl to version 0.05

This version brings a some new tests, and a host of changes to fix
false positives, of particular note:

- detect 'var ++;' and 'var --;' as a bad combination
- multistatement #defines are now checked based on statement count
- multistatement #defines with initialisation correctly reported
- checks the location of the inline keywords
- EXPORT_SYMBOL for variables are now understood
- typedefs are loosened to handle sparse etc

This version of checkpatch.pl can be found at the following URL:

http://www.shadowen.org/~apw/public/checkpatch/checkpatch.pl-0.05

Full Changelog:

Andy Whitcroft (18):
Version: 0.05
macro definition checks should be for a single statement
avoid assignements only in if conditionals
declarations of function pointers need no space
multiline macros which are purely initialisation cannot be wrapped
EXPORT_SYMBOL can also directly follow a variable definition
check on the location of the inline keyword
EXPORT_SYMBOL needs to allow for attributes
ensure we do not find C99 // in strings
handle malformed #include lines
accept the {0,} form
typedefs are sensible for defining function pointer parameters
ensure { handling correctly handles nested switch() statements
trailing whitespace checks are not anchored
typedefs for sparse bitwise annotations make sense
update the type matcher to include sparse annotations
clean up indent and spacing

Signed-off-by: Andy Whitcroft <apw@shadowen.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Andy Whitcroft and committed by Linus Torvalds 653d4876 92c4ca5c

+129 -54
+129 -54
scripts/checkpatch.pl
··· 9 my $P = $0; 10 $P =~ s@.*/@@g; 11 12 - my $V = '0.04'; 13 14 use Getopt::Long qw(:config no_auto_abbrev); 15 ··· 17 my $tree = 1; 18 my $chk_signoff = 1; 19 my $chk_patch = 1; 20 GetOptions( 21 'q|quiet' => \$quiet, 22 'tree!' => \$tree, 23 'signoff!' => \$chk_signoff, 24 'patch!' => \$chk_patch, 25 ) or exit; 26 27 my $exit = 0; ··· 153 } 154 155 sub ctx_block_get { 156 - my ($linenr, $remain, $outer) = @_; 157 my $line; 158 my $start = $linenr - 1; 159 my $blk = ''; ··· 167 168 $blk .= $rawlines[$line]; 169 170 - @o = ($blk =~ /\{/g); 171 - @c = ($blk =~ /\}/g); 172 173 if (!$outer || (scalar(@o) - scalar(@c)) == 1) { 174 push(@res, $rawlines[$line]); ··· 182 sub ctx_block_outer { 183 my ($linenr, $remain) = @_; 184 185 - return ctx_block_get($linenr, $remain, 1); 186 } 187 sub ctx_block { 188 my ($linenr, $remain) = @_; 189 190 - return ctx_block_get($linenr, $remain, 0); 191 } 192 193 sub ctx_locate_comment { ··· 271 my $in_comment = 0; 272 my $first_line = 0; 273 274 foreach my $line (@lines) { 275 $linenr++; 276 277 #extract the filename as it passes 278 if ($line=~/^\+\+\+\s+(\S+)/) { ··· 389 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 390 391 #trailing whitespace 392 - if ($line=~/\+.*\S\s+$/) { 393 my $herevet = "$here\n" . cat_vet($line) . "\n\n"; 394 print "trailing whitespace\n"; 395 print "$herevet"; ··· 420 # 421 next if ($in_comment); 422 423 - # Remove comments from the line before processing. 424 $line =~ s@/\*.*\*/@@g; 425 $line =~ s@/\*.*@@; 426 $line =~ s@.*\*/@@; 427 428 - # 429 - # Checks which may be anchored in the context. 430 - # 431 432 - # Check for switch () and associated case and default 433 - # statements should be at the same indent. 434 if ($line=~/\bswitch\s*\(.*\)/) { 435 my $err = ''; 436 my $sep = ''; ··· 459 #ignore lines not being added 460 if ($line=~/^[^\+]/) {next;} 461 462 - # 463 - # Checks which are anchored on the added line. 464 - # 465 466 # no C99 // comments 467 if ($line =~ m{//}) { ··· 493 # Remove C99 comments. 494 $line =~ s@//.*@@; 495 496 - # Standardise the strings and chars within the input 497 - # to simplify matching. 498 - $line = sanitise_line($line); 499 - 500 #EXPORT_SYMBOL should immediately follow its function closing }. 501 - if (($line =~ /EXPORT_SYMBOL.*\(.*\)/) || 502 - ($line =~ /EXPORT_UNUSED_SYMBOL.*\(.*\)/)) { 503 if (($prevline !~ /^}/) && 504 ($prevline !~ /^\+}/) && 505 - ($prevline !~ /^ }/)) { 506 - print "EXPORT_SYMBOL(func); should immediately follow its function\n"; 507 print "$herecurr"; 508 $clean = 0; 509 } 510 } 511 512 - # check for static initialisers. 513 if ($line=~/\s*static\s.*=\s+(0|NULL);/) { 514 print "do not initialise statics to 0 or NULL\n"; 515 print "$herecurr"; 516 $clean = 0; 517 } 518 519 - # check for new typedefs. 520 - if ($line=~/\s*typedef\s/) { 521 print "do not add new typedefs\n"; 522 print "$herecurr"; 523 $clean = 0; 524 } 525 526 # * goes on variable not on type 527 - my $type = '(?:char|short|int|long|unsigned|float|double|' . 528 - 'struct\s+[A-Za-z\d_]+|' . 529 - 'union\s+[A-Za-z\d_]+)'; 530 - 531 if ($line =~ m{[A-Za-z\d_]+(\*+) [A-Za-z\d_]+}) { 532 print "\"foo$1 bar\" should be \"foo $1bar\"\n"; 533 print "$herecurr"; 534 $clean = 0; 535 } 536 - if ($line =~ m{$type (\*) [A-Za-z\d_]+} || 537 $line =~ m{[A-Za-z\d_]+ (\*\*+) [A-Za-z\d_]+}) { 538 print "\"foo $1 bar\" should be \"foo $1bar\"\n"; 539 print "$herecurr"; ··· 579 } 580 } 581 582 - #function brace can't be on same line, except for #defines of do while, or if closed on same line 583 if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and 584 !($line=~/\#define.*do\s{/) and !($line=~/}/)) { 585 print "braces following function declarations go on the next line\n"; 586 print "$herecurr"; 587 $clean = 0; 588 } 589 # Note we expand the line with the leading + as the real 590 # line will be displayed with the leading + and the tabs 591 # will therefore also expand that way. ··· 596 $opline = expand_tabs($opline); 597 $opline =~ s/^./ /; 598 if (!($line=~/\#\s*include/)) { 599 - # Check operator spacing. 600 my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline); 601 my $off = 0; 602 for (my $n = 0; $n < $#elements; $n += 2) { ··· 623 # Pick up the preceeding and succeeding characters. 624 my $ca = substr($opline, $off - 1, 1); 625 my $cc = ''; 626 - if (length($opline) > ($off + length($elements[$n]))) { 627 - $cc = substr($opline, $off + 1 + length($elements[$n]), 1); 628 } 629 630 my $ctx = "${a}x${c}"; ··· 649 650 # , must have a space on the right. 651 } elsif ($op eq ',') { 652 - if ($ctx !~ /.xW|.xE/) { 653 print "need space after that '$op' $at\n"; 654 print "$hereptr"; 655 $clean = 0; ··· 670 671 # unary ++ and unary -- are allowed no space on one side. 672 } elsif ($op eq '++' or $op eq '--') { 673 - if ($ctx !~ /[WOB]x[^W]|[^W]x[WOB]/) { 674 print "need space one side of that '$op' $at\n"; 675 print "$hereptr"; 676 $clean = 0; 677 } ··· 712 print "$hereptr"; 713 $clean = 0; 714 } 715 - } elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB/) { 716 print "need space before that '$op' $at\n"; 717 print "$hereptr"; 718 $clean = 0; ··· 761 } 762 763 # Check for illegal assignment in if conditional. 764 - if ($line=~/\b(if|while)\s*\(.*[^<>!=]=[^=].*\)/) { 765 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/); 766 - print "do not use assignment in condition\n"; 767 print "$herecurr"; 768 $clean = 0; 769 } ··· 791 $clean = 0; 792 } 793 794 - #warn if <asm/foo.h> is #included and <linux/foo.h> is available. 795 - if ($tree && $line =~ qr|\s*\#\s*include\s*\<asm\/(.*)\.h\>|) { 796 my $checkfile = "include/linux/$1.h"; 797 if (-f $checkfile) { 798 print "Use #include <linux/$1.h> instead of <asm/$1.h>\n"; ··· 801 } 802 } 803 804 - #if/while/etc brace do not go on next line, unless #defining a do while loop, or if that brace on the next line is for something else 805 if ($prevline=~/\b(if|while|for|switch)\s*\(/) { 806 my @opened = $prevline=~/\(/g; 807 my @closed = $prevline=~/\)/g; ··· 824 } 825 826 if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and 827 - !($next_line=~/\b(if|while|for)/) and !($next_line=~/\#define.*do.*while/)) { 828 print "That { should be on the previous line\n"; 829 print "$here\n$display_segment\n$next_line\n\n"; 830 $clean = 0; 831 } 832 } 833 834 - #multiline macros should be enclosed in a do while loop 835 - if (($prevline=~/\#define.*\\/) and !($prevline=~/do\s+{/) and 836 - !($prevline=~/\(\{/) and ($line=~/;\s*\\/) and 837 - !($line=~/do.*{/) and !($line=~/\(\{/)) { 838 - print "Macros with multiple statements should be enclosed in a do - while loop\n"; 839 - print "$hereprev"; 840 - $clean = 0; 841 } 842 843 - # don't include deprecated include files 844 for my $inc (@dep_includes) { 845 - if ($line =~ m@\#\s*include\s*\<$inc>@) { 846 print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n"; 847 print "$herecurr"; 848 $clean = 0; ··· 910 # check of hardware specific defines 911 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) { 912 print "architecture specific defines should be avoided\n"; 913 print "$herecurr"; 914 $clean = 0; 915 }
··· 9 my $P = $0; 10 $P =~ s@.*/@@g; 11 12 + my $V = '0.05'; 13 14 use Getopt::Long qw(:config no_auto_abbrev); 15 ··· 17 my $tree = 1; 18 my $chk_signoff = 1; 19 my $chk_patch = 1; 20 + my $tst_type = 0; 21 GetOptions( 22 'q|quiet' => \$quiet, 23 'tree!' => \$tree, 24 'signoff!' => \$chk_signoff, 25 'patch!' => \$chk_patch, 26 + 'test-type!' => \$tst_type, 27 ) or exit; 28 29 my $exit = 0; ··· 151 } 152 153 sub ctx_block_get { 154 + my ($linenr, $remain, $outer, $open, $close) = @_; 155 my $line; 156 my $start = $linenr - 1; 157 my $blk = ''; ··· 165 166 $blk .= $rawlines[$line]; 167 168 + @o = ($blk =~ /$open/g); 169 + @c = ($blk =~ /$close/g); 170 171 if (!$outer || (scalar(@o) - scalar(@c)) == 1) { 172 push(@res, $rawlines[$line]); ··· 180 sub ctx_block_outer { 181 my ($linenr, $remain) = @_; 182 183 + return ctx_block_get($linenr, $remain, 1, '\{', '\}'); 184 } 185 sub ctx_block { 186 my ($linenr, $remain) = @_; 187 188 + return ctx_block_get($linenr, $remain, 0, '\{', '\}'); 189 + } 190 + sub ctx_statement { 191 + my ($linenr, $remain) = @_; 192 + 193 + return ctx_block_get($linenr, $remain, 0, '\(', '\)'); 194 } 195 196 sub ctx_locate_comment { ··· 264 my $in_comment = 0; 265 my $first_line = 0; 266 267 + my $ident = '[A-Za-z\d_]+'; 268 + my $storage = '(?:extern|static)'; 269 + my $sparse = '(?:__user|__kernel|__force|__iomem)'; 270 + my $type = '(?:unsigned\s+)?' . 271 + '(?:void|char|short|int|long|unsigned|float|double|' . 272 + 'long\s+long|' . 273 + "struct\\s+${ident}|" . 274 + "union\\s+${ident}|" . 275 + "${ident}_t)" . 276 + "(?:\\s+$sparse)*" . 277 + '(?:\s*\*+)?'; 278 + my $attribute = '(?:__read_mostly|__init|__initdata)'; 279 + 280 + my $Ident = $ident; 281 + my $Type = $type; 282 + my $Storage = $storage; 283 + my $Declare = "(?:$storage\\s+)?$type"; 284 + my $Attribute = $attribute; 285 + 286 foreach my $line (@lines) { 287 $linenr++; 288 + 289 + my $rawline = $line; 290 291 #extract the filename as it passes 292 if ($line=~/^\+\+\+\s+(\S+)/) { ··· 361 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 362 363 #trailing whitespace 364 + if ($line=~/^\+.*\S\s+$/) { 365 my $herevet = "$here\n" . cat_vet($line) . "\n\n"; 366 print "trailing whitespace\n"; 367 print "$herevet"; ··· 392 # 393 next if ($in_comment); 394 395 + # Remove comments from the line before processing. 396 $line =~ s@/\*.*\*/@@g; 397 $line =~ s@/\*.*@@; 398 $line =~ s@.*\*/@@; 399 400 + # Standardise the strings and chars within the input to simplify matching. 401 + $line = sanitise_line($line); 402 403 + # 404 + # Checks which may be anchored in the context. 405 + # 406 + 407 + # Check for switch () and associated case and default 408 + # statements should be at the same indent. 409 if ($line=~/\bswitch\s*\(.*\)/) { 410 my $err = ''; 411 my $sep = ''; ··· 428 #ignore lines not being added 429 if ($line=~/^[^\+]/) {next;} 430 431 + # TEST: allow direct testing of the type matcher. 432 + if ($tst_type && $line =~ /^.$Declare$/) { 433 + print "TEST: is type $Declare\n"; 434 + print "$herecurr"; 435 + $clean = 0; 436 + next; 437 + } 438 + 439 + # 440 + # Checks which are anchored on the added line. 441 + # 442 + 443 + # check for malformed paths in #include statements (uses RAW line) 444 + if ($rawline =~ m{^.#\s*include\s+[<"](.*)[">]}) { 445 + my $path = $1; 446 + if ($path =~ m{//}) { 447 + print "malformed #include filename\n"; 448 + print "$herecurr"; 449 + $clean = 0; 450 + } 451 + # Sanitise this special form of string. 452 + $path = 'X' x length($path); 453 + $line =~ s{\<.*\>}{<$path>}; 454 + } 455 456 # no C99 // comments 457 if ($line =~ m{//}) { ··· 441 # Remove C99 comments. 442 $line =~ s@//.*@@; 443 444 #EXPORT_SYMBOL should immediately follow its function closing }. 445 + if (($line =~ /EXPORT_SYMBOL.*\((.*)\)/) || 446 + ($line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 447 + my $name = $1; 448 if (($prevline !~ /^}/) && 449 ($prevline !~ /^\+}/) && 450 + ($prevline !~ /^ }/) && 451 + ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) { 452 + print "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n"; 453 print "$herecurr"; 454 $clean = 0; 455 } 456 } 457 458 + # check for static initialisers. 459 if ($line=~/\s*static\s.*=\s+(0|NULL);/) { 460 print "do not initialise statics to 0 or NULL\n"; 461 print "$herecurr"; 462 $clean = 0; 463 } 464 465 + # check for new typedefs, only function parameters and sparse annotations 466 + # make sense. 467 + if ($line =~ /\btypedef\s/ && 468 + $line !~ /\btypedef\s+$Type\s+\(\s*$Ident\s*\)\s*\(/ && 469 + $line !~ /\b__bitwise(?:__|)\b/) { 470 print "do not add new typedefs\n"; 471 print "$herecurr"; 472 $clean = 0; 473 } 474 475 # * goes on variable not on type 476 if ($line =~ m{[A-Za-z\d_]+(\*+) [A-Za-z\d_]+}) { 477 print "\"foo$1 bar\" should be \"foo $1bar\"\n"; 478 print "$herecurr"; 479 $clean = 0; 480 } 481 + if ($line =~ m{$Type (\*) [A-Za-z\d_]+} || 482 $line =~ m{[A-Za-z\d_]+ (\*\*+) [A-Za-z\d_]+}) { 483 print "\"foo $1 bar\" should be \"foo $1bar\"\n"; 484 print "$herecurr"; ··· 530 } 531 } 532 533 + # function brace can't be on same line, except for #defines of do while, 534 + # or if closed on same line 535 if (($line=~/[A-Za-z\d_]+\**\s+\**[A-Za-z\d_]+\(.*\).* {/) and 536 !($line=~/\#define.*do\s{/) and !($line=~/}/)) { 537 print "braces following function declarations go on the next line\n"; 538 print "$herecurr"; 539 $clean = 0; 540 } 541 + 542 + # Check operator spacing. 543 # Note we expand the line with the leading + as the real 544 # line will be displayed with the leading + and the tabs 545 # will therefore also expand that way. ··· 544 $opline = expand_tabs($opline); 545 $opline =~ s/^./ /; 546 if (!($line=~/\#\s*include/)) { 547 my @elements = split(/(<<=|>>=|<=|>=|==|!=|\+=|-=|\*=|\/=|%=|\^=|\|=|&=|->|<<|>>|<|>|=|!|~|&&|\|\||,|\^|\+\+|--|;|&|\||\+|-|\*|\/\/|\/)/, $opline); 548 my $off = 0; 549 for (my $n = 0; $n < $#elements; $n += 2) { ··· 572 # Pick up the preceeding and succeeding characters. 573 my $ca = substr($opline, $off - 1, 1); 574 my $cc = ''; 575 + if (length($opline) >= ($off + length($elements[$n + 1]))) { 576 + $cc = substr($opline, $off + length($elements[$n + 1]), 1); 577 } 578 579 my $ctx = "${a}x${c}"; ··· 598 599 # , must have a space on the right. 600 } elsif ($op eq ',') { 601 + if ($ctx !~ /.xW|.xE/ && $cc ne '}') { 602 print "need space after that '$op' $at\n"; 603 print "$hereptr"; 604 $clean = 0; ··· 619 620 # unary ++ and unary -- are allowed no space on one side. 621 } elsif ($op eq '++' or $op eq '--') { 622 + if ($ctx !~ /[WOB]x[^W]/ && $ctx !~ /[^W]x[WOB]/) { 623 print "need space one side of that '$op' $at\n"; 624 + print "$hereptr"; 625 + $clean = 0; 626 + } 627 + if ($ctx =~ /Wx./ && $cc eq ';') { 628 + print "no space before that '$op' $at\n"; 629 print "$hereptr"; 630 $clean = 0; 631 } ··· 656 print "$hereptr"; 657 $clean = 0; 658 } 659 + } elsif ($ctx !~ /VxV|[EW]x[WE]|[EWB]x[VO]|OxV|WxB|BxB/) { 660 print "need space before that '$op' $at\n"; 661 print "$hereptr"; 662 $clean = 0; ··· 705 } 706 707 # Check for illegal assignment in if conditional. 708 + if ($line=~/\bif\s*\(.*[^<>!=]=[^=].*\)/) { 709 #next if ($line=~/\".*\Q$op\E.*\"/ or $line=~/\'\Q$op\E\'/); 710 + print "do not use assignment in if condition\n"; 711 print "$herecurr"; 712 $clean = 0; 713 } ··· 735 $clean = 0; 736 } 737 738 + #warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 739 + if ($tree && $rawline =~ m{^.\#\s*include\s*\<asm\/(.*)\.h\>}) { 740 my $checkfile = "include/linux/$1.h"; 741 if (-f $checkfile) { 742 print "Use #include <linux/$1.h> instead of <asm/$1.h>\n"; ··· 745 } 746 } 747 748 + # if/while/etc brace do not go on next line, unless defining a do while loop, 749 + # or if that brace on the next line is for something else 750 if ($prevline=~/\b(if|while|for|switch)\s*\(/) { 751 my @opened = $prevline=~/\(/g; 752 my @closed = $prevline=~/\)/g; ··· 767 } 768 769 if (($prevline=~/\b(if|while|for|switch)\s*\(.*\)\s*$/) and ($next_line=~/{/) and 770 + !($next_line=~/\b(if|while|for|switch)/) and !($next_line=~/\#define.*do.*while/)) { 771 print "That { should be on the previous line\n"; 772 print "$here\n$display_segment\n$next_line\n\n"; 773 $clean = 0; 774 } 775 } 776 777 + # multi-statement macros should be enclosed in a do while loop, grab the 778 + # first statement and ensure its the whole macro if its not enclosed 779 + # in a known goot container 780 + if (($prevline=~/\#define.*\\/) and 781 + !($prevline=~/do\s+{/) and !($prevline=~/\(\{/) and 782 + !($line=~/do.*{/) and !($line=~/\(\{/) and 783 + !($line=~/^.\s*$Declare\s/)) { 784 + # Grab the first statement, if that is the entire macro 785 + # its ok. This may start either on the #define line 786 + # or the one below. 787 + my $ctx1 = join('', ctx_statement($linenr - 1, $realcnt + 1)); 788 + my $ctx2 = join('', ctx_statement($linenr, $realcnt)); 789 + 790 + if ($ctx1 =~ /\\$/ && $ctx2 =~ /\\$/) { 791 + print "Macros with multiple statements should be enclosed in a do - while loop\n"; 792 + print "$hereprev"; 793 + $clean = 0; 794 + } 795 } 796 797 + # don't include deprecated include files (uses RAW line) 798 for my $inc (@dep_includes) { 799 + if ($rawline =~ m@\#\s*include\s*\<$inc>@) { 800 print "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n"; 801 print "$herecurr"; 802 $clean = 0; ··· 842 # check of hardware specific defines 843 if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) { 844 print "architecture specific defines should be avoided\n"; 845 + print "$herecurr"; 846 + $clean = 0; 847 + } 848 + 849 + if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ || 850 + $line =~ /\b(?:inline|always_inline)\s+$Storage/) { 851 + print "inline keyword should sit between storage class and type\n"; 852 print "$herecurr"; 853 $clean = 0; 854 }