summaryrefslogtreecommitdiffstats
path: root/tools/lib/python/kdoc/kdoc_parser.py
diff options
context:
space:
mode:
authorJonathan Corbet <corbet@lwn.net>2026-03-22 15:25:08 -0600
committerJonathan Corbet <corbet@lwn.net>2026-03-22 15:25:08 -0600
commit0a1a27776ddf0072883cdb4a61b91155553fcb96 (patch)
tree278b2bec91d7c05f9c3132da36256b1d433b35ab /tools/lib/python/kdoc/kdoc_parser.py
parent781171bec0650c00c642564afcb5cce57abda5bf (diff)
parent01d6d7bf9672f1aeabbffaa3fbfb8017223ff878 (diff)
downloadlinux-0a1a27776ddf0072883cdb4a61b91155553fcb96.tar.gz
linux-0a1a27776ddf0072883cdb4a61b91155553fcb96.zip
Merge branch 'mauro' into docs-mw
This series comes after: https://lore.kernel.org/linux-doc/cover.1773770483.git.mchehab+huawei@kernel.org/ It basically contains patches I submitted before on a 40+ patch series, but were less relevant, plus a couple of other minor fixes: - patch 1 improves one of the CTokenizer unit test, fixing some potential issues on it; - patches 2 and 3 contain some improvement/fixes for Sphinx Python autodoc extension. They basically document c_lex.py; - The remaining patches: - create a new class for kernel-doc config; - fix some internal representations of KdocItem; - add unit tests for KernelDoc() parser class; - add support to output KdocItem in YAML, which is a machine-readable output for all documented kAPI. None of the patches should affect man or html output.
Diffstat (limited to 'tools/lib/python/kdoc/kdoc_parser.py')
-rw-r--r--tools/lib/python/kdoc/kdoc_parser.py33
1 files changed, 29 insertions, 4 deletions
diff --git a/tools/lib/python/kdoc/kdoc_parser.py b/tools/lib/python/kdoc/kdoc_parser.py
index f6c4ee3b18c9..a10e64589d76 100644
--- a/tools/lib/python/kdoc/kdoc_parser.py
+++ b/tools/lib/python/kdoc/kdoc_parser.py
@@ -140,7 +140,7 @@ class KernelEntry:
self.parametertypes = {}
self.parameterdesc_start_lines = {}
- self.section_start_lines = {}
+ self.sections_start_lines = {}
self.sections = {}
self.anon_struct_union = False
@@ -220,7 +220,7 @@ class KernelEntry:
self.sections[name] += '\n' + contents
else:
self.sections[name] = contents
- self.section_start_lines[name] = self.new_start_line
+ self.sections_start_lines[name] = self.new_start_line
self.new_start_line = 0
# self.config.log.debug("Section: %s : %s", name, pformat(vars(self)))
@@ -246,12 +246,13 @@ class KernelDoc:
#: String to write when a parameter is not described.
undescribed = "-- undescribed --"
- def __init__(self, config, fname, xforms):
+ def __init__(self, config, fname, xforms, store_src=False):
"""Initialize internal variables"""
self.fname = fname
self.config = config
self.xforms = xforms
+ self.store_src = store_src
tokenizer_set_log(self.config.log, f"{self.fname}: CMatch: ")
@@ -264,6 +265,9 @@ class KernelDoc:
# Place all potential outputs into an array
self.entries = []
+ # When store_src is true, the kernel-doc source content is stored here
+ self.source = None
+
#
# We need Python 3.7 for its "dicts remember the insertion
# order" guarantee
@@ -316,7 +320,7 @@ class KernelDoc:
for section in ["Description", "Return"]:
if section in sections and not sections[section].rstrip():
del sections[section]
- item.set_sections(sections, self.entry.section_start_lines)
+ item.set_sections(sections, self.entry.sections_start_lines)
item.set_params(self.entry.parameterlist, self.entry.parameterdescs,
self.entry.parametertypes,
self.entry.parameterdesc_start_lines)
@@ -1592,6 +1596,15 @@ class KernelDoc:
state.DOCBLOCK: process_docblock,
}
+ def get_source(self):
+ """
+ Return the file content of the lines handled by kernel-doc at the
+ latest parse_kdoc() run.
+
+ Returns none if KernelDoc() was not initialized with store_src,
+ """
+ return self.source
+
def parse_kdoc(self):
"""
Open and process each line of a C source file.
@@ -1605,6 +1618,8 @@ class KernelDoc:
prev = ""
prev_ln = None
export_table = set()
+ self.source = []
+ self.state = state.NORMAL
try:
with open(self.fname, "r", encoding="utf8",
@@ -1631,6 +1646,8 @@ class KernelDoc:
ln, state.name[self.state],
line)
+ prev_state = self.state
+
# This is an optimization over the original script.
# There, when export_file was used for the same file,
# it was read twice. Here, we use the already-existing
@@ -1641,6 +1658,14 @@ class KernelDoc:
# Hand this line to the appropriate state handler
self.state_actions[self.state](self, ln, line)
+ if self.store_src and prev_state != self.state or self.state != state.NORMAL:
+ if self.state == state.NAME:
+ # A "/**" was detected. Add a new source element
+ self.source.append({"ln": ln, "data": line + "\n"})
+ else:
+ # Append to the existing one
+ self.source[-1]["data"] += line + "\n"
+
self.emit_unused_warnings()
except OSError: