Skip to content

Commit 1e558da

Browse files
committed
[Clang] Apply exclude_from_explicit_instantiation to dllimport/dllexport
Attaching `__declspec(dllexport/dllimport)` to explicit instantiation declaration made its whole member instantiated even if they were `__attribute__((__exclude_from_explicit_instantation__))`. Such members should not be instantiated nor be exported to avoid symbol leakage or duplication.
1 parent 3234c26 commit 1e558da

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6551,6 +6551,8 @@ void Sema::checkClassLevelDLLAttribute(CXXRecordDecl *Class) {
65516551
return;
65526552
}
65536553

6554+
TemplateSpecializationKind TSK = Class->getTemplateSpecializationKind();
6555+
65546556
if (Context.getTargetInfo().shouldDLLImportComdatSymbols() &&
65556557
!ClassAttr->isInherited()) {
65566558
// Diagnose dll attributes on members of class with dll attribute.
@@ -6561,6 +6563,11 @@ void Sema::checkClassLevelDLLAttribute(CXXRecordDecl *Class) {
65616563
if (!MemberAttr || MemberAttr->isInherited() || Member->isInvalidDecl())
65626564
continue;
65636565

6566+
if ((TSK == TSK_ExplicitInstantiationDeclaration ||
6567+
TSK == TSK_ExplicitInstantiationDefinition) &&
6568+
Member->hasAttr<ExcludeFromExplicitInstantiationAttr>())
6569+
continue;
6570+
65646571
Diag(MemberAttr->getLocation(),
65656572
diag::err_attribute_dll_member_of_dll_class)
65666573
<< MemberAttr << ClassAttr;
@@ -6583,8 +6590,6 @@ void Sema::checkClassLevelDLLAttribute(CXXRecordDecl *Class) {
65836590
!ClassExported &&
65846591
cast<DLLImportAttr>(ClassAttr)->wasPropagatedToBaseTemplate();
65856592

6586-
TemplateSpecializationKind TSK = Class->getTemplateSpecializationKind();
6587-
65886593
// Ignore explicit dllexport on explicit class template instantiation
65896594
// declarations, except in MinGW mode.
65906595
if (ClassExported && !ClassAttr->isInherited() &&
@@ -6601,6 +6606,11 @@ void Sema::checkClassLevelDLLAttribute(CXXRecordDecl *Class) {
66016606
// seem to be true in practice?
66026607

66036608
for (Decl *Member : Class->decls()) {
6609+
if ((TSK == TSK_ExplicitInstantiationDeclaration ||
6610+
TSK == TSK_ExplicitInstantiationDefinition) &&
6611+
Member->hasAttr<ExcludeFromExplicitInstantiationAttr>())
6612+
continue;
6613+
66046614
VarDecl *VD = dyn_cast<VarDecl>(Member);
66056615
CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member);
66066616

0 commit comments

Comments
 (0)