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