Skip to contentSkip to conversation

kernel-doc: fix typedef function parser

PATCH Superseded Mauro Carvalho Chehab 2 participants 4 messages Oct 27, 2020 – Oct 28, 2020 Documentation

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover kernel-doc: fix typedef function parser

Hi Jon,

This small series contain the latest version of the typedef parsing fixes that we've been discussing as:

[PATCH v3 01/56] scripts: kernel-doc: fix typedef parsing

As I said there, at least while discussing it, I opted to split the patch in two.

The first one changes the regex;
The second one is just a cleanup that splits the 3 arguments into 3 vars.

From my side, I'm not 100% confident if the second patch is worth or not.

The advantage of it is that it makes easier to read the regex. It also also makes clearer about the differences between the two typedef regex'es that are used there.

On the other hand, using a site like regex101.com to test it is harder, as one needs to copy-and-paste 3 expressions instead of just one.

So, when appliying, feel free to decide to either:

- merge both as-is (two separate patches);
- fold them into a single patch;
- drop the second patch.

Mauro Carvalho Chehab (2):
scripts: kernel-doc: fix typedef parsing
scripts: kernel-doc: split typedef complex regex

 scripts/kernel-doc | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
Signature · 1 line
2.26.2
#2 lore raw

PATCH v4 2/2 scripts: kernel-doc: split typedef complex regex

The typedef regex for function prototypes are very complex. Split them into 3 separate regex and then join them using qr.

---
 scripts/kernel-doc | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
scripts/kernel-doc +9 -5
index 698835909ac1..f699cf05d409 100755
@@ -1431,17 +1431,21 @@ sub dump_enum($$) {
     }
 }
 
+my $typedef_type = qr { ((?:\s+[\w\*]+){1,8})\s* }x;
+my $typedef_ident = qr { \*?\s*(\w\S+)\s* }x;
+my $typedef_args = qr { \s*\((.*)\); }x;
+
+my $typedef1 = qr { typedef$typedef_type\($typedef_ident\)$typedef_args }x;
+my $typedef2 = qr { typedef$typedef_type$typedef_ident$typedef_args }x;
+
 sub dump_typedef($$) {
     my $x = shift;
     my $file = shift;
 
     $x =~ s@/\*.*?\*/@@gos;	# strip comments.
 
-    # Parse function prototypes
-    if ($x =~ /typedef((?:\s+[\w\*]+){1,8})\s*\(\*?\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
-	$x =~ /typedef((?:\s+[\w\*]+\s+){1,8})\s*\*?(\w\S+)\s*\s*\((.*)\);/) {
-
-	# Function typedefs
+    # Parse function typedef prototypes
+    if ($x =~ $typedef1 || $x =~ $typedef2) {
 	$return_type = $1;
 	$declaration_name = $2;
 	my $args = $3;
Signature · 1 line
2.26.2
#1 lore raw

PATCH v4 1/2 scripts: kernel-doc: fix typedef parsing

In reply to Mauro Carvalho Chehab

The include/linux/genalloc.h file defined this typedef:

	typedef unsigned long (*genpool_algo_t)(unsigned long *map,unsigned long size,unsigned long start,unsigned int nr,void *data, struct gen_pool *pool, unsigned long start_addr);

Because it has a type composite of two words (unsigned long), the parser gets the typedef name wrong:

.. c:macro:: long

**Typedef**: Allocation callback function type definition

Fix the regex in order to accept composite types when defining a typedef for a function pointer.

---
 scripts/kernel-doc | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
scripts/kernel-doc +3 -2
index 99cd8418ff8a..698835909ac1 100755
@@ -1438,13 +1438,14 @@ sub dump_typedef($$) {
     $x =~ s@/\*.*?\*/@@gos;	# strip comments.
 
     # Parse function prototypes
-    if ($x =~ /typedef\s+(\w+)\s*\(\*\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
-	$x =~ /typedef\s+(\w+)\s*(\w\S+)\s*\s*\((.*)\);/) {
+    if ($x =~ /typedef((?:\s+[\w\*]+){1,8})\s*\(\*?\s*(\w\S+)\s*\)\s*\((.*)\);/ ||
+	$x =~ /typedef((?:\s+[\w\*]+\s+){1,8})\s*\*?(\w\S+)\s*\s*\((.*)\);/) {
 
 	# Function typedefs
 	$return_type = $1;
 	$declaration_name = $2;
 	my $args = $3;
+	$return_type =~ s/^\s+//;
 
 	create_parameterlist($args, ',', $file, $declaration_name);
 
Signature · 1 line
2.26.2
maintainer
#3 lore raw
In reply to Mauro Carvalho Chehab
Quoted 23 lines · On Tue, 27 Oct 2020 11:20:35 +0100 Mauro Carvalho Chehab…

This small series contain the latest version of the typedef parsing fixes that we've been discussing as:

[PATCH v3 01/56] scripts: kernel-doc: fix typedef parsing

As I said there, at least while discussing it, I opted to split the patch in two.

The first one changes the regex;
The second one is just a cleanup that splits the 3 arguments into 3 vars.

From my side, I'm not 100% confident if the second patch is worth or not.

The advantage of it is that it makes easier to read the regex. It also also makes clearer about the differences between the two typedef regex'es that are used there.

On the other hand, using a site like regex101.com to test it is harder, as one needs to copy-and-paste 3 expressions instead of just one.

Quoted 5 lines

So, when appliying, feel free to decide to either:

- merge both as-is (two separate patches);
- fold them into a single patch;
- drop the second patch.

I've gone ahead and applied them both. Anything that makes the kernel-doc script more understandable and maintainable is a step in the right direction, I think.

Thanks,

jon