aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl107
1 files changed, 97 insertions, 10 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 4d08b398411f..374abf443636 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -9,7 +9,8 @@ use strict;
9use POSIX; 9use POSIX;
10 10
11my $P = $0; 11my $P = $0;
12$P =~ s@.*/@@g; 12$P =~ s@(.*)/@@g;
13my $D = $1;
13 14
14my $V = '0.32'; 15my $V = '0.32';
15 16
@@ -43,6 +44,8 @@ my $configuration_file = ".checkpatch.conf";
43my $max_line_length = 80; 44my $max_line_length = 80;
44my $ignore_perl_version = 0; 45my $ignore_perl_version = 0;
45my $minimum_perl_version = 5.10.0; 46my $minimum_perl_version = 5.10.0;
47my $min_conf_desc_length = 4;
48my $spelling_file = "$D/spelling.txt";
46 49
47sub help { 50sub help {
48 my ($exitcode) = @_; 51 my ($exitcode) = @_;
@@ -63,6 +66,7 @@ Options:
63 --types TYPE(,TYPE2...) show only these comma separated message types 66 --types TYPE(,TYPE2...) show only these comma separated message types
64 --ignore TYPE(,TYPE2...) ignore various comma separated message types 67 --ignore TYPE(,TYPE2...) ignore various comma separated message types
65 --max-line-length=n set the maximum line length, if exceeded, warn 68 --max-line-length=n set the maximum line length, if exceeded, warn
69 --min-conf-desc-length=n set the min description length, if shorter, warn
66 --show-types show the message "types" in the output 70 --show-types show the message "types" in the output
67 --root=PATH PATH to the kernel tree root 71 --root=PATH PATH to the kernel tree root
68 --no-summary suppress the per-file summary 72 --no-summary suppress the per-file summary
@@ -131,6 +135,7 @@ GetOptions(
131 'types=s' => \@use, 135 'types=s' => \@use,
132 'show-types!' => \$show_types, 136 'show-types!' => \$show_types,
133 'max-line-length=i' => \$max_line_length, 137 'max-line-length=i' => \$max_line_length,
138 'min-conf-desc-length=i' => \$min_conf_desc_length,
134 'root=s' => \$root, 139 'root=s' => \$root,
135 'summary!' => \$summary, 140 'summary!' => \$summary,
136 'mailback!' => \$mailback, 141 'mailback!' => \$mailback,
@@ -425,10 +430,35 @@ foreach my $entry (@mode_permission_funcs) {
425 430
426our $allowed_asm_includes = qr{(?x: 431our $allowed_asm_includes = qr{(?x:
427 irq| 432 irq|
428 memory 433 memory|
434 time|
435 reboot
429)}; 436)};
430# memory.h: ARM has a custom one 437# memory.h: ARM has a custom one
431 438
439# Load common spelling mistakes and build regular expression list.
440my $misspellings;
441my @spelling_list;
442my %spelling_fix;
443open(my $spelling, '<', $spelling_file)
444 or die "$P: Can't open $spelling_file for reading: $!\n";
445while (<$spelling>) {
446 my $line = $_;
447
448 $line =~ s/\s*\n?$//g;
449 $line =~ s/^\s*//g;
450
451 next if ($line =~ m/^\s*#/);
452 next if ($line =~ m/^\s*$/);
453
454 my ($suspect, $fix) = split(/\|\|/, $line);
455
456 push(@spelling_list, $suspect);
457 $spelling_fix{$suspect} = $fix;
458}
459close($spelling);
460$misspellings = join("|", @spelling_list);
461
432sub build_types { 462sub build_types {
433 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 463 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
434 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 464 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
@@ -2215,6 +2245,23 @@ sub process {
2215 "8-bit UTF-8 used in possible commit log\n" . $herecurr); 2245 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2216 } 2246 }
2217 2247
2248# Check for various typo / spelling mistakes
2249 if ($in_commit_log || $line =~ /^\+/) {
2250 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2251 my $typo = $1;
2252 my $typo_fix = $spelling_fix{lc($typo)};
2253 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2254 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2255 my $msg_type = \&WARN;
2256 $msg_type = \&CHK if ($file);
2257 if (&{$msg_type}("TYPO_SPELLING",
2258 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2259 $fix) {
2260 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2261 }
2262 }
2263 }
2264
2218# ignore non-hunk lines and lines being removed 2265# ignore non-hunk lines and lines being removed
2219 next if (!$hunk_line || $line =~ /^-/); 2266 next if (!$hunk_line || $line =~ /^-/);
2220 2267
@@ -2283,8 +2330,10 @@ sub process {
2283 } 2330 }
2284 $length++; 2331 $length++;
2285 } 2332 }
2286 WARN("CONFIG_DESCRIPTION", 2333 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2287 "please write a paragraph that describes the config symbol fully\n" . $herecurr) if ($is_start && $is_end && $length < 4); 2334 WARN("CONFIG_DESCRIPTION",
2335 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2336 }
2288 #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 2337 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2289 } 2338 }
2290 2339
@@ -2341,7 +2390,7 @@ sub process {
2341 } 2390 }
2342 2391
2343# check we are in a valid source file if not then ignore this hunk 2392# check we are in a valid source file if not then ignore this hunk
2344 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 2393 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
2345 2394
2346#line length limit 2395#line length limit
2347 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 2396 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
@@ -2402,7 +2451,7 @@ sub process {
2402 } 2451 }
2403 2452
2404# check we are in a valid source file C or perl if not then ignore this hunk 2453# check we are in a valid source file C or perl if not then ignore this hunk
2405 next if ($realfile !~ /\.(h|c|pl)$/); 2454 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
2406 2455
2407# at the beginning of a line any tabs must come first and anything 2456# at the beginning of a line any tabs must come first and anything
2408# more than 8 must use tabs. 2457# more than 8 must use tabs.
@@ -2424,7 +2473,7 @@ sub process {
2424 "please, no space before tabs\n" . $herevet) && 2473 "please, no space before tabs\n" . $herevet) &&
2425 $fix) { 2474 $fix) {
2426 while ($fixed[$fixlinenr] =~ 2475 while ($fixed[$fixlinenr] =~
2427 s/(^\+.*) {8,8}+\t/$1\t\t/) {} 2476 s/(^\+.*) {8,8}\t/$1\t\t/) {}
2428 while ($fixed[$fixlinenr] =~ 2477 while ($fixed[$fixlinenr] =~
2429 s/(^\+.*) +\t/$1\t/) {} 2478 s/(^\+.*) +\t/$1\t/) {}
2430 } 2479 }
@@ -2592,10 +2641,14 @@ sub process {
2592 next if ($realfile !~ /\.(h|c)$/); 2641 next if ($realfile !~ /\.(h|c)$/);
2593 2642
2594# check indentation of any line with a bare else 2643# check indentation of any line with a bare else
2644# (but not if it is a multiple line "if (foo) return bar; else return baz;")
2595# if the previous line is a break or return and is indented 1 tab more... 2645# if the previous line is a break or return and is indented 1 tab more...
2596 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 2646 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2597 my $tabs = length($1) + 1; 2647 my $tabs = length($1) + 1;
2598 if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) { 2648 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2649 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2650 defined $lines[$linenr] &&
2651 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2599 WARN("UNNECESSARY_ELSE", 2652 WARN("UNNECESSARY_ELSE",
2600 "else is not generally useful after a break or return\n" . $hereprev); 2653 "else is not generally useful after a break or return\n" . $hereprev);
2601 } 2654 }
@@ -3752,7 +3805,6 @@ sub process {
3752 if (ERROR("SPACING", 3805 if (ERROR("SPACING",
3753 "space prohibited before that close parenthesis ')'\n" . $herecurr) && 3806 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3754 $fix) { 3807 $fix) {
3755 print("fixlinenr: <$fixlinenr> fixed[fixlinenr]: <$fixed[$fixlinenr]>\n");
3756 $fixed[$fixlinenr] =~ 3808 $fixed[$fixlinenr] =~
3757 s/\s+\)/\)/; 3809 s/\s+\)/\)/;
3758 } 3810 }
@@ -4060,12 +4112,17 @@ sub process {
4060 my $cnt = $realcnt; 4112 my $cnt = $realcnt;
4061 my ($off, $dstat, $dcond, $rest); 4113 my ($off, $dstat, $dcond, $rest);
4062 my $ctx = ''; 4114 my $ctx = '';
4115 my $has_flow_statement = 0;
4116 my $has_arg_concat = 0;
4063 ($dstat, $dcond, $ln, $cnt, $off) = 4117 ($dstat, $dcond, $ln, $cnt, $off) =
4064 ctx_statement_block($linenr, $realcnt, 0); 4118 ctx_statement_block($linenr, $realcnt, 0);
4065 $ctx = $dstat; 4119 $ctx = $dstat;
4066 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 4120 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4067 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 4121 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4068 4122
4123 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4124 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4125
4069 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 4126 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4070 $dstat =~ s/$;//g; 4127 $dstat =~ s/$;//g;
4071 $dstat =~ s/\\\n.//g; 4128 $dstat =~ s/\\\n.//g;
@@ -4126,10 +4183,23 @@ sub process {
4126 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 4183 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4127 } else { 4184 } else {
4128 ERROR("COMPLEX_MACRO", 4185 ERROR("COMPLEX_MACRO",
4129 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); 4186 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4130 } 4187 }
4131 } 4188 }
4132 4189
4190# check for macros with flow control, but without ## concatenation
4191# ## concatenation is commonly a macro that defines a function so ignore those
4192 if ($has_flow_statement && !$has_arg_concat) {
4193 my $herectx = $here . "\n";
4194 my $cnt = statement_rawlines($ctx);
4195
4196 for (my $n = 0; $n < $cnt; $n++) {
4197 $herectx .= raw_line($linenr, $n) . "\n";
4198 }
4199 WARN("MACRO_WITH_FLOW_CONTROL",
4200 "Macros with flow control statements should be avoided\n" . "$herectx");
4201 }
4202
4133# check for line continuations outside of #defines, preprocessor #, and asm 4203# check for line continuations outside of #defines, preprocessor #, and asm
4134 4204
4135 } else { 4205 } else {
@@ -4338,6 +4408,12 @@ sub process {
4338 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 4408 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4339 } 4409 }
4340 4410
4411# concatenated string without spaces between elements
4412 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4413 CHK("CONCATENATED_STRING",
4414 "Concatenated strings should use spaces between elements\n" . $herecurr);
4415 }
4416
4341# warn about #if 0 4417# warn about #if 0
4342 if ($line =~ /^.\s*\#\s*if\s+0\b/) { 4418 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4343 CHK("REDUNDANT_CODE", 4419 CHK("REDUNDANT_CODE",
@@ -4371,6 +4447,17 @@ sub process {
4371 } 4447 }
4372 } 4448 }
4373 4449
4450# check for logging functions with KERN_<LEVEL>
4451 if ($line !~ /printk\s*\(/ &&
4452 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4453 my $level = $1;
4454 if (WARN("UNNECESSARY_KERN_LEVEL",
4455 "Possible unnecessary $level\n" . $herecurr) &&
4456 $fix) {
4457 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4458 }
4459 }
4460
4374# check for bad placement of section $InitAttribute (e.g.: __initdata) 4461# check for bad placement of section $InitAttribute (e.g.: __initdata)
4375 if ($line =~ /(\b$InitAttribute\b)/) { 4462 if ($line =~ /(\b$InitAttribute\b)/) {
4376 my $attr = $1; 4463 my $attr = $1;