blob: 9bd7390d076ceedd84dd661368d56757957156d5 [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
Rafael Espindolac532b502011-01-18 21:07:57 +000015// CHECK: @__dso_handle = external unnamed_addr global i8*
John McCallfb8b69a2010-02-02 08:02:49 +000016// CHECK: @c = global %struct.C zeroinitializer, align 8
17
John McCall6d311222010-08-12 07:31:42 +000018// It's okay if we ever implement the IR-generation optimization to remove this.
19// CHECK: @_ZN5test3L3varE = internal constant i8* getelementptr inbounds ([7 x i8]*
20
Anders Carlsson5c5a7642010-10-31 20:41:46 +000021// PR6205: The casts should not require global initializers
22// CHECK: @_ZN6PR59741cE = external global %"struct.PR5974::C"
Anders Carlssoneb9d81d2011-04-17 21:56:13 +000023// CHECK: @_ZN6PR59741aE = global %"struct.PR5974::A"* getelementptr inbounds (%"struct.PR5974::C"* @_ZN6PR59741cE, i32 0, i32 0)
24// CHECK: @_ZN6PR59741bE = global %"struct.PR5974::A"* bitcast (i8* getelementptr (i8* bitcast (%"struct.PR5974::C"* @_ZN6PR59741cE to i8*), i64 4) to %"struct.PR5974::A"*), align 8
Anders Carlsson5c5a7642010-10-31 20:41:46 +000025
Anders Carlsson74d644a2009-10-08 17:28:59 +000026// CHECK: call void @_ZN1AC1Ev(%struct.A* @a)
Anders Carlssona3f36ab2009-10-08 17:22:47 +000027// 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 +000028A a;
Anders Carlsson74d644a2009-10-08 17:28:59 +000029
30// CHECK: call void @_ZN1BC1Ev(%struct.A* @b)
31// 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*))
32B b;
John McCallfb8b69a2010-02-02 08:02:49 +000033
34// PR6205: this should not require a global initializer
35// CHECK-NOT: call void @_ZN1CC1Ev(%struct.C* @c)
36C c;
37
Anders Carlsson2ca4f632010-02-05 18:38:45 +000038// 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*))
39D d;
40
John McCallbf40cb52010-07-15 23:40:35 +000041// <rdar://problem/7458115>
42namespace test1 {
43 int f();
44 const int x = f(); // This has side-effects and gets emitted immediately.
45 const int y = x - 1; // This gets deferred.
46 const int z = ~y; // This also gets deferred, but gets "undeferred" before y.
47 int test() { return z; }
John McCall39dad532010-08-03 22:46:07 +000048// CHECK: define i32 @_ZN5test14testEv()
John McCall85aca0f2010-07-30 04:56:58 +000049
50 // All of these initializers end up delayed, so we check them later.
51}
52
53// <rdar://problem/8246444>
54namespace test2 {
55 struct allocator { allocator(); ~allocator(); };
56 struct A { A(const allocator &a = allocator()); ~A(); };
57
58 A a;
59// CHECK: call void @_ZN5test29allocatorC1Ev(
60// CHECK: invoke void @_ZN5test21AC1ERKNS_9allocatorE(
61// CHECK: call void @_ZN5test29allocatorD1Ev(
62// CHECK: call i32 @__cxa_atexit({{.*}} @_ZN5test21AD1Ev {{.*}} @_ZN5test21aE
63}
64
John McCall6d311222010-08-12 07:31:42 +000065namespace test3 {
66 // Tested at the beginning of the file.
67 const char * const var = "string";
68 extern const char * const var;
69
70 const char *test() { return var; }
71}
72
Anders Carlsson5c5a7642010-10-31 20:41:46 +000073namespace PR5974 {
74 struct A { int a; };
75 struct B { int b; };
76 struct C : A, B { int c; };
77
78 extern C c;
79
80 // These should not require global initializers.
81 A* a = &c;
82 B* b = &c;
83}
John McCallbf40cb52010-07-15 23:40:35 +000084// CHECK: define internal void [[TEST1_Z_INIT:@.*]]()
85// CHECK: load i32* @_ZN5test1L1yE
86// CHECK-NEXT: xor
87// CHECK-NEXT: store i32 {{.*}}, i32* @_ZN5test1L1zE
88// CHECK: define internal void [[TEST1_Y_INIT:@.*]]()
89// CHECK: load i32* @_ZN5test1L1xE
90// CHECK-NEXT: sub
91// CHECK-NEXT: store i32 {{.*}}, i32* @_ZN5test1L1yE
92
John McCall26fbc722011-04-12 01:01:22 +000093// PR9570: the indirect field shouldn't crash IR gen.
94namespace test5 {
John McCall1d1a6792011-04-12 01:15:45 +000095 static union {
John McCall26fbc722011-04-12 01:01:22 +000096 unsigned bar[4096] __attribute__((aligned(128)));
97 };
98}
99
John McCall99ace162011-04-12 01:46:54 +0000100namespace test6 {
101 struct A {
102 A();
103 };
104 extern int foo();
105
106 // This needs an initialization function but not guard variables.
107 __attribute__((weak)) int x = foo();
108}
109
John McCall85aca0f2010-07-30 04:56:58 +0000110// At the end of the file, we check that y is initialized before z.
John McCallbf40cb52010-07-15 23:40:35 +0000111
Anders Carlsson9df792c2010-06-09 01:42:52 +0000112// CHECK: define internal void @_GLOBAL__I_a() section "__TEXT,__StaticInit,regular,pure_instructions" {
John McCallbf40cb52010-07-15 23:40:35 +0000113// CHECK: call void [[TEST1_Y_INIT]]
114// CHECK: call void [[TEST1_Z_INIT]]
Anders Carlsson9df792c2010-06-09 01:42:52 +0000115
John McCall044cc542010-07-06 04:38:10 +0000116// rdar://problem/8090834: this should be nounwind
117// CHECK-NOEXC: define internal void @_GLOBAL__I_a() nounwind section "__TEXT,__StaticInit,regular,pure_instructions" {