Skip to contentSkip to conversation

checkpatch: fix CONST_STRUCT when const_structs.checkpatch is missing

PATCH Superseded Quentin Monnet 2 participants 3 messages Jun 22, 2020 – Jun 23, 2020 Checkpatch

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch: fix CONST_STRUCT when const_structs.checkpatch is missing

Checkpatch reports warnings when some specific structs are not declared as const in the code. The list of structs to consider was initially defined in the checkpatch.pl script itself, but it was later moved to an external file (scripts/const_structs.checkpatch). This introduced two minor issues:

- When file scripts/const_structs.checkpatch is not present (for example, if checkpatch is run outside of the kernel directory with the "--no-tree" option), a warning is printed to stderr to tell the user that "No structs that should be const will be found". This is fair, but the warning is printed unconditionally, even if the option "--ignore CONST_STRUCT" is passed. In the latter case, we explicitly ask checkpatch to skip this check, so no warning should be printed.

- When scripts/const_structs.checkpatch is missing, or even when trying to silence the warning by adding an empty file, $const_structs is set to "", and the regex used for finding structs that should be const, "$line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/)", matches all structs found in the code, thus reporting a number of false positives.

Let's fix the first item by skipping scripts/const_structs.checkpatch processing if "CONST_STRUCT" checks are ignored, and the second one by skipping the test if $const_structs is an empty string.

Fixes bf1fa1dae68e ("checkpatch: externalize the structs that should be const")
---
 scripts/checkpatch.pl | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
scripts/checkpatch.pl +6 -3
index b06093777fd8..dcbf4ff5d445 100755
@@ -781,8 +781,10 @@ sub read_words {
 }
 
 my $const_structs = "";
-read_words(\$const_structs, $conststructsfile)
-    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
+if (show_type("CONST_STRUCT")) {
+	read_words(\$const_structs, $conststructsfile)
+	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
+}
 
 my $typeOtherTypedefs = "";
 if (length($typedefsfile)) {
@@ -6660,7 +6662,8 @@ 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 ($const_structs ne "" &&
+		    $line !~ /\bconst\b/ &&
 		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
 			WARN("CONST_STRUCT",
 			     "struct $1 should normally be const\n" . $herecurr);
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 20 lines · Quentin Monnet

Checkpatch reports warnings when some specific structs are not declared as const in the code. The list of structs to consider was initially defined in the checkpatch.pl script itself, but it was later moved to an external file (scripts/const_structs.checkpatch). This introduced two minor issues:

- When file scripts/const_structs.checkpatch is not present (for example, if checkpatch is run outside of the kernel directory with the "--no-tree" option), a warning is printed to stderr to tell the user that "No structs that should be const will be found". This is fair, but the warning is printed unconditionally, even if the option "--ignore CONST_STRUCT" is passed. In the latter case, we explicitly ask checkpatch to skip this check, so no warning should be printed.

- When scripts/const_structs.checkpatch is missing, or even when trying to silence the warning by adding an empty file, $const_structs is set to "", and the regex used for finding structs that should be const, "$line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/)", matches all structs found in the code, thus reporting a number of false positives.

Quoted 5 lines

Let's fix the first item by skipping scripts/const_structs.checkpatch processing if "CONST_STRUCT" checks are ignored, and the second one by skipping the test if $const_structs is an empty string.

Fixes bf1fa1dae68e ("checkpatch: externalize the structs that should be const")

Probably not worthy of a Fixes: line, as that's
generally used for backporting, but OK by me.

scripts/checkpatch.pl

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

[]

scripts/checkpatch.pl sub read_words { · line 783

Quoted 4 lines
scripts/checkpatch.pl +0 -0
@@ -781,8 +781,10 @@ sub read_words {
 }
 
 my $const_structs = "";

This might be a tiny bit faster/less cpu using:

my $const_structs;

Quoted 10 lines
scripts/checkpatch.pl +4 -2
-read_words(\$const_structs, $conststructsfile)
-    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
+if (show_type("CONST_STRUCT")) {
+	read_words(\$const_structs, $conststructsfile)
+	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
+}
 
 my $typeOtherTypedefs = "";
 if (length($typedefsfile)) {

scripts/checkpatch.pl sub process { · line 6665

Quoted 5 lines
scripts/checkpatch.pl +1 -1
@@ -6660,7 +6662,8 @@ 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 ($const_structs ne "" &&

instead testing

		if (defined($const_structs) &&
Quoted 4 lines
scripts/checkpatch.pl +1 -0
+		    $line !~ /\bconst\b/ &&
 		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
 			WARN("CONST_STRUCT",
 			     "struct $1 should normally be const\n" . $herecurr);
#2 lore raw

2020-06-22 14:24 UTC-0700 ~ Joe Perches <[email protected]>

Quoted 21 lines · Quentin Monnet
Quoted 20 lines

Checkpatch reports warnings when some specific structs are not declared as const in the code. The list of structs to consider was initially defined in the checkpatch.pl script itself, but it was later moved to an external file (scripts/const_structs.checkpatch). This introduced two minor issues:

- When file scripts/const_structs.checkpatch is not present (for example, if checkpatch is run outside of the kernel directory with the "--no-tree" option), a warning is printed to stderr to tell the user that "No structs that should be const will be found". This is fair, but the warning is printed unconditionally, even if the option "--ignore CONST_STRUCT" is passed. In the latter case, we explicitly ask checkpatch to skip this check, so no warning should be printed.

- When scripts/const_structs.checkpatch is missing, or even when trying to silence the warning by adding an empty file, $const_structs is set to "", and the regex used for finding structs that should be const, "$line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/)", matches all structs found in the code, thus reporting a number of false positives.

Quoted 8 lines
Quoted 5 lines

Let's fix the first item by skipping scripts/const_structs.checkpatch processing if "CONST_STRUCT" checks are ignored, and the second one by skipping the test if $const_structs is an empty string.

Fixes bf1fa1dae68e ("checkpatch: externalize the structs that should be const")

Probably not worthy of a Fixes: line, as that's
generally used for backporting, but OK by me.

Yeah I hesitated, I'll drop it for v2.

Quoted 23 lines

scripts/checkpatch.pl

Quoted 2 lines
scripts/checkpatch.pl +0 -0

[]

scripts/checkpatch.pl sub read_words { · line 783

Quoted 4 lines
scripts/checkpatch.pl +0 -0
@@ -781,8 +781,10 @@ sub read_words {
 }
 
 my $const_structs = "";

This might be a tiny bit faster/less cpu using:

my $const_structs;

Quoted 11 lines
scripts/checkpatch.pl +4 -2
-read_words(\$const_structs, $conststructsfile)
-    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
+if (show_type("CONST_STRUCT")) {
+	read_words(\$const_structs, $conststructsfile)
+	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
+}
 
 my $typeOtherTypedefs = "";
 if (length($typedefsfile)) {
@@ -6660,7 +6662,8 @@ sub process {
 
Quoted 8 lines

scripts/checkpatch.pl sub process { · line 6665

Quoted 4 lines
scripts/checkpatch.pl +1 -1
 # 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 ($const_structs ne "" &&

instead testing

		if (defined($const_structs) &&

Sure, I will respin.

Thank you for the review!
Quentin