Skip to contentSkip to conversation

scripts/checkpatch.pl - don't check for const structs if list is empty

PATCH In review Valdis Klētnieks 2 participants 2 messages Sep 3, 2019 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts/checkpatch.pl - don't check for const structs if list is empty

If the list of structures we expect to be const is empty (due to file permissions, or the file being empty, etc), we get odd complaints about structures:

[/usr/src/linux-next] scripts/checkpatch.pl -f drivers/staging/netlogic/platform_net.h
No structs that should be const will be found - file '/usr/src/linux-next/scripts/const_structs.checkpatch': Permission denied
WARNING: struct should normally be const
#9: FILE: drivers/staging/netlogic/platform_net.h:9:
+struct xlr_net_data {

WARNING: struct should normally be const #20: FILE: drivers/staging/netlogic/platform_net.h:20:
+ struct xlr_fmn_info *gmac_fmn_info;

total: 0 errors, 2 warnings, 0 checks, 21 lines checked

Fix it so that it actually *obeys* what it said about not finding structures.

---

scripts/checkpatch.pl +1 -1
index f4b6127ff469..103c67665f61 100755
@@ -6497,7 +6497,7 @@ sub process {
 
 # check for various structs that are normally const (ops, kgdb, device_tree)
 # and avoid what seem like struct definitions 'struct foo {'
-		if ($line !~ /\bconst\b/ &&
+		if ($line !~ /\bconst\b/ && $const_structs ne "" &&
 		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
 			WARN("CONST_STRUCT",
 			     "struct $1 should normally be const\n" . $herecurr);
maintainer
#1 lore raw
Quoted 25 lines · Valdis Klētnieks

If the list of structures we expect to be const is empty (due to file permissions, or the file being empty, etc), we get odd complaints about structures:

[/usr/src/linux-next] scripts/checkpatch.pl -f drivers/staging/netlogic/platform_net.h
No structs that should be const will be found - file '/usr/src/linux-next/scripts/const_structs.checkpatch': Permission denied
WARNING: struct should normally be const
#9: FILE: drivers/staging/netlogic/platform_net.h:9:
+struct xlr_net_data {

WARNING: struct should normally be const #20: FILE: drivers/staging/netlogic/platform_net.h:20:

(quoted diff) +1 -0
+	struct xlr_fmn_info *gmac_fmn_info;

total: 0 errors, 2 warnings, 0 checks, 21 lines checked

Fix it so that it actually *obeys* what it said about not finding structures.

---

scripts/checkpatch.pl +0 -0
index f4b6127ff469..103c67665f61 100755

scripts/checkpatch.pl sub process { · line 6500

Quoted 5 lines
scripts/checkpatch.pl +1 -1
@@ -6497,7 +6497,7 @@ sub process {
 
 # check for various structs that are normally const (ops, kgdb, device_tree)
 # and avoid what seem like struct definitions 'struct foo {'
-		if ($line !~ /\bconst\b/ &&
+		if ($line !~ /\bconst\b/ && $const_structs ne "" &&

Seems sensible, thanks.

I think this would read better with this test order reversed.

Maybe this should verify that const does not exist before the specific struct found: (and is not also a forward declaration)

---
 scripts/checkpatch.pl | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
scripts/checkpatch.pl +5 -4
index f4b6127ff469..77d585950ab0 100755
@@ -6496,11 +6496,12 @@ sub process {
 		}
 
 # check for various structs that are normally const (ops, kgdb, device_tree)
-# and avoid what seem like struct definitions 'struct foo {'
-		if ($line !~ /\bconst\b/ &&
-		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
+# avoid struct definitions 'struct foo {' and forward declarations 'struct foo;'
+		if ($const_structs ne "" &&
+		    $line =~ /((\bstruct\s+($const_structs))\b(?!\s*[\{;]))/ &&
+		    $line !~ /\bconst\s+\Q$1\E/) {
 			WARN("CONST_STRUCT",
-			     "struct $1 should normally be const\n" . $herecurr);
+			     "struct $2 should normally be const\n" . $herecurr);
 		}
 
 # use of NR_CPUS is usually wrong