aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl166
1 files changed, 118 insertions, 48 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 66cad506b8a2..61090e0ff613 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -241,8 +241,11 @@ our $Sparse = qr{
241 __ref| 241 __ref|
242 __rcu 242 __rcu
243 }x; 243 }x;
244 244our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
245our $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)}; 245our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
246our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
247our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
248our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
246 249
247# Notes to $Attribute: 250# Notes to $Attribute:
248# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 251# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
@@ -323,7 +326,8 @@ our $logFunctions = qr{(?x:
323 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 326 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
324 WARN(?:_RATELIMIT|_ONCE|)| 327 WARN(?:_RATELIMIT|_ONCE|)|
325 panic| 328 panic|
326 MODULE_[A-Z_]+ 329 MODULE_[A-Z_]+|
330 seq_vprintf|seq_printf|seq_puts
327)}; 331)};
328 332
329our $signature_tags = qr{(?xi: 333our $signature_tags = qr{(?xi:
@@ -442,8 +446,9 @@ sub seed_camelcase_file {
442 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 446 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
443 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 447 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
444 $camelcase{$1} = 1; 448 $camelcase{$1} = 1;
445 } 449 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
446 elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) { 450 $camelcase{$1} = 1;
451 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
447 $camelcase{$1} = 1; 452 $camelcase{$1} = 1;
448 } 453 }
449 } 454 }
@@ -1512,6 +1517,14 @@ sub rtrim {
1512 return $string; 1517 return $string;
1513} 1518}
1514 1519
1520sub string_find_replace {
1521 my ($string, $find, $replace) = @_;
1522
1523 $string =~ s/$find/$replace/g;
1524
1525 return $string;
1526}
1527
1515sub tabify { 1528sub tabify {
1516 my ($leading) = @_; 1529 my ($leading) = @_;
1517 1530
@@ -1612,6 +1625,8 @@ sub process {
1612 my @setup_docs = (); 1625 my @setup_docs = ();
1613 my $setup_docs = 0; 1626 my $setup_docs = 0;
1614 1627
1628 my $camelcase_file_seeded = 0;
1629
1615 sanitise_line_reset(); 1630 sanitise_line_reset();
1616 my $line; 1631 my $line;
1617 foreach my $rawline (@rawlines) { 1632 foreach my $rawline (@rawlines) {
@@ -1754,11 +1769,11 @@ sub process {
1754 # extract the filename as it passes 1769 # extract the filename as it passes
1755 if ($line =~ /^diff --git.*?(\S+)$/) { 1770 if ($line =~ /^diff --git.*?(\S+)$/) {
1756 $realfile = $1; 1771 $realfile = $1;
1757 $realfile =~ s@^([^/]*)/@@; 1772 $realfile =~ s@^([^/]*)/@@ if (!$file);
1758 $in_commit_log = 0; 1773 $in_commit_log = 0;
1759 } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 1774 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
1760 $realfile = $1; 1775 $realfile = $1;
1761 $realfile =~ s@^([^/]*)/@@; 1776 $realfile =~ s@^([^/]*)/@@ if (!$file);
1762 $in_commit_log = 0; 1777 $in_commit_log = 0;
1763 1778
1764 $p1_prefix = $1; 1779 $p1_prefix = $1;
@@ -1947,6 +1962,18 @@ sub process {
1947 $rpt_cleaners = 1; 1962 $rpt_cleaners = 1;
1948 } 1963 }
1949 1964
1965# Check for FSF mailing addresses.
1966 if ($rawline =~ /You should have received a copy/ ||
1967 $rawline =~ /write to the Free Software/ ||
1968 $rawline =~ /59 Temple Place/ ||
1969 $rawline =~ /51 Franklin Street/) {
1970 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
1971 my $msg_type = \&ERROR;
1972 $msg_type = \&CHK if ($file);
1973 &{$msg_type}("FSF_MAILING_ADDRESS",
1974 "Do not include the paragraph about writing to the Free Software Foundation's mailing address from the sample GPL notice. The FSF has changed addresses in the past, and may do so again. Linux already includes a copy of the GPL.\n" . $herevet)
1975 }
1976
1950# check for Kconfig help text having a real description 1977# check for Kconfig help text having a real description
1951# Only applies when adding the entry originally, after that we do not have 1978# Only applies when adding the entry originally, after that we do not have
1952# sufficient context to determine whether it is indeed long enough. 1979# sufficient context to determine whether it is indeed long enough.
@@ -2838,7 +2865,7 @@ sub process {
2838 \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 2865 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
2839 =>|->|<<|>>|<|>|=|!|~| 2866 =>|->|<<|>>|<|>|=|!|~|
2840 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 2867 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
2841 \?|: 2868 \?:|\?|:
2842 }x; 2869 }x;
2843 my @elements = split(/($ops|;)/, $opline); 2870 my @elements = split(/($ops|;)/, $opline);
2844 2871
@@ -3061,15 +3088,13 @@ sub process {
3061 $ok = 1; 3088 $ok = 1;
3062 } 3089 }
3063 3090
3064 # Ignore ?: 3091 # messages are ERROR, but ?: are CHK
3065 if (($opv eq ':O' && $ca =~ /\?$/) ||
3066 ($op eq '?' && $cc =~ /^:/)) {
3067 $ok = 1;
3068 }
3069
3070 if ($ok == 0) { 3092 if ($ok == 0) {
3071 if (ERROR("SPACING", 3093 my $msg_type = \&ERROR;
3072 "spaces required around that '$op' $at\n" . $hereptr)) { 3094 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
3095
3096 if (&{$msg_type}("SPACING",
3097 "spaces required around that '$op' $at\n" . $hereptr)) {
3073 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3098 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
3074 if (defined $fix_elements[$n + 2]) { 3099 if (defined $fix_elements[$n + 2]) {
3075 $fix_elements[$n + 2] =~ s/^\s+//; 3100 $fix_elements[$n + 2] =~ s/^\s+//;
@@ -3208,21 +3233,10 @@ sub process {
3208 } 3233 }
3209 3234
3210# Return is not a function. 3235# Return is not a function.
3211 if (defined($stat) && $stat =~ /^.\s*return(\s*)(\(.*);/s) { 3236 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
3212 my $spacing = $1; 3237 my $spacing = $1;
3213 my $value = $2; 3238 if ($^V && $^V ge 5.10.0 &&
3214 3239 $stat =~ /^.\s*return\s*$balanced_parens\s*;\s*$/) {
3215 # Flatten any parentheses
3216 $value =~ s/\(/ \(/g;
3217 $value =~ s/\)/\) /g;
3218 while ($value =~ s/\[[^\[\]]*\]/1/ ||
3219 $value !~ /(?:$Ident|-?$Constant)\s*
3220 $Compare\s*
3221 (?:$Ident|-?$Constant)/x &&
3222 $value =~ s/\([^\(\)]*\)/1/) {
3223 }
3224#print "value<$value>\n";
3225 if ($value =~ /^\s*(?:$Ident|-?$Constant)\s*$/) {
3226 ERROR("RETURN_PARENTHESES", 3240 ERROR("RETURN_PARENTHESES",
3227 "return is not a function, parentheses are not required\n" . $herecurr); 3241 "return is not a function, parentheses are not required\n" . $herecurr);
3228 3242
@@ -3231,6 +3245,7 @@ sub process {
3231 "space required before the open parenthesis '('\n" . $herecurr); 3245 "space required before the open parenthesis '('\n" . $herecurr);
3232 } 3246 }
3233 } 3247 }
3248
3234# Return of what appears to be an errno should normally be -'ve 3249# Return of what appears to be an errno should normally be -'ve
3235 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 3250 if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) {
3236 my $name = $1; 3251 my $name = $1;
@@ -3396,7 +3411,13 @@ sub process {
3396 while ($var =~ m{($Ident)}g) { 3411 while ($var =~ m{($Ident)}g) {
3397 my $word = $1; 3412 my $word = $1;
3398 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 3413 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3399 seed_camelcase_includes() if ($check); 3414 if ($check) {
3415 seed_camelcase_includes();
3416 if (!$file && !$camelcase_file_seeded) {
3417 seed_camelcase_file($realfile);
3418 $camelcase_file_seeded = 1;
3419 }
3420 }
3400 if (!defined $camelcase{$word}) { 3421 if (!defined $camelcase{$word}) {
3401 $camelcase{$word} = 1; 3422 $camelcase{$word} = 1;
3402 CHK("CAMELCASE", 3423 CHK("CAMELCASE",
@@ -3725,14 +3746,6 @@ sub process {
3725 } 3746 }
3726 } 3747 }
3727 3748
3728sub string_find_replace {
3729 my ($string, $find, $replace) = @_;
3730
3731 $string =~ s/$find/$replace/g;
3732
3733 return $string;
3734}
3735
3736# check for bad placement of section $InitAttribute (e.g.: __initdata) 3749# check for bad placement of section $InitAttribute (e.g.: __initdata)
3737 if ($line =~ /(\b$InitAttribute\b)/) { 3750 if ($line =~ /(\b$InitAttribute\b)/) {
3738 my $attr = $1; 3751 my $attr = $1;
@@ -3751,6 +3764,35 @@ sub string_find_replace {
3751 } 3764 }
3752 } 3765 }
3753 3766
3767# check for $InitAttributeData (ie: __initdata) with const
3768 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
3769 my $attr = $1;
3770 $attr =~ /($InitAttributePrefix)(.*)/;
3771 my $attr_prefix = $1;
3772 my $attr_type = $2;
3773 if (ERROR("INIT_ATTRIBUTE",
3774 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
3775 $fix) {
3776 $fixed[$linenr - 1] =~
3777 s/$InitAttributeData/${attr_prefix}initconst/;
3778 }
3779 }
3780
3781# check for $InitAttributeConst (ie: __initconst) without const
3782 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
3783 my $attr = $1;
3784 if (ERROR("INIT_ATTRIBUTE",
3785 "Use of $attr requires a separate use of const\n" . $herecurr) &&
3786 $fix) {
3787 my $lead = $fixed[$linenr - 1] =~
3788 /(^\+\s*(?:static\s+))/;
3789 $lead = rtrim($1);
3790 $lead = "$lead " if ($lead !~ /^\+$/);
3791 $lead = "${lead}const ";
3792 $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
3793 }
3794 }
3795
3754# prefer usleep_range over udelay 3796# prefer usleep_range over udelay
3755 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 3797 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
3756 # ignore udelay's < 10, however 3798 # ignore udelay's < 10, however
@@ -3810,8 +3852,8 @@ sub string_find_replace {
3810# check for memory barriers without a comment. 3852# check for memory barriers without a comment.
3811 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 3853 if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
3812 if (!ctx_has_comment($first_line, $linenr)) { 3854 if (!ctx_has_comment($first_line, $linenr)) {
3813 CHK("MEMORY_BARRIER", 3855 WARN("MEMORY_BARRIER",
3814 "memory barrier without comment\n" . $herecurr); 3856 "memory barrier without comment\n" . $herecurr);
3815 } 3857 }
3816 } 3858 }
3817# check of hardware specific defines 3859# check of hardware specific defines
@@ -3835,7 +3877,8 @@ sub string_find_replace {
3835 } 3877 }
3836 3878
3837# Check for __inline__ and __inline, prefer inline 3879# Check for __inline__ and __inline, prefer inline
3838 if ($line =~ /\b(__inline__|__inline)\b/) { 3880 if ($realfile !~ m@\binclude/uapi/@ &&
3881 $line =~ /\b(__inline__|__inline)\b/) {
3839 if (WARN("INLINE", 3882 if (WARN("INLINE",
3840 "plain inline is preferred over $1\n" . $herecurr) && 3883 "plain inline is preferred over $1\n" . $herecurr) &&
3841 $fix) { 3884 $fix) {
@@ -3845,19 +3888,22 @@ sub string_find_replace {
3845 } 3888 }
3846 3889
3847# Check for __attribute__ packed, prefer __packed 3890# Check for __attribute__ packed, prefer __packed
3848 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 3891 if ($realfile !~ m@\binclude/uapi/@ &&
3892 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3849 WARN("PREFER_PACKED", 3893 WARN("PREFER_PACKED",
3850 "__packed is preferred over __attribute__((packed))\n" . $herecurr); 3894 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3851 } 3895 }
3852 3896
3853# Check for __attribute__ aligned, prefer __aligned 3897# Check for __attribute__ aligned, prefer __aligned
3854 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 3898 if ($realfile !~ m@\binclude/uapi/@ &&
3899 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3855 WARN("PREFER_ALIGNED", 3900 WARN("PREFER_ALIGNED",
3856 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 3901 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3857 } 3902 }
3858 3903
3859# Check for __attribute__ format(printf, prefer __printf 3904# Check for __attribute__ format(printf, prefer __printf
3860 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 3905 if ($realfile !~ m@\binclude/uapi/@ &&
3906 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3861 if (WARN("PREFER_PRINTF", 3907 if (WARN("PREFER_PRINTF",
3862 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 3908 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3863 $fix) { 3909 $fix) {
@@ -3867,7 +3913,8 @@ sub string_find_replace {
3867 } 3913 }
3868 3914
3869# Check for __attribute__ format(scanf, prefer __scanf 3915# Check for __attribute__ format(scanf, prefer __scanf
3870 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 3916 if ($realfile !~ m@\binclude/uapi/@ &&
3917 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3871 if (WARN("PREFER_SCANF", 3918 if (WARN("PREFER_SCANF",
3872 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 3919 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3873 $fix) { 3920 $fix) {
@@ -3903,9 +3950,9 @@ sub string_find_replace {
3903 } 3950 }
3904 3951
3905# check for seq_printf uses that could be seq_puts 3952# check for seq_printf uses that could be seq_puts
3906 if ($line =~ /\bseq_printf\s*\(/) { 3953 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
3907 my $fmt = get_quoted_string($line, $rawline); 3954 my $fmt = get_quoted_string($line, $rawline);
3908 if ($fmt !~ /[^\\]\%/) { 3955 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
3909 if (WARN("PREFER_SEQ_PUTS", 3956 if (WARN("PREFER_SEQ_PUTS",
3910 "Prefer seq_puts to seq_printf\n" . $herecurr) && 3957 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3911 $fix) { 3958 $fix) {
@@ -3972,6 +4019,23 @@ sub string_find_replace {
3972 } 4019 }
3973 } 4020 }
3974 4021
4022# check for naked sscanf
4023 if ($^V && $^V ge 5.10.0 &&
4024 defined $stat &&
4025 $stat =~ /\bsscanf\b/ &&
4026 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4027 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4028 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4029 my $lc = $stat =~ tr@\n@@;
4030 $lc = $lc + $linenr;
4031 my $stat_real = raw_line($linenr, 0);
4032 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4033 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4034 }
4035 WARN("NAKED_SSCANF",
4036 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4037 }
4038
3975# check for new externs in .h files. 4039# check for new externs in .h files.
3976 if ($realfile =~ /\.h$/ && 4040 if ($realfile =~ /\.h$/ &&
3977 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 4041 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
@@ -4190,6 +4254,12 @@ sub string_find_replace {
4190 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 4254 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4191 } 4255 }
4192 4256
4257# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4258 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4259 ERROR("DEFINE_ARCH_HAS",
4260 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4261 }
4262
4193# check for %L{u,d,i} in strings 4263# check for %L{u,d,i} in strings
4194 my $string; 4264 my $string;
4195 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 4265 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {