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