checkpatch: add test for repeated const uses
authorJoe Perches <joe@perches.com>
Thu, 16 Apr 2015 19:44:22 +0000 (12:44 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 17 Apr 2015 13:03:57 +0000 (09:03 -0400)
Using 'const <type> const *' is generally meant to be written 'const
<type> * const'.

Add a test for the miswritten form.

Signed-off-by: Joe Perches <joe@perches.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
scripts/checkpatch.pl

index 45babf2243f3e9da1e1ff81cab892d010c1f90f4..d54a814a4bc813cbaf40876b487d73a1d3cf54fb 100755 (executable)
@@ -323,6 +323,7 @@ our $Operators      = qr{
 
 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
 
+our $BasicType;
 our $NonptrType;
 our $NonptrTypeMisordered;
 our $NonptrTypeWithAttr;
@@ -514,6 +515,11 @@ sub build_types {
        my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
        my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
        $Modifier       = qr{(?:$Attribute|$Sparse|$mods)};
+       $BasicType      = qr{
+                               (?:$typeOtherOSTypedefs\b)|
+                               (?:$typeTypedefs\b)|
+                               (?:${all}\b)
+               }x;
        $NonptrType     = qr{
                        (?:$Modifier\s+|const\s+)*
                        (?:
@@ -3192,6 +3198,18 @@ sub process {
                                $herecurr);
                }
 
+# check for const <foo> const where <foo> is not a pointer or array type
+               if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
+                       my $found = $1;
+                       if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
+                               WARN("CONST_CONST",
+                                    "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
+                       } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
+                               WARN("CONST_CONST",
+                                    "'const $found const' should probably be 'const $found'\n" . $herecurr);
+                       }
+               }
+
 # check for non-global char *foo[] = {"bar", ...} declarations.
                if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
                        WARN("STATIC_CONST_CHAR_ARRAY",