1 # Copyright (C) 2008, 2009, 2010 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 python pretty
21 # Test returning a Value from a printer.
23 def __init__(self
, val
):
27 return self
.val
['whybother']['contents']
29 # Test a class-based printer.
30 class ContainerPrinter
:
32 def __init__ (self
, pointer
, len):
34 self
.pointer
= pointer
35 self
.end
= pointer
+ len
41 if self
.pointer
== self
.end
:
44 self
.pointer
= self
.pointer
+ 1
45 return ('[%d]' % int (result
- self
.start
), result
.dereference())
47 def __init__(self
, val
):
51 return 'container %s with %d elements' % (self
.val
['name'], self
.val
['len'])
54 return self
._iterator
(self
.val
['elements'], self
.val
['len'])
56 # Test a printer where to_string is None
57 class NoStringContainerPrinter
:
59 def __init__ (self
, pointer
, len):
61 self
.pointer
= pointer
62 self
.end
= pointer
+ len
68 if self
.pointer
== self
.end
:
71 self
.pointer
= self
.pointer
+ 1
72 return ('[%d]' % int (result
- self
.start
), result
.dereference())
74 def __init__(self
, val
):
81 return self
._iterator
(self
.val
['elements'], self
.val
['len'])
84 def __init__(self
, val
):
91 raise Exception("&a(%s) != b(%s)" % (str(a
.address
), str(b
)))
92 return " a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
95 def __init__(self
, val
):
99 return "a=<" + str(self
.val
["a"]) + "> b=<" + str(self
.val
["b"]) + ">"
102 def __init__(self
, val
):
106 return "a=<" + str(self
.val
['a']) + "> b=<" + str(self
.val
["b"]) + ">"
108 class pp_multiple_virtual
:
109 def __init__ (self
, val
):
112 def to_string (self
):
113 return "pp value variable is: " + str (self
.val
['value'])
116 def __init__ (self
, val
):
119 def to_string (self
):
120 return "pp class name: " + self
.val
.type.tag
123 def __init__(self
, val
):
127 return self
.val
['s'].string(gdb
.target_charset())
130 "Print a std::basic_string of some kind"
132 def __init__(self
, val
):
136 len = self
.val
['length']
137 return self
.val
['null_str'].string (gdb
.target_charset(), length
= len)
139 def display_hint (self
):
142 pp_ls_encoding
= None
145 "Print a std::basic_string of some kind"
147 def __init__(self
, val
):
151 if pp_ls_encoding
is not None:
152 return self
.val
['lazy_str'].lazy_string(encoding
= pp_ls_encoding
)
154 return self
.val
['lazy_str'].lazy_string()
156 def display_hint (self
):
162 def __init__ (self
, val
):
165 def to_string (self
):
166 return "x = %s" % self
.val
['x']
169 yield 's', self
.val
['s']
170 yield 'x', self
.val
['x']
172 def lookup_function (val
):
173 "Look-up and return a pretty-printer that can print val."
178 # If it points to a reference, get the reference.
179 if type.code
== gdb
.TYPE_CODE_REF
:
180 type = type.target ()
182 # Get the unqualified type, stripped of typedefs.
183 type = type.unqualified ().strip_typedefs ()
191 # Iterate over local dictionary of types to determine
192 # if a printer is registered for that type. Return an
193 # instantiation of the printer if found.
194 for function
in pretty_printers_dict
:
195 if function
.match (typename
):
196 return pretty_printers_dict
[function
] (val
)
198 # Cannot find a pretty printer. Return None.
202 def disable_lookup_function ():
203 lookup_function
.enabled
= False
205 def enable_lookup_function ():
206 lookup_function
.enabled
= True
208 def register_pretty_printers ():
209 pretty_printers_dict
[re
.compile ('^struct s$')] = pp_s
210 pretty_printers_dict
[re
.compile ('^s$')] = pp_s
211 pretty_printers_dict
[re
.compile ('^S$')] = pp_s
213 pretty_printers_dict
[re
.compile ('^struct ss$')] = pp_ss
214 pretty_printers_dict
[re
.compile ('^ss$')] = pp_ss
215 pretty_printers_dict
[re
.compile ('^const S &$')] = pp_s
216 pretty_printers_dict
[re
.compile ('^SSS$')] = pp_sss
218 pretty_printers_dict
[re
.compile ('^VirtualTest$')] = pp_multiple_virtual
219 pretty_printers_dict
[re
.compile ('^Vbase1$')] = pp_vbase1
221 pretty_printers_dict
[re
.compile ('^struct nullstr$')] = pp_nullstr
222 pretty_printers_dict
[re
.compile ('^nullstr$')] = pp_nullstr
224 # Note that we purposely omit the typedef names here.
225 # Printer lookup is based on canonical name.
226 # However, we do need both tagged and untagged variants, to handle
227 # both the C and C++ cases.
228 pretty_printers_dict
[re
.compile ('^struct string_repr$')] = string_print
229 pretty_printers_dict
[re
.compile ('^struct container$')] = ContainerPrinter
230 pretty_printers_dict
[re
.compile ('^struct justchildren$')] = NoStringContainerPrinter
231 pretty_printers_dict
[re
.compile ('^string_repr$')] = string_print
232 pretty_printers_dict
[re
.compile ('^container$')] = ContainerPrinter
233 pretty_printers_dict
[re
.compile ('^justchildren$')] = NoStringContainerPrinter
235 pretty_printers_dict
[re
.compile ('^struct ns$')] = pp_ns
236 pretty_printers_dict
[re
.compile ('^ns$')] = pp_ns
238 pretty_printers_dict
[re
.compile ('^struct lazystring$')] = pp_ls
239 pretty_printers_dict
[re
.compile ('^lazystring$')] = pp_ls
241 pretty_printers_dict
[re
.compile ('^struct outerstruct$')] = pp_outer
242 pretty_printers_dict
[re
.compile ('^outerstruct$')] = pp_outer
244 pretty_printers_dict
= {}
246 register_pretty_printers ()
247 gdb
.pretty_printers
.append (lookup_function
)
This page took 0.057103 seconds and 5 git commands to generate.