History

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.

Mauro Carvalho Chehab committed 7 months ago

Commit 01d6d7b1 parent b37b3cb3 files · +107 −15Browse files

Showing the changes to tools/lib/python (2 of 3 files). Show all

Unified · Split

2 files
  1. tools/lib/python/kdoc/kdoc_files.py +41 −6
  2. tools/lib/python/kdoc/kdoc_parser.py +26 −1
tools/lib/python/kdoc/kdoc_files.py+41−6
@@ -16,6 +16,7 @@ import re · 15 unmodified lines
1616 from kdoc.kdoc_parser import KernelDoc
1717 from kdoc.xforms_lists import CTransforms
1818 from kdoc.kdoc_output import OutputFormat
19+from kdoc.kdoc_yaml_file import KDocTestFile
1920
2021
2122 class GlobSourceFiles:
@@ -152,6 +153,12 @@ class KernelFiles(): · 130 unmodified lines
152153
153154 If not specified, defaults to use: ``logging.getLogger("kernel-doc")``
154155
156+ ``yaml_file``
157+ If defined, stores the output inside a YAML file.
158+
159+ ``yaml_content``
160+ Defines what will be inside the YAML file.
161+
155162 Note:
156163 There are two type of parsers defined here:
157164
@@ -181,7 +188,12 @@ class KernelFiles(): · 23 unmodified lines
181188 if fname in self.files:
182189 return
183190
184− doc = KernelDoc(self.config, fname, self.xforms)
191+ if self.test_file:
192+ store_src = True
193+ else:
194+ store_src = False
195+
196+ doc = KernelDoc(self.config, fname, self.xforms, store_src=store_src)
185197 export_table, entries = doc.parse_kdoc()
186198
187199 self.export_table[fname] = export_table
@@ -191,6 +203,10 @@ class KernelFiles(): · 3 unmodified lines
191203
192204 self.results[fname] = entries
193205
206+ source = doc.get_source()
207+ if source:
208+ self.source[fname] = source
209+
194210 def process_export_file(self, fname):
195211 """
196212 Parses ``EXPORT_SYMBOL*`` macros from a single Kernel source file.
@@ -220,7 +236,7 @@ class KernelFiles(): · 23 unmodified lines
220236 def __init__(self, verbose=False, out_style=None, xforms=None,
221237 werror=False, wreturn=False, wshort_desc=False,
222238 wcontents_before_sections=False,
223− logger=None):
239+ yaml_file=None, yaml_content=None, logger=None):
224240 """
225241 Initialize startup variables and parse all files.
226242 """
@@ -259,6 +275,11 @@ class KernelFiles(): · 32 unmodified lines
259275 # Override log warning, as we want to count errors
260276 self.config.warning = self.warning
261277
278+ if yaml_file:
279+ self.test_file = KDocTestFile(self.config, yaml_file, yaml_content)
280+ else:
281+ self.test_file = None
282+
262283 if xforms:
263284 self.xforms = xforms
264285 else:
@@ -273,6 +294,7 @@ class KernelFiles(): · 8 unmodified lines
273294
274295 self.errors = 0
275296 self.results = {}
297+ self.source = {}
276298
277299 self.files = set()
278300 self.export_files = set()
@@ -331,16 +353,29 @@ class KernelFiles(): · 52 unmodified lines
331353 for s in symbol:
332354 function_table.add(s)
333355
334− self.out_style.set_filter(export, internal, symbol, nosymbol,
335− function_table, enable_lineno,
336− no_doc_sections)
337−
338356 if fname not in self.results:
339357 self.config.log.warning("No kernel-doc for file %s", fname)
340358 continue
341359
342360 symbols = self.results[fname]
343361
362+ if self.test_file:
363+ self.test_file.set_filter(export, internal, symbol, nosymbol,
364+ function_table, enable_lineno,
365+ no_doc_sections)
366+
367+ self.test_file.output_symbols(fname, symbols,
368+ self.source.get(fname))
369+
370+ continue
371+
372+ self.out_style.set_filter(export, internal, symbol, nosymbol,
373+ function_table, enable_lineno,
374+ no_doc_sections)
375+
344376 msg = self.out_style.output_symbols(fname, symbols)
345377 if msg:
346378 yield fname, msg
379+
380+ if self.test_file:
381+ self.test_file.write()
tools/lib/python/kdoc/kdoc_parser.py+26−1
@@ -246,12 +246,13 @@ class KernelDoc: · 245 unmodified lines
246246 #: String to write when a parameter is not described.
247247 undescribed = "-- undescribed --"
248248
249− def __init__(self, config, fname, xforms):
249+ def __init__(self, config, fname, xforms, store_src=False):
250250 """Initialize internal variables"""
251251
252252 self.fname = fname
253253 self.config = config
254254 self.xforms = xforms
255+ self.store_src = store_src
255256
256257 tokenizer_set_log(self.config.log, f"{self.fname}: CMatch: ")
257258
@@ -264,6 +265,9 @@ class KernelDoc: · 6 unmodified lines
264265 # Place all potential outputs into an array
265266 self.entries = []
266267
268+ # When store_src is true, the kernel-doc source content is stored here
269+ self.source = None
270+
267271 #
268272 # We need Python 3.7 for its "dicts remember the insertion
269273 # order" guarantee
@@ -1592,6 +1596,15 @@ class KernelDoc: · 1322 unmodified lines
15921596 state.DOCBLOCK: process_docblock,
15931597 }
15941598
1599+ def get_source(self):
1600+ """
1601+ Return the file content of the lines handled by kernel-doc at the
1602+ latest parse_kdoc() run.
1603+
1604+ Returns none if KernelDoc() was not initialized with store_src,
1605+ """
1606+ return self.source
1607+
15951608 def parse_kdoc(self):
15961609 """
15971610 Open and process each line of a C source file.
@@ -1605,6 +1618,8 @@ class KernelDoc: · 7 unmodified lines
16051618 prev = ""
16061619 prev_ln = None
16071620 export_table = set()
1621+ self.source = []
1622+ self.state = state.NORMAL
16081623
16091624 try:
16101625 with open(self.fname, "r", encoding="utf8",
@@ -1631,6 +1646,8 @@ class KernelDoc: · 20 unmodified lines
16311646 ln, state.name[self.state],
16321647 line)
16331648
1649+ prev_state = self.state
1650+
16341651 # This is an optimization over the original script.
16351652 # There, when export_file was used for the same file,
16361653 # it was read twice. Here, we use the already-existing
@@ -1641,6 +1658,14 @@ class KernelDoc: · 4 unmodified lines
16411658 # Hand this line to the appropriate state handler
16421659 self.state_actions[self.state](self, ln, line)
16431660
1661+ if self.store_src and prev_state != self.state or self.state != state.NORMAL:
1662+ if self.state == state.NAME:
1663+ # A "/**" was detected. Add a new source element
1664+ self.source.append({"ln": ln, "data": line + "\n"})
1665+ else:
1666+ # Append to the existing one
1667+ self.source[-1]["data"] += line + "\n"
1668+
16441669 self.emit_unused_warnings()
16451670
16461671 except OSError: