1 # SPDX-License-Identifier: GPL-2.0
4 # Apply kernel-specific tweaks after the initial document processing
7 from docutils import nodes
9 from sphinx import addnodes
10 if sphinx.version_info[0] < 2 or \
11 sphinx.version_info[0] == 2 and sphinx.version_info[1] < 1:
12 from sphinx.environment import NoUri
14 from sphinx.errors import NoUri
16 from itertools import chain
19 # Python 2 lacks re.ASCII...
23 except AttributeError:
27 # Regex nastiness. Of course.
28 # Try to identify "function()" that's not already marked up some
29 # other way. Sphinx doesn't like a lot of stuff right after a
30 # :c:func: block (i.e. ":c:func:`mmap()`s" flakes out), so the last
31 # bit tries to restrict matches to things that won't create trouble.
33 RE_function = re.compile(r'\b(([a-zA-Z_]\w+)\(\))', flags=ascii_p3)
36 # Sphinx 2 uses the same :c:type role for struct, union, enum and typedef
38 RE_generic_type = re.compile(r'\b(struct|union|enum|typedef)\s+([a-zA-Z_]\w+)',
42 # Sphinx 3 uses a different C role for each one of struct, union, enum and
45 RE_struct = re.compile(r'\b(struct)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
46 RE_union = re.compile(r'\b(union)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
47 RE_enum = re.compile(r'\b(enum)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
48 RE_typedef = re.compile(r'\b(typedef)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
51 # Detects a reference to a documentation page of the form Documentation/... with
52 # an optional extension
54 RE_doc = re.compile(r'(\bDocumentation/)?((\.\./)*[\w\-/]+)\.(rst|txt)')
56 RE_namespace = re.compile(r'^\s*..\s*c:namespace::\s*(\S+)\s*$')
59 # Reserved C words that we should skip when cross-referencing
61 Skipnames = [ 'for', 'if', 'register', 'sizeof', 'struct', 'unsigned' ]
65 # Many places in the docs refer to common system calls. It is
66 # pointless to try to cross-reference them and, as has been known
67 # to happen, somebody defining a function by these names can lead
68 # to the creation of incorrect and confusing cross references. So
69 # just don't even try with these names.
71 Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap',
72 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl',
77 def markup_refs(docname, app, node):
82 # Associate each regex with the function that will markup its matches
84 markup_func_sphinx2 = {RE_doc: markup_doc_ref,
85 RE_function: markup_c_ref,
86 RE_generic_type: markup_c_ref}
88 markup_func_sphinx3 = {RE_doc: markup_doc_ref,
89 RE_function: markup_func_ref_sphinx3,
90 RE_struct: markup_c_ref,
91 RE_union: markup_c_ref,
92 RE_enum: markup_c_ref,
93 RE_typedef: markup_c_ref}
95 if sphinx.version_info[0] >= 3:
96 markup_func = markup_func_sphinx3
98 markup_func = markup_func_sphinx2
100 match_iterators = [regex.finditer(t) for regex in markup_func]
102 # Sort all references by the starting position in text
104 sorted_matches = sorted(chain(*match_iterators), key=lambda m: m.start())
105 for m in sorted_matches:
107 # Include any text prior to match as a normal text node.
110 repl.append(nodes.Text(t[done:m.start()]))
113 # Call the function associated with the regex that matched this text and
114 # append its return to the text
116 repl.append(markup_func[m.re](docname, app, m))
120 repl.append(nodes.Text(t[done:]))
124 # In sphinx3 we can cross-reference to C macro and function, each one with its
125 # own C role, but both match the same regex, so we try both.
127 def markup_func_ref_sphinx3(docname, app, match):
128 class_str = ['c-func', 'c-macro']
129 reftype_str = ['function', 'macro']
131 cdom = app.env.domains['c']
133 # Go through the dance of getting an xref out of the C domain
135 base_target = match.group(2)
136 target_text = nodes.Text(match.group(0))
138 possible_targets = [base_target]
139 # Check if this document has a namespace, and if so, try
140 # cross-referencing inside it first.
142 possible_targets.insert(0, c_namespace + "." + base_target)
144 if base_target not in Skipnames:
145 for target in possible_targets:
146 if target not in Skipfuncs:
147 for class_s, reftype_s in zip(class_str, reftype_str):
148 lit_text = nodes.literal(classes=['xref', 'c', class_s])
149 lit_text += target_text
150 pxref = addnodes.pending_xref('', refdomain = 'c',
152 reftarget = target, modname = None,
155 # XXX The Latex builder will throw NoUri exceptions here,
156 # work around that by ignoring them.
159 xref = cdom.resolve_xref(app.env, docname, app.builder,
160 reftype_s, target, pxref,
170 def markup_c_ref(docname, app, match):
171 class_str = {# Sphinx 2 only
172 RE_function: 'c-func',
173 RE_generic_type: 'c-type',
175 RE_struct: 'c-struct',
178 RE_typedef: 'c-type',
180 reftype_str = {# Sphinx 2 only
181 RE_function: 'function',
182 RE_generic_type: 'type',
190 cdom = app.env.domains['c']
192 # Go through the dance of getting an xref out of the C domain
194 base_target = match.group(2)
195 target_text = nodes.Text(match.group(0))
197 possible_targets = [base_target]
198 # Check if this document has a namespace, and if so, try
199 # cross-referencing inside it first.
201 possible_targets.insert(0, c_namespace + "." + base_target)
203 if base_target not in Skipnames:
204 for target in possible_targets:
205 if not (match.re == RE_function and target in Skipfuncs):
206 lit_text = nodes.literal(classes=['xref', 'c', class_str[match.re]])
207 lit_text += target_text
208 pxref = addnodes.pending_xref('', refdomain = 'c',
209 reftype = reftype_str[match.re],
210 reftarget = target, modname = None,
213 # XXX The Latex builder will throw NoUri exceptions here,
214 # work around that by ignoring them.
217 xref = cdom.resolve_xref(app.env, docname, app.builder,
218 reftype_str[match.re], target, pxref,
229 # Try to replace a documentation reference of the form Documentation/... with a
230 # cross reference to that page
232 def markup_doc_ref(docname, app, match):
233 stddom = app.env.domains['std']
235 # Go through the dance of getting an xref out of the std domain
237 absolute = match.group(1)
238 target = match.group(2)
240 target = "/" + target
242 pxref = addnodes.pending_xref('', refdomain = 'std', reftype = 'doc',
243 reftarget = target, modname = None,
244 classname = None, refexplicit = False)
246 # XXX The Latex builder will throw NoUri exceptions here,
247 # work around that by ignoring them.
250 xref = stddom.resolve_xref(app.env, docname, app.builder, 'doc',
255 # Return the xref if we got it; otherwise just return the plain text.
260 return nodes.Text(match.group(0))
262 def get_c_namespace(app, docname):
263 source = app.env.doc2path(docname)
264 with open(source) as f:
266 match = RE_namespace.search(l)
268 return match.group(1)
271 def auto_markup(app, doctree, name):
273 c_namespace = get_c_namespace(app, name)
274 def text_but_not_a_reference(node):
275 # The nodes.literal test catches ``literal text``, its purpose is to
276 # avoid adding cross-references to functions that have been explicitly
277 # marked with cc:func:.
278 if not isinstance(node, nodes.Text) or isinstance(node.parent, nodes.literal):
281 child_of_reference = False
284 if isinstance(parent, nodes.Referential):
285 child_of_reference = True
287 parent = parent.parent
288 return not child_of_reference
291 # This loop could eventually be improved on. Someday maybe we
292 # want a proper tree traversal with a lot of awareness of which
293 # kinds of nodes to prune. But this works well for now.
295 for para in doctree.traverse(nodes.paragraph):
296 for node in para.traverse(condition=text_but_not_a_reference):
297 node.parent.replace(node, markup_refs(name, app, node))
300 app.connect('doctree-resolved', auto_markup)
302 'parallel_read_safe': True,
303 'parallel_write_safe': True,