aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl164
1 files changed, 141 insertions, 23 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index d12435992dea..89b1df4e72ab 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -47,6 +47,8 @@ my $ignore_perl_version = 0;
47my $minimum_perl_version = 5.10.0; 47my $minimum_perl_version = 5.10.0;
48my $min_conf_desc_length = 4; 48my $min_conf_desc_length = 4;
49my $spelling_file = "$D/spelling.txt"; 49my $spelling_file = "$D/spelling.txt";
50my $codespell = 0;
51my $codespellfile = "/usr/local/share/codespell/dictionary.txt";
50 52
51sub help { 53sub help {
52 my ($exitcode) = @_; 54 my ($exitcode) = @_;
@@ -88,6 +90,9 @@ Options:
88 file. It's your fault if there's no backup or git 90 file. It's your fault if there's no backup or git
89 --ignore-perl-version override checking of perl version. expect 91 --ignore-perl-version override checking of perl version. expect
90 runtime errors. 92 runtime errors.
93 --codespell Use the codespell dictionary for spelling/typos
94 (default:/usr/local/share/codespell/dictionary.txt)
95 --codespellfile Use this codespell dictionary
91 -h, --help, --version display this help and exit 96 -h, --help, --version display this help and exit
92 97
93When FILE is - read standard input. 98When FILE is - read standard input.
@@ -146,6 +151,8 @@ GetOptions(
146 'ignore-perl-version!' => \$ignore_perl_version, 151 'ignore-perl-version!' => \$ignore_perl_version,
147 'debug=s' => \%debug, 152 'debug=s' => \%debug,
148 'test-only=s' => \$tst_only, 153 'test-only=s' => \$tst_only,
154 'codespell!' => \$codespell,
155 'codespellfile=s' => \$codespellfile,
149 'h|help' => \$help, 156 'h|help' => \$help,
150 'version' => \$help 157 'version' => \$help
151) or help(1); 158) or help(1);
@@ -316,6 +323,7 @@ our $Operators = qr{
316 323
317our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 324our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
318 325
326our $BasicType;
319our $NonptrType; 327our $NonptrType;
320our $NonptrTypeMisordered; 328our $NonptrTypeMisordered;
321our $NonptrTypeWithAttr; 329our $NonptrTypeWithAttr;
@@ -436,6 +444,14 @@ foreach my $entry (@mode_permission_funcs) {
436 $mode_perms_search .= $entry->[0]; 444 $mode_perms_search .= $entry->[0];
437} 445}
438 446
447our $mode_perms_world_writable = qr{
448 S_IWUGO |
449 S_IWOTH |
450 S_IRWXUGO |
451 S_IALLUGO |
452 0[0-7][0-7][2367]
453}x;
454
439our $allowed_asm_includes = qr{(?x: 455our $allowed_asm_includes = qr{(?x:
440 irq| 456 irq|
441 memory| 457 memory|
@@ -449,7 +465,6 @@ my $misspellings;
449my %spelling_fix; 465my %spelling_fix;
450 466
451if (open(my $spelling, '<', $spelling_file)) { 467if (open(my $spelling, '<', $spelling_file)) {
452 my @spelling_list;
453 while (<$spelling>) { 468 while (<$spelling>) {
454 my $line = $_; 469 my $line = $_;
455 470
@@ -461,21 +476,50 @@ if (open(my $spelling, '<', $spelling_file)) {
461 476
462 my ($suspect, $fix) = split(/\|\|/, $line); 477 my ($suspect, $fix) = split(/\|\|/, $line);
463 478
464 push(@spelling_list, $suspect);
465 $spelling_fix{$suspect} = $fix; 479 $spelling_fix{$suspect} = $fix;
466 } 480 }
467 close($spelling); 481 close($spelling);
468 $misspellings = join("|", @spelling_list);
469} else { 482} else {
470 warn "No typos will be found - file '$spelling_file': $!\n"; 483 warn "No typos will be found - file '$spelling_file': $!\n";
471} 484}
472 485
486if ($codespell) {
487 if (open(my $spelling, '<', $codespellfile)) {
488 while (<$spelling>) {
489 my $line = $_;
490
491 $line =~ s/\s*\n?$//g;
492 $line =~ s/^\s*//g;
493
494 next if ($line =~ m/^\s*#/);
495 next if ($line =~ m/^\s*$/);
496 next if ($line =~ m/, disabled/i);
497
498 $line =~ s/,.*$//;
499
500 my ($suspect, $fix) = split(/->/, $line);
501
502 $spelling_fix{$suspect} = $fix;
503 }
504 close($spelling);
505 } else {
506 warn "No codespell typos will be found - file '$codespellfile': $!\n";
507 }
508}
509
510$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
511
473sub build_types { 512sub build_types {
474 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 513 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
475 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 514 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
476 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 515 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
477 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 516 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
478 $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 517 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
518 $BasicType = qr{
519 (?:$typeOtherOSTypedefs\b)|
520 (?:$typeTypedefs\b)|
521 (?:${all}\b)
522 }x;
479 $NonptrType = qr{ 523 $NonptrType = qr{
480 (?:$Modifier\s+|const\s+)* 524 (?:$Modifier\s+|const\s+)*
481 (?: 525 (?:
@@ -1646,7 +1690,7 @@ sub fix_inserted_deleted_lines {
1646 foreach my $old_line (@{$linesRef}) { 1690 foreach my $old_line (@{$linesRef}) {
1647 my $save_line = 1; 1691 my $save_line = 1;
1648 my $line = $old_line; #don't modify the array 1692 my $line = $old_line; #don't modify the array
1649 if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename 1693 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
1650 $delta_offset = 0; 1694 $delta_offset = 0;
1651 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 1695 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1652 $range_last_linenr = $new_linenr; 1696 $range_last_linenr = $new_linenr;
@@ -1854,6 +1898,7 @@ sub process {
1854 1898
1855 my $in_header_lines = $file ? 0 : 1; 1899 my $in_header_lines = $file ? 0 : 1;
1856 my $in_commit_log = 0; #Scanning lines before patch 1900 my $in_commit_log = 0; #Scanning lines before patch
1901 my $commit_log_long_line = 0;
1857 my $reported_maintainer_file = 0; 1902 my $reported_maintainer_file = 0;
1858 my $non_utf8_charset = 0; 1903 my $non_utf8_charset = 0;
1859 1904
@@ -2189,6 +2234,14 @@ sub process {
2189 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 2234 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2190 } 2235 }
2191 2236
2237# Check for line lengths > 75 in commit log, warn once
2238 if ($in_commit_log && !$commit_log_long_line &&
2239 length($line) > 75) {
2240 WARN("COMMIT_LOG_LONG_LINE",
2241 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2242 $commit_log_long_line = 1;
2243 }
2244
2192# Check for git id commit length and improperly formed commit descriptions 2245# Check for git id commit length and improperly formed commit descriptions
2193 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) { 2246 if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) {
2194 my $init_char = $1; 2247 my $init_char = $1;
@@ -2303,8 +2356,9 @@ sub process {
2303 } 2356 }
2304 2357
2305# Check for various typo / spelling mistakes 2358# Check for various typo / spelling mistakes
2306 if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) { 2359 if (defined($misspellings) &&
2307 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) { 2360 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2361 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
2308 my $typo = $1; 2362 my $typo = $1;
2309 my $typo_fix = $spelling_fix{lc($typo)}; 2363 my $typo_fix = $spelling_fix{lc($typo)};
2310 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 2364 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
@@ -2459,8 +2513,9 @@ sub process {
2459#line length limit 2513#line length limit
2460 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 2514 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
2461 $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 2515 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
2462 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || 2516 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?$String\s*(?:|,|\)\s*;)\s*$/ ||
2463 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 2517 $line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2518 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) &&
2464 $length > $max_line_length) 2519 $length > $max_line_length)
2465 { 2520 {
2466 WARN("LONG_LINE", 2521 WARN("LONG_LINE",
@@ -2552,8 +2607,15 @@ sub process {
2552 } 2607 }
2553 } 2608 }
2554 2609
2555 if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;:\?\(\{\}\[\<\>]|&&|\|\||\\$)/ && 2610# check for space after cast like "(int) foo" or "(struct foo) bar"
2556 (!defined($1) || $1 !~ /sizeof\s*/)) { 2611# avoid checking a few false positives:
2612# "sizeof(<type>)" or "__alignof__(<type>)"
2613# function pointer declarations like "(*foo)(int) = bar;"
2614# structure definitions like "(struct foo) { 0 };"
2615# multiline macros that define functions
2616# known attributes or the __attribute__ keyword
2617 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2618 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
2557 if (CHK("SPACING", 2619 if (CHK("SPACING",
2558 "No space is necessary after a cast\n" . $herecurr) && 2620 "No space is necessary after a cast\n" . $herecurr) &&
2559 $fix) { 2621 $fix) {
@@ -3146,6 +3208,18 @@ sub process {
3146 $herecurr); 3208 $herecurr);
3147 } 3209 }
3148 3210
3211# check for const <foo> const where <foo> is not a pointer or array type
3212 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3213 my $found = $1;
3214 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3215 WARN("CONST_CONST",
3216 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3217 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3218 WARN("CONST_CONST",
3219 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3220 }
3221 }
3222
3149# check for non-global char *foo[] = {"bar", ...} declarations. 3223# check for non-global char *foo[] = {"bar", ...} declarations.
3150 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 3224 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3151 WARN("STATIC_CONST_CHAR_ARRAY", 3225 WARN("STATIC_CONST_CHAR_ARRAY",
@@ -3153,6 +3227,19 @@ sub process {
3153 $herecurr); 3227 $herecurr);
3154 } 3228 }
3155 3229
3230# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3231 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3232 my $array = $1;
3233 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3234 my $array_div = $1;
3235 if (WARN("ARRAY_SIZE",
3236 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3237 $fix) {
3238 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3239 }
3240 }
3241 }
3242
3156# check for function declarations without arguments like "int foo()" 3243# check for function declarations without arguments like "int foo()"
3157 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 3244 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3158 if (ERROR("FUNCTION_WITHOUT_ARGS", 3245 if (ERROR("FUNCTION_WITHOUT_ARGS",
@@ -3309,6 +3396,14 @@ sub process {
3309 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 3396 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3310 } 3397 }
3311 3398
3399# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3400# number of false positives, but assembly files are not checked, so at
3401# least the arch entry code will not trigger this warning.
3402 if ($line =~ /\bENOSYS\b/) {
3403 WARN("ENOSYS",
3404 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3405 }
3406
3312# function brace can't be on same line, except for #defines of do while, 3407# function brace can't be on same line, except for #defines of do while,
3313# or if closed on same line 3408# or if closed on same line
3314 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and 3409 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
@@ -3565,7 +3660,7 @@ sub process {
3565 3660
3566 # Ignore operators passed as parameters. 3661 # Ignore operators passed as parameters.
3567 if ($op_type ne 'V' && 3662 if ($op_type ne 'V' &&
3568 $ca =~ /\s$/ && $cc =~ /^\s*,/) { 3663 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
3569 3664
3570# # Ignore comments 3665# # Ignore comments
3571# } elsif ($op =~ /^$;+$/) { 3666# } elsif ($op =~ /^$;+$/) {
@@ -3750,6 +3845,14 @@ sub process {
3750 $ok = 1; 3845 $ok = 1;
3751 } 3846 }
3752 3847
3848 # for asm volatile statements
3849 # ignore a colon with another
3850 # colon immediately before or after
3851 if (($op eq ':') &&
3852 ($ca =~ /:$/ || $cc =~ /^:/)) {
3853 $ok = 1;
3854 }
3855
3753 # messages are ERROR, but ?: are CHK 3856 # messages are ERROR, but ?: are CHK
3754 if ($ok == 0) { 3857 if ($ok == 0) {
3755 my $msg_type = \&ERROR; 3858 my $msg_type = \&ERROR;
@@ -3963,12 +4066,12 @@ sub process {
3963 } 4066 }
3964 } 4067 }
3965 4068
3966# Return of what appears to be an errno should normally be -'ve 4069# Return of what appears to be an errno should normally be negative
3967 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 4070 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
3968 my $name = $1; 4071 my $name = $1;
3969 if ($name ne 'EOF' && $name ne 'ERROR') { 4072 if ($name ne 'EOF' && $name ne 'ERROR') {
3970 WARN("USE_NEGATIVE_ERRNO", 4073 WARN("USE_NEGATIVE_ERRNO",
3971 "return of an errno should typically be -ve (return -$1)\n" . $herecurr); 4074 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
3972 } 4075 }
3973 } 4076 }
3974 4077
@@ -4178,7 +4281,8 @@ sub process {
4178 } 4281 }
4179 } 4282 }
4180 4283
4181#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 4284# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4285# itself <asm/foo.h> (uses RAW line)
4182 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 4286 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4183 my $file = "$1.h"; 4287 my $file = "$1.h";
4184 my $checkfile = "include/linux/$file"; 4288 my $checkfile = "include/linux/$file";
@@ -4186,12 +4290,15 @@ sub process {
4186 $realfile ne $checkfile && 4290 $realfile ne $checkfile &&
4187 $1 !~ /$allowed_asm_includes/) 4291 $1 !~ /$allowed_asm_includes/)
4188 { 4292 {
4189 if ($realfile =~ m{^arch/}) { 4293 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4190 CHK("ARCH_INCLUDE_LINUX", 4294 if ($asminclude > 0) {
4191 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4295 if ($realfile =~ m{^arch/}) {
4192 } else { 4296 CHK("ARCH_INCLUDE_LINUX",
4193 WARN("INCLUDE_LINUX", 4297 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4194 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4298 } else {
4299 WARN("INCLUDE_LINUX",
4300 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4301 }
4195 } 4302 }
4196 } 4303 }
4197 } 4304 }
@@ -4700,6 +4807,16 @@ sub process {
4700 } 4807 }
4701 } 4808 }
4702 4809
4810# check for __read_mostly with const non-pointer (should just be const)
4811 if ($line =~ /\b__read_mostly\b/ &&
4812 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
4813 if (ERROR("CONST_READ_MOSTLY",
4814 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
4815 $fix) {
4816 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
4817 }
4818 }
4819
4703# don't use __constant_<foo> functions outside of include/uapi/ 4820# don't use __constant_<foo> functions outside of include/uapi/
4704 if ($realfile !~ m@^include/uapi/@ && 4821 if ($realfile !~ m@^include/uapi/@ &&
4705 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 4822 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
@@ -5261,6 +5378,7 @@ sub process {
5261 stacktrace_ops| 5378 stacktrace_ops|
5262 sysfs_ops| 5379 sysfs_ops|
5263 tty_operations| 5380 tty_operations|
5381 uart_ops|
5264 usb_mon_operations| 5382 usb_mon_operations|
5265 wd_ops}x; 5383 wd_ops}x;
5266 if ($line !~ /\bconst\b/ && 5384 if ($line !~ /\bconst\b/ &&
@@ -5318,8 +5436,8 @@ sub process {
5318 } 5436 }
5319 } 5437 }
5320 5438
5321 if ($line =~ /debugfs_create_file.*S_IWUGO/ || 5439 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5322 $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { 5440 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
5323 WARN("EXPORTED_WORLD_WRITABLE", 5441 WARN("EXPORTED_WORLD_WRITABLE",
5324 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 5442 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
5325 } 5443 }