[PATCH V9 2/2] checkpatch: check format of Vec<String> in modules

Guilherme Giacomo Simoes posted 2 patches 9 months, 1 week ago
[PATCH V9 2/2] checkpatch: check format of Vec<String> in modules
Posted by Guilherme Giacomo Simoes 9 months, 1 week ago
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

Signed-off-by: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
---
 scripts/checkpatch.pl | 67 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 7b28ad331742..dda89ffedd1e 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -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++;
@@ -3567,6 +3573,67 @@ 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);
+			}
+
+			if ($line =~ /^\+\s*([\t ]+)(\S)/) {
+				$spaces = $1;
+				$herevet_space_add = $herevet;
+			}
+		}
+
+		if ($expected_spaces && $spaces) {
+			if (length($spaces) != length($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;
+		}
+
 #trailing whitespace
 		if ($line =~ /^\+.*\015/) {
 			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
-- 
2.34.1
Re: [PATCH V9 2/2] checkpatch: check format of Vec<String> in modules
Posted by Joe Perches 9 months, 1 week ago
On Sun, 2025-03-09 at 14:57 -0300, Guilherme Giacomo Simoes wrote:
> 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

What happens if the patch contains more than one module?

> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> +# 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*\[/) {

?

> +		if ($expected_spaces && $spaces) {
> +			if (length($spaces) != length($expected_spaces)) {

spaces contains both spaces and tabs.
Why not test the strings rather than the length?
Otherwise tab-space is the same length as space-tab.

> +				WARN("ARRAY_MODULE_MACRO",
> +					 "Prefer aligned parameters\n" . $herevet_space_add);

Alignment to open parenthesis please.

Why is herevet_space_add more useful than herevet?
Re: [PATCH V9 2/2] checkpatch: check format of Vec<String> in modules
Posted by Guilherme Giacomo Simoes 9 months, 1 week ago
Joe Perches <joe@perches.com> wrote:
> What happens if the patch contains more than one module?
Sincerely I do not was test this. I don't was think about when the patch
contains more than one module, is a critical problem that went unnoticed by me.
Thanks for noticing this.
I will make some tests for this and make some changes if neccessary.

> spaces contains both spaces and tabs.
> Why not test the strings rather than the length?
> Otherwise tab-space is the same length as space-tab.
You is right, we can test the string rather than length. 

> Why is herevet_space_add more useful than herevet?
The $herevet_space_add store the $herevet for the added line. Otherwise, if the
change is like:
authors: [
+	 author_1
	author_2
	author_3
]
The $expected_spaces and $spaces will be filled in the "author_2" line. And the
content of $herevet will be "	author_2" rather than "+	 author_1".

How the rust-side patch already is merged in rust-next, I only will send a
patch for this checkpatch.
I will make this changes that you suggested for me, and send a new patch.

Thanks,
Guilherme