diff options
| author | David Woodhouse <dwmw2@infradead.org> | 2007-08-23 05:43:14 -0400 |
|---|---|---|
| committer | David Woodhouse <dwmw2@infradead.org> | 2007-08-23 05:43:14 -0400 |
| commit | ac0c955d5048c2c580fa7166a89133f0fd76c125 (patch) | |
| tree | 041ac4fb544c7244a1a0b35c8ceabc142d5645c1 /scripts/checkpatch.pl | |
| parent | 68d09b1b6780415d82160f6b6d88e82bd724e691 (diff) | |
| parent | b377fd3982ad957c796758a90e2988401a884241 (diff) | |
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
Diffstat (limited to 'scripts/checkpatch.pl')
| -rwxr-xr-x | scripts/checkpatch.pl | 102 |
1 files changed, 71 insertions, 31 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 73751ab6ec0c..dae7d30dca0f 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.08'; | 12 | my $V = '0.09'; |
| 13 | 13 | ||
| 14 | use Getopt::Long qw(:config no_auto_abbrev); | 14 | use Getopt::Long qw(:config no_auto_abbrev); |
| 15 | 15 | ||
| @@ -311,7 +311,7 @@ sub process { | |||
| 311 | 311 | ||
| 312 | my $Ident = qr{[A-Za-z\d_]+}; | 312 | my $Ident = qr{[A-Za-z\d_]+}; |
| 313 | my $Storage = qr{extern|static}; | 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 | my $NonptrType = qr{ | 315 | my $NonptrType = qr{ |
| 316 | \b | 316 | \b |
| 317 | (?:const\s+)? | 317 | (?:const\s+)? |
| @@ -325,6 +325,7 @@ sub process { | |||
| 325 | unsigned| | 325 | unsigned| |
| 326 | float| | 326 | float| |
| 327 | double| | 327 | double| |
| 328 | bool| | ||
| 328 | long\s+int| | 329 | long\s+int| |
| 329 | long\s+long| | 330 | long\s+long| |
| 330 | long\s+long\s+int| | 331 | long\s+long\s+int| |
| @@ -340,7 +341,8 @@ sub process { | |||
| 340 | }x; | 341 | }x; |
| 341 | my $Type = qr{ | 342 | my $Type = qr{ |
| 342 | \b$NonptrType\b | 343 | \b$NonptrType\b |
| 343 | (?:\s*\*+\s*const|\s*\*+)? | 344 | (?:\s*\*+\s*const|\s*\*+|(?:\s*\[\s*\])+)? |
| 345 | (?:\s+$Sparse)* | ||
| 344 | }x; | 346 | }x; |
| 345 | my $Declare = qr{(?:$Storage\s+)?$Type}; | 347 | my $Declare = qr{(?:$Storage\s+)?$Type}; |
| 346 | my $Attribute = qr{const|__read_mostly|__init|__initdata|__meminit}; | 348 | my $Attribute = qr{const|__read_mostly|__init|__initdata|__meminit}; |
| @@ -494,16 +496,15 @@ sub process { | |||
| 494 | ERROR("use tabs not spaces\n" . $herevet); | 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 | # Remove comments from the line before processing. | 499 | # Remove comments from the line before processing. |
| 504 | $line =~ s@/\*.*\*/@@g; | 500 | my $comment_edge = ($line =~ s@/\*.*\*/@@g) + |
| 505 | $line =~ s@/\*.*@@; | 501 | ($line =~ s@/\*.*@@) + |
| 506 | $line =~ s@.*\*/@@; | 502 | ($line =~ s@^(.).*\*/@$1@); |
| 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); | ||
| 507 | 508 | ||
| 508 | # Standardise the strings and chars within the input to simplify matching. | 509 | # Standardise the strings and chars within the input to simplify matching. |
| 509 | $line = sanitise_line($line); | 510 | $line = sanitise_line($line); |
| @@ -599,7 +600,7 @@ sub process { | |||
| 599 | if (($prevline !~ /^}/) && | 600 | if (($prevline !~ /^}/) && |
| 600 | ($prevline !~ /^\+}/) && | 601 | ($prevline !~ /^\+}/) && |
| 601 | ($prevline !~ /^ }/) && | 602 | ($prevline !~ /^ }/) && |
| 602 | ($prevline !~ /\s$name(?:\s+$Attribute)?\s*(?:;|=)/)) { | 603 | ($prevline !~ /\b\Q$name\E(?:\s+$Attribute)?\s*(?:;|=)/)) { |
| 603 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); | 604 | WARN("EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); |
| 604 | } | 605 | } |
| 605 | } | 606 | } |
| @@ -680,9 +681,9 @@ sub process { | |||
| 680 | 681 | ||
| 681 | # check for spaces between functions and their parentheses. | 682 | # check for spaces between functions and their parentheses. |
| 682 | if ($line =~ /($Ident)\s+\(/ && | 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 | $line !~ /$Type\s+\(/ && $line !~ /^.\#\s*define\b/) { | 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 | # Check operator spacing. | 688 | # Check operator spacing. |
| 688 | # Note we expand the line with the leading + as the real | 689 | # Note we expand the line with the leading + as the real |
| @@ -712,6 +713,7 @@ sub process { | |||
| 712 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); | 713 | $c = 'W' if ($elements[$n + 2] =~ /^\s/); |
| 713 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); | 714 | $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); |
| 714 | $c = 'O' if ($elements[$n + 2] eq ''); | 715 | $c = 'O' if ($elements[$n + 2] eq ''); |
| 716 | $c = 'E' if ($elements[$n + 2] =~ /\s*\\$/); | ||
| 715 | } else { | 717 | } else { |
| 716 | $c = 'E'; | 718 | $c = 'E'; |
| 717 | } | 719 | } |
| @@ -812,7 +814,11 @@ sub process { | |||
| 812 | 814 | ||
| 813 | # All the others need spaces both sides. | 815 | # All the others need spaces both sides. |
| 814 | } elsif ($ctx !~ /[EW]x[WE]/) { | 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 | $off += length($elements[$n + 1]); | 823 | $off += length($elements[$n + 1]); |
| 818 | } | 824 | } |
| @@ -823,15 +829,24 @@ sub process { | |||
| 823 | WARN("multiple assignments should be avoided\n" . $herecurr); | 829 | WARN("multiple assignments should be avoided\n" . $herecurr); |
| 824 | } | 830 | } |
| 825 | 831 | ||
| 826 | # check for multiple declarations, allowing for a function declaration | 832 | ## # check for multiple declarations, allowing for a function declaration |
| 827 | # continuation. | 833 | ## # continuation. |
| 828 | if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && | 834 | ## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && |
| 829 | $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { | 835 | ## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { |
| 830 | WARN("declaring multiple variables together should be avoided\n" . $herecurr); | 836 | ## |
| 831 | } | 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 | #need space before brace following if, while, etc | 847 | #need space before brace following if, while, etc |
| 834 | if ($line =~ /\(.*\){/ || $line =~ /do{/) { | 848 | if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || |
| 849 | $line =~ /do{/) { | ||
| 835 | ERROR("need a space before the open brace '{'\n" . $herecurr); | 850 | ERROR("need a space before the open brace '{'\n" . $herecurr); |
| 836 | } | 851 | } |
| 837 | 852 | ||
| @@ -841,6 +856,22 @@ sub process { | |||
| 841 | ERROR("need a space after that close brace '}'\n" . $herecurr); | 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 | #goto labels aren't indented, allow a single space however | 875 | #goto labels aren't indented, allow a single space however |
| 845 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and | 876 | if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and |
| 846 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { | 877 | !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { |
| @@ -910,7 +941,7 @@ sub process { | |||
| 910 | # grabbing the statement after the identifier | 941 | # grabbing the statement after the identifier |
| 911 | $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$}; | 942 | $prevline =~ m{^(.#\s*define\s*$Ident(?:\([^\)]*\))?\s*)(.*)\\\s*$}; |
| 912 | ##print "1<$1> 2<$2>\n"; | 943 | ##print "1<$1> 2<$2>\n"; |
| 913 | if ($2 ne '') { | 944 | if (defined $2 && $2 ne '') { |
| 914 | $off = length($1); | 945 | $off = length($1); |
| 915 | $ln--; | 946 | $ln--; |
| 916 | $cnt++; | 947 | $cnt++; |
| @@ -950,8 +981,10 @@ sub process { | |||
| 950 | my ($lvl, @block) = ctx_block_level($nr, $cnt); | 981 | my ($lvl, @block) = ctx_block_level($nr, $cnt); |
| 951 | 982 | ||
| 952 | my $stmt = join(' ', @block); | 983 | my $stmt = join(' ', @block); |
| 953 | $stmt =~ s/^[^{]*{//; | 984 | $stmt =~ s/(^[^{]*){//; |
| 954 | $stmt =~ s/}[^}]*$//; | 985 | my $before = $1; |
| 986 | $stmt =~ s/}([^}]*$)//; | ||
| 987 | my $after = $1; | ||
| 955 | 988 | ||
| 956 | #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; | 989 | #print "block<" . join(' ', @block) . "><" . scalar(@block) . ">\n"; |
| 957 | #print "stmt<$stmt>\n\n"; | 990 | #print "stmt<$stmt>\n\n"; |
| @@ -963,12 +996,14 @@ sub process { | |||
| 963 | # Also nested if's often require braces to | 996 | # Also nested if's often require braces to |
| 964 | # disambiguate the else binding so shhh there. | 997 | # disambiguate the else binding so shhh there. |
| 965 | my @semi = ($stmt =~ /;/g); | 998 | my @semi = ($stmt =~ /;/g); |
| 999 | push(@semi, "/**/") if ($stmt =~ m@/\*@); | ||
| 966 | ##print "semi<" . scalar(@semi) . ">\n"; | 1000 | ##print "semi<" . scalar(@semi) . ">\n"; |
| 967 | if ($lvl == 0 && scalar(@semi) < 2 && | 1001 | if ($lvl == 0 && scalar(@semi) < 2 && |
| 968 | $stmt !~ /{/ && $stmt !~ /\bif\b/) { | 1002 | $stmt !~ /{/ && $stmt !~ /\bif\b/ && |
| 1003 | $before !~ /}/ && $after !~ /{/) { | ||
| 969 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; | 1004 | my $herectx = "$here\n" . join("\n", @control, @block[1 .. $#block]) . "\n"; |
| 970 | shift(@block); | 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 @@ sub process { | |||
| 1013 | # $clean = 0; | 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 | # check for spinlock_t definitions without a comment. | 1056 | # check for spinlock_t definitions without a comment. |
| 1017 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { | 1057 | if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/) { |
| 1018 | my $which = $1; | 1058 | my $which = $1; |
| @@ -1027,14 +1067,14 @@ sub process { | |||
| 1027 | } | 1067 | } |
| 1028 | } | 1068 | } |
| 1029 | # check of hardware specific defines | 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 | CHK("architecture specific defines should be avoided\n" . $herecurr); | 1071 | CHK("architecture specific defines should be avoided\n" . $herecurr); |
| 1032 | } | 1072 | } |
| 1033 | 1073 | ||
| 1034 | # check the location of the inline attribute, that it is between | 1074 | # check the location of the inline attribute, that it is between |
| 1035 | # storage class and type. | 1075 | # storage class and type. |
| 1036 | if ($line =~ /$Type\s+(?:inline|__always_inline)\b/ || | 1076 | if ($line =~ /$Type\s+(?:inline|__always_inline|noinline)\b/ || |
| 1037 | $line =~ /\b(?:inline|always_inline)\s+$Storage/) { | 1077 | $line =~ /\b(?:inline|__always_inline|noinline)\s+$Storage/) { |
| 1038 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); | 1078 | ERROR("inline keyword should sit between storage class and type\n" . $herecurr); |
| 1039 | } | 1079 | } |
| 1040 | 1080 | ||
