On Mon, 27 Jul 2026 at 19:02, <[email protected]> wrote:
>
> From: David Faure <[email protected]>
>
> 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]>

Thanks, pushed to trunk and I'll backport it to gcc-16.

> ---
>  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 eff4b72..1bba377 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'
> --
> 2.55.0
>

Reply via email to