blob: 7b506b6344048832316738984d93428fda498e5b [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
David Blaikiefc473552015-09-11 03:22:18 +000018// CHECK1: @_ZN5test16foobarIvED1Ev = weak_odr alias void (%"struct.test1::foobar"*), void (%"struct.test1::foobar"*)* @_ZN5test16foobarIvED2Ev
Rafael Espindolac0f4a302015-01-06 22:55:40 +000019// 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)
Rafael Espindola1e4df922014-09-16 15:18:21 +000022// 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).
Rafael Espindoladbee8a72015-01-15 21:36:08 +000025// COFF: define weak_odr {{.*}} void @_ZN5test16foobarIvEC2Ev({{.*}} comdat align
26// COFF: define weak_odr {{.*}} void @_ZN5test16foobarIvEC1Ev({{.*}} comdat align
27// COFF: define weak_odr {{.*}} void @_ZN5test16foobarIvED2Ev({{.*}} comdat align
28// COFF: define weak_odr {{.*}} void @_ZN5test16foobarIvED0Ev({{.*}} comdat align
Rafael Espindola0806f982014-09-16 20:19:43 +000029
Rafael Espindola1e4df922014-09-16 15:18:21 +000030template <typename T>
31struct foobar {
Rafael Espindola3f643bd2013-11-04 18:38:59 +000032 foobar() {}
Rafael Espindola1e4df922014-09-16 15:18:21 +000033 virtual ~foobar() {}
Rafael Espindola3f643bd2013-11-04 18:38:59 +000034};
35
36template struct foobar<void>;
37}
38
39namespace test2 {
Rafael Espindola2e2995b2013-11-05 21:37:29 +000040// test that when the destrucor is linkonce_odr we just replace every use of
41// C1 with C2.
Rafael Espindola3f643bd2013-11-04 18:38:59 +000042
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000043// CHECK1: define internal void @__cxx_global_var_init()
44// CHECK1: call void @_ZN5test26foobarIvEC2Ev
Rafael Espindoladbee8a72015-01-15 21:36:08 +000045// CHECK1: define linkonce_odr void @_ZN5test26foobarIvEC2Ev({{.*}} comdat align
Rafael Espindola3f643bd2013-11-04 18:38:59 +000046void g();
47template <typename T> struct foobar {
48 foobar() { g(); }
49};
50foobar<void> x;
51}
52
53namespace test3 {
Rafael Espindola23d37512013-11-08 23:46:20 +000054// test that instead of an internal alias we just use the other destructor
55// directly.
Rafael Espindola3f643bd2013-11-04 18:38:59 +000056
Sunil Srivastava3acf6272015-05-12 16:48:43 +000057// CHECK1: define internal void @__cxx_global_var_init.1()
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000058// CHECK1: call i32 @__cxa_atexit{{.*}}_ZN5test312_GLOBAL__N_11AD2Ev
59// CHECK1: define internal void @_ZN5test312_GLOBAL__N_11AD2Ev(
Rafael Espindola3f643bd2013-11-04 18:38:59 +000060namespace {
61struct A {
62 ~A() {}
63};
64
65struct B : public A {};
66}
67
68B x;
69}
70
71namespace test4 {
72 // Test that we don't produce aliases from B to A. We cannot because we cannot
Rafael Espindolae2ec6fa2013-11-08 22:59:46 +000073 // guarantee that they will be present in every TU. Instead, we just call
74 // A's destructor directly.
Rafael Espindola3f643bd2013-11-04 18:38:59 +000075
Sunil Srivastava3acf6272015-05-12 16:48:43 +000076 // CHECK1: define internal void @__cxx_global_var_init.2()
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000077 // CHECK1: call i32 @__cxa_atexit{{.*}}_ZN5test41AD2Ev
Rafael Espindoladbee8a72015-01-15 21:36:08 +000078 // CHECK1: define linkonce_odr void @_ZN5test41AD2Ev({{.*}} comdat align
Rafael Espindolad967bad2013-11-13 23:20:45 +000079
80 // test that we don't do this optimization at -O0 so that the debugger can
81 // see both destructors.
Sunil Srivastava3acf6272015-05-12 16:48:43 +000082 // NOOPT: define internal void @__cxx_global_var_init.2()
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000083 // NOOPT: call i32 @__cxa_atexit{{.*}}@_ZN5test41BD2Ev
Rafael Espindoladbee8a72015-01-15 21:36:08 +000084 // NOOPT: define linkonce_odr void @_ZN5test41BD2Ev({{.*}} comdat align
Rafael Espindola3f643bd2013-11-04 18:38:59 +000085 struct A {
86 virtual ~A() {}
87 };
88 struct B : public A{
89 ~B() {}
90 };
91 B X;
92}
Rafael Espindola23d37512013-11-08 23:46:20 +000093
94namespace test5 {
95 // similar to test4, but with an internal B.
96
Sunil Srivastava3acf6272015-05-12 16:48:43 +000097 // CHECK2: define internal void @__cxx_global_var_init.3()
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000098 // CHECK2: call i32 @__cxa_atexit{{.*}}_ZN5test51AD2Ev
Rafael Espindoladbee8a72015-01-15 21:36:08 +000099 // CHECK2: define linkonce_odr void @_ZN5test51AD2Ev({{.*}} comdat align
Rafael Espindola23d37512013-11-08 23:46:20 +0000100 struct A {
101 virtual ~A() {}
102 };
103 namespace {
104 struct B : public A{
105 ~B() {}
106 };
107 }
108 B X;
109}
Rafael Espindola961ba212013-11-09 01:57:21 +0000110
111namespace test6 {
112 // Test that we use ~A directly, even when ~A is not defined. The symbol for
113 // ~B would have been internal and still contain a reference to ~A.
114 struct A {
115 virtual ~A();
116 };
117 namespace {
118 struct B : public A {
119 ~B() {}
120 };
121 }
122 B X;
Sunil Srivastava3acf6272015-05-12 16:48:43 +0000123 // CHECK3: define internal void @__cxx_global_var_init.4()
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000124 // CHECK3: call i32 @__cxa_atexit({{.*}}@_ZN5test61AD2Ev
Rafael Espindola961ba212013-11-09 01:57:21 +0000125}
Rafael Espindolab15683e2013-11-11 19:35:06 +0000126
127namespace test7 {
128 // Test that we don't produce an alias from ~B to ~A<int> (or crash figuring
129 // out if we should).
130 // pr17875.
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000131 // CHECK3: define void @_ZN5test71BD2Ev
Rafael Espindolab15683e2013-11-11 19:35:06 +0000132 template <typename> struct A {
133 ~A() {}
134 };
135 class B : A<int> {
136 ~B();
137 };
138 template class A<int>;
139 B::~B() {}
140}
Rafael Espindola50e1f002013-11-11 22:55:13 +0000141
142namespace test8 {
143 // Test that we replace ~zed with ~bar which is an alias to ~foo.
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000144 // CHECK4: @_ZN5test83barD2Ev = alias {{.*}} @_ZN5test83fooD2Ev
Sunil Srivastava3acf6272015-05-12 16:48:43 +0000145 // CHECK4: define internal void @__cxx_global_var_init.5()
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000146 // CHECK4: call i32 @__cxa_atexit({{.*}}@_ZN5test83barD2Ev
Rafael Espindola50e1f002013-11-11 22:55:13 +0000147 struct foo {
148 ~foo();
149 };
150 foo::~foo() {}
151 struct bar : public foo {
152 ~bar();
153 };
154 bar::~bar() {}
155 struct zed : public bar {};
156 zed foo;
157}
Joerg Sonnenberger374c2bb2013-11-22 21:34:35 +0000158
Rafael Espindola191b9512014-03-05 21:04:41 +0000159namespace test9 {
160struct foo {
161 __attribute__((stdcall)) ~foo() {
162 }
163};
164
165struct bar : public foo {};
166
167void zed() {
168 // Test that we produce a call to bar's destructor. We used to call foo's, but
169 // it has a different calling conversion.
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000170 // CHECK4: call void @_ZN5test93barD2Ev
Rafael Espindola191b9512014-03-05 21:04:41 +0000171 bar ptr;
172}
173}
174
Peter Collingbourne2849c4e2016-12-13 20:40:39 +0000175// CHECK5: @_ZTV1C = linkonce_odr unnamed_addr constant { [4 x i8*] } {{[^@]*}}@_ZTI1C {{[^@]*}}@_ZN1CD2Ev {{[^@]*}}@_ZN1CD0Ev {{[^@]*}}]
Joerg Sonnenberger374c2bb2013-11-22 21:34:35 +0000176// r194296 replaced C::~C with B::~B without emitting the later.
177
178class A {
179public:
180 A(int);
181 virtual ~A();
182};
183
184template <class>
185class B : A {
186public:
187 B()
188 : A(0) {
189 }
190 __attribute__((always_inline)) ~B() {
191 }
192};
193
194extern template class B<char>;
195
196class C : B<char> {
197};
198
199void
200fn1() {
201 new C;
202}
Rafael Espindola1e4df922014-09-16 15:18:21 +0000203
204namespace test10 {
205// Test that if a destructor is in a comdat, we don't try to emit is as an
206// alias to a base class destructor.
207struct bar {
208 ~bar();
209};
210bar::~bar() {
211}
212} // closing the namespace causes ~bar to be sent to CodeGen
213namespace test10 {
214template <typename T>
215struct foo : public bar {
216 ~foo();
217};
218template <typename T>
219foo<T>::~foo() {}
220template class foo<int>;
Rafael Espindolac0f4a302015-01-06 22:55:40 +0000221// CHECK5: define weak_odr void @_ZN6test103fooIiED2Ev({{.*}} comdat($_ZN6test103fooIiED5Ev)
Rafael Espindola1e4df922014-09-16 15:18:21 +0000222}
223
224namespace test11 {
225// Test that when we don't have to worry about COMDATs we produce an alias
226// from complate to base and from base to base class base.
227struct bar {
228 ~bar();
229};
230bar::~bar() {}
231struct foo : public bar {
232 ~foo();
233};
234foo::~foo() {}
235// CHECK6: @_ZN6test113fooD2Ev = alias {{.*}} @_ZN6test113barD2Ev
236// CHECK6: @_ZN6test113fooD1Ev = alias {{.*}} @_ZN6test113fooD2Ev
237}
Rafael Espindola6565e922015-01-23 05:26:38 +0000238
239namespace test12 {
240template <int>
241struct foo {
242 ~foo() { delete this; }
243};
244
245template class foo<1>;
246// CHECK6: @_ZN6test123fooILi1EED1Ev = weak_odr alias {{.*}} @_ZN6test123fooILi1EED2Ev
247// CHECK6: define weak_odr void @_ZN6test123fooILi1EED2Ev({{.*}}) {{.*}} comdat($_ZN6test123fooILi1EED5Ev)
248}