From: Hans Wennborg Date: Tue, 10 Oct 2017 16:53:25 +0000 (+0000) Subject: For dllexport class templates, export specializations of member functions (PR34849... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a92cdd5042367c446fbbd2b5453e7a01ef71c737;p=clang For dllexport class templates, export specializations of member functions (PR34849) (take 2) This is a re-commit of r315025, but making sure to only apply this to specializations of class template member functions; i.e. not when the function itself is a template. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@315330 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 78ceeedc3a..541cf693b6 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -6041,6 +6041,22 @@ static void checkDLLAttributeRedeclaration(Sema &S, NamedDecl *OldDecl, diag::warn_dllimport_dropped_from_inline_function) << NewDecl << OldImportAttr; } + + // A specialization of a class template member function is processed here + // since it's a redeclaration. If the parent class is dllexport, the + // specialization inherits that attribute. This doesn't happen automatically + // since the parent class isn't instantiated until later. + if (const CXXMethodDecl *MD = dyn_cast(NewDecl)) { + if (MD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization && + !NewImportAttr && !NewExportAttr) { + if (const DLLExportAttr *ParentExportAttr = + MD->getParent()->getAttr()) { + DLLExportAttr *NewAttr = ParentExportAttr->clone(S.Context); + NewAttr->setInherited(true); + NewDecl->addAttr(NewAttr); + } + } + } } /// Given that we are within the definition of the given function, diff --git a/test/CodeGenCXX/dllexport.cpp b/test/CodeGenCXX/dllexport.cpp index bdef2eb06e..1c96de9b6c 100644 --- a/test/CodeGenCXX/dllexport.cpp +++ b/test/CodeGenCXX/dllexport.cpp @@ -831,6 +831,21 @@ template struct ExplicitInstantiationTwoAttributes { void f() {} }; template struct __declspec(dllexport) __declspec(dllimport) ExplicitInstantiationTwoAttributes; // M32-DAG: define weak_odr dllexport x86_thiscallcc void @"\01?f@?$ExplicitInstantiationTwoAttributes@H@@QAEXXZ" +namespace pr34849 { +// Specializations of exported class template member functions get exported. +template struct __declspec(dllexport) ExportedClassTemplate { void foo(); }; +template<> void ExportedClassTemplate::foo() {} +template struct ExportedClassTemplate; +// M32-DAG: define dllexport x86_thiscallcc void @"\01?foo@?$ExportedClassTemplate@H@pr34849@@QAEXXZ" + +// Specializations of exported class member template functions do not get exported. +struct __declspec(dllexport) ExportedClass { template void bar() ; }; +template<> void ExportedClass::bar() {} +// M32-DAG: define x86_thiscallcc void @"\01??$bar@H@ExportedClass@pr34849@@QAEXXZ" +template struct __declspec(dllexport) ExportedClassTemplate2 { template void baz(); }; +template<> template<> void ExportedClassTemplate2::baz() {} +// M32-DAG: define x86_thiscallcc void @"\01??$baz@H@?$ExportedClassTemplate2@H@pr34849@@QAEXXZ" +} //===----------------------------------------------------------------------===// // Classes with template base classes