Skip to contentSkip to conversation

checkpatch: update kconfig parsing

PATCH Superseded Nicolai Fischer 2 participants 16 messages Dec 14, 2020 – Jan 5, 2021 Checkpatch

lore
Share & tools
lore
#0 lore raw

cover checkpatch: update kconfig parsing

This series updates the parsing of Kconfig files within checkpatch.pl to the current state, as discussed previously.

Nicolai Fischer (2):
checkpatch: kconfig: replace '---help---' with 'help'
checkpatch: kconfig: add missing types to regex

 scripts/checkpatch.pl | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

v2 cover checkpatch: update kconfig parsing

This series updates the parsing of Kconfig files within checkpatch.pl to the current state, as discussed previously.

The second iteration contains two more patches.
Patch 3 adds a new warning regarding the indentation as discussed. Patch 4 aims to clarify the existing warning.

Nicolai Fischer (4):
checkpatch: kconfig: replace '---help---' with 'help'
checkpatch: kconfig: add missing types to regex
checkpatch: kconfig: enforce help text indentation
checkpatch: kconfig: clarify warning for paragraph length

 scripts/checkpatch.pl | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
Signature · 1 line
2.29.2
#2 lore raw

PATCH v2 1/4 checkpatch: kconfig: replace '---help---' with 'help'

All '---help---' lines have been replaced by just 'help'. Therefore it is no longer necessary to include '---' in the regex.

---
 scripts/checkpatch.pl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -1
index 00085308ed9d..f6fd811c54a0 100755
@@ -3323,7 +3323,7 @@ sub process {
 
 				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
 					$is_start = 1;
-				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
+				} elsif ($lines[$ln - 1] =~ /^\+\s*help$/) {
 					$length = -1;
 				}
 
Signature · 1 line
2.29.2
#3 lore raw

PATCH v2 2/4 checkpatch: kconfig: add missing types to regex

In reply to Nicolai Fischer

Kconfig parsing does not recognise all type attributes. This adds the missing 'int', 'string' and 'hex' types.

---
 scripts/checkpatch.pl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -1
index f6fd811c54a0..c86a971a3205 100755
@@ -3321,7 +3321,7 @@ sub process {
 				next if ($f =~ /^-/);
 				last if (!$file && $f =~ /^\@\@/);
 
-				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
+				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|int|hex|string|prompt)\s*(?:["'].*)?$/) {
 					$is_start = 1;
 				} elsif ($lines[$ln - 1] =~ /^\+\s*help$/) {
 					$length = -1;
Signature · 1 line
2.29.2
#4 lore raw

PATCH v2 3/4 checkpatch: kconfig: enforce help text indentation

In reply to Nicolai Fischer

Adds a new warning in case the indentation level of the first line of a Kconfig help message is not two spaces higher than the keyword itself.
Blank lines between the message and the help keyword are ignored.

---
 scripts/checkpatch.pl | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +17 -2
index c86a971a3205..aa2205ee9ab8 100755
@@ -3313,6 +3313,8 @@ sub process {
 			my $f;
 			my $is_start = 0;
 			my $is_end = 0;
+			my $help_indent;
+			my $help_stat_real;
 			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
 				$f = $lines[$ln - 1];
 				$cnt-- if ($lines[$ln - 1] !~ /^-/);
@@ -3323,8 +3325,10 @@ sub process {
 
 				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|int|hex|string|prompt)\s*(?:["'].*)?$/) {
 					$is_start = 1;
-				} elsif ($lines[$ln - 1] =~ /^\+\s*help$/) {
-					$length = -1;
+				} elsif ($lines[$ln - 1] =~ /^\+(\s*)help$/) {
+					$help_indent = $1;
+					$length = 0;
+					next;
 				}
 
 				$f =~ s/^.//;
@@ -3332,6 +3336,13 @@ sub process {
 				$f =~ s/^\s+//;
 				next if ($f =~ /^$/);
 
+				if (defined $help_indent) {
+					if ($lines[$ln - 1] !~ /^\+$help_indent\ {2}\S*/) {
+						$help_stat_real = get_stat_real($ln - 1, $ln);
+					}
+					undef $help_indent;
+				}
+
 				# This only checks context lines in the patch
 				# and so hopefully shouldn't trigger false
 				# positives, even though some of these are
@@ -3347,6 +3358,10 @@ sub process {
 				WARN("CONFIG_DESCRIPTION",
 				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
 			}
+			if ($is_start && $is_end && defined $help_stat_real) {
+				WARN("CONFIG_DESCRIPTION",
+				     "please indent the help text two spaces more than the keyword\n" . "$here\n$help_stat_real\n");
+			}
 			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
 		}
 
Signature · 1 line
2.29.2
maintainer
#5 lore raw

Re: [PATCH v2 3/4] checkpatch: kconfig: enforce help text indentation

Quoted 5 lines · Nicolai Fischer

Adds a new warning in case the indentation level of the first line of a Kconfig help message is not two spaces higher than the keyword itself.
Blank lines between the message and the help keyword are ignored.

[]

scripts/checkpatch.pl

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

[]

scripts/checkpatch.pl sub process { · line 3344

Quoted 11 lines
scripts/checkpatch.pl +0 -0
@@ -3332,6 +3336,13 @@ sub process {
  				$f =~ s/^\s+//;
  				next if ($f =~ /^$/);

scripts/checkpatch.pl +6 -0
+				if (defined $help_indent) {
+					if ($lines[$ln - 1] !~ /^\+$help_indent\ {2}\S*/) {
+						$help_stat_real = get_stat_real($ln - 1, $ln);
+					}
+					undef $help_indent;
+				}

This doesn't work if the indent is more than 2 spaces.

$ cat Kconfigtest
menuconfig FOO
	bool "Enable foo" if EXPERT
	default y
	help
	   Line 1.
	   Line 2.
	   Line 3.
	   Line 4.
$ ./scripts/checkpatch.pl -f Kconfigtest
total: 0 errors, 0 warnings, 10 lines checked

Kconfigtest has no obvious style problems and is ready for submission.

Also, it may be useful to test that the indent after a block uses a single tab more than the block start.

Look at the first block of block/Kconfig:

The indentation of bool and help uses 7 spaces but the indentation of the help text uses a tab then 1 space.

It'd be useful to emit a warning for that.

menuconfig BLOCK
bool "Enable the block layer" if EXPERT default y
select SBITMAP
select SRCU
help
Provide block layer support for the kernel.

Disable this option to remove the block layer support from the kernel. This may be useful for embedded devices.

If this option is disabled:

- block device files will become unusable
- some filesystems (such as ext3) will become unavailable.

Also, SCSI character devices and USB storage will be disabled since they make use of various block layer definitions and facilities.

Say Y here unless you know you really don't want to mount disks and suchlike.

#6 lore raw

PATCH v2 4/4 checkpatch: kconfig: clarify warning for paragraph length

In reply to Nicolai Fischer

Currently checkpatch displays a warning if it detects a help text of a Kconfig option which is too short. However the warning does not contain any further information.
This adds the expected and currently detected number of lines to the warning, which makes it more obvious why checkpatch is warning.

Furthermore this makes it easier to quickly identify false positives, e.g. when a help message contains a Kconfig keyword and falsely triggers checkpatch to stop counting lines, it will be more apparent, because the user can see how many lines they wrote and how many of those were counted correctly.

---
 scripts/checkpatch.pl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -1
index aa2205ee9ab8..bc363933e0aa 100755
@@ -3356,7 +3356,7 @@ sub process {
 			}
 			if ($is_start && $is_end && $length < $min_conf_desc_length) {
 				WARN("CONFIG_DESCRIPTION",
-				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
+				     "please write a paragraph ($length/$min_conf_desc_length lines) that describes the config symbol fully\n" . $herecurr);
 			}
 			if ($is_start && $is_end && defined $help_stat_real) {
 				WARN("CONFIG_DESCRIPTION",
Signature · 1 line
2.29.2
#7 lore raw

v3 cover update kconfig parsing

In reply to Nicolai Fischer

This series updates the parsing of Kconfig files within checkpatch.pl to the current state, as discussed previously.

Third iteration fixes patch 3 and adds one new patch.
Patch 5 adds a new waring regarding the indentation of a Kconfig block

Nicolai Fischer (5):
checkpatch: kconfig: replace '---help---' with 'help'
checkpatch: kconfig: add missing types to regex
checkpatch: kconfig: enforce help text indentation
checkpatch: kconfig: clarify warning for paragraph length
checkpatch: kconfig: enforce block indentation

 scripts/checkpatch.pl | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)
Signature · 1 line
2.29.2
#8 lore raw

PATCH v3 1/5 checkpatch: kconfig: replace '---help---' with 'help'

All '---help---' lines have been replaced by just 'help'. Therefore it is no longer necessary to include '---' in the regex.

---
 scripts/checkpatch.pl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -1
index 00085308ed9d..f6fd811c54a0 100755
@@ -3323,7 +3323,7 @@ sub process {
 
 				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
 					$is_start = 1;
-				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
+				} elsif ($lines[$ln - 1] =~ /^\+\s*help$/) {
 					$length = -1;
 				}
 
Signature · 1 line
2.29.2
#9 lore raw

PATCH v3 2/5 checkpatch: kconfig: add missing types to regex

In reply to Nicolai Fischer

Kconfig parsing does not recognise all type attributes. This adds the missing 'int', 'string' and 'hex' types.

---
 scripts/checkpatch.pl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -1
index f6fd811c54a0..c86a971a3205 100755
@@ -3321,7 +3321,7 @@ sub process {
 				next if ($f =~ /^-/);
 				last if (!$file && $f =~ /^\@\@/);
 
-				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
+				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|int|hex|string|prompt)\s*(?:["'].*)?$/) {
 					$is_start = 1;
 				} elsif ($lines[$ln - 1] =~ /^\+\s*help$/) {
 					$length = -1;
Signature · 1 line
2.29.2
#10 lore raw

PATCH v3 3/5 checkpatch: kconfig: enforce help text indentation

In reply to Nicolai Fischer

Adds a new warning in case the indentation level of the first line of a Kconfig help message is not at least two spaces higher than the keyword itself.
Blank lines between the message and the help keyword are ignored.

---

Now matches indentation of two or more spaces, instead of exactly two.

 scripts/checkpatch.pl | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +17 -2
index c86a971a3205..209880810aaa 100755
@@ -3313,6 +3313,8 @@ sub process {
 			my $f;
 			my $is_start = 0;
 			my $is_end = 0;
+			my $help_indent;
+			my $help_stat_real;
 			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
 				$f = $lines[$ln - 1];
 				$cnt-- if ($lines[$ln - 1] !~ /^-/);
@@ -3323,8 +3325,10 @@ sub process {
 
 				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|int|hex|string|prompt)\s*(?:["'].*)?$/) {
 					$is_start = 1;
-				} elsif ($lines[$ln - 1] =~ /^\+\s*help$/) {
-					$length = -1;
+				} elsif ($lines[$ln - 1] =~ /^\+(\s*)help$/) {
+					$help_indent = $1;
+					$length = 0;
+					next;
 				}
 
 				$f =~ s/^.//;
@@ -3332,6 +3336,13 @@ sub process {
 				$f =~ s/^\s+//;
 				next if ($f =~ /^$/);
 
+				if (defined $help_indent) {
+					if ($lines[$ln - 1] !~ /^\+$help_indent\ {2,}\S*/) {
+						$help_stat_real = get_stat_real($ln - 1, $ln);
+					}
+					undef $help_indent;
+				}
+
 				# This only checks context lines in the patch
 				# and so hopefully shouldn't trigger false
 				# positives, even though some of these are
@@ -3347,6 +3358,10 @@ sub process {
 				WARN("CONFIG_DESCRIPTION",
 				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
 			}
+			if ($is_start && $is_end && defined $help_stat_real) {
+				WARN("CONFIG_DESCRIPTION",
+				     "please indent the help text two spaces more than the keyword\n" . "$here\n$help_stat_real\n");
+			}
 			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
 		}
 
Signature · 1 line
2.29.2
maintainer
#11 lore raw

Re: [PATCH v3 3/5] checkpatch: kconfig: enforce help text indentation

Quoted 6 lines · Nicolai Fischer

Adds a new warning in case the indentation level of the first line of a Kconfig help message is not at least two spaces higher than the keyword itself.
Blank lines between the message and the help keyword are ignored.

Quoted 6 lines

---

Now matches indentation of two or more spaces, instead of exactly two.

No, this should match exactly 2 and warn on any other use.

maintainer
#12 lore raw

Re: [PATCH v3 3/5] checkpatch: kconfig: enforce help text indentation

Quoted 7 lines · Joe Perches
Quoted 6 lines · Nicolai Fischer

Adds a new warning in case the indentation level of the first line of a Kconfig help message is not at least two spaces higher than the keyword itself.
Blank lines between the message and the help keyword are ignored.

Quoted 8 lines
Quoted 6 lines

---

Now matches indentation of two or more spaces, instead of exactly two.

No, this should match exactly 2 and warn on any other use.

To clarify, only the first line after the help keyword needs to have a 2 space indent more than the help keyword and the help block may start with Kconfig keywords.

Subsequent help block lines may have more than 2 chars.

The help block line count should end when the indent is less than the help keyword indent and is a non-blank line.

This should be valid:

	help
	  line 1
	    -- reason 1
	    -- reason 2
	       continuation
	    -- reason 3

But this should warn only on line 1:

	help
	   line 1 has a 3 space indent
	   -- reason 1
	   -- reason 2
	       continuation
	   -- reason 3
#13 lore raw

Re: [PATCH v3 3/5] checkpatch: kconfig: enforce help text indentation

Quoted 13 lines · Joe Perches
Quoted 12 lines · Joe Perches
Quoted 11 lines · Nicolai Fischer

Adds a new warning in case the indentation level of the first line of a Kconfig help message is not at least two spaces higher than the keyword itself.
Blank lines between the message and the help keyword are ignored.

---

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Now matches indentation of two or more spaces, instead of exactly two.

No, this should match exactly 2 and warn on any other use.

To clarify, only the first line after the help keyword needs to have a 2 space indent more than the help keyword and the help block may start with Kconfig keywords.

Quoted 1 line

Subsequent help block lines may have more than 2 chars.

Okay, thank you for the clarification.

Quoted 3 lines

The help block line count should end when the indent is less than the help keyword indent and is a non-blank line.

We could do something like this

  if (defined $help_indent) {
    $lines[$ln - 1] =~ /^\+(\s*)\S+/;
	if (length($1) < length($help_indent)) {
	  is_end = 1; last;

}

as an extra patch after patch 3.

Please clarify whether we should match for a smaller indent than the help keyword or the first non-blank line after the keyword.

Quoted 20 lines

This should be valid:

	help
	  line 1
	    -- reason 1
	    -- reason 2
	       continuation
	    -- reason 3

But this should warn only on line 1:

	help
	   line 1 has a 3 space indent
	   -- reason 1
	   -- reason 2
	       continuation
	   -- reason 3
#14 lore raw

PATCH v3 4/5 checkpatch: kconfig: clarify warning for paragraph length

In reply to Nicolai Fischer

Currently checkpatch displays a warning if it detects a help text of a Kconfig option which is too short. However the warning does not contain any further information.
This adds the expected and currently detected number of lines to the warning, which makes it more obvious why checkpatch is warning.

Furthermore this makes it easier to quickly identify false positives, e.g. when a help message contains a Kconfig keyword and falsely triggers checkpatch to stop counting lines, it will be more apparent, because the user can see how many lines they wrote and how many of those were counted correctly.

---
 scripts/checkpatch.pl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -1
index 209880810aaa..805b5870803f 100755
@@ -3356,7 +3356,7 @@ sub process {
 			}
 			if ($is_start && $is_end && $length < $min_conf_desc_length) {
 				WARN("CONFIG_DESCRIPTION",
-				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
+				     "please write a paragraph ($length/$min_conf_desc_length lines) that describes the config symbol fully\n" . $herecurr);
 			}
 			if ($is_start && $is_end && defined $help_stat_real) {
 				WARN("CONFIG_DESCRIPTION",
Signature · 1 line
2.29.2
#15 lore raw

PATCH v3 5/5 checkpatch: kconfig: enforce block indentation

In reply to Nicolai Fischer

Adds a new warning to checkpatch in case a new Kconfig block is not indented one sigle tab more than the keyword which starts it.

---
 scripts/checkpatch.pl | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
scripts/checkpatch.pl +11 -2
index 805b5870803f..8a82ea5c2eb3 100755
@@ -3306,7 +3306,8 @@ sub process {
 		    # 'choice' is usually the last thing on the line (though
 		    # Kconfig supports named choices), so use a word boundary
 		    # (\b) rather than a whitespace character (\s)
-		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
+		    $line =~ /^\+(\s*)(?:config|menuconfig|choice)\b/) {
+			my $base_indent = $1;
 			my $length = 0;
 			my $cnt = $realcnt;
 			my $ln = $linenr + 1;
@@ -3315,6 +3316,7 @@ sub process {
 			my $is_end = 0;
 			my $help_indent;
 			my $help_stat_real;
+			my $block_stat_real;
 			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
 				$f = $lines[$ln - 1];
 				$cnt-- if ($lines[$ln - 1] !~ /^-/);
@@ -3323,7 +3325,10 @@ sub process {
 				next if ($f =~ /^-/);
 				last if (!$file && $f =~ /^\@\@/);
 
-				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|int|hex|string|prompt)\s*(?:["'].*)?$/) {
+				if ($lines[$ln - 1] =~ /^\+(\s*)(?:bool|tristate|int|hex|string|prompt)\s*(?:["'].*)?$/) {
+					if ($1 !~ /^$base_indent\t$/) {
+						$block_stat_real = get_stat_real($linenr, $ln);
+					}
 					$is_start = 1;
 				} elsif ($lines[$ln - 1] =~ /^\+(\s*)help$/) {
 					$help_indent = $1;
@@ -3358,6 +3363,10 @@ sub process {
 				WARN("CONFIG_DESCRIPTION",
 				     "please write a paragraph ($length/$min_conf_desc_length lines) that describes the config symbol fully\n" . $herecurr);
 			}
+			if ($is_start && $is_end && defined $block_stat_real) {
+				WARN("CONFIG_DESCRIPTION",
+				     "please indent the block a single tab more than the start\n" . "$here\n$block_stat_real\n");
+			}
 			if ($is_start && $is_end && defined $help_stat_real) {
 				WARN("CONFIG_DESCRIPTION",
 				     "please indent the help text two spaces more than the keyword\n" . "$here\n$help_stat_real\n");
Signature · 1 line
2.29.2