diff options
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-x | scripts/checkpatch.pl | 183 |
1 files changed, 158 insertions, 25 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl index a3b9782441f9..de639eeeed50 100755 --- a/scripts/checkpatch.pl +++ b/scripts/checkpatch.pl | |||
@@ -323,17 +323,22 @@ sub build_types { | |||
323 | }x; | 323 | }x; |
324 | $Type = qr{ | 324 | $Type = qr{ |
325 | $NonptrType | 325 | $NonptrType |
326 | (?:[\s\*]+\s*const|[\s\*]+|(?:\s*\[\s*\])+)? | 326 | (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*|\[\])+|(?:\s*\[\s*\])+)? |
327 | (?:\s+$Inline|\s+$Modifier)* | 327 | (?:\s+$Inline|\s+$Modifier)* |
328 | }x; | 328 | }x; |
329 | $Declare = qr{(?:$Storage\s+)?$Type}; | 329 | $Declare = qr{(?:$Storage\s+)?$Type}; |
330 | } | 330 | } |
331 | build_types(); | 331 | build_types(); |
332 | 332 | ||
333 | our $match_balanced_parentheses = qr/(\((?:[^\(\)]+|(-1))*\))/; | ||
334 | 333 | ||
335 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; | 334 | our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; |
336 | our $LvalOrFunc = qr{($Lval)\s*($match_balanced_parentheses{0,1})\s*}; | 335 | |
336 | # Using $balanced_parens, $LvalOrFunc, or $FuncArg | ||
337 | # requires at least perl version v5.10.0 | ||
338 | # Any use must be runtime checked with $^V | ||
339 | |||
340 | our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; | ||
341 | our $LvalOrFunc = qr{($Lval)\s*($balanced_parens{0,1})\s*}; | ||
337 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; | 342 | our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant)}; |
338 | 343 | ||
339 | sub deparenthesize { | 344 | sub deparenthesize { |
@@ -1330,6 +1335,36 @@ sub check_absolute_file { | |||
1330 | } | 1335 | } |
1331 | } | 1336 | } |
1332 | 1337 | ||
1338 | sub pos_last_openparen { | ||
1339 | my ($line) = @_; | ||
1340 | |||
1341 | my $pos = 0; | ||
1342 | |||
1343 | my $opens = $line =~ tr/\(/\(/; | ||
1344 | my $closes = $line =~ tr/\)/\)/; | ||
1345 | |||
1346 | my $last_openparen = 0; | ||
1347 | |||
1348 | if (($opens == 0) || ($closes >= $opens)) { | ||
1349 | return -1; | ||
1350 | } | ||
1351 | |||
1352 | my $len = length($line); | ||
1353 | |||
1354 | for ($pos = 0; $pos < $len; $pos++) { | ||
1355 | my $string = substr($line, $pos); | ||
1356 | if ($string =~ /^($FuncArg|$balanced_parens)/) { | ||
1357 | $pos += length($1) - 1; | ||
1358 | } elsif (substr($line, $pos, 1) eq '(') { | ||
1359 | $last_openparen = $pos; | ||
1360 | } elsif (index($string, '(') == -1) { | ||
1361 | last; | ||
1362 | } | ||
1363 | } | ||
1364 | |||
1365 | return $last_openparen + 1; | ||
1366 | } | ||
1367 | |||
1333 | sub process { | 1368 | sub process { |
1334 | my $filename = shift; | 1369 | my $filename = shift; |
1335 | 1370 | ||
@@ -1737,6 +1772,21 @@ sub process { | |||
1737 | "line over 80 characters\n" . $herecurr); | 1772 | "line over 80 characters\n" . $herecurr); |
1738 | } | 1773 | } |
1739 | 1774 | ||
1775 | # Check for user-visible strings broken across lines, which breaks the ability | ||
1776 | # to grep for the string. Limited to strings used as parameters (those | ||
1777 | # following an open parenthesis), which almost completely eliminates false | ||
1778 | # positives, as well as warning only once per parameter rather than once per | ||
1779 | # line of the string. Make an exception when the previous string ends in a | ||
1780 | # newline (multiple lines in one string constant) or \n\t (common in inline | ||
1781 | # assembly to indent the instruction on the following line). | ||
1782 | if ($line =~ /^\+\s*"/ && | ||
1783 | $prevline =~ /"\s*$/ && | ||
1784 | $prevline =~ /\(/ && | ||
1785 | $prevrawline !~ /\\n(?:\\t)*"\s*$/) { | ||
1786 | WARN("SPLIT_STRING", | ||
1787 | "quoted string split across lines\n" . $hereprev); | ||
1788 | } | ||
1789 | |||
1740 | # check for spaces before a quoted newline | 1790 | # check for spaces before a quoted newline |
1741 | if ($rawline =~ /^.*\".*\s\\n/) { | 1791 | if ($rawline =~ /^.*\".*\s\\n/) { |
1742 | WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", | 1792 | WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", |
@@ -1783,6 +1833,48 @@ sub process { | |||
1783 | "please, no space before tabs\n" . $herevet); | 1833 | "please, no space before tabs\n" . $herevet); |
1784 | } | 1834 | } |
1785 | 1835 | ||
1836 | # check for && or || at the start of a line | ||
1837 | if ($rawline =~ /^\+\s*(&&|\|\|)/) { | ||
1838 | CHK("LOGICAL_CONTINUATIONS", | ||
1839 | "Logical continuations should be on the previous line\n" . $hereprev); | ||
1840 | } | ||
1841 | |||
1842 | # check multi-line statement indentation matches previous line | ||
1843 | if ($^V && $^V ge 5.10.0 && | ||
1844 | $prevline =~ /^\+(\t*)(if \(|$Ident\().*(\&\&|\|\||,)\s*$/) { | ||
1845 | $prevline =~ /^\+(\t*)(.*)$/; | ||
1846 | my $oldindent = $1; | ||
1847 | my $rest = $2; | ||
1848 | |||
1849 | my $pos = pos_last_openparen($rest); | ||
1850 | if ($pos >= 0) { | ||
1851 | $line =~ /^\+([ \t]*)/; | ||
1852 | my $newindent = $1; | ||
1853 | |||
1854 | my $goodtabindent = $oldindent . | ||
1855 | "\t" x ($pos / 8) . | ||
1856 | " " x ($pos % 8); | ||
1857 | my $goodspaceindent = $oldindent . " " x $pos; | ||
1858 | |||
1859 | if ($newindent ne $goodtabindent && | ||
1860 | $newindent ne $goodspaceindent) { | ||
1861 | CHK("PARENTHESIS_ALIGNMENT", | ||
1862 | "Alignment should match open parenthesis\n" . $hereprev); | ||
1863 | } | ||
1864 | } | ||
1865 | } | ||
1866 | |||
1867 | if ($line =~ /^\+.*\*[ \t]*\)[ \t]+/) { | ||
1868 | CHK("SPACING", | ||
1869 | "No space is necessary after a cast\n" . $hereprev); | ||
1870 | } | ||
1871 | |||
1872 | if ($rawline =~ /^\+[ \t]*\/\*[ \t]*$/ && | ||
1873 | $prevrawline =~ /^\+[ \t]*$/) { | ||
1874 | CHK("BLOCK_COMMENT_STYLE", | ||
1875 | "Don't begin block comments with only a /* line, use /* comment...\n" . $hereprev); | ||
1876 | } | ||
1877 | |||
1786 | # check for spaces at the beginning of a line. | 1878 | # check for spaces at the beginning of a line. |
1787 | # Exceptions: | 1879 | # Exceptions: |
1788 | # 1) within comments | 1880 | # 1) within comments |
@@ -2325,7 +2417,7 @@ sub process { | |||
2325 | my ($where, $prefix) = ($-[1], $1); | 2417 | my ($where, $prefix) = ($-[1], $1); |
2326 | if ($prefix !~ /$Type\s+$/ && | 2418 | if ($prefix !~ /$Type\s+$/ && |
2327 | ($where != 0 || $prefix !~ /^.\s+$/) && | 2419 | ($where != 0 || $prefix !~ /^.\s+$/) && |
2328 | $prefix !~ /{\s+$/) { | 2420 | $prefix !~ /[{,]\s+$/) { |
2329 | ERROR("BRACKET_SPACE", | 2421 | ERROR("BRACKET_SPACE", |
2330 | "space prohibited before open square bracket '['\n" . $herecurr); | 2422 | "space prohibited before open square bracket '['\n" . $herecurr); |
2331 | } | 2423 | } |
@@ -2828,6 +2920,12 @@ sub process { | |||
2828 | { | 2920 | { |
2829 | } | 2921 | } |
2830 | 2922 | ||
2923 | # Flatten any obvious string concatentation. | ||
2924 | while ($dstat =~ s/("X*")\s*$Ident/$1/ || | ||
2925 | $dstat =~ s/$Ident\s*("X*")/$1/) | ||
2926 | { | ||
2927 | } | ||
2928 | |||
2831 | my $exceptions = qr{ | 2929 | my $exceptions = qr{ |
2832 | $Declare| | 2930 | $Declare| |
2833 | module_param_named| | 2931 | module_param_named| |
@@ -2844,7 +2942,8 @@ sub process { | |||
2844 | if ($dstat ne '' && | 2942 | if ($dstat ne '' && |
2845 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), | 2943 | $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), |
2846 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); | 2944 | $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); |
2847 | $dstat !~ /^(?:$Ident|-?$Constant)$/ && # 10 // foo() | 2945 | $dstat !~ /^[!~-]?(?:$Ident|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo |
2946 | $dstat !~ /^'X'$/ && # character constants | ||
2848 | $dstat !~ /$exceptions/ && | 2947 | $dstat !~ /$exceptions/ && |
2849 | $dstat !~ /^\.$Ident\s*=/ && # .foo = | 2948 | $dstat !~ /^\.$Ident\s*=/ && # .foo = |
2850 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) | 2949 | $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) |
@@ -2888,7 +2987,8 @@ sub process { | |||
2888 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; | 2987 | #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; |
2889 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; | 2988 | #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; |
2890 | if ($#chunks > 0 && $level == 0) { | 2989 | if ($#chunks > 0 && $level == 0) { |
2891 | my $allowed = 0; | 2990 | my @allowed = (); |
2991 | my $allow = 0; | ||
2892 | my $seen = 0; | 2992 | my $seen = 0; |
2893 | my $herectx = $here . "\n"; | 2993 | my $herectx = $here . "\n"; |
2894 | my $ln = $linenr - 1; | 2994 | my $ln = $linenr - 1; |
@@ -2899,6 +2999,7 @@ sub process { | |||
2899 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); | 2999 | my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); |
2900 | my $offset = statement_rawlines($whitespace) - 1; | 3000 | my $offset = statement_rawlines($whitespace) - 1; |
2901 | 3001 | ||
3002 | $allowed[$allow] = 0; | ||
2902 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; | 3003 | #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; |
2903 | 3004 | ||
2904 | # We have looked at and allowed this specific line. | 3005 | # We have looked at and allowed this specific line. |
@@ -2911,23 +3012,34 @@ sub process { | |||
2911 | 3012 | ||
2912 | $seen++ if ($block =~ /^\s*{/); | 3013 | $seen++ if ($block =~ /^\s*{/); |
2913 | 3014 | ||
2914 | #print "cond<$cond> block<$block> allowed<$allowed>\n"; | 3015 | #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; |
2915 | if (statement_lines($cond) > 1) { | 3016 | if (statement_lines($cond) > 1) { |
2916 | #print "APW: ALLOWED: cond<$cond>\n"; | 3017 | #print "APW: ALLOWED: cond<$cond>\n"; |
2917 | $allowed = 1; | 3018 | $allowed[$allow] = 1; |
2918 | } | 3019 | } |
2919 | if ($block =~/\b(?:if|for|while)\b/) { | 3020 | if ($block =~/\b(?:if|for|while)\b/) { |
2920 | #print "APW: ALLOWED: block<$block>\n"; | 3021 | #print "APW: ALLOWED: block<$block>\n"; |
2921 | $allowed = 1; | 3022 | $allowed[$allow] = 1; |
2922 | } | 3023 | } |
2923 | if (statement_block_size($block) > 1) { | 3024 | if (statement_block_size($block) > 1) { |
2924 | #print "APW: ALLOWED: lines block<$block>\n"; | 3025 | #print "APW: ALLOWED: lines block<$block>\n"; |
2925 | $allowed = 1; | 3026 | $allowed[$allow] = 1; |
2926 | } | 3027 | } |
3028 | $allow++; | ||
2927 | } | 3029 | } |
2928 | if ($seen && !$allowed) { | 3030 | if ($seen) { |
2929 | WARN("BRACES", | 3031 | my $sum_allowed = 0; |
2930 | "braces {} are not necessary for any arm of this statement\n" . $herectx); | 3032 | foreach (@allowed) { |
3033 | $sum_allowed += $_; | ||
3034 | } | ||
3035 | if ($sum_allowed == 0) { | ||
3036 | WARN("BRACES", | ||
3037 | "braces {} are not necessary for any arm of this statement\n" . $herectx); | ||
3038 | } elsif ($sum_allowed != $allow && | ||
3039 | $seen != $allow) { | ||
3040 | CHK("BRACES", | ||
3041 | "braces {} should be used on all arms of this statement\n" . $herectx); | ||
3042 | } | ||
2931 | } | 3043 | } |
2932 | } | 3044 | } |
2933 | } | 3045 | } |
@@ -3123,6 +3235,12 @@ sub process { | |||
3123 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr); | 3235 | "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr); |
3124 | } | 3236 | } |
3125 | 3237 | ||
3238 | # Check for __attribute__ format(scanf, prefer __scanf | ||
3239 | if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { | ||
3240 | WARN("PREFER_SCANF", | ||
3241 | "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr); | ||
3242 | } | ||
3243 | |||
3126 | # check for sizeof(&) | 3244 | # check for sizeof(&) |
3127 | if ($line =~ /\bsizeof\s*\(\s*\&/) { | 3245 | if ($line =~ /\bsizeof\s*\(\s*\&/) { |
3128 | WARN("SIZEOF_ADDRESS", | 3246 | WARN("SIZEOF_ADDRESS", |
@@ -3136,12 +3254,13 @@ sub process { | |||
3136 | } | 3254 | } |
3137 | 3255 | ||
3138 | # Check for misused memsets | 3256 | # Check for misused memsets |
3139 | if (defined $stat && | 3257 | if ($^V && $^V ge 5.10.0 && |
3258 | defined $stat && | ||
3140 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { | 3259 | $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { |
3141 | 3260 | ||
3142 | my $ms_addr = $2; | 3261 | my $ms_addr = $2; |
3143 | my $ms_val = $8; | 3262 | my $ms_val = $7; |
3144 | my $ms_size = $14; | 3263 | my $ms_size = $12; |
3145 | 3264 | ||
3146 | if ($ms_size =~ /^(0x|)0$/i) { | 3265 | if ($ms_size =~ /^(0x|)0$/i) { |
3147 | ERROR("MEMSET", | 3266 | ERROR("MEMSET", |
@@ -3153,17 +3272,18 @@ sub process { | |||
3153 | } | 3272 | } |
3154 | 3273 | ||
3155 | # typecasts on min/max could be min_t/max_t | 3274 | # typecasts on min/max could be min_t/max_t |
3156 | if (defined $stat && | 3275 | if ($^V && $^V ge 5.10.0 && |
3276 | defined $stat && | ||
3157 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { | 3277 | $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { |
3158 | if (defined $2 || defined $8) { | 3278 | if (defined $2 || defined $7) { |
3159 | my $call = $1; | 3279 | my $call = $1; |
3160 | my $cast1 = deparenthesize($2); | 3280 | my $cast1 = deparenthesize($2); |
3161 | my $arg1 = $3; | 3281 | my $arg1 = $3; |
3162 | my $cast2 = deparenthesize($8); | 3282 | my $cast2 = deparenthesize($7); |
3163 | my $arg2 = $9; | 3283 | my $arg2 = $8; |
3164 | my $cast; | 3284 | my $cast; |
3165 | 3285 | ||
3166 | if ($cast1 ne "" && $cast2 ne "") { | 3286 | if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { |
3167 | $cast = "$cast1 or $cast2"; | 3287 | $cast = "$cast1 or $cast2"; |
3168 | } elsif ($cast1 ne "") { | 3288 | } elsif ($cast1 ne "") { |
3169 | $cast = $cast1; | 3289 | $cast = $cast1; |
@@ -3233,22 +3353,30 @@ sub process { | |||
3233 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); | 3353 | "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr); |
3234 | } | 3354 | } |
3235 | 3355 | ||
3356 | # check for use of yield() | ||
3357 | if ($line =~ /\byield\s*\(\s*\)/) { | ||
3358 | WARN("YIELD", | ||
3359 | "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); | ||
3360 | } | ||
3361 | |||
3236 | # check for semaphores initialized locked | 3362 | # check for semaphores initialized locked |
3237 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { | 3363 | if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { |
3238 | WARN("CONSIDER_COMPLETION", | 3364 | WARN("CONSIDER_COMPLETION", |
3239 | "consider using a completion\n" . $herecurr); | 3365 | "consider using a completion\n" . $herecurr); |
3240 | |||
3241 | } | 3366 | } |
3367 | |||
3242 | # recommend kstrto* over simple_strto* and strict_strto* | 3368 | # recommend kstrto* over simple_strto* and strict_strto* |
3243 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { | 3369 | if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { |
3244 | WARN("CONSIDER_KSTRTO", | 3370 | WARN("CONSIDER_KSTRTO", |
3245 | "$1 is obsolete, use k$3 instead\n" . $herecurr); | 3371 | "$1 is obsolete, use k$3 instead\n" . $herecurr); |
3246 | } | 3372 | } |
3373 | |||
3247 | # check for __initcall(), use device_initcall() explicitly please | 3374 | # check for __initcall(), use device_initcall() explicitly please |
3248 | if ($line =~ /^.\s*__initcall\s*\(/) { | 3375 | if ($line =~ /^.\s*__initcall\s*\(/) { |
3249 | WARN("USE_DEVICE_INITCALL", | 3376 | WARN("USE_DEVICE_INITCALL", |
3250 | "please use device_initcall() instead of __initcall()\n" . $herecurr); | 3377 | "please use device_initcall() instead of __initcall()\n" . $herecurr); |
3251 | } | 3378 | } |
3379 | |||
3252 | # check for various ops structs, ensure they are const. | 3380 | # check for various ops structs, ensure they are const. |
3253 | my $struct_ops = qr{acpi_dock_ops| | 3381 | my $struct_ops = qr{acpi_dock_ops| |
3254 | address_space_operations| | 3382 | address_space_operations| |
@@ -3385,6 +3513,12 @@ sub process { | |||
3385 | } | 3513 | } |
3386 | 3514 | ||
3387 | if ($quiet == 0) { | 3515 | if ($quiet == 0) { |
3516 | |||
3517 | if ($^V lt 5.10.0) { | ||
3518 | print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); | ||
3519 | print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); | ||
3520 | } | ||
3521 | |||
3388 | # If there were whitespace errors which cleanpatch can fix | 3522 | # If there were whitespace errors which cleanpatch can fix |
3389 | # then suggest that. | 3523 | # then suggest that. |
3390 | if ($rpt_cleaners) { | 3524 | if ($rpt_cleaners) { |
@@ -3394,13 +3528,12 @@ sub process { | |||
3394 | } | 3528 | } |
3395 | } | 3529 | } |
3396 | 3530 | ||
3397 | if (keys %ignore_type) { | 3531 | if ($quiet == 0 && keys %ignore_type) { |
3398 | print "NOTE: Ignored message types:"; | 3532 | print "NOTE: Ignored message types:"; |
3399 | foreach my $ignore (sort keys %ignore_type) { | 3533 | foreach my $ignore (sort keys %ignore_type) { |
3400 | print " $ignore"; | 3534 | print " $ignore"; |
3401 | } | 3535 | } |
3402 | print "\n"; | 3536 | print "\n\n"; |
3403 | print "\n" if ($quiet == 0); | ||
3404 | } | 3537 | } |
3405 | 3538 | ||
3406 | if ($clean == 1 && $quiet == 0) { | 3539 | if ($clean == 1 && $quiet == 0) { |