From: Douglas Gregor Date: Wed, 6 Jan 2010 20:27:16 +0000 (+0000) Subject: Fix marking of virtual members for nested classes whose first non-pure virtual functi... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9a;p=clang Fix marking of virtual members for nested classes whose first non-pure virtual function has a body inlined in the class git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92855 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGVtable.cpp b/lib/CodeGen/CGVtable.cpp index c04a8fabf3..d2ba77d335 100644 --- a/lib/CodeGen/CGVtable.cpp +++ b/lib/CodeGen/CGVtable.cpp @@ -1490,52 +1490,8 @@ void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) { return; } - llvm::GlobalVariable::LinkageTypes Linkage = - llvm::GlobalVariable::InternalLinkage; - if (RD->isInAnonymousNamespace() || !RD->hasLinkage()) - Linkage = llvm::GlobalVariable::InternalLinkage; - else if (KeyFunction && !MD->isInlined()) { - switch (MD->getTemplateSpecializationKind()) { - case TSK_Undeclared: - case TSK_ExplicitSpecialization: - Linkage = llvm::GlobalVariable::ExternalLinkage; - break; - - case TSK_ImplicitInstantiation: - case TSK_ExplicitInstantiationDefinition: - Linkage = llvm::GlobalVariable::WeakODRLinkage; - break; - - case TSK_ExplicitInstantiationDeclaration: - // FIXME: Use available_externally linkage. However, this currently - // breaks LLVM's build due to undefined symbols. - // Linkage = llvm::GlobalVariable::AvailableExternallyLinkage; - Linkage = llvm::GlobalVariable::WeakODRLinkage; - break; - } - } - else if (KeyFunction) - Linkage = llvm::GlobalVariable::WeakODRLinkage; - else { - Linkage = llvm::GlobalVariable::WeakODRLinkage; - - switch (RD->getTemplateSpecializationKind()) { - case TSK_Undeclared: - case TSK_ExplicitSpecialization: - case TSK_ImplicitInstantiation: - case TSK_ExplicitInstantiationDefinition: - break; - - case TSK_ExplicitInstantiationDeclaration: - // FIXME: Use available_externally linkage. However, this currently - // breaks LLVM's build due to undefined symbols. - // Linkage = llvm::GlobalVariable::AvailableExternallyLinkage; - break; - } - } - // Emit the data. - GenerateClassData(Linkage, RD); + GenerateClassData(CGM.getVtableLinkage(RD), RD); for (CXXRecordDecl::method_iterator i = RD->method_begin(), e = RD->method_end(); i != e; ++i) { diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 85d57e7cbe..e0e8c54434 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -887,6 +887,60 @@ void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { EmitGlobalVarDefinition(D); } +llvm::GlobalVariable::LinkageTypes +CodeGenModule::getVtableLinkage(const CXXRecordDecl *RD) { + // Get the key function. + const CXXMethodDecl *KeyFunction = getContext().getKeyFunction(RD); + + if (KeyFunction) { + const FunctionDecl *Def = 0; + if (KeyFunction->getBody(Def)) + KeyFunction = cast(Def); + } + + if (RD->isInAnonymousNamespace() || !RD->hasLinkage()) + return llvm::GlobalVariable::InternalLinkage; + else if (KeyFunction) { + switch (KeyFunction->getTemplateSpecializationKind()) { + case TSK_Undeclared: + case TSK_ExplicitSpecialization: + if (KeyFunction->isInlined()) + return llvm::GlobalVariable::WeakODRLinkage; + + return llvm::GlobalVariable::ExternalLinkage; + + case TSK_ImplicitInstantiation: + case TSK_ExplicitInstantiationDefinition: + return llvm::GlobalVariable::WeakODRLinkage; + + case TSK_ExplicitInstantiationDeclaration: + // FIXME: Use available_externally linkage. However, this currently + // breaks LLVM's build due to undefined symbols. + // return llvm::GlobalVariable::AvailableExternallyLinkage; + return llvm::GlobalVariable::WeakODRLinkage; + } + } else if (KeyFunction) { + return llvm::GlobalVariable::WeakODRLinkage; + } else { + switch (RD->getTemplateSpecializationKind()) { + case TSK_Undeclared: + case TSK_ExplicitSpecialization: + case TSK_ImplicitInstantiation: + case TSK_ExplicitInstantiationDefinition: + return llvm::GlobalVariable::WeakODRLinkage; + + case TSK_ExplicitInstantiationDeclaration: + // FIXME: Use available_externally linkage. However, this currently + // breaks LLVM's build due to undefined symbols. + // return llvm::GlobalVariable::AvailableExternallyLinkage; + return llvm::GlobalVariable::WeakODRLinkage; + } + } + + // Silence GCC warning. + return llvm::GlobalVariable::WeakODRLinkage; +} + static CodeGenModule::GVALinkage GetLinkageForVariable(ASTContext &Context, const VarDecl *VD) { // Everything located semantically within an anonymous namespace is diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index a31e23b2c7..575b518767 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -407,6 +407,10 @@ public: GVA_TemplateInstantiation }; + /// getVtableLinkage - Return the appropriate linkage for the vtable, VTT, + /// and type information of the given class. + llvm::GlobalVariable::LinkageTypes getVtableLinkage(const CXXRecordDecl *RD); + private: /// UniqueMangledName - Unique a name by (if necessary) inserting it into the /// MangledNames string map. diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index c9757d728a..2e6f8bc687 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -5693,14 +5693,40 @@ void Sema::MaybeMarkVirtualMembersReferenced(SourceLocation Loc, if (!RD->isDynamicClass()) return; - // Only out-of-line definitions matter. - if (!MD->isOutOfLine()) + // Ignore declarations that are not definitions. + if (!MD->isThisDeclarationADefinition()) return; - const CXXMethodDecl *KeyFunction = Context.getKeyFunction(RD); - if (!KeyFunction || KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl()) + if (isa(MD)) { + switch (MD->getParent()->getTemplateSpecializationKind()) { + case TSK_Undeclared: + case TSK_ExplicitSpecialization: + // Classes that aren't instantiations of templates don't need their + // virtual methods marked until we see the definition of the key + // function. + return; + + case TSK_ImplicitInstantiation: + case TSK_ExplicitInstantiationDeclaration: + case TSK_ExplicitInstantiationDefinition: + // This is a constructor of a class template; mark all of the virtual + // members as referenced to ensure that they get instantiatied. + break; + } + } else if (!MD->isOutOfLine()) { + // Consider only out-of-line definitions of member functions. When we see + // an inline definition, it's too early to compute the key function. return; - + } else if (const CXXMethodDecl *KeyFunction = Context.getKeyFunction(RD)) { + // If this is not the key function, we don't need to mark virtual members. + if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl()) + return; + } else { + // The class has no key function, so we've already noted that we need to + // mark the virtual members of this class. + return; + } + // We will need to mark all of the virtual members as referenced to build the // vtable. ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(RD, Loc)); diff --git a/test/SemaTemplate/virtual-member-functions.cpp b/test/SemaTemplate/virtual-member-functions.cpp index a9eb729ed5..db243130bb 100644 --- a/test/SemaTemplate/virtual-member-functions.cpp +++ b/test/SemaTemplate/virtual-member-functions.cpp @@ -41,3 +41,15 @@ struct Derived : Base { template struct Derived; // expected-note{{instantiation}} +template +struct HasOutOfLineKey { + HasOutOfLineKey() { } // expected-note{{in instantiation of member function 'HasOutOfLineKey::f' requested here}} + virtual T *f(float *fp); +}; + +template +T *HasOutOfLineKey::f(float *fp) { + return fp; // expected-error{{cannot initialize return object of type 'int *' with an lvalue of type 'float *'}} +} + +HasOutOfLineKey out_of_line;