xref: /llvm-project/clang/test/CodeGen/designated-initializers.c (revision 627746581b8fde4143533937130f420bbbdf9ddf)
1 // RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -o - | FileCheck %s
2 
3 struct foo {
4     void *a;
5     int b;
6 };
7 
8 // CHECK: @u ={{.*}} global %union.anon zeroinitializer
9 union { int i; float f; } u = { };
10 
11 // CHECK: @u2 ={{.*}} global { i32, [4 x i8] } zeroinitializer
12 union { int i; double f; } u2 = { };
13 
14 // CHECK: @u3 ={{.*}} global  %union.anon.1 zeroinitializer
15 union { double f; int i; } u3 = { };
16 
17 // CHECK: @b ={{.*}} global [2 x i32] [i32 0, i32 22]
18 int b[2] = {
19   [1] = 22
20 };
21 
22 // PR6955
23 
24 struct ds {
25   struct {
26     struct {
27       short a;
28     };
29     short b;
30     struct {
31       short c;
32     };
33   };
34 };
35 
36 // Traditional C anonymous member init
37 struct ds ds0 = { { { .a = 0 } } };
38 // C1X lookup-based anonymous member init cases
39 struct ds ds1 = { { .a = 1 } };
40 struct ds ds2 = { { .b = 1 } };
41 struct ds ds3 = { .a = 0 };
42 // CHECK: @ds4 ={{.*}} global %struct.ds { %struct.anon.3 { %struct.anon zeroinitializer, i16 0, %struct.anon.2 { i16 1 } } }
43 struct ds ds4 = { .c = 1 };
44 struct ds ds5 = { { { .a = 0 } }, .b = 1 };
45 struct ds ds6 = { { .a = 0, .b = 1 } };
46 // CHECK: @ds7 ={{.*}} global %struct.ds { %struct.anon.3 { %struct.anon { i16 2 }, i16 3, %struct.anon.2 zeroinitializer } }
47 struct ds ds7 = {
48   { {
49       .a = 1
50     } },
51   .a = 2,
52   .b = 3
53 };
54 
55 
56 struct overwrite_string_struct1 {
57   __typeof(L"foo"[0]) L[6];
58   int M;
59 } overwrite_string1[] = { { { L"foo" }, 1 }, [0].L[2] = L'x'};
60 // CHECK: [6 x i32] [i32 102, i32 111, i32 120, i32 0, i32 0, i32 0], i32 1
61 struct overwrite_string_struct2 {
62   char L[6];
63   int M;
64 } overwrite_string2[] = { { { "foo" }, 1 }, [0].L[2] = 'x'};
65 // CHECK: [6 x i8] c"fox\00\00\00", [2 x i8] zeroinitializer, i32 1
66 struct overwrite_string_struct3 {
67   char L[3];
68   int M;
69 } overwrite_string3[] = { { { "foo" }, 1 }, [0].L[2] = 'x'};
70 // CHECK: [3 x i8] c"fox", i8 0, i32 1
71 struct overwrite_string_struct4 {
72   char L[3];
73   int M;
74 } overwrite_string4[] = { { { "foobar" }, 1 }, [0].L[2] = 'x'};
75 // CHECK: [3 x i8] c"fox", i8 0, i32 1
76 struct overwrite_string_struct5 {
77   char L[6];
78   int M;
79 } overwrite_string5[] = { { { "foo" }, 1 }, [0].L[4] = 'y'};
80 // CHECK: [6 x i8] c"foo\00y\00", [2 x i8] zeroinitializer, i32 1
81 
82 
83 // CHECK: @u1 = {{.*}} { i32 65535 }
84 union u_FFFF { char c; long l; } u1 = { .l = 0xFFFF };
85 
86 
87 /// PR16644
88 typedef union u_16644 {
89   struct s_16644 {
90     int zero;
91     int one;
92     int two;
93     int three;
94   } a;
95   int b[4];
96 } union_16644_t;
97 
98 // CHECK: @union_16644_instance_0 = {{.*}} { i32 0, i32 0, i32 0, i32 3 } }
99 union_16644_t union_16644_instance_0 =
100 {
101   .b[0]    = 0,
102   .a.one   = 1,
103   .b[2]    = 2,
104   .a.three = 3,
105 };
106 
107 // CHECK: @union_16644_instance_1 = {{.*}} [i32 10, i32 0, i32 0, i32 0]
108 union_16644_t union_16644_instance_1 =
109 {
110   .a.three = 13,
111   .b[2]    = 12,
112   .a.one   = 11,
113   .b[0]    = 10,
114 };
115 
116 // CHECK: @union_16644_instance_2 = {{.*}} [i32 0, i32 20, i32 0, i32 0]
117 union_16644_t union_16644_instance_2 =
118 {
119   .a.one   = 21,
120   .b[1]    = 20,
121 };
122 
123 // CHECK: @union_16644_instance_3 = {{.*}} { i32 0, i32 31, i32 0, i32 0 }
124 union_16644_t union_16644_instance_3 =
125 {
126   .b[1]    = 30,
127   .a = {
128     .one = 31
129   }
130 };
131 
132 // CHECK: @union_16644_instance_4 = {{.*}} { i32 5, i32 2, i32 0, i32 0 } {{.*}} [i32 0, i32 4, i32 0, i32 0]
133 union_16644_t union_16644_instance_4[2] =
134 {
135   [0].a.one = 2,
136   [1].a.zero = 3,
137   [0].a.zero = 5,
138   [1].b[1] = 4
139 };
140 
141 // CHECK: @lab ={{.*}} global { [4 x i8], i32 } { [4 x i8] zeroinitializer, i32 123 }
142 struct leading_anon_bitfield { int : 32; int n; } lab = { .n = 123 };
143 
144 struct Base {
145   struct {
146     int A;
147   };
148 };
149 struct Derived {
150   struct Base B;
151 };
152 struct Derived D = {{}, .B.A = 42};
153 // CHECK: @D ={{.*}} global %struct.Derived { %struct.Base { %struct.anon.4 { i32 42 } } }, align 4
154 
155 void test1(int argc, char **argv)
156 {
157   // CHECK: internal global %struct.foo { ptr null, i32 1024 }
158   static struct foo foo = {
159     .b = 1024,
160   };
161 
162   // CHECK: call void @llvm.memset
163    union { int i; float f; } u2 = { };
164 
165   // CHECK-NOT: call void @llvm.memset
166   union { int i; float f; } u3;
167 
168   // CHECK: ret void
169 }
170 
171 
172 // PR7151
173 struct S {
174   int nkeys;
175   int *keys;
176   union {
177     void *data;
178   };
179 };
180 
181 void test2(void) {
182   struct S *btkr;
183 
184   *btkr = (struct S) {
185     .keys  = 0,
186     { .data  = 0 },
187   };
188 }
189