[PATCH v2 2/2] checkpatch: 3 use-cases for --debug rx=1 option

Jim Cromie posted 2 patches 3 months, 2 weeks ago
[PATCH v2 2/2] checkpatch: 3 use-cases for --debug rx=1 option
Posted by Jim Cromie 3 months, 2 weeks ago
Use the drx_print() helper in 3 cases inside code which counts macro
arg expansions.

$ scripts/checkpatch.pl --strict patch-1 --debug rx='##'
drx_print: 'arg ##' catenations
  >> Matched (`$&`): <_id##>
drx_print: 'arg ##' catenations
  >> Matched (`$&`): <_id##>
drx_print: '#|## arg' catenations
  >> Matched (`$&`): <##_model>
drx_print: '#|## arg' catenations
  >> Matched (`$&`): <##_model>

$ scripts/checkpatch.pl --strict patch-1 --debug rx='insp'
drx_print: -arg-inspections-
  >> Matched (`$&`): <__builtin_constant_p(cls>
  >> Capture 1 (`$1`): <__builtin_constant_p>

NB: see also the extended --debug key=1 facility:

$ scripts/checkpatch.pl --strict ../linux.git/pt-1 --debug foo=1
Unknown debug key 'foo', expecting: 'values possible type attr rx'

NB: I moved the 2 #|## strippers above the more complex macro, because
the latter caught one ## case that it needn't have.

Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
 scripts/checkpatch.pl | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index c174e3bef2b2..240ddab17d89 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6078,9 +6078,17 @@ sub process {
 			        next if ($arg =~ /\.\.\./);
 			        next if ($arg =~ /^type$/i);
 				my $tmp_stmt = $define_stmt;
-				$tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
-				$tmp_stmt =~ s/\#+\s*$arg\b//g;
-				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
+
+				$tmp_stmt =~ s/\#+\s*$arg\b/drx_print("'#|## arg' catenations")/ge;
+				$tmp_stmt =~ s/\b$arg\s*\#\#/drx_print("'arg ##' catenations");/ge;
+				$tmp_stmt =~ s{
+					\b(__must_be_array|offsetof|sizeof|sizeof_field|
+					   __stringify|typeof|__typeof__|__builtin\w+|
+					   typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b }
+				{
+					drx_print("-arg-inspections-");
+				}xge;
+
 				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
 				if ($use_cnt > 1) {
 					CHK("MACRO_ARG_REUSE",
-- 
2.51.0
Re: [PATCH v2 2/2] checkpatch: 3 use-cases for --debug rx=1 option
Posted by Joe Perches 3 months, 2 weeks ago
On Sun, 2025-10-26 at 14:21 -0600, Jim Cromie wrote:
> Use the drx_print() helper in 3 cases inside code which counts macro
> arg expansions.
[]
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
[]
> @@ -6078,9 +6078,17 @@ sub process {
>  			        next if ($arg =~ /\.\.\./);
>  			        next if ($arg =~ /^type$/i);
>  				my $tmp_stmt = $define_stmt;
> -				$tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
> -				$tmp_stmt =~ s/\#+\s*$arg\b//g;
> -				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
> +
> +				$tmp_stmt =~ s/\#+\s*$arg\b/drx_print("'#|## arg' catenations")/ge;
> +				$tmp_stmt =~ s/\b$arg\s*\#\#/drx_print("'arg ##' catenations");/ge;

stray trailing ; in the replacement ?

> +				$tmp_stmt =~ s{
> +					\b(__must_be_array|offsetof|sizeof|sizeof_field|
> +					   __stringify|typeof|__typeof__|__builtin\w+|
> +					   typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b }

This might be easier to read using a qr but I'm not sure the
embedded capture groups and their use in drx_print is sensible
as it doesn't seem extensible.

our $stmt_stripper = qr{\b(
		__must_be_array |
		offsetof | typeof | __typeof__ |
		sizeof | sizeof_field |
		__builtin\w+
		typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)\(*\s*$arg\s*\)*
		
> +				{
> +					drx_print("-arg-inspections-");
> +				}xge;
> +
>  				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
>  				if ($use_cnt > 1) {
>  					CHK("MACRO_ARG_REUSE",

Back with I suggested this a dozen years ago I thought it was overkill.
Maybe it is and the whole test should be offed.

https://lore.kernel.org/lkml/1352198139.16194.21.camel@joe-AO722/
Re: [PATCH v2 2/2] checkpatch: 3 use-cases for --debug rx=1 option
Posted by jim.cromie@gmail.com 3 months, 1 week ago
On Sun, Oct 26, 2025 at 5:40 PM Joe Perches <joe@perches.com> wrote:
>
> On Sun, 2025-10-26 at 14:21 -0600, Jim Cromie wrote:
> > Use the drx_print() helper in 3 cases inside code which counts macro
> > arg expansions.
> []
> > diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> []
> > @@ -6078,9 +6078,17 @@ sub process {
> >                               next if ($arg =~ /\.\.\./);
> >                               next if ($arg =~ /^type$/i);
> >                               my $tmp_stmt = $define_stmt;
> > -                             $tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
> > -                             $tmp_stmt =~ s/\#+\s*$arg\b//g;
> > -                             $tmp_stmt =~ s/\b$arg\s*\#\#//g;
> > +
> > +                             $tmp_stmt =~ s/\#+\s*$arg\b/drx_print("'#|## arg' catenations")/ge;
> > +                             $tmp_stmt =~ s/\b$arg\s*\#\#/drx_print("'arg ##' catenations");/ge;
>
> stray trailing ; in the replacement ?
>
> > +                             $tmp_stmt =~ s{
> > +                                     \b(__must_be_array|offsetof|sizeof|sizeof_field|
> > +                                        __stringify|typeof|__typeof__|__builtin\w+|
> > +                                        typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b }
>
> This might be easier to read using a qr but I'm not sure the
> embedded capture groups and their use in drx_print is sensible
> as it doesn't seem extensible.
>

yes, the extra whitespace is better.
I will play with qr// see if the captures work the same.

> our $stmt_stripper = qr{\b(
>                 __must_be_array |
>                 offsetof | typeof | __typeof__ |
>                 sizeof | sizeof_field |
>                 __builtin\w+
>                 typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)\(*\s*$arg\s*\)*
>
> > +                             {
> > +                                     drx_print("-arg-inspections-");
> > +                             }xge;
> > +
> >                               my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
> >                               if ($use_cnt > 1) {
> >                                       CHK("MACRO_ARG_REUSE",
>
> Back with I suggested this a dozen years ago I thought it was overkill.
> Maybe it is and the whole test should be offed.
>

I am now playing with an __lvalue(x) macro,
based upon  __must_be_array(x),
it is a compile-time check, so it gives a high-quality signal to checkpatch
if x is an lval, the multiple expansion warnings can be silenced for x.

So if this works out, we could take off the wart, rather than the finger.

> https://lore.kernel.org/lkml/1352198139.16194.21.camel@joe-AO722/