blob: 1ae9eada78cc104ae3ce6ab7bb31d4455f91c733 [file] [log] [blame]
Rafael Espindola191b9512014-03-05 21:04:41 +00001// RUN: %clang_cc1 %s -triple i686-linux -emit-llvm -o - -mconstructor-aliases | FileCheck --check-prefix=NOOPT %s
Rafael Espindola3f643bd2013-11-04 18:38:59 +00002
Rafael Espindolaf7765ac2014-08-30 00:15:37 +00003// RUN: %clang_cc1 %s -triple i686-linux -emit-llvm -o - -mconstructor-aliases -O1 -disable-llvm-optzns > %t
4// RUN: FileCheck --check-prefix=CHECK1 --input-file=%t %s
5// RUN: FileCheck --check-prefix=CHECK2 --input-file=%t %s
6// RUN: FileCheck --check-prefix=CHECK3 --input-file=%t %s
7// RUN: FileCheck --check-prefix=CHECK4 --input-file=%t %s
8// RUN: FileCheck --check-prefix=CHECK5 --input-file=%t %s
Rafael Espindola1e4df922014-09-16 15:18:21 +00009// RUN: FileCheck --check-prefix=CHECK6 --input-file=%t %s
Joerg Sonnenberger374c2bb2013-11-22 21:34:35 +000010
Rafael Espindola0806f982014-09-16 20:19:43 +000011// RUN: %clang_cc1 %s -triple i686-pc-windows-gnu -emit-llvm -o - -mconstructor-aliases -O1 -disable-llvm-optzns | FileCheck --check-prefix=COFF %s
12
Rafael Espindola3f643bd2013-11-04 18:38:59 +000013namespace test1 {
Rafael Espindola1e4df922014-09-16 15:18:21 +000014// Test that we produce the apropriate comdats when creating aliases to
15// weak_odr constructors and destructors.
Rafael Espindola3f643bd2013-11-04 18:38:59 +000016
Rafael Espindola1e4df922014-09-16 15:18:21 +000017// CHECK1: @_ZN5test16foobarIvEC1Ev = weak_odr alias void {{.*}} @_ZN5test16foobarIvEC2Ev
18// CHECK1: @_ZN5test16foobarIvED1Ev = weak_odr alias void (%"struct.test1::foobar"*)* @_ZN5test16foobarIvED2Ev
19// CHECK1: define weak_odr void @_ZN5test16foobarIvEC2Ev({{.*}} comdat $_ZN5test16foobarIvEC5Ev
20// CHECK1: define weak_odr void @_ZN5test16foobarIvED2Ev({{.*}} comdat $_ZN5test16foobarIvED5Ev
21// CHECK1: define weak_odr void @_ZN5test16foobarIvED0Ev({{.*}} comdat $_ZN5test16foobarIvED5Ev
22// CHECK1-NOT: comdat
Rafael Espindola129d3132013-11-12 22:06:46 +000023
Rafael Espindola0806f982014-09-16 20:19:43 +000024// COFF doesn't support comdats with arbitrary names (C5/D5).
25// COFF-NOT: comdat
26
Rafael Espindola1e4df922014-09-16 15:18:21 +000027template <typename T>
28struct foobar {
Rafael Espindola3f643bd2013-11-04 18:38:59 +000029 foobar() {}
Rafael Espindola1e4df922014-09-16 15:18:21 +000030 virtual ~foobar() {}
Rafael Espindola3f643bd2013-11-04 18:38:59 +000031};
32
33template struct foobar<void>;
34}
35
36namespace test2 {
Rafael Espindola2e2995b2013-11-05 21:37:29 +000037// test that when the destrucor is linkonce_odr we just replace every use of
38// C1 with C2.
Rafael Espindola3f643bd2013-11-04 18:38:59 +000039
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000040// CHECK1: define internal void @__cxx_global_var_init()
41// CHECK1: call void @_ZN5test26foobarIvEC2Ev
42// CHECK1: define linkonce_odr void @_ZN5test26foobarIvEC2Ev(
Rafael Espindola3f643bd2013-11-04 18:38:59 +000043void g();
44template <typename T> struct foobar {
45 foobar() { g(); }
46};
47foobar<void> x;
48}
49
50namespace test3 {
Rafael Espindola23d37512013-11-08 23:46:20 +000051// test that instead of an internal alias we just use the other destructor
52// directly.
Rafael Espindola3f643bd2013-11-04 18:38:59 +000053
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000054// CHECK1: define internal void @__cxx_global_var_init1()
55// CHECK1: call i32 @__cxa_atexit{{.*}}_ZN5test312_GLOBAL__N_11AD2Ev
56// CHECK1: define internal void @_ZN5test312_GLOBAL__N_11AD2Ev(
Rafael Espindola3f643bd2013-11-04 18:38:59 +000057namespace {
58struct A {
59 ~A() {}
60};
61
62struct B : public A {};
63}
64
65B x;
66}
67
68namespace test4 {
69 // Test that we don't produce aliases from B to A. We cannot because we cannot
Rafael Espindolae2ec6fa2013-11-08 22:59:46 +000070 // guarantee that they will be present in every TU. Instead, we just call
71 // A's destructor directly.
Rafael Espindola3f643bd2013-11-04 18:38:59 +000072
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000073 // CHECK1: define internal void @__cxx_global_var_init2()
74 // CHECK1: call i32 @__cxa_atexit{{.*}}_ZN5test41AD2Ev
75 // CHECK1: define linkonce_odr void @_ZN5test41AD2Ev(
Rafael Espindolad967bad2013-11-13 23:20:45 +000076
77 // test that we don't do this optimization at -O0 so that the debugger can
78 // see both destructors.
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000079 // NOOPT: define internal void @__cxx_global_var_init2()
80 // NOOPT: call i32 @__cxa_atexit{{.*}}@_ZN5test41BD2Ev
81 // NOOPT: define linkonce_odr void @_ZN5test41BD2Ev
Rafael Espindola3f643bd2013-11-04 18:38:59 +000082 struct A {
83 virtual ~A() {}
84 };
85 struct B : public A{
86 ~B() {}
87 };
88 B X;
89}
Rafael Espindola23d37512013-11-08 23:46:20 +000090
91namespace test5 {
92 // similar to test4, but with an internal B.
93
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000094 // CHECK2: define internal void @__cxx_global_var_init3()
95 // CHECK2: call i32 @__cxa_atexit{{.*}}_ZN5test51AD2Ev
96 // CHECK2: define linkonce_odr void @_ZN5test51AD2Ev(
Rafael Espindola23d37512013-11-08 23:46:20 +000097 struct A {
98 virtual ~A() {}
99 };
100 namespace {
101 struct B : public A{
102 ~B() {}
103 };
104 }
105 B X;
106}
Rafael Espindola961ba212013-11-09 01:57:21 +0000107
108namespace test6 {
109 // Test that we use ~A directly, even when ~A is not defined. The symbol for
110 // ~B would have been internal and still contain a reference to ~A.
111 struct A {
112 virtual ~A();
113 };
114 namespace {
115 struct B : public A {
116 ~B() {}
117 };
118 }
119 B X;
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000120 // CHECK3: define internal void @__cxx_global_var_init4()
121 // CHECK3: call i32 @__cxa_atexit({{.*}}@_ZN5test61AD2Ev
Rafael Espindola961ba212013-11-09 01:57:21 +0000122}
Rafael Espindolab15683e2013-11-11 19:35:06 +0000123
124namespace test7 {
125 // Test that we don't produce an alias from ~B to ~A<int> (or crash figuring
126 // out if we should).
127 // pr17875.
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000128 // CHECK3: define void @_ZN5test71BD2Ev
Rafael Espindolab15683e2013-11-11 19:35:06 +0000129 template <typename> struct A {
130 ~A() {}
131 };
132 class B : A<int> {
133 ~B();
134 };
135 template class A<int>;
136 B::~B() {}
137}
Rafael Espindola50e1f002013-11-11 22:55:13 +0000138
139namespace test8 {
140 // Test that we replace ~zed with ~bar which is an alias to ~foo.
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000141 // CHECK4: @_ZN5test83barD2Ev = alias {{.*}} @_ZN5test83fooD2Ev
142 // CHECK4: define internal void @__cxx_global_var_init5()
143 // CHECK4: call i32 @__cxa_atexit({{.*}}@_ZN5test83barD2Ev
Rafael Espindola50e1f002013-11-11 22:55:13 +0000144 struct foo {
145 ~foo();
146 };
147 foo::~foo() {}
148 struct bar : public foo {
149 ~bar();
150 };
151 bar::~bar() {}
152 struct zed : public bar {};
153 zed foo;
154}
Joerg Sonnenberger374c2bb2013-11-22 21:34:35 +0000155
Rafael Espindola191b9512014-03-05 21:04:41 +0000156namespace test9 {
157struct foo {
158 __attribute__((stdcall)) ~foo() {
159 }
160};
161
162struct bar : public foo {};
163
164void zed() {
165 // Test that we produce a call to bar's destructor. We used to call foo's, but
166 // it has a different calling conversion.
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000167 // CHECK4: call void @_ZN5test93barD2Ev
Rafael Espindola191b9512014-03-05 21:04:41 +0000168 bar ptr;
169}
170}
171
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000172// CHECK5: @_ZTV1C = linkonce_odr unnamed_addr constant [4 x i8*] [{{[^@]*}}@_ZTI1C {{[^@]*}}@_ZN1CD2Ev {{[^@]*}}@_ZN1CD0Ev {{[^@]*}}]
Joerg Sonnenberger374c2bb2013-11-22 21:34:35 +0000173// r194296 replaced C::~C with B::~B without emitting the later.
174
175class A {
176public:
177 A(int);
178 virtual ~A();
179};
180
181template <class>
182class B : A {
183public:
184 B()
185 : A(0) {
186 }
187 __attribute__((always_inline)) ~B() {
188 }
189};
190
191extern template class B<char>;
192
193class C : B<char> {
194};
195
196void
197fn1() {
198 new C;
199}
Rafael Espindola1e4df922014-09-16 15:18:21 +0000200
201namespace test10 {
202// Test that if a destructor is in a comdat, we don't try to emit is as an
203// alias to a base class destructor.
204struct bar {
205 ~bar();
206};
207bar::~bar() {
208}
209} // closing the namespace causes ~bar to be sent to CodeGen
210namespace test10 {
211template <typename T>
212struct foo : public bar {
213 ~foo();
214};
215template <typename T>
216foo<T>::~foo() {}
217template class foo<int>;
218// CHECK5: define weak_odr void @_ZN6test103fooIiED2Ev({{.*}} comdat $_ZN6test103fooIiED5Ev
219}
220
221namespace test11 {
222// Test that when we don't have to worry about COMDATs we produce an alias
223// from complate to base and from base to base class base.
224struct bar {
225 ~bar();
226};
227bar::~bar() {}
228struct foo : public bar {
229 ~foo();
230};
231foo::~foo() {}
232// CHECK6: @_ZN6test113fooD2Ev = alias {{.*}} @_ZN6test113barD2Ev
233// CHECK6: @_ZN6test113fooD1Ev = alias {{.*}} @_ZN6test113fooD2Ev
234}