docs: kernel-doc: add support to store output on a YAML file

Add a command line parameter and library support to optionally
store:
- KdocItem intermediate format after parsing;
- man pages output;
- rst output.

inside a YAML file.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Message-ID: <ba54277b3c909867153b9547dfa33c1831ca35d9.1773823995.git.mchehab+huawei@kernel.org>
This commit is contained in:
Mauro Carvalho Chehab
2026-03-18 10:11:14 +01:00
committed by Jonathan Corbet
parent b37b3cbbb1
commit 01d6d7bf96
3 changed files with 107 additions and 15 deletions

View File

@@ -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
@@ -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: