checkpatch: check format of Vec<String> in modules
PATCH 1/1 checkpatch: check format of Vec<String> in modules
Implement a check to ensure that the author, firmware, and alias fields of the module! macro are properly formatted.
* If the array contains more than one value, enforce vertical formatting.
* If the array contains only one value, it may be formatted on a single line
* If the array is not indentation aligned, enforce indentation alignment
Signed-off-by Guilherme Giacomo Simoes <[email protected]>
--- scripts/checkpatch.pl | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+)
scripts/checkpatch.pl +77 -0
@@ -2775,6 +2775,12 @@ sub process { $realcnt = 0; $linenr = 0; $fixlinenr = -1; + + my %array_parse_module; + my $expected_spaces; + my $spaces; + my $herevet_space_add; + foreach my $line (@lines) { $linenr++; $fixlinenr++;
@@ -2851,6 +2857,11 @@ sub process { $realfile =~ s@^([^/]*)/@@ if (!$file); $in_commit_log = 0; $found_file = 1; + + %array_parse_module = (); + $expected_spaces = undef; + $spaces = undef; + } elsif ($line =~ /^\+\+\+\s+(\S+)/) { $realfile = $1; $realfile =~ s@^([^/]*)/@@ if (!$file);
@@ -3567,6 +3578,72 @@ sub process { # ignore non-hunk lines and lines being removed next if (!$hunk_line || $line =~ /^-/); +# check if the field is about author, firmware or alias from module! macro and find malformed arrays + my $inline = 0; + my $key = ""; + my $add_line = $line =~ /^\+/; + + if ($line =~ /\b(authors|alias|firmware)\s*:\s*\[/) { + $inline = 1; + $array_parse_module{$1} = 1; + } + + my @keys = keys %array_parse_module; + if (@keys) { + $key = $keys[0]; + } + + if (!$expected_spaces && !$add_line && $key && !$inline) { + if ($line =~ /^([\t ]+)(\s)/) { + $expected_spaces = $1; + } + } + + if ($add_line && $key) { + my $herevet = "$here\n" . cat_vet($rawline) . "\n"; + + my $counter = () = $line =~ /"/g; + my $more_than_one = $counter > 2; + if ($more_than_one) { + WARN("ARRAY_MODULE_MACRO", + "Prefer each array element on a separate line\n". $herevet); + } elsif ($inline && $line !~ /\]/ && $line !~ /,/ && $line =~ /"/) { + WARN("ARRAY_MODULE_MACRO", + "Prefer to declare ] on the same line\n" . $herevet); + } elsif (!$inline && $line =~ /\]/ && $line =~ /\"/) { + WARN("ARRAY_MODULE_MACRO", + "Prefer a new line after the last value and before ]\n" . $herevet); + } elsif ($inline && $line =~ /,/ && $line !~ /\]/) { + WARN("ARRAY_MODULE_MACRO", + "Prefer a new line after [\n" . $herevet); + } + + my $line_cmp = $line; + $line_cmp =~ s/\+/ /; + $spaces = -1; + if ($line_cmp =~ /^([\t ]+)(\s)/) { + $spaces = $1; + } + + $herevet_space_add = $herevet; + } + + if ($expected_spaces && $spaces) { + if ($spaces ne $expected_spaces) { + WARN("ARRAY_MODULE_MACRO", + "Prefer aligned parameters\n" . $herevet_space_add); + } + + $spaces = undef; + } + + #END OF ANALYZE FIELD + if ($line =~ /\]/) { + delete $array_parse_module{$key}; + $expected_spaces = undef; + $spaces = undef; + } + #trailing whitespace if ($line =~ /^\+.*\015/) { my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Signature · 1 line
2.34.1