aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl180
1 files changed, 163 insertions, 17 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 9fb30b15c9dc..1dbd6d1cd1b5 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -29,6 +29,7 @@ my $mailback = 0;
29my $summary_file = 0; 29my $summary_file = 0;
30my $show_types = 0; 30my $show_types = 0;
31my $fix = 0; 31my $fix = 0;
32my $fix_inplace = 0;
32my $root; 33my $root;
33my %debug; 34my %debug;
34my %camelcase = (); 35my %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
141help(0) if ($help); 146help(0) if ($help);
142 147
148$fix = 1 if ($fix_inplace);
149
143my $exit = 0; 150my $exit = 0;
144 151
145if ($^V && $^V lt $minimum_perl_version) { 152if ($^V && $^V lt $minimum_perl_version) {
@@ -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
@@ -2805,6 +2836,65 @@ sub process {
2805 } 2836 }
2806 } 2837 }
2807 2838
2839# Function pointer declarations
2840# check spacing between type, funcptr, and args
2841# canonical declaration is "type (*funcptr)(args...)"
2842#
2843# the $Declare variable will capture all spaces after the type
2844# so check it for trailing missing spaces or multiple spaces
2845 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)$Ident(\s*)\)(\s*)\(/) {
2846 my $declare = $1;
2847 my $pre_pointer_space = $2;
2848 my $post_pointer_space = $3;
2849 my $funcname = $4;
2850 my $post_funcname_space = $5;
2851 my $pre_args_space = $6;
2852
2853 if ($declare !~ /\s$/) {
2854 WARN("SPACING",
2855 "missing space after return type\n" . $herecurr);
2856 }
2857
2858# unnecessary space "type (*funcptr)(args...)"
2859 elsif ($declare =~ /\s{2,}$/) {
2860 WARN("SPACING",
2861 "Multiple spaces after return type\n" . $herecurr);
2862 }
2863
2864# unnecessary space "type ( *funcptr)(args...)"
2865 if (defined $pre_pointer_space &&
2866 $pre_pointer_space =~ /^\s/) {
2867 WARN("SPACING",
2868 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
2869 }
2870
2871# unnecessary space "type (* funcptr)(args...)"
2872 if (defined $post_pointer_space &&
2873 $post_pointer_space =~ /^\s/) {
2874 WARN("SPACING",
2875 "Unnecessary space before function pointer name\n" . $herecurr);
2876 }
2877
2878# unnecessary space "type (*funcptr )(args...)"
2879 if (defined $post_funcname_space &&
2880 $post_funcname_space =~ /^\s/) {
2881 WARN("SPACING",
2882 "Unnecessary space after function pointer name\n" . $herecurr);
2883 }
2884
2885# unnecessary space "type (*funcptr) (args...)"
2886 if (defined $pre_args_space &&
2887 $pre_args_space =~ /^\s/) {
2888 WARN("SPACING",
2889 "Unnecessary space before function pointer arguments\n" . $herecurr);
2890 }
2891
2892 if (show_type("SPACING") && $fix) {
2893 $fixed[$linenr - 1] =~
2894 s/^(.\s*$Declare)\(\s*\*\s*($Ident)\s*\)\s*\(/rtrim($1) . " " . "\(\*$2\)\("/ex;
2895 }
2896 }
2897
2808# check for spacing round square brackets; allowed: 2898# check for spacing round square brackets; allowed:
2809# 1. with a type on the left -- int [] a; 2899# 1. with a type on the left -- int [] a;
2810# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 2900# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
@@ -3125,7 +3215,7 @@ sub process {
3125 } 3215 }
3126 3216
3127# check for whitespace before a non-naked semicolon 3217# check for whitespace before a non-naked semicolon
3128 if ($line =~ /^\+.*\S\s+;/) { 3218 if ($line =~ /^\+.*\S\s+;\s*$/) {
3129 if (WARN("SPACING", 3219 if (WARN("SPACING",
3130 "space prohibited before semicolon\n" . $herecurr) && 3220 "space prohibited before semicolon\n" . $herecurr) &&
3131 $fix) { 3221 $fix) {
@@ -3249,6 +3339,20 @@ sub process {
3249 } 3339 }
3250 } 3340 }
3251 3341
3342# if statements using unnecessary parentheses - ie: if ((foo == bar))
3343 if ($^V && $^V ge 5.10.0 &&
3344 $line =~ /\bif\s*((?:\(\s*){2,})/) {
3345 my $openparens = $1;
3346 my $count = $openparens =~ tr@\(@\(@;
3347 my $msg = "";
3348 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
3349 my $comp = $4; #Not $1 because of $LvalOrFunc
3350 $msg = " - maybe == should be = ?" if ($comp eq "==");
3351 WARN("UNNECESSARY_PARENTHESES",
3352 "Unnecessary parentheses$msg\n" . $herecurr);
3353 }
3354 }
3355
3252# Return of what appears to be an errno should normally be -'ve 3356# Return of what appears to be an errno should normally be -'ve
3253 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 3357 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3254 my $name = $1; 3358 my $name = $1;
@@ -3983,6 +4087,16 @@ sub process {
3983 } 4087 }
3984 } 4088 }
3985 4089
4090# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
4091 if ($^V && $^V ge 5.10.0 &&
4092 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) {
4093 if (WARN("PREFER_ETHER_ADDR_COPY",
4094 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) &&
4095 $fix) {
4096 $fixed[$linenr - 1] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
4097 }
4098 }
4099
3986# typecasts on min/max could be min_t/max_t 4100# typecasts on min/max could be min_t/max_t
3987 if ($^V && $^V ge 5.10.0 && 4101 if ($^V && $^V ge 5.10.0 &&
3988 defined $stat && 4102 defined $stat &&
@@ -4117,6 +4231,12 @@ sub process {
4117 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 4231 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
4118 } 4232 }
4119 4233
4234# check for GFP_NOWAIT use
4235 if ($line =~ /\b__GFP_NOFAIL\b/) {
4236 WARN("__GFP_NOFAIL",
4237 "Use of __GFP_NOFAIL is deprecated, no new users should be added\n" . $herecurr);
4238 }
4239
4120# check for multiple semicolons 4240# check for multiple semicolons
4121 if ($line =~ /;\s*;\s*$/) { 4241 if ($line =~ /;\s*;\s*$/) {
4122 if (WARN("ONE_SEMICOLON", 4242 if (WARN("ONE_SEMICOLON",
@@ -4126,6 +4246,31 @@ sub process {
4126 } 4246 }
4127 } 4247 }
4128 4248
4249# check for case / default statements not preceeded by break/fallthrough/switch
4250 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4251 my $has_break = 0;
4252 my $has_statement = 0;
4253 my $count = 0;
4254 my $prevline = $linenr;
4255 while ($prevline > 1 && $count < 3 && !$has_break) {
4256 $prevline--;
4257 my $rline = $rawlines[$prevline - 1];
4258 my $fline = $lines[$prevline - 1];
4259 last if ($fline =~ /^\@\@/);
4260 next if ($fline =~ /^\-/);
4261 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
4262 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
4263 next if ($fline =~ /^.[\s$;]*$/);
4264 $has_statement = 1;
4265 $count++;
4266 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
4267 }
4268 if (!$has_break && $has_statement) {
4269 WARN("MISSING_BREAK",
4270 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
4271 }
4272 }
4273
4129# check for switch/default statements without a break; 4274# check for switch/default statements without a break;
4130 if ($^V && $^V ge 5.10.0 && 4275 if ($^V && $^V ge 5.10.0 &&
4131 defined $stat && 4276 defined $stat &&
@@ -4361,7 +4506,8 @@ sub process {
4361 hash_show_words(\%ignore_type, "Ignored"); 4506 hash_show_words(\%ignore_type, "Ignored");
4362 4507
4363 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") { 4508 if ($clean == 0 && $fix && "@rawlines" ne "@fixed") {
4364 my $newfile = $filename . ".EXPERIMENTAL-checkpatch-fixes"; 4509 my $newfile = $filename;
4510 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
4365 my $linecount = 0; 4511 my $linecount = 0;
4366 my $f; 4512 my $f;
4367 4513