xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/static-init.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 %s -triple=x86_64-pc-linuxs -emit-llvm -o - | FileCheck %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc // CHECK: @_ZZ1hvE1i = internal global i32 0, align 4
4f4a2713aSLionel Sambuc // CHECK: @base_req = global [4 x i8] c"foo\00", align 1
5f4a2713aSLionel Sambuc // CHECK: @base_req_uchar = global [4 x i8] c"bar\00", align 1
6f4a2713aSLionel Sambuc 
7f4a2713aSLionel Sambuc // CHECK: @_ZZN5test31BC1EvE1u = internal global { i8, [3 x i8] } { i8 97, [3 x i8] undef }, align 4
8f4a2713aSLionel Sambuc // CHECK: @_ZZN5test1L6getvarEiE3var = internal constant [4 x i32] [i32 1, i32 0, i32 2, i32 4], align 16
9*0a6a1f1dSLionel Sambuc 
10f4a2713aSLionel Sambuc // CHECK: @_ZZ2h2vE1i = linkonce_odr global i32 0
11*0a6a1f1dSLionel Sambuc // CHECK-NOT: comdat
12f4a2713aSLionel Sambuc // CHECK: @_ZGVZ2h2vE1i = linkonce_odr global i64 0
13*0a6a1f1dSLionel Sambuc // CHECK-NOT: comdat
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc struct A {
16f4a2713aSLionel Sambuc   A();
17f4a2713aSLionel Sambuc   ~A();
18f4a2713aSLionel Sambuc };
19f4a2713aSLionel Sambuc 
f()20f4a2713aSLionel Sambuc void f() {
21f4a2713aSLionel Sambuc   // CHECK: load atomic i8* bitcast (i64* @_ZGVZ1fvE1a to i8*) acquire, align 1
22f4a2713aSLionel Sambuc   // CHECK: call i32 @__cxa_guard_acquire
23f4a2713aSLionel Sambuc   // CHECK: call void @_ZN1AC1Ev
24f4a2713aSLionel Sambuc   // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.A*)* @_ZN1AD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.A* @_ZZ1fvE1a, i32 0, i32 0), i8* @__dso_handle)
25f4a2713aSLionel Sambuc   // CHECK: call void @__cxa_guard_release
26f4a2713aSLionel Sambuc   static A a;
27f4a2713aSLionel Sambuc }
28f4a2713aSLionel Sambuc 
g()29f4a2713aSLionel Sambuc void g() {
30f4a2713aSLionel Sambuc   // CHECK: call noalias i8* @_Znwm(i64 1)
31f4a2713aSLionel Sambuc   // CHECK: call void @_ZN1AC1Ev(
32f4a2713aSLionel Sambuc   static A& a = *new A;
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc int a();
h()36f4a2713aSLionel Sambuc void h() {
37f4a2713aSLionel Sambuc   static const int i = a();
38f4a2713aSLionel Sambuc }
39f4a2713aSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc // CHECK: define linkonce_odr void @_Z2h2v()
41*0a6a1f1dSLionel Sambuc // CHECK-NOT: comdat
h2()42f4a2713aSLionel Sambuc inline void h2() {
43f4a2713aSLionel Sambuc   static int i = a();
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc 
h3()46f4a2713aSLionel Sambuc void h3() {
47f4a2713aSLionel Sambuc   h2();
48f4a2713aSLionel Sambuc }
49f4a2713aSLionel Sambuc 
50f4a2713aSLionel Sambuc // PR6980: this shouldn't crash
51f4a2713aSLionel Sambuc namespace test0 {
52f4a2713aSLionel Sambuc   struct A { A(); };
53f4a2713aSLionel Sambuc   __attribute__((noreturn)) int throw_exception();
54f4a2713aSLionel Sambuc 
test()55f4a2713aSLionel Sambuc   void test() {
56f4a2713aSLionel Sambuc     throw_exception();
57f4a2713aSLionel Sambuc     static A r;
58f4a2713aSLionel Sambuc   }
59f4a2713aSLionel Sambuc }
60f4a2713aSLionel Sambuc 
61f4a2713aSLionel Sambuc namespace test1 {
62f4a2713aSLionel Sambuc   // CHECK-LABEL: define internal i32 @_ZN5test1L6getvarEi(
getvar(int index)63f4a2713aSLionel Sambuc   static inline int getvar(int index) {
64f4a2713aSLionel Sambuc     static const int var[] = { 1, 0, 2, 4 };
65f4a2713aSLionel Sambuc     return var[index];
66f4a2713aSLionel Sambuc   }
67f4a2713aSLionel Sambuc 
test()68f4a2713aSLionel Sambuc   void test() { (void) getvar(2); }
69f4a2713aSLionel Sambuc }
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc // Make sure we emit the initializer correctly for the following:
72f4a2713aSLionel Sambuc char base_req[] = { "foo" };
73f4a2713aSLionel Sambuc unsigned char base_req_uchar[] = { "bar" };
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc namespace union_static_local {
76f4a2713aSLionel Sambuc   // CHECK-LABEL: define internal void @_ZZN18union_static_local4testEvEN1c4mainEv
77f4a2713aSLionel Sambuc   // CHECK: call void @_ZN18union_static_local1fEPNS_1xE(%"union.union_static_local::x"* bitcast ({ [2 x i8*] }* @_ZZN18union_static_local4testEvE3foo to %"union.union_static_local::x"*))
78f4a2713aSLionel Sambuc   union x { long double y; const char *x[2]; };
79f4a2713aSLionel Sambuc   void f(union x*);
test()80f4a2713aSLionel Sambuc   void test() {
81f4a2713aSLionel Sambuc     static union x foo = { .x = { "a", "b" } };
82f4a2713aSLionel Sambuc     struct c {
83f4a2713aSLionel Sambuc       static void main() {
84f4a2713aSLionel Sambuc         f(&foo);
85f4a2713aSLionel Sambuc       }
86f4a2713aSLionel Sambuc     };
87f4a2713aSLionel Sambuc     c::main();
88f4a2713aSLionel Sambuc   }
89f4a2713aSLionel Sambuc }
90f4a2713aSLionel Sambuc 
91f4a2713aSLionel Sambuc // rdar://problem/11091093
92f4a2713aSLionel Sambuc //   Static variables should be consistent across constructor
93f4a2713aSLionel Sambuc //   or destructor variants.
94f4a2713aSLionel Sambuc namespace test2 {
95f4a2713aSLionel Sambuc   struct A {
96f4a2713aSLionel Sambuc     A();
97f4a2713aSLionel Sambuc     ~A();
98f4a2713aSLionel Sambuc   };
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc   struct B : virtual A {
101f4a2713aSLionel Sambuc     B();
102f4a2713aSLionel Sambuc     ~B();
103f4a2713aSLionel Sambuc   };
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc   // If we ever implement this as a delegate ctor call, just change
106f4a2713aSLionel Sambuc   // this to take variadic arguments or something.
107f4a2713aSLionel Sambuc   extern int foo();
B()108f4a2713aSLionel Sambuc   B::B() {
109f4a2713aSLionel Sambuc     static int x = foo();
110f4a2713aSLionel Sambuc   }
111*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: define void @_ZN5test21BC2Ev
112f4a2713aSLionel Sambuc   // CHECK:   load atomic i8* bitcast (i64* @_ZGVZN5test21BC1EvE1x to i8*) acquire,
113f4a2713aSLionel Sambuc   // CHECK:   call i32 @__cxa_guard_acquire(i64* @_ZGVZN5test21BC1EvE1x)
114f4a2713aSLionel Sambuc   // CHECK:   [[T0:%.*]] = call i32 @_ZN5test23fooEv()
115f4a2713aSLionel Sambuc   // CHECK:   store i32 [[T0]], i32* @_ZZN5test21BC1EvE1x,
116f4a2713aSLionel Sambuc   // CHECK:   call void @__cxa_guard_release(i64* @_ZGVZN5test21BC1EvE1x)
117f4a2713aSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: define void @_ZN5test21BC1Ev
119f4a2713aSLionel Sambuc   // CHECK:   load atomic i8* bitcast (i64* @_ZGVZN5test21BC1EvE1x to i8*) acquire,
120f4a2713aSLionel Sambuc   // CHECK:   call i32 @__cxa_guard_acquire(i64* @_ZGVZN5test21BC1EvE1x)
121f4a2713aSLionel Sambuc   // CHECK:   [[T0:%.*]] = call i32 @_ZN5test23fooEv()
122f4a2713aSLionel Sambuc   // CHECK:   store i32 [[T0]], i32* @_ZZN5test21BC1EvE1x,
123f4a2713aSLionel Sambuc   // CHECK:   call void @__cxa_guard_release(i64* @_ZGVZN5test21BC1EvE1x)
124f4a2713aSLionel Sambuc 
125f4a2713aSLionel Sambuc   // This is just for completeness, because we actually emit this
126f4a2713aSLionel Sambuc   // using a delegate dtor call.
~B()127f4a2713aSLionel Sambuc   B::~B() {
128f4a2713aSLionel Sambuc     static int y = foo();
129f4a2713aSLionel Sambuc   }
130f4a2713aSLionel Sambuc   // CHECK-LABEL: define void @_ZN5test21BD2Ev(
131f4a2713aSLionel Sambuc   // CHECK:   load atomic i8* bitcast (i64* @_ZGVZN5test21BD1EvE1y to i8*) acquire,
132f4a2713aSLionel Sambuc   // CHECK:   call i32 @__cxa_guard_acquire(i64* @_ZGVZN5test21BD1EvE1y)
133f4a2713aSLionel Sambuc   // CHECK:   [[T0:%.*]] = call i32 @_ZN5test23fooEv()
134f4a2713aSLionel Sambuc   // CHECK:   store i32 [[T0]], i32* @_ZZN5test21BD1EvE1y,
135f4a2713aSLionel Sambuc   // CHECK:   call void @__cxa_guard_release(i64* @_ZGVZN5test21BD1EvE1y)
136*0a6a1f1dSLionel Sambuc 
137*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: define void @_ZN5test21BD1Ev(
138*0a6a1f1dSLionel Sambuc   // CHECK:   call void @_ZN5test21BD2Ev(
139f4a2713aSLionel Sambuc }
140f4a2713aSLionel Sambuc 
141f4a2713aSLionel Sambuc // This shouldn't error out.
142f4a2713aSLionel Sambuc namespace test3 {
143f4a2713aSLionel Sambuc   struct A {
144f4a2713aSLionel Sambuc     A();
145f4a2713aSLionel Sambuc     ~A();
146f4a2713aSLionel Sambuc   };
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc   struct B : virtual A {
149f4a2713aSLionel Sambuc     B();
150f4a2713aSLionel Sambuc     ~B();
151f4a2713aSLionel Sambuc   };
152f4a2713aSLionel Sambuc 
B()153f4a2713aSLionel Sambuc   B::B() {
154f4a2713aSLionel Sambuc     union U { char x; int i; };
155f4a2713aSLionel Sambuc     static U u = { 'a' };
156f4a2713aSLionel Sambuc   }
157f4a2713aSLionel Sambuc   // CHECK-LABEL: define void @_ZN5test31BC2Ev(
158*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: define void @_ZN5test31BC1Ev(
159f4a2713aSLionel Sambuc }
160