History

docs: kdoc: Move content handling into KernelEntry

Rather than having other code mucking around with this bit of internal state, encapsulate it internally. Accumulate the description as a list of strings, joining them at the end, which is a more efficient way of building the text.

Jonathan Corbet committed 1 year ago

Commit 1e2a79c1 parent 4eaf6121 file · +30 −32Browse files

Unified · Split

scripts/lib/kdoc/kdoc_parser.py+30−32
@@ -128,7 +128,7 @@ class KernelEntry: · 127 unmodified lines
128128 def __init__(self, config, ln):
129129 self.config = config
130130
131− self.contents = ""
131+ self._contents = []
132132 self.function = ""
133133 self.sectcheck = ""
134134 self.struct_actual = ""
@@ -153,6 +153,15 @@ class KernelEntry: · 18 unmodified lines
153153 self.brcount = 0
154154 self.declaration_start_line = ln + 1
155155
156+ #
157+ # Management of section contents
158+ #
159+ def add_text(self, text):
160+ self._contents.append(text)
161+
162+ def contents(self):
163+ return '\n'.join(self._contents) + '\n'
164+
156165 # TODO: rename to emit_message after removal of kernel-doc.pl
157166 def emit_msg(self, log_msg, warning=True):
158167 """Emit a message"""
@@ -180,9 +189,14 @@ class KernelEntry: · 21 unmodified lines
180189 """
181190 Dumps section contents to arrays/hashes intended for that purpose.
182191 """
183−
192+ #
193+ # If we have accumulated no contents in the default ("description")
194+ # section, don't bother.
195+ #
196+ if self.section == SECTION_DEFAULT and not self._contents:
197+ return
184198 name = self.section
185− contents = self.contents
199+ contents = self.contents()
186200
187201 if type_param.match(name):
188202 name = type_param.group(1)
@@ -206,7 +220,8 @@ class KernelEntry: · 17 unmodified lines
206220 if name != SECTION_DEFAULT:
207221 self.emit_msg(self.new_start_line,
208222 f"duplicate section name '{name}'\n")
209− self.sections[name] += contents
223+ # Treat as a new paragraph - add a blank line
224+ self.sections[name] += '\n' + contents
210225 else:
211226 self.sections[name] = contents
212227 self.sectionlist.append(name)
@@ -217,7 +232,7 @@ class KernelEntry: · 4 unmodified lines
217232
218233 if start_new:
219234 self.section = SECTION_DEFAULT
220− self.contents = ""
235+ self._contents = []
221236
222237
223238 class KernelDoc:
@@ -1334,16 +1349,11 @@ class KernelDoc: · 1110 unmodified lines
13341349 newcontents = doc_sect.group(2)
13351350 if not newcontents:
13361351 newcontents = ""
1337−
1338− if self.entry.contents.strip("\n"):
1339− self.dump_section()
1340−
1352+ self.dump_section()
13411353 self.entry.begin_section(ln, newsection)
13421354 self.entry.leading_space = None
13431355
1344− self.entry.contents = newcontents.lstrip()
1345− if self.entry.contents:
1346− self.entry.contents += "\n"
1356+ self.entry.add_text(newcontents.lstrip())
13471357 return True
13481358 return False
13491359
@@ -1385,7 +1395,6 @@ class KernelDoc: · 35 unmodified lines
13851395 #
13861396 if cont == "":
13871397 self.state = state.BODY
1388− self.entry.contents += "\n" # needed?
13891398 #
13901399 # Otherwise we have more of the declaration section to soak up.
13911400 #
@@ -1407,7 +1416,6 @@ class KernelDoc: · 15 unmodified lines
14071416 #
14081417 if KernRe(r"\s*\*\s*$").match(line):
14091418 self.entry.begin_section(ln, dump = True)
1410− self.entry.contents += '\n'
14111419 self.state = state.BODY
14121420 return
14131421 #
@@ -1444,7 +1452,7 @@ class KernelDoc: · 30 unmodified lines
14441452 #
14451453 # Add the trimmed result to the section and we're done.
14461454 #
1447− self.entry.contents += cont[self.entry.leading_space:] + '\n'
1455+ self.entry.add_text(cont[self.entry.leading_space:])
14481456 else:
14491457 # Unknown line, ignore
14501458 self.emit_msg(ln, f"bad line: {line}")
@@ -1458,7 +1466,7 @@ class KernelDoc: · 7 unmodified lines
14581466
14591467 if doc_content.search(line):
14601468 cont = doc_content.group(1)
1461− self.entry.contents += cont + "\n"
1469+ self.entry.add_text(cont)
14621470 else:
14631471 # Unknown line, ignore
14641472 self.emit_msg(ln, f"bad line: {line}")
@@ -1470,27 +1478,20 @@ class KernelDoc: · 5 unmodified lines
14701478 doc_inline_sect.search(line):
14711479 self.entry.begin_section(ln, doc_inline_sect.group(1))
14721480
1473− self.entry.contents = doc_inline_sect.group(2).lstrip()
1474− if self.entry.contents != "":
1475− self.entry.contents += "\n"
1476−
1481+ self.entry.add_text(doc_inline_sect.group(2).lstrip())
14771482 self.inline_doc_state = state.INLINE_TEXT
14781483 # Documentation block end */
14791484 return
14801485
14811486 if doc_inline_end.search(line):
1482− if self.entry.contents not in ["", "\n"]:
1483− self.dump_section()
1484−
1487+ self.dump_section()
14851488 self.state = state.PROTO
14861489 self.inline_doc_state = state.INLINE_NA
14871490 return
14881491
14891492 if doc_content.search(line):
14901493 if self.inline_doc_state == state.INLINE_TEXT:
1491− self.entry.contents += doc_content.group(1) + "\n"
1492− if not self.entry.contents.strip(" ").rstrip("\n"):
1493− self.entry.contents = ""
1494+ self.entry.add_text(doc_content.group(1))
14941495
14951496 elif self.inline_doc_state == state.INLINE_NAME:
14961497 self.emit_msg(ln,
@@ -1668,11 +1669,8 @@ class KernelDoc: · 171 unmodified lines
16681669
16691670 if doc_inline_oneline.search(line):
16701671 self.entry.begin_section(ln, doc_inline_oneline.group(1))
1671− self.entry.contents = doc_inline_oneline.group(2)
1672−
1673− if self.entry.contents != "":
1674− self.entry.contents += "\n"
1675− self.dump_section(start_new=False)
1672+ self.entry.add_text(doc_inline_oneline.group(2))
1673+ self.dump_section()
16761674
16771675 elif doc_inline_start.search(line):
16781676 self.state = state.INLINE
@@ -1696,7 +1694,7 @@ class KernelDoc: · 17 unmodified lines
16961694 self.reset_state(ln)
16971695
16981696 elif doc_content.search(line):
1699− self.entry.contents += doc_content.group(1) + "\n"
1697+ self.entry.add_text(doc_content.group(1))
17001698
17011699 def parse_export(self):
17021700 """