aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl319
1 files changed, 248 insertions, 71 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index c5ec977b9c37..90e1edc8dd42 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -9,6 +9,7 @@ use strict;
9use POSIX; 9use POSIX;
10use File::Basename; 10use File::Basename;
11use Cwd 'abs_path'; 11use Cwd 'abs_path';
12use Term::ANSIColor qw(:constants);
12 13
13my $P = $0; 14my $P = $0;
14my $D = dirname(abs_path($P)); 15my $D = dirname(abs_path($P));
@@ -24,6 +25,7 @@ my $chk_patch = 1;
24my $tst_only; 25my $tst_only;
25my $emacs = 0; 26my $emacs = 0;
26my $terse = 0; 27my $terse = 0;
28my $showfile = 0;
27my $file = 0; 29my $file = 0;
28my $check = 0; 30my $check = 0;
29my $check_orig = 0; 31my $check_orig = 0;
@@ -48,7 +50,8 @@ my $minimum_perl_version = 5.10.0;
48my $min_conf_desc_length = 4; 50my $min_conf_desc_length = 4;
49my $spelling_file = "$D/spelling.txt"; 51my $spelling_file = "$D/spelling.txt";
50my $codespell = 0; 52my $codespell = 0;
51my $codespellfile = "/usr/local/share/codespell/dictionary.txt"; 53my $codespellfile = "/usr/share/codespell/dictionary.txt";
54my $color = 1;
52 55
53sub help { 56sub help {
54 my ($exitcode) = @_; 57 my ($exitcode) = @_;
@@ -64,6 +67,7 @@ Options:
64 --patch treat FILE as patchfile (default) 67 --patch treat FILE as patchfile (default)
65 --emacs emacs compile window format 68 --emacs emacs compile window format
66 --terse one line per report 69 --terse one line per report
70 --showfile emit diffed file position, not input file position
67 -f, --file treat FILE as regular source file 71 -f, --file treat FILE as regular source file
68 --subjective, --strict enable more subjective tests 72 --subjective, --strict enable more subjective tests
69 --types TYPE(,TYPE2...) show only these comma separated message types 73 --types TYPE(,TYPE2...) show only these comma separated message types
@@ -91,8 +95,9 @@ Options:
91 --ignore-perl-version override checking of perl version. expect 95 --ignore-perl-version override checking of perl version. expect
92 runtime errors. 96 runtime errors.
93 --codespell Use the codespell dictionary for spelling/typos 97 --codespell Use the codespell dictionary for spelling/typos
94 (default:/usr/local/share/codespell/dictionary.txt) 98 (default:/usr/share/codespell/dictionary.txt)
95 --codespellfile Use this codespell dictionary 99 --codespellfile Use this codespell dictionary
100 --color Use colors when output is STDOUT (default: on)
96 -h, --help, --version display this help and exit 101 -h, --help, --version display this help and exit
97 102
98When FILE is - read standard input. 103When FILE is - read standard input.
@@ -134,6 +139,7 @@ GetOptions(
134 'patch!' => \$chk_patch, 139 'patch!' => \$chk_patch,
135 'emacs!' => \$emacs, 140 'emacs!' => \$emacs,
136 'terse!' => \$terse, 141 'terse!' => \$terse,
142 'showfile!' => \$showfile,
137 'f|file!' => \$file, 143 'f|file!' => \$file,
138 'subjective!' => \$check, 144 'subjective!' => \$check,
139 'strict!' => \$check, 145 'strict!' => \$check,
@@ -153,6 +159,7 @@ GetOptions(
153 'test-only=s' => \$tst_only, 159 'test-only=s' => \$tst_only,
154 'codespell!' => \$codespell, 160 'codespell!' => \$codespell,
155 'codespellfile=s' => \$codespellfile, 161 'codespellfile=s' => \$codespellfile,
162 'color!' => \$color,
156 'h|help' => \$help, 163 'h|help' => \$help,
157 'version' => \$help 164 'version' => \$help
158) or help(1); 165) or help(1);
@@ -196,12 +203,12 @@ sub hash_save_array_words {
196sub hash_show_words { 203sub hash_show_words {
197 my ($hashRef, $prefix) = @_; 204 my ($hashRef, $prefix) = @_;
198 205
199 if ($quiet == 0 && keys %$hashRef) { 206 if (keys %$hashRef) {
200 print "NOTE: $prefix message types:"; 207 print "\nNOTE: $prefix message types:";
201 foreach my $word (sort keys %$hashRef) { 208 foreach my $word (sort keys %$hashRef) {
202 print " $word"; 209 print " $word";
203 } 210 }
204 print "\n\n"; 211 print "\n";
205 } 212 }
206} 213}
207 214
@@ -347,15 +354,20 @@ our $UTF8 = qr{
347 | $NON_ASCII_UTF8 354 | $NON_ASCII_UTF8
348}x; 355}x;
349 356
357our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
350our $typeOtherOSTypedefs = qr{(?x: 358our $typeOtherOSTypedefs = qr{(?x:
351 u_(?:char|short|int|long) | # bsd 359 u_(?:char|short|int|long) | # bsd
352 u(?:nchar|short|int|long) # sysv 360 u(?:nchar|short|int|long) # sysv
353)}; 361)};
354 362our $typeKernelTypedefs = qr{(?x:
355our $typeTypedefs = qr{(?x:
356 (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 363 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
357 atomic_t 364 atomic_t
358)}; 365)};
366our $typeTypedefs = qr{(?x:
367 $typeC99Typedefs\b|
368 $typeOtherOSTypedefs\b|
369 $typeKernelTypedefs\b
370)};
359 371
360our $logFunctions = qr{(?x: 372our $logFunctions = qr{(?x:
361 printk(?:_ratelimited|_once|)| 373 printk(?:_ratelimited|_once|)|
@@ -418,6 +430,7 @@ our @typeList = (
418 qr{${Ident}_handler_fn}, 430 qr{${Ident}_handler_fn},
419 @typeListMisordered, 431 @typeListMisordered,
420); 432);
433our @typeListFile = ();
421our @typeListWithAttr = ( 434our @typeListWithAttr = (
422 @typeList, 435 @typeList,
423 qr{struct\s+$InitAttribute\s+$Ident}, 436 qr{struct\s+$InitAttribute\s+$Ident},
@@ -427,6 +440,7 @@ our @typeListWithAttr = (
427our @modifierList = ( 440our @modifierList = (
428 qr{fastcall}, 441 qr{fastcall},
429); 442);
443our @modifierListFile = ();
430 444
431our @mode_permission_funcs = ( 445our @mode_permission_funcs = (
432 ["module_param", 3], 446 ["module_param", 3],
@@ -510,13 +524,12 @@ if ($codespell) {
510$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 524$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
511 525
512sub build_types { 526sub build_types {
513 my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 527 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
514 my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 528 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
515 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 529 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
516 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 530 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
517 $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 531 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
518 $BasicType = qr{ 532 $BasicType = qr{
519 (?:$typeOtherOSTypedefs\b)|
520 (?:$typeTypedefs\b)| 533 (?:$typeTypedefs\b)|
521 (?:${all}\b) 534 (?:${all}\b)
522 }x; 535 }x;
@@ -524,7 +537,6 @@ sub build_types {
524 (?:$Modifier\s+|const\s+)* 537 (?:$Modifier\s+|const\s+)*
525 (?: 538 (?:
526 (?:typeof|__typeof__)\s*\([^\)]*\)| 539 (?:typeof|__typeof__)\s*\([^\)]*\)|
527 (?:$typeOtherOSTypedefs\b)|
528 (?:$typeTypedefs\b)| 540 (?:$typeTypedefs\b)|
529 (?:${all}\b) 541 (?:${all}\b)
530 ) 542 )
@@ -542,7 +554,6 @@ sub build_types {
542 (?: 554 (?:
543 (?:typeof|__typeof__)\s*\([^\)]*\)| 555 (?:typeof|__typeof__)\s*\([^\)]*\)|
544 (?:$typeTypedefs\b)| 556 (?:$typeTypedefs\b)|
545 (?:$typeOtherOSTypedefs\b)|
546 (?:${allWithAttr}\b) 557 (?:${allWithAttr}\b)
547 ) 558 )
548 (?:\s+$Modifier|\s+const)* 559 (?:\s+$Modifier|\s+const)*
@@ -737,6 +748,13 @@ for my $filename (@ARGV) {
737 push(@rawlines, $_); 748 push(@rawlines, $_);
738 } 749 }
739 close($FILE); 750 close($FILE);
751
752 if ($#ARGV > 0 && $quiet == 0) {
753 print '-' x length($vname) . "\n";
754 print "$vname\n";
755 print '-' x length($vname) . "\n";
756 }
757
740 if (!process($filename)) { 758 if (!process($filename)) {
741 $exit = 1; 759 $exit = 1;
742 } 760 }
@@ -746,6 +764,29 @@ for my $filename (@ARGV) {
746 @fixed_inserted = (); 764 @fixed_inserted = ();
747 @fixed_deleted = (); 765 @fixed_deleted = ();
748 $fixlinenr = -1; 766 $fixlinenr = -1;
767 @modifierListFile = ();
768 @typeListFile = ();
769 build_types();
770}
771
772if (!$quiet) {
773 hash_show_words(\%use_type, "Used");
774 hash_show_words(\%ignore_type, "Ignored");
775
776 if ($^V lt 5.10.0) {
777 print << "EOM"
778
779NOTE: perl $^V is not modern enough to detect all possible issues.
780 An upgrade to at least perl v5.10.0 is suggested.
781EOM
782 }
783 if ($exit) {
784 print << "EOM"
785
786NOTE: If any of the errors are false positives, please report
787 them to the maintainer, see CHECKPATCH in MAINTAINERS.
788EOM
789 }
749} 790}
750 791
751exit($exit); 792exit($exit);
@@ -1001,7 +1042,7 @@ sub sanitise_line {
1001sub get_quoted_string { 1042sub get_quoted_string {
1002 my ($line, $rawline) = @_; 1043 my ($line, $rawline) = @_;
1003 1044
1004 return "" if ($line !~ m/(\"[X\t]+\")/g); 1045 return "" if ($line !~ m/($String)/g);
1005 return substr($rawline, $-[0], $+[0] - $-[0]); 1046 return substr($rawline, $-[0], $+[0] - $-[0]);
1006} 1047}
1007 1048
@@ -1610,13 +1651,13 @@ sub possible {
1610 for my $modifier (split(' ', $possible)) { 1651 for my $modifier (split(' ', $possible)) {
1611 if ($modifier !~ $notPermitted) { 1652 if ($modifier !~ $notPermitted) {
1612 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1653 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1613 push(@modifierList, $modifier); 1654 push(@modifierListFile, $modifier);
1614 } 1655 }
1615 } 1656 }
1616 1657
1617 } else { 1658 } else {
1618 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 1659 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1619 push(@typeList, $possible); 1660 push(@typeListFile, $possible);
1620 } 1661 }
1621 build_types(); 1662 build_types();
1622 } else { 1663 } else {
@@ -1641,15 +1682,32 @@ sub report {
1641 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 1682 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1642 return 0; 1683 return 0;
1643 } 1684 }
1644 my $line; 1685 my $output = '';
1686 if (-t STDOUT && $color) {
1687 if ($level eq 'ERROR') {
1688 $output .= RED;
1689 } elsif ($level eq 'WARNING') {
1690 $output .= YELLOW;
1691 } else {
1692 $output .= GREEN;
1693 }
1694 }
1695 $output .= $prefix . $level . ':';
1645 if ($show_types) { 1696 if ($show_types) {
1646 $line = "$prefix$level:$type: $msg\n"; 1697 $output .= BLUE if (-t STDOUT && $color);
1647 } else { 1698 $output .= "$type:";
1648 $line = "$prefix$level: $msg\n";
1649 } 1699 }
1650 $line = (split('\n', $line))[0] . "\n" if ($terse); 1700 $output .= RESET if (-t STDOUT && $color);
1701 $output .= ' ' . $msg . "\n";
1651 1702
1652 push(our @report, $line); 1703 if ($showfile) {
1704 my @lines = split("\n", $output, -1);
1705 splice(@lines, 1, 1);
1706 $output = join("\n", @lines);
1707 }
1708 $output = (split('\n', $output))[0] . "\n" if ($terse);
1709
1710 push(our @report, $output);
1653 1711
1654 return 1; 1712 return 1;
1655} 1713}
@@ -1899,6 +1957,7 @@ sub process {
1899 my $in_header_lines = $file ? 0 : 1; 1957 my $in_header_lines = $file ? 0 : 1;
1900 my $in_commit_log = 0; #Scanning lines before patch 1958 my $in_commit_log = 0; #Scanning lines before patch
1901 my $commit_log_long_line = 0; 1959 my $commit_log_long_line = 0;
1960 my $commit_log_has_diff = 0;
1902 my $reported_maintainer_file = 0; 1961 my $reported_maintainer_file = 0;
1903 my $non_utf8_charset = 0; 1962 my $non_utf8_charset = 0;
1904 1963
@@ -2032,7 +2091,8 @@ sub process {
2032 my $rawline = $rawlines[$linenr - 1]; 2091 my $rawline = $rawlines[$linenr - 1];
2033 2092
2034#extract the line range in the file after the patch is applied 2093#extract the line range in the file after the patch is applied
2035 if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2094 if (!$in_commit_log &&
2095 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2036 $is_patch = 1; 2096 $is_patch = 1;
2037 $first_line = $linenr + 1; 2097 $first_line = $linenr + 1;
2038 $realline=$1-1; 2098 $realline=$1-1;
@@ -2073,10 +2133,6 @@ sub process {
2073 2133
2074 my $hunk_line = ($realcnt != 0); 2134 my $hunk_line = ($realcnt != 0);
2075 2135
2076#make up the handle for any error we report on this line
2077 $prefix = "$filename:$realline: " if ($emacs && $file);
2078 $prefix = "$filename:$linenr: " if ($emacs && !$file);
2079
2080 $here = "#$linenr: " if (!$file); 2136 $here = "#$linenr: " if (!$file);
2081 $here = "#$realline: " if ($file); 2137 $here = "#$realline: " if ($file);
2082 2138
@@ -2106,6 +2162,13 @@ sub process {
2106 $found_file = 1; 2162 $found_file = 1;
2107 } 2163 }
2108 2164
2165#make up the handle for any error we report on this line
2166 if ($showfile) {
2167 $prefix = "$realfile:$realline: "
2168 } elsif ($emacs) {
2169 $prefix = "$filename:$linenr: ";
2170 }
2171
2109 if ($found_file) { 2172 if ($found_file) {
2110 if ($realfile =~ m@^(drivers/net/|net/)@) { 2173 if ($realfile =~ m@^(drivers/net/|net/)@) {
2111 $check = 1; 2174 $check = 1;
@@ -2123,6 +2186,17 @@ sub process {
2123 2186
2124 $cnt_lines++ if ($realcnt != 0); 2187 $cnt_lines++ if ($realcnt != 0);
2125 2188
2189# Check if the commit log has what seems like a diff which can confuse patch
2190 if ($in_commit_log && !$commit_log_has_diff &&
2191 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2192 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2193 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2194 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2195 ERROR("DIFF_IN_COMMIT_MSG",
2196 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2197 $commit_log_has_diff = 1;
2198 }
2199
2126# Check for incorrect file permissions 2200# Check for incorrect file permissions
2127 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 2201 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2128 my $permhere = $here . "FILE: $realfile\n"; 2202 my $permhere = $here . "FILE: $realfile\n";
@@ -2510,16 +2584,56 @@ sub process {
2510# check we are in a valid source file if not then ignore this hunk 2584# check we are in a valid source file if not then ignore this hunk
2511 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/); 2585 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
2512 2586
2513#line length limit 2587# line length limit (with some exclusions)
2514 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 2588#
2515 $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 2589# There are a few types of lines that may extend beyond $max_line_length:
2516 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?$String\s*(?:|,|\)\s*;)\s*$/ || 2590# logging functions like pr_info that end in a string
2517 $line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 2591# lines with a single string
2518 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) && 2592# #defines that are a single string
2519 $length > $max_line_length) 2593#
2520 { 2594# There are 3 different line length message types:
2521 WARN("LONG_LINE", 2595# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2522 "line over $max_line_length characters\n" . $herecurr); 2596# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2597# LONG_LINE all other lines longer than $max_line_length
2598#
2599# if LONG_LINE is ignored, the other 2 types are also ignored
2600#
2601
2602 if ($length > $max_line_length) {
2603 my $msg_type = "LONG_LINE";
2604
2605 # Check the allowed long line types first
2606
2607 # logging functions that end in a string that starts
2608 # before $max_line_length
2609 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2610 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2611 $msg_type = "";
2612
2613 # lines with only strings (w/ possible termination)
2614 # #defines with only strings
2615 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2616 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2617 $msg_type = "";
2618
2619 # Otherwise set the alternate message types
2620
2621 # a comment starts before $max_line_length
2622 } elsif ($line =~ /($;[\s$;]*)$/ &&
2623 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2624 $msg_type = "LONG_LINE_COMMENT"
2625
2626 # a quoted string starts before $max_line_length
2627 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2628 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2629 $msg_type = "LONG_LINE_STRING"
2630 }
2631
2632 if ($msg_type ne "" &&
2633 (show_type("LONG_LINE") || show_type($msg_type))) {
2634 WARN($msg_type,
2635 "line over $max_line_length characters\n" . $herecurr);
2636 }
2523 } 2637 }
2524 2638
2525# check for adding lines without a newline. 2639# check for adding lines without a newline.
@@ -3264,7 +3378,6 @@ sub process {
3264 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 3378 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3265 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 3379 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
3266 $line !~ /\b$typeTypedefs\b/ && 3380 $line !~ /\b$typeTypedefs\b/ &&
3267 $line !~ /\b$typeOtherOSTypedefs\b/ &&
3268 $line !~ /\b__bitwise(?:__|)\b/) { 3381 $line !~ /\b__bitwise(?:__|)\b/) {
3269 WARN("NEW_TYPEDEFS", 3382 WARN("NEW_TYPEDEFS",
3270 "do not add new typedefs\n" . $herecurr); 3383 "do not add new typedefs\n" . $herecurr);
@@ -4337,8 +4450,8 @@ sub process {
4337 } 4450 }
4338 4451
4339 # Flatten any obvious string concatentation. 4452 # Flatten any obvious string concatentation.
4340 while ($dstat =~ s/("X*")\s*$Ident/$1/ || 4453 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4341 $dstat =~ s/$Ident\s*("X*")/$1/) 4454 $dstat =~ s/$Ident\s*($String)/$1/)
4342 { 4455 {
4343 } 4456 }
4344 4457
@@ -4619,7 +4732,7 @@ sub process {
4619# to grep for the string. Make exceptions when the previous string ends in a 4732# to grep for the string. Make exceptions when the previous string ends in a
4620# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 4733# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
4621# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 4734# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
4622 if ($line =~ /^\+\s*"[X\t]*"/ && 4735 if ($line =~ /^\+\s*$String/ &&
4623 $prevline =~ /"\s*$/ && 4736 $prevline =~ /"\s*$/ &&
4624 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 4737 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
4625 if (WARN("SPLIT_STRING", 4738 if (WARN("SPLIT_STRING",
@@ -4665,13 +4778,13 @@ sub process {
4665 } 4778 }
4666 4779
4667# concatenated string without spaces between elements 4780# concatenated string without spaces between elements
4668 if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) { 4781 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
4669 CHK("CONCATENATED_STRING", 4782 CHK("CONCATENATED_STRING",
4670 "Concatenated strings should use spaces between elements\n" . $herecurr); 4783 "Concatenated strings should use spaces between elements\n" . $herecurr);
4671 } 4784 }
4672 4785
4673# uncoalesced string fragments 4786# uncoalesced string fragments
4674 if ($line =~ /"X*"\s*"/) { 4787 if ($line =~ /$String\s*"/) {
4675 WARN("STRING_FRAGMENTS", 4788 WARN("STRING_FRAGMENTS",
4676 "Consecutive strings are generally better as a single string\n" . $herecurr); 4789 "Consecutive strings are generally better as a single string\n" . $herecurr);
4677 } 4790 }
@@ -4898,6 +5011,13 @@ sub process {
4898 "memory barrier without comment\n" . $herecurr); 5011 "memory barrier without comment\n" . $herecurr);
4899 } 5012 }
4900 } 5013 }
5014# check for waitqueue_active without a comment.
5015 if ($line =~ /\bwaitqueue_active\s*\(/) {
5016 if (!ctx_has_comment($first_line, $linenr)) {
5017 WARN("WAITQUEUE_ACTIVE",
5018 "waitqueue_active without comment\n" . $herecurr);
5019 }
5020 }
4901# check of hardware specific defines 5021# check of hardware specific defines
4902 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 5022 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
4903 CHK("ARCH_DEFINES", 5023 CHK("ARCH_DEFINES",
@@ -4973,6 +5093,24 @@ sub process {
4973 "Using weak declarations can have unintended link defects\n" . $herecurr); 5093 "Using weak declarations can have unintended link defects\n" . $herecurr);
4974 } 5094 }
4975 5095
5096# check for c99 types like uint8_t used outside of uapi/
5097 if ($realfile !~ m@\binclude/uapi/@ &&
5098 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5099 my $type = $1;
5100 if ($type =~ /\b($typeC99Typedefs)\b/) {
5101 $type = $1;
5102 my $kernel_type = 'u';
5103 $kernel_type = 's' if ($type =~ /^_*[si]/);
5104 $type =~ /(\d+)/;
5105 $kernel_type .= $1;
5106 if (CHK("PREFER_KERNEL_TYPES",
5107 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5108 $fix) {
5109 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5110 }
5111 }
5112 }
5113
4976# check for sizeof(&) 5114# check for sizeof(&)
4977 if ($line =~ /\bsizeof\s*\(\s*\&/) { 5115 if ($line =~ /\bsizeof\s*\(\s*\&/) {
4978 WARN("SIZEOF_ADDRESS", 5116 WARN("SIZEOF_ADDRESS",
@@ -5010,7 +5148,7 @@ sub process {
5010# Check for misused memsets 5148# Check for misused memsets
5011 if ($^V && $^V ge 5.10.0 && 5149 if ($^V && $^V ge 5.10.0 &&
5012 defined $stat && 5150 defined $stat &&
5013 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { 5151 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5014 5152
5015 my $ms_addr = $2; 5153 my $ms_addr = $2;
5016 my $ms_val = $7; 5154 my $ms_val = $7;
@@ -5027,14 +5165,46 @@ sub process {
5027 5165
5028# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 5166# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5029 if ($^V && $^V ge 5.10.0 && 5167 if ($^V && $^V ge 5.10.0 &&
5030 $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) { 5168 defined $stat &&
5169 $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5031 if (WARN("PREFER_ETHER_ADDR_COPY", 5170 if (WARN("PREFER_ETHER_ADDR_COPY",
5032 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) && 5171 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5033 $fix) { 5172 $fix) {
5034 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 5173 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5035 } 5174 }
5036 } 5175 }
5037 5176
5177# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5178 if ($^V && $^V ge 5.10.0 &&
5179 defined $stat &&
5180 $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5181 WARN("PREFER_ETHER_ADDR_EQUAL",
5182 "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5183 }
5184
5185# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5186# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5187 if ($^V && $^V ge 5.10.0 &&
5188 defined $stat &&
5189 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5190
5191 my $ms_val = $7;
5192
5193 if ($ms_val =~ /^(?:0x|)0+$/i) {
5194 if (WARN("PREFER_ETH_ZERO_ADDR",
5195 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5196 $fix) {
5197 $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5198 }
5199 } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5200 if (WARN("PREFER_ETH_BROADCAST_ADDR",
5201 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5202 $fix) {
5203 $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5204 }
5205 }
5206 }
5207
5038# typecasts on min/max could be min_t/max_t 5208# typecasts on min/max could be min_t/max_t
5039 if ($^V && $^V ge 5.10.0 && 5209 if ($^V && $^V ge 5.10.0 &&
5040 defined $stat && 5210 defined $stat &&
@@ -5472,6 +5642,24 @@ sub process {
5472 } 5642 }
5473 } 5643 }
5474 } 5644 }
5645
5646# validate content of MODULE_LICENSE against list from include/linux/module.h
5647 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
5648 my $extracted_string = get_quoted_string($line, $rawline);
5649 my $valid_licenses = qr{
5650 GPL|
5651 GPL\ v2|
5652 GPL\ and\ additional\ rights|
5653 Dual\ BSD/GPL|
5654 Dual\ MIT/GPL|
5655 Dual\ MPL/GPL|
5656 Proprietary
5657 }x;
5658 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
5659 WARN("MODULE_LICENSE",
5660 "unknown module license " . $extracted_string . "\n" . $herecurr);
5661 }
5662 }
5475 } 5663 }
5476 5664
5477 # If we have no input at all, then there is nothing to report on 5665 # If we have no input at all, then there is nothing to report on
@@ -5492,11 +5680,11 @@ sub process {
5492 exit(0); 5680 exit(0);
5493 } 5681 }
5494 5682
5495 if (!$is_patch) { 5683 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
5496 ERROR("NOT_UNIFIED_DIFF", 5684 ERROR("NOT_UNIFIED_DIFF",
5497 "Does not appear to be a unified-diff format patch\n"); 5685 "Does not appear to be a unified-diff format patch\n");
5498 } 5686 }
5499 if ($is_patch && $chk_signoff && $signoff == 0) { 5687 if ($is_patch && $filename ne '-' && $chk_signoff && $signoff == 0) {
5500 ERROR("MISSING_SIGN_OFF", 5688 ERROR("MISSING_SIGN_OFF",
5501 "Missing Signed-off-by: line(s)\n"); 5689 "Missing Signed-off-by: line(s)\n");
5502 } 5690 }
@@ -5507,28 +5695,21 @@ sub process {
5507 print "total: $cnt_error errors, $cnt_warn warnings, " . 5695 print "total: $cnt_error errors, $cnt_warn warnings, " .
5508 (($check)? "$cnt_chk checks, " : "") . 5696 (($check)? "$cnt_chk checks, " : "") .
5509 "$cnt_lines lines checked\n"; 5697 "$cnt_lines lines checked\n";
5510 print "\n" if ($quiet == 0);
5511 } 5698 }
5512 5699
5513 if ($quiet == 0) { 5700 if ($quiet == 0) {
5514
5515 if ($^V lt 5.10.0) {
5516 print("NOTE: perl $^V is not modern enough to detect all possible issues.\n");
5517 print("An upgrade to at least perl v5.10.0 is suggested.\n\n");
5518 }
5519
5520 # If there were whitespace errors which cleanpatch can fix 5701 # If there were whitespace errors which cleanpatch can fix
5521 # then suggest that. 5702 # then suggest that.
5522 if ($rpt_cleaners) { 5703 if ($rpt_cleaners) {
5523 print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n";
5524 print " scripts/cleanfile\n\n";
5525 $rpt_cleaners = 0; 5704 $rpt_cleaners = 0;
5705 print << "EOM"
5706
5707NOTE: Whitespace errors detected.
5708 You may wish to use scripts/cleanpatch or scripts/cleanfile
5709EOM
5526 } 5710 }
5527 } 5711 }
5528 5712
5529 hash_show_words(\%use_type, "Used");
5530 hash_show_words(\%ignore_type, "Ignored");
5531
5532 if ($clean == 0 && $fix && 5713 if ($clean == 0 && $fix &&
5533 ("@rawlines" ne "@fixed" || 5714 ("@rawlines" ne "@fixed" ||
5534 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 5715 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
@@ -5556,6 +5737,7 @@ sub process {
5556 5737
5557 if (!$quiet) { 5738 if (!$quiet) {
5558 print << "EOM"; 5739 print << "EOM";
5740
5559Wrote EXPERIMENTAL --fix correction(s) to '$newfile' 5741Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
5560 5742
5561Do _NOT_ trust the results written to this file. 5743Do _NOT_ trust the results written to this file.
@@ -5563,22 +5745,17 @@ Do _NOT_ submit these changes without inspecting them for correctness.
5563 5745
5564This EXPERIMENTAL file is simply a convenience to help rewrite patches. 5746This EXPERIMENTAL file is simply a convenience to help rewrite patches.
5565No warranties, expressed or implied... 5747No warranties, expressed or implied...
5566
5567EOM 5748EOM
5568 } 5749 }
5569 } 5750 }
5570 5751
5571 if ($clean == 1 && $quiet == 0) { 5752 if ($quiet == 0) {
5572 print "$vname has no obvious style problems and is ready for submission.\n" 5753 print "\n";
5573 } 5754 if ($clean == 1) {
5574 if ($clean == 0 && $quiet == 0) { 5755 print "$vname has no obvious style problems and is ready for submission.\n";
5575 print << "EOM"; 5756 } else {
5576$vname has style problems, please review. 5757 print "$vname has style problems, please review.\n";
5577 5758 }
5578If any of these errors are false positives, please report
5579them to the maintainer, see CHECKPATCH in MAINTAINERS.
5580EOM
5581 } 5759 }
5582
5583 return $clean; 5760 return $clean;
5584} 5761}