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