blob: 6ff9598afce16789ea8ebfde0b54c14ccd25cfbb [file] [log] [blame]
John McCall044cc542010-07-06 04:38:10 +00001// RUN: %clang_cc1 -triple=x86_64-apple-darwin10 -emit-llvm -fexceptions %s -o - |FileCheck %s
2// RUN: %clang_cc1 -triple=x86_64-apple-darwin10 -emit-llvm %s -o - |FileCheck -check-prefix NOEXC %s
Anders Carlsson89ed31d2009-08-08 23:24:23 +00003
4struct A {
5 A();
6 ~A();
7};
8
Anders Carlsson74d644a2009-10-08 17:28:59 +00009struct B { B(); ~B(); };
10
John McCallfb8b69a2010-02-02 08:02:49 +000011struct C { void *field; };
12
Anders Carlsson2ca4f632010-02-05 18:38:45 +000013struct D { ~D(); };
14
John McCallfb8b69a2010-02-02 08:02:49 +000015// CHECK: @c = global %struct.C zeroinitializer, align 8
16
John McCall6d311222010-08-12 07:31:42 +000017// It's okay if we ever implement the IR-generation optimization to remove this.
18// CHECK: @_ZN5test3L3varE = internal constant i8* getelementptr inbounds ([7 x i8]*
19
Anders Carlsson74d644a2009-10-08 17:28:59 +000020// CHECK: call void @_ZN1AC1Ev(%struct.A* @a)
Anders Carlssona3f36ab2009-10-08 17:22:47 +000021// CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.A*)* @_ZN1AD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.A* @a, i32 0, i32 0), i8* bitcast (i8** @__dso_handle to i8*))
Anders Carlsson89ed31d2009-08-08 23:24:23 +000022A a;
Anders Carlsson74d644a2009-10-08 17:28:59 +000023
24// CHECK: call void @_ZN1BC1Ev(%struct.A* @b)
25// CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.A*)* @_ZN1BD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.A* @b, i32 0, i32 0), i8* bitcast (i8** @__dso_handle to i8*))
26B b;
John McCallfb8b69a2010-02-02 08:02:49 +000027
28// PR6205: this should not require a global initializer
29// CHECK-NOT: call void @_ZN1CC1Ev(%struct.C* @c)
30C c;
31
Anders Carlsson2ca4f632010-02-05 18:38:45 +000032// CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.A*)* @_ZN1DD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.A* @d, i32 0, i32 0), i8* bitcast (i8** @__dso_handle to i8*))
33D d;
34
John McCallbf40cb52010-07-15 23:40:35 +000035// <rdar://problem/7458115>
36namespace test1 {
37 int f();
38 const int x = f(); // This has side-effects and gets emitted immediately.
39 const int y = x - 1; // This gets deferred.
40 const int z = ~y; // This also gets deferred, but gets "undeferred" before y.
41 int test() { return z; }
John McCall39dad532010-08-03 22:46:07 +000042// CHECK: define i32 @_ZN5test14testEv()
John McCall85aca0f2010-07-30 04:56:58 +000043
44 // All of these initializers end up delayed, so we check them later.
45}
46
47// <rdar://problem/8246444>
48namespace test2 {
49 struct allocator { allocator(); ~allocator(); };
50 struct A { A(const allocator &a = allocator()); ~A(); };
51
52 A a;
53// CHECK: call void @_ZN5test29allocatorC1Ev(
54// CHECK: invoke void @_ZN5test21AC1ERKNS_9allocatorE(
55// CHECK: call void @_ZN5test29allocatorD1Ev(
56// CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test21AD1Ev {{.*}} @_ZN5test21aE
57}
58
John McCall6d311222010-08-12 07:31:42 +000059namespace test3 {
60 // Tested at the beginning of the file.
61 const char * const var = "string";
62 extern const char * const var;
63
64 const char *test() { return var; }
65}
66
John McCallbf40cb52010-07-15 23:40:35 +000067// CHECK: define internal void [[TEST1_Z_INIT:@.*]]()
68// CHECK: load i32* @_ZN5test1L1yE
69// CHECK-NEXT: xor
70// CHECK-NEXT: store i32 {{.*}}, i32* @_ZN5test1L1zE
71// CHECK: define internal void [[TEST1_Y_INIT:@.*]]()
72// CHECK: load i32* @_ZN5test1L1xE
73// CHECK-NEXT: sub
74// CHECK-NEXT: store i32 {{.*}}, i32* @_ZN5test1L1yE
75
John McCall85aca0f2010-07-30 04:56:58 +000076// At the end of the file, we check that y is initialized before z.
John McCallbf40cb52010-07-15 23:40:35 +000077
Anders Carlsson9df792c2010-06-09 01:42:52 +000078// CHECK: define internal void @_GLOBAL__I_a() section "__TEXT,__StaticInit,regular,pure_instructions" {
John McCallbf40cb52010-07-15 23:40:35 +000079// CHECK: call void [[TEST1_Y_INIT]]
80// CHECK: call void [[TEST1_Z_INIT]]
Anders Carlsson9df792c2010-06-09 01:42:52 +000081
John McCall044cc542010-07-06 04:38:10 +000082// rdar://problem/8090834: this should be nounwind
83// CHECK-NOEXC: define internal void @_GLOBAL__I_a() nounwind section "__TEXT,__StaticInit,regular,pure_instructions" {