Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

checkpatch: add test for repeated const uses

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>

authored by

Joe Perches and committed by
Linus Torvalds
ab7e23f3 f34e4a4f

+18
+18
scripts/checkpatch.pl
··· 323 323 324 324 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 325 325 326 + our $BasicType; 326 327 our $NonptrType; 327 328 our $NonptrTypeMisordered; 328 329 our $NonptrTypeWithAttr; ··· 515 514 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 516 515 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 517 516 $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 517 + $BasicType = qr{ 518 + (?:$typeOtherOSTypedefs\b)| 519 + (?:$typeTypedefs\b)| 520 + (?:${all}\b) 521 + }x; 518 522 $NonptrType = qr{ 519 523 (?:$Modifier\s+|const\s+)* 520 524 (?: ··· 3197 3191 "static char array declaration should probably be static const char\n" . 3198 3192 $herecurr); 3199 3193 } 3194 + 3195 + # check for const <foo> const where <foo> is not a pointer or array type 3196 + if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 3197 + my $found = $1; 3198 + if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 3199 + WARN("CONST_CONST", 3200 + "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 3201 + } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 3202 + WARN("CONST_CONST", 3203 + "'const $found const' should probably be 'const $found'\n" . $herecurr); 3204 + } 3205 + } 3200 3206 3201 3207 # check for non-global char *foo[] = {"bar", ...} declarations. 3202 3208 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {