Skip to content

Commit ced69da

Browse files
qmonnettorvalds
authored andcommitted
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), in commit bf1fa1d ("checkpatch: externalize the structs that should be const"). 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 =~ /struct\s+($const_structs)(?!\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 not defined. Since we modify the read_words() function a little bit, update the checks for $typedefsfile/$typeOtherTypedefs as well. Signed-off-by: Quentin Monnet <quentin@isovalent.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Acked-by: Joe Perches <joe@perches.com> Link: http://lkml.kernel.org/r/20200623221822.3727-1-quentin@isovalent.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 65b64b3 commit ced69da

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

‎scripts/checkpatch.pl‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
my $codespell = 0;
6060
my $codespellfile = "/usr/share/codespell/dictionary.txt";
6161
my $conststructsfile = "$D/const_structs.checkpatch";
62-
my $typedefsfile = "";
62+
my $typedefsfile;
6363
my $color = "auto";
6464
my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
6565
# git output parsing needs US English output, so first set backtick child process LANGUAGE
@@ -756,7 +756,7 @@ sub read_words {
756756
next;
757757
}
758758

759-
$$wordsRef .= '|' if ($$wordsRef ne "");
759+
$$wordsRef .= '|' if (defined $$wordsRef);
760760
$$wordsRef .= $line;
761761
}
762762
close($file);
@@ -766,16 +766,18 @@ sub read_words {
766766
return 0;
767767
}
768768

769-
my $const_structs = "";
770-
read_words(\$const_structs, $conststructsfile)
771-
or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
769+
my $const_structs;
770+
if (show_type("CONST_STRUCT")) {
771+
read_words(\$const_structs, $conststructsfile)
772+
or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
773+
}
772774

773-
my $typeOtherTypedefs = "";
774-
if (length($typedefsfile)) {
775+
if (defined($typedefsfile)) {
776+
my $typeOtherTypedefs;
775777
read_words(\$typeOtherTypedefs, $typedefsfile)
776778
or warn "No additional types will be considered - file '$typedefsfile': $!\n";
779+
$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
777780
}
778-
$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
779781

780782
sub build_types {
781783
my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
@@ -6643,7 +6645,8 @@ sub process {
66436645

66446646
# check for various structs that are normally const (ops, kgdb, device_tree)
66456647
# and avoid what seem like struct definitions 'struct foo {'
6646-
if ($line !~ /\bconst\b/ &&
6648+
if (defined($const_structs) &&
6649+
$line !~ /\bconst\b/ &&
66476650
$line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
66486651
WARN("CONST_STRUCT",
66496652
"struct $1 should normally be const\n" . $herecurr);

0 commit comments

Comments
 (0)