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

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/lib/kdoc/kdoc_parser.py+30−32
@@ -128,7 +128,7 @@ class KernelEntry: · 127 unmodified lines
128 def __init__(self, config, ln):128 def __init__(self, config, ln):
129 self.config = config129 self.config = config
130 130
131 self.contents = ""131 self._contents = []
132 self.function = ""132 self.function = ""
133 self.sectcheck = ""133 self.sectcheck = ""
134 self.struct_actual = ""134 self.struct_actual = ""
@@ -153,6 +153,15 @@ class KernelEntry: · 18 unmodified lines
153 self.brcount = 0153 self.brcount = 0
154 self.declaration_start_line = ln + 1154 self.declaration_start_line = ln + 1
155 155
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
156 # TODO: rename to emit_message after removal of kernel-doc.pl165 # TODO: rename to emit_message after removal of kernel-doc.pl
157 def emit_msg(self, log_msg, warning=True):166 def emit_msg(self, log_msg, warning=True):
158 """Emit a message"""167 """Emit a message"""
@@ -180,9 +189,14 @@ class KernelEntry: · 21 unmodified lines
180 """189 """
181 Dumps section contents to arrays/hashes intended for that purpose.190 Dumps section contents to arrays/hashes intended for that purpose.
182 """191 """
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
184 name = self.section198 name = self.section
185 contents = self.contents199 contents = self.contents()
186 200
187 if type_param.match(name):201 if type_param.match(name):
188 name = type_param.group(1)202 name = type_param.group(1)
@@ -206,7 +220,8 @@ class KernelEntry: · 17 unmodified lines
206 if name != SECTION_DEFAULT:220 if name != SECTION_DEFAULT:
207 self.emit_msg(self.new_start_line,221 self.emit_msg(self.new_start_line,
208 f"duplicate section name '{name}'\n")222 f"duplicate section name '{name}'\n")
209 self.sections[name] += contents223 # Treat as a new paragraph - add a blank line
224 self.sections[name] += '\n' + contents
210 else:225 else:
211 self.sections[name] = contents226 self.sections[name] = contents
212 self.sectionlist.append(name)227 self.sectionlist.append(name)
@@ -217,7 +232,7 @@ class KernelEntry: · 4 unmodified lines
217 232
218 if start_new:233 if start_new:
219 self.section = SECTION_DEFAULT234 self.section = SECTION_DEFAULT
220 self.contents = ""235 self._contents = []
221 236
222 237
223class KernelDoc:238class KernelDoc:
@@ -1334,16 +1349,11 @@ class KernelDoc: · 1110 unmodified lines
1334 newcontents = doc_sect.group(2)1349 newcontents = doc_sect.group(2)
1335 if not newcontents:1350 if not newcontents:
1336 newcontents = ""1351 newcontents = ""
1337 1352 self.dump_section()
1338 if self.entry.contents.strip("\n"):
1339 self.dump_section()
1340
1341 self.entry.begin_section(ln, newsection)1353 self.entry.begin_section(ln, newsection)
1342 self.entry.leading_space = None1354 self.entry.leading_space = None
1343 1355
1344 self.entry.contents = newcontents.lstrip()1356 self.entry.add_text(newcontents.lstrip())
1345 if self.entry.contents:
1346 self.entry.contents += "\n"
1347 return True1357 return True
1348 return False1358 return False
1349 1359
@@ -1385,7 +1395,6 @@ class KernelDoc: · 35 unmodified lines
1385 #1395 #
1386 if cont == "":1396 if cont == "":
1387 self.state = state.BODY1397 self.state = state.BODY
1388 self.entry.contents += "\n" # needed?
1389 #1398 #
1390 # Otherwise we have more of the declaration section to soak up.1399 # Otherwise we have more of the declaration section to soak up.
1391 #1400 #
@@ -1407,7 +1416,6 @@ class KernelDoc: · 15 unmodified lines
1407 #1416 #
1408 if KernRe(r"\s*\*\s*$").match(line):1417 if KernRe(r"\s*\*\s*$").match(line):
1409 self.entry.begin_section(ln, dump = True)1418 self.entry.begin_section(ln, dump = True)
1410 self.entry.contents += '\n'
1411 self.state = state.BODY1419 self.state = state.BODY
1412 return1420 return
1413 #1421 #
@@ -1444,7 +1452,7 @@ class KernelDoc: · 30 unmodified lines
1444 #1452 #
1445 # Add the trimmed result to the section and we're done.1453 # Add the trimmed result to the section and we're done.
1446 #1454 #
1447 self.entry.contents += cont[self.entry.leading_space:] + '\n'1455 self.entry.add_text(cont[self.entry.leading_space:])
1448 else:1456 else:
1449 # Unknown line, ignore1457 # Unknown line, ignore
1450 self.emit_msg(ln, f"bad line: {line}")1458 self.emit_msg(ln, f"bad line: {line}")
@@ -1458,7 +1466,7 @@ class KernelDoc: · 7 unmodified lines
1458 1466
1459 if doc_content.search(line):1467 if doc_content.search(line):
1460 cont = doc_content.group(1)1468 cont = doc_content.group(1)
1461 self.entry.contents += cont + "\n"1469 self.entry.add_text(cont)
1462 else:1470 else:
1463 # Unknown line, ignore1471 # Unknown line, ignore
1464 self.emit_msg(ln, f"bad line: {line}")1472 self.emit_msg(ln, f"bad line: {line}")
@@ -1470,27 +1478,20 @@ class KernelDoc: · 5 unmodified lines
1470 doc_inline_sect.search(line):1478 doc_inline_sect.search(line):
1471 self.entry.begin_section(ln, doc_inline_sect.group(1))1479 self.entry.begin_section(ln, doc_inline_sect.group(1))
1472 1480
1473 self.entry.contents = doc_inline_sect.group(2).lstrip()1481 self.entry.add_text(doc_inline_sect.group(2).lstrip())
1474 if self.entry.contents != "":
1475 self.entry.contents += "\n"
1476
1477 self.inline_doc_state = state.INLINE_TEXT1482 self.inline_doc_state = state.INLINE_TEXT
1478 # Documentation block end */1483 # Documentation block end */
1479 return1484 return
1480 1485
1481 if doc_inline_end.search(line):1486 if doc_inline_end.search(line):
1482 if self.entry.contents not in ["", "\n"]:1487 self.dump_section()
1483 self.dump_section()
1484
1485 self.state = state.PROTO1488 self.state = state.PROTO
1486 self.inline_doc_state = state.INLINE_NA1489 self.inline_doc_state = state.INLINE_NA
1487 return1490 return
1488 1491
1489 if doc_content.search(line):1492 if doc_content.search(line):
1490 if self.inline_doc_state == state.INLINE_TEXT:1493 if self.inline_doc_state == state.INLINE_TEXT:
1491 self.entry.contents += doc_content.group(1) + "\n"1494 self.entry.add_text(doc_content.group(1))
1492 if not self.entry.contents.strip(" ").rstrip("\n"):
1493 self.entry.contents = ""
1494 1495
1495 elif self.inline_doc_state == state.INLINE_NAME:1496 elif self.inline_doc_state == state.INLINE_NAME:
1496 self.emit_msg(ln,1497 self.emit_msg(ln,
@@ -1668,11 +1669,8 @@ class KernelDoc: · 171 unmodified lines
1668 1669
1669 if doc_inline_oneline.search(line):1670 if doc_inline_oneline.search(line):
1670 self.entry.begin_section(ln, doc_inline_oneline.group(1))1671 self.entry.begin_section(ln, doc_inline_oneline.group(1))
1671 self.entry.contents = doc_inline_oneline.group(2)1672 self.entry.add_text(doc_inline_oneline.group(2))
1672 1673 self.dump_section()
1673 if self.entry.contents != "":
1674 self.entry.contents += "\n"
1675 self.dump_section(start_new=False)
1676 1674
1677 elif doc_inline_start.search(line):1675 elif doc_inline_start.search(line):
1678 self.state = state.INLINE1676 self.state = state.INLINE
@@ -1696,7 +1694,7 @@ class KernelDoc: · 17 unmodified lines
1696 self.reset_state(ln)1694 self.reset_state(ln)
1697 1695
1698 elif doc_content.search(line):1696 elif doc_content.search(line):
1699 self.entry.contents += doc_content.group(1) + "\n"1697 self.entry.add_text(doc_content.group(1))
1700 1698
1701 def parse_export(self):1699 def parse_export(self):
1702 """1700 """