[PATCH 1/7] docs: kdoc: don't reinvent string.strip()

Jonathan Corbet posted 7 patches 3 months, 1 week ago
There is a newer version of this series
[PATCH 1/7] docs: kdoc: don't reinvent string.strip()
Posted by Jonathan Corbet 3 months, 1 week ago
process_proto_type() and process_proto_function() reinventing the strip()
string method with a whole series of separate regexes; take all that out
and just use strip().

The previous implementation also (in process_proto_type()) removed C++
comments *after* the above dance, leaving trailing whitespace in that case;
now we do the stripping afterward.  This results in exactly one output
change: the removal of a spurious space in the definition of
BACKLIGHT_POWER_REDUCED - see
https://docs.kernel.org/gpu/backlight.html#c.backlight_properties.

I note that we are putting semicolons after #define lines that really
shouldn't be there - a task for another day.

Signed-off-by: Jonathan Corbet <corbet@lwn.net>
---
 scripts/lib/kdoc/kdoc_parser.py | 27 +++++----------------------
 1 file changed, 5 insertions(+), 22 deletions(-)

diff --git a/scripts/lib/kdoc/kdoc_parser.py b/scripts/lib/kdoc/kdoc_parser.py
index 93938155fce2..d9ff2d066160 100644
--- a/scripts/lib/kdoc/kdoc_parser.py
+++ b/scripts/lib/kdoc/kdoc_parser.py
@@ -1567,17 +1567,9 @@ class KernelDoc:
                 self.entry.prototype += r.group(1) + " "
 
         if '{' in line or ';' in line or KernRe(r'\s*#\s*define').match(line):
-            # strip comments
-            r = KernRe(r'/\*.*?\*/')
-            self.entry.prototype = r.sub('', self.entry.prototype)
-
-            # strip newlines/cr's
-            r = KernRe(r'[\r\n]+')
-            self.entry.prototype = r.sub(' ', self.entry.prototype)
-
-            # strip leading spaces
-            r = KernRe(r'^\s+')
-            self.entry.prototype = r.sub('', self.entry.prototype)
+            # strip comments and surrounding spaces
+            r = KernRe(r'/\*.*\*/')
+            self.entry.prototype = r.sub('', self.entry.prototype).strip()
 
             # Handle self.entry.prototypes for function pointers like:
             #       int (*pcs_config)(struct foo)
@@ -1600,17 +1592,8 @@ class KernelDoc:
     def process_proto_type(self, ln, line):
         """Ancillary routine to process a type"""
 
-        # Strip newlines/cr's.
-        line = KernRe(r'[\r\n]+', re.S).sub(' ', line)
-
-        # Strip leading spaces
-        line = KernRe(r'^\s+', re.S).sub('', line)
-
-        # Strip trailing spaces
-        line = KernRe(r'\s+$', re.S).sub('', line)
-
-        # Strip C99-style comments to the end of the line
-        line = KernRe(r"\/\/.*$", re.S).sub('', line)
+        # Strip C99-style comments and surrounding whitespace
+        line = KernRe(r"//.*$", re.S).sub('', line).strip()
 
         # To distinguish preprocessor directive from regular declaration later.
         if line.startswith('#'):
-- 
2.49.0
Re: [PATCH 1/7] docs: kdoc: don't reinvent string.strip()
Posted by Mauro Carvalho Chehab 3 months ago
Em Tue,  1 Jul 2025 14:57:24 -0600
Jonathan Corbet <corbet@lwn.net> escreveu:

> process_proto_type() and process_proto_function() reinventing the strip()
> string method with a whole series of separate regexes; take all that out
> and just use strip().
> 
> The previous implementation also (in process_proto_type()) removed C++
> comments *after* the above dance, leaving trailing whitespace in that case;
> now we do the stripping afterward.  This results in exactly one output
> change: the removal of a spurious space in the definition of
> BACKLIGHT_POWER_REDUCED - see
> https://docs.kernel.org/gpu/backlight.html#c.backlight_properties.
> 
> I note that we are putting semicolons after #define lines that really
> shouldn't be there - a task for another day.

Perhaps add a FIXME note for us to not forget again about this.

> 
> Signed-off-by: Jonathan Corbet <corbet@lwn.net>

Reviewed-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>

> ---
>  scripts/lib/kdoc/kdoc_parser.py | 27 +++++----------------------
>  1 file changed, 5 insertions(+), 22 deletions(-)
> 
> diff --git a/scripts/lib/kdoc/kdoc_parser.py b/scripts/lib/kdoc/kdoc_parser.py
> index 93938155fce2..d9ff2d066160 100644
> --- a/scripts/lib/kdoc/kdoc_parser.py
> +++ b/scripts/lib/kdoc/kdoc_parser.py
> @@ -1567,17 +1567,9 @@ class KernelDoc:
>                  self.entry.prototype += r.group(1) + " "
>  
>          if '{' in line or ';' in line or KernRe(r'\s*#\s*define').match(line):
> -            # strip comments
> -            r = KernRe(r'/\*.*?\*/')
> -            self.entry.prototype = r.sub('', self.entry.prototype)
> -
> -            # strip newlines/cr's
> -            r = KernRe(r'[\r\n]+')
> -            self.entry.prototype = r.sub(' ', self.entry.prototype)
> -
> -            # strip leading spaces
> -            r = KernRe(r'^\s+')
> -            self.entry.prototype = r.sub('', self.entry.prototype)
> +            # strip comments and surrounding spaces
> +            r = KernRe(r'/\*.*\*/')
> +            self.entry.prototype = r.sub('', self.entry.prototype).strip()
>  
>              # Handle self.entry.prototypes for function pointers like:
>              #       int (*pcs_config)(struct foo)
> @@ -1600,17 +1592,8 @@ class KernelDoc:
>      def process_proto_type(self, ln, line):
>          """Ancillary routine to process a type"""
>  
> -        # Strip newlines/cr's.
> -        line = KernRe(r'[\r\n]+', re.S).sub(' ', line)
> -
> -        # Strip leading spaces
> -        line = KernRe(r'^\s+', re.S).sub('', line)
> -
> -        # Strip trailing spaces
> -        line = KernRe(r'\s+$', re.S).sub('', line)
> -
> -        # Strip C99-style comments to the end of the line
> -        line = KernRe(r"\/\/.*$", re.S).sub('', line)
> +        # Strip C99-style comments and surrounding whitespace
> +        line = KernRe(r"//.*$", re.S).sub('', line).strip()
>  
>          # To distinguish preprocessor directive from regular declaration later.
>          if line.startswith('#'):