xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGen/const-init.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple i386-pc-linux-gnu -ffreestanding -verify -emit-llvm -o - %s | FileCheck %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc #include <stdint.h>
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc // Brace-enclosed string array initializers
6f4a2713aSLionel Sambuc char a[] = { "asdf" };
7f4a2713aSLionel Sambuc // CHECK: @a = global [5 x i8] c"asdf\00"
8f4a2713aSLionel Sambuc 
9f4a2713aSLionel Sambuc char a2[2][5] = { "asdf" };
10f4a2713aSLionel Sambuc // CHECK: @a2 = global [2 x [5 x i8]] {{\[}}[5 x i8] c"asdf\00", [5 x i8] zeroinitializer]
11f4a2713aSLionel Sambuc 
12f4a2713aSLionel Sambuc // Double-implicit-conversions of array/functions (not legal C, but
13f4a2713aSLionel Sambuc // clang accepts it for gcc compat).
14f4a2713aSLionel Sambuc intptr_t b = a; // expected-warning {{incompatible pointer to integer conversion}}
15f4a2713aSLionel Sambuc int c();
16f4a2713aSLionel Sambuc void *d = c;
17f4a2713aSLionel Sambuc intptr_t e = c; // expected-warning {{incompatible pointer to integer conversion}}
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc int f, *g = __extension__ &f, *h = (1 != 1) ? &f : &f;
20f4a2713aSLionel Sambuc 
21f4a2713aSLionel Sambuc union s2 {
22f4a2713aSLionel Sambuc   struct {
23f4a2713aSLionel Sambuc     struct { } *f0;
24f4a2713aSLionel Sambuc   } f0;
25f4a2713aSLionel Sambuc };
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc int g0 = (int)(&(((union s2 *) 0)->f0.f0) - 0);
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc // CHECK: @g1x = global { double, double } { double 1.000000e+00{{[0]*}}, double 0.000000e+00{{[0]*}} }
30f4a2713aSLionel Sambuc _Complex double g1x = 1.0f;
31f4a2713aSLionel Sambuc // CHECK: @g1y = global { double, double } { double 0.000000e+00{{[0]*}}, double 1.000000e+00{{[0]*}} }
32f4a2713aSLionel Sambuc _Complex double g1y = 1.0fi;
33f4a2713aSLionel Sambuc // CHECK: @g1 = global { i8, i8 } { i8 1, i8 10 }
34f4a2713aSLionel Sambuc _Complex char g1 = (char) 1 + (char) 10 * 1i;
35f4a2713aSLionel Sambuc // CHECK: @g2 = global { i32, i32 } { i32 1, i32 10 }
36f4a2713aSLionel Sambuc _Complex int g2 = 1 + 10i;
37f4a2713aSLionel Sambuc // CHECK: @g3 = global { float, float } { float 1.000000e+00{{[0]*}}, float 1.000000e+0{{[0]*}}1 }
38f4a2713aSLionel Sambuc _Complex float g3 = 1.0 + 10.0i;
39f4a2713aSLionel Sambuc // CHECK: @g4 = global { double, double } { double 1.000000e+00{{[0]*}}, double 1.000000e+0{{[0]*}}1 }
40f4a2713aSLionel Sambuc _Complex double g4 = 1.0 + 10.0i;
41f4a2713aSLionel Sambuc // CHECK: @g5 = global { i32, i32 } zeroinitializer
42f4a2713aSLionel Sambuc _Complex int g5 = (2 + 3i) == (5 + 7i);
43f4a2713aSLionel Sambuc // CHECK: @g6 = global { double, double } { double -1.100000e+0{{[0]*}}1, double 2.900000e+0{{[0]*}}1 }
44f4a2713aSLionel Sambuc _Complex double g6 = (2.0 + 3.0i) * (5.0 + 7.0i);
45f4a2713aSLionel Sambuc // CHECK: @g7 = global i32 1
46f4a2713aSLionel Sambuc int g7 = (2 + 3i) * (5 + 7i) == (-11 + 29i);
47f4a2713aSLionel Sambuc // CHECK: @g8 = global i32 1
48f4a2713aSLionel Sambuc int g8 = (2.0 + 3.0i) * (5.0 + 7.0i) == (-11.0 + 29.0i);
49f4a2713aSLionel Sambuc // CHECK: @g9 = global i32 0
50f4a2713aSLionel Sambuc int g9 = (2 + 3i) * (5 + 7i) != (-11 + 29i);
51f4a2713aSLionel Sambuc // CHECK: @g10 = global i32 0
52f4a2713aSLionel Sambuc int g10 = (2.0 + 3.0i) * (5.0 + 7.0i) != (-11.0 + 29.0i);
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc // PR5108
55f4a2713aSLionel Sambuc // CHECK: @gv1 = global %struct.anon <{ i32 0, i8 7 }>, align 1
56f4a2713aSLionel Sambuc struct {
57f4a2713aSLionel Sambuc   unsigned long a;
58f4a2713aSLionel Sambuc   unsigned long b:3;
59f4a2713aSLionel Sambuc } __attribute__((__packed__)) gv1  = { .a = 0x0, .b = 7,  };
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc // PR5118
62f4a2713aSLionel Sambuc // CHECK: @gv2 = global %struct.anon.0 <{ i8 1, i8* null }>, align 1
63f4a2713aSLionel Sambuc struct {
64f4a2713aSLionel Sambuc   unsigned char a;
65f4a2713aSLionel Sambuc   char *b;
66f4a2713aSLionel Sambuc } __attribute__((__packed__)) gv2 = { 1, (void*)0 };
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc // Global references
69f4a2713aSLionel Sambuc // CHECK: @g11.l0 = internal global i32 ptrtoint (i32 ()* @g11 to i32)
g11()70f4a2713aSLionel Sambuc long g11() {
71f4a2713aSLionel Sambuc   static long l0 = (long) g11;
72f4a2713aSLionel Sambuc   return l0;
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc // CHECK: @g12 = global i32 ptrtoint (i8* @g12_tmp to i32)
76f4a2713aSLionel Sambuc static char g12_tmp;
77f4a2713aSLionel Sambuc long g12 = (long) &g12_tmp;
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc // CHECK: @g13 = global [1 x %struct.g13_s0] [%struct.g13_s0 { i32 ptrtoint (i8* @g12_tmp to i32) }]
80f4a2713aSLionel Sambuc struct g13_s0 {
81f4a2713aSLionel Sambuc    long a;
82f4a2713aSLionel Sambuc };
83f4a2713aSLionel Sambuc struct g13_s0 g13[] = {
84f4a2713aSLionel Sambuc    { (long) &g12_tmp }
85f4a2713aSLionel Sambuc };
86f4a2713aSLionel Sambuc 
87f4a2713aSLionel Sambuc // CHECK: @g14 = global i8* inttoptr (i64 100 to i8*)
88f4a2713aSLionel Sambuc void *g14 = (void*) 100;
89f4a2713aSLionel Sambuc 
90f4a2713aSLionel Sambuc // CHECK: @g15 = global i32 -1
91f4a2713aSLionel Sambuc int g15 = (int) (char) ((void*) 0 + 255);
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc // CHECK: @g16 = global i64 4294967295
94f4a2713aSLionel Sambuc long long g16 = (long long) ((void*) 0xFFFFFFFF);
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc // CHECK: @g17 = global i32* @g15
97f4a2713aSLionel Sambuc int *g17 = (int *) ((long) &g15);
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc // CHECK: @g18.p = internal global [1 x i32*] [i32* @g19]
g18(void)100f4a2713aSLionel Sambuc void g18(void) {
101f4a2713aSLionel Sambuc   extern int g19;
102f4a2713aSLionel Sambuc   static int *p[] = { &g19 };
103f4a2713aSLionel Sambuc }
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc // CHECK: @g20.l0 = internal global %struct.g20_s1 { %struct.g20_s0* null, %struct.g20_s0** getelementptr inbounds (%struct.g20_s1* @g20.l0, i32 0, i32 0) }
106f4a2713aSLionel Sambuc struct g20_s0;
107f4a2713aSLionel Sambuc struct g20_s1 {
108f4a2713aSLionel Sambuc   struct g20_s0 *f0, **f1;
109f4a2713aSLionel Sambuc };
g20(void)110f4a2713aSLionel Sambuc void *g20(void) {
111f4a2713aSLionel Sambuc   static struct g20_s1 l0 = { ((void*) 0), &l0.f0 };
112f4a2713aSLionel Sambuc   return l0.f1;
113f4a2713aSLionel Sambuc }
114f4a2713aSLionel Sambuc 
115f4a2713aSLionel Sambuc // PR4108
116f4a2713aSLionel Sambuc struct g21 {int g21;};
117f4a2713aSLionel Sambuc const struct g21 g21 = (struct g21){1};
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc // PR5474
120f4a2713aSLionel Sambuc struct g22 {int x;} __attribute((packed));
121f4a2713aSLionel Sambuc struct g23 {char a; short b; char c; struct g22 d;};
122f4a2713aSLionel Sambuc struct g23 g24 = {1,2,3,4};
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc // CHECK: @g25.g26 = internal global i8* getelementptr inbounds ([4 x i8]* @__func__.g25, i32 0, i32 0)
125f4a2713aSLionel Sambuc // CHECK: @__func__.g25 = private unnamed_addr constant [4 x i8] c"g25\00"
g25()126f4a2713aSLionel Sambuc int g25() {
127f4a2713aSLionel Sambuc   static const char *g26 = __func__;
128f4a2713aSLionel Sambuc   return *g26;
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc // CHECK: @g27.x = internal global i8* bitcast (i8** @g27.x to i8*), align 4
g27()132f4a2713aSLionel Sambuc void g27() { // PR8073
133f4a2713aSLionel Sambuc   static void *x = &x;
134f4a2713aSLionel Sambuc }
135f4a2713aSLionel Sambuc 
g28()136f4a2713aSLionel Sambuc void g28() {
137f4a2713aSLionel Sambuc   typedef long long v1i64 __attribute((vector_size(8)));
138f4a2713aSLionel Sambuc   typedef short v12i16 __attribute((vector_size(24)));
139f4a2713aSLionel Sambuc   typedef long double v2f80 __attribute((vector_size(24)));
140f4a2713aSLionel Sambuc   // CHECK: @g28.a = internal global <1 x i64> <i64 10>
141f4a2713aSLionel Sambuc   // CHECK: @g28.b = internal global <12 x i16> <i16 0, i16 0, i16 0, i16 -32768, i16 16383, i16 0, i16 0, i16 0, i16 0, i16 -32768, i16 16384, i16 0>
142f4a2713aSLionel Sambuc   // CHECK: @g28.c = internal global <2 x x86_fp80> <x86_fp80 0xK3FFF8000000000000000, x86_fp80 0xK40008000000000000000>, align 32
143f4a2713aSLionel Sambuc   static v1i64 a = (v1i64)10LL;
144f4a2713aSLionel Sambuc   static v12i16 b = (v2f80){1,2};
145f4a2713aSLionel Sambuc   static v2f80 c = (v12i16){0,0,0,-32768,16383,0,0,0,0,-32768,16384,0};
146f4a2713aSLionel Sambuc }
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc // PR13643
g29()149f4a2713aSLionel Sambuc void g29() {
150f4a2713aSLionel Sambuc   typedef char DCC_PASSWD[2];
151f4a2713aSLionel Sambuc   typedef struct
152f4a2713aSLionel Sambuc   {
153f4a2713aSLionel Sambuc       DCC_PASSWD passwd;
154f4a2713aSLionel Sambuc   } DCC_SRVR_NM;
155f4a2713aSLionel Sambuc   // CHECK: @g29.a = internal global %struct.DCC_SRVR_NM { [2 x i8] c"@\00" }, align 1
156f4a2713aSLionel Sambuc   // CHECK: @g29.b = internal global [1 x i32] [i32 ptrtoint ([5 x i8]* @.str to i32)], align 4
157f4a2713aSLionel Sambuc   // CHECK: @g29.c = internal global [1 x i32] [i32 97], align 4
158f4a2713aSLionel Sambuc   static DCC_SRVR_NM a = { {"@"} };
159f4a2713aSLionel Sambuc   static int b[1] = { "asdf" }; // expected-warning {{incompatible pointer to integer conversion initializing 'int' with an expression of type 'char [5]'}}
160f4a2713aSLionel Sambuc   static int c[1] = { L"a" };
161f4a2713aSLionel Sambuc }
162*0a6a1f1dSLionel Sambuc 
163*0a6a1f1dSLionel Sambuc // PR21300
g30()164*0a6a1f1dSLionel Sambuc void g30() {
165*0a6a1f1dSLionel Sambuc #pragma pack(1)
166*0a6a1f1dSLionel Sambuc   static struct {
167*0a6a1f1dSLionel Sambuc     int : 1;
168*0a6a1f1dSLionel Sambuc     int x;
169*0a6a1f1dSLionel Sambuc   } a = {};
170*0a6a1f1dSLionel Sambuc   // CHECK: @g30.a = internal global %struct.anon.1 <{ i8 undef, i32 0 }>, align 1
171*0a6a1f1dSLionel Sambuc #pragma pack()
172*0a6a1f1dSLionel Sambuc }
173*0a6a1f1dSLionel Sambuc 
g31()174*0a6a1f1dSLionel Sambuc void g31() {
175*0a6a1f1dSLionel Sambuc #pragma pack(4)
176*0a6a1f1dSLionel Sambuc   static struct {
177*0a6a1f1dSLionel Sambuc     short a;
178*0a6a1f1dSLionel Sambuc     long x;
179*0a6a1f1dSLionel Sambuc     short z;
180*0a6a1f1dSLionel Sambuc   } a = {23122, -12312731, -312};
181*0a6a1f1dSLionel Sambuc #pragma pack()
182*0a6a1f1dSLionel Sambuc   // CHECK: @g31.a = internal global %struct.anon.2 { i16 23122, i32 -12312731, i16 -312 }, align 4
183*0a6a1f1dSLionel Sambuc }
184