1 # Copyright (C) 2019-2025 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # This file is part of the GDB testsuite. It tests GDB's printing of
17 # nested map like structures.
24 def _iterator1(pointer
, len):
26 map = pointer
.dereference()
27 yield ("", map["name"])
28 yield ("", map.dereference())
33 def _iterator2(pointer1
, pointer2
, len):
35 yield ("", pointer1
.dereference())
36 yield ("", pointer2
.dereference())
43 def __init__(self
, val
):
47 if self
.val
["show_header"] == 0:
53 return _iterator2(self
.val
["keys"], self
.val
["values"], self
.val
["length"])
55 def display_hint(self
):
59 class pp_map_map(object):
60 def __init__(self
, val
):
64 if self
.val
["show_header"] == 0:
70 return _iterator1(self
.val
["values"], self
.val
["length"])
72 def display_hint(self
):
76 def lookup_function(val
):
77 "Look-up and return a pretty-printer that can print val."
82 # If it points to a reference, get the reference.
83 if type.code
== gdb
.TYPE_CODE_REF
:
86 # Get the unqualified type, stripped of typedefs.
87 type = type.unqualified().strip_typedefs()
95 # Iterate over local dictionary of types to determine
96 # if a printer is registered for that type. Return an
97 # instantiation of the printer if found.
98 for function
in pretty_printers_dict
:
99 if function
.match(typename
):
100 return pretty_printers_dict
[function
](val
)
102 # Cannot find a pretty printer. Return None.
106 # Lookup a printer for VAL in the typedefs dict.
107 def lookup_typedefs_function(val
):
108 "Look-up and return a pretty-printer that can print val (typedefs)."
113 if type is None or type.name
is None or type.code
!= gdb
.TYPE_CODE_TYPEDEF
:
116 # Iterate over local dictionary of typedef types to determine if a
117 # printer is registered for that type. Return an instantiation of
118 # the printer if found.
119 for function
in typedefs_pretty_printers_dict
:
120 if function
.match(type.name
):
121 return typedefs_pretty_printers_dict
[function
](val
)
123 # Cannot find a pretty printer.
127 def register_pretty_printers():
128 pretty_printers_dict
[re
.compile("^struct map_t$")] = pp_map
129 pretty_printers_dict
[re
.compile("^map_t$")] = pp_map
130 pretty_printers_dict
[re
.compile("^struct map_map_t$")] = pp_map_map
131 pretty_printers_dict
[re
.compile("^map_map_t$")] = pp_map_map
134 # Dict for struct types with typedefs fully stripped.
135 pretty_printers_dict
= {}
136 # Dict for typedef types.
137 typedefs_pretty_printers_dict
= {}
139 register_pretty_printers()
140 gdb
.pretty_printers
.append(lookup_function
)
141 gdb
.pretty_printers
.append(lookup_typedefs_function
)