Commit | Line | Data |
---|---|---|
a6bac58e TT |
1 | # Copyright (C) 2008, 2009 Free Software Foundation, Inc. |
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 | ||
19 | import re | |
20 | ||
21 | # Test returning a Value from a printer. | |
22 | class 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. | |
30 | class 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 | ||
56 | class pp_s: | |
57 | def __init__(self, val): | |
58 | self.val = val | |
59 | ||
60 | def to_string(self): | |
61 | a = self.val["a"] | |
62 | b = self.val["b"] | |
63 | if a.address != b: | |
64 | raise Exception("&a(%s) != b(%s)" % (str(a.address), str(b))) | |
65 | return " a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">" | |
66 | ||
67 | class pp_ss: | |
68 | def __init__(self, val): | |
69 | self.val = val | |
70 | ||
71 | def to_string(self): | |
72 | return "a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">" | |
73 | ||
74 | class pp_sss: | |
75 | def __init__(self, val): | |
76 | self.val = val | |
77 | ||
78 | def to_string(self): | |
79 | return "a=<" + str(self.val['a']) + "> b=<" + str(self.val["b"]) + ">" | |
80 | ||
81 | class pp_multiple_virtual: | |
82 | def __init__ (self, val): | |
83 | self.val = val | |
84 | ||
85 | def to_string (self): | |
86 | return "pp value variable is: " + str (self.val['value']) | |
87 | ||
88 | class pp_vbase1: | |
89 | def __init__ (self, val): | |
90 | self.val = val | |
91 | ||
92 | def to_string (self): | |
93 | return "pp class name: " + self.val.type.tag | |
94 | ||
95 | def lookup_function (val): | |
96 | "Look-up and return a pretty-printer that can print val." | |
97 | ||
98 | # Get the type. | |
99 | type = val.type; | |
100 | ||
101 | # If it points to a reference, get the reference. | |
102 | if type.code == gdb.TYPE_CODE_REF: | |
103 | type = type.target () | |
104 | ||
105 | # Get the unqualified type, stripped of typedefs. | |
106 | type = type.unqualified ().strip_typedefs () | |
107 | ||
108 | # Get the type name. | |
109 | typename = type.tag | |
110 | ||
111 | if typename == None: | |
112 | return None | |
113 | ||
114 | # Iterate over local dictionary of types to determine | |
115 | # if a printer is registered for that type. Return an | |
116 | # instantiation of the printer if found. | |
117 | for function in pretty_printers_dict: | |
118 | if function.match (typename): | |
119 | return pretty_printers_dict[function] (val) | |
120 | ||
121 | # Cannot find a pretty printer. Return None. | |
122 | ||
123 | return None | |
124 | ||
125 | ||
126 | def register_pretty_printers (): | |
127 | pretty_printers_dict[re.compile ('^struct s$')] = pp_s | |
128 | pretty_printers_dict[re.compile ('^s$')] = pp_s | |
129 | pretty_printers_dict[re.compile ('^S$')] = pp_s | |
130 | ||
131 | pretty_printers_dict[re.compile ('^struct ss$')] = pp_ss | |
132 | pretty_printers_dict[re.compile ('^ss$')] = pp_ss | |
133 | pretty_printers_dict[re.compile ('^const S &$')] = pp_s | |
134 | pretty_printers_dict[re.compile ('^SSS$')] = pp_sss | |
135 | ||
136 | pretty_printers_dict[re.compile ('^VirtualTest$')] = pp_multiple_virtual | |
137 | pretty_printers_dict[re.compile ('^Vbase1$')] = pp_vbase1 | |
138 | ||
139 | # Note that we purposely omit the typedef names here. | |
140 | # Printer lookup is based on canonical name. | |
141 | # However, we do need both tagged and untagged variants, to handle | |
142 | # both the C and C++ cases. | |
143 | pretty_printers_dict[re.compile ('^struct string_repr$')] = string_print | |
144 | pretty_printers_dict[re.compile ('^struct container$')] = ContainerPrinter | |
145 | pretty_printers_dict[re.compile ('^string_repr$')] = string_print | |
146 | pretty_printers_dict[re.compile ('^container$')] = ContainerPrinter | |
147 | ||
148 | pretty_printers_dict = {} | |
149 | ||
150 | register_pretty_printers () | |
151 | gdb.pretty_printers.append (lookup_function) |