blob: ca47428fe2c6f2f389ee375ac91bfa718b634f0f [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
Anders Carlsson071c8102010-01-26 04:02:23 +00002
3// CHECK: @_ZZ1hvE1i = internal global i32 0, align 4
Eli Friedman8b42ab72012-03-09 03:06:56 +00004// CHECK: @base_req = global [4 x i8] c"foo\00", align 1
Anders Carlssonf6b89a12010-02-07 02:03:08 +00005
John McCall78951942011-03-22 06:58:49 +00006// CHECK: @_ZZN5test1L6getvarEiE3var = internal constant [4 x i32] [i32 1, i32 0, i32 2, i32 4], align 16
John McCall8b242332010-05-25 04:30:21 +00007// CHECK: @_ZZ2h2vE1i = linkonce_odr global i32 0
8// CHECK: @_ZGVZ2h2vE1i = linkonce_odr global i64 0
Anders Carlssonf6b89a12010-02-07 02:03:08 +00009
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000010struct A {
11 A();
12 ~A();
13};
14
15void f() {
Eli Friedmaneb43f4a2011-09-13 22:21:56 +000016 // CHECK: load atomic i8* bitcast (i64* @_ZGVZ1fvE1a to i8*) acquire, align 1
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000017 // CHECK: call i32 @__cxa_guard_acquire
18 // CHECK: call void @_ZN1AC1Ev
Anders Carlssonc7974ca2009-12-10 01:05:11 +000019 // 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* bitcast (i8** @__dso_handle to i8*))
John McCall5cd91b52010-09-08 01:44:27 +000020 // CHECK: call void @__cxa_guard_release
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000021 static A a;
22}
23
Anders Carlssonc7974ca2009-12-10 01:05:11 +000024void g() {
Nuno Lopesfc284482009-12-16 16:59:22 +000025 // CHECK: call noalias i8* @_Znwm(i64 1)
Anders Carlssonc7974ca2009-12-10 01:05:11 +000026 // CHECK: call void @_ZN1AC1Ev(
27 static A& a = *new A;
28}
Anders Carlsson071c8102010-01-26 04:02:23 +000029
30int a();
31void h() {
32 static const int i = a();
33}
Anders Carlssonf6b89a12010-02-07 02:03:08 +000034
35inline void h2() {
36 static int i = a();
37}
38
39void h3() {
40 h2();
41}
John McCalle65ce962010-05-03 21:39:56 +000042
43// PR6980: this shouldn't crash
44namespace test0 {
45 struct A { A(); };
46 __attribute__((noreturn)) int throw_exception();
47
48 void test() {
49 throw_exception();
50 static A r;
51 }
52}
John McCall8b242332010-05-25 04:30:21 +000053
54namespace test1 {
John McCall78951942011-03-22 06:58:49 +000055 // CHECK: define internal i32 @_ZN5test1L6getvarEi(
John McCall8b242332010-05-25 04:30:21 +000056 static inline int getvar(int index) {
57 static const int var[] = { 1, 0, 2, 4 };
58 return var[index];
59 }
60
61 void test() { (void) getvar(2); }
62}
Eli Friedman8b42ab72012-03-09 03:06:56 +000063
64// Make sure we emit the initializer correctly for the following:
65char base_req[] = { "foo" };
Eli Friedman71cba342012-03-09 03:27:46 +000066
67namespace union_static_local {
68 // CHECK: define internal void @_ZZN18union_static_local4testEvEN1c4mainEv
69 // 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"*))
70 union x { long double y; const char *x[2]; };
71 void f(union x*);
72 void test() {
73 static union x foo = { .x = { "a", "b" } };
74 struct c {
75 static void main() {
76 f(&foo);
77 }
78 };
79 c::main();
80 }
81}
John McCall9993cc72012-03-30 04:25:14 +000082
83// rdar://problem/11091093
84// Static variables should be consistent across constructor
85// or destructor variants.
86namespace test2 {
87 struct A {
88 A();
89 ~A();
90 };
91
92 struct B : virtual A {
93 B();
94 ~B();
95 };
96
97 // If we ever implement this as a delegate ctor call, just change
98 // this to take variadic arguments or something.
99 extern int foo();
100 B::B() {
101 static int x = foo();
102 }
103 // CHECK: define void @_ZN5test21BC1Ev
104 // CHECK: load atomic i8* bitcast (i64* @_ZGVZN5test21BC1EvE1x to i8*) acquire,
105 // CHECK: call i32 @__cxa_guard_acquire(i64* @_ZGVZN5test21BC1EvE1x)
106 // CHECK: [[T0:%.*]] = call i32 @_ZN5test23fooEv()
107 // CHECK: store i32 [[T0]], i32* @_ZZN5test21BC1EvE1x,
108 // CHECK: call void @__cxa_guard_release(i64* @_ZGVZN5test21BC1EvE1x)
109
110 // CHECK: define void @_ZN5test21BC2Ev
111 // CHECK: load atomic i8* bitcast (i64* @_ZGVZN5test21BC1EvE1x to i8*) acquire,
112 // CHECK: call i32 @__cxa_guard_acquire(i64* @_ZGVZN5test21BC1EvE1x)
113 // CHECK: [[T0:%.*]] = call i32 @_ZN5test23fooEv()
114 // CHECK: store i32 [[T0]], i32* @_ZZN5test21BC1EvE1x,
115 // CHECK: call void @__cxa_guard_release(i64* @_ZGVZN5test21BC1EvE1x)
116
117 // This is just for completeness, because we actually emit this
118 // using a delegate dtor call.
119 B::~B() {
120 static int y = foo();
121 }
122 // CHECK: define void @_ZN5test21BD1Ev(
123 // CHECK: call void @_ZN5test21BD2Ev(
124
125 // CHECK: define void @_ZN5test21BD2Ev(
126 // CHECK: load atomic i8* bitcast (i64* @_ZGVZN5test21BD1EvE1y to i8*) acquire,
127 // CHECK: call i32 @__cxa_guard_acquire(i64* @_ZGVZN5test21BD1EvE1y)
128 // CHECK: [[T0:%.*]] = call i32 @_ZN5test23fooEv()
129 // CHECK: store i32 [[T0]], i32* @_ZZN5test21BD1EvE1y,
130 // CHECK: call void @__cxa_guard_release(i64* @_ZGVZN5test21BD1EvE1y)
131}