Skip to contentSkip to conversation

checkpatch: Look for Kconfig indentation errors

PATCH In review Krzysztof Kozlowski 3 participants 8 messages Nov 28, 2019 – Dec 3, 2019 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch: Look for Kconfig indentation errors

Kconfig should be indented with one tab for first level and tab+2 spaces for second level. There are many mixups of this so add a checkpatch rule.

---
 scripts/checkpatch.pl | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/checkpatch.pl +7 -0
index e41f4adcc1be..875e862cf076 100755
@@ -3046,6 +3046,13 @@ sub process {
 			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
 		}
 
+# Kconfig has special indentation
+		if ($realfile =~ /Kconfig/ &&
+		    ($rawline =~ /^\+ +\t* *[a-zA-Z-]/) || ($rawline =~ /^\+\t( |   )[a-zA-Z-]/)) {
+			WARN("CONFIG_INDENTATION",
+			     "Kconfig uses one tab indentation, optionally followed by two spaces.\n" . $herecurr);
+		}
+
 		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
 		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
 			my $flag = $1;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 5 lines · Krzysztof Kozlowski

Kconfig should be indented with one tab for first level and tab+2 spaces for second level. There are many mixups of this so add a checkpatch rule.

I agree unifying the indentation is nice, and without something like this it'll start bitrotting before Krzysztof's done fixing them all... I think there's been quite a few fixes merged lately.

I approve of the idea, but I'm clueless about the implementation.

BR,
Jani.

Quoted 22 lines
---
 scripts/checkpatch.pl | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/checkpatch.pl +7 -0
index e41f4adcc1be..875e862cf076 100755
@@ -3046,6 +3046,13 @@ sub process {
 			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
 		}
 
+# Kconfig has special indentation
+		if ($realfile =~ /Kconfig/ &&
+		    ($rawline =~ /^\+ +\t* *[a-zA-Z-]/) || ($rawline =~ /^\+\t( |   )[a-zA-Z-]/)) {
+			WARN("CONFIG_INDENTATION",
+			     "Kconfig uses one tab indentation, optionally followed by two spaces.\n" . $herecurr);
+		}
+
 		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
 		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
 			my $flag = $1;
Signature · 1 line
Jani Nikula, Intel Open Source Graphics Center
maintainer
#2 lore raw
Quoted 5 lines · Jani Nikula
Quoted 4 lines · Krzysztof Kozlowski

Kconfig should be indented with one tab for first level and tab+2 spaces for second level. There are many mixups of this so add a checkpatch rule.

Quoted 7 lines
Quoted 1 line

I agree unifying the indentation is nice, and without something like this it'll start bitrotting before Krzysztof's done fixing them all... I think there's been quite a few fixes merged lately.

I approve of the idea, but I'm clueless about the implementation.

I think that a grammar, or a least an array of words that are supposed to start on a tab should be used here.

#3 lore raw
Quoted 9 lines · Joe Perches
Quoted 7 lines · Jani Nikula
Quoted 5 lines · Krzysztof Kozlowski

Kconfig should be indented with one tab for first level and tab+2 spaces for second level. There are many mixups of this so add a checkpatch rule.

Quoted 8 lines
Quoted 5 lines

I agree unifying the indentation is nice, and without something like this it'll start bitrotting before Krzysztof's done fixing them all... I think there's been quite a few fixes merged lately.

I approve of the idea, but I'm clueless about the implementation.

I think that a grammar, or a least an array of words that are supposed to start on a tab should be used here.

This won't work for wrong indentation of help text. This is quite popular Kconfig indentation violation so worth checking. I can then check for:
1. any white-space violations before array of Kconfig words - that
2. spaces mixed with tab before any text,
3. just spaces before any text,
4. tab + wrong number of spaces before any text.

It would look like:
+ if ($realfile =~ /Kconfig/ &&
+ (($rawline =~
/^\+\s+(?:config|menuconfig|choice|endchoice|if|endif|menu|endmenu|source|bool|tristate|prompt|help|---help---|depends|select)\b/ &&
+ $rawline !~ /^\+\t[a-z-]/) ||
+ $rawline =~ /^\+\t* +\t+ *[a-zA-Z0-9-]/ ||
+ $rawline =~ /^\+\t( | )[a-zA-Z0-9-]/)) {

Best regards,
Krzysztof

#4 lore raw
Quoted 33 lines · Krzysztof Kozlowski
Quoted 9 lines · Joe Perches
Quoted 7 lines · Jani Nikula
Quoted 5 lines · Krzysztof Kozlowski

Kconfig should be indented with one tab for first level and tab+2 spaces for second level. There are many mixups of this so add a checkpatch rule.

Quoted 8 lines
Quoted 5 lines

I agree unifying the indentation is nice, and without something like this it'll start bitrotting before Krzysztof's done fixing them all... I think there's been quite a few fixes merged lately.

I approve of the idea, but I'm clueless about the implementation.

I think that a grammar, or a least an array of words that are supposed to start on a tab should be used here.

This won't work for wrong indentation of help text. This is quite popular Kconfig indentation violation so worth checking. I can then check for:
1. any white-space violations before array of Kconfig words - that
2. spaces mixed with tab before any text,
3. just spaces before any text,
4. tab + wrong number of spaces before any text.

It would look like:

(quoted diff) +2 -0
+               if ($realfile =~ /Kconfig/ &&
+                   (($rawline =~

/^\+\s+(?:config|menuconfig|choice|endchoice|if|endif|menu|endmenu|source|bool|tristate|prompt|help|---help---|depends|select)\b/ &&

Review

Quoted 3 lines
(quoted diff) +3 -0
+                     $rawline !~ /^\+\t[a-z-]/) ||
+                    $rawline =~ /^\+\t* +\t+ *[a-zA-Z0-9-]/ ||
+                    $rawline =~ /^\+\t( |   )[a-zA-Z0-9-]/)) {

This unfortunately fails if help text starts with one of syntax keywords (e.g. "if"). Isn't this getting over-complicated? The Kconfig is rather simple:
1. no indentation,
2. one tab,
3. one tab + 2 spaces
4. one tab + 2 spaces + some more spaces (e.g. help text)

Best regards,
Krzysztof

maintainer
#5 lore raw
In reply to Krzysztof Kozlowski
Quoted 29 lines · Krzysztof Kozlowski
Quoted 8 lines · Joe Perches
Quoted 7 lines · Jani Nikula
Quoted 5 lines · Krzysztof Kozlowski

Kconfig should be indented with one tab for first level and tab+2 spaces for second level. There are many mixups of this so add a checkpatch rule.

Quoted 8 lines
Quoted 5 lines

I agree unifying the indentation is nice, and without something like this it'll start bitrotting before Krzysztof's done fixing them all... I think there's been quite a few fixes merged lately.

I approve of the idea, but I'm clueless about the implementation.

I think that a grammar, or a least an array of words that are supposed to start on a tab should be used here.

This won't work for wrong indentation of help text. This is quite popular Kconfig indentation violation so worth checking. I can then check for:
1. any white-space violations before array of Kconfig words - that
2. spaces mixed with tab before any text,
3. just spaces before any text,
4. tab + wrong number of spaces before any text.

It would look like:

(quoted diff) +2 -0
+               if ($realfile =~ /Kconfig/ &&
+                   (($rawline =~
Quoted 1 line

/^\+\s+(?:config|menuconfig|choice|endchoice|if|endif|menu|endmenu|source|bool|tristate|prompt|help|---help---|depends|select)\b/

Many of these are not correct.

config, menuconfig, choice, endchoice, source
are primarily used at the beginning of a line.

if is odd as it's a logical block or test

It really needs a lex grammar to work properly.

#6 lore raw
Quoted 32 lines · Joe Perches
Quoted 29 lines · Krzysztof Kozlowski
Quoted 8 lines · Joe Perches
Quoted 7 lines · Jani Nikula
Quoted 5 lines · Krzysztof Kozlowski

Kconfig should be indented with one tab for first level and tab+2 spaces for second level. There are many mixups of this so add a checkpatch rule.

Quoted 8 lines
Quoted 5 lines

I agree unifying the indentation is nice, and without something like this it'll start bitrotting before Krzysztof's done fixing them all... I think there's been quite a few fixes merged lately.

I approve of the idea, but I'm clueless about the implementation.

I think that a grammar, or a least an array of words that are supposed to start on a tab should be used here.

This won't work for wrong indentation of help text. This is quite popular Kconfig indentation violation so worth checking. I can then check for:
1. any white-space violations before array of Kconfig words - that
2. spaces mixed with tab before any text,
3. just spaces before any text,
4. tab + wrong number of spaces before any text.

It would look like:

(quoted diff) +2 -0
+               if ($realfile =~ /Kconfig/ &&
+                   (($rawline =~
Quoted 1 line

/^\+\s+(?:config|menuconfig|choice|endchoice|if|endif|menu|endmenu|source|bool|tristate|prompt|help|---help---|depends|select)\b/

Quoted 8 lines

Many of these are not correct.

config, menuconfig, choice, endchoice, source
are primarily used at the beginning of a line.

if is odd as it's a logical block or test

It really needs a lex grammar to work properly.

Alternatively, perhaps you could complain about indentation that is not one of 1) empty string, 2) exactly one tab, or 3) exactly one tab followed by exactly two spaces?

BR,
Jani.

Signature · 1 line
Jani Nikula, Intel Open Source Graphics Center
maintainer
#7 lore raw
Quoted 3 lines · Jani Nikula

Alternatively, perhaps you could complain about indentation that is not one of 1) empty string, 2) exactly one tab, or 3) exactly one tab followed by exactly two spaces?

Way too many false positives.

Try something like:

$ git grep -P -oh "^\s*\w+\b" -- '*/Kconfig*' | \
  perl -p -e 'my $tabs=0;my $spaces=0;while ($_ =~ /^\s/) { if (substr($_,0,1) eq " ") { $spaces++; } else { $tabs++; } $_ = substr($_, 1); } print "tabs: $tabs spaces: $spaces: word: $_";'