"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
164 lines
4.3 KiB
C++
164 lines
4.3 KiB
C++
// RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o %t
|
|
// RUN: FileCheck --check-prefix=CHECK-1 %s < %t
|
|
// RUN: FileCheck --check-prefix=CHECK-2 %s < %t
|
|
// RUN: FileCheck --check-prefix=CHECK-3 %s < %t
|
|
// RUN: FileCheck --check-prefix=CHECK-4 %s < %t
|
|
// RUN: FileCheck --check-prefix=CHECK-5 %s < %t
|
|
// RUN: FileCheck --check-prefix=CHECK-6 %s < %t
|
|
// RUN: FileCheck --check-prefix=CHECK-7 %s < %t
|
|
// RUN: FileCheck --check-prefix=CHECK-8 %s < %t
|
|
// RUN: FileCheck --check-prefix=CHECK-9 %s < %t
|
|
// RUN: FileCheck --check-prefix=CHECK-10 %s < %t
|
|
// RUN: FileCheck --check-prefix=CHECK-11 %s < %t
|
|
// RUN: FileCheck --check-prefix=CHECK-12 %s < %t
|
|
|
|
namespace {
|
|
struct A {
|
|
virtual void f() { }
|
|
};
|
|
}
|
|
|
|
void f() { A b; }
|
|
|
|
struct B {
|
|
B();
|
|
virtual void f();
|
|
};
|
|
|
|
B::B() { }
|
|
|
|
struct C {
|
|
C();
|
|
virtual void f() { }
|
|
};
|
|
|
|
C::C() { }
|
|
|
|
struct D {
|
|
virtual void f();
|
|
};
|
|
|
|
void D::f() { }
|
|
|
|
static struct : D { } e;
|
|
|
|
// The destructor is the key function.
|
|
template<typename T>
|
|
struct E {
|
|
virtual ~E();
|
|
};
|
|
|
|
template<typename T> E<T>::~E() { }
|
|
|
|
// Anchor is the key function
|
|
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;
|
|
}
|
|
|
|
// 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>;
|
|
|
|
void use_F(F<char> &fc) {
|
|
F<int> fi;
|
|
fi.foo();
|
|
F<long> fl;
|
|
(void)fl;
|
|
fc.foo();
|
|
}
|
|
|
|
// B has a key function that is not defined in this translation unit so its vtable
|
|
// has external linkage.
|
|
// CHECK-1: @_ZTV1B = external constant
|
|
|
|
// C has no key function, so its vtable should have weak_odr linkage.
|
|
// CHECK-2: @_ZTV1C = weak_odr constant
|
|
// CHECK-2: @_ZTS1C = weak_odr constant
|
|
// CHECK-2: @_ZTI1C = weak_odr constant
|
|
|
|
// D has a key function that is defined in this translation unit so its vtable is
|
|
// defined in the translation unit.
|
|
// CHECK-3: @_ZTV1D = constant
|
|
// CHECK-3: @_ZTS1D = constant
|
|
// CHECK-3: @_ZTI1D = constant
|
|
|
|
// E<char> is an explicit specialization with a key function defined
|
|
// in this translation unit, so its vtable should have external
|
|
// linkage.
|
|
// CHECK-4: @_ZTV1EIcE = constant
|
|
// CHECK-4: @_ZTS1EIcE = constant
|
|
// CHECK-4: @_ZTI1EIcE = constant
|
|
|
|
// E<short> is an explicit template instantiation with a key function
|
|
// defined in this translation unit, so its vtable should have
|
|
// weak_odr linkage.
|
|
// CHECK-5: @_ZTV1EIsE = weak_odr constant
|
|
// CHECK-5: @_ZTS1EIsE = weak_odr constant
|
|
// CHECK-5: @_ZTI1EIsE = weak_odr constant
|
|
|
|
// F<short> is an explicit template instantiation without a key
|
|
// function, so its vtable should have weak_odr linkage
|
|
// CHECK-6: @_ZTV1FIsE = weak_odr constant
|
|
// CHECK-6: @_ZTS1FIsE = weak_odr constant
|
|
// CHECK-6: @_ZTI1FIsE = weak_odr constant
|
|
|
|
// E<long> is an implicit template instantiation with a key function
|
|
// defined in this translation unit, so its vtable should have
|
|
// weak_odr linkage.
|
|
// CHECK-7: @_ZTV1EIlE = weak_odr constant
|
|
// CHECK-7: @_ZTS1EIlE = weak_odr constant
|
|
// CHECK-7: @_ZTI1EIlE = weak_odr constant
|
|
|
|
// F<long> is an implicit template instantiation with no key function,
|
|
// so its vtable should have weak_odr linkage.
|
|
// CHECK-8: @_ZTV1FIlE = weak_odr constant
|
|
// CHECK-8: @_ZTS1FIlE = weak_odr constant
|
|
// CHECK-8: @_ZTI1FIlE = weak_odr constant
|
|
|
|
// F<int> is an explicit template instantiation declaration without a
|
|
// key function, so its vtable should have external linkage.
|
|
// CHECK-9: @_ZTV1FIiE = external constant
|
|
|
|
// E<int> is an explicit template instantiation declaration. It has a
|
|
// key function that is not instantiated, so we should only reference
|
|
// its vtable, not define it.
|
|
// CHECK-10: @_ZTV1EIiE = external constant
|
|
|
|
// The anonymous struct for e has no linkage, so the vtable should have
|
|
// internal linkage.
|
|
// CHECK-11: @"_ZTV3$_0" = internal constant
|
|
// CHECK-11: @"_ZTS3$_0" = internal constant
|
|
// CHECK-11: @"_ZTI3$_0" = internal constant
|
|
|
|
// The A vtable should have internal linkage since it is inside an anonymous
|
|
// namespace.
|
|
// CHECK-12: @_ZTVN12_GLOBAL__N_11AE = internal constant
|
|
// CHECK-12: @_ZTSN12_GLOBAL__N_11AE = internal constant
|
|
// CHECK-12: @_ZTIN12_GLOBAL__N_11AE = internal constant
|
|
|
|
|