Fix type casts losing typedefs and reimplement "whatis" typedef stripping
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-prettyprint.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3 Copyright 2008-2017 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <string.h>
19
20 struct s
21 {
22 int a;
23 int *b;
24 };
25
26 struct ss
27 {
28 struct s a;
29 struct s b;
30 };
31
32 struct arraystruct
33 {
34 int y;
35 struct s x[2];
36 };
37
38 struct ns {
39 const char *null_str;
40 int length;
41 };
42
43 struct lazystring {
44 const char *lazy_str;
45 /* If -1, don't pass length to gdb.lazy_string(). */
46 int len;
47 };
48
49 struct hint_error {
50 int x;
51 };
52
53 struct children_as_list {
54 int x;
55 };
56
57 #ifdef __cplusplus
58 struct S : public s {
59 int zs;
60 };
61
62 struct SS {
63 int zss;
64 S s;
65 };
66
67 struct SSS
68 {
69 SSS (int x, const S& r);
70 int a;
71 const S &b;
72 };
73 SSS::SSS (int x, const S& r) : a(x), b(r) { }
74
75 class VirtualTest
76 {
77 private:
78 int value;
79
80 public:
81 VirtualTest ()
82 {
83 value = 1;
84 }
85 };
86
87 class Vbase1 : public virtual VirtualTest { };
88 class Vbase2 : public virtual VirtualTest { };
89 class Vbase3 : public virtual VirtualTest { };
90
91 class Derived : public Vbase1, public Vbase2, public Vbase3
92 {
93 private:
94 int value;
95
96 public:
97 Derived ()
98 {
99 value = 2;
100 }
101 };
102
103 class Fake
104 {
105 int sname;
106
107 public:
108 Fake (const int name = 0):
109 sname (name)
110 {
111 }
112 };
113 #endif
114
115 struct substruct {
116 int a;
117 int b;
118 };
119
120 struct outerstruct {
121 struct substruct s;
122 int x;
123 };
124
125 struct outerstruct
126 substruct_test (void)
127 {
128 struct outerstruct outer;
129 outer.s.a = 0;
130 outer.s.b = 0;
131 outer.x = 0;
132
133 outer.s.a = 3; /* MI outer breakpoint here */
134
135 return outer;
136 }
137
138 typedef struct string_repr
139 {
140 struct whybother
141 {
142 const char *contents;
143 } whybother;
144 } string;
145
146 /* This lets us avoid malloc. */
147 int array[100];
148 int narray[10];
149
150 struct justchildren
151 {
152 int len;
153 int *elements;
154 };
155
156 typedef struct justchildren nostring_type;
157
158 struct memory_error
159 {
160 const char *s;
161 };
162
163 struct container
164 {
165 string name;
166 int len;
167 int *elements;
168 };
169
170 typedef struct container zzz_type;
171
172 string
173 make_string (const char *s)
174 {
175 string result;
176 result.whybother.contents = s;
177 return result;
178 }
179
180 zzz_type
181 make_container (const char *s)
182 {
183 zzz_type result;
184
185 result.name = make_string (s);
186 result.len = 0;
187 result.elements = 0;
188
189 return result;
190 }
191
192 void
193 add_item (zzz_type *c, int val)
194 {
195 if (c->len == 0)
196 c->elements = array;
197 c->elements[c->len] = val;
198 ++c->len;
199 }
200
201 void
202 set_item(zzz_type *c, int i, int val)
203 {
204 if (i < c->len)
205 c->elements[i] = val;
206 }
207
208 void init_s(struct s *s, int a)
209 {
210 s->a = a;
211 s->b = &s->a;
212 }
213
214 void init_ss(struct ss *s, int a, int b)
215 {
216 init_s(&s->a, a);
217 init_s(&s->b, b);
218 }
219
220 void do_nothing(void)
221 {
222 int c;
223
224 c = 23; /* Another MI breakpoint */
225 }
226
227 struct nullstr
228 {
229 char *s;
230 };
231
232 struct string_repr string_1 = { { "one" } };
233 struct string_repr string_2 = { { "two" } };
234
235 int
236 eval_func (int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8)
237 {
238 return p1;
239 }
240
241 static void
242 eval_sub (void)
243 {
244 struct eval_type_s { int x; } eval1 = { 1 }, eval2 = { 2 }, eval3 = { 3 },
245 eval4 = { 4 }, eval5 = { 5 }, eval6 = { 6 },
246 eval7 = { 7 }, eval8 = { 8 }, eval9 = { 9 };
247
248 eval1.x++; /* eval-break */
249 }
250
251 static void
252 bug_14741()
253 {
254 zzz_type c = make_container ("bug_14741");
255 add_item (&c, 71);
256 set_item(&c, 0, 42); /* breakpoint bug 14741 */
257 set_item(&c, 0, 5);
258 }
259
260 /* Some typedefs/variables for checking that GDB doesn't lose typedefs
261 when looking for a printer. */
262 typedef int int_type;
263 typedef int_type int_type2;
264
265 int an_int = -1;
266 int_type an_int_type = 1;
267 int_type2 an_int_type2 = 2;
268
269 int
270 main ()
271 {
272 struct ss ss;
273 struct ss ssa[2];
274 struct arraystruct arraystruct;
275 string x = make_string ("this is x");
276 zzz_type c = make_container ("container");
277 zzz_type c2 = make_container ("container2");
278 const struct string_repr cstring = { { "const string" } };
279 /* Clearing by being `static' could invoke an other GDB C++ bug. */
280 struct nullstr nullstr;
281 nostring_type nstype, nstype2;
282 struct memory_error me;
283 struct ns ns, ns2;
284 struct lazystring estring, estring2, estring3;
285 struct hint_error hint_error;
286 struct children_as_list children_as_list;
287
288 nstype.elements = narray;
289 nstype.len = 0;
290
291 me.s = "blah";
292
293 init_ss(&ss, 1, 2);
294 init_ss(ssa+0, 3, 4);
295 init_ss(ssa+1, 5, 6);
296 memset (&nullstr, 0, sizeof nullstr);
297
298 arraystruct.y = 7;
299 init_s (&arraystruct.x[0], 23);
300 init_s (&arraystruct.x[1], 24);
301
302 ns.null_str = "embedded\0null\0string";
303 ns.length = 20;
304
305 /* Make a "corrupted" string. */
306 ns2.null_str = NULL;
307 ns2.length = 20;
308
309 estring.lazy_str = "embedded x\201\202\203\204";
310 estring.len = -1;
311
312 /* Incomplete UTF-8, but ok Latin-1. */
313 estring2.lazy_str = "embedded x\302";
314 estring2.len = -1;
315
316 estring3.lazy_str = NULL;
317 estring3.len = 42;
318
319 #ifdef __cplusplus
320 S cps;
321
322 cps.zs = 7;
323 init_s(&cps, 8);
324
325 SS cpss;
326 cpss.zss = 9;
327 init_s(&cpss.s, 10);
328
329 SS cpssa[2];
330 cpssa[0].zss = 11;
331 init_s(&cpssa[0].s, 12);
332 cpssa[1].zss = 13;
333 init_s(&cpssa[1].s, 14);
334
335 SSS sss(15, cps);
336
337 SSS& ref (sss);
338
339 Derived derived;
340
341 Fake fake (42);
342 #endif
343
344 add_item (&c, 23); /* MI breakpoint here */
345 add_item (&c, 72);
346
347 #ifdef MI
348 add_item (&c, 1011);
349 c.elements[0] = 1023;
350 c.elements[0] = 2323;
351
352 add_item (&c2, 2222);
353 add_item (&c2, 3333);
354
355 substruct_test ();
356 do_nothing ();
357 #endif
358
359 nstype.elements[0] = 7;
360 nstype.elements[1] = 42;
361 nstype.len = 2;
362
363 nstype2 = nstype;
364
365 eval_sub ();
366
367 bug_14741(); /* break to inspect struct and union */
368 return 0;
369 }
This page took 0.036467 seconds and 4 git commands to generate.