xref: /linux/tools/lib/python/kdoc/xforms_lists.py (revision 713e899a683eb764b645eaeab79e7308cda497a7)
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, CTokenizer
9
10struct_args_pattern = r"([^,)]+)"
11
12
13class CTransforms:
14    """
15    Data class containing a long set of transformations to turn
16    structure member prefixes, and macro invocations and variables
17    into something we can parse and generate kdoc for.
18    """
19
20    #
21    # NOTE:
22    #      Due to performance reasons, place CMatch rules before KernRe,
23    #      as this avoids running the C parser every time.
24    #
25
26    #: Transforms for structs and unions.
27    struct_xforms = [
28        (CMatch("__attribute__"), ""),
29        (CMatch("__aligned"), ""),
30        (CMatch("__counted_by"), ""),
31        (CMatch("__counted_by_(le|be)"), ""),
32        (CMatch("__guarded_by"), ""),
33        (CMatch("__pt_guarded_by"), ""),
34        (CMatch("__packed"), ""),
35        (CMatch("CRYPTO_MINALIGN_ATTR"), ""),
36        (CMatch("__private"), ""),
37        (CMatch("__rcu"), ""),
38        (CMatch("____cacheline_aligned_in_smp"), ""),
39        (CMatch("____cacheline_aligned"), ""),
40        (CMatch("__cacheline_group_(?:begin|end)"), ""),
41        (CMatch("__ETHTOOL_DECLARE_LINK_MODE_MASK"), r"DECLARE_BITMAP(\1, __ETHTOOL_LINK_MODE_MASK_NBITS)"),
42        (CMatch("DECLARE_PHY_INTERFACE_MASK",),r"DECLARE_BITMAP(\1, PHY_INTERFACE_MODE_MAX)"),
43        (CMatch("DECLARE_BITMAP"), r"unsigned long \1[BITS_TO_LONGS(\2)]"),
44        (CMatch("DECLARE_HASHTABLE"), r"unsigned long \1[1 << ((\2) - 1)]"),
45        (CMatch("DECLARE_KFIFO"), r"\2 *\1"),
46        (CMatch("DECLARE_KFIFO_PTR"), r"\2 *\1"),
47        (CMatch("(?:__)?DECLARE_FLEX_ARRAY"), r"\1 \2[]"),
48        (CMatch("DEFINE_DMA_UNMAP_ADDR"), r"dma_addr_t \1"),
49        (CMatch("DEFINE_DMA_UNMAP_LEN"), r"__u32 \1"),
50        (CMatch("VIRTIO_DECLARE_FEATURES"), r"union { u64 \1; u64 \1_array[VIRTIO_FEATURES_U64S]; }"),
51        (CMatch("__cond_acquires"), ""),
52        (CMatch("__cond_releases"), ""),
53        (CMatch("__acquires"), ""),
54        (CMatch("__releases"), ""),
55        (CMatch("__must_hold"), ""),
56        (CMatch("__must_not_hold"), ""),
57        (CMatch("__must_hold_shared"), ""),
58        (CMatch("__cond_acquires_shared"), ""),
59        (CMatch("__acquires_shared"), ""),
60        (CMatch("__releases_shared"), ""),
61        (CMatch("__attribute__"), ""),
62
63        #
64        # Macro __struct_group() creates an union with an anonymous
65        # and a non-anonymous struct, depending on the parameters. We only
66        # need one of those at kernel-doc, as we won't be documenting the same
67        # members twice.
68        #
69        (CMatch("struct_group"), r"struct { \2+ };"),
70        (CMatch("struct_group_attr"), r"struct { \3+ };"),
71        (CMatch("struct_group_tagged"), r"struct { \3+ };"),
72        (CMatch("__struct_group"), r"struct { \4+ };"),
73    ]
74
75    #: Transforms for function prototypes.
76    function_xforms = [
77        (CMatch("static"), ""),
78        (CMatch("extern"), ""),
79        (CMatch("asmlinkage"), ""),
80        (CMatch("inline"), ""),
81        (CMatch("__inline__"), ""),
82        (CMatch("__inline"), ""),
83        (CMatch("__always_inline"), ""),
84        (CMatch("noinline"), ""),
85        (CMatch("__FORTIFY_INLINE"), ""),
86        (CMatch("__init"), ""),
87        (CMatch("__init_or_module"), ""),
88        (CMatch("__exit"), ""),
89        (CMatch("__deprecated"), ""),
90        (CMatch("__flatten"), ""),
91        (CMatch("__meminit"), ""),
92        (CMatch("__must_check"), ""),
93        (CMatch("__weak"), ""),
94        (CMatch("__sched"), ""),
95        (CMatch("__always_unused"), ""),
96        (CMatch("__maybe_unused"), ""),
97        (CMatch("__printf"), ""),
98        (CMatch("__(?:re)?alloc_size"), ""),
99        (CMatch("__diagnose_as"), ""),
100        (CMatch("DECL_BUCKET_PARAMS"), r"\1, \2"),
101        (CMatch("__no_context_analysis"), ""),
102        (CMatch("__attribute_const__"), ""),
103        (CMatch("__attribute__"), ""),
104
105        #
106        # HACK: this is similar to process_export() hack. It is meant to
107        # drop _noproof from function name. See for instance:
108        # ahash_request_alloc kernel-doc declaration at include/crypto/hash.h.
109        #
110        (KernRe("_noprof"), ""),
111    ]
112
113    #: Transforms for variable prototypes.
114    var_xforms = [
115        (CMatch("__read_mostly"), ""),
116        (CMatch("__ro_after_init"), ""),
117        (CMatch("__guarded_by"), ""),
118        (CMatch("__pt_guarded_by"), ""),
119        (CMatch("LIST_HEAD"), r"struct list_head \1"),
120
121        (KernRe(r"(?://.*)$"), ""),
122        (KernRe(r"(?:/\*.*\*/)"), ""),
123        (KernRe(r";$"), ""),
124    ]
125
126    #: Transforms main dictionary used at apply_transforms().
127    xforms = {
128        "struct": struct_xforms,
129        "func": function_xforms,
130        "var": var_xforms,
131    }
132
133    def apply(self, xforms_type, source):
134        """
135        Apply a set of transforms to a block of source.
136
137        As tokenizer is used here, this function also remove comments
138        at the end.
139        """
140        if xforms_type not in self.xforms:
141            return source
142
143        if isinstance(source, str):
144            source = CTokenizer(source)
145
146        for search, subst in self.xforms[xforms_type]:
147            #
148            # KernRe only accept strings.
149            #
150            if isinstance(search, KernRe):
151                source = str(source)
152
153            source = search.sub(subst, source)
154        return str(source)
155