Skip to content

Commit e86bdb2

Browse files
AdityaSrivastJonathan Corbet
authored andcommitted
scripts: kernel-doc: reduce repeated regex expressions into variables
There are some regex expressions in the kernel-doc script, which are used repeatedly in the script. Reduce such expressions into variables, which can be used everywhere. A quick manual check found that no errors and warnings were added/removed in this process. Suggested-by: Jonathan Corbet <corbet@lwn.net> Signed-off-by: Aditya Srivastava <yashsri421@gmail.com> Link: https://lore.kernel.org/r/20210514144244.25341-1-yashsri421@gmail.com Signed-off-by: Jonathan Corbet <corbet@lwn.net>
1 parent 2c5ff2c commit e86bdb2

1 file changed

Lines changed: 34 additions & 37 deletions

File tree

‎scripts/kernel-doc‎

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ my $doc_inline_sect = '\s*\*\s*(@\s*[\w][\w\.]*\s*):(.*)';
406406
my $doc_inline_end = '^\s*\*/\s*$';
407407
my $doc_inline_oneline = '^\s*/\*\*\s*(@[\w\s]+):\s*(.*)\s*\*/\s*$';
408408
my $export_symbol = '^\s*EXPORT_SYMBOL(_GPL)?\s*\(\s*(\w+)\s*\)\s*;';
409+
my $function_pointer = qr{([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)};
410+
my $attribute = qr{__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)}i;
409411

410412
my %parameterdescs;
411413
my %parameterdesc_start_lines;
@@ -694,7 +696,7 @@ sub output_function_man(%) {
694696
$post = ");";
695697
}
696698
$type = $args{'parametertypes'}{$parameter};
697-
if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
699+
if ($type =~ m/$function_pointer/) {
698700
# pointer-to-function
699701
print ".BI \"" . $parenth . $1 . "\" " . " \") (" . $2 . ")" . $post . "\"\n";
700702
} else {
@@ -974,7 +976,7 @@ sub output_function_rst(%) {
974976
$count++;
975977
$type = $args{'parametertypes'}{$parameter};
976978

977-
if ($type =~ m/([^\(]*\(\*)\s*\)\s*\(([^\)]*)\)/) {
979+
if ($type =~ m/$function_pointer/) {
978980
# pointer-to-function
979981
print $1 . $parameter . ") (" . $2 . ")";
980982
} else {
@@ -1211,7 +1213,9 @@ sub dump_struct($$) {
12111213
my $members;
12121214
my $type = qr{struct|union};
12131215
# For capturing struct/union definition body, i.e. "{members*}qualifiers*"
1214-
my $definition_body = qr{\{(.*)\}(?:\s*(?:__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned|__attribute__\s*\(\([a-z0-9,_\s\(\)]*\)\)))*};
1216+
my $qualifiers = qr{$attribute|__packed|__aligned|____cacheline_aligned_in_smp|____cacheline_aligned};
1217+
my $definition_body = qr{\{(.*)\}\s*$qualifiers*};
1218+
my $struct_members = qr{($type)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;};
12151219

12161220
if ($x =~ /($type)\s+(\w+)\s*$definition_body/) {
12171221
$decl_type = $1;
@@ -1235,27 +1239,27 @@ sub dump_struct($$) {
12351239
# strip comments:
12361240
$members =~ s/\/\*.*?\*\///gos;
12371241
# strip attributes
1238-
$members =~ s/\s*__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)/ /gi;
1242+
$members =~ s/\s*$attribute/ /gi;
12391243
$members =~ s/\s*__aligned\s*\([^;]*\)/ /gos;
12401244
$members =~ s/\s*__packed\s*/ /gos;
12411245
$members =~ s/\s*CRYPTO_MINALIGN_ATTR/ /gos;
12421246
$members =~ s/\s*____cacheline_aligned_in_smp/ /gos;
12431247
$members =~ s/\s*____cacheline_aligned/ /gos;
12441248

1249+
my $args = qr{([^,)]+)};
12451250
# replace DECLARE_BITMAP
12461251
$members =~ s/__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)/DECLARE_BITMAP($1, __ETHTOOL_LINK_MODE_MASK_NBITS)/gos;
1247-
$members =~ s/DECLARE_BITMAP\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
1252+
$members =~ s/DECLARE_BITMAP\s*\($args,\s*$args\)/unsigned long $1\[BITS_TO_LONGS($2)\]/gos;
12481253
# replace DECLARE_HASHTABLE
1249-
$members =~ s/DECLARE_HASHTABLE\s*\(([^,)]+),\s*([^,)]+)\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
1254+
$members =~ s/DECLARE_HASHTABLE\s*\($args,\s*$args\)/unsigned long $1\[1 << (($2) - 1)\]/gos;
12501255
# replace DECLARE_KFIFO
1251-
$members =~ s/DECLARE_KFIFO\s*\(([^,)]+),\s*([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1256+
$members =~ s/DECLARE_KFIFO\s*\($args,\s*$args,\s*$args\)/$2 \*$1/gos;
12521257
# replace DECLARE_KFIFO_PTR
1253-
$members =~ s/DECLARE_KFIFO_PTR\s*\(([^,)]+),\s*([^,)]+)\)/$2 \*$1/gos;
1254-
1258+
$members =~ s/DECLARE_KFIFO_PTR\s*\($args,\s*$args\)/$2 \*$1/gos;
12551259
my $declaration = $members;
12561260

12571261
# Split nested struct/union elements as newer ones
1258-
while ($members =~ m/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/) {
1262+
while ($members =~ m/$struct_members/) {
12591263
my $newmember;
12601264
my $maintype = $1;
12611265
my $ids = $4;
@@ -1315,7 +1319,7 @@ sub dump_struct($$) {
13151319
}
13161320
}
13171321
}
1318-
$members =~ s/(struct|union)([^\{\};]+)\{([^\{\}]*)\}([^\{\}\;]*)\;/$newmember/;
1322+
$members =~ s/$struct_members/$newmember/;
13191323
}
13201324

13211325
# Ignore other nested elements, like enums
@@ -1555,8 +1559,9 @@ sub create_parameterlist($$$$) {
15551559
my $param;
15561560

15571561
# temporarily replace commas inside function pointer definition
1558-
while ($args =~ /(\([^\),]+),/) {
1559-
$args =~ s/(\([^\),]+),/$1#/g;
1562+
my $arg_expr = qr{\([^\),]+};
1563+
while ($args =~ /$arg_expr,/) {
1564+
$args =~ s/($arg_expr),/$1#/g;
15601565
}
15611566

15621567
foreach my $arg (split($splitter, $args)) {
@@ -1707,7 +1712,7 @@ sub check_sections($$$$$) {
17071712
foreach $px (0 .. $#prms) {
17081713
$prm_clean = $prms[$px];
17091714
$prm_clean =~ s/\[.*\]//;
1710-
$prm_clean =~ s/__attribute__\s*\(\([a-z,_\*\s\(\)]*\)\)//i;
1715+
$prm_clean =~ s/$attribute//i;
17111716
# ignore array size in a parameter string;
17121717
# however, the original param string may contain
17131718
# spaces, e.g.: addr[6 + 2]
@@ -1809,32 +1814,24 @@ sub dump_function($$) {
18091814
# - parport_register_device (function pointer parameters)
18101815
# - atomic_set (macro)
18111816
# - pci_match_device, __copy_to_user (long return type)
1812-
1813-
if ($define && $prototype =~ m/^()([a-zA-Z0-9_~:]+)\s+/) {
1817+
my $name = qr{[a-zA-Z0-9_~:]+};
1818+
my $prototype_end1 = qr{[^\(]*};
1819+
my $prototype_end2 = qr{[^\{]*};
1820+
my $prototype_end = qr{\(($prototype_end1|$prototype_end2)\)};
1821+
my $type1 = qr{[\w\s]+};
1822+
my $type2 = qr{$type1\*+};
1823+
1824+
if ($define && $prototype =~ m/^()($name)\s+/) {
18141825
# This is an object-like macro, it has no return type and no parameter
18151826
# list.
18161827
# Function-like macros are not allowed to have spaces between
18171828
# declaration_name and opening parenthesis (notice the \s+).
18181829
$return_type = $1;
18191830
$declaration_name = $2;
18201831
$noret = 1;
1821-
} elsif ($prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1822-
$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1823-
$prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1824-
$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1825-
$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1826-
$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1827-
$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\(]*)\)/ ||
1828-
$prototype =~ m/^()([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1829-
$prototype =~ m/^(\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1830-
$prototype =~ m/^(\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1831-
$prototype =~ m/^(\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1832-
$prototype =~ m/^(\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1833-
$prototype =~ m/^(\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1834-
$prototype =~ m/^(\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1835-
$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+)\s+([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1836-
$prototype =~ m/^(\w+\s+\w+\s+\w+\s+\w+\s*\*+)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/ ||
1837-
$prototype =~ m/^(\w+\s+\w+\s*\*+\s*\w+\s*\*+\s*)\s*([a-zA-Z0-9_~:]+)\s*\(([^\{]*)\)/) {
1832+
} elsif ($prototype =~ m/^()($name)\s*$prototype_end/ ||
1833+
$prototype =~ m/^($type1)\s+($name)\s*$prototype_end/ ||
1834+
$prototype =~ m/^($type2+)\s*($name)\s*$prototype_end/) {
18381835
$return_type = $1;
18391836
$declaration_name = $2;
18401837
my $args = $3;
@@ -2111,12 +2108,12 @@ sub process_name($$) {
21112108
} elsif (/$doc_decl/o) {
21122109
$identifier = $1;
21132110
my $is_kernel_comment = 0;
2114-
my $decl_start = qr{\s*\*};
2111+
my $decl_start = qr{$doc_com};
21152112
# test for pointer declaration type, foo * bar() - desc
21162113
my $fn_type = qr{\w+\s*\*\s*};
21172114
my $parenthesis = qr{\(\w*\)};
21182115
my $decl_end = qr{[-:].*};
2119-
if (/^$decl_start\s*([\w\s]+?)$parenthesis?\s*$decl_end?$/) {
2116+
if (/^$decl_start([\w\s]+?)$parenthesis?\s*$decl_end?$/) {
21202117
$identifier = $1;
21212118
}
21222119
if ($identifier =~ m/^(struct|union|enum|typedef)\b\s*(\S*)/) {
@@ -2126,8 +2123,8 @@ sub process_name($$) {
21262123
}
21272124
# Look for foo() or static void foo() - description; or misspelt
21282125
# identifier
2129-
elsif (/^$decl_start\s*$fn_type?(\w+)\s*$parenthesis?\s*$decl_end?$/ ||
2130-
/^$decl_start\s*$fn_type?(\w+.*)$parenthesis?\s*$decl_end$/) {
2126+
elsif (/^$decl_start$fn_type?(\w+)\s*$parenthesis?\s*$decl_end?$/ ||
2127+
/^$decl_start$fn_type?(\w+.*)$parenthesis?\s*$decl_end$/) {
21312128
$identifier = $1;
21322129
$decl_type = 'function';
21332130
$identifier =~ s/^define\s+//;

0 commit comments

Comments
 (0)