1// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -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// CHECK: @_ZZ2h2vE1i = linkonce_odr global i32 0
10// CHECK: @_ZGVZ2h2vE1i = linkonce_odr global i64 0
11
12struct A {
13  A();
14  ~A();
15};
16
17void f() {
18  // CHECK: load atomic i8* bitcast (i64* @_ZGVZ1fvE1a to i8*) acquire, align 1
19  // CHECK: call i32 @__cxa_guard_acquire
20  // CHECK: call void @_ZN1AC1Ev
21  // 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)
22  // CHECK: call void @__cxa_guard_release
23  static A a;
24}
25
26void g() {
27  // CHECK: call noalias i8* @_Znwm(i64 1)
28  // CHECK: call void @_ZN1AC1Ev(
29  static A& a = *new A;
30}
31
32int a();
33void h() {
34  static const int i = a();
35}
36
37inline void h2() {
38  static int i = a();
39}
40
41void h3() {
42  h2();
43}
44
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}
55
56namespace test1 {
57  // CHECK-LABEL: define internal i32 @_ZN5test1L6getvarEi(
58  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}
65
66// Make sure we emit the initializer correctly for the following:
67char base_req[] = { "foo" };
68unsigned char base_req_uchar[] = { "bar" };
69
70namespace union_static_local {
71  // CHECK-LABEL: 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}
85
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-LABEL: define void @_ZN5test21BC2Ev
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-LABEL: define void @_ZN5test21BC1Ev
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-LABEL: 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
132  // CHECK-LABEL: define void @_ZN5test21BD1Ev(
133  // CHECK:   call void @_ZN5test21BD2Ev(
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-LABEL: define void @_ZN5test31BC2Ev(
153  // CHECK-LABEL: define void @_ZN5test31BC1Ev(
154}
155