Ensure unreferenced static symbols aren't omitted by clang (either marking them __att...
[external/binutils.git] / gdb / testsuite / gdb.python / py-prettyprint.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3    Copyright 2008-2014 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 };
46
47 struct hint_error {
48   int x;
49 };
50
51 struct children_as_list {
52   int x;
53 };
54
55 #ifdef __cplusplus
56 struct S : public s {
57   int zs;
58 };
59
60 struct SS {
61   int zss;
62   S s;
63 };
64
65 struct SSS
66 {
67   SSS (int x, const S& r);
68   int a;
69   const S &b;
70 };
71 SSS::SSS (int x, const S& r) : a(x), b(r) { }
72
73 class VirtualTest 
74
75  private: 
76   int value; 
77
78  public: 
79   VirtualTest () 
80     { 
81       value = 1;
82     } 
83 };
84
85 class Vbase1 : public virtual VirtualTest { };
86 class Vbase2 : public virtual VirtualTest { };
87 class Vbase3 : public virtual VirtualTest { };
88
89 class Derived : public Vbase1, public Vbase2, public Vbase3
90
91  private: 
92   int value; 
93   
94  public:
95   Derived () 
96     { 
97       value = 2; 
98     }
99 };
100
101 class Fake
102 {
103   int sname;
104   
105  public:
106   Fake (const int name = 0):
107   sname (name)
108   {
109   }
110 };
111 #endif
112
113 struct substruct {
114   int a;
115   int b;
116 };
117
118 struct outerstruct {
119   struct substruct s;
120   int x;
121 };
122
123 struct outerstruct
124 substruct_test (void)
125 {
126   struct outerstruct outer;
127   outer.s.a = 0;
128   outer.s.b = 0;
129   outer.x = 0;
130
131   outer.s.a = 3;                /* MI outer breakpoint here */
132
133   return outer;  
134 }
135
136 typedef struct string_repr
137 {
138   struct whybother
139   {
140     const char *contents;
141   } whybother;
142 } string;
143
144 /* This lets us avoid malloc.  */
145 int array[100];
146 int narray[10];
147
148 struct justchildren
149 {
150   int len;
151   int *elements;
152 };
153
154 typedef struct justchildren nostring_type;
155
156 struct memory_error
157 {
158   const char *s;
159 };
160
161 struct container
162 {
163   string name;
164   int len;
165   int *elements;
166 };
167
168 typedef struct container zzz_type;
169
170 string
171 make_string (const char *s)
172 {
173   string result;
174   result.whybother.contents = s;
175   return result;
176 }
177
178 zzz_type
179 make_container (const char *s)
180 {
181   zzz_type result;
182
183   result.name = make_string (s);
184   result.len = 0;
185   result.elements = 0;
186
187   return result;
188 }
189
190 void
191 add_item (zzz_type *c, int val)
192 {
193   if (c->len == 0)
194     c->elements = array;
195   c->elements[c->len] = val;
196   ++c->len;
197 }
198
199 void
200 set_item(zzz_type *c, int i, int val)
201 {
202   if (i < c->len)
203     c->elements[i] = val;
204 }
205
206 void init_s(struct s *s, int a)
207 {
208   s->a = a;
209   s->b = &s->a;
210 }
211
212 void init_ss(struct ss *s, int a, int b)
213 {
214   init_s(&s->a, a);
215   init_s(&s->b, b);
216 }
217
218 void do_nothing(void)
219 {
220   int c;
221
222   c = 23;                       /* Another MI breakpoint */
223 }
224
225 struct nullstr
226 {
227   char *s;
228 };
229
230 struct string_repr string_1 = { { "one" } };
231 struct string_repr string_2 = { { "two" } };
232
233 int
234 eval_func (int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8)
235 {
236   return p1;
237 }
238
239 static void
240 eval_sub (void)
241 {
242   struct eval_type_s { int x; } eval1 = { 1 }, eval2 = { 2 }, eval3 = { 3 },
243                                 eval4 = { 4 }, eval5 = { 5 }, eval6 = { 6 },
244                                 eval7 = { 7 }, eval8 = { 8 }, eval9 = { 9 };
245
246   eval1.x++; /* eval-break */
247 }
248
249 static void
250 bug_14741()
251 {
252   zzz_type c = make_container ("bug_14741");
253   add_item (&c, 71);
254   set_item(&c, 0, 42); /* breakpoint bug 14741 */
255   set_item(&c, 0, 5);
256 }
257
258 int
259 main ()
260 {
261   struct ss  ss;
262   struct ss  ssa[2];
263   struct arraystruct arraystruct;
264   string x = make_string ("this is x");
265   zzz_type c = make_container ("container");
266   zzz_type c2 = make_container ("container2");
267   const struct string_repr cstring = { { "const string" } };
268   /* Clearing by being `static' could invoke an other GDB C++ bug.  */
269   struct nullstr nullstr;
270   nostring_type nstype, nstype2;
271   struct memory_error me;
272   struct ns ns, ns2;
273   struct lazystring estring, estring2;
274   struct hint_error hint_error;
275   struct children_as_list children_as_list;
276
277   nstype.elements = narray;
278   nstype.len = 0;
279
280   me.s = "blah";
281
282   init_ss(&ss, 1, 2);
283   init_ss(ssa+0, 3, 4);
284   init_ss(ssa+1, 5, 6);
285   memset (&nullstr, 0, sizeof nullstr);
286
287   arraystruct.y = 7;
288   init_s (&arraystruct.x[0], 23);
289   init_s (&arraystruct.x[1], 24);
290
291   ns.null_str = "embedded\0null\0string";
292   ns.length = 20;
293
294   /* Make a "corrupted" string.  */
295   ns2.null_str = NULL;
296   ns2.length = 20;
297
298   estring.lazy_str = "embedded x\201\202\203\204" ;
299
300   /* Incomplete UTF-8, but ok Latin-1.  */
301   estring2.lazy_str = "embedded x\302";
302
303 #ifdef __cplusplus
304   S cps;
305
306   cps.zs = 7;
307   init_s(&cps, 8);
308
309   SS cpss;
310   cpss.zss = 9;
311   init_s(&cpss.s, 10);
312
313   SS cpssa[2];
314   cpssa[0].zss = 11;
315   init_s(&cpssa[0].s, 12);
316   cpssa[1].zss = 13;
317   init_s(&cpssa[1].s, 14);
318
319   SSS sss(15, cps);
320
321   SSS& ref (sss);
322
323   Derived derived;
324   
325   Fake fake (42);
326 #endif
327
328   add_item (&c, 23);            /* MI breakpoint here */
329   add_item (&c, 72);
330
331 #ifdef MI
332   add_item (&c, 1011);
333   c.elements[0] = 1023;
334   c.elements[0] = 2323;
335
336   add_item (&c2, 2222);
337   add_item (&c2, 3333);
338
339   substruct_test ();
340   do_nothing ();
341 #endif
342
343   nstype.elements[0] = 7;
344   nstype.elements[1] = 42;
345   nstype.len = 2;
346   
347   nstype2 = nstype;
348
349   eval_sub ();
350
351   bug_14741();      /* break to inspect struct and union */
352   return 0;
353 }