Simplify one gnarly regex and remove another altogether; add a comment
describing what is going on.
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
---
scripts/lib/kdoc/kdoc_parser.py | 18 +++++++-----------
1 file changed, 7 insertions(+), 11 deletions(-)
diff --git a/scripts/lib/kdoc/kdoc_parser.py b/scripts/lib/kdoc/kdoc_parser.py
index 36c4035343dc..d7fb79a64487 100644
--- a/scripts/lib/kdoc/kdoc_parser.py
+++ b/scripts/lib/kdoc/kdoc_parser.py
@@ -527,23 +527,19 @@ class KernelDoc:
dtype = KernRe(r'([^\(]+\(\*?)\s*' + re.escape(param)).sub(r'\1', arg)
self.push_parameter(ln, decl_type, param, dtype,
arg, declaration_name)
-
+ #
+ # The array-of-pointers case. Dig the parameter name out from the middle
+ # of the declaration.
+ #
elif KernRe(r'\(.+\)\s*\[').search(arg):
- # Array-of-pointers
-
- arg = arg.replace('#', ',')
- r = KernRe(r'[^\(]+\(\s*\*\s*([\w\[\].]*?)\s*(\s*\[\s*[\w]+\s*\]\s*)*\)')
+ r = KernRe(r'[^\(]+\(\s*\*\s*' r'([\w.]*?)' r'\s*(\[\s*\w+\s*\]\s*)*\)')
if r.match(arg):
param = r.group(1)
else:
self.emit_msg(ln, f"Invalid param: {arg}")
param = arg
-
- dtype = KernRe(r'([^\(]+\(\*?)\s*' + re.escape(param)).sub(r'\1', arg)
-
- self.push_parameter(ln, decl_type, param, dtype,
- arg, declaration_name)
-
+ dtype = arg.replace(param, '')
+ self.push_parameter(ln, decl_type, param, dtype, arg, declaration_name)
elif arg:
#
# Clean up extraneous spaces and split the string at commas; the first
--
2.50.1
On Tue, 12 Aug 2025 13:57:46 -0600 Jonathan Corbet <corbet@lwn.net> wrote: > Simplify one gnarly regex and remove another altogether; add a comment > describing what is going on. > > Signed-off-by: Jonathan Corbet <corbet@lwn.net> > --- > scripts/lib/kdoc/kdoc_parser.py | 18 +++++++----------- > 1 file changed, 7 insertions(+), 11 deletions(-) > > diff --git a/scripts/lib/kdoc/kdoc_parser.py b/scripts/lib/kdoc/kdoc_parser.py > index 36c4035343dc..d7fb79a64487 100644 > --- a/scripts/lib/kdoc/kdoc_parser.py > +++ b/scripts/lib/kdoc/kdoc_parser.py > @@ -527,23 +527,19 @@ class KernelDoc: > dtype = KernRe(r'([^\(]+\(\*?)\s*' + re.escape(param)).sub(r'\1', arg) > self.push_parameter(ln, decl_type, param, dtype, > arg, declaration_name) > - > + # > + # The array-of-pointers case. Dig the parameter name out from the middle > + # of the declaration. > + # > elif KernRe(r'\(.+\)\s*\[').search(arg): > - # Array-of-pointers > - > - arg = arg.replace('#', ',') Hmm... if I'm not mistaken, there is(was?) a previous code that replaced commas by "#". Such statement is needed to catch some corner case. This like here is(was?) needed to restore the original arg string. > - r = KernRe(r'[^\(]+\(\s*\*\s*([\w\[\].]*?)\s*(\s*\[\s*[\w]+\s*\]\s*)*\)') > + r = KernRe(r'[^\(]+\(\s*\*\s*' r'([\w.]*?)' r'\s*(\[\s*\w+\s*\]\s*)*\)') As mentioned on patch 6/7, IMHO doing concats like that at the same line IMO makes it harder to understand. This works best: r = KernRe(r'[^\(]+\(\s*\*\s*' r'([\w.]*?)' r'\s*(\[\s*\w+\s*\]\s*)*\)') > if r.match(arg): > param = r.group(1) > else: > self.emit_msg(ln, f"Invalid param: {arg}") > param = arg > - > - dtype = KernRe(r'([^\(]+\(\*?)\s*' + re.escape(param)).sub(r'\1', arg) > - > - self.push_parameter(ln, decl_type, param, dtype, > - arg, declaration_name) > - > + dtype = arg.replace(param, '') > + self.push_parameter(ln, decl_type, param, dtype, arg, declaration_name) > elif arg: > # > # Clean up extraneous spaces and split the string at commas; the first Thanks, Mauro
Mauro Carvalho Chehab <mchehab+huawei@kernel.org> writes: > On Tue, 12 Aug 2025 13:57:46 -0600 > Jonathan Corbet <corbet@lwn.net> wrote: >> elif KernRe(r'\(.+\)\s*\[').search(arg): >> - # Array-of-pointers >> - >> - arg = arg.replace('#', ',') > > Hmm... if I'm not mistaken, there is(was?) a previous code that replaced > commas by "#". Such statement is needed to catch some corner case. > > This like here is(was?) needed to restore the original arg string. That (hackish :) replacement is still there ... but there will be no commas in anything matched by the regex here, so the restoration is not needed. I can add that to the changelog for curious readers in the future. >> - r = KernRe(r'[^\(]+\(\s*\*\s*([\w\[\].]*?)\s*(\s*\[\s*[\w]+\s*\]\s*)*\)') >> + r = KernRe(r'[^\(]+\(\s*\*\s*' r'([\w.]*?)' r'\s*(\[\s*\w+\s*\]\s*)*\)') > > As mentioned on patch 6/7, IMHO doing concats like that at the same line > IMO makes it harder to understand. This works best: > > r = KernRe(r'[^\(]+\(\s*\*\s*' > r'([\w.]*?)' > r'\s*(\[\s*\w+\s*\]\s*)*\)') I'll do that. Thanks, jon
© 2016 - 2025 Red Hat, Inc.