linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RFC] checkpatch: fix multi-statement macro checks
@ 2020-10-01 10:33 Dwaipayan Ray
  2020-10-01 11:12 ` Lukas Bulwahn
  2020-10-01 13:17 ` Joe Perches
  0 siblings, 2 replies; 9+ messages in thread
From: Dwaipayan Ray @ 2020-10-01 10:33 UTC (permalink / raw)
  To: joe; +Cc: linux-kernel, linux-kernel-mentees, dwaipayanray1, lukas.bulwahn

Checkpatch.pl doesn't have a check for excluding while (...) {...}
blocks from MULTISTATEMENT_MACRO_USE_DO_WHILE error.

For example, running checkpatch.pl on the file mm/access.c in the
kernel generates the following error:

ERROR: Macros with complex values should be enclosed in parentheses
+#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label)	\
+	while (len >= sizeof(type)) {					\
+		__get_kernel_nofault(dst, src, type, err_label);	\
+		dst += sizeof(type);					\
+		src += sizeof(type);					\
+		len -= sizeof(type);					\
+	}

The error is misleading for this case. Enclosing it in parantheses
doesn't make any sense.

Checkpatch already has an exception list for such common macro types.
Added a new exception for while (...) {...} style blocks to the same.
This effectively fixed the wrong error message.

Signed-off-by: Dwaipayan Ray <dwaipayanray1@gmail.com>
---
 scripts/checkpatch.pl | 1 +
 1 file changed, 1 insertion(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 72c4072307ea..c2c211374662 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -5342,6 +5342,7 @@ sub process {
 			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
 			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
 			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
+			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}
 			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
 			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
 			    $dstat !~ /^do\s*{/ &&					# do {...
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH RFC] checkpatch: fix multi-statement macro checks
  2020-10-01 10:33 [PATCH RFC] checkpatch: fix multi-statement macro checks Dwaipayan Ray
@ 2020-10-01 11:12 ` Lukas Bulwahn
  2020-10-01 13:17 ` Joe Perches
  1 sibling, 0 replies; 9+ messages in thread
From: Lukas Bulwahn @ 2020-10-01 11:12 UTC (permalink / raw)
  To: Dwaipayan Ray; +Cc: joe, linux-kernel, linux-kernel-mentees, lukas.bulwahn



On Thu, 1 Oct 2020, Dwaipayan Ray wrote:

> Checkpatch.pl doesn't have a check for excluding while (...) {...}
> blocks from MULTISTATEMENT_MACRO_USE_DO_WHILE error.
> 
> For example, running checkpatch.pl on the file mm/access.c in the
> kernel generates the following error:
> 
> ERROR: Macros with complex values should be enclosed in parentheses
> +#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label)	\
> +	while (len >= sizeof(type)) {					\
> +		__get_kernel_nofault(dst, src, type, err_label);	\
> +		dst += sizeof(type);					\
> +		src += sizeof(type);					\
> +		len -= sizeof(type);					\
> +	}
> 
> The error is misleading for this case. Enclosing it in parantheses

s/parantheses/parentheses/

In my previous review, I already pointed that spelling mistake; was there 
a mess-up with sending out the new patch?

I will start running a quick evaluation...

> doesn't make any sense.
> 
> Checkpatch already has an exception list for such common macro types.
> Added a new exception for while (...) {...} style blocks to the same.
> This effectively fixed the wrong error message.
> 
> Signed-off-by: Dwaipayan Ray <dwaipayanray1@gmail.com>
> ---
>  scripts/checkpatch.pl | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 72c4072307ea..c2c211374662 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -5342,6 +5342,7 @@ sub process {
>  			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
>  			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
>  			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
> +			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}
>  			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
>  			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
>  			    $dstat !~ /^do\s*{/ &&					# do {...
> -- 
> 2.27.0
> 
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH RFC] checkpatch: fix multi-statement macro checks
  2020-10-01 10:33 [PATCH RFC] checkpatch: fix multi-statement macro checks Dwaipayan Ray
  2020-10-01 11:12 ` Lukas Bulwahn
@ 2020-10-01 13:17 ` Joe Perches
  2020-10-01 13:27   ` Dwaipayan Ray
  1 sibling, 1 reply; 9+ messages in thread
From: Joe Perches @ 2020-10-01 13:17 UTC (permalink / raw)
  To: Dwaipayan Ray; +Cc: linux-kernel, linux-kernel-mentees, lukas.bulwahn

On Thu, 2020-10-01 at 16:03 +0530, Dwaipayan Ray wrote:
> Checkpatch.pl doesn't have a check for excluding while (...) {...}
> blocks from MULTISTATEMENT_MACRO_USE_DO_WHILE error.
> 
> For example, running checkpatch.pl on the file mm/access.c in the
> kernel generates the following error:
> 
> ERROR: Macros with complex values should be enclosed in parentheses
> +#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label)	\
> +	while (len >= sizeof(type)) {					\
> +		__get_kernel_nofault(dst, src, type, err_label);	\
> +		dst += sizeof(type);					\
> +		src += sizeof(type);					\
> +		len -= sizeof(type);					\
> +	}
> 
> The error is misleading for this case. Enclosing it in parantheses
> doesn't make any sense.

OK

> Checkpatch already has an exception list for such common macro types.
> Added a new exception for while (...) {...} style blocks to the same.
> This effectively fixed the wrong error message.
[]
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> @@ -5342,6 +5342,7 @@ sub process {
>  			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
>  			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
>  			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
> +			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}

Did you try to output $dstat for some matching cases?
What was the $dstat value for the cases you tried?



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH RFC] checkpatch: fix multi-statement macro checks
  2020-10-01 13:17 ` Joe Perches
@ 2020-10-01 13:27   ` Dwaipayan Ray
  2020-10-01 13:42     ` Joe Perches
  0 siblings, 1 reply; 9+ messages in thread
From: Dwaipayan Ray @ 2020-10-01 13:27 UTC (permalink / raw)
  To: Joe Perches; +Cc: linux-kernel, linux-kernel-mentees, Lukas Bulwahn

On Thu, Oct 1, 2020 at 6:47 PM Joe Perches <joe@perches.com> wrote:
>
> On Thu, 2020-10-01 at 16:03 +0530, Dwaipayan Ray wrote:
> > Checkpatch.pl doesn't have a check for excluding while (...) {...}
> > blocks from MULTISTATEMENT_MACRO_USE_DO_WHILE error.
> >
> > For example, running checkpatch.pl on the file mm/access.c in the
> > kernel generates the following error:
> >
> > ERROR: Macros with complex values should be enclosed in parentheses
> > +#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label)        \
> > +     while (len >= sizeof(type)) {                                   \
> > +             __get_kernel_nofault(dst, src, type, err_label);        \
> > +             dst += sizeof(type);                                    \
> > +             src += sizeof(type);                                    \
> > +             len -= sizeof(type);                                    \
> > +     }
> >
> > The error is misleading for this case. Enclosing it in parantheses
> > doesn't make any sense.
>
> OK
>
> > Checkpatch already has an exception list for such common macro types.
> > Added a new exception for while (...) {...} style blocks to the same.
> > This effectively fixed the wrong error message.
> []
> > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
> > @@ -5342,6 +5342,7 @@ sub process {
> >                           $dstat !~ /^\.$Ident\s*=/ &&                                # .foo =
> >                           $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&          # stringification #foo
> >                           $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&       # do {...} while (...); // do {...} while (...)
> > +                         $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&           # while (...) {...}
>
> Did you try to output $dstat for some matching cases?
> What was the $dstat value for the cases you tried?
>
>
Hi,
I did check $dstat values.

For example on file mm/maccess.c, there were two such macros:

Case 1:

$ctx:
+#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \
+ while (len >= sizeof(type)) { \
+ __get_kernel_nofault(dst, src, type, err_label); \
+ dst += sizeof(type); \
+ src += sizeof(type); \
+ len -= sizeof(type); \
+ }

$dstat:
while 1 1

Case 2:

$ctx:
+#define copy_to_kernel_nofault_loop(dst, src, len, type, err_label) \
+ while (len >= sizeof(type)) { \
+ __put_kernel_nofault(dst, src, type, err_label); \
+ dst += sizeof(type); \
+ src += sizeof(type); \
+ len -= sizeof(type); \
+ }

$dstat:
while 1 1


Thanks,
Dwaipayan.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH RFC] checkpatch: fix multi-statement macro checks
  2020-10-01 13:27   ` Dwaipayan Ray
@ 2020-10-01 13:42     ` Joe Perches
  2020-10-01 14:14       ` Dwaipayan Ray
  0 siblings, 1 reply; 9+ messages in thread
From: Joe Perches @ 2020-10-01 13:42 UTC (permalink / raw)
  To: Dwaipayan Ray; +Cc: linux-kernel, linux-kernel-mentees, Lukas Bulwahn

On Thu, 2020-10-01 at 18:57 +0530, Dwaipayan Ray wrote:
> On Thu, Oct 1, 2020 at 6:47 PM Joe Perches <joe@perches.com> wrote:
> > On Thu, 2020-10-01 at 16:03 +0530, Dwaipayan Ray wrote:
> > > Checkpatch.pl doesn't have a check for excluding while (...) {...}
> > > blocks from MULTISTATEMENT_MACRO_USE_DO_WHILE error.
> > > 
> > > For example, running checkpatch.pl on the file mm/access.c in the
> > > kernel generates the following error:
> > > 
> > > ERROR: Macros with complex values should be enclosed in parentheses
> > > +#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label)        \
> > > +     while (len >= sizeof(type)) {                                   \
> > > +             __get_kernel_nofault(dst, src, type, err_label);        \
> > > +             dst += sizeof(type);                                    \
> > > +             src += sizeof(type);                                    \
> > > +             len -= sizeof(type);                                    \
> > > +     }
> > > 
> > > The error is misleading for this case. Enclosing it in parantheses
> > > doesn't make any sense.
> > 
> > OK
> > 
> > > Checkpatch already has an exception list for such common macro types.
> > > Added a new exception for while (...) {...} style blocks to the same.
> > > This effectively fixed the wrong error message.
> > []
> > > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> > []
> > > @@ -5342,6 +5342,7 @@ sub process {
> > >                           $dstat !~ /^\.$Ident\s*=/ &&                                # .foo =
> > >                           $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&          # stringification #foo
> > >                           $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&       # do {...} while (...); // do {...} while (...)
> > > +                         $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&           # while (...) {...}

Note the \s*
                                                              ^

> > Did you try to output $dstat for some matching cases?
> > What was the $dstat value for the cases you tried?
> > 
> > 
> Hi,
> I did check $dstat values.
> 
> For example on file mm/maccess.c, there were two such macros:
> 
> Case 1:
> 
> $ctx:
> +#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \
> + while (len >= sizeof(type)) { \
> + __get_kernel_nofault(dst, src, type, err_label); \
> + dst += sizeof(type); \
> + src += sizeof(type); \
> + len -= sizeof(type); \
> + }
> 
> $dstat:
> while 1 1

And perhaps this test should use \s+ instead.
What is $dstat with a #define like:

#define foo(bar,baz)while(bar){bar--;baz++;}

(no spaces anywhere bot the required one after define
	
> Case 2:
> 
> $ctx:
> +#define copy_to_kernel_nofault_loop(dst, src, len, type, err_label) \
> + while (len >= sizeof(type)) { \
> + __put_kernel_nofault(dst, src, type, err_label); \
> + dst += sizeof(type); \
> + src += sizeof(type); \
> + len -= sizeof(type); \
> + }
> 
> $dstat:
> while 1 1
> 
> 
> Thanks,
> Dwaipayan.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH RFC] checkpatch: fix multi-statement macro checks
  2020-10-01 13:42     ` Joe Perches
@ 2020-10-01 14:14       ` Dwaipayan Ray
  2020-10-01 14:38         ` Joe Perches
  0 siblings, 1 reply; 9+ messages in thread
From: Dwaipayan Ray @ 2020-10-01 14:14 UTC (permalink / raw)
  To: Joe Perches; +Cc: linux-kernel, linux-kernel-mentees, Lukas Bulwahn

On Thu, Oct 1, 2020 at 7:12 PM Joe Perches <joe@perches.com> wrote:
>
> On Thu, 2020-10-01 at 18:57 +0530, Dwaipayan Ray wrote:
> > On Thu, Oct 1, 2020 at 6:47 PM Joe Perches <joe@perches.com> wrote:
> > > On Thu, 2020-10-01 at 16:03 +0530, Dwaipayan Ray wrote:
> > > > Checkpatch.pl doesn't have a check for excluding while (...) {...}
> > > > blocks from MULTISTATEMENT_MACRO_USE_DO_WHILE error.
> > > >
> > > > For example, running checkpatch.pl on the file mm/access.c in the
> > > > kernel generates the following error:
> > > >
> > > > ERROR: Macros with complex values should be enclosed in parentheses
> > > > +#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label)        \
> > > > +     while (len >= sizeof(type)) {                                   \
> > > > +             __get_kernel_nofault(dst, src, type, err_label);        \
> > > > +             dst += sizeof(type);                                    \
> > > > +             src += sizeof(type);                                    \
> > > > +             len -= sizeof(type);                                    \
> > > > +     }
> > > >
> > > > The error is misleading for this case. Enclosing it in parantheses
> > > > doesn't make any sense.
> > >
> > > OK
> > >
> > > > Checkpatch already has an exception list for such common macro types.
> > > > Added a new exception for while (...) {...} style blocks to the same.
> > > > This effectively fixed the wrong error message.
> > > []
> > > > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> > > []
> > > > @@ -5342,6 +5342,7 @@ sub process {
> > > >                           $dstat !~ /^\.$Ident\s*=/ &&                                # .foo =
> > > >                           $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&          # stringification #foo
> > > >                           $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&       # do {...} while (...); // do {...} while (...)
> > > > +                         $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&           # while (...) {...}
>
> Note the \s*
>                                                               ^
>
> > > Did you try to output $dstat for some matching cases?
> > > What was the $dstat value for the cases you tried?
> > >
> > >
> > Hi,
> > I did check $dstat values.
> >
> > For example on file mm/maccess.c, there were two such macros:
> >
> > Case 1:
> >
> > $ctx:
> > +#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \
> > + while (len >= sizeof(type)) { \
> > + __get_kernel_nofault(dst, src, type, err_label); \
> > + dst += sizeof(type); \
> > + src += sizeof(type); \
> > + len -= sizeof(type); \
> > + }
> >
> > $dstat:
> > while 1 1
>
> And perhaps this test should use \s+ instead.
> What is $dstat with a #define like:
>
> #define foo(bar,baz)while(bar){bar--;baz++;}
>
> (no spaces anywhere bot the required one after define
>

In this case, $dstat is: while11

So, if \s+ is used, it won't match with this. I ran checkpatch
on it and some other condition seems to match, so it is
excluded from the error.

However, if the macro is like:

#define foo(bar,baz)while(bar) {bar--;baz++;}
(one space after condition)

$dstat is: while1 1
(space after first 1)
and the same error is again emitted.

So I think \s* works better since there can be
0 or more whitespaces between them.

Thanks,
Dwaipayan.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH RFC] checkpatch: fix multi-statement macro checks
  2020-10-01 14:14       ` Dwaipayan Ray
@ 2020-10-01 14:38         ` Joe Perches
  2020-10-01 15:26           ` Joe Perches
  0 siblings, 1 reply; 9+ messages in thread
From: Joe Perches @ 2020-10-01 14:38 UTC (permalink / raw)
  To: Dwaipayan Ray; +Cc: linux-kernel, linux-kernel-mentees, Lukas Bulwahn

On Thu, 2020-10-01 at 19:44 +0530, Dwaipayan Ray wrote:
> On Thu, Oct 1, 2020 at 7:12 PM Joe Perches <joe@perches.com> wrote:
> > On Thu, 2020-10-01 at 18:57 +0530, Dwaipayan Ray wrote:
> > > On Thu, Oct 1, 2020 at 6:47 PM Joe Perches <joe@perches.com> wrote:
> > > > On Thu, 2020-10-01 at 16:03 +0530, Dwaipayan Ray wrote:
> > > > > Checkpatch.pl doesn't have a check for excluding while (...) {...}
> > > > > blocks from MULTISTATEMENT_MACRO_USE_DO_WHILE error.
> > > > > 
> > > > > For example, running checkpatch.pl on the file mm/access.c in the
> > > > > kernel generates the following error:
> > > > > 
> > > > > ERROR: Macros with complex values should be enclosed in parentheses
> > > > > +#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label)        \
> > > > > +     while (len >= sizeof(type)) {                                   \
> > > > > +             __get_kernel_nofault(dst, src, type, err_label);        \
> > > > > +             dst += sizeof(type);                                    \
> > > > > +             src += sizeof(type);                                    \
> > > > > +             len -= sizeof(type);                                    \
> > > > > +     }
> > > > > 
> > > > > The error is misleading for this case. Enclosing it in parantheses
> > > > > doesn't make any sense.
> > > > 
> > > > OK
> > > > 
> > > > > Checkpatch already has an exception list for such common macro types.
> > > > > Added a new exception for while (...) {...} style blocks to the same.
> > > > > This effectively fixed the wrong error message.
> > > > []
> > > > > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> > > > []
> > > > > @@ -5342,6 +5342,7 @@ sub process {
> > > > >                           $dstat !~ /^\.$Ident\s*=/ &&                                # .foo =
> > > > >                           $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&          # stringification #foo
> > > > >                           $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&       # do {...} while (...); // do {...} while (...)
> > > > > +                         $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&           # while (...) {...}
> > 
> > Note the \s*
> >                                                               ^
> > 
> > > > Did you try to output $dstat for some matching cases?
> > > > What was the $dstat value for the cases you tried?
> > > > 
> > > > 
> > > Hi,
> > > I did check $dstat values.
> > > 
> > > For example on file mm/maccess.c, there were two such macros:
> > > 
> > > Case 1:
> > > 
> > > $ctx:
> > > +#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \
> > > + while (len >= sizeof(type)) { \
> > > + __get_kernel_nofault(dst, src, type, err_label); \
> > > + dst += sizeof(type); \
> > > + src += sizeof(type); \
> > > + len -= sizeof(type); \
> > > + }
> > > 
> > > $dstat:
> > > while 1 1
> > 
> > And perhaps this test should use \s+ instead.
> > What is $dstat with a #define like:
> > 
> > #define foo(bar,baz)while(bar){bar--;baz++;}
> > 
> > (no spaces anywhere bot the required one after define
> > 
> 
> In this case, $dstat is: while11
> 
> So, if \s+ is used, it won't match with this. I ran checkpatch
> on it and some other condition seems to match, so it is
> excluded from the error.
> 
> However, if the macro is like:
> 
> #define foo(bar,baz)while(bar) {bar--;baz++;}
> (one space after condition)
> 
> $dstat is: while1 1
> (space after first 1)
> and the same error is again emitted.
> 
> So I think \s* works better since there can be
> 0 or more whitespaces between them.

All I'm trying to point out to you is that $Constant\s*$Constant
isn't a proper test as the first $Constant will pull the test
entire sequence of digits and the second $Constant will not be
met.

It may take some conversion of the collapsing of the dstat
block to work appropriately


			# Flatten any parentheses and braces
			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
			       $dstat =~ s/.\[[^\[\]]*\]/1/)
			{
			}

Maybe the /1/ should be / 1 / but I didn't look to see what
happens to the exclusion tests below that.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH RFC] checkpatch: fix multi-statement macro checks
  2020-10-01 14:38         ` Joe Perches
@ 2020-10-01 15:26           ` Joe Perches
  2020-10-01 15:36             ` Dwaipayan Ray
  0 siblings, 1 reply; 9+ messages in thread
From: Joe Perches @ 2020-10-01 15:26 UTC (permalink / raw)
  To: Dwaipayan Ray; +Cc: linux-kernel, linux-kernel-mentees, Lukas Bulwahn

On Thu, 2020-10-01 at 07:38 -0700, Joe Perches wrote:
> On Thu, 2020-10-01 at 19:44 +0530, Dwaipayan Ray wrote:
> > On Thu, Oct 1, 2020 at 7:12 PM Joe Perches <joe@perches.com> wrote:
> > > On Thu, 2020-10-01 at 18:57 +0530, Dwaipayan Ray wrote:
> > > > On Thu, Oct 1, 2020 at 6:47 PM Joe Perches <joe@perches.com> wrote:
> > > > > On Thu, 2020-10-01 at 16:03 +0530, Dwaipayan Ray wrote:
> > > > > > Checkpatch.pl doesn't have a check for excluding while (...) {...}
> > > > > > blocks from MULTISTATEMENT_MACRO_USE_DO_WHILE error.
> > > > > > 
> > > > > > For example, running checkpatch.pl on the file mm/access.c in the
> > > > > > kernel generates the following error:
> > > > > > 
> > > > > > ERROR: Macros with complex values should be enclosed in parentheses
> > > > > > +#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label)        \
> > > > > > +     while (len >= sizeof(type)) {                                   \
> > > > > > +             __get_kernel_nofault(dst, src, type, err_label);        \
> > > > > > +             dst += sizeof(type);                                    \
> > > > > > +             src += sizeof(type);                                    \
> > > > > > +             len -= sizeof(type);                                    \
> > > > > > +     }
> > > > > > 
> > > > > > The error is misleading for this case. Enclosing it in parantheses
> > > > > > doesn't make any sense.
> > > > > 
> > > > > OK
> > > > > 
> > > > > > Checkpatch already has an exception list for such common macro types.
> > > > > > Added a new exception for while (...) {...} style blocks to the same.
> > > > > > This effectively fixed the wrong error message.
> > > > > []
> > > > > > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> > > > > []
> > > > > > @@ -5342,6 +5342,7 @@ sub process {
> > > > > >                           $dstat !~ /^\.$Ident\s*=/ &&                                # .foo =
> > > > > >                           $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&          # stringification #foo
> > > > > >                           $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&       # do {...} while (...); // do {...} while (...)
> > > > > > +                         $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&           # while (...) {...}
> > > 
> > > Note the \s*
> > >                                                               ^
> > > 
> > > > > Did you try to output $dstat for some matching cases?
> > > > > What was the $dstat value for the cases you tried?
> > > > > 
> > > > > 
> > > > Hi,
> > > > I did check $dstat values.
> > > > 
> > > > For example on file mm/maccess.c, there were two such macros:
> > > > 
> > > > Case 1:
> > > > 
> > > > $ctx:
> > > > +#define copy_from_kernel_nofault_loop(dst, src, len, type, err_label) \
> > > > + while (len >= sizeof(type)) { \
> > > > + __get_kernel_nofault(dst, src, type, err_label); \
> > > > + dst += sizeof(type); \
> > > > + src += sizeof(type); \
> > > > + len -= sizeof(type); \
> > > > + }
> > > > 
> > > > $dstat:
> > > > while 1 1
> > > 
> > > And perhaps this test should use \s+ instead.
> > > What is $dstat with a #define like:
> > > 
> > > #define foo(bar,baz)while(bar){bar--;baz++;}
> > > 
> > > (no spaces anywhere bot the required one after define
> > > 
> > 
> > In this case, $dstat is: while11
> > 
> > So, if \s+ is used, it won't match with this. I ran checkpatch
> > on it and some other condition seems to match, so it is
> > excluded from the error.
> > 
> > However, if the macro is like:
> > 
> > #define foo(bar,baz)while(bar) {bar--;baz++;}
> > (one space after condition)
> > 
> > $dstat is: while1 1
> > (space after first 1)
> > and the same error is again emitted.
> > 
> > So I think \s* works better since there can be
> > 0 or more whitespaces between them.
> 
> All I'm trying to point out to you is that $Constant\s*$Constant
> isn't a proper test as the first $Constant will pull the test
> entire sequence of digits and the second $Constant will not be
> met.
> 
> It may take some conversion of the collapsing of the dstat
> block to work appropriately
> 
> 
> 			# Flatten any parentheses and braces
> 			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
> 			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
> 			       $dstat =~ s/.\[[^\[\]]*\]/1/)
> 			{
> 			}
> 
> Maybe the /1/ should be / 1 / but I didn't look to see what
> happens to the exclusion tests below that.

I think your patch would work well enough if the /1/ bits
here were simply changed to /1u/.

1 is a $Constant as it's just a number.
11 though is also a $Constant.
1u is also a $Constant but it stops the acquisition of
digits that 11 would not and the sequence of
"while1u1u" should match your newly introduced test
of $Constant\s*$Constant as "while11" would not match.







> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH RFC] checkpatch: fix multi-statement macro checks
  2020-10-01 15:26           ` Joe Perches
@ 2020-10-01 15:36             ` Dwaipayan Ray
  0 siblings, 0 replies; 9+ messages in thread
From: Dwaipayan Ray @ 2020-10-01 15:36 UTC (permalink / raw)
  To: Joe Perches; +Cc: linux-kernel, linux-kernel-mentees, Lukas Bulwahn

> > All I'm trying to point out to you is that $Constant\s*$Constant
> > isn't a proper test as the first $Constant will pull the test
> > entire sequence of digits and the second $Constant will not be
> > met.
> >
> > It may take some conversion of the collapsing of the dstat
> > block to work appropriately
> >
> >
> >                       # Flatten any parentheses and braces
> >                       while ($dstat =~ s/\([^\(\)]*\)/1/ ||
> >                              $dstat =~ s/\{[^\{\}]*\}/1/ ||
> >                              $dstat =~ s/.\[[^\[\]]*\]/1/)
> >                       {
> >                       }
> >
> > Maybe the /1/ should be / 1 / but I didn't look to see what
> > happens to the exclusion tests below that.
>
> I think your patch would work well enough if the /1/ bits
> here were simply changed to /1u/.
>
> 1 is a $Constant as it's just a number.
> 11 though is also a $Constant.
> 1u is also a $Constant but it stops the acquisition of
> digits that 11 would not and the sequence of
> "while1u1u" should match your newly introduced test
> of $Constant\s*$Constant as "while11" would not match.
>
>

Hi,
That's an amazing idea! I tried it and this time it seems to
detect it properly. Also this fixes the similar case in
for(...) {...}.

It should not have any side effects also for other checks.
Pretty amazing.

I will rewrite the patch with your suggestion and send it
back.

Thanks,
Dwaipayan.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-10-01 15:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-01 10:33 [PATCH RFC] checkpatch: fix multi-statement macro checks Dwaipayan Ray
2020-10-01 11:12 ` Lukas Bulwahn
2020-10-01 13:17 ` Joe Perches
2020-10-01 13:27   ` Dwaipayan Ray
2020-10-01 13:42     ` Joe Perches
2020-10-01 14:14       ` Dwaipayan Ray
2020-10-01 14:38         ` Joe Perches
2020-10-01 15:26           ` Joe Perches
2020-10-01 15:36             ` Dwaipayan Ray

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).