xref: /linux/tools/lib/python/kdoc/xforms_lists.py (revision f63e6163c7e4f988b2ff35721ffc86b95425293f)
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        (KernRe(r'__ETHTOOL_DECLARE_LINK_MODE_MASK\s*\(([^\)]+)\)', re.S),
36        r'DECLARE_BITMAP(\1, __ETHTOOL_LINK_MODE_MASK_NBITS)'),
37        (KernRe(r'DECLARE_PHY_INTERFACE_MASK\s*\(([^\)]+)\)', re.S),
38        r'DECLARE_BITMAP(\1, PHY_INTERFACE_MODE_MAX)'),
39        (KernRe(r'DECLARE_BITMAP\s*\(' + struct_args_pattern + r',\s*' + struct_args_pattern + r'\)',
40                re.S), r'unsigned long \1[BITS_TO_LONGS(\2)]'),
41        (KernRe(r'DECLARE_HASHTABLE\s*\(' + struct_args_pattern + r',\s*' + struct_args_pattern + r'\)',
42                re.S), r'unsigned long \1[1 << ((\2) - 1)]'),
43        (KernRe(r'DECLARE_KFIFO\s*\(' + struct_args_pattern + r',\s*' + struct_args_pattern +
44                r',\s*' + struct_args_pattern + r'\)', re.S), r'\2 *\1'),
45        (KernRe(r'DECLARE_KFIFO_PTR\s*\(' + struct_args_pattern + r',\s*' +
46                struct_args_pattern + r'\)', re.S), r'\2 *\1'),
47        (KernRe(r'(?:__)?DECLARE_FLEX_ARRAY\s*\(' + struct_args_pattern + r',\s*' +
48                struct_args_pattern + r'\)', re.S), r'\1 \2[]'),
49        (KernRe(r'DEFINE_DMA_UNMAP_ADDR\s*\(' + struct_args_pattern + r'\)', re.S), r'dma_addr_t \1'),
50        (KernRe(r'DEFINE_DMA_UNMAP_LEN\s*\(' + struct_args_pattern + r'\)', re.S), r'__u32 \1'),
51        (KernRe(r'VIRTIO_DECLARE_FEATURES\(([\w_]+)\)'), r'union { u64 \1; u64 \1_array[VIRTIO_FEATURES_U64S]; }'),
52
53        (CMatch(r"__cond_acquires"), ""),
54        (CMatch(r"__cond_releases"), ""),
55        (CMatch(r"__acquires"), ""),
56        (CMatch(r"__releases"), ""),
57        (CMatch(r"__must_hold"), ""),
58        (CMatch(r"__must_not_hold"), ""),
59        (CMatch(r"__must_hold_shared"), ""),
60        (CMatch(r"__cond_acquires_shared"), ""),
61        (CMatch(r"__acquires_shared"), ""),
62        (CMatch(r"__releases_shared"), ""),
63
64        (CMatch('struct_group'), r'\2'),
65        (CMatch('struct_group_attr'), r'\3'),
66        (CMatch('struct_group_tagged'), r'struct \1 \2; \3'),
67        (CMatch('__struct_group'), r'\4'),
68
69    ]
70
71    #: Transforms for function prototypes.
72    function_xforms = [
73        (KernRe(r"^static +"), ""),
74        (KernRe(r"^extern +"), ""),
75        (KernRe(r"^asmlinkage +"), ""),
76        (KernRe(r"^inline +"), ""),
77        (KernRe(r"^__inline__ +"), ""),
78        (KernRe(r"^__inline +"), ""),
79        (KernRe(r"^__always_inline +"), ""),
80        (KernRe(r"^noinline +"), ""),
81        (KernRe(r"^__FORTIFY_INLINE +"), ""),
82        (KernRe(r"__init +"), ""),
83        (KernRe(r"__init_or_module +"), ""),
84        (KernRe(r"__exit +"), ""),
85        (KernRe(r"__deprecated +"), ""),
86        (KernRe(r"__flatten +"), ""),
87        (KernRe(r"__meminit +"), ""),
88        (KernRe(r"__must_check +"), ""),
89        (KernRe(r"__weak +"), ""),
90        (KernRe(r"__sched +"), ""),
91        (KernRe(r"_noprof"), ""),
92        (KernRe(r"__always_unused *"), ""),
93        (KernRe(r"__printf\s*\(\s*\d*\s*,\s*\d*\s*\) +"), ""),
94        (KernRe(r"__(?:re)?alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) +"), ""),
95        (KernRe(r"__diagnose_as\s*\(\s*\S+\s*(?:,\s*\d+\s*)*\) +"), ""),
96        (KernRe(r"DECL_BUCKET_PARAMS\s*\(\s*(\S+)\s*,\s*(\S+)\s*\)"), r"\1, \2"),
97        (KernRe(r"__no_context_analysis\s*"), ""),
98        (KernRe(r"__attribute_const__ +"), ""),
99        (KernRe(r"__attribute__\s*\(\((?:[\w\s]+(?:\([^)]*\))?\s*,?)+\)\)\s+"), ""),
100    ]
101
102    #: Transforms for variable prototypes.
103    var_xforms = [
104        (KernRe(r"__read_mostly"), ""),
105        (KernRe(r"__ro_after_init"), ""),
106        (KernRe(r'\s*__guarded_by\s*\([^\)]*\)', re.S), ""),
107        (KernRe(r'\s*__pt_guarded_by\s*\([^\)]*\)', re.S), ""),
108        (KernRe(r"LIST_HEAD\(([\w_]+)\)"), r"struct list_head \1"),
109        (KernRe(r"(?://.*)$"), ""),
110        (KernRe(r"(?:/\*.*\*/)"), ""),
111        (KernRe(r";$"), ""),
112    ]
113
114    #: Transforms main dictionary used at apply_transforms().
115    xforms = {
116        "struct": struct_xforms,
117        "func": function_xforms,
118        "var": var_xforms,
119    }
120
121    def apply(self, xforms_type, text):
122        """
123        Apply a set of transforms to a block of text.
124        """
125        if xforms_type not in self.xforms:
126            return text
127
128        for search, subst in self.xforms[xforms_type]:
129            text = search.sub(subst, text)
130        return text
131