aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/checkpatch.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/checkpatch.pl')
-rwxr-xr-xscripts/checkpatch.pl171
1 files changed, 121 insertions, 50 deletions
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 47016c304c84..9c9810030377 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;
@@ -3274,6 +3289,7 @@ sub process {
3274 } 3289 }
3275 } 3290 }
3276 if (!defined $suppress_whiletrailers{$linenr} && 3291 if (!defined $suppress_whiletrailers{$linenr} &&
3292 defined($stat) && defined($cond) &&
3277 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 3293 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
3278 my ($s, $c) = ($stat, $cond); 3294 my ($s, $c) = ($stat, $cond);
3279 3295
@@ -3396,7 +3412,13 @@ sub process {
3396 while ($var =~ m{($Ident)}g) { 3412 while ($var =~ m{($Ident)}g) {
3397 my $word = $1; 3413 my $word = $1;
3398 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 3414 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
3399 seed_camelcase_includes() if ($check); 3415 if ($check) {
3416 seed_camelcase_includes();
3417 if (!$file && !$camelcase_file_seeded) {
3418 seed_camelcase_file($realfile);
3419 $camelcase_file_seeded = 1;
3420 }
3421 }
3400 if (!defined $camelcase{$word}) { 3422 if (!defined $camelcase{$word}) {
3401 $camelcase{$word} = 1; 3423 $camelcase{$word} = 1;
3402 CHK("CAMELCASE", 3424 CHK("CAMELCASE",
@@ -3725,14 +3747,6 @@ sub process {
3725 } 3747 }
3726 } 3748 }
3727 3749
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) 3750# check for bad placement of section $InitAttribute (e.g.: __initdata)
3737 if ($line =~ /(\b$InitAttribute\b)/) { 3751 if ($line =~ /(\b$InitAttribute\b)/) {
3738 my $attr = $1; 3752 my $attr = $1;
@@ -3751,6 +3765,35 @@ sub string_find_replace {
3751 } 3765 }
3752 } 3766 }
3753 3767
3768# check for $InitAttributeData (ie: __initdata) with const
3769 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
3770 my $attr = $1;
3771 $attr =~ /($InitAttributePrefix)(.*)/;
3772 my $attr_prefix = $1;
3773 my $attr_type = $2;
3774 if (ERROR("INIT_ATTRIBUTE",
3775 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
3776 $fix) {
3777 $fixed[$linenr - 1] =~
3778 s/$InitAttributeData/${attr_prefix}initconst/;
3779 }
3780 }
3781
3782# check for $InitAttributeConst (ie: __initconst) without const
3783 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
3784 my $attr = $1;
3785 if (ERROR("INIT_ATTRIBUTE",
3786 "Use of $attr requires a separate use of const\n" . $herecurr) &&
3787 $fix) {
3788 my $lead = $fixed[$linenr - 1] =~
3789 /(^\+\s*(?:static\s+))/;
3790 $lead = rtrim($1);
3791 $lead = "$lead " if ($lead !~ /^\+$/);
3792 $lead = "${lead}const ";
3793 $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
3794 }
3795 }
3796
3754# prefer usleep_range over udelay 3797# prefer usleep_range over udelay
3755 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 3798 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
3756 # ignore udelay's < 10, however 3799 # ignore udelay's < 10, however
@@ -3810,8 +3853,8 @@ sub string_find_replace {
3810# check for memory barriers without a comment. 3853# 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)\(/) { 3854 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)) { 3855 if (!ctx_has_comment($first_line, $linenr)) {
3813 CHK("MEMORY_BARRIER", 3856 WARN("MEMORY_BARRIER",
3814 "memory barrier without comment\n" . $herecurr); 3857 "memory barrier without comment\n" . $herecurr);
3815 } 3858 }
3816 } 3859 }
3817# check of hardware specific defines 3860# check of hardware specific defines
@@ -3835,7 +3878,8 @@ sub string_find_replace {
3835 } 3878 }
3836 3879
3837# Check for __inline__ and __inline, prefer inline 3880# Check for __inline__ and __inline, prefer inline
3838 if ($line =~ /\b(__inline__|__inline)\b/) { 3881 if ($realfile !~ m@\binclude/uapi/@ &&
3882 $line =~ /\b(__inline__|__inline)\b/) {
3839 if (WARN("INLINE", 3883 if (WARN("INLINE",
3840 "plain inline is preferred over $1\n" . $herecurr) && 3884 "plain inline is preferred over $1\n" . $herecurr) &&
3841 $fix) { 3885 $fix) {
@@ -3845,19 +3889,22 @@ sub string_find_replace {
3845 } 3889 }
3846 3890
3847# Check for __attribute__ packed, prefer __packed 3891# Check for __attribute__ packed, prefer __packed
3848 if ($line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 3892 if ($realfile !~ m@\binclude/uapi/@ &&
3893 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
3849 WARN("PREFER_PACKED", 3894 WARN("PREFER_PACKED",
3850 "__packed is preferred over __attribute__((packed))\n" . $herecurr); 3895 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
3851 } 3896 }
3852 3897
3853# Check for __attribute__ aligned, prefer __aligned 3898# Check for __attribute__ aligned, prefer __aligned
3854 if ($line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 3899 if ($realfile !~ m@\binclude/uapi/@ &&
3900 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
3855 WARN("PREFER_ALIGNED", 3901 WARN("PREFER_ALIGNED",
3856 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 3902 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
3857 } 3903 }
3858 3904
3859# Check for __attribute__ format(printf, prefer __printf 3905# Check for __attribute__ format(printf, prefer __printf
3860 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 3906 if ($realfile !~ m@\binclude/uapi/@ &&
3907 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
3861 if (WARN("PREFER_PRINTF", 3908 if (WARN("PREFER_PRINTF",
3862 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 3909 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
3863 $fix) { 3910 $fix) {
@@ -3867,7 +3914,8 @@ sub string_find_replace {
3867 } 3914 }
3868 3915
3869# Check for __attribute__ format(scanf, prefer __scanf 3916# Check for __attribute__ format(scanf, prefer __scanf
3870 if ($line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 3917 if ($realfile !~ m@\binclude/uapi/@ &&
3918 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
3871 if (WARN("PREFER_SCANF", 3919 if (WARN("PREFER_SCANF",
3872 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 3920 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
3873 $fix) { 3921 $fix) {
@@ -3903,9 +3951,9 @@ sub string_find_replace {
3903 } 3951 }
3904 3952
3905# check for seq_printf uses that could be seq_puts 3953# check for seq_printf uses that could be seq_puts
3906 if ($line =~ /\bseq_printf\s*\(/) { 3954 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
3907 my $fmt = get_quoted_string($line, $rawline); 3955 my $fmt = get_quoted_string($line, $rawline);
3908 if ($fmt !~ /[^\\]\%/) { 3956 if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
3909 if (WARN("PREFER_SEQ_PUTS", 3957 if (WARN("PREFER_SEQ_PUTS",
3910 "Prefer seq_puts to seq_printf\n" . $herecurr) && 3958 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
3911 $fix) { 3959 $fix) {
@@ -3972,11 +4020,28 @@ sub string_find_replace {
3972 } 4020 }
3973 } 4021 }
3974 4022
4023# check for naked sscanf
4024 if ($^V && $^V ge 5.10.0 &&
4025 defined $stat &&
4026 $stat =~ /\bsscanf\b/ &&
4027 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
4028 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
4029 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
4030 my $lc = $stat =~ tr@\n@@;
4031 $lc = $lc + $linenr;
4032 my $stat_real = raw_line($linenr, 0);
4033 for (my $count = $linenr + 1; $count <= $lc; $count++) {
4034 $stat_real = $stat_real . "\n" . raw_line($count, 0);
4035 }
4036 WARN("NAKED_SSCANF",
4037 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
4038 }
4039
3975# check for new externs in .h files. 4040# check for new externs in .h files.
3976 if ($realfile =~ /\.h$/ && 4041 if ($realfile =~ /\.h$/ &&
3977 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 4042 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
3978 if (WARN("AVOID_EXTERNS", 4043 if (CHK("AVOID_EXTERNS",
3979 "extern prototypes should be avoided in .h files\n" . $herecurr) && 4044 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
3980 $fix) { 4045 $fix) {
3981 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 4046 $fixed[$linenr - 1] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
3982 } 4047 }
@@ -4190,6 +4255,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); 4255 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
4191 } 4256 }
4192 4257
4258# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
4259 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
4260 ERROR("DEFINE_ARCH_HAS",
4261 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
4262 }
4263
4193# check for %L{u,d,i} in strings 4264# check for %L{u,d,i} in strings
4194 my $string; 4265 my $string;
4195 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 4266 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {