Instead of peeking inside the section list and always pulling the -1th
item, keep track specifically of the last added section. This is
because as we allow generated documentation to be inserted into
arbitrary positions, the last item in the list will not always
actually be the active section under consideration.

(Also, the simple list representation is going away, and accessing the
-1th item won't actually work anymore.)

Signed-off-by: John Snow <[email protected]>
---
 scripts/qapi/parser.py | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py
index bdd0f1ec3ff..894b8979b9e 100644
--- a/scripts/qapi/parser.py
+++ b/scripts/qapi/parser.py
@@ -760,6 +760,10 @@ def __init__(self, info: QAPISourceInfo, symbol: 
Optional[str] = None):
         self.symbol: Optional[str] = symbol
         # the sections in textual order
         self._sections: List[QAPIDoc.Section] = []
+
+        # The most recently appended section, on which append_line operates
+        self._active_section: Optional[QAPIDoc.Section] = None
+
         # dicts mapping parameter/feature names to their description
         self._args: Dict[str, QAPIDoc.ArgSection] = {}
         self._features: Dict[str, QAPIDoc.ArgSection] = {}
@@ -799,6 +803,7 @@ def _append(self, section: 'QAPIDoc.Section') -> None:
                     section.info, "duplicated '%s' section" % section.kind)
             self.since = section
         self._sections.append(section)
+        self._active_section = section
 
     def __iter__(self) -> Iterator['QAPIDoc.Section']:
         return iter(self._sections)
@@ -820,13 +825,12 @@ def end(self) -> None:
     def ensure_untagged_section(self, info: QAPISourceInfo) -> None:
         kind = QAPIDoc.Kind.DETAILS
 
-        if self._sections and self._sections[-1].kind == kind:
+        if self._active_section and self._active_section.kind == kind:
             # extend current section
-            section = self._sections[-1]
-            if not section.text:
+            if not self._active_section.text:
                 # Section is empty so far; update info to start *here*.
-                section.info = info
-            section.text += '\n'
+                self._active_section.info = info
+            self._active_section.text += '\n'
             return
 
         # start new section
@@ -863,7 +867,8 @@ def new_feature(self, info: QAPISourceInfo, name: str) -> 
None:
         self._new_description(info, name, QAPIDoc.Kind.FEATURE, self._features)
 
     def append_line(self, line: str) -> None:
-        self._sections[-1].append_line(line)
+        assert self._active_section is not None
+        self._active_section.append_line(line)
 
     def _insert_near_kind(
         self,
-- 
2.55.0


Reply via email to