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