Skip to content

Commit a2752f8

Browse files
author
Jonathan Corbet
committed
doc: kdoc: unify transform handling
Both functions and structs are passed through a set of regex-based transforms, but the two were structured differently, despite being the same thing. Create a utility function to apply transformations and use it in both cases. Signed-off-by: Jonathan Corbet <corbet@lwn.net>
1 parent 4c232a8 commit a2752f8

1 file changed

Lines changed: 34 additions & 31 deletions

File tree

scripts/lib/kdoc/kdoc_parser.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
#
7979
struct_args_pattern = r'([^,)]+)'
8080

81-
struct_prefixes = [
81+
struct_xforms = [
8282
# Strip attributes
8383
(KernRe(r"__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)", flags=re.I | re.S, cache=False), ' '),
8484
(KernRe(r'\s*__aligned\s*\([^;]*\)', re.S), ' '),
@@ -165,33 +165,39 @@
165165
# Transforms for function prototypes
166166
#
167167
function_xforms = [
168-
(r"^static +", "", 0),
169-
(r"^extern +", "", 0),
170-
(r"^asmlinkage +", "", 0),
171-
(r"^inline +", "", 0),
172-
(r"^__inline__ +", "", 0),
173-
(r"^__inline +", "", 0),
174-
(r"^__always_inline +", "", 0),
175-
(r"^noinline +", "", 0),
176-
(r"^__FORTIFY_INLINE +", "", 0),
177-
(r"__init +", "", 0),
178-
(r"__init_or_module +", "", 0),
179-
(r"__deprecated +", "", 0),
180-
(r"__flatten +", "", 0),
181-
(r"__meminit +", "", 0),
182-
(r"__must_check +", "", 0),
183-
(r"__weak +", "", 0),
184-
(r"__sched +", "", 0),
185-
(r"_noprof", "", 0),
186-
(r"__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +", "", 0),
187-
(r"__(?:re)?alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) +", "", 0),
188-
(r"__diagnose_as\s*\(\s*\S+\s*(?:,\s*\d+\s*)*\) +", "", 0),
189-
(r"DECL_BUCKET_PARAMS\s*\(\s*(\S+)\s*,\s*(\S+)\s*\)", r"\1, \2", 0),
190-
(r"__attribute_const__ +", "", 0),
191-
(r"__attribute__\s*\(\((?:[\w\s]+(?:\([^)]*\))?\s*,?)+\)\)\s+", "", 0),
168+
(KernRe(r"^static +"), ""),
169+
(KernRe(r"^extern +"), ""),
170+
(KernRe(r"^asmlinkage +"), ""),
171+
(KernRe(r"^inline +"), ""),
172+
(KernRe(r"^__inline__ +"), ""),
173+
(KernRe(r"^__inline +"), ""),
174+
(KernRe(r"^__always_inline +"), ""),
175+
(KernRe(r"^noinline +"), ""),
176+
(KernRe(r"^__FORTIFY_INLINE +"), ""),
177+
(KernRe(r"__init +"), ""),
178+
(KernRe(r"__init_or_module +"), ""),
179+
(KernRe(r"__deprecated +"), ""),
180+
(KernRe(r"__flatten +"), ""),
181+
(KernRe(r"__meminit +"), ""),
182+
(KernRe(r"__must_check +"), ""),
183+
(KernRe(r"__weak +"), ""),
184+
(KernRe(r"__sched +"), ""),
185+
(KernRe(r"_noprof"), ""),
186+
(KernRe(r"__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +"), ""),
187+
(KernRe(r"__(?:re)?alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) +"), ""),
188+
(KernRe(r"__diagnose_as\s*\(\s*\S+\s*(?:,\s*\d+\s*)*\) +"), ""),
189+
(KernRe(r"DECL_BUCKET_PARAMS\s*\(\s*(\S+)\s*,\s*(\S+)\s*\)"), r"\1, \2"),
190+
(KernRe(r"__attribute_const__ +"), ""),
191+
(KernRe(r"__attribute__\s*\(\((?:[\w\s]+(?:\([^)]*\))?\s*,?)+\)\)\s+"), ""),
192192
]
193193

194-
194+
#
195+
# Apply a set of transforms to a block of text.
196+
#
197+
def apply_transforms(xforms, text):
198+
for search, subst in xforms:
199+
text = search.sub(subst, text)
200+
return text
195201

196202
#
197203
# A little helper to get rid of excess white space
@@ -807,8 +813,7 @@ def dump_struct(self, ln, proto):
807813
# Go through the list of members applying all of our transformations.
808814
#
809815
members = trim_private_members(members)
810-
for search, sub in struct_prefixes:
811-
members = search.sub(sub, members)
816+
members = apply_transforms(struct_xforms, members)
812817

813818
nested = NestedMatch()
814819
for search, sub in struct_nested_prefixes:
@@ -924,12 +929,10 @@ def dump_function(self, ln, prototype):
924929
func_macro = False
925930
return_type = ''
926931
decl_type = 'function'
927-
928932
#
929933
# Apply the initial transformations.
930934
#
931-
for search, sub, flags in function_xforms:
932-
prototype = KernRe(search, flags).sub(sub, prototype)
935+
prototype = apply_transforms(function_xforms, prototype)
933936

934937
# Macros are a special case, as they change the prototype format
935938
new_proto = KernRe(r"^#\s*define\s+").sub("", prototype)

0 commit comments

Comments
 (0)