Commit ced69da1 authored by Quentin Monnet's avatar Quentin Monnet Committed by Linus Torvalds

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 bf1fa1da
("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: default avatarQuentin Monnet <quentin@isovalent.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Acked-by: default avatarJoe Perches <joe@perches.com>
Link: http://lkml.kernel.org/r/20200623221822.3727-1-quentin@isovalent.comSigned-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 65b64b3b
...@@ -59,7 +59,7 @@ my $spelling_file = "$D/spelling.txt"; ...@@ -59,7 +59,7 @@ my $spelling_file = "$D/spelling.txt";
my $codespell = 0; my $codespell = 0;
my $codespellfile = "/usr/share/codespell/dictionary.txt"; my $codespellfile = "/usr/share/codespell/dictionary.txt";
my $conststructsfile = "$D/const_structs.checkpatch"; my $conststructsfile = "$D/const_structs.checkpatch";
my $typedefsfile = ""; my $typedefsfile;
my $color = "auto"; my $color = "auto";
my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE my $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
# git output parsing needs US English output, so first set backtick child process LANGUAGE # git output parsing needs US English output, so first set backtick child process LANGUAGE
...@@ -756,7 +756,7 @@ sub read_words { ...@@ -756,7 +756,7 @@ sub read_words {
next; next;
} }
$$wordsRef .= '|' if ($$wordsRef ne ""); $$wordsRef .= '|' if (defined $$wordsRef);
$$wordsRef .= $line; $$wordsRef .= $line;
} }
close($file); close($file);
...@@ -766,16 +766,18 @@ sub read_words { ...@@ -766,16 +766,18 @@ sub read_words {
return 0; return 0;
} }
my $const_structs = ""; my $const_structs;
read_words(\$const_structs, $conststructsfile) if (show_type("CONST_STRUCT")) {
read_words(\$const_structs, $conststructsfile)
or warn "No structs that should be const will be found - file '$conststructsfile': $!\n"; or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
}
my $typeOtherTypedefs = ""; if (defined($typedefsfile)) {
if (length($typedefsfile)) { my $typeOtherTypedefs;
read_words(\$typeOtherTypedefs, $typedefsfile) read_words(\$typeOtherTypedefs, $typedefsfile)
or warn "No additional types will be considered - file '$typedefsfile': $!\n"; or warn "No additional types will be considered - file '$typedefsfile': $!\n";
$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
} }
$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
sub build_types { sub build_types {
my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
...@@ -6643,7 +6645,8 @@ sub process { ...@@ -6643,7 +6645,8 @@ sub process {
# check for various structs that are normally const (ops, kgdb, device_tree) # 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 {'
if ($line !~ /\bconst\b/ && if (defined($const_structs) &&
$line !~ /\bconst\b/ &&
$line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) { $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
WARN("CONST_STRUCT", WARN("CONST_STRUCT",
"struct $1 should normally be const\n" . $herecurr); "struct $1 should normally be const\n" . $herecurr);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment