]> Pileus Git - ~andy/linux/blobdiff - scripts/checkpatch.pl
checkpatch: make the memory barrier test noisier
[~andy/linux] / scripts / checkpatch.pl
index 66cad506b8a2a944f2873856ef0078445f673b8b..2e40f64ed77f656b340c1e49bd5c541528ea397a 100755 (executable)
@@ -241,8 +241,11 @@ our $Sparse        = qr{
                        __ref|
                        __rcu
                }x;
-
-our $InitAttribute = qr{__(?:mem|cpu|dev|net_|)(?:initdata|initconst|init\b)};
+our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
+our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
+our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
+our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
+our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
 
 # Notes to $Attribute:
 # We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
@@ -323,7 +326,8 @@ our $logFunctions = qr{(?x:
        (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
        WARN(?:_RATELIMIT|_ONCE|)|
        panic|
-       MODULE_[A-Z_]+
+       MODULE_[A-Z_]+|
+       seq_vprintf|seq_printf|seq_puts
 )};
 
 our $signature_tags = qr{(?xi:
@@ -442,8 +446,9 @@ sub seed_camelcase_file {
                next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
                if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
                        $camelcase{$1} = 1;
-               }
-               elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*\(/) {
+               } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
+                       $camelcase{$1} = 1;
+               } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
                        $camelcase{$1} = 1;
                }
        }
@@ -1512,6 +1517,14 @@ sub rtrim {
        return $string;
 }
 
+sub string_find_replace {
+       my ($string, $find, $replace) = @_;
+
+       $string =~ s/$find/$replace/g;
+
+       return $string;
+}
+
 sub tabify {
        my ($leading) = @_;
 
@@ -1612,6 +1625,8 @@ sub process {
        my @setup_docs = ();
        my $setup_docs = 0;
 
+       my $camelcase_file_seeded = 0;
+
        sanitise_line_reset();
        my $line;
        foreach my $rawline (@rawlines) {
@@ -2838,7 +2853,7 @@ sub process {
                                \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
                                =>|->|<<|>>|<|>|=|!|~|
                                &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
-                               \?|:
+                               \?:|\?|:
                        }x;
                        my @elements = split(/($ops|;)/, $opline);
 
@@ -3061,15 +3076,13 @@ sub process {
                                                $ok = 1;
                                        }
 
-                                       # Ignore ?:
-                                       if (($opv eq ':O' && $ca =~ /\?$/) ||
-                                           ($op eq '?' && $cc =~ /^:/)) {
-                                               $ok = 1;
-                                       }
-
+                                       # messages are ERROR, but ?: are CHK
                                        if ($ok == 0) {
-                                               if (ERROR("SPACING",
-                                                         "spaces required around that '$op' $at\n" . $hereptr)) {
+                                               my $msg_type = \&ERROR;
+                                               $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
+
+                                               if (&{$msg_type}("SPACING",
+                                                                "spaces required around that '$op' $at\n" . $hereptr)) {
                                                        $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
                                                        if (defined $fix_elements[$n + 2]) {
                                                                $fix_elements[$n + 2] =~ s/^\s+//;
@@ -3396,7 +3409,13 @@ sub process {
                                while ($var =~ m{($Ident)}g) {
                                        my $word = $1;
                                        next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
-                                       seed_camelcase_includes() if ($check);
+                                       if ($check) {
+                                               seed_camelcase_includes();
+                                               if (!$file && !$camelcase_file_seeded) {
+                                                       seed_camelcase_file($realfile);
+                                                       $camelcase_file_seeded = 1;
+                                               }
+                                       }
                                        if (!defined $camelcase{$word}) {
                                                $camelcase{$word} = 1;
                                                CHK("CAMELCASE",
@@ -3725,14 +3744,6 @@ sub process {
                        }
                }
 
-sub string_find_replace {
-       my ($string, $find, $replace) = @_;
-
-       $string =~ s/$find/$replace/g;
-
-       return $string;
-}
-
 # check for bad placement of section $InitAttribute (e.g.: __initdata)
                if ($line =~ /(\b$InitAttribute\b)/) {
                        my $attr = $1;
@@ -3751,6 +3762,35 @@ sub string_find_replace {
                        }
                }
 
+# check for $InitAttributeData (ie: __initdata) with const
+               if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
+                       my $attr = $1;
+                       $attr =~ /($InitAttributePrefix)(.*)/;
+                       my $attr_prefix = $1;
+                       my $attr_type = $2;
+                       if (ERROR("INIT_ATTRIBUTE",
+                                 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
+                           $fix) {
+                               $fixed[$linenr - 1] =~
+                                   s/$InitAttributeData/${attr_prefix}initconst/;
+                       }
+               }
+
+# check for $InitAttributeConst (ie: __initconst) without const
+               if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
+                       my $attr = $1;
+                       if (ERROR("INIT_ATTRIBUTE",
+                                 "Use of $attr requires a separate use of const\n" . $herecurr) &&
+                           $fix) {
+                               my $lead = $fixed[$linenr - 1] =~
+                                   /(^\+\s*(?:static\s+))/;
+                               $lead = rtrim($1);
+                               $lead = "$lead " if ($lead !~ /^\+$/);
+                               $lead = "${lead}const ";
+                               $fixed[$linenr - 1] =~ s/(^\+\s*(?:static\s+))/$lead/;
+                       }
+               }
+
 # prefer usleep_range over udelay
                if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
                        # ignore udelay's < 10, however
@@ -3810,8 +3850,8 @@ sub string_find_replace {
 # check for memory barriers without a comment.
                if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) {
                        if (!ctx_has_comment($first_line, $linenr)) {
-                               CHK("MEMORY_BARRIER",
-                                   "memory barrier without comment\n" . $herecurr);
+                               WARN("MEMORY_BARRIER",
+                                    "memory barrier without comment\n" . $herecurr);
                        }
                }
 # check of hardware specific defines
@@ -3903,9 +3943,9 @@ sub string_find_replace {
                }
 
 # check for seq_printf uses that could be seq_puts
-               if ($line =~ /\bseq_printf\s*\(/) {
+               if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
                        my $fmt = get_quoted_string($line, $rawline);
-                       if ($fmt !~ /[^\\]\%/) {
+                       if ($fmt ne "" && $fmt !~ /[^\\]\%/) {
                                if (WARN("PREFER_SEQ_PUTS",
                                         "Prefer seq_puts to seq_printf\n" . $herecurr) &&
                                    $fix) {
@@ -4190,6 +4230,12 @@ sub string_find_replace {
                             "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
                }
 
+# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
+               if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
+                       ERROR("DEFINE_ARCH_HAS",
+                             "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
+               }
+
 # check for %L{u,d,i} in strings
                my $string;
                while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {