blob: b11c67a454213518339d290e3991b0e8348f1db8 [file] [log] [blame]
Nuno Lopes5863c992010-04-16 20:56:35 +00001// RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -o - | FileCheck %s
Nuno Lopes92f95eb2009-01-30 12:58:18 +00002
Douglas Gregor0202cb42009-01-29 17:44:32 +00003struct foo {
4 void *a;
5 int b;
6};
7
Chris Lattner73e30042011-07-12 05:53:08 +00008// CHECK: @u = global %union.anon zeroinitializer
Douglas Gregor0202cb42009-01-29 17:44:32 +00009union { int i; float f; } u = { };
10
Chris Lattnera5f58b02011-07-09 17:41:47 +000011// CHECK: @u2 = global { i32, [4 x i8] } { i32 0, [4 x i8] undef }
Nuno Lopes5863c992010-04-16 20:56:35 +000012union { int i; double f; } u2 = { };
13
Chris Lattner73e30042011-07-12 05:53:08 +000014// CHECK: @u3 = global %union.anon.1 zeroinitializer
Nuno Lopes1c5c30f2010-04-16 21:19:39 +000015union { double f; int i; } u3 = { };
16
Nuno Lopes5863c992010-04-16 20:56:35 +000017// CHECK: @b = global [2 x i32] [i32 0, i32 22]
18int b[2] = {
19 [1] = 22
20};
21
Douglas Gregor559c9fb2010-10-08 20:44:28 +000022// PR6955
23
24struct 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
37struct ds ds0 = { { { .a = 0 } } };
38// C1X lookup-based anonymous member init cases
39struct ds ds1 = { { .a = 1 } };
40struct ds ds2 = { { .b = 1 } };
41struct ds ds3 = { .a = 0 };
Chris Lattner73e30042011-07-12 05:53:08 +000042// CHECK: @ds4 = global %struct.ds { %struct.anon.3 { %struct.anon zeroinitializer, i16 0, %struct.anon.2 { i16 1 } } }
Douglas Gregor559c9fb2010-10-08 20:44:28 +000043struct ds ds4 = { .c = 1 };
44struct ds ds5 = { { { .a = 0 } }, .b = 1 };
45struct ds ds6 = { { .a = 0, .b = 1 } };
Chris Lattner73e30042011-07-12 05:53:08 +000046// CHECK: @ds7 = global %struct.ds { %struct.anon.3 { %struct.anon { i16 2 }, i16 3, %struct.anon.2 zeroinitializer } }
Douglas Gregor559c9fb2010-10-08 20:44:28 +000047struct ds ds7 = {
48 { {
49 .a = 1
50 } },
51 .a = 2,
52 .b = 3
53};
54
Eli Friedman1f16b742013-06-11 21:48:11 +000055
56// <rdar://problem/10465114>
57struct overwrite_string_struct1 {
58 __typeof(L"foo"[0]) L[6];
59 int M;
60} overwrite_string1[] = { { { L"foo" }, 1 }, [0].L[2] = L'x'};
61// CHECK: [6 x i32] [i32 102, i32 111, i32 120, i32 0, i32 0, i32 0], i32 1
62struct overwrite_string_struct2 {
63 char L[6];
64 int M;
65} overwrite_string2[] = { { { "foo" }, 1 }, [0].L[2] = 'x'};
66// CHECK: [6 x i8] c"fox\00\00\00", i32 1
67struct overwrite_string_struct3 {
68 char L[3];
69 int M;
70} overwrite_string3[] = { { { "foo" }, 1 }, [0].L[2] = 'x'};
71// CHECK: [3 x i8] c"fox", i32 1
72struct overwrite_string_struct4 {
73 char L[3];
74 int M;
75} overwrite_string4[] = { { { "foobar" }, 1 }, [0].L[2] = 'x'};
76// CHECK: [3 x i8] c"fox", i32 1
77struct overwrite_string_struct5 {
78 char L[6];
79 int M;
80} overwrite_string5[] = { { { "foo" }, 1 }, [0].L[4] = 'y'};
81// CHECK: [6 x i8] c"foo\00y\00", i32 1
82
83
Matthew Curtis274a9cc2013-10-03 12:14:24 +000084// CHECK: @u1 = {{.*}} { i32 65535 }
85union u_FFFF { char c; long l; } u1 = { .l = 0xFFFF };
86
87
88/// PR16644
89typedef union u_16644 {
90 struct s_16644 {
91 int zero;
92 int one;
93 int two;
94 int three;
95 } a;
96 int b[4];
97} union_16644_t;
98
99// CHECK: @union_16644_instance_0 = {{.*}} { i32 0, i32 0, i32 0, i32 3 } }
100union_16644_t union_16644_instance_0 =
101{
102 .b[0] = 0,
103 .a.one = 1,
104 .b[2] = 2,
105 .a.three = 3,
106};
107
108// CHECK: @union_16644_instance_1 = {{.*}} [i32 10, i32 0, i32 0, i32 0]
109union_16644_t union_16644_instance_1 =
110{
111 .a.three = 13,
112 .b[2] = 12,
113 .a.one = 11,
114 .b[0] = 10,
115};
116
117// CHECK: @union_16644_instance_2 = {{.*}} [i32 0, i32 20, i32 0, i32 0]
118union_16644_t union_16644_instance_2 =
119{
120 .a.one = 21,
121 .b[1] = 20,
122};
123
124// CHECK: @union_16644_instance_3 = {{.*}} { i32 0, i32 31, i32 0, i32 0 }
125union_16644_t union_16644_instance_3 =
126{
127 .b[1] = 30,
128 .a = {
129 .one = 31
130 }
131};
132
133// CHECK: @union_16644_instance_4 = {{.*}} { i32 5, i32 2, i32 0, i32 0 } {{.*}} [i32 0, i32 4, i32 0, i32 0]
134union_16644_t union_16644_instance_4[2] =
135{
136 [0].a.one = 2,
137 [1].a.zero = 3,
138 [0].a.zero = 5,
139 [1].b[1] = 4
140};
Eli Friedman1f16b742013-06-11 21:48:11 +0000141
Chris Lattner52bcf962010-09-06 00:13:11 +0000142void test1(int argc, char **argv)
Douglas Gregor0202cb42009-01-29 17:44:32 +0000143{
Nuno Lopes5863c992010-04-16 20:56:35 +0000144 // CHECK: internal global %struct.foo { i8* null, i32 1024 }
145 static struct foo foo = {
146 .b = 1024,
147 };
Douglas Gregor8aa6bf52009-03-27 23:40:29 +0000148
Chris Lattner73e30042011-07-12 05:53:08 +0000149 // CHECK: bitcast %union.anon.4* %u2
Nuno Lopes5863c992010-04-16 20:56:35 +0000150 // CHECK: call void @llvm.memset
151 union { int i; float f; } u2 = { };
152
153 // CHECK-NOT: call void @llvm.memset
154 union { int i; float f; } u3;
155
Chris Lattner52bcf962010-09-06 00:13:11 +0000156 // CHECK: ret void
157}
158
159
160// PR7151
161struct S {
162 int nkeys;
163 int *keys;
164 union {
165 void *data;
166 };
167};
168
169void test2() {
170 struct S *btkr;
171
172 *btkr = (struct S) {
173 .keys = 0,
174 { .data = 0 },
175 };
Nuno Lopes5863c992010-04-16 20:56:35 +0000176}