@@ -1079,38 +1079,28 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
10791079 if (!RD->isExternallyVisible ())
10801080 return llvm::GlobalVariable::InternalLinkage;
10811081
1082- bool IsInNamedModule = RD->isInNamedModule ();
1083- // If the CXXRecordDecl are not in a module unit, we need to get
1084- // its key function. We're at the end of the translation unit, so the current
1085- // key function is fully correct.
1086- const CXXMethodDecl *keyFunction =
1087- IsInNamedModule ? nullptr : Context.getCurrentKeyFunction (RD);
1088- if (IsInNamedModule || (keyFunction && !RD->hasAttr <DLLImportAttr>())) {
1082+ // We're at the end of the translation unit, so the current key
1083+ // function is fully correct.
1084+ const CXXMethodDecl *keyFunction = Context.getCurrentKeyFunction (RD);
1085+ if (keyFunction && !RD->hasAttr <DLLImportAttr>()) {
10891086 // If this class has a key function, use that to determine the
10901087 // linkage of the vtable.
10911088 const FunctionDecl *def = nullptr ;
1092- if (keyFunction && keyFunction ->hasBody (def))
1089+ if (keyFunction->hasBody (def))
10931090 keyFunction = cast<CXXMethodDecl>(def);
10941091
1095- bool IsExternalDefinition =
1096- IsInNamedModule ? RD->shouldEmitInExternalSource () : !def;
1097-
1098- TemplateSpecializationKind Kind =
1099- IsInNamedModule ? RD->getTemplateSpecializationKind ()
1100- : keyFunction->getTemplateSpecializationKind ();
1101-
1102- switch (Kind) {
1103- case TSK_Undeclared:
1104- case TSK_ExplicitSpecialization:
1092+ switch (keyFunction->getTemplateSpecializationKind ()) {
1093+ case TSK_Undeclared:
1094+ case TSK_ExplicitSpecialization:
11051095 assert (
1106- (IsInNamedModule || def || CodeGenOpts.OptimizationLevel > 0 ||
1096+ (def || CodeGenOpts.OptimizationLevel > 0 ||
11071097 CodeGenOpts.getDebugInfo () != llvm::codegenoptions::NoDebugInfo) &&
1108- " Shouldn't query vtable linkage without the class in module units , "
1109- " key function, optimizations, or debug info" );
1110- if (IsExternalDefinition && CodeGenOpts.OptimizationLevel > 0 )
1098+ " Shouldn't query vtable linkage without key function , "
1099+ " optimizations, or debug info" );
1100+ if (!def && CodeGenOpts.OptimizationLevel > 0 )
11111101 return llvm::GlobalVariable::AvailableExternallyLinkage;
11121102
1113- if (keyFunction && keyFunction ->isInlined ())
1103+ if (keyFunction->isInlined ())
11141104 return !Context.getLangOpts ().AppleKext
11151105 ? llvm::GlobalVariable::LinkOnceODRLinkage
11161106 : llvm::Function::InternalLinkage;
@@ -1129,7 +1119,7 @@ CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) {
11291119
11301120 case TSK_ExplicitInstantiationDeclaration:
11311121 llvm_unreachable (" Should not have been asked to emit this" );
1132- }
1122+ }
11331123 }
11341124
11351125 // -fapple-kext mode does not support weak linkage, so we must use
@@ -1223,21 +1213,6 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
12231213 TSK == TSK_ExplicitInstantiationDefinition)
12241214 return false ;
12251215
1226- // Itanium C++ ABI [5.2.3]:
1227- // Virtual tables for dynamic classes are emitted as follows:
1228- //
1229- // - If the class is templated, the tables are emitted in every object that
1230- // references any of them.
1231- // - Otherwise, if the class is attached to a module, the tables are uniquely
1232- // emitted in the object for the module unit in which it is defined.
1233- // - Otherwise, if the class has a key function (see below), the tables are
1234- // emitted in the object for the translation unit containing the definition of
1235- // the key function. This is unique if the key function is not inline.
1236- // - Otherwise, the tables are emitted in every object that references any of
1237- // them.
1238- if (RD->isInNamedModule ())
1239- return RD->shouldEmitInExternalSource ();
1240-
12411216 // Otherwise, if the class doesn't have a key function (possibly
12421217 // anymore), the vtable must be defined here.
12431218 const CXXMethodDecl *keyFunction = CGM.getContext ().getCurrentKeyFunction (RD);
@@ -1247,7 +1222,13 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
12471222 const FunctionDecl *Def;
12481223 // Otherwise, if we don't have a definition of the key function, the
12491224 // vtable must be defined somewhere else.
1250- return !keyFunction->hasBody (Def);
1225+ if (!keyFunction->hasBody (Def))
1226+ return true ;
1227+
1228+ assert (Def && " The body of the key function is not assigned to Def?" );
1229+ // If the non-inline key function comes from another module unit, the vtable
1230+ // must be defined there.
1231+ return Def->isInAnotherModuleUnit () && !Def->isInlineSpecified ();
12511232}
12521233
12531234// / Given that we're currently at the end of the translation unit, and
0 commit comments