[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CodeGen / const-init.c
blob175d221ad410a65dcea69bc092fc7e0062158ce9
1 // setting strict FP behaviour in the run line below tests that the compiler
2 // does the right thing for global compound literals (compoundliteral test)
3 // RUN: %clang_cc1 -triple i386-pc-linux-gnu -ffreestanding -Wno-pointer-to-int-cast -Wno-int-conversion -ffp-exception-behavior=strict -emit-llvm -o - %s | FileCheck %s
5 #include <stdint.h>
7 // Brace-enclosed string array initializers
8 char a[] = { "asdf" };
9 // CHECK: @a ={{.*}} global [5 x i8] c"asdf\00"
11 char a2[2][5] = { "asdf" };
12 // CHECK: @a2 ={{.*}} global [2 x [5 x i8]] {{\[}}[5 x i8] c"asdf\00", [5 x i8] zeroinitializer]
14 // Double-implicit-conversions of array/functions (not legal C, but
15 // clang accepts it for gcc compat).
16 intptr_t b = a;
17 int c(void);
18 void *d = c;
19 intptr_t e = c;
21 int f, *g = __extension__ &f, *h = (1 != 1) ? &f : &f;
23 union s2 {
24 struct {
25 struct { } *f0;
26 } f0;
29 int g0 = (int)(&(((union s2 *) 0)->f0.f0) - 0);
31 // CHECK: @g1x ={{.*}} global { double, double } { double 1.000000e+00{{[0]*}}, double 0.000000e+00{{[0]*}} }
32 _Complex double g1x = 1.0f;
33 // CHECK: @g1y ={{.*}} global { double, double } { double 0.000000e+00{{[0]*}}, double 1.000000e+00{{[0]*}} }
34 _Complex double g1y = 1.0fi;
35 // CHECK: @g1 ={{.*}} global { i8, i8 } { i8 1, i8 10 }
36 _Complex char g1 = (char) 1 + (char) 10 * 1i;
37 // CHECK: @g2 ={{.*}} global { i32, i32 } { i32 1, i32 10 }
38 _Complex int g2 = 1 + 10i;
39 // CHECK: @g3 ={{.*}} global { float, float } { float 1.000000e+00{{[0]*}}, float 1.000000e+0{{[0]*}}1 }
40 _Complex float g3 = 1.0 + 10.0i;
41 // CHECK: @g4 ={{.*}} global { double, double } { double 1.000000e+00{{[0]*}}, double 1.000000e+0{{[0]*}}1 }
42 _Complex double g4 = 1.0 + 10.0i;
43 // CHECK: @g5 ={{.*}} global { i32, i32 } zeroinitializer
44 _Complex int g5 = (2 + 3i) == (5 + 7i);
45 // CHECK: @g6 ={{.*}} global { double, double } { double -1.100000e+0{{[0]*}}1, double 2.900000e+0{{[0]*}}1 }
46 _Complex double g6 = (2.0 + 3.0i) * (5.0 + 7.0i);
47 // CHECK: @g7 ={{.*}} global i32 1
48 int g7 = (2 + 3i) * (5 + 7i) == (-11 + 29i);
49 // CHECK: @g8 ={{.*}} global i32 1
50 int g8 = (2.0 + 3.0i) * (5.0 + 7.0i) == (-11.0 + 29.0i);
51 // CHECK: @g9 ={{.*}} global i32 0
52 int g9 = (2 + 3i) * (5 + 7i) != (-11 + 29i);
53 // CHECK: @g10 ={{.*}} global i32 0
54 int g10 = (2.0 + 3.0i) * (5.0 + 7.0i) != (-11.0 + 29.0i);
56 // PR5108
57 // CHECK: @gv1 ={{.*}} global %struct.anon <{ i32 0, i8 7 }>, align 1
58 struct {
59 unsigned long a;
60 unsigned long b:3;
61 } __attribute__((__packed__)) gv1 = { .a = 0x0, .b = 7, };
63 // PR5118
64 // CHECK: @gv2 ={{.*}} global %struct.anon.0 <{ i8 1, ptr null }>, align 1
65 struct {
66 unsigned char a;
67 char *b;
68 } __attribute__((__packed__)) gv2 = { 1, (void*)0 };
70 // Global references
71 // CHECK: @g11.l0 = internal global i32 ptrtoint (ptr @g11 to i32)
72 long g11(void) {
73 static long l0 = (long) g11;
74 return l0;
77 // CHECK: @g12 ={{.*}} global i32 ptrtoint (ptr @g12_tmp to i32)
78 static char g12_tmp;
79 long g12 = (long) &g12_tmp;
81 // CHECK: @g13 ={{.*}} global [1 x %struct.g13_s0] [%struct.g13_s0 { i32 ptrtoint (ptr @g12_tmp to i32) }]
82 struct g13_s0 {
83 long a;
85 struct g13_s0 g13[] = {
86 { (long) &g12_tmp }
89 // CHECK: @g14 ={{.*}} global ptr inttoptr (i32 100 to ptr)
90 void *g14 = (void*) 100;
92 // CHECK: @g15 ={{.*}} global i32 -1
93 int g15 = (int) (char) ((void*) 0 + 255);
95 // CHECK: @g16 ={{.*}} global i64 4294967295
96 long long g16 = (long long) ((void*) 0xFFFFFFFF);
98 // CHECK: @g17 ={{.*}} global ptr @g15
99 int *g17 = (int *) ((long) &g15);
101 // CHECK: @g18.p = internal global [1 x ptr] [ptr @g19]
102 void g18(void) {
103 extern int g19;
104 static int *p[] = { &g19 };
107 // CHECK: @g20.l0 = internal global %struct.g20_s1 { ptr null, ptr @g20.l0 }
108 struct g20_s0;
109 struct g20_s1 {
110 struct g20_s0 *f0, **f1;
112 void *g20(void) {
113 static struct g20_s1 l0 = { ((void*) 0), &l0.f0 };
114 return l0.f1;
117 // PR4108
118 struct g21 {int g21;};
119 const struct g21 g21 = (struct g21){1};
121 // PR5474
122 struct g22 {int x;} __attribute((packed));
123 struct g23 {char a; short b; char c; struct g22 d;};
124 struct g23 g24 = {1,2,3,4};
126 // CHECK: @g25.g26 = internal global ptr @[[FUNC:.*]], align 4
127 // CHECK: @[[FUNC]] = private unnamed_addr constant [4 x i8] c"g25\00"
128 int g25(void) {
129 static const char *g26 = __func__;
130 return *g26;
133 // CHECK: @g27.x = internal global ptr @g27.x, align 4
134 void g27(void) { // PR8073
135 static void *x = &x;
138 void g28(void) {
139 typedef long long v1i64 __attribute((vector_size(8)));
140 typedef short v12i16 __attribute((vector_size(24)));
141 typedef long double v2f80 __attribute((vector_size(24)));
142 // CHECK: @g28.a = internal global <1 x i64> splat (i64 10)
143 // @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>
144 // @g28.c = internal global <2 x x86_fp80> <x86_fp80 0xK3FFF8000000000000000, x86_fp80 0xK40008000000000000000>, align 32
145 static v1i64 a = (v1i64)10LL;
146 //FIXME: support constant bitcast between vectors of x86_fp80
147 //static v12i16 b = (v12i16)(v2f80){1,2};
148 //static v2f80 c = (v2f80)(v12i16){0,0,0,-32768,16383,0,0,0,0,-32768,16384,0};
151 // PR13643
152 void g29(void) {
153 typedef char DCC_PASSWD[2];
154 typedef struct
156 DCC_PASSWD passwd;
157 } DCC_SRVR_NM;
158 // CHECK: @g29.a = internal global %struct.DCC_SRVR_NM { [2 x i8] c"@\00" }, align 1
159 // CHECK: @g29.b = internal global [1 x i32] [i32 ptrtoint (ptr @.str.1 to i32)], align 4
160 // CHECK: @g29.c = internal global [1 x i32] [i32 97], align 4
161 static DCC_SRVR_NM a = { {"@"} };
162 static int b[1] = { "asdf" };
163 static int c[1] = { L"a" };
166 // PR21300
167 void g30(void) {
168 #pragma pack(1)
169 static struct {
170 int : 1;
171 int x;
172 } a = {};
173 // CHECK: @g30.a = internal global %struct.anon.1 zeroinitializer, align 1
174 #pragma pack()
177 void g31(void) {
178 #pragma pack(4)
179 static struct {
180 short a;
181 long x;
182 short z;
183 } a = {23122, -12312731, -312};
184 #pragma pack()
185 // CHECK: @g31.a = internal global { i16, [2 x i8], i32, i16, [2 x i8] } { i16 23122, [2 x i8] zeroinitializer, i32 -12312731, i16 -312, [2 x i8] zeroinitializer }, align 4
188 // Clang should evaluate this in constant context, so floating point mode should
189 // have no effect.
190 // CHECK: @.compoundliteral = internal global [1 x float] [float 0x3FB99999A0000000], align 4
191 struct { const float *floats; } compoundliteral = {
192 (float[1]) { 0.1, },
195 struct PR4517_foo {
196 int x;
198 struct PR4517_bar {
199 struct PR4517_foo foo;
201 const struct PR4517_foo my_foo = {.x = 42};
202 struct PR4517_bar my_bar = {.foo = my_foo};
203 struct PR4517_bar my_bar2 = (struct PR4517_bar){.foo = my_foo};
204 struct PR4517_bar my_bar3 = {my_foo};
205 struct PR4517_bar my_bar4 = (struct PR4517_bar){my_foo};
206 // CHECK: @my_foo = constant %struct.PR4517_foo { i32 42 }, align 4
207 // CHECK: @my_bar = global %struct.PR4517_bar { %struct.PR4517_foo { i32 42 } }, align 4
208 // CHECK: @my_bar2 = global %struct.PR4517_bar { %struct.PR4517_foo { i32 42 } }, align 4
209 // CHECK: @my_bar3 = global %struct.PR4517_bar { %struct.PR4517_foo { i32 42 } }, align 4
210 // CHECK: @my_bar4 = global %struct.PR4517_bar { %struct.PR4517_foo { i32 42 } }, align 4
211 const int PR4517_arrc[2] = {41, 42};
212 int PR4517_x = PR4517_arrc[1];
213 const int PR4517_idx = 1;
214 int PR4517_x2 = PR4517_arrc[PR4517_idx];
215 // CHECK: @PR4517_arrc = constant [2 x i32] [i32 41, i32 42], align 4
216 // CHECK: @PR4517_x = global i32 42, align 4
217 // CHECK: @PR4517_idx = constant i32 1, align 4
218 // CHECK: @PR4517_x2 = global i32 42, align 4
220 // CHECK: @GH84784_inf = constant i8 1
221 _Bool const GH84784_inf = (1.0/0.0);