aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl336
1 files changed, 272 insertions, 64 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 4d08b398411f..f0bb6d60c07b 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7,9 +7,11 @@
7 7
8use strict; 8use strict;
9use POSIX; 9use POSIX;
10use File::Basename;
11use Cwd 'abs_path';
10 12
11my $P = $0; 13my $P = $0;
12$P =~ s@.*/@@g; 14my $D = dirname(abs_path($P));
13 15
14my $V = '0.32'; 16my $V = '0.32';
15 17
@@ -43,6 +45,8 @@ my $configuration_file = ".checkpatch.conf";
43my $max_line_length = 80; 45my $max_line_length = 80;
44my $ignore_perl_version = 0; 46my $ignore_perl_version = 0;
45my $minimum_perl_version = 5.10.0; 47my $minimum_perl_version = 5.10.0;
48my $min_conf_desc_length = 4;
49my $spelling_file = "$D/spelling.txt";
46 50
47sub help { 51sub help {
48 my ($exitcode) = @_; 52 my ($exitcode) = @_;
@@ -63,6 +67,7 @@ Options:
63 --types TYPE(,TYPE2...) show only these comma separated message types 67 --types TYPE(,TYPE2...) show only these comma separated message types
64 --ignore TYPE(,TYPE2...) ignore various comma separated message types 68 --ignore TYPE(,TYPE2...) ignore various comma separated message types
65 --max-line-length=n set the maximum line length, if exceeded, warn 69 --max-line-length=n set the maximum line length, if exceeded, warn
70 --min-conf-desc-length=n set the min description length, if shorter, warn
66 --show-types show the message "types" in the output 71 --show-types show the message "types" in the output
67 --root=PATH PATH to the kernel tree root 72 --root=PATH PATH to the kernel tree root
68 --no-summary suppress the per-file summary 73 --no-summary suppress the per-file summary
@@ -131,6 +136,7 @@ GetOptions(
131 'types=s' => \@use, 136 'types=s' => \@use,
132 'show-types!' => \$show_types, 137 'show-types!' => \$show_types,
133 'max-line-length=i' => \$max_line_length, 138 'max-line-length=i' => \$max_line_length,
139 'min-conf-desc-length=i' => \$min_conf_desc_length,
134 'root=s' => \$root, 140 'root=s' => \$root,
135 'summary!' => \$summary, 141 'summary!' => \$summary,
136 'mailback!' => \$mailback, 142 'mailback!' => \$mailback,
@@ -425,10 +431,38 @@ foreach my $entry (@mode_permission_funcs) {
425 431
426our $allowed_asm_includes = qr{(?x: 432our $allowed_asm_includes = qr{(?x:
427 irq| 433 irq|
428 memory 434 memory|
435 time|
436 reboot
429)}; 437)};
430# memory.h: ARM has a custom one 438# memory.h: ARM has a custom one
431 439
440# Load common spelling mistakes and build regular expression list.
441my $misspellings;
442my %spelling_fix;
443
444if (open(my $spelling, '<', $spelling_file)) {
445 my @spelling_list;
446 while (<$spelling>) {
447 my $line = $_;
448
449 $line =~ s/\s*\n?$//g;
450 $line =~ s/^\s*//g;
451
452 next if ($line =~ m/^\s*#/);
453 next if ($line =~ m/^\s*$/);
454
455 my ($suspect, $fix) = split(/\|\|/, $line);
456
457 push(@spelling_list, $suspect);
458 $spelling_fix{$suspect} = $fix;
459 }
460 close($spelling);
461 $misspellings = join("|", @spelling_list);
462} else {
463 warn "No typos will be found - file '$spelling_file': $!\n";
464}
465
432sub build_types { 466sub build_types {
433 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 467 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)";
434 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 468 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)";
@@ -912,7 +946,7 @@ sub sanitise_line {
912sub get_quoted_string { 946sub get_quoted_string {
913 my ($line, $rawline) = @_; 947 my ($line, $rawline) = @_;
914 948
915 return "" if ($line !~ m/(\"[X]+\")/g); 949 return "" if ($line !~ m/(\"[X\t]+\")/g);
916 return substr($rawline, $-[0], $+[0] - $-[0]); 950 return substr($rawline, $-[0], $+[0] - $-[0]);
917} 951}
918 952
@@ -1813,6 +1847,7 @@ sub process {
1813 my $non_utf8_charset = 0; 1847 my $non_utf8_charset = 0;
1814 1848
1815 my $last_blank_line = 0; 1849 my $last_blank_line = 0;
1850 my $last_coalesced_string_linenr = -1;
1816 1851
1817 our @report = (); 1852 our @report = ();
1818 our $cnt_lines = 0; 1853 our $cnt_lines = 0;
@@ -2048,6 +2083,12 @@ sub process {
2048 $in_commit_log = 0; 2083 $in_commit_log = 0;
2049 } 2084 }
2050 2085
2086# Check if MAINTAINERS is being updated. If so, there's probably no need to
2087# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2088 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2089 $reported_maintainer_file = 1;
2090 }
2091
2051# Check signature styles 2092# Check signature styles
2052 if (!$in_header_lines && 2093 if (!$in_header_lines &&
2053 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 2094 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
@@ -2215,6 +2256,23 @@ sub process {
2215 "8-bit UTF-8 used in possible commit log\n" . $herecurr); 2256 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2216 } 2257 }
2217 2258
2259# Check for various typo / spelling mistakes
2260 if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) {
2261 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) {
2262 my $typo = $1;
2263 my $typo_fix = $spelling_fix{lc($typo)};
2264 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2265 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2266 my $msg_type = \&WARN;
2267 $msg_type = \&CHK if ($file);
2268 if (&{$msg_type}("TYPO_SPELLING",
2269 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2270 $fix) {
2271 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2272 }
2273 }
2274 }
2275
2218# ignore non-hunk lines and lines being removed 2276# ignore non-hunk lines and lines being removed
2219 next if (!$hunk_line || $line =~ /^-/); 2277 next if (!$hunk_line || $line =~ /^-/);
2220 2278
@@ -2283,8 +2341,10 @@ sub process {
2283 } 2341 }
2284 $length++; 2342 $length++;
2285 } 2343 }
2286 WARN("CONFIG_DESCRIPTION", 2344 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); 2345 WARN("CONFIG_DESCRIPTION",
2346 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2347 }
2288 #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 2348 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
2289 } 2349 }
2290 2350
@@ -2341,7 +2401,7 @@ sub process {
2341 } 2401 }
2342 2402
2343# check we are in a valid source file if not then ignore this hunk 2403# check we are in a valid source file if not then ignore this hunk
2344 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 2404 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
2345 2405
2346#line length limit 2406#line length limit
2347 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 2407 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
@@ -2354,33 +2414,6 @@ sub process {
2354 "line over $max_line_length characters\n" . $herecurr); 2414 "line over $max_line_length characters\n" . $herecurr);
2355 } 2415 }
2356 2416
2357# Check for user-visible strings broken across lines, which breaks the ability
2358# to grep for the string. Make exceptions when the previous string ends in a
2359# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
2360# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
2361 if ($line =~ /^\+\s*"/ &&
2362 $prevline =~ /"\s*$/ &&
2363 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
2364 WARN("SPLIT_STRING",
2365 "quoted string split across lines\n" . $hereprev);
2366 }
2367
2368# check for missing a space in a string concatination
2369 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
2370 WARN('MISSING_SPACE',
2371 "break quoted strings at a space character\n" . $hereprev);
2372 }
2373
2374# check for spaces before a quoted newline
2375 if ($rawline =~ /^.*\".*\s\\n/) {
2376 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
2377 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
2378 $fix) {
2379 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
2380 }
2381
2382 }
2383
2384# check for adding lines without a newline. 2417# check for adding lines without a newline.
2385 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 2418 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2386 WARN("MISSING_EOF_NEWLINE", 2419 WARN("MISSING_EOF_NEWLINE",
@@ -2402,7 +2435,7 @@ sub process {
2402 } 2435 }
2403 2436
2404# check we are in a valid source file C or perl if not then ignore this hunk 2437# check we are in a valid source file C or perl if not then ignore this hunk
2405 next if ($realfile !~ /\.(h|c|pl)$/); 2438 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
2406 2439
2407# at the beginning of a line any tabs must come first and anything 2440# at the beginning of a line any tabs must come first and anything
2408# more than 8 must use tabs. 2441# more than 8 must use tabs.
@@ -2424,7 +2457,7 @@ sub process {
2424 "please, no space before tabs\n" . $herevet) && 2457 "please, no space before tabs\n" . $herevet) &&
2425 $fix) { 2458 $fix) {
2426 while ($fixed[$fixlinenr] =~ 2459 while ($fixed[$fixlinenr] =~
2427 s/(^\+.*) {8,8}+\t/$1\t\t/) {} 2460 s/(^\+.*) {8,8}\t/$1\t\t/) {}
2428 while ($fixed[$fixlinenr] =~ 2461 while ($fixed[$fixlinenr] =~
2429 s/(^\+.*) +\t/$1\t/) {} 2462 s/(^\+.*) +\t/$1\t/) {}
2430 } 2463 }
@@ -2466,7 +2499,8 @@ sub process {
2466 } 2499 }
2467 } 2500 }
2468 2501
2469 if ($line =~ /^\+.*\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|{)/) { 2502 if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;\({\[\<\>])/ &&
2503 (!defined($1) || $1 !~ /sizeof\s*/)) {
2470 if (CHK("SPACING", 2504 if (CHK("SPACING",
2471 "No space is necessary after a cast\n" . $herecurr) && 2505 "No space is necessary after a cast\n" . $herecurr) &&
2472 $fix) { 2506 $fix) {
@@ -2592,10 +2626,14 @@ sub process {
2592 next if ($realfile !~ /\.(h|c)$/); 2626 next if ($realfile !~ /\.(h|c)$/);
2593 2627
2594# check indentation of any line with a bare else 2628# check indentation of any line with a bare else
2629# (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... 2630# if the previous line is a break or return and is indented 1 tab more...
2596 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 2631 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2597 my $tabs = length($1) + 1; 2632 my $tabs = length($1) + 1;
2598 if ($prevline =~ /^\+\t{$tabs,$tabs}(?:break|return)\b/) { 2633 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2634 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2635 defined $lines[$linenr] &&
2636 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2599 WARN("UNNECESSARY_ELSE", 2637 WARN("UNNECESSARY_ELSE",
2600 "else is not generally useful after a break or return\n" . $hereprev); 2638 "else is not generally useful after a break or return\n" . $hereprev);
2601 } 2639 }
@@ -3510,14 +3548,33 @@ sub process {
3510 } 3548 }
3511 } 3549 }
3512 3550
3513 # , must have a space on the right. 3551 # , must not have a space before and must have a space on the right.
3514 } elsif ($op eq ',') { 3552 } elsif ($op eq ',') {
3553 my $rtrim_before = 0;
3554 my $space_after = 0;
3555 if ($ctx =~ /Wx./) {
3556 if (ERROR("SPACING",
3557 "space prohibited before that '$op' $at\n" . $hereptr)) {
3558 $line_fixed = 1;
3559 $rtrim_before = 1;
3560 }
3561 }
3515 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 3562 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
3516 if (ERROR("SPACING", 3563 if (ERROR("SPACING",
3517 "space required after that '$op' $at\n" . $hereptr)) { 3564 "space required after that '$op' $at\n" . $hereptr)) {
3518 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
3519 $line_fixed = 1; 3565 $line_fixed = 1;
3520 $last_after = $n; 3566 $last_after = $n;
3567 $space_after = 1;
3568 }
3569 }
3570 if ($rtrim_before || $space_after) {
3571 if ($rtrim_before) {
3572 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
3573 } else {
3574 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
3575 }
3576 if ($space_after) {
3577 $good .= " ";
3521 } 3578 }
3522 } 3579 }
3523 3580
@@ -3752,7 +3809,6 @@ sub process {
3752 if (ERROR("SPACING", 3809 if (ERROR("SPACING",
3753 "space prohibited before that close parenthesis ')'\n" . $herecurr) && 3810 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
3754 $fix) { 3811 $fix) {
3755 print("fixlinenr: <$fixlinenr> fixed[fixlinenr]: <$fixed[$fixlinenr]>\n");
3756 $fixed[$fixlinenr] =~ 3812 $fixed[$fixlinenr] =~
3757 s/\s+\)/\)/; 3813 s/\s+\)/\)/;
3758 } 3814 }
@@ -3762,9 +3818,27 @@ sub process {
3762# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 3818# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
3763 3819
3764 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 3820 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
3765 CHK("UNNECESSARY_PARENTHESES", 3821 my $var = $1;
3766 "Unnecessary parentheses around $1\n" . $herecurr); 3822 if (CHK("UNNECESSARY_PARENTHESES",
3767 } 3823 "Unnecessary parentheses around $var\n" . $herecurr) &&
3824 $fix) {
3825 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
3826 }
3827 }
3828
3829# check for unnecessary parentheses around function pointer uses
3830# ie: (foo->bar)(); should be foo->bar();
3831# but not "if (foo->bar) (" to avoid some false positives
3832 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
3833 my $var = $2;
3834 if (CHK("UNNECESSARY_PARENTHESES",
3835 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
3836 $fix) {
3837 my $var2 = deparenthesize($var);
3838 $var2 =~ s/\s//g;
3839 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
3840 }
3841 }
3768 3842
3769#goto labels aren't indented, allow a single space however 3843#goto labels aren't indented, allow a single space however
3770 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 3844 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
@@ -4004,7 +4078,9 @@ sub process {
4004#Ignore Page<foo> variants 4078#Ignore Page<foo> variants
4005 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 4079 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
4006#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 4080#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4007 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/) { 4081 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4082#Ignore some three character SI units explicitly, like MiB and KHz
4083 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
4008 while ($var =~ m{($Ident)}g) { 4084 while ($var =~ m{($Ident)}g) {
4009 my $word = $1; 4085 my $word = $1;
4010 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 4086 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
@@ -4060,12 +4136,17 @@ sub process {
4060 my $cnt = $realcnt; 4136 my $cnt = $realcnt;
4061 my ($off, $dstat, $dcond, $rest); 4137 my ($off, $dstat, $dcond, $rest);
4062 my $ctx = ''; 4138 my $ctx = '';
4139 my $has_flow_statement = 0;
4140 my $has_arg_concat = 0;
4063 ($dstat, $dcond, $ln, $cnt, $off) = 4141 ($dstat, $dcond, $ln, $cnt, $off) =
4064 ctx_statement_block($linenr, $realcnt, 0); 4142 ctx_statement_block($linenr, $realcnt, 0);
4065 $ctx = $dstat; 4143 $ctx = $dstat;
4066 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 4144 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4067 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 4145 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4068 4146
4147 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
4148 $has_arg_concat = 1 if ($ctx =~ /\#\#/);
4149
4069 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 4150 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4070 $dstat =~ s/$;//g; 4151 $dstat =~ s/$;//g;
4071 $dstat =~ s/\\\n.//g; 4152 $dstat =~ s/\\\n.//g;
@@ -4126,8 +4207,21 @@ sub process {
4126 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 4207 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4127 } else { 4208 } else {
4128 ERROR("COMPLEX_MACRO", 4209 ERROR("COMPLEX_MACRO",
4129 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); 4210 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4211 }
4212 }
4213
4214# check for macros with flow control, but without ## concatenation
4215# ## concatenation is commonly a macro that defines a function so ignore those
4216 if ($has_flow_statement && !$has_arg_concat) {
4217 my $herectx = $here . "\n";
4218 my $cnt = statement_rawlines($ctx);
4219
4220 for (my $n = 0; $n < $cnt; $n++) {
4221 $herectx .= raw_line($linenr, $n) . "\n";
4130 } 4222 }
4223 WARN("MACRO_WITH_FLOW_CONTROL",
4224 "Macros with flow control statements should be avoided\n" . "$herectx");
4131 } 4225 }
4132 4226
4133# check for line continuations outside of #defines, preprocessor #, and asm 4227# check for line continuations outside of #defines, preprocessor #, and asm
@@ -4338,6 +4432,85 @@ sub process {
4338 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 4432 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
4339 } 4433 }
4340 4434
4435# Check for user-visible strings broken across lines, which breaks the ability
4436# to grep for the string. Make exceptions when the previous string ends in a
4437# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4438# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4439 if ($line =~ /^\+\s*"[X\t]*"/ &&
4440 $prevline =~ /"\s*$/ &&
4441 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4442 if (WARN("SPLIT_STRING",
4443 "quoted string split across lines\n" . $hereprev) &&
4444 $fix &&
4445 $prevrawline =~ /^\+.*"\s*$/ &&
4446 $last_coalesced_string_linenr != $linenr - 1) {
4447 my $extracted_string = get_quoted_string($line, $rawline);
4448 my $comma_close = "";
4449 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
4450 $comma_close = $1;
4451 }
4452
4453 fix_delete_line($fixlinenr - 1, $prevrawline);
4454 fix_delete_line($fixlinenr, $rawline);
4455 my $fixedline = $prevrawline;
4456 $fixedline =~ s/"\s*$//;
4457 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
4458 fix_insert_line($fixlinenr - 1, $fixedline);
4459 $fixedline = $rawline;
4460 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
4461 if ($fixedline !~ /\+\s*$/) {
4462 fix_insert_line($fixlinenr, $fixedline);
4463 }
4464 $last_coalesced_string_linenr = $linenr;
4465 }
4466 }
4467
4468# check for missing a space in a string concatenation
4469 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
4470 WARN('MISSING_SPACE',
4471 "break quoted strings at a space character\n" . $hereprev);
4472 }
4473
4474# check for spaces before a quoted newline
4475 if ($rawline =~ /^.*\".*\s\\n/) {
4476 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
4477 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
4478 $fix) {
4479 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
4480 }
4481
4482 }
4483
4484# concatenated string without spaces between elements
4485 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) {
4486 CHK("CONCATENATED_STRING",
4487 "Concatenated strings should use spaces between elements\n" . $herecurr);
4488 }
4489
4490# uncoalesced string fragments
4491 if ($line =~ /"X*"\s*"/) {
4492 WARN("STRING_FRAGMENTS",
4493 "Consecutive strings are generally better as a single string\n" . $herecurr);
4494 }
4495
4496# check for %L{u,d,i} in strings
4497 my $string;
4498 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4499 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4500 $string =~ s/%%/__/g;
4501 if ($string =~ /(?<!%)%L[udi]/) {
4502 WARN("PRINTF_L",
4503 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4504 last;
4505 }
4506 }
4507
4508# check for line continuations in quoted strings with odd counts of "
4509 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4510 WARN("LINE_CONTINUATIONS",
4511 "Avoid line continuations in quoted strings\n" . $herecurr);
4512 }
4513
4341# warn about #if 0 4514# warn about #if 0
4342 if ($line =~ /^.\s*\#\s*if\s+0\b/) { 4515 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4343 CHK("REDUNDANT_CODE", 4516 CHK("REDUNDANT_CODE",
@@ -4350,7 +4523,7 @@ sub process {
4350 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; 4523 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
4351 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { 4524 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
4352 WARN('NEEDLESS_IF', 4525 WARN('NEEDLESS_IF',
4353 "$1(NULL) is safe this check is probably not required\n" . $hereprev); 4526 "$1(NULL) is safe and this check is probably not required\n" . $hereprev);
4354 } 4527 }
4355 } 4528 }
4356 4529
@@ -4371,6 +4544,39 @@ sub process {
4371 } 4544 }
4372 } 4545 }
4373 4546
4547# check for logging functions with KERN_<LEVEL>
4548 if ($line !~ /printk\s*\(/ &&
4549 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
4550 my $level = $1;
4551 if (WARN("UNNECESSARY_KERN_LEVEL",
4552 "Possible unnecessary $level\n" . $herecurr) &&
4553 $fix) {
4554 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
4555 }
4556 }
4557
4558# check for mask then right shift without a parentheses
4559 if ($^V && $^V ge 5.10.0 &&
4560 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
4561 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
4562 WARN("MASK_THEN_SHIFT",
4563 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
4564 }
4565
4566# check for pointer comparisons to NULL
4567 if ($^V && $^V ge 5.10.0) {
4568 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
4569 my $val = $1;
4570 my $equal = "!";
4571 $equal = "" if ($4 eq "!=");
4572 if (CHK("COMPARISON_TO_NULL",
4573 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
4574 $fix) {
4575 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
4576 }
4577 }
4578 }
4579
4374# check for bad placement of section $InitAttribute (e.g.: __initdata) 4580# check for bad placement of section $InitAttribute (e.g.: __initdata)
4375 if ($line =~ /(\b$InitAttribute\b)/) { 4581 if ($line =~ /(\b$InitAttribute\b)/) {
4376 my $attr = $1; 4582 my $attr = $1;
@@ -4565,6 +4771,15 @@ sub process {
4565 } 4771 }
4566 } 4772 }
4567 4773
4774# Check for __attribute__ weak, or __weak declarations (may have link issues)
4775 if ($^V && $^V ge 5.10.0 &&
4776 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
4777 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
4778 $line =~ /\b__weak\b/)) {
4779 ERROR("WEAK_DECLARATION",
4780 "Using weak declarations can have unintended link defects\n" . $herecurr);
4781 }
4782
4568# check for sizeof(&) 4783# check for sizeof(&)
4569 if ($line =~ /\bsizeof\s*\(\s*\&/) { 4784 if ($line =~ /\bsizeof\s*\(\s*\&/) {
4570 WARN("SIZEOF_ADDRESS", 4785 WARN("SIZEOF_ADDRESS",
@@ -4580,12 +4795,6 @@ sub process {
4580 } 4795 }
4581 } 4796 }
4582 4797
4583# check for line continuations in quoted strings with odd counts of "
4584 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
4585 WARN("LINE_CONTINUATIONS",
4586 "Avoid line continuations in quoted strings\n" . $herecurr);
4587 }
4588
4589# check for struct spinlock declarations 4798# check for struct spinlock declarations
4590 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 4799 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
4591 WARN("USE_SPINLOCK_T", 4800 WARN("USE_SPINLOCK_T",
@@ -4821,6 +5030,17 @@ sub process {
4821 } 5030 }
4822 } 5031 }
4823 5032
5033# check for #defines like: 1 << <digit> that could be BIT(digit)
5034 if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
5035 my $ull = "";
5036 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5037 if (CHK("BIT_MACRO",
5038 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5039 $fix) {
5040 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5041 }
5042 }
5043
4824# check for case / default statements not preceded by break/fallthrough/switch 5044# check for case / default statements not preceded by break/fallthrough/switch
4825 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 5045 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
4826 my $has_break = 0; 5046 my $has_break = 0;
@@ -4984,18 +5204,6 @@ sub process {
4984 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 5204 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4985 } 5205 }
4986 5206
4987# check for %L{u,d,i} in strings
4988 my $string;
4989 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
4990 $string = substr($rawline, $-[1], $+[1] - $-[1]);
4991 $string =~ s/%%/__/g;
4992 if ($string =~ /(?<!%)%L[udi]/) {
4993 WARN("PRINTF_L",
4994 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
4995 last;
4996 }
4997 }
4998
4999# whine mightly about in_atomic 5207# whine mightly about in_atomic
5000 if ($line =~ /\bin_atomic\s*\(/) { 5208 if ($line =~ /\bin_atomic\s*\(/) {
5001 if ($realfile =~ m@^drivers/@) { 5209 if ($realfile =~ m@^drivers/@) {