blob: 7e840f5622df1c209d14fd6e0f035a0fda6826fa [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 McCall49d26d22012-03-30 07:09:50 +00006// CHECK: @_ZZN5test31BC1EvE1u = internal global { i8, [3 x i8] } { i8 97, [3 x i8] undef }, align 4
John McCall78951942011-03-22 06:58:49 +00007// CHECK: @_ZZN5test1L6getvarEiE3var = internal constant [4 x i32] [i32 1, i32 0, i32 2, i32 4], align 16
John McCall8b242332010-05-25 04:30:21 +00008// CHECK: @_ZZ2h2vE1i = linkonce_odr global i32 0
9// CHECK: @_ZGVZ2h2vE1i = linkonce_odr global i64 0
Anders Carlssonf6b89a12010-02-07 02:03:08 +000010
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000011struct A {
12 A();
13 ~A();
14};
15
16void f() {
Eli Friedmaneb43f4a2011-09-13 22:21:56 +000017 // CHECK: load atomic i8* bitcast (i64* @_ZGVZ1fvE1a to i8*) acquire, align 1
Douglas Gregorcc6a44b2010-05-05 15:38:32 +000018 // CHECK: call i32 @__cxa_guard_acquire
19 // CHECK: call void @_ZN1AC1Ev
Anders Carlssonc7974ca2009-12-10 01:05:11 +000020 // 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 +000021 // CHECK: call void @__cxa_guard_release
Anders Carlsson3b2e16b2009-08-08 21:45:14 +000022 static A a;
23}
24
Anders Carlssonc7974ca2009-12-10 01:05:11 +000025void g() {
Nuno Lopesfc284482009-12-16 16:59:22 +000026 // CHECK: call noalias i8* @_Znwm(i64 1)
Anders Carlssonc7974ca2009-12-10 01:05:11 +000027 // CHECK: call void @_ZN1AC1Ev(
28 static A& a = *new A;
29}
Anders Carlsson071c8102010-01-26 04:02:23 +000030
31int a();
32void h() {
33 static const int i = a();
34}
Anders Carlssonf6b89a12010-02-07 02:03:08 +000035
36inline void h2() {
37 static int i = a();
38}
39
40void h3() {
41 h2();
42}
John McCalle65ce962010-05-03 21:39:56 +000043
44// PR6980: this shouldn't crash
45namespace test0 {
46 struct A { A(); };
47 __attribute__((noreturn)) int throw_exception();
48
49 void test() {
50 throw_exception();
51 static A r;
52 }
53}
John McCall8b242332010-05-25 04:30:21 +000054
55namespace test1 {
John McCall78951942011-03-22 06:58:49 +000056 // CHECK: define internal i32 @_ZN5test1L6getvarEi(
John McCall8b242332010-05-25 04:30:21 +000057 static inline int getvar(int index) {
58 static const int var[] = { 1, 0, 2, 4 };
59 return var[index];
60 }
61
62 void test() { (void) getvar(2); }
63}
Eli Friedman8b42ab72012-03-09 03:06:56 +000064
65// Make sure we emit the initializer correctly for the following:
66char base_req[] = { "foo" };
Eli Friedman71cba342012-03-09 03:27:46 +000067
68namespace union_static_local {
69 // CHECK: define internal void @_ZZN18union_static_local4testEvEN1c4mainEv
70 // 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"*))
71 union x { long double y; const char *x[2]; };
72 void f(union x*);
73 void test() {
74 static union x foo = { .x = { "a", "b" } };
75 struct c {
76 static void main() {
77 f(&foo);
78 }
79 };
80 c::main();
81 }
82}
John McCall9993cc72012-03-30 04:25:14 +000083
84// rdar://problem/11091093
85// Static variables should be consistent across constructor
86// or destructor variants.
87namespace test2 {
88 struct A {
89 A();
90 ~A();
91 };
92
93 struct B : virtual A {
94 B();
95 ~B();
96 };
97
98 // If we ever implement this as a delegate ctor call, just change
99 // this to take variadic arguments or something.
100 extern int foo();
101 B::B() {
102 static int x = foo();
103 }
104 // CHECK: define void @_ZN5test21BC1Ev
105 // CHECK: load atomic i8* bitcast (i64* @_ZGVZN5test21BC1EvE1x to i8*) acquire,
106 // CHECK: call i32 @__cxa_guard_acquire(i64* @_ZGVZN5test21BC1EvE1x)
107 // CHECK: [[T0:%.*]] = call i32 @_ZN5test23fooEv()
108 // CHECK: store i32 [[T0]], i32* @_ZZN5test21BC1EvE1x,
109 // CHECK: call void @__cxa_guard_release(i64* @_ZGVZN5test21BC1EvE1x)
110
111 // CHECK: 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 // This is just for completeness, because we actually emit this
119 // using a delegate dtor call.
120 B::~B() {
121 static int y = foo();
122 }
123 // CHECK: define void @_ZN5test21BD1Ev(
124 // CHECK: call void @_ZN5test21BD2Ev(
125
126 // CHECK: define void @_ZN5test21BD2Ev(
127 // CHECK: load atomic i8* bitcast (i64* @_ZGVZN5test21BD1EvE1y to i8*) acquire,
128 // CHECK: call i32 @__cxa_guard_acquire(i64* @_ZGVZN5test21BD1EvE1y)
129 // CHECK: [[T0:%.*]] = call i32 @_ZN5test23fooEv()
130 // CHECK: store i32 [[T0]], i32* @_ZZN5test21BD1EvE1y,
131 // CHECK: call void @__cxa_guard_release(i64* @_ZGVZN5test21BD1EvE1y)
132}
John McCall49d26d22012-03-30 07:09:50 +0000133
134// This shouldn't error out.
135namespace test3 {
136 struct A {
137 A();
138 ~A();
139 };
140
141 struct B : virtual A {
142 B();
143 ~B();
144 };
145
146 B::B() {
147 union U { char x; int i; };
148 static U u = { 'a' };
149 }
150 // CHECK: define void @_ZN5test31BC1Ev(
151 // CHECK: define void @_ZN5test31BC2Ev(
152}