[gcc r17-2748] libstdc++: Fix num_children of the map pretty-printers

Jonathan Wakely via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <20260728100116.7958A4BB24E9__24817.7647286624$1785232888$gmane$org@sourceware.org>
https://gcc.gnu.org/g:b80a4347fc63b1a6ed6e2b3ea26c5948f08ba3cd

commit r17-2748-gb80a4347fc63b1a6ed6e2b3ea26c5948f08ba3cd
Author: David Faure <[email protected]>
Date:   Mon Jul 27 20:02:29 2026 +0200

    libstdc++: Fix num_children of the map pretty-printers
    
    StdMapPrinter and Tr1UnorderedMapPrinter yield a separate child for the
    key and for the value of every element, as their 'map' display hint
    requires, but their num_children methods return the number of elements,
    i.e. half of what children() produces.
    
    GDB counts the children itself where it does not use num_children, so
    the two disagree about the same map. For a std::map with 2 elements:
    
        (gdb) interpreter-exec mi "-enable-pretty-printing"
        (gdb) interpreter-exec mi "-var-create v * m"
        (gdb) interpreter-exec mi "-var-list-children v"
        ^done,numchild="4",displayhint="map",children=[...]
    
    while num_children() returns 2.  The DAP code in gdb treats the two as
    interchangeable: gdb/python/lib/gdb/dap/varref.py calls num_children()
    when the printer has one, and falls back to len(list(children())) when
    it does not, so a DAP client sees half the children of a std::map,
    std::multimap or std::unordered_map.
    
    StdSetPrinter and the sequence container printers yield one child per
    element, so they are correct as they are.
    
    libstdc++-v3/ChangeLog
    
            * python/libstdcxx/v6/printers.py (StdMapPrinter.num_children):
            Count two children per element.
            (Tr1UnorderedMapPrinter.num_children): Likewise.
    
    Signed-off-by: David Faure <[email protected]>
    Reviewed-by: Tom Tromey <[email protected]>

Diff:
---
 libstdc++-v3/python/libstdcxx/v6/printers.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py
index eff4b72c1d8a..1bba377513ca 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -909,7 +909,8 @@ class StdMapPrinter(printer_base):
         return self._iter(RbtreeIterator(self._val), node)
 
     def num_children(self):
-        return len(RbtreeIterator(self._val))
+        # Each element produces two children, the key and the value.
+        return 2 * len(RbtreeIterator(self._val))
 
     def display_hint(self):
         return 'map'
@@ -1301,7 +1302,8 @@ class Tr1UnorderedMapPrinter(printer_base):
         return izip(counter, data)
 
     def num_children(self):
-        return int(self._hashtable()['_M_element_count'])
+        # Each element produces two children, the key and the value.
+        return 2 * int(self._hashtable()['_M_element_count'])
 
     def display_hint(self):
         return 'map'
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.