Skip to content

Commit e68c84b

Browse files
mchehabJonathan Corbet
authored andcommitted
docs: kdoc: parse_data_structs: Improve docstrings and comments
In preparation to document kernel-doc module, improve its documentation. Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org> Signed-off-by: Jonathan Corbet <corbet@lwn.net> Message-ID: <76ead85b4c13a8038180a792e270c3691d26cd25.1768838938.git.mchehab+huawei@kernel.org>
1 parent b0b8891 commit e68c84b

1 file changed

Lines changed: 39 additions & 23 deletions

File tree

tools/lib/python/kdoc/parse_data_structs.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
It accepts an optional file to change the default symbol reference or to
1010
suppress symbols from the output.
1111
12-
It is capable of identifying defines, functions, structs, typedefs,
13-
enums and enum symbols and create cross-references for all of them.
12+
It is capable of identifying ``define``, function, ``struct``, ``typedef``,
13+
``enum`` and ``enum`` symbols and create cross-references for all of them.
1414
It is also capable of distinguish #define used for specifying a Linux
1515
ioctl.
1616
17-
The optional rules file contains a set of rules like:
17+
The optional rules file contains a set of rules like::
1818
1919
ignore ioctl VIDIOC_ENUM_FMT
2020
replace ioctl VIDIOC_DQBUF vidioc_qbuf
@@ -34,22 +34,22 @@ class ParseDataStructs:
3434
It is meant to allow having a more comprehensive documentation, where
3535
uAPI headers will create cross-reference links to the code.
3636
37-
It is capable of identifying defines, functions, structs, typedefs,
38-
enums and enum symbols and create cross-references for all of them.
37+
It is capable of identifying ``define``, function, ``struct``, ``typedef``,
38+
``enum`` and ``enum`` symbols and create cross-references for all of them.
3939
It is also capable of distinguish #define used for specifying a Linux
4040
ioctl.
4141
4242
By default, it create rules for all symbols and defines, but it also
4343
allows parsing an exception file. Such file contains a set of rules
4444
using the syntax below:
4545
46-
1. Ignore rules:
46+
1. Ignore rules::
4747
4848
ignore <type> <symbol>`
4949
5050
Removes the symbol from reference generation.
5151
52-
2. Replace rules:
52+
2. Replace rules::
5353
5454
replace <type> <old_symbol> <new_reference>
5555
@@ -58,22 +58,22 @@ class ParseDataStructs:
5858
- A simple symbol name;
5959
- A full Sphinx reference.
6060
61-
3. Namespace rules
61+
3. Namespace rules::
6262
6363
namespace <namespace>
6464
6565
Sets C namespace to be used during cross-reference generation. Can
6666
be overridden by replace rules.
6767
68-
On ignore and replace rules, <type> can be:
69-
- ioctl: for defines that end with _IO*, e.g. ioctl definitions
70-
- define: for other defines
71-
- symbol: for symbols defined within enums;
72-
- typedef: for typedefs;
73-
- enum: for the name of a non-anonymous enum;
74-
- struct: for structs.
68+
On ignore and replace rules, ``<type>`` can be:
69+
- ``ioctl``: for defines that end with ``_IO*``, e.g. ioctl definitions
70+
- ``define``: for other defines
71+
- ``symbol``: for symbols defined within enums;
72+
- ``typedef``: for typedefs;
73+
- ``enum``: for the name of a non-anonymous enum;
74+
- ``struct``: for structs.
7575
76-
Examples:
76+
Examples::
7777
7878
ignore define __LINUX_MEDIA_H
7979
ignore ioctl VIDIOC_ENUM_FMT
@@ -83,25 +83,29 @@ class ParseDataStructs:
8383
namespace MC
8484
"""
8585

86-
# Parser regexes with multiple ways to capture enums and structs
86+
#: Parser regex with multiple ways to capture enums.
8787
RE_ENUMS = [
8888
re.compile(r"^\s*enum\s+([\w_]+)\s*\{"),
8989
re.compile(r"^\s*enum\s+([\w_]+)\s*$"),
9090
re.compile(r"^\s*typedef\s*enum\s+([\w_]+)\s*\{"),
9191
re.compile(r"^\s*typedef\s*enum\s+([\w_]+)\s*$"),
9292
]
93+
94+
#: Parser regex with multiple ways to capture structs.
9395
RE_STRUCTS = [
9496
re.compile(r"^\s*struct\s+([_\w][\w\d_]+)\s*\{"),
9597
re.compile(r"^\s*struct\s+([_\w][\w\d_]+)$"),
9698
re.compile(r"^\s*typedef\s*struct\s+([_\w][\w\d_]+)\s*\{"),
9799
re.compile(r"^\s*typedef\s*struct\s+([_\w][\w\d_]+)$"),
98100
]
99101

100-
# FIXME: the original code was written a long time before Sphinx C
102+
# NOTE: the original code was written a long time before Sphinx C
101103
# domain to have multiple namespaces. To avoid to much turn at the
102104
# existing hyperlinks, the code kept using "c:type" instead of the
103105
# right types. To change that, we need to change the types not only
104106
# here, but also at the uAPI media documentation.
107+
108+
#: Dictionary containing C type identifiers to be transformed.
105109
DEF_SYMBOL_TYPES = {
106110
"ioctl": {
107111
"prefix": "\\ ",
@@ -158,6 +162,10 @@ def __init__(self, debug: bool = False):
158162
self.symbols[symbol_type] = {}
159163

160164
def read_exceptions(self, fname: str):
165+
"""
166+
Read an optional exceptions file, used to override defaults.
167+
"""
168+
161169
if not fname:
162170
return
163171

@@ -242,9 +250,9 @@ def apply_exceptions(self):
242250
def store_type(self, ln, symbol_type: str, symbol: str,
243251
ref_name: str = None, replace_underscores: bool = True):
244252
"""
245-
Stores a new symbol at self.symbols under symbol_type.
253+
Store a new symbol at self.symbols under symbol_type.
246254
247-
By default, underscores are replaced by "-"
255+
By default, underscores are replaced by ``-``.
248256
"""
249257
defs = self.DEF_SYMBOL_TYPES[symbol_type]
250258

@@ -276,12 +284,16 @@ def store_type(self, ln, symbol_type: str, symbol: str,
276284
self.symbols[symbol_type][symbol] = (f"{prefix}{ref_link}{suffix}", ln)
277285

278286
def store_line(self, line):
279-
"""Stores a line at self.data, properly indented"""
287+
"""
288+
Store a line at self.data, properly indented.
289+
"""
280290
line = " " + line.expandtabs()
281291
self.data += line.rstrip(" ")
282292

283293
def parse_file(self, file_in: str, exceptions: str = None):
284-
"""Reads a C source file and get identifiers"""
294+
"""
295+
Read a C source file and get identifiers.
296+
"""
285297
self.data = ""
286298
is_enum = False
287299
is_comment = False
@@ -433,7 +445,7 @@ def gen_output(self):
433445

434446
def gen_toc(self):
435447
"""
436-
Create a list of symbols to be part of a TOC contents table
448+
Create a list of symbols to be part of a TOC contents table.
437449
"""
438450
text = []
439451

@@ -464,6 +476,10 @@ def gen_toc(self):
464476
return "\n".join(text)
465477

466478
def write_output(self, file_in: str, file_out: str, toc: bool):
479+
"""
480+
Write a ReST output file.
481+
"""
482+
467483
title = os.path.basename(file_in)
468484

469485
if toc:

0 commit comments

Comments
 (0)