xref: /linux/tools/lib/python/kdoc/xforms_lists.py (revision 600079fdcf46fafe15b4ccd62804d66e05309cc6)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3# Copyright(c) 2026: Mauro Carvalho Chehab <mchehab@kernel.org>.
4
5import re
6
7from kdoc.kdoc_re import KernRe
8from kdoc.c_lex import CMatch
9
10struct_args_pattern = r'([^,)]+)'
11
12class CTransforms:
13    """
14    Data class containing a long set of transformations to turn
15    structure member prefixes, and macro invocations and variables
16    into something we can parse and generate kdoc for.
17    """
18
19    #: Transforms for structs and unions.
20    struct_xforms = [
21        # Strip attributes
22        (KernRe(r"__attribute__\s*\(\([a-z0-9,_\*\s\(\)]*\)\)", flags=re.I | re.S, cache=False), ' '),
23        (KernRe(r'\s*__aligned\s*\([^;]*\)', re.S), ' '),
24        (KernRe(r'\s*__counted_by\s*\([^;]*\)', re.S), ' '),
25        (KernRe(r'\s*__counted_by_(le|be)\s*\([^;]*\)', re.S), ' '),
26        (KernRe(r'\s*__guarded_by\s*\([^\)]*\)', re.S), ' '),
27        (KernRe(r'\s*__pt_guarded_by\s*\([^\)]*\)', re.S), ' '),
28        (KernRe(r'\s*__packed\s*', re.S), ' '),
29        (KernRe(r'\s*CRYPTO_MINALIGN_ATTR', re.S), ' '),
30        (KernRe(r'\s*__private', re.S), ' '),
31        (KernRe(r'\s*__rcu', re.S), ' '),
32        (KernRe(r'\s*____cacheline_aligned_in_smp', re.S), ' '),
33        (KernRe(r'\s*____cacheline_aligned', re.S), ' '),
34        (KernRe(r'\s*__cacheline_group_(begin|end)\([^\)]+\);'), ''),
35        #
36        # Unwrap struct_group macros based on this definition:
37        # __struct_group(TAG, NAME, ATTRS, MEMBERS...)
38        # which has variants like: struct_group(NAME, MEMBERS...)
39        # Only MEMBERS arguments require documentation.
40        #
41        # Parsing them happens on two steps:
42        #
43        # 1. drop struct group arguments that aren't at MEMBERS,
44        #    storing them as STRUCT_GROUP(MEMBERS)
45        #
46        # 2. remove STRUCT_GROUP() ancillary macro.
47        #
48        # The original logic used to remove STRUCT_GROUP() using an
49        # advanced regex:
50        #
51        #   \bSTRUCT_GROUP(\(((?:(?>[^)(]+)|(?1))*)\))[^;]*;
52        #
53        # with two patterns that are incompatible with
54        # Python re module, as it has:
55        #
56        #   - a recursive pattern: (?1)
57        #   - an atomic grouping: (?>...)
58        #
59        # I tried a simpler version: but it didn't work either:
60        #   \bSTRUCT_GROUP\(([^\)]+)\)[^;]*;
61        #
62        # As it doesn't properly match the end parenthesis on some cases.
63        #
64        # So, a better solution was crafted: there's now a CMatch
65        # class that ensures that delimiters after a search are properly
66        # matched. So, the implementation to drop STRUCT_GROUP() will be
67        # handled in separate.
68        #
69        (KernRe(r'\bstruct_group\s*\(([^,]*,)', re.S), r'STRUCT_GROUP('),
70        (KernRe(r'\bstruct_group_attr\s*\(([^,]*,){2}', re.S), r'STRUCT_GROUP('),
71        (KernRe(r'\bstruct_group_tagged\s*\(([^,]*),([^,]*),', re.S), r'struct \1 \2; STRUCT_GROUP('),
72        (KernRe(r'\b__struct_group\s*\(([^,]*,){3}', re.S), r'STRUCT_GROUP('),
73        #
74        # Replace macros
75        #
76        # TODO: use CMatch for FOO($1, $2, ...) matches
77        #
78        # it is better to also move those to the CMatch logic,
79        # to ensure that parentheses will be properly matched.
80        #
81        (KernRe(r'__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)', re.S),
82        r'DECLARE_BITMAP(\1, __ETHTOOL_LINK_MODE_MASK_NBITS)'),
83        (KernRe(r'DECLARE_PHY_INTERFACE_MASK\s*\(([^\)]+)\)', re.S),
84        r'DECLARE_BITMAP(\1, PHY_INTERFACE_MODE_MAX)'),
85        (KernRe(r'DECLARE_BITMAP\s*\(' + struct_args_pattern + r',\s*' + struct_args_pattern + r'\)',
86                re.S), r'unsigned long \1[BITS_TO_LONGS(\2)]'),
87        (KernRe(r'DECLARE_HASHTABLE\s*\(' + struct_args_pattern + r',\s*' + struct_args_pattern + r'\)',
88                re.S), r'unsigned long \1[1 << ((\2) - 1)]'),
89        (KernRe(r'DECLARE_KFIFO\s*\(' + struct_args_pattern + r',\s*' + struct_args_pattern +
90                r',\s*' + struct_args_pattern + r'\)', re.S), r'\2 *\1'),
91        (KernRe(r'DECLARE_KFIFO_PTR\s*\(' + struct_args_pattern + r',\s*' +
92                struct_args_pattern + r'\)', re.S), r'\2 *\1'),
93        (KernRe(r'(?:__)?DECLARE_FLEX_ARRAY\s*\(' + struct_args_pattern + r',\s*' +
94                struct_args_pattern + r'\)', re.S), r'\1 \2[]'),
95        (KernRe(r'DEFINE_DMA_UNMAP_ADDR\s*\(' + struct_args_pattern + r'\)', re.S), r'dma_addr_t \1'),
96        (KernRe(r'DEFINE_DMA_UNMAP_LEN\s*\(' + struct_args_pattern + r'\)', re.S), r'__u32 \1'),
97        (KernRe(r'VIRTIO_DECLARE_FEATURES\(([\w_]+)\)'), r'union { u64 \1; u64 \1_array[VIRTIO_FEATURES_U64S]; }'),
98
99        (CMatch(r"__cond_acquires"), ""),
100        (CMatch(r"__cond_releases"), ""),
101        (CMatch(r"__acquires"), ""),
102        (CMatch(r"__releases"), ""),
103        (CMatch(r"__must_hold"), ""),
104        (CMatch(r"__must_not_hold"), ""),
105        (CMatch(r"__must_hold_shared"), ""),
106        (CMatch(r"__cond_acquires_shared"), ""),
107        (CMatch(r"__acquires_shared"), ""),
108        (CMatch(r"__releases_shared"), ""),
109        (CMatch(r"STRUCT_GROUP"), r'\0'),
110    ]
111
112    #: Transforms for function prototypes.
113    function_xforms = [
114        (KernRe(r"^static +"), ""),
115        (KernRe(r"^extern +"), ""),
116        (KernRe(r"^asmlinkage +"), ""),
117        (KernRe(r"^inline +"), ""),
118        (KernRe(r"^__inline__ +"), ""),
119        (KernRe(r"^__inline +"), ""),
120        (KernRe(r"^__always_inline +"), ""),
121        (KernRe(r"^noinline +"), ""),
122        (KernRe(r"^__FORTIFY_INLINE +"), ""),
123        (KernRe(r"__init +"), ""),
124        (KernRe(r"__init_or_module +"), ""),
125        (KernRe(r"__exit +"), ""),
126        (KernRe(r"__deprecated +"), ""),
127        (KernRe(r"__flatten +"), ""),
128        (KernRe(r"__meminit +"), ""),
129        (KernRe(r"__must_check +"), ""),
130        (KernRe(r"__weak +"), ""),
131        (KernRe(r"__sched +"), ""),
132        (KernRe(r"_noprof"), ""),
133        (KernRe(r"__always_unused *"), ""),
134        (KernRe(r"__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +"), ""),
135        (KernRe(r"__(?:re)?alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) +"), ""),
136        (KernRe(r"__diagnose_as\s*\(\s*\S+\s*(?:,\s*\d+\s*)*\) +"), ""),
137        (KernRe(r"DECL_BUCKET_PARAMS\s*\(\s*(\S+)\s*,\s*(\S+)\s*\)"), r"\1, \2"),
138        (KernRe(r"__no_context_analysis\s*"), ""),
139        (KernRe(r"__attribute_const__ +"), ""),
140        (KernRe(r"__attribute__\s*\(\((?:[\w\s]+(?:\([^)]*\))?\s*,?)+\)\)\s+"), ""),
141    ]
142
143    #: Transforms for variable prototypes.
144    var_xforms = [
145        (KernRe(r"__read_mostly"), ""),
146        (KernRe(r"__ro_after_init"), ""),
147        (KernRe(r'\s*__guarded_by\s*\([^\)]*\)', re.S), ""),
148        (KernRe(r'\s*__pt_guarded_by\s*\([^\)]*\)', re.S), ""),
149        (KernRe(r"LIST_HEAD\(([\w_]+)\)"), r"struct list_head \1"),
150        (KernRe(r"(?://.*)$"), ""),
151        (KernRe(r"(?:/\*.*\*/)"), ""),
152        (KernRe(r";$"), ""),
153    ]
154
155    #: Transforms main dictionary used at apply_transforms().
156    xforms = {
157        "struct": struct_xforms,
158        "func": function_xforms,
159        "var": var_xforms,
160    }
161
162    def apply(self, xforms_type, text):
163        """
164        Apply a set of transforms to a block of text.
165        """
166        if xforms_type not in self.xforms:
167            return text
168
169        for search, subst in self.xforms[xforms_type]:
170            text = search.sub(subst, text)
171        return text
172