blob: 10923d1bc7932afceb79108a0bc411f1248905d8 [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 Espindola3f643bd2013-11-04 18:38:59 +000011namespace test1 {
Rafael Espindola1e4df922014-09-16 15:18:21 +000012// Test that we produce the apropriate comdats when creating aliases to
13// weak_odr constructors and destructors.
Rafael Espindola3f643bd2013-11-04 18:38:59 +000014
Rafael Espindola1e4df922014-09-16 15:18:21 +000015// CHECK1: @_ZN5test16foobarIvEC1Ev = weak_odr alias void {{.*}} @_ZN5test16foobarIvEC2Ev
16// CHECK1: @_ZN5test16foobarIvED1Ev = weak_odr alias void (%"struct.test1::foobar"*)* @_ZN5test16foobarIvED2Ev
17// CHECK1: define weak_odr void @_ZN5test16foobarIvEC2Ev({{.*}} comdat $_ZN5test16foobarIvEC5Ev
18// CHECK1: define weak_odr void @_ZN5test16foobarIvED2Ev({{.*}} comdat $_ZN5test16foobarIvED5Ev
19// CHECK1: define weak_odr void @_ZN5test16foobarIvED0Ev({{.*}} comdat $_ZN5test16foobarIvED5Ev
20// CHECK1-NOT: comdat
Rafael Espindola129d3132013-11-12 22:06:46 +000021
Rafael Espindola1e4df922014-09-16 15:18:21 +000022template <typename T>
23struct foobar {
Rafael Espindola3f643bd2013-11-04 18:38:59 +000024 foobar() {}
Rafael Espindola1e4df922014-09-16 15:18:21 +000025 virtual ~foobar() {}
Rafael Espindola3f643bd2013-11-04 18:38:59 +000026};
27
28template struct foobar<void>;
29}
30
31namespace test2 {
Rafael Espindola2e2995b2013-11-05 21:37:29 +000032// test that when the destrucor is linkonce_odr we just replace every use of
33// C1 with C2.
Rafael Espindola3f643bd2013-11-04 18:38:59 +000034
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000035// CHECK1: define internal void @__cxx_global_var_init()
36// CHECK1: call void @_ZN5test26foobarIvEC2Ev
37// CHECK1: define linkonce_odr void @_ZN5test26foobarIvEC2Ev(
Rafael Espindola3f643bd2013-11-04 18:38:59 +000038void g();
39template <typename T> struct foobar {
40 foobar() { g(); }
41};
42foobar<void> x;
43}
44
45namespace test3 {
Rafael Espindola23d37512013-11-08 23:46:20 +000046// test that instead of an internal alias we just use the other destructor
47// directly.
Rafael Espindola3f643bd2013-11-04 18:38:59 +000048
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000049// CHECK1: define internal void @__cxx_global_var_init1()
50// CHECK1: call i32 @__cxa_atexit{{.*}}_ZN5test312_GLOBAL__N_11AD2Ev
51// CHECK1: define internal void @_ZN5test312_GLOBAL__N_11AD2Ev(
Rafael Espindola3f643bd2013-11-04 18:38:59 +000052namespace {
53struct A {
54 ~A() {}
55};
56
57struct B : public A {};
58}
59
60B x;
61}
62
63namespace test4 {
64 // Test that we don't produce aliases from B to A. We cannot because we cannot
Rafael Espindolae2ec6fa2013-11-08 22:59:46 +000065 // guarantee that they will be present in every TU. Instead, we just call
66 // A's destructor directly.
Rafael Espindola3f643bd2013-11-04 18:38:59 +000067
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000068 // CHECK1: define internal void @__cxx_global_var_init2()
69 // CHECK1: call i32 @__cxa_atexit{{.*}}_ZN5test41AD2Ev
70 // CHECK1: define linkonce_odr void @_ZN5test41AD2Ev(
Rafael Espindolad967bad2013-11-13 23:20:45 +000071
72 // test that we don't do this optimization at -O0 so that the debugger can
73 // see both destructors.
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000074 // NOOPT: define internal void @__cxx_global_var_init2()
75 // NOOPT: call i32 @__cxa_atexit{{.*}}@_ZN5test41BD2Ev
76 // NOOPT: define linkonce_odr void @_ZN5test41BD2Ev
Rafael Espindola3f643bd2013-11-04 18:38:59 +000077 struct A {
78 virtual ~A() {}
79 };
80 struct B : public A{
81 ~B() {}
82 };
83 B X;
84}
Rafael Espindola23d37512013-11-08 23:46:20 +000085
86namespace test5 {
87 // similar to test4, but with an internal B.
88
Rafael Espindolaf7765ac2014-08-30 00:15:37 +000089 // CHECK2: define internal void @__cxx_global_var_init3()
90 // CHECK2: call i32 @__cxa_atexit{{.*}}_ZN5test51AD2Ev
91 // CHECK2: define linkonce_odr void @_ZN5test51AD2Ev(
Rafael Espindola23d37512013-11-08 23:46:20 +000092 struct A {
93 virtual ~A() {}
94 };
95 namespace {
96 struct B : public A{
97 ~B() {}
98 };
99 }
100 B X;
101}
Rafael Espindola961ba212013-11-09 01:57:21 +0000102
103namespace test6 {
104 // Test that we use ~A directly, even when ~A is not defined. The symbol for
105 // ~B would have been internal and still contain a reference to ~A.
106 struct A {
107 virtual ~A();
108 };
109 namespace {
110 struct B : public A {
111 ~B() {}
112 };
113 }
114 B X;
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000115 // CHECK3: define internal void @__cxx_global_var_init4()
116 // CHECK3: call i32 @__cxa_atexit({{.*}}@_ZN5test61AD2Ev
Rafael Espindola961ba212013-11-09 01:57:21 +0000117}
Rafael Espindolab15683e2013-11-11 19:35:06 +0000118
119namespace test7 {
120 // Test that we don't produce an alias from ~B to ~A<int> (or crash figuring
121 // out if we should).
122 // pr17875.
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000123 // CHECK3: define void @_ZN5test71BD2Ev
Rafael Espindolab15683e2013-11-11 19:35:06 +0000124 template <typename> struct A {
125 ~A() {}
126 };
127 class B : A<int> {
128 ~B();
129 };
130 template class A<int>;
131 B::~B() {}
132}
Rafael Espindola50e1f002013-11-11 22:55:13 +0000133
134namespace test8 {
135 // Test that we replace ~zed with ~bar which is an alias to ~foo.
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000136 // CHECK4: @_ZN5test83barD2Ev = alias {{.*}} @_ZN5test83fooD2Ev
137 // CHECK4: define internal void @__cxx_global_var_init5()
138 // CHECK4: call i32 @__cxa_atexit({{.*}}@_ZN5test83barD2Ev
Rafael Espindola50e1f002013-11-11 22:55:13 +0000139 struct foo {
140 ~foo();
141 };
142 foo::~foo() {}
143 struct bar : public foo {
144 ~bar();
145 };
146 bar::~bar() {}
147 struct zed : public bar {};
148 zed foo;
149}
Joerg Sonnenberger374c2bb2013-11-22 21:34:35 +0000150
Rafael Espindola191b9512014-03-05 21:04:41 +0000151namespace test9 {
152struct foo {
153 __attribute__((stdcall)) ~foo() {
154 }
155};
156
157struct bar : public foo {};
158
159void zed() {
160 // Test that we produce a call to bar's destructor. We used to call foo's, but
161 // it has a different calling conversion.
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000162 // CHECK4: call void @_ZN5test93barD2Ev
Rafael Espindola191b9512014-03-05 21:04:41 +0000163 bar ptr;
164}
165}
166
Rafael Espindolaf7765ac2014-08-30 00:15:37 +0000167// CHECK5: @_ZTV1C = linkonce_odr unnamed_addr constant [4 x i8*] [{{[^@]*}}@_ZTI1C {{[^@]*}}@_ZN1CD2Ev {{[^@]*}}@_ZN1CD0Ev {{[^@]*}}]
Joerg Sonnenberger374c2bb2013-11-22 21:34:35 +0000168// r194296 replaced C::~C with B::~B without emitting the later.
169
170class A {
171public:
172 A(int);
173 virtual ~A();
174};
175
176template <class>
177class B : A {
178public:
179 B()
180 : A(0) {
181 }
182 __attribute__((always_inline)) ~B() {
183 }
184};
185
186extern template class B<char>;
187
188class C : B<char> {
189};
190
191void
192fn1() {
193 new C;
194}
Rafael Espindola1e4df922014-09-16 15:18:21 +0000195
196namespace test10 {
197// Test that if a destructor is in a comdat, we don't try to emit is as an
198// alias to a base class destructor.
199struct bar {
200 ~bar();
201};
202bar::~bar() {
203}
204} // closing the namespace causes ~bar to be sent to CodeGen
205namespace test10 {
206template <typename T>
207struct foo : public bar {
208 ~foo();
209};
210template <typename T>
211foo<T>::~foo() {}
212template class foo<int>;
213// CHECK5: define weak_odr void @_ZN6test103fooIiED2Ev({{.*}} comdat $_ZN6test103fooIiED5Ev
214}
215
216namespace test11 {
217// Test that when we don't have to worry about COMDATs we produce an alias
218// from complate to base and from base to base class base.
219struct bar {
220 ~bar();
221};
222bar::~bar() {}
223struct foo : public bar {
224 ~foo();
225};
226foo::~foo() {}
227// CHECK6: @_ZN6test113fooD2Ev = alias {{.*}} @_ZN6test113barD2Ev
228// CHECK6: @_ZN6test113fooD1Ev = alias {{.*}} @_ZN6test113fooD2Ev
229}