aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl150
1 files changed, 88 insertions, 62 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index f18750e3bd6c..4d2c7dfdaabd 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -33,6 +33,7 @@ my %ignore_type = ();
33my @ignore = (); 33my @ignore = ();
34my $help = 0; 34my $help = 0;
35my $configuration_file = ".checkpatch.conf"; 35my $configuration_file = ".checkpatch.conf";
36my $max_line_length = 80;
36 37
37sub help { 38sub help {
38 my ($exitcode) = @_; 39 my ($exitcode) = @_;
@@ -51,6 +52,7 @@ Options:
51 -f, --file treat FILE as regular source file 52 -f, --file treat FILE as regular source file
52 --subjective, --strict enable more subjective tests 53 --subjective, --strict enable more subjective tests
53 --ignore TYPE(,TYPE2...) ignore various comma separated message types 54 --ignore TYPE(,TYPE2...) ignore various comma separated message types
55 --max-line-length=n set the maximum line length, if exceeded, warn
54 --show-types show the message "types" in the output 56 --show-types show the message "types" in the output
55 --root=PATH PATH to the kernel tree root 57 --root=PATH PATH to the kernel tree root
56 --no-summary suppress the per-file summary 58 --no-summary suppress the per-file summary
@@ -107,6 +109,7 @@ GetOptions(
107 'strict!' => \$check, 109 'strict!' => \$check,
108 'ignore=s' => \@ignore, 110 'ignore=s' => \@ignore,
109 'show-types!' => \$show_types, 111 'show-types!' => \$show_types,
112 'max-line-length=i' => \$max_line_length,
110 'root=s' => \$root, 113 'root=s' => \$root,
111 'summary!' => \$summary, 114 'summary!' => \$summary,
112 'mailback!' => \$mailback, 115 'mailback!' => \$mailback,
@@ -227,7 +230,11 @@ our $Inline = qr{inline|__always_inline|noinline};
227our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 230our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
228our $Lval = qr{$Ident(?:$Member)*}; 231our $Lval = qr{$Ident(?:$Member)*};
229 232
230our $Constant = qr{(?i:(?:[0-9]+|0x[0-9a-f]+)[ul]*)}; 233our $Float_hex = qr{(?i:0x[0-9a-f]+p-?[0-9]+[fl]?)};
234our $Float_dec = qr{(?i:((?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?))};
235our $Float_int = qr{(?i:[0-9]+e-?[0-9]+[fl]?)};
236our $Float = qr{$Float_hex|$Float_dec|$Float_int};
237our $Constant = qr{(?:$Float|(?i:(?:0x[0-9a-f]+|[0-9]+)[ul]*))};
231our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)}; 238our $Assignment = qr{(?:\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=)};
232our $Compare = qr{<=|>=|==|!=|<|>}; 239our $Compare = qr{<=|>=|==|!=|<|>};
233our $Operators = qr{ 240our $Operators = qr{
@@ -352,27 +359,6 @@ sub deparenthesize {
352 359
353$chk_signoff = 0 if ($file); 360$chk_signoff = 0 if ($file);
354 361
355my @dep_includes = ();
356my @dep_functions = ();
357my $removal = "Documentation/feature-removal-schedule.txt";
358if ($tree && -f "$root/$removal") {
359 open(my $REMOVE, '<', "$root/$removal") ||
360 die "$P: $removal: open failed - $!\n";
361 while (<$REMOVE>) {
362 if (/^Check:\s+(.*\S)/) {
363 for my $entry (split(/[, ]+/, $1)) {
364 if ($entry =~ m@include/(.*)@) {
365 push(@dep_includes, $1);
366
367 } elsif ($entry !~ m@/@) {
368 push(@dep_functions, $entry);
369 }
370 }
371 }
372 }
373 close($REMOVE);
374}
375
376my @rawlines = (); 362my @rawlines = ();
377my @lines = (); 363my @lines = ();
378my $vname; 364my $vname;
@@ -1412,6 +1398,8 @@ sub process {
1412 my %suppress_export; 1398 my %suppress_export;
1413 my $suppress_statement = 0; 1399 my $suppress_statement = 0;
1414 1400
1401 my %camelcase = ();
1402
1415 # Pre-scan the patch sanitizing the lines. 1403 # Pre-scan the patch sanitizing the lines.
1416 # Pre-scan the patch looking for any __setup documentation. 1404 # Pre-scan the patch looking for any __setup documentation.
1417 # 1405 #
@@ -1757,6 +1745,13 @@ sub process {
1757 #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 1745 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
1758 } 1746 }
1759 1747
1748# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
1749 if ($realfile =~ /Kconfig/ &&
1750 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
1751 WARN("CONFIG_EXPERIMENTAL",
1752 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1753 }
1754
1760 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 1755 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
1761 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 1756 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
1762 my $flag = $1; 1757 my $flag = $1;
@@ -1774,15 +1769,15 @@ sub process {
1774# check we are in a valid source file if not then ignore this hunk 1769# check we are in a valid source file if not then ignore this hunk
1775 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/); 1770 next if ($realfile !~ /\.(h|c|s|S|pl|sh)$/);
1776 1771
1777#80 column limit 1772#line length limit
1778 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 1773 if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ &&
1779 $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 1774 $rawline !~ /^.\s*\*\s*\@$Ident\s/ &&
1780 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || 1775 !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ ||
1781 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 1776 $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) &&
1782 $length > 80) 1777 $length > $max_line_length)
1783 { 1778 {
1784 WARN("LONG_LINE", 1779 WARN("LONG_LINE",
1785 "line over 80 characters\n" . $herecurr); 1780 "line over $max_line_length characters\n" . $herecurr);
1786 } 1781 }
1787 1782
1788# Check for user-visible strings broken across lines, which breaks the ability 1783# Check for user-visible strings broken across lines, which breaks the ability
@@ -1912,6 +1907,12 @@ sub process {
1912# check we are in a valid C source file if not then ignore this hunk 1907# check we are in a valid C source file if not then ignore this hunk
1913 next if ($realfile !~ /\.(h|c)$/); 1908 next if ($realfile !~ /\.(h|c)$/);
1914 1909
1910# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
1911 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
1912 WARN("CONFIG_EXPERIMENTAL",
1913 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
1914 }
1915
1915# check for RCS/CVS revision markers 1916# check for RCS/CVS revision markers
1916 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 1917 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
1917 WARN("CVS_KEYWORD", 1918 WARN("CVS_KEYWORD",
@@ -2225,8 +2226,11 @@ sub process {
2225 my $path = $1; 2226 my $path = $1;
2226 if ($path =~ m{//}) { 2227 if ($path =~ m{//}) {
2227 ERROR("MALFORMED_INCLUDE", 2228 ERROR("MALFORMED_INCLUDE",
2228 "malformed #include filename\n" . 2229 "malformed #include filename\n" . $herecurr);
2229 $herecurr); 2230 }
2231 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
2232 ERROR("UAPI_INCLUDE",
2233 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
2230 } 2234 }
2231 } 2235 }
2232 2236
@@ -2906,12 +2910,17 @@ sub process {
2906 } 2910 }
2907 } 2911 }
2908 2912
2909#studly caps, commented out until figure out how to distinguish between use of existing and adding new 2913#CamelCase
2910# if (($line=~/[\w_][a-z\d]+[A-Z]/) and !($line=~/print/)) { 2914 while ($line =~ m{($Constant|$Lval)}g) {
2911# print "No studly caps, use _\n"; 2915 my $var = $1;
2912# print "$herecurr"; 2916 if ($var !~ /$Constant/ &&
2913# $clean = 0; 2917 $var =~ /[A-Z]\w*[a-z]|[a-z]\w*[A-Z]/ &&
2914# } 2918 !defined $camelcase{$var}) {
2919 $camelcase{$var} = 1;
2920 WARN("CAMELCASE",
2921 "Avoid CamelCase: <$var>\n" . $herecurr);
2922 }
2923 }
2915 2924
2916#no spaces allowed after \ in define 2925#no spaces allowed after \ in define
2917 if ($line=~/\#\s*define.*\\\s$/) { 2926 if ($line=~/\#\s*define.*\\\s$/) {
@@ -3013,6 +3022,17 @@ sub process {
3013 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx"); 3022 "Macros with complex values should be enclosed in parenthesis\n" . "$herectx");
3014 } 3023 }
3015 } 3024 }
3025
3026# check for line continuations outside of #defines, preprocessor #, and asm
3027
3028 } else {
3029 if ($prevline !~ /^..*\\$/ &&
3030 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
3031 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
3032 $line =~ /^\+.*\\$/) {
3033 WARN("LINE_CONTINUATIONS",
3034 "Avoid unnecessary line continuations\n" . $herecurr);
3035 }
3016 } 3036 }
3017 3037
3018# do {} while (0) macro tests: 3038# do {} while (0) macro tests:
@@ -3183,20 +3203,14 @@ sub process {
3183 } 3203 }
3184 } 3204 }
3185 3205
3186# don't include deprecated include files (uses RAW line) 3206# check for unnecessary blank lines around braces
3187 for my $inc (@dep_includes) { 3207 if (($line =~ /^..*}\s*$/ && $prevline =~ /^.\s*$/)) {
3188 if ($rawline =~ m@^.\s*\#\s*include\s*\<$inc>@) { 3208 CHK("BRACES",
3189 ERROR("DEPRECATED_INCLUDE", 3209 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev);
3190 "Don't use <$inc>: see Documentation/feature-removal-schedule.txt\n" . $herecurr);
3191 }
3192 } 3210 }
3193 3211 if (($line =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
3194# don't use deprecated functions 3212 CHK("BRACES",
3195 for my $func (@dep_functions) { 3213 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev);
3196 if ($line =~ /\b$func\b/) {
3197 ERROR("DEPRECATED_FUNCTION",
3198 "Don't use $func(): see Documentation/feature-removal-schedule.txt\n" . $herecurr);
3199 }
3200 } 3214 }
3201 3215
3202# no volatiles please 3216# no volatiles please
@@ -3213,20 +3227,12 @@ sub process {
3213 $herecurr); 3227 $herecurr);
3214 } 3228 }
3215 3229
3216# check for needless kfree() checks 3230# check for needless "if (<foo>) fn(<foo>)" uses
3217 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) { 3231 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
3218 my $expr = $1; 3232 my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;';
3219 if ($line =~ /\bkfree\(\Q$expr\E\);/) { 3233 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) {
3220 WARN("NEEDLESS_KFREE", 3234 WARN('NEEDLESS_IF',
3221 "kfree(NULL) is safe this check is probably not required\n" . $hereprev); 3235 "$1(NULL) is safe this check is probably not required\n" . $hereprev);
3222 }
3223 }
3224# check for needless usb_free_urb() checks
3225 if ($prevline =~ /\bif\s*\(([^\)]*)\)/) {
3226 my $expr = $1;
3227 if ($line =~ /\busb_free_urb\(\Q$expr\E\);/) {
3228 WARN("NEEDLESS_USB_FREE_URB",
3229 "usb_free_urb(NULL) is safe this check is probably not required\n" . $hereprev);
3230 } 3236 }
3231 } 3237 }
3232 3238
@@ -3344,6 +3350,12 @@ sub process {
3344 "Avoid line continuations in quoted strings\n" . $herecurr); 3350 "Avoid line continuations in quoted strings\n" . $herecurr);
3345 } 3351 }
3346 3352
3353# check for struct spinlock declarations
3354 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
3355 WARN("USE_SPINLOCK_T",
3356 "struct spinlock should be spinlock_t\n" . $herecurr);
3357 }
3358
3347# Check for misused memsets 3359# Check for misused memsets
3348 if ($^V && $^V ge 5.10.0 && 3360 if ($^V && $^V ge 5.10.0 &&
3349 defined $stat && 3361 defined $stat &&
@@ -3450,8 +3462,22 @@ sub process {
3450 3462
3451# check for multiple semicolons 3463# check for multiple semicolons
3452 if ($line =~ /;\s*;\s*$/) { 3464 if ($line =~ /;\s*;\s*$/) {
3453 WARN("ONE_SEMICOLON", 3465 WARN("ONE_SEMICOLON",
3454 "Statements terminations use 1 semicolon\n" . $herecurr); 3466 "Statements terminations use 1 semicolon\n" . $herecurr);
3467 }
3468
3469# check for switch/default statements without a break;
3470 if ($^V && $^V ge 5.10.0 &&
3471 defined $stat &&
3472 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
3473 my $ctx = '';
3474 my $herectx = $here . "\n";
3475 my $cnt = statement_rawlines($stat);
3476 for (my $n = 0; $n < $cnt; $n++) {
3477 $herectx .= raw_line($linenr, $n) . "\n";
3478 }
3479 WARN("DEFAULT_NO_BREAK",
3480 "switch default: should use break\n" . $herectx);
3455 } 3481 }
3456 3482
3457# check for gcc specific __FUNCTION__ 3483# check for gcc specific __FUNCTION__