1 # Copyright (C) 2019-2024 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.
23 def _iterator1(pointer
, len):
25 map = pointer
.dereference()
26 yield ("", map["name"])
27 yield ("", map.dereference())
32 def _iterator2(pointer1
, pointer2
, len):
34 yield ("", pointer1
.dereference())
35 yield ("", pointer2
.dereference())
42 def __init__(self
, val
):
46 if self
.val
["show_header"] == 0:
52 return _iterator2(self
.val
["keys"], self
.val
["values"], self
.val
["length"])
54 def display_hint(self
):
58 class pp_map_map(object):
59 def __init__(self
, val
):
63 if self
.val
["show_header"] == 0:
69 return _iterator1(self
.val
["values"], self
.val
["length"])
71 def display_hint(self
):
75 def lookup_function(val
):
76 "Look-up and return a pretty-printer that can print val."
81 # If it points to a reference, get the reference.
82 if type.code
== gdb
.TYPE_CODE_REF
:
85 # Get the unqualified type, stripped of typedefs.
86 type = type.unqualified().strip_typedefs()
94 # Iterate over local dictionary of types to determine
95 # if a printer is registered for that type. Return an
96 # instantiation of the printer if found.
97 for function
in pretty_printers_dict
:
98 if function
.match(typename
):
99 return pretty_printers_dict
[function
](val
)
101 # Cannot find a pretty printer. Return None.
105 # Lookup a printer for VAL in the typedefs dict.
106 def lookup_typedefs_function(val
):
107 "Look-up and return a pretty-printer that can print val (typedefs)."
112 if type is None or type.name
is None or type.code
!= gdb
.TYPE_CODE_TYPEDEF
:
115 # Iterate over local dictionary of typedef types to determine if a
116 # printer is registered for that type. Return an instantiation of
117 # the printer if found.
118 for function
in typedefs_pretty_printers_dict
:
119 if function
.match(type.name
):
120 return typedefs_pretty_printers_dict
[function
](val
)
122 # Cannot find a pretty printer.
126 def register_pretty_printers():
127 pretty_printers_dict
[re
.compile("^struct map_t$")] = pp_map
128 pretty_printers_dict
[re
.compile("^map_t$")] = pp_map
129 pretty_printers_dict
[re
.compile("^struct map_map_t$")] = pp_map_map
130 pretty_printers_dict
[re
.compile("^map_map_t$")] = pp_map_map
133 # Dict for struct types with typedefs fully stripped.
134 pretty_printers_dict
= {}
135 # Dict for typedef types.
136 typedefs_pretty_printers_dict
= {}
138 register_pretty_printers()
139 gdb
.pretty_printers
.append(lookup_function
)
140 gdb
.pretty_printers
.append(lookup_typedefs_function
)