From aeb085d5c3a7eb22fd2470584bd1884b7fb8cead Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Thu, 21 Aug 2014 01:14:01 +0000 Subject: [PATCH] MS ABI: Don't always instantiate all members of dllexported class templates (PR20163) Normally we mark all members of exported classes referenced to get them emitted. However, MSVC doesn't do this for class templates that are implicitly specialized or just have an explicit instantiation declaration. For such specializations, the members are emitted when referenced. The exception is the case when the dllexport attribute is propagated from a base class to a base class template that doesn't have an explicit attribute: in this case all methods of the base class template do get instantiated. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@216145 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDeclCXX.cpp | 12 +++++++++- test/CodeGenCXX/dllexport.cpp | 7 +++--- test/SemaCXX/dllexport.cpp | 43 +++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 49a5068ffa..ffec44243a 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -4432,6 +4432,9 @@ static void checkDLLAttribute(Sema &S, CXXRecordDecl *Class) { // FIXME: MSVC's docs say all bases must be exportable, but this doesn't // seem to be true in practice? + TemplateSpecializationKind TSK = + Class->getTemplateSpecializationKind(); + for (Decl *Member : Class->decls()) { VarDecl *VD = dyn_cast(Member); CXXMethodDecl *MD = dyn_cast(Member); @@ -4471,7 +4474,14 @@ static void checkDLLAttribute(Sema &S, CXXRecordDecl *Class) { if (CXXMethodDecl *MD = dyn_cast(Member)) { if (ClassExported) { if (MD->isUserProvided()) { - // Instantiate non-default methods. + // Instantiate non-default methods.. + + // .. except for certain kinds of template specializations. + if (TSK == TSK_ExplicitInstantiationDeclaration) + continue; + if (TSK == TSK_ImplicitInstantiation && !ClassAttr->isInherited()) + continue; + S.MarkFunctionReferenced(Class->getLocation(), MD); } else if (!MD->isTrivial() || MD->isExplicitlyDefaulted() || MD->isCopyAssignmentOperator() || diff --git a/test/CodeGenCXX/dllexport.cpp b/test/CodeGenCXX/dllexport.cpp index 846978dbb1..4cf6005134 100644 --- a/test/CodeGenCXX/dllexport.cpp +++ b/test/CodeGenCXX/dllexport.cpp @@ -509,11 +509,9 @@ USEVAR(T::b) int T::c; template struct __declspec(dllexport) U { void foo() {} }; -// The U specialization below must cause the following to be emitted: -// M32-DAG: define weak_odr dllexport x86_thiscallcc void @"\01?foo@?$U@H@@QAEXXZ" -// M32-DAG: define weak_odr dllexport x86_thiscallcc dereferenceable({{[0-9]+}}) %struct.U* @"\01??4?$U@H@@QAEAAU0@ABU0@@Z" struct __declspec(dllexport) V : public U { }; - +// U's assignment operator is emitted. +// M32-DAG: define weak_odr dllexport x86_thiscallcc dereferenceable({{[0-9]+}}) %struct.U* @"\01??4?$U@H@@QAEAAU0@ABU0@@Z" struct __declspec(dllexport) W { virtual void foo() {} }; // Default ctor: @@ -618,6 +616,7 @@ USEMEMFUNC(DerivedFromTemplate, func) // ExportedTemplate is explicitly exported. struct __declspec(dllexport) DerivedFromExportedTemplate : public ExportedClassTemplate {}; +USEMEMFUNC(DerivedFromExportedTemplate, func) // M32-DAG: define weak_odr dllexport x86_thiscallcc void @"\01?func@?$ExportedClassTemplate@H@@QAEXXZ" // G32-DAG: define weak_odr dllexport x86_thiscallcc void @_ZN21ExportedClassTemplateIiE4funcEv diff --git a/test/SemaCXX/dllexport.cpp b/test/SemaCXX/dllexport.cpp index 50d163a7f2..bad6a2b9ca 100644 --- a/test/SemaCXX/dllexport.cpp +++ b/test/SemaCXX/dllexport.cpp @@ -327,6 +327,49 @@ template struct __declspec(dllexport) PartiallySpecializedClassTemp template struct ExpliciallySpecializedClassTemplate {}; template <> struct __declspec(dllexport) ExpliciallySpecializedClassTemplate { void f() {} }; +// Don't instantiate class members of implicitly instantiated templates, even if they are exported. +struct IncompleteType; +template struct __declspec(dllexport) ImplicitlyInstantiatedExportedTemplate { + int f() { return sizeof(T); } // no-error +}; +ImplicitlyInstantiatedExportedTemplate implicitlyInstantiatedExportedTemplate; + +// Don't instantiate class members of templates with explicit instantiation declarations, even if they are exported. +struct IncompleteType2; +template struct __declspec(dllexport) ExportedTemplateWithExplicitInstantiationDecl { + int f() { return sizeof(T); } // no-error +}; +extern template struct ExportedTemplateWithExplicitInstantiationDecl; + +// Instantiate class members for explicitly instantiated exported templates. +struct IncompleteType3; // expected-note{{forward declaration of 'IncompleteType3'}} +template struct __declspec(dllexport) ExplicitlyInstantiatedExportedTemplate { + int f() { return sizeof(T); } // expected-error{{invalid application of 'sizeof' to an incomplete type 'IncompleteType3'}} +}; +template struct ExplicitlyInstantiatedExportedTemplate; // expected-note{{in instantiation of member function 'ExplicitlyInstantiatedExportedTemplate::f' requested here}} + +// In MS mode, instantiate members of class templates that are base classes of exported classes. +#ifdef MS + // expected-note@+3{{forward declaration of 'IncompleteType4'}} + // expected-note@+3{{in instantiation of member function 'BaseClassTemplateOfExportedClass::f' requested here}} +#endif +struct IncompleteType4; +template struct BaseClassTemplateOfExportedClass { +#ifdef MS + // expected-error@+2{{invalid application of 'sizeof' to an incomplete type 'IncompleteType4'}} +#endif + int f() { return sizeof(T); }; +}; +struct __declspec(dllexport) ExportedBaseClass : public BaseClassTemplateOfExportedClass {}; + +// Don't instantiate members of explicitly exported class templates that are base classes of exported classes. +struct IncompleteType5; +template struct __declspec(dllexport) ExportedBaseClassTemplateOfExportedClass { + int f() { return sizeof(T); }; // no-error +}; +struct __declspec(dllexport) ExportedBaseClass2 : public ExportedBaseClassTemplateOfExportedClass {}; + + //===----------------------------------------------------------------------===// // Classes with template base classes -- 2.40.0