2019-11-28 02:09:44

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH] 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.

Signed-off-by: Krzysztof Kozlowski <[email protected]>
---
scripts/checkpatch.pl | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index e41f4adcc1be..875e862cf076 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -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;
--
2.7.4


2019-11-28 09:31:39

by Jani Nikula

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Look for Kconfig indentation errors

On Thu, 28 Nov 2019, Krzysztof Kozlowski <[email protected]> wrote:
> 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.
>
> Signed-off-by: Krzysztof Kozlowski <[email protected]>

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.


> ---
> scripts/checkpatch.pl | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index e41f4adcc1be..875e862cf076 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -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;

--
Jani Nikula, Intel Open Source Graphics Center

2019-11-28 09:39:25

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Look for Kconfig indentation errors

On Thu, 2019-11-28 at 11:29 +0200, Jani Nikula wrote:
> On Thu, 28 Nov 2019, Krzysztof Kozlowski <[email protected]> wrote:
> > 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.
> >
> > Signed-off-by: Krzysztof Kozlowski <[email protected]>
>
> 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.


2019-12-03 08:41:44

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Look for Kconfig indentation errors

On Thu, 28 Nov 2019 at 17:35, Joe Perches <[email protected]> wrote:
>
> On Thu, 2019-11-28 at 11:29 +0200, Jani Nikula wrote:
> > On Thu, 28 Nov 2019, Krzysztof Kozlowski <[email protected]> wrote:
> > > 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.
> > >
> > > Signed-off-by: Krzysztof Kozlowski <[email protected]>
> >
> > 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

2019-12-03 08:56:14

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Look for Kconfig indentation errors

On Tue, 3 Dec 2019 at 16:40, Krzysztof Kozlowski <[email protected]> wrote:
>
> On Thu, 28 Nov 2019 at 17:35, Joe Perches <[email protected]> wrote:
> >
> > On Thu, 2019-11-28 at 11:29 +0200, Jani Nikula wrote:
> > > On Thu, 28 Nov 2019, Krzysztof Kozlowski <[email protected]> wrote:
> > > > 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.
> > > >
> > > > Signed-off-by: Krzysztof Kozlowski <[email protected]>
> > >
> > > 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-]/)) {

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

2019-12-03 08:57:38

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Look for Kconfig indentation errors

On Tue, 2019-12-03 at 16:40 +0800, Krzysztof Kozlowski wrote:
> On Thu, 28 Nov 2019 at 17:35, Joe Perches <[email protected]> wrote:
> > On Thu, 2019-11-28 at 11:29 +0200, Jani Nikula wrote:
> > > On Thu, 28 Nov 2019, Krzysztof Kozlowski <[email protected]> wrote:
> > > > 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.
> > > >
> > > > Signed-off-by: Krzysztof Kozlowski <[email protected]>
> > >
> > > 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/

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.



2019-12-03 09:25:47

by Jani Nikula

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Look for Kconfig indentation errors

On Tue, 03 Dec 2019, Joe Perches <[email protected]> wrote:
> On Tue, 2019-12-03 at 16:40 +0800, Krzysztof Kozlowski wrote:
>> On Thu, 28 Nov 2019 at 17:35, Joe Perches <[email protected]> wrote:
>> > On Thu, 2019-11-28 at 11:29 +0200, Jani Nikula wrote:
>> > > On Thu, 28 Nov 2019, Krzysztof Kozlowski <[email protected]> wrote:
>> > > > 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.
>> > > >
>> > > > Signed-off-by: Krzysztof Kozlowski <[email protected]>
>> > >
>> > > 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/
>
> 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.

--
Jani Nikula, Intel Open Source Graphics Center

2019-12-03 09:40:41

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH] checkpatch: Look for Kconfig indentation errors

On Tue, 2019-12-03 at 11:23 +0200, Jani Nikula wrote:
> 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: $_";'