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.
Unified · Split
| @@ -128,7 +128,7 @@ class KernelEntry: · 127 unmodified lines | ||
| 128 | 128 | def __init__(self, config, ln): |
| 129 | 129 | self.config = config |
| 130 | 130 | |
| 131 | − self.contents = "" | |
| 131 | + self._contents = [] | |
| 132 | 132 | self.function = "" |
| 133 | 133 | self.sectcheck = "" |
| 134 | 134 | self.struct_actual = "" |
| @@ -153,6 +153,15 @@ class KernelEntry: · 18 unmodified lines | ||
| 153 | 153 | self.brcount = 0 |
| 154 | 154 | 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 | 165 | # TODO: rename to emit_message after removal of kernel-doc.pl |
| 157 | 166 | def emit_msg(self, log_msg, warning=True): |
| 158 | 167 | """Emit a message""" |
| @@ -180,9 +189,14 @@ class KernelEntry: · 21 unmodified lines | ||
| 180 | 189 | """ |
| 181 | 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 | 198 | name = self.section |
| 185 | − contents = self.contents | |
| 199 | + contents = self.contents() | |
| 186 | 200 | |
| 187 | 201 | if type_param.match(name): |
| 188 | 202 | name = type_param.group(1) |
| @@ -206,7 +220,8 @@ class KernelEntry: · 17 unmodified lines | ||
| 206 | 220 | if name != SECTION_DEFAULT: |
| 207 | 221 | self.emit_msg(self.new_start_line, |
| 208 | 222 | 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 | |
| 210 | 225 | else: |
| 211 | 226 | self.sections[name] = contents |
| 212 | 227 | self.sectionlist.append(name) |
| @@ -217,7 +232,7 @@ class KernelEntry: · 4 unmodified lines | ||
| 217 | 232 | |
| 218 | 233 | if start_new: |
| 219 | 234 | self.section = SECTION_DEFAULT |
| 220 | − self.contents = "" | |
| 235 | + self._contents = [] | |
| 221 | 236 | |
| 222 | 237 | |
| 223 | 238 | class KernelDoc: |
| @@ -1334,16 +1349,11 @@ class KernelDoc: · 1110 unmodified lines | ||
| 1334 | 1349 | newcontents = doc_sect.group(2) |
| 1335 | 1350 | if not newcontents: |
| 1336 | 1351 | newcontents = "" |
| 1337 | − | |
| 1338 | − if self.entry.contents.strip("\n"): | |
| 1339 | − self.dump_section() | |
| 1340 | − | |
| 1352 | + self.dump_section() | |
| 1341 | 1353 | self.entry.begin_section(ln, newsection) |
| 1342 | 1354 | self.entry.leading_space = None |
| 1343 | 1355 | |
| 1344 | − self.entry.contents = newcontents.lstrip() | |
| 1345 | − if self.entry.contents: | |
| 1346 | − self.entry.contents += "\n" | |
| 1356 | + self.entry.add_text(newcontents.lstrip()) | |
| 1347 | 1357 | return True |
| 1348 | 1358 | return False |
| 1349 | 1359 | |
| @@ -1385,7 +1395,6 @@ class KernelDoc: · 35 unmodified lines | ||
| 1385 | 1395 | # |
| 1386 | 1396 | if cont == "": |
| 1387 | 1397 | self.state = state.BODY |
| 1388 | − self.entry.contents += "\n" # needed? | |
| 1389 | 1398 | # |
| 1390 | 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 | 1417 | if KernRe(r"\s*\*\s*$").match(line): |
| 1409 | 1418 | self.entry.begin_section(ln, dump = True) |
| 1410 | − self.entry.contents += '\n' | |
| 1411 | 1419 | self.state = state.BODY |
| 1412 | 1420 | return |
| 1413 | 1421 | # |
| @@ -1444,7 +1452,7 @@ class KernelDoc: · 30 unmodified lines | ||
| 1444 | 1452 | # |
| 1445 | 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 | 1456 | else: |
| 1449 | 1457 | # Unknown line, ignore |
| 1450 | 1458 | self.emit_msg(ln, f"bad line: {line}") |
| @@ -1458,7 +1466,7 @@ class KernelDoc: · 7 unmodified lines | ||
| 1458 | 1466 | |
| 1459 | 1467 | if doc_content.search(line): |
| 1460 | 1468 | cont = doc_content.group(1) |
| 1461 | − self.entry.contents += cont + "\n" | |
| 1469 | + self.entry.add_text(cont) | |
| 1462 | 1470 | else: |
| 1463 | 1471 | # Unknown line, ignore |
| 1464 | 1472 | self.emit_msg(ln, f"bad line: {line}") |
| @@ -1470,27 +1478,20 @@ class KernelDoc: · 5 unmodified lines | ||
| 1470 | 1478 | doc_inline_sect.search(line): |
| 1471 | 1479 | self.entry.begin_section(ln, doc_inline_sect.group(1)) |
| 1472 | 1480 | |
| 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()) | |
| 1477 | 1482 | self.inline_doc_state = state.INLINE_TEXT |
| 1478 | 1483 | # Documentation block end */ |
| 1479 | 1484 | return |
| 1480 | 1485 | |
| 1481 | 1486 | if doc_inline_end.search(line): |
| 1482 | − if self.entry.contents not in ["", "\n"]: | |
| 1483 | − self.dump_section() | |
| 1484 | − | |
| 1487 | + self.dump_section() | |
| 1485 | 1488 | self.state = state.PROTO |
| 1486 | 1489 | self.inline_doc_state = state.INLINE_NA |
| 1487 | 1490 | return |
| 1488 | 1491 | |
| 1489 | 1492 | if doc_content.search(line): |
| 1490 | 1493 | 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)) | |
| 1494 | 1495 | |
| 1495 | 1496 | elif self.inline_doc_state == state.INLINE_NAME: |
| 1496 | 1497 | self.emit_msg(ln, |
| @@ -1668,11 +1669,8 @@ class KernelDoc: · 171 unmodified lines | ||
| 1668 | 1669 | |
| 1669 | 1670 | if doc_inline_oneline.search(line): |
| 1670 | 1671 | 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() | |
| 1676 | 1674 | |
| 1677 | 1675 | elif doc_inline_start.search(line): |
| 1678 | 1676 | self.state = state.INLINE |
| @@ -1696,7 +1694,7 @@ class KernelDoc: · 17 unmodified lines | ||
| 1696 | 1694 | self.reset_state(ln) |
| 1697 | 1695 | |
| 1698 | 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 | 1699 | def parse_export(self): |
| 1702 | 1700 | """ |