[RFC,v2] scripts: kernel-doc: fix typedef support for struct/union parsing
diff mbox series

Message ID 20210224133002.13503-1-yashsri421@gmail.com
State In Next
Commit 4754eeb04933bfc856588ddc60a5ba01f75eaa91
Headers show
Series
  • [RFC,v2] scripts: kernel-doc: fix typedef support for struct/union parsing
Related show

Commit Message

Aditya Srivastava Feb. 24, 2021, 1:30 p.m. UTC
Currently, there are 447 files in the kernel tree, which use 'typedef
struct/union' syntax for defining some struct/union. In total, there
are ~1290 such occurrences in the kernel tree.

However, kernel-doc does not support it currently, and running
scripts/kernel-doc -none on include/linux/zstd.h emits:
"error: Cannot parse struct or union!"

Add support for parsing struct/union following this syntax.

Signed-off-by: Aditya Srivastava <yashsri421@gmail.com>
---
Changes in v2:
- Split recurring regex into multiple variables
- Modify commit message

 scripts/kernel-doc | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

Comments

Lukas Bulwahn Feb. 25, 2021, 11:48 a.m. UTC | #1
On Wed, Feb 24, 2021 at 2:30 PM Aditya Srivastava <yashsri421@gmail.com> wrote:
>
> Currently, there are 447 files in the kernel tree, which use 'typedef
> struct/union' syntax for defining some struct/union. In total, there
> are ~1290 such occurrences in the kernel tree.
>
> However, kernel-doc does not support it currently, and running
> scripts/kernel-doc -none on emits:
> "error: Cannot parse struct or union!"
>

I will help rephrasing it more precisely:

Currently, there are ~1290 occurrences in 447 files in the kernel tree
'typedef struct/union' syntax for defining some struct/union. However,
kernel-doc currently does not support that syntax. Of the ~1290
occurrences, there are four occurrences in ./include/linux/zstd.h with
typedef struct/union syntax and a preceding kernel-doc; all other
occurrences have no preceding kernel-doc.

Add support for parsing struct/union following this syntax.



I also tested it; here is my quick diff of "git ls-files | xargs
./scripts/kernel-doc -none 2>&1" before and after patch application:

< include/linux/zstd.h:154: error: Cannot parse struct or union!
< include/linux/zstd.h:171: error: Cannot parse struct or union!
< include/linux/zstd.h:181: error: Cannot parse struct or union!
18857d18853
< include/linux/zstd.h:936: error: Cannot parse struct or union!


So, again:

Tested-by: Lukas Bulwahn <lukas.bulwahn@gmail.com>

Aditya, can you please pick up my rephrasing and send out a v3.


Lukas

Patch
diff mbox series

diff --git a/scripts/kernel-doc b/scripts/kernel-doc
index 8b5bc7bf4bb8..68df17877384 100755
--- a/scripts/kernel-doc
+++ b/scripts/kernel-doc
@@ -1201,12 +1201,23 @@  sub dump_union($$) {
 sub dump_struct($$) {
     my $x = shift;
     my $file = shift;
+    my $decl_type;
+    my $members;
+    my $type = qr{struct|union};
+    # For capturing struct/union definition body, i.e. "{members*}qualifiers*"
+    my $definition_body = qr{\{(.*)\}(?:\s*(?:__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*};
 
-    if ($x =~ /(struct|union)\s+(\w+)\s*\{(.*)\}(\s*(__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*/) {
-	my $decl_type = $1;
+    if ($x =~ /($type)\s+(\w+)\s*$definition_body/) {
+	$decl_type = $1;
 	$declaration_name = $2;
-	my $members = $3;
+	$members = $3;
+    } elsif ($x =~ /typedef\s+($type)\s*$definition_body\s*(\w+)\s*;/) {
+	$decl_type = $1;
+	$declaration_name = $3;
+	$members = $2;
+    }
 
+    if ($members) {
 	if ($identifier ne $declaration_name) {
 	    print STDERR "${file}:$.: warning: expecting prototype for $decl_type $identifier. Prototype was for $decl_type $declaration_name instead\n";
 	    return;