Commit 22f2a2ef9b468569cb34a7a056e54d56fdac0b9d

Authored by Andy Whitcroft
Committed by Linus Torvalds
1 parent a44648b057

update checkpatch.pl to version 0.09

This version brings a number of new checks, and a number of bug
fixes.  Of note:

  - checks for spacing on round and square bracket combinations
  - loosening of the single statement brace checks, to allow
    them when they contain comments or where other blocks in a
    compound statement have them.
  - parks the multple declaration support
  - allows architecture defines in architecture specific headers

Andy Whitcroft (21):
      Version: 0.09
      loosen single statement brace checks
      fix up multiple declaration to avoid function arguments
      add some function space parenthesis check exceptions
      handle EXPORT_'s with parentheses in their names
      clean up some warnings in multi-line macro bracketing support
      park the multiple declaration checks
      make block brace checks count comments as a statement
      __volatile__ and __extension__ are not functions
      allow architecture specific defined within architecture includes
      check spacing on square brackets
      check spacing on parentheses
      ensure we apply checks to the part before start comment
      check #ifdef conditional spacing
      handle __init_refok and __must_check
      add noinline to inline checks
      prevent email addresses from tripping spacing checks
      handle typed initialiser spacing
      handle line contination as end of line
      add bool to the type matcher
      refine EXPORT_SYMBOL checks to handle pointers

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>

Showing 1 changed file with 71 additions and 31 deletions Side-by-side Diff

scripts/checkpatch.pl
... ... @@ -9,7 +9,7 @@
9 9 my $P = $0;
10 10 $P =~ s@.*/@@g;
11 11  
12   -my $V = '0.08';
  12 +my $V = '0.09';
13 13  
14 14 use Getopt::Long qw(:config no_auto_abbrev);
15 15  
... ... @@ -311,7 +311,7 @@
311 311  
312 312 my $Ident = qr{[A-Za-z\d_]+};
313 313 my $Storage = qr{extern|static};
314   - my $Sparse = qr{__user|__kernel|__force|__iomem};
  314 + my $Sparse = qr{__user|__kernel|__force|__iomem|__must_check|__init_refok};
315 315 my $NonptrType = qr{
316 316 \b
317 317 (?:const\s+)?
... ... @@ -325,6 +325,7 @@
325 325 unsigned|
326 326 float|
327 327 double|
  328 + bool|
328 329 long\s+int|
329 330 long\s+long|
330 331 long\s+long\s+int|
... ... @@ -340,7 +341,8 @@
340 341 }x;
341 342 my $Type = qr{
342 343 \b$NonptrType\b
343   - (?:\s*\*+\s*const|\s*\*+)?
  344 + (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)?
  345 + (?:\s+$Sparse)*
344 346 }x;
345 347 my $Declare = qr{(?:$Storage\s+)?$Type};
346 348 my $Attribute = qr{const|__read_mostly|__init|__initdata|__meminit};
347 349  
348 350  
... ... @@ -494,17 +496,16 @@
494 496 ERROR("use tabs not spaces\n" . $herevet);
495 497 }
496 498  
497   - #
498   - # The rest of our checks refer specifically to C style
499   - # only apply those _outside_ comments.
500   - #
501   - next if ($in_comment);
502   -
503 499 # Remove comments from the line before processing.
504   - $line =~ s@/\*.*\*/@@g;
505   - $line =~ s@/\*.*@@;
506   - $line =~ s@.*\*/@@;
  500 + my $comment_edge = ($line =~ s@/\*.*\*/@@g) +
  501 + ($line =~ s@/\*.*@@) +
  502 + ($line =~ s@^(.).*\*/@$1@);
507 503  
  504 +# The rest of our checks refer specifically to C style
  505 +# only apply those _outside_ comments. Only skip
  506 +# lines in the middle of comments.
  507 + next if (!$comment_edge && $in_comment);
  508 +
508 509 # Standardise the strings and chars within the input to simplify matching.
509 510 $line = sanitise_line($line);
510 511  
... ... @@ -599,7 +600,7 @@
599 600 if (($prevline !~ /^}/) &&
600 601 ($prevline !~ /^\+}/) &&
601 602 ($prevline !~ /^ }/) &&
602   - ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) {
  603 + ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) {
603 604 WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
604 605 }
605 606 }
606 607  
... ... @@ -680,9 +681,9 @@
680 681  
681 682 # check for spaces between functions and their parentheses.
682 683 if ($line =~ /($Ident)\s+\(/ &&
683   - $1 !~ /^(?:if|for|while|switch|return|volatile)$/ &&
  684 + $1 !~ /^(?:if|for|while|switch|return|volatile|__volatile__|__attribute__|format|__extension__|Copyright)$/ &&
684 685 $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) {
685   - ERROR("no space between function name and open parenthesis '('\n" . $herecurr);
  686 + WARN("no space between function name and open parenthesis '('\n" . $herecurr);
686 687 }
687 688 # Check operator spacing.
688 689 # Note we expand the line with the leading + as the real
... ... @@ -712,6 +713,7 @@
712 713 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
713 714 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
714 715 $c = 'O' if ($elements[$n + 2] eq '');
  716 + $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/);
715 717 } else {
716 718 $c = 'E';
717 719 }
... ... @@ -812,7 +814,11 @@
812 814  
813 815 # All the others need spaces both sides.
814 816 } elsif ($ctx !~ /[EW]x[WE]/) {
815   - ERROR("need spaces around that '$op' $at\n" . $hereptr);
  817 + # Ignore email addresses <foo@bar>
  818 + if (!($op eq '<' && $cb =~ /$;\S+\@\S+>/) &&
  819 + !($op eq '>' && $cb =~ /<\S+\@\S+$;/)) {
  820 + ERROR("need spaces around that '$op' $at\n" . $hereptr);
  821 + }
816 822 }
817 823 $off += length($elements[$n + 1]);
818 824 }
819 825  
... ... @@ -823,15 +829,24 @@
823 829 WARN("multiple assignments should be avoided\n" . $herecurr);
824 830 }
825 831  
826   -# check for multiple declarations, allowing for a function declaration
827   -# continuation.
828   - if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
829   - $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
830   - WARN("declaring multiple variables together should be avoided\n" . $herecurr);
831   - }
  832 +## # check for multiple declarations, allowing for a function declaration
  833 +## # continuation.
  834 +## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
  835 +## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
  836 +##
  837 +## # Remove any bracketed sections to ensure we do not
  838 +## # falsly report the parameters of functions.
  839 +## my $ln = $line;
  840 +## while ($ln =~ s/\([^\(\)]*\)//g) {
  841 +## }
  842 +## if ($ln =~ /,/) {
  843 +## WARN("declaring multiple variables together should be avoided\n" . $herecurr);
  844 +## }
  845 +## }
832 846  
833 847 #need space before brace following if, while, etc
834   - if ($line =~ /\(.*\){/ || $line =~ /do{/) {
  848 + if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
  849 + $line =~ /do{/) {
835 850 ERROR("need a space before the open brace '{'\n" . $herecurr);
836 851 }
837 852  
... ... @@ -841,6 +856,22 @@
841 856 ERROR("need a space after that close brace '}'\n" . $herecurr);
842 857 }
843 858  
  859 +# check spacing on square brackets
  860 + if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
  861 + ERROR("no space after that open square bracket '['\n" . $herecurr);
  862 + }
  863 + if ($line =~ /\s\]/) {
  864 + ERROR("no space before that close square bracket ']'\n" . $herecurr);
  865 + }
  866 +
  867 +# check spacing on paretheses
  868 + if ($line =~ /\(\s/ && $line !~ /\(\s*$/) {
  869 + ERROR("no space after that open parenthesis '('\n" . $herecurr);
  870 + }
  871 + if ($line =~ /\s\)/) {
  872 + ERROR("no space before that close parenthesis ')'\n" . $herecurr);
  873 + }
  874 +
844 875 #goto labels aren't indented, allow a single space however
845 876 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
846 877 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
... ... @@ -910,7 +941,7 @@
910 941 # grabbing the statement after the identifier
911 942 $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$};
912 943 ##print "1<$1> 2<$2>\n";
913   - if ($2 ne '') {
  944 + if (defined $2 && $2 ne '') {
914 945 $off = length($1);
915 946 $ln--;
916 947 $cnt++;
... ... @@ -950,8 +981,10 @@
950 981 my ($lvl, @block) = ctx_block_level($nr, $cnt);
951 982  
952 983 my $stmt = join(' ', @block);
953   - $stmt =~ s/^[^{]*{//;
954   - $stmt =~ s/}[^}]*$//;
  984 + $stmt =~ s/(^[^{]*){//;
  985 + my $before = $1;
  986 + $stmt =~ s/}([^}]*$)//;
  987 + my $after = $1;
955 988  
956 989 #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n";
957 990 #print "stmt<$stmt>\n\n";
958 991  
959 992  
... ... @@ -963,12 +996,14 @@
963 996 # Also nested if's often require braces to
964 997 # disambiguate the else binding so shhh there.
965 998 my @semi = ($stmt =~ /;/g);
  999 + push(@semi, "/**/") if ($stmt =~ m@/\*@);
966 1000 ##print "semi<" . scalar(@semi) . ">\n";
967 1001 if ($lvl == 0 && scalar(@semi) < 2 &&
968   - $stmt !~ /{/ && $stmt !~ /\bif\b/) {
  1002 + $stmt !~ /{/ && $stmt !~ /\bif\b/ &&
  1003 + $before !~ /}/ && $after !~ /{/) {
969 1004 my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n";
970 1005 shift(@block);
971   - ERROR("braces {} are not necessary for single statement blocks\n" . $herectx);
  1006 + WARN("braces {} are not necessary for single statement blocks\n" . $herectx);
972 1007 }
973 1008 }
974 1009 }
... ... @@ -1013,6 +1048,11 @@
1013 1048 # $clean = 0;
1014 1049 # }
1015 1050  
  1051 +# warn about spacing in #ifdefs
  1052 + if ($line =~ /^.#\s*(ifdef|ifndef|elif)\s\s+/) {
  1053 + ERROR("exactly one space required after that #$1\n" . $herecurr);
  1054 + }
  1055 +
1016 1056 # check for spinlock_t definitions without a comment.
1017 1057 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) {
1018 1058 my $which = $1;
1019 1059  
... ... @@ -1027,14 +1067,14 @@
1027 1067 }
1028 1068 }
1029 1069 # check of hardware specific defines
1030   - if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@) {
  1070 + if ($line =~ m@^.#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
1031 1071 CHK("architecture specific defines should be avoided\n" . $herecurr);
1032 1072 }
1033 1073  
1034 1074 # check the location of the inline attribute, that it is between
1035 1075 # storage class and type.
1036   - if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ ||
1037   - $line =~ /\b(?:inline|always_inline)\s+$Storage/) {
  1076 + if ($line =~ /$Type\s+(?:inline|__always_inline|noinline)\b/ ||
  1077 + $line =~ /\b(?:inline|__always_inline|noinline)\s+$Storage/) {
1038 1078 ERROR("inline keyword should sit between storage class and type\n" . $herecurr);
1039 1079 }
1040 1080