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