merge from gcc
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-prettyprint.py
CommitLineData
4c38e0a4 1# Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
a6bac58e
TT
2
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.
7#
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.
12#
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/>.
15
16# This file is part of the GDB testsuite. It tests python pretty
17# printers.
18
19import re
20
21# Test returning a Value from a printer.
22class string_print:
23 def __init__(self, val):
24 self.val = val
25
26 def to_string(self):
27 return self.val['whybother']['contents']
28
29# Test a class-based printer.
30class ContainerPrinter:
31 class _iterator:
32 def __init__ (self, pointer, len):
33 self.start = pointer
34 self.pointer = pointer
35 self.end = pointer + len
36
37 def __iter__(self):
38 return self
39
40 def next(self):
41 if self.pointer == self.end:
42 raise StopIteration
43 result = self.pointer
44 self.pointer = self.pointer + 1
45 return ('[%d]' % int (result - self.start), result.dereference())
46
47 def __init__(self, val):
48 self.val = val
49
50 def to_string(self):
51 return 'container %s with %d elements' % (self.val['name'], self.val['len'])
52
53 def children(self):
54 return self._iterator(self.val['elements'], self.val['len'])
55
79f283fe
PM
56# Test a printer where to_string is None
57class NoStringContainerPrinter:
58 class _iterator:
59 def __init__ (self, pointer, len):
60 self.start = pointer
61 self.pointer = pointer
62 self.end = pointer + len
63
64 def __iter__(self):
65 return self
66
67 def next(self):
68 if self.pointer == self.end:
69 raise StopIteration
70 result = self.pointer
71 self.pointer = self.pointer + 1
72 return ('[%d]' % int (result - self.start), result.dereference())
73
74 def __init__(self, val):
75 self.val = val
76
77 def to_string(self):
78 return None
79
80 def children(self):
81 return self._iterator(self.val['elements'], self.val['len'])
82
a6bac58e
TT
83class pp_s:
84 def __init__(self, val):
85 self.val = val
86
87 def to_string(self):
88 a = self.val["a"]
89 b = self.val["b"]
90 if a.address != b:
91 raise Exception("&a(%s) != b(%s)" % (str(a.address), str(b)))
92 return " a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
93
94class pp_ss:
95 def __init__(self, val):
96 self.val = val
97
98 def to_string(self):
99 return "a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
100
101class pp_sss:
102 def __init__(self, val):
103 self.val = val
104
105 def to_string(self):
106 return "a=<" + str(self.val['a']) + "> b=<" + str(self.val["b"]) + ">"
107
108class pp_multiple_virtual:
109 def __init__ (self, val):
110 self.val = val
111
112 def to_string (self):
113 return "pp value variable is: " + str (self.val['value'])
114
115class pp_vbase1:
116 def __init__ (self, val):
117 self.val = val
118
119 def to_string (self):
120 return "pp class name: " + self.val.type.tag
121
0cc7d26f
TT
122class pp_nullstr:
123 def __init__(self, val):
124 self.val = val
125
126 def to_string(self):
f870a310 127 return self.val['s'].string(gdb.target_charset())
0cc7d26f 128
fbb8f299
PM
129class pp_ns:
130 "Print a std::basic_string of some kind"
131
132 def __init__(self, val):
133 self.val = val
134
135 def to_string(self):
136 len = self.val['length']
f870a310 137 return self.val['null_str'].string (gdb.target_charset(), length = len)
fbb8f299
PM
138
139 def display_hint (self):
140 return 'string'
141
be759fcf
PM
142class pp_ls:
143 "Print a std::basic_string of some kind"
144
145 def __init__(self, val):
146 self.val = val
147
148 def to_string(self):
149 return self.val['lazy_str'].lazy_string()
150
151 def display_hint (self):
152 return 'string'
153
0cc7d26f
TT
154class pp_outer:
155 "Print struct outer"
156
157 def __init__ (self, val):
158 self.val = val
159
160 def to_string (self):
161 return "x = %s" % self.val['x']
162
163 def children (self):
164 yield 's', self.val['s']
165 yield 'x', self.val['x']
166
a6bac58e
TT
167def lookup_function (val):
168 "Look-up and return a pretty-printer that can print val."
169
170 # Get the type.
0cc7d26f 171 type = val.type
a6bac58e
TT
172
173 # If it points to a reference, get the reference.
174 if type.code == gdb.TYPE_CODE_REF:
175 type = type.target ()
176
177 # Get the unqualified type, stripped of typedefs.
178 type = type.unqualified ().strip_typedefs ()
179
180 # Get the type name.
181 typename = type.tag
182
183 if typename == None:
184 return None
185
186 # Iterate over local dictionary of types to determine
187 # if a printer is registered for that type. Return an
188 # instantiation of the printer if found.
189 for function in pretty_printers_dict:
190 if function.match (typename):
191 return pretty_printers_dict[function] (val)
192
193 # Cannot find a pretty printer. Return None.
194
195 return None
196
967cf477
DE
197def disable_lookup_function ():
198 lookup_function.enabled = False
199
200def enable_lookup_function ():
201 lookup_function.enabled = True
a6bac58e
TT
202
203def register_pretty_printers ():
204 pretty_printers_dict[re.compile ('^struct s$')] = pp_s
205 pretty_printers_dict[re.compile ('^s$')] = pp_s
206 pretty_printers_dict[re.compile ('^S$')] = pp_s
207
208 pretty_printers_dict[re.compile ('^struct ss$')] = pp_ss
209 pretty_printers_dict[re.compile ('^ss$')] = pp_ss
210 pretty_printers_dict[re.compile ('^const S &$')] = pp_s
211 pretty_printers_dict[re.compile ('^SSS$')] = pp_sss
212
213 pretty_printers_dict[re.compile ('^VirtualTest$')] = pp_multiple_virtual
214 pretty_printers_dict[re.compile ('^Vbase1$')] = pp_vbase1
0cc7d26f
TT
215
216 pretty_printers_dict[re.compile ('^struct nullstr$')] = pp_nullstr
217 pretty_printers_dict[re.compile ('^nullstr$')] = pp_nullstr
a6bac58e
TT
218
219 # Note that we purposely omit the typedef names here.
220 # Printer lookup is based on canonical name.
221 # However, we do need both tagged and untagged variants, to handle
222 # both the C and C++ cases.
223 pretty_printers_dict[re.compile ('^struct string_repr$')] = string_print
224 pretty_printers_dict[re.compile ('^struct container$')] = ContainerPrinter
79f283fe 225 pretty_printers_dict[re.compile ('^struct justchildren$')] = NoStringContainerPrinter
a6bac58e
TT
226 pretty_printers_dict[re.compile ('^string_repr$')] = string_print
227 pretty_printers_dict[re.compile ('^container$')] = ContainerPrinter
79f283fe 228 pretty_printers_dict[re.compile ('^justchildren$')] = NoStringContainerPrinter
a6bac58e 229
fbb8f299
PM
230 pretty_printers_dict[re.compile ('^struct ns$')] = pp_ns
231 pretty_printers_dict[re.compile ('^ns$')] = pp_ns
0cc7d26f 232
be759fcf
PM
233 pretty_printers_dict[re.compile ('^struct lazystring$')] = pp_ls
234 pretty_printers_dict[re.compile ('^lazystring$')] = pp_ls
235
0cc7d26f
TT
236 pretty_printers_dict[re.compile ('^struct outerstruct$')] = pp_outer
237 pretty_printers_dict[re.compile ('^outerstruct$')] = pp_outer
238
a6bac58e
TT
239pretty_printers_dict = {}
240
241register_pretty_printers ()
242gdb.pretty_printers.append (lookup_function)
This page took 0.191865 seconds and 4 git commands to generate.