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