xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.base/vla-struct-fields.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /* This testcase is part of GDB, the GNU debugger.
2 
3    Copyright 2014-2023 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 <stddef.h>
19 #define SIZE 5
20 
21 struct foo
22 {
23   int a;
24 };
25 
26 typedef struct bar
27 {
28   int x;
29   struct foo y;
30 } BAR;
31 
32 void
33 vla_factory (int n)
34 {
35   struct vla_struct
36   {
37     int something;
38     int vla_field[n];
39   };
40   /* Define a typedef for a VLA structure.  */
41   typedef struct vla_struct vla_struct_typedef;
42   vla_struct_typedef vla_struct_object;
43 
44   struct inner_vla_struct
45   {
46     int something;
47     int vla_field[n];
48     int after;
49   } inner_vla_struct_object;
50 
51   /* Define a structure which uses a typedef for the VLA field
52      to make sure that GDB creates the proper type for this field,
53      preventing a possible assertion failure (see gdb/21356).  */
54   struct vla_struct_typedef_struct_member
55   {
56     int something;
57     vla_struct_typedef vla_object;
58   } vla_struct_typedef_struct_member_object;
59 
60   union vla_union
61   {
62     int vla_field[n];
63   } vla_union_object;
64 
65   /* Like vla_struct_typedef_struct_member but a union type.  */
66   union vla_struct_typedef_union_member
67   {
68     int something;
69     vla_struct_typedef vla_object;
70   } vla_struct_typedef_union_member_object;
71   int i;
72 
73   vla_struct_object.something = n;
74   inner_vla_struct_object.something = n;
75   inner_vla_struct_object.after = n;
76   vla_struct_typedef_struct_member_object.something = n * 2;
77   vla_struct_typedef_struct_member_object.vla_object.something = n * 3;
78   vla_struct_typedef_union_member_object.vla_object.something = n + 1;
79 
80   for (i = 0; i < n; i++)
81     {
82       vla_struct_object.vla_field[i] = i*2;
83       vla_union_object.vla_field[i] = i*2;
84       inner_vla_struct_object.vla_field[i] = i*2;
85       vla_struct_typedef_struct_member_object.vla_object.vla_field[i]
86 	= i * 3;
87       vla_struct_typedef_union_member_object.vla_object.vla_field[i]
88 	= i * 3 - 1;
89     }
90 
91   size_t vla_struct_object_size
92     = sizeof(vla_struct_object);          /* vlas_filled */
93   size_t vla_union_object_size = sizeof(vla_union_object);
94   size_t inner_vla_struct_object_size = sizeof(inner_vla_struct_object);
95 
96   return;                                 /* break_end_of_vla_factory */
97 }
98 
99 int
100 main (void)
101 {
102   vla_factory(SIZE);
103   return 0;
104 }
105