History

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.

Joe Perches committed 11 years ago

Commit ab7e23f1 parent f34e4a41 file · +18 −0Browse files

Showing the changes to scripts/checkpatch.pl (1 of 1 file). Show all

Unified · Split

scripts/checkpatch.pl+18−0
@@ -323,6 +323,7 @@ our $Operators = qr{ · 322 unmodified lines
323323
324324 our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
325325
326+our $BasicType;
326327 our $NonptrType;
327328 our $NonptrTypeMisordered;
328329 our $NonptrTypeWithAttr;
@@ -514,6 +515,11 @@ sub build_types { · 185 unmodified lines
514515 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
515516 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
516517 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
518+ $BasicType = qr{
519+ (?:$typeOtherOSTypedefs\b)|
520+ (?:$typeTypedefs\b)|
521+ (?:${all}\b)
522+ }x;
517523 $NonptrType = qr{
518524 (?:$Modifier\s+|const\s+)*
519525 (?:
@@ -3192,6 +3198,18 @@ sub process { · 2672 unmodified lines
31923198 $herecurr);
31933199 }
31943200
3201+# check for const <foo> const where <foo> is not a pointer or array type
3202+ if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3203+ my $found = $1;
3204+ if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3205+ WARN("CONST_CONST",
3206+ "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3207+ } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3208+ WARN("CONST_CONST",
3209+ "'const $found const' should probably be 'const $found'\n" . $herecurr);
3210+ }
3211+ }
3212+
31953213 # check for non-global char *foo[] = {"bar", ...} declarations.
31963214 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
31973215 WARN("STATIC_CONST_CHAR_ARRAY",

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t