Fix PR c++/23373: GDB hangs when printing a struct with a static member of itself
[external/binutils.git] / gdb / testsuite / gdb.base / ptype-offsets.cc
1 /* This testcase is part of GDB, the GNU debugger.
2
3    Copyright 2017-2018 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 <stdint.h>
19
20 /* A struct with many types of fields, in order to test 'ptype
21    /o'.  */
22
23 struct abc
24 {
25   /* Virtual destructor.  */
26   virtual ~abc ()
27   {}
28
29   /* 8-byte address.  Because of the virtual destructor above, this
30      field's offset will be 8.  */
31   void *field1;
32
33   /* No hole here.  */
34
35   /* 4-byte int bitfield of 1-bit.  */
36   unsigned int field2 : 1;
37
38   /* 31-bit hole here.  */
39
40   /* 4-byte int.  */
41   signed int field3;
42
43   /* No hole here.  */
44
45   /* 1-byte char.  */
46   signed char field4;
47
48   /* 7-byte hole here.  */
49
50   /* 8-byte int.  */
51   uint64_t field5;
52
53   /* We just print the offset and size of a union, ignoring its
54      fields.  */
55   union
56   {
57     /* 8-byte address.  */
58     void *field6;
59
60     /* 4-byte int.  */
61     signed int field7;
62   } field8;
63
64   /* Empty constructor.  */
65   abc ()
66   {}
67
68   /* Typedef defined in-struct.  */
69   typedef int my_int_type;
70
71   my_int_type field9;
72 };
73
74 /* This struct will be nested inside 'struct xyz'.  */
75
76 struct tuv
77 {
78   signed int a1;
79
80   signed char *a2;
81
82   signed int a3;
83 };
84
85 /* This struct will be nested inside 'struct pqr'.  */
86
87 struct xyz
88 {
89   signed int f1;
90
91   signed char f2;
92
93   void *f3;
94
95   struct tuv f4;
96 };
97
98 /* A struct with a nested struct.  */
99
100 struct pqr
101 {
102   signed int ff1;
103
104   struct xyz ff2;
105
106   signed char ff3;
107 };
108
109 /* A union with two nested structs.  */
110
111 union qwe
112 {
113   struct tuv fff1;
114
115   struct xyz fff2;
116 };
117
118 /* A struct with an union.  */
119
120 struct poi
121 {
122   signed int f1;
123
124   union qwe f2;
125
126   uint16_t f3;
127
128   struct pqr f4;
129 };
130
131 /* A struct with bitfields.  */
132
133 struct tyu
134 {
135   signed int a1 : 1;
136
137   signed int a2 : 3;
138
139   signed int a3 : 23;
140
141   signed char a4 : 2;
142
143   int64_t a5;
144
145   signed int a6 : 5;
146
147   int64_t a7 : 3;
148 };
149
150 /* A struct with structs and unions.  */
151
152 struct asd
153 {
154   struct jkl
155   {
156       signed char *f1;
157       union
158       {
159         void *ff1;
160       } f2;
161       union
162       {
163         signed char *ff2;
164       } f3;
165       int f4 : 5;
166       unsigned int f5 : 1;
167       short f6;
168   } f7;
169   unsigned long f8;
170   signed char *f9;
171   int f10 : 4;
172   unsigned int f11 : 1;
173   unsigned int f12 : 1;
174   unsigned int f13 : 1;
175   unsigned int f14 : 1;
176   void *f15;
177   void *f16;
178 };
179
180 /* See PR c++/23373.  */
181
182 struct static_member
183 {
184   static static_member Empty;
185   int abc;
186 };
187
188 int
189 main (int argc, char *argv[])
190 {
191   struct abc foo;
192   struct pqr bar;
193   union qwe c;
194   struct poi d;
195   struct tyu e;
196   struct asd f;
197   uint8_t i;
198   static_member stmember;
199
200   return 0;
201 }