1 # Copyright (C) 2019-2020 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.
22 def _iterator1 (pointer, len):
24 map = pointer.dereference()
25 yield ('', map['name'])
26 yield ('', map.dereference())
30 def _iterator2 (pointer1, pointer2, len):
32 yield ("", pointer1.dereference())
33 yield ("", pointer2.dereference())
38 class pp_map (object):
39 def __init__(self, val):
43 if (self.val['show_header'] == 0):
49 return _iterator2(self.val['keys'],
53 def display_hint (self):
56 class pp_map_map (object):
57 def __init__(self, val):
61 if (self.val['show_header'] == 0):
67 return _iterator1(self.val['values'],
70 def display_hint (self):
73 def lookup_function (val):
74 "Look-up and return a pretty-printer that can print val."
79 # If it points to a reference, get the reference.
80 if type.code == gdb.TYPE_CODE_REF:
83 # Get the unqualified type, stripped of typedefs.
84 type = type.unqualified ().strip_typedefs ()
92 # Iterate over local dictionary of types to determine
93 # if a printer is registered for that type. Return an
94 # instantiation of the printer if found.
95 for function in pretty_printers_dict:
96 if function.match (typename):
97 return pretty_printers_dict[function] (val)
99 # Cannot find a pretty printer. Return None.
102 # Lookup a printer for VAL in the typedefs dict.
103 def lookup_typedefs_function (val):
104 "Look-up and return a pretty-printer that can print val (typedefs)."
109 if type == None or type.name == None or type.code != gdb.TYPE_CODE_TYPEDEF:
112 # Iterate over local dictionary of typedef types to determine if a
113 # printer is registered for that type. Return an instantiation of
114 # the printer if found.
115 for function in typedefs_pretty_printers_dict:
116 if function.match (type.name):
117 return typedefs_pretty_printers_dict[function] (val)
119 # Cannot find a pretty printer.
122 def register_pretty_printers ():
123 pretty_printers_dict[re.compile ('^struct map_t$')] = pp_map
124 pretty_printers_dict[re.compile ('^map_t$')] = pp_map
125 pretty_printers_dict[re.compile ('^struct map_map_t$')] = pp_map_map
126 pretty_printers_dict[re.compile ('^map_map_t$')] = pp_map_map
128 # Dict for struct types with typedefs fully stripped.
129 pretty_printers_dict = {}
130 # Dict for typedef types.
131 typedefs_pretty_printers_dict = {}
133 register_pretty_printers ()
134 gdb.pretty_printers.append (lookup_function)
135 gdb.pretty_printers.append (lookup_typedefs_function)