diff options
| author | Richard Smith <richard-llvm@metafoo.co.uk> | 2017-11-16 23:54:56 +0000 |
|---|---|---|
| committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2017-11-16 23:54:56 +0000 |
| commit | e2467b7aed5d90db90108e8486cfc5e6dbae2b41 (patch) | |
| tree | 7917e383903f16ba5990a0b27ed29861218f269d | |
| parent | 39bcd4ed3e5e824f363303077c98f54be61c3add (diff) | |
| download | bcm5719-llvm-e2467b7aed5d90db90108e8486cfc5e6dbae2b41.tar.gz bcm5719-llvm-e2467b7aed5d90db90108e8486cfc5e6dbae2b41.zip | |
PR22763: if a defaulted (non-user-provided) special member function is
explicitly instantiated, still emit it with each use.
We don't emit a definition of the member with an explicit instantiation
definition (and indeed it appears that we're not allowed to, since an explicit
instantiation definition does not constitute an odr-use and only odr-use
permits definition for defaulted special members). So we still need to emit a
weak definition with each use.
This also makes defaulted-in-class declarations behave more like
implicitly-declared special members, which matches their design intent.
And it matches the way this problem was solved in GCC.
llvm-svn: 318474
| -rw-r--r-- | clang/include/clang/AST/DeclCXX.h | 6 | ||||
| -rw-r--r-- | clang/lib/AST/ASTContext.cpp | 6 | ||||
| -rw-r--r-- | clang/test/CodeGenCXX/cxx11-special-members.cpp | 4 | ||||
| -rw-r--r-- | clang/test/CodeGenCXX/explicit-instantiation.cpp | 19 |
4 files changed, 33 insertions, 2 deletions
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h index 59c9b3500ef..09abd2f9c4b 100644 --- a/clang/include/clang/AST/DeclCXX.h +++ b/clang/include/clang/AST/DeclCXX.h @@ -2061,7 +2061,11 @@ public: /// True if this method is user-declared and was not /// deleted or defaulted on its first declaration. bool isUserProvided() const { - return !(isDeleted() || getCanonicalDecl()->isDefaulted()); + auto *DeclAsWritten = this; + if (auto *Pattern = getTemplateInstantiationPattern()) + DeclAsWritten = cast<CXXMethodDecl>(Pattern); + return !(DeclAsWritten->isDeleted() || + DeclAsWritten->getCanonicalDecl()->isDefaulted()); } void addOverriddenMethod(const CXXMethodDecl *MD); diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index 8247d1a23ef..d3a5f075dc2 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -8963,6 +8963,12 @@ static GVALinkage basicGVALinkageForFunction(const ASTContext &Context, if (!FD->isExternallyVisible()) return GVA_Internal; + // Non-user-provided functions get emitted as weak definitions with every + // use, no matter whether they've been explicitly instantiated etc. + if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) + if (!MD->isUserProvided()) + return GVA_DiscardableODR; + GVALinkage External; switch (FD->getTemplateSpecializationKind()) { case TSK_Undeclared: diff --git a/clang/test/CodeGenCXX/cxx11-special-members.cpp b/clang/test/CodeGenCXX/cxx11-special-members.cpp index 037e59a6408..96109ba5bac 100644 --- a/clang/test/CodeGenCXX/cxx11-special-members.cpp +++ b/clang/test/CodeGenCXX/cxx11-special-members.cpp @@ -39,7 +39,9 @@ void f3() { C<0> a; D b; } -// CHECK: define {{.*}} @_ZN1CILi0EEC1Ev +// Trivial default ctor, might or might not be defined, but we must not expect +// someone else ot define it. +// CHECK-NOT: declare {{.*}} @_ZN1CILi0EEC1Ev // CHECK: define {{.*}} @_ZN1DC1Ev // CHECK: define {{.*}} @_ZN1BC2EOS_( diff --git a/clang/test/CodeGenCXX/explicit-instantiation.cpp b/clang/test/CodeGenCXX/explicit-instantiation.cpp index 85857fb6fb8..ad20abde8f1 100644 --- a/clang/test/CodeGenCXX/explicit-instantiation.cpp +++ b/clang/test/CodeGenCXX/explicit-instantiation.cpp @@ -170,3 +170,22 @@ void use() { f<int>(); } } + +namespace DefaultedMembers { + struct B { B(); B(const B&); ~B(); }; + template<typename T> struct A : B { + A() = default; + ~A() = default; + }; + extern template struct A<int>; + + // CHECK-LABEL: define {{.*}} @_ZN16DefaultedMembers1AIiEC2Ev + // CHECK-LABEL: define {{.*}} @_ZN16DefaultedMembers1AIiED2Ev + A<int> ai; + + // CHECK-LABEL: define {{.*}} @_ZN16DefaultedMembers1AIiEC2ERKS1_ + A<int> ai2(ai); + + // CHECK-NOT: @_ZN16DefaultedMembers1AIcE + template struct A<char>; +} |

