diff options
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-x | scripts/checkpatch.pl | 204 |
1 files changed, 181 insertions, 23 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index 9c9810030377..464dcef79b35 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -29,6 +29,7 @@ my $mailback = 0; | |||
29 | my $summary_file = 0; | 29 | my $summary_file = 0; |
30 | my $show_types = 0; | 30 | my $show_types = 0; |
31 | my $fix = 0; | 31 | my $fix = 0; |
32 | my $fix_inplace = 0; | ||
32 | my $root; | 33 | my $root; |
33 | my %debug; | 34 | my %debug; |
34 | my %camelcase = (); | 35 | my %camelcase = (); |
@@ -76,6 +77,9 @@ Options: | |||
76 | "<inputfile>.EXPERIMENTAL-checkpatch-fixes" | 77 | "<inputfile>.EXPERIMENTAL-checkpatch-fixes" |
77 | with potential errors corrected to the preferred | 78 | with potential errors corrected to the preferred |
78 | checkpatch style | 79 | checkpatch style |
80 | --fix-inplace EXPERIMENTAL - may create horrible results | ||
81 | Is the same as --fix, but overwrites the input | ||
82 | file. It's your fault if there's no backup or git | ||
79 | --ignore-perl-version override checking of perl version. expect | 83 | --ignore-perl-version override checking of perl version. expect |
80 | runtime errors. | 84 | runtime errors. |
81 | -h, --help, --version display this help and exit | 85 | -h, --help, --version display this help and exit |
@@ -131,6 +135,7 @@ GetOptions( | |||
131 | 'mailback!' => \$mailback, | 135 | 'mailback!' => \$mailback, |
132 | 'summary-file!' => \$summary_file, | 136 | 'summary-file!' => \$summary_file, |
133 | 'fix!' => \$fix, | 137 | 'fix!' => \$fix, |
138 | 'fix-inplace!' => \$fix_inplace, | ||
134 | 'ignore-perl-version!' => \$ignore_perl_version, | 139 | 'ignore-perl-version!' => \$ignore_perl_version, |
135 | 'debug=s' => \%debug, | 140 | 'debug=s' => \%debug, |
136 | 'test-only=s' => \$tst_only, | 141 | 'test-only=s' => \$tst_only, |
@@ -140,6 +145,8 @@ GetOptions( | |||
140 | 145 | ||
141 | help(0) if ($help); | 146 | help(0) if ($help); |
142 | 147 | ||
148 | $fix = 1 if ($fix_inplace); | ||
149 | |||
143 | my $exit = 0; | 150 | my $exit = 0; |
144 | 151 | ||
145 | if ($^V && $^V lt $minimum_perl_version) { | 152 | if ($^V && $^V lt $minimum_perl_version) { |
@@ -464,7 +471,7 @@ sub seed_camelcase_includes { | |||
464 | 471 | ||
465 | $camelcase_seeded = 1; | 472 | $camelcase_seeded = 1; |
466 | 473 | ||
467 | if (-d ".git") { | 474 | if (-e ".git") { |
468 | my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; | 475 | my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; |
469 | chomp $git_last_include_commit; | 476 | chomp $git_last_include_commit; |
470 | $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; | 477 | $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; |
@@ -492,7 +499,7 @@ sub seed_camelcase_includes { | |||
492 | return; | 499 | return; |
493 | } | 500 | } |
494 | 501 | ||
495 | if (-d ".git") { | 502 | if (-e ".git") { |
496 | $files = `git ls-files "include/*.h"`; | 503 | $files = `git ls-files "include/*.h"`; |
497 | @include_files = split('\n', $files); | 504 | @include_files = split('\n', $files); |
498 | } | 505 | } |
@@ -1963,15 +1970,14 @@ sub process { | |||
1963 | } | 1970 | } |
1964 | 1971 | ||
1965 | # Check for FSF mailing addresses. | 1972 | # Check for FSF mailing addresses. |
1966 | if ($rawline =~ /You should have received a copy/ || | 1973 | if ($rawline =~ /\bwrite to the Free/i || |
1967 | $rawline =~ /write to the Free Software/ || | 1974 | $rawline =~ /\b59\s+Temple\s+Pl/i || |
1968 | $rawline =~ /59 Temple Place/ || | 1975 | $rawline =~ /\b51\s+Franklin\s+St/i) { |
1969 | $rawline =~ /51 Franklin Street/) { | ||
1970 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; | 1976 | my $herevet = "$here\n" . cat_vet($rawline) . "\n"; |
1971 | my $msg_type = \&ERROR; | 1977 | my $msg_type = \&ERROR; |
1972 | $msg_type = \&CHK if ($file); | 1978 | $msg_type = \&CHK if ($file); |
1973 | &{$msg_type}("FSF_MAILING_ADDRESS", | 1979 | &{$msg_type}("FSF_MAILING_ADDRESS", |
1974 | "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet) | 1980 | "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet) |
1975 | } | 1981 | } |
1976 | 1982 | ||
1977 | # check for Kconfig help text having a real description | 1983 | # check for Kconfig help text having a real description |
@@ -2034,6 +2040,33 @@ sub process { | |||
2034 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); | 2040 | "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); |
2035 | } | 2041 | } |
2036 | 2042 | ||
2043 | # check for DT compatible documentation | ||
2044 | if (defined $root && $realfile =~ /\.dts/ && | ||
2045 | $rawline =~ /^\+\s*compatible\s*=/) { | ||
2046 | my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; | ||
2047 | |||
2048 | foreach my $compat (@compats) { | ||
2049 | my $compat2 = $compat; | ||
2050 | my $dt_path = $root . "/Documentation/devicetree/bindings/"; | ||
2051 | $compat2 =~ s/\,[a-z]*\-/\,<\.\*>\-/; | ||
2052 | `grep -Erq "$compat|$compat2" $dt_path`; | ||
2053 | if ( $? >> 8 ) { | ||
2054 | WARN("UNDOCUMENTED_DT_STRING", | ||
2055 | "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); | ||
2056 | } | ||
2057 | |||
2058 | my $vendor = $compat; | ||
2059 | my $vendor_path = $dt_path . "vendor-prefixes.txt"; | ||
2060 | next if (! -f $vendor_path); | ||
2061 | $vendor =~ s/^([a-zA-Z0-9]+)\,.*/$1/; | ||
2062 | `grep -Eq "$vendor" $vendor_path`; | ||
2063 | if ( $? >> 8 ) { | ||
2064 | WARN("UNDOCUMENTED_DT_STRING", | ||
2065 | "DT compatible string vendor \"$vendor\" appears un-documented -- check $vendor_path\n" . $herecurr); | ||
2066 | } | ||
2067 | } | ||
2068 | } | ||
2069 | |||
2037 | # check we are in a valid source file if not then ignore this hunk | 2070 | # check we are in a valid source file if not then ignore this hunk |
2038 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); | 2071 | next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); |
2039 | 2072 | ||
@@ -2049,16 +2082,12 @@ sub process { | |||
2049 | } | 2082 | } |
2050 | 2083 | ||
2051 | # Check for user-visible strings broken across lines, which breaks the ability | 2084 | # Check for user-visible strings broken across lines, which breaks the ability |
2052 | # to grep for the string. Limited to strings used as parameters (those | 2085 | # to grep for the string. Make exceptions when the previous string ends in a |
2053 | # following an open parenthesis), which almost completely eliminates false | 2086 | # newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' |
2054 | # positives, as well as warning only once per parameter rather than once per | 2087 | # (common in inline assembly) or is a octal \123 or hexadecimal \xaf value |
2055 | # line of the string. Make an exception when the previous string ends in a | ||
2056 | # newline (multiple lines in one string constant) or \n\t (common in inline | ||
2057 | # assembly to indent the instruction on the following line). | ||
2058 | if ($line =~ /^\+\s*"/ && | 2088 | if ($line =~ /^\+\s*"/ && |
2059 | $prevline =~ /"\s*$/ && | 2089 | $prevline =~ /"\s*$/ && |
2060 | $prevline =~ /\(/ && | 2090 | $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { |
2061 | $prevrawline !~ /\\n(?:\\t)*"\s*$/) { | ||
2062 | WARN("SPLIT_STRING", | 2091 | WARN("SPLIT_STRING", |
2063 | "quoted string split across lines\n" . $hereprev); | 2092 | "quoted string split across lines\n" . $hereprev); |
2064 | } | 2093 | } |
@@ -2115,8 +2144,10 @@ sub process { | |||
2115 | if (WARN("SPACE_BEFORE_TAB", | 2144 | if (WARN("SPACE_BEFORE_TAB", |
2116 | "please, no space before tabs\n" . $herevet) && | 2145 | "please, no space before tabs\n" . $herevet) && |
2117 | $fix) { | 2146 | $fix) { |
2118 | $fixed[$linenr - 1] =~ | 2147 | while ($fixed[$linenr - 1] =~ |
2119 | s/(^\+.*) +\t/$1\t/; | 2148 | s/(^\+.*) {8,8}+\t/$1\t\t/) {} |
2149 | while ($fixed[$linenr - 1] =~ | ||
2150 | s/(^\+.*) +\t/$1\t/) {} | ||
2120 | } | 2151 | } |
2121 | } | 2152 | } |
2122 | 2153 | ||
@@ -2634,10 +2665,22 @@ sub process { | |||
2634 | $herecurr); | 2665 | $herecurr); |
2635 | } | 2666 | } |
2636 | 2667 | ||
2637 | # check for declarations of struct pci_device_id | 2668 | # check for function declarations without arguments like "int foo()" |
2638 | if ($line =~ /\bstruct\s+pci_device_id\s+\w+\s*\[\s*\]\s*\=\s*\{/) { | 2669 | if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { |
2639 | WARN("DEFINE_PCI_DEVICE_TABLE", | 2670 | if (ERROR("FUNCTION_WITHOUT_ARGS", |
2640 | "Use DEFINE_PCI_DEVICE_TABLE for struct pci_device_id\n" . $herecurr); | 2671 | "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && |
2672 | $fix) { | ||
2673 | $fixed[$linenr - 1] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; | ||
2674 | } | ||
2675 | } | ||
2676 | |||
2677 | # check for uses of DEFINE_PCI_DEVICE_TABLE | ||
2678 | if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) { | ||
2679 | if (WARN("DEFINE_PCI_DEVICE_TABLE", | ||
2680 | "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) && | ||
2681 | $fix) { | ||
2682 | $fixed[$linenr - 1] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /; | ||
2683 | } | ||
2641 | } | 2684 | } |
2642 | 2685 | ||
2643 | # check for new typedefs, only function parameters and sparse annotations | 2686 | # check for new typedefs, only function parameters and sparse annotations |
@@ -2802,6 +2845,65 @@ sub process { | |||
2802 | } | 2845 | } |
2803 | } | 2846 | } |
2804 | 2847 | ||
2848 | # Function pointer declarations | ||
2849 | # check spacing between type, funcptr, and args | ||
2850 | # canonical declaration is "type (*funcptr)(args...)" | ||
2851 | # | ||
2852 | # the $Declare variable will capture all spaces after the type | ||
2853 | # so check it for trailing missing spaces or multiple spaces | ||
2854 | if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)$Ident(\s*)\)(\s*)\(/) { | ||
2855 | my $declare = $1; | ||
2856 | my $pre_pointer_space = $2; | ||
2857 | my $post_pointer_space = $3; | ||
2858 | my $funcname = $4; | ||
2859 | my $post_funcname_space = $5; | ||
2860 | my $pre_args_space = $6; | ||
2861 | |||
2862 | if ($declare !~ /\s$/) { | ||
2863 | WARN("SPACING", | ||
2864 | "missing space after return type\n" . $herecurr); | ||
2865 | } | ||
2866 | |||
2867 | # unnecessary space "type (*funcptr)(args...)" | ||
2868 | elsif ($declare =~ /\s{2,}$/) { | ||
2869 | WARN("SPACING", | ||
2870 | "Multiple spaces after return type\n" . $herecurr); | ||
2871 | } | ||
2872 | |||
2873 | # unnecessary space "type ( *funcptr)(args...)" | ||
2874 | if (defined $pre_pointer_space && | ||
2875 | $pre_pointer_space =~ /^\s/) { | ||
2876 | WARN("SPACING", | ||
2877 | "Unnecessary space after function pointer open parenthesis\n" . $herecurr); | ||
2878 | } | ||
2879 | |||
2880 | # unnecessary space "type (* funcptr)(args...)" | ||
2881 | if (defined $post_pointer_space && | ||
2882 | $post_pointer_space =~ /^\s/) { | ||
2883 | WARN("SPACING", | ||
2884 | "Unnecessary space before function pointer name\n" . $herecurr); | ||
2885 | } | ||
2886 | |||
2887 | # unnecessary space "type (*funcptr )(args...)" | ||
2888 | if (defined $post_funcname_space && | ||
2889 | $post_funcname_space =~ /^\s/) { | ||
2890 | WARN("SPACING", | ||
2891 | "Unnecessary space after function pointer name\n" . $herecurr); | ||
2892 | } | ||
2893 | |||
2894 | # unnecessary space "type (*funcptr) (args...)" | ||
2895 | if (defined $pre_args_space && | ||
2896 | $pre_args_space =~ /^\s/) { | ||
2897 | WARN("SPACING", | ||
2898 | "Unnecessary space before function pointer arguments\n" . $herecurr); | ||
2899 | } | ||
2900 | |||
2901 | if (show_type("SPACING") && $fix) { | ||
2902 | $fixed[$linenr - 1] =~ | ||
2903 | s/^(.\s*$Declare)\(\s*\*\s*($Ident)\s*\)\s*\(/rtrim($1) . " " . "\(\*$2\)\("/ex; | ||
2904 | } | ||
2905 | } | ||
2906 | |||
2805 | # check for spacing round square brackets; allowed: | 2907 | # check for spacing round square brackets; allowed: |
2806 | # 1. with a type on the left -- int [] a; | 2908 | # 1. with a type on the left -- int [] a; |
2807 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, | 2909 | # 2. at the beginning of a line for slice initialisers -- [0...10] = 5, |
@@ -3122,7 +3224,7 @@ sub process { | |||
3122 | } | 3224 | } |
3123 | 3225 | ||
3124 | # check for whitespace before a non-naked semicolon | 3226 | # check for whitespace before a non-naked semicolon |
3125 | if ($line =~ /^\+.*\S\s+;/) { | 3227 | if ($line =~ /^\+.*\S\s+;\s*$/) { |
3126 | if (WARN("SPACING", | 3228 | if (WARN("SPACING", |
3127 | "space prohibited before semicolon\n" . $herecurr) && | 3229 | "space prohibited before semicolon\n" . $herecurr) && |
3128 | $fix) { | 3230 | $fix) { |
@@ -3246,6 +3348,20 @@ sub process { | |||
3246 | } | 3348 | } |
3247 | } | 3349 | } |
3248 | 3350 | ||
3351 | # if statements using unnecessary parentheses - ie: if ((foo == bar)) | ||
3352 | if ($^V && $^V ge 5.10.0 && | ||
3353 | $line =~ /\bif\s*((?:\(\s*){2,})/) { | ||
3354 | my $openparens = $1; | ||
3355 | my $count = $openparens =~ tr@\(@\(@; | ||
3356 | my $msg = ""; | ||
3357 | if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { | ||
3358 | my $comp = $4; #Not $1 because of $LvalOrFunc | ||
3359 | $msg = " - maybe == should be = ?" if ($comp eq "=="); | ||
3360 | WARN("UNNECESSARY_PARENTHESES", | ||
3361 | "Unnecessary parentheses$msg\n" . $herecurr); | ||
3362 | } | ||
3363 | } | ||
3364 | |||
3249 | # Return of what appears to be an errno should normally be -'ve | 3365 | # Return of what appears to be an errno should normally be -'ve |
3250 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { | 3366 | if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { |
3251 | my $name = $1; | 3367 | my $name = $1; |
@@ -3980,6 +4096,16 @@ sub process { | |||
3980 | } | 4096 | } |
3981 | } | 4097 | } |
3982 | 4098 | ||
4099 | # Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) | ||
4100 | if ($^V && $^V ge 5.10.0 && | ||
4101 | $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) { | ||
4102 | if (WARN("PREFER_ETHER_ADDR_COPY", | ||
4103 | "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) && | ||
4104 | $fix) { | ||
4105 | $fixed[$linenr - 1] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; | ||
4106 | } | ||
4107 | } | ||
4108 | |||
3983 | # typecasts on min/max could be min_t/max_t | 4109 | # typecasts on min/max could be min_t/max_t |
3984 | if ($^V && $^V ge 5.10.0 && | 4110 | if ($^V && $^V ge 5.10.0 && |
3985 | defined $stat && | 4111 | defined $stat && |
@@ -4114,6 +4240,12 @@ sub process { | |||
4114 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); | 4240 | "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); |
4115 | } | 4241 | } |
4116 | 4242 | ||
4243 | # check for GFP_NOWAIT use | ||
4244 | if ($line =~ /\b__GFP_NOFAIL\b/) { | ||
4245 | WARN("__GFP_NOFAIL", | ||
4246 | "Use of __GFP_NOFAIL is deprecated, no new users should be added\n" . $herecurr); | ||
4247 | } | ||
4248 | |||
4117 | # check for multiple semicolons | 4249 | # check for multiple semicolons |
4118 | if ($line =~ /;\s*;\s*$/) { | 4250 | if ($line =~ /;\s*;\s*$/) { |
4119 | if (WARN("ONE_SEMICOLON", | 4251 | if (WARN("ONE_SEMICOLON", |
@@ -4123,6 +4255,31 @@ sub process { | |||
4123 | } | 4255 | } |
4124 | } | 4256 | } |
4125 | 4257 | ||
4258 | # check for case / default statements not preceeded by break/fallthrough/switch | ||
4259 | if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { | ||
4260 | my $has_break = 0; | ||
4261 | my $has_statement = 0; | ||
4262 | my $count = 0; | ||
4263 | my $prevline = $linenr; | ||
4264 | while ($prevline > 1 && $count < 3 && !$has_break) { | ||
4265 | $prevline--; | ||
4266 | my $rline = $rawlines[$prevline - 1]; | ||
4267 | my $fline = $lines[$prevline - 1]; | ||
4268 | last if ($fline =~ /^\@\@/); | ||
4269 | next if ($fline =~ /^\-/); | ||
4270 | next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); | ||
4271 | $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); | ||
4272 | next if ($fline =~ /^.[\s$;]*$/); | ||
4273 | $has_statement = 1; | ||
4274 | $count++; | ||
4275 | $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/); | ||
4276 | } | ||
4277 | if (!$has_break && $has_statement) { | ||
4278 | WARN("MISSING_BREAK", | ||
4279 | "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr); | ||
4280 | } | ||
4281 | } | ||
4282 | |||
4126 | # check for switch/default statements without a break; | 4283 | # check for switch/default statements without a break; |
4127 | if ($^V && $^V ge 5.10.0 && | 4284 | if ($^V && $^V ge 5.10.0 && |
4128 | defined $stat && | 4285 | defined $stat && |
@@ -4358,7 +4515,8 @@ sub process { | |||
4358 | hash_show_words(\%ignore_type, "Ignored"); | 4515 | hash_show_words(\%ignore_type, "Ignored"); |
4359 | 4516 | ||
4360 | if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { | 4517 | if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { |
4361 | my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes"; | 4518 | my $newfile = $filename; |
4519 | $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); | ||
4362 | my $linecount = 0; | 4520 | my $linecount = 0; |
4363 | my $f; | 4521 | my $f; |
4364 | 4522 | ||