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