Index: cfe/trunk/lib/CodeGen/CGDecl.cpp =================================================================== --- cfe/trunk/lib/CodeGen/CGDecl.cpp +++ cfe/trunk/lib/CodeGen/CGDecl.cpp @@ -240,7 +240,7 @@ getModule(), LTy, Ty.isConstant(getContext()), Linkage, Init, Name, nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS); GV->setAlignment(getContext().getDeclAlign(&D).getQuantity()); - setGlobalVisibility(GV, &D); + setGlobalVisibility(GV, &D, ForDefinition); if (supportsCOMDAT() && GV->isWeakForLinker()) GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); Index: cfe/trunk/lib/CodeGen/CGVTT.cpp =================================================================== --- cfe/trunk/lib/CodeGen/CGVTT.cpp +++ cfe/trunk/lib/CodeGen/CGVTT.cpp @@ -100,7 +100,7 @@ VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName())); // Set the right visibility. - CGM.setGlobalVisibility(VTT, RD); + CGM.setGlobalVisibility(VTT, RD, ForDefinition); } llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) { Index: cfe/trunk/lib/CodeGen/CGVTables.cpp =================================================================== --- cfe/trunk/lib/CodeGen/CGVTables.cpp +++ cfe/trunk/lib/CodeGen/CGVTables.cpp @@ -51,7 +51,7 @@ static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD, const ThunkInfo &Thunk, llvm::Function *Fn) { - CGM.setGlobalVisibility(Fn, MD); + CGM.setGlobalVisibility(Fn, MD, ForDefinition); } static void setThunkProperties(CodeGenModule &CGM, const ThunkInfo &Thunk, @@ -730,7 +730,7 @@ // Create the variable that will hold the construction vtable. llvm::GlobalVariable *VTable = CGM.CreateOrReplaceCXXRuntimeVariable(Name, VTType, Linkage); - CGM.setGlobalVisibility(VTable, RD); + CGM.setGlobalVisibility(VTable, RD, ForDefinition); // V-tables are always unnamed_addr. VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); Index: cfe/trunk/lib/CodeGen/CodeGenModule.h =================================================================== --- cfe/trunk/lib/CodeGen/CodeGenModule.h +++ cfe/trunk/lib/CodeGen/CodeGenModule.h @@ -710,7 +710,8 @@ llvm::ConstantInt *getSize(CharUnits numChars); /// Set the visibility for the given LLVM GlobalValue. - void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const; + void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D, + ForDefinition_t IsForDefinition) const; /// Set the TLS mode for the given LLVM GlobalValue for the thread-local /// variable declaration D. Index: cfe/trunk/lib/CodeGen/CodeGenModule.cpp =================================================================== --- cfe/trunk/lib/CodeGen/CodeGenModule.cpp +++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp @@ -662,7 +662,8 @@ } void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, - const NamedDecl *D) const { + const NamedDecl *D, + ForDefinition_t IsForDefinition) const { // Internal definitions always have default visibility. if (GV->hasLocalLinkage()) { GV->setVisibility(llvm::GlobalValue::DefaultVisibility); @@ -671,7 +672,8 @@ // Set visibility for definitions. LinkageInfo LV = D->getLinkageAndVisibility(); - if (LV.isVisibilityExplicit() || !GV->hasAvailableExternallyLinkage()) + if (LV.isVisibilityExplicit() || + (IsForDefinition && !GV->hasAvailableExternallyLinkage())) GV->setVisibility(GetLLVMVisibility(LV.getVisibility())); } @@ -1052,7 +1054,7 @@ void CodeGenModule::SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV) { if (const auto *ND = dyn_cast_or_null(D)) - setGlobalVisibility(GV, ND); + setGlobalVisibility(GV, ND, ForDefinition); else GV->setVisibility(llvm::GlobalValue::DefaultVisibility); @@ -1108,8 +1110,8 @@ setNonAliasAttributes(D, F); } -static void setLinkageAndVisibilityForGV(llvm::GlobalValue *GV, - const NamedDecl *ND) { +static void setLinkageForGV(llvm::GlobalValue *GV, + const NamedDecl *ND) { // Set linkage and visibility in case we never see a definition. LinkageInfo LV = ND->getLinkageAndVisibility(); if (!isExternallyVisible(LV.getLinkage())) { @@ -1125,10 +1127,6 @@ // separate linkage types for this. GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); } - - // Set visibility on a declaration only if it's explicit. - if (LV.isVisibilityExplicit()) - GV->setVisibility(CodeGenModule::GetLLVMVisibility(LV.getVisibility())); } } @@ -1197,7 +1195,8 @@ // Only a few attributes are set on declarations; these may later be // overridden by a definition. - setLinkageAndVisibilityForGV(F, FD); + setLinkageForGV(F, FD); + setGlobalVisibility(F, FD, NotForDefinition); if (FD->getAttr()) { F->addFnAttr("implicit-section-name"); @@ -2434,7 +2433,8 @@ GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); - setLinkageAndVisibilityForGV(GV, D); + setLinkageForGV(GV, D); + setGlobalVisibility(GV, D, NotForDefinition); if (D->getTLSKind()) { if (D->getTLSKind() == VarDecl::TLS_Dynamic) @@ -3253,7 +3253,7 @@ setFunctionDLLStorageClass(GD, Fn); // FIXME: this is redundant with part of setFunctionDefinitionAttributes - setGlobalVisibility(Fn, D); + setGlobalVisibility(Fn, D, ForDefinition); MaybeHandleStaticInExternC(D, Fn); @@ -3849,7 +3849,7 @@ getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(), /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS); if (emitter) emitter->finalize(GV); - setGlobalVisibility(GV, VD); + setGlobalVisibility(GV, VD, ForDefinition); GV->setAlignment(Align.getQuantity()); if (supportsCOMDAT() && GV->isWeakForLinker()) GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); Index: cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp =================================================================== --- cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp +++ cfe/trunk/lib/CodeGen/ItaniumCXXABI.cpp @@ -1527,7 +1527,7 @@ VTable->setComdat(CGM.getModule().getOrInsertComdat(VTable->getName())); // Set the right visibility. - CGM.setGlobalVisibility(VTable, RD); + CGM.setGlobalVisibility(VTable, RD, ForDefinition); // Use pointer alignment for the vtable. Otherwise we would align them based // on the size of the initializer which doesn't make sense as only single @@ -1637,6 +1637,7 @@ VTable = CGM.CreateOrReplaceCXXRuntimeVariable( Name, VTableType, llvm::GlobalValue::ExternalLinkage); VTable->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); + CGM.setGlobalVisibility(VTable, RD, NotForDefinition); if (RD->hasAttr()) VTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); Index: cfe/trunk/test/CodeGen/push-hidden-visibility-subclass.cpp =================================================================== --- cfe/trunk/test/CodeGen/push-hidden-visibility-subclass.cpp +++ cfe/trunk/test/CodeGen/push-hidden-visibility-subclass.cpp @@ -0,0 +1,21 @@ +// RUN: %clang -S -emit-llvm -o %t %s +// RUN: FileCheck --input-file=%t %s + +#pragma GCC visibility push(hidden) + +struct Base { + virtual ~Base() = default; + virtual void* Alloc() = 0; +}; + +class Child : public Base { +public: + Child() = default; + void* Alloc(); +}; + +void test() { + Child x; +} + +// CHECK: @_ZTV5Child = external hidden unnamed_addr constant