2015-01-16 07:18:01 +08:00
|
|
|
// RUN: %clang_cc1 %s -triple=x86_64-pc-linux -emit-llvm -o %t
|
2013-02-16 08:51:21 +08:00
|
|
|
// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -disable-llvm-optzns -O3 -emit-llvm -o %t.opt
|
2013-08-27 06:10:31 +08:00
|
|
|
// RUN: FileCheck --check-prefix=CHECK %s < %t
|
|
|
|
// RUN: FileCheck --check-prefix=CHECK-OPT %s < %t.opt
|
2009-12-06 01:04:47 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct A {
|
|
|
|
virtual void f() { }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-12-06 06:19:10 +08:00
|
|
|
void f() { A b; }
|
|
|
|
|
|
|
|
struct B {
|
|
|
|
B();
|
|
|
|
virtual void f();
|
|
|
|
};
|
|
|
|
|
|
|
|
B::B() { }
|
|
|
|
|
2011-01-30 03:34:19 +08:00
|
|
|
struct C : virtual B {
|
2009-12-06 06:24:38 +08:00
|
|
|
C();
|
|
|
|
virtual void f() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
C::C() { }
|
|
|
|
|
2009-12-06 08:53:22 +08:00
|
|
|
struct D {
|
|
|
|
virtual void f();
|
|
|
|
};
|
|
|
|
|
|
|
|
void D::f() { }
|
|
|
|
|
2009-12-12 04:48:18 +08:00
|
|
|
static struct : D { } e;
|
|
|
|
|
2010-01-06 05:40:05 +08:00
|
|
|
// The destructor is the key function.
|
2010-01-06 03:06:31 +08:00
|
|
|
template<typename T>
|
|
|
|
struct E {
|
|
|
|
virtual ~E();
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> E<T>::~E() { }
|
|
|
|
|
2010-01-06 05:40:05 +08:00
|
|
|
// Anchor is the key function
|
2010-01-06 03:06:31 +08:00
|
|
|
template<>
|
|
|
|
struct E<char> {
|
|
|
|
virtual void anchor();
|
|
|
|
};
|
|
|
|
|
|
|
|
void E<char>::anchor() { }
|
|
|
|
|
|
|
|
template struct E<short>;
|
|
|
|
extern template struct E<int>;
|
|
|
|
|
|
|
|
void use_E() {
|
|
|
|
E<int> ei;
|
|
|
|
(void)ei;
|
|
|
|
E<long> el;
|
|
|
|
(void)el;
|
|
|
|
}
|
|
|
|
|
2010-01-06 05:40:05 +08:00
|
|
|
// No key function
|
|
|
|
template<typename T>
|
|
|
|
struct F {
|
|
|
|
virtual void foo() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
// No key function
|
|
|
|
template<>
|
|
|
|
struct F<char> {
|
|
|
|
virtual void foo() { }
|
|
|
|
};
|
|
|
|
|
|
|
|
template struct F<short>;
|
|
|
|
extern template struct F<int>;
|
|
|
|
|
2010-08-04 14:38:15 +08:00
|
|
|
void use_F() {
|
|
|
|
F<char> fc;
|
|
|
|
fc.foo();
|
2010-01-06 05:40:05 +08:00
|
|
|
F<int> fi;
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
llvm-svn: 103718
2010-05-14 00:44:06 +08:00
|
|
|
fi.foo();
|
2010-01-06 05:40:05 +08:00
|
|
|
F<long> fl;
|
|
|
|
(void)fl;
|
|
|
|
}
|
|
|
|
|
2009-12-06 06:19:10 +08:00
|
|
|
// B has a key function that is not defined in this translation unit so its vtable
|
|
|
|
// has external linkage.
|
2013-08-27 06:10:31 +08:00
|
|
|
// CHECK-DAG: @_ZTV1B = external unnamed_addr constant
|
2009-12-06 06:19:10 +08:00
|
|
|
|
2010-08-03 15:24:12 +08:00
|
|
|
// C has no key function, so its vtable should have weak_odr linkage
|
|
|
|
// and hidden visibility (rdar://problem/7523229).
|
2015-01-16 07:18:01 +08:00
|
|
|
// CHECK-DAG: @_ZTV1C = linkonce_odr unnamed_addr constant {{.*}}, comdat,
|
|
|
|
// CHECK-DAG: @_ZTS1C = linkonce_odr constant {{.*}}, comdat{{$}}
|
|
|
|
// CHECK-DAG: @_ZTI1C = linkonce_odr constant {{.*}}, comdat{{$}}
|
|
|
|
// CHECK-DAG: @_ZTT1C = linkonce_odr unnamed_addr constant {{.*}}, comdat{{$}}
|
2009-12-06 06:24:38 +08:00
|
|
|
|
2009-12-06 08:53:22 +08:00
|
|
|
// D has a key function that is defined in this translation unit so its vtable is
|
|
|
|
// defined in the translation unit.
|
2013-08-27 06:10:31 +08:00
|
|
|
// CHECK-DAG: @_ZTV1D = unnamed_addr constant
|
|
|
|
// CHECK-DAG: @_ZTS1D = constant
|
2014-02-08 11:26:05 +08:00
|
|
|
// CHECK-DAG: @_ZTI1D = constant
|
2009-12-06 08:53:22 +08:00
|
|
|
|
2010-01-06 03:06:31 +08:00
|
|
|
// E<char> is an explicit specialization with a key function defined
|
|
|
|
// in this translation unit, so its vtable should have external
|
|
|
|
// linkage.
|
2013-08-27 06:10:31 +08:00
|
|
|
// CHECK-DAG: @_ZTV1EIcE = unnamed_addr constant
|
|
|
|
// CHECK-DAG: @_ZTS1EIcE = constant
|
2014-02-08 11:26:05 +08:00
|
|
|
// CHECK-DAG: @_ZTI1EIcE = constant
|
2010-01-06 03:06:31 +08:00
|
|
|
|
|
|
|
// E<short> is an explicit template instantiation with a key function
|
|
|
|
// defined in this translation unit, so its vtable should have
|
|
|
|
// weak_odr linkage.
|
2015-01-16 07:18:01 +08:00
|
|
|
// CHECK-DAG: @_ZTV1EIsE = weak_odr unnamed_addr constant {{.*}}, comdat,
|
|
|
|
// CHECK-DAG: @_ZTS1EIsE = weak_odr constant {{.*}}, comdat{{$}}
|
|
|
|
// CHECK-DAG: @_ZTI1EIsE = weak_odr constant {{.*}}, comdat{{$}}
|
2010-01-06 03:06:31 +08:00
|
|
|
|
2010-01-06 05:40:05 +08:00
|
|
|
// F<short> is an explicit template instantiation without a key
|
|
|
|
// function, so its vtable should have weak_odr linkage
|
2015-01-16 07:18:01 +08:00
|
|
|
// CHECK-DAG: @_ZTV1FIsE = weak_odr unnamed_addr constant {{.*}}, comdat,
|
|
|
|
// CHECK-DAG: @_ZTS1FIsE = weak_odr constant {{.*}}, comdat{{$}}
|
|
|
|
// CHECK-DAG: @_ZTI1FIsE = weak_odr constant {{.*}}, comdat{{$}}
|
2010-01-06 05:40:05 +08:00
|
|
|
|
2010-01-06 03:06:31 +08:00
|
|
|
// E<long> is an implicit template instantiation with a key function
|
|
|
|
// defined in this translation unit, so its vtable should have
|
2011-01-24 08:46:19 +08:00
|
|
|
// linkonce_odr linkage.
|
2015-01-16 07:18:01 +08:00
|
|
|
// CHECK-DAG: @_ZTV1EIlE = linkonce_odr unnamed_addr constant {{.*}}, comdat,
|
|
|
|
// CHECK-DAG: @_ZTS1EIlE = linkonce_odr constant {{.*}}, comdat{{$}}
|
|
|
|
// CHECK-DAG: @_ZTI1EIlE = linkonce_odr constant {{.*}}, comdat{{$}}
|
2010-01-06 03:06:31 +08:00
|
|
|
|
2010-01-06 05:40:05 +08:00
|
|
|
// F<long> is an implicit template instantiation with no key function,
|
2011-01-24 08:46:19 +08:00
|
|
|
// so its vtable should have linkonce_odr linkage.
|
2015-01-16 07:18:01 +08:00
|
|
|
// CHECK-DAG: @_ZTV1FIlE = linkonce_odr unnamed_addr constant {{.*}}, comdat,
|
|
|
|
// CHECK-DAG: @_ZTS1FIlE = linkonce_odr constant {{.*}}, comdat{{$}}
|
|
|
|
// CHECK-DAG: @_ZTI1FIlE = linkonce_odr constant {{.*}}, comdat{{$}}
|
2010-01-06 05:40:05 +08:00
|
|
|
|
|
|
|
// F<int> is an explicit template instantiation declaration without a
|
2010-04-03 12:26:42 +08:00
|
|
|
// key function, so its vtable should have external linkage.
|
2013-08-27 06:10:31 +08:00
|
|
|
// CHECK-DAG: @_ZTV1FIiE = external unnamed_addr constant
|
2013-09-04 05:05:13 +08:00
|
|
|
// CHECK-OPT-DAG: @_ZTV1FIiE = external unnamed_addr constant
|
2010-01-06 05:40:05 +08:00
|
|
|
|
2010-01-06 03:06:31 +08:00
|
|
|
// E<int> is an explicit template instantiation declaration. It has a
|
2010-01-07 12:09:30 +08:00
|
|
|
// key function that is not instantiated, so we should only reference
|
2010-01-06 03:06:31 +08:00
|
|
|
// its vtable, not define it.
|
2013-08-27 06:10:31 +08:00
|
|
|
// CHECK-DAG: @_ZTV1EIiE = external unnamed_addr constant
|
2013-09-04 05:05:13 +08:00
|
|
|
// CHECK-OPT-DAG: @_ZTV1EIiE = external unnamed_addr constant
|
2010-01-06 03:06:31 +08:00
|
|
|
|
2010-01-08 08:50:11 +08:00
|
|
|
// The anonymous struct for e has no linkage, so the vtable should have
|
|
|
|
// internal linkage.
|
2013-08-27 06:10:31 +08:00
|
|
|
// CHECK-DAG: @"_ZTV3$_0" = internal unnamed_addr constant
|
|
|
|
// CHECK-DAG: @"_ZTS3$_0" = internal constant
|
2014-02-08 11:26:05 +08:00
|
|
|
// CHECK-DAG: @"_ZTI3$_0" = internal constant
|
2010-01-08 08:50:11 +08:00
|
|
|
|
2009-12-06 06:19:10 +08:00
|
|
|
// The A vtable should have internal linkage since it is inside an anonymous
|
|
|
|
// namespace.
|
2013-08-27 06:10:31 +08:00
|
|
|
// CHECK-DAG: @_ZTVN12_GLOBAL__N_11AE = internal unnamed_addr constant
|
|
|
|
// CHECK-DAG: @_ZTSN12_GLOBAL__N_11AE = internal constant
|
2014-02-08 11:26:05 +08:00
|
|
|
// CHECK-DAG: @_ZTIN12_GLOBAL__N_11AE = internal constant
|
2010-01-06 03:06:31 +08:00
|
|
|
|
2010-08-04 14:38:15 +08:00
|
|
|
// F<char> is an explicit specialization without a key function, so
|
2011-01-24 08:46:19 +08:00
|
|
|
// its vtable should have linkonce_odr linkage.
|
2015-01-16 07:18:01 +08:00
|
|
|
// CHECK-DAG: @_ZTV1FIcE = linkonce_odr unnamed_addr constant {{.*}}, comdat,
|
|
|
|
// CHECK-DAG: @_ZTS1FIcE = linkonce_odr constant {{.*}}, comdat{{$}}
|
|
|
|
// CHECK-DAG: @_ZTI1FIcE = linkonce_odr constant {{.*}}, comdat{{$}}
|
2010-08-04 14:38:15 +08:00
|
|
|
|
2015-01-16 07:18:01 +08:00
|
|
|
// CHECK-DAG: @_ZTV1GIiE = linkonce_odr unnamed_addr constant {{.*}}, comdat,
|
2010-05-25 08:33:13 +08:00
|
|
|
template <typename T>
|
|
|
|
class G {
|
|
|
|
public:
|
|
|
|
G() {}
|
|
|
|
virtual void f0();
|
|
|
|
virtual void f1();
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
void G<int>::f1() {}
|
|
|
|
template <typename T>
|
|
|
|
void G<T>::f0() {}
|
|
|
|
void G_f0() { new G<int>(); }
|
2010-10-11 11:25:57 +08:00
|
|
|
|
|
|
|
// H<int> has a key function without a body but it's a template instantiation
|
2011-04-15 13:22:18 +08:00
|
|
|
// so its VTable must be emitted.
|
2015-01-16 07:18:01 +08:00
|
|
|
// CHECK-DAG: @_ZTV1HIiE = linkonce_odr unnamed_addr constant {{.*}}, comdat,
|
2010-10-11 11:25:57 +08:00
|
|
|
template <typename T>
|
|
|
|
class H {
|
|
|
|
public:
|
|
|
|
virtual ~H();
|
|
|
|
};
|
|
|
|
|
|
|
|
void use_H() {
|
|
|
|
H<int> h;
|
|
|
|
}
|
2013-02-16 08:51:21 +08:00
|
|
|
|
|
|
|
// I<int> has an explicit instantiation declaration and needs a VTT and
|
2013-09-04 05:05:13 +08:00
|
|
|
// construction vtables.
|
2013-02-16 08:51:21 +08:00
|
|
|
|
2013-08-27 06:10:31 +08:00
|
|
|
// CHECK-DAG: @_ZTV1IIiE = external unnamed_addr constant
|
|
|
|
// CHECK-DAG: @_ZTT1IIiE = external unnamed_addr constant
|
|
|
|
// CHECK-NOT: @_ZTC1IIiE
|
2013-02-16 08:51:21 +08:00
|
|
|
//
|
2013-09-04 05:05:13 +08:00
|
|
|
// CHECK-OPT-DAG: @_ZTV1IIiE = external unnamed_addr constant
|
|
|
|
// CHECK-OPT-DAG: @_ZTT1IIiE = external unnamed_addr constant
|
2013-02-16 08:51:21 +08:00
|
|
|
struct VBase1 { virtual void f(); }; struct VBase2 : virtual VBase1 {};
|
|
|
|
template<typename T>
|
|
|
|
struct I : VBase2 {};
|
|
|
|
extern template struct I<int>;
|
|
|
|
I<int> i;
|