Skip to contentSkip to conversation

checkpatch: exclude forward declarations of const structs

PATCH Superseded Taylor Nelms 2 participants 4 messages Mar 31 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch: exclude forward declarations of const structs

Limit checkpatch warnings for normally-const structs by excluding patterns consistent with forward declarations.

For example, the forward declaration struct regmap_access_table; in a header file currently generates a warning recommending that it is generally declared as const; however, this would apply a useless type qualifier in the empty declaration const struct regmap_access_table;, and subsequently generate compiler warnings.

---
 scripts/checkpatch.pl | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +2 -2
index e56374662ff7..47bce7e7aeba 100755
@@ -7502,10 +7502,10 @@ sub process {
 		}
 
 # check for various structs that are normally const (ops, kgdb, device_tree)
-# and avoid what seem like struct definitions 'struct foo {'
+# and avoid what seem like struct definitions 'struct foo {' or forward declarations 'struct foo;'
 		if (defined($const_structs) &&
 		    $line !~ /\bconst\b/ &&
-		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
+		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*(\{|;))/) {
 			WARN("CONST_STRUCT",
 			     "struct $1 should normally be const\n" . $herecurr);
 		}
Signature · 1 line
2.53.0.1118.gaef5881109-goog
maintainer
#1 lore raw
Quoted 11 lines · Taylor Nelms

```
Limit checkpatch warnings for normally-const structs by excluding patterns consistent with forward declarations.

For example, the forward declaration struct regmap_access_table; in a header file currently generates a warning recommending that it is generally declared as const; however, this would apply a useless type qualifier in the empty declaration const struct regmap_access_table;, and subsequently generate compiler warnings.

Seems sensible, thanks but:

scripts/checkpatch.pl

Quoted 1 line
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub process { · line 7508

Quoted 10 lines
scripts/checkpatch.pl +2 -2
@@ -7502,10 +7502,10 @@ sub process {
 		}
 
 # check for various structs that are normally const (ops, kgdb, device_tree)
-# and avoid what seem like struct definitions 'struct foo {'
+# and avoid what seem like struct definitions 'struct foo {' or forward declarations 'struct foo;'
 		if (defined($const_structs) &&
 		    $line !~ /\bconst\b/ &&
-		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
+		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*(\{|;))/) {

Prefer using (?:\{|;) to avoid capture groups or maybe [\{;]

#2 lore raw

PATCH v2 1/1 checkpatch: exclude forward declarations of const structs

Limit checkpatch warnings for normally-const structs by excluding patterns consistent with forward declarations.

For example, the forward declaration struct regmap_access_table; in a header file currently generates a warning recommending that it is generally declared as const; however, this would apply a useless type qualifier in the empty declaration const struct regmap_access_table;, and subsequently generate compiler warnings.

---

Changelog since v1:
- Changed capturing group to character class in regex.

 scripts/checkpatch.pl | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +2 -2
index e56374662ff7..bed0bfedf5ba 100755
@@ -7502,10 +7502,10 @@ sub process {
 		}
 
 # check for various structs that are normally const (ops, kgdb, device_tree)
-# and avoid what seem like struct definitions 'struct foo {'
+# and avoid what seem like struct definitions 'struct foo {' or forward declarations 'struct foo;'
 		if (defined($const_structs) &&
 		    $line !~ /\bconst\b/ &&
-		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
+		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*[\{;])/) {
 			WARN("CONST_STRUCT",
 			     "struct $1 should normally be const\n" . $herecurr);
 		}
Signature · 1 line
2.53.0.1118.gaef5881109-goog
maintainer Acked-by on v2 1/1
#3 lore raw
Quoted 10 lines · Taylor Nelms

Limit checkpatch warnings for normally-const structs by excluding patterns consistent with forward declarations.

For example, the forward declaration struct regmap_access_table; in a header file currently generates a warning recommending that it is generally declared as const; however, this would apply a useless type qualifier in the empty declaration const struct regmap_access_table;, and subsequently generate compiler warnings.

Quoted 27 lines

---

Changelog since v1:
- Changed capturing group to character class in regex.

 scripts/checkpatch.pl | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +2 -2
index e56374662ff7..bed0bfedf5ba 100755
@@ -7502,10 +7502,10 @@ sub process {
 		}
 
 # check for various structs that are normally const (ops, kgdb, device_tree)
-# and avoid what seem like struct definitions 'struct foo {'
+# and avoid what seem like struct definitions 'struct foo {' or forward declarations 'struct foo;'
 		if (defined($const_structs) &&
 		    $line !~ /\bconst\b/ &&
-		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
+		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*[\{;])/) {
 			WARN("CONST_STRUCT",
 			     "struct $1 should normally be const\n" . $herecurr);
 		}

```