Index: clang/lib/Serialization/ASTWriterDecl.cpp =================================================================== --- clang/lib/Serialization/ASTWriterDecl.cpp +++ clang/lib/Serialization/ASTWriterDecl.cpp @@ -1017,10 +1017,12 @@ // definition in the module interface is provided by the compilation of // that module interface unit, not by its users. (Inline variables are // still emitted in module users.) + // Similarly if a PCH is built with codegen and its own object file. ModulesCodegen = (((Writer.WritingModule && Writer.WritingModule->Kind == Module::ModuleInterfaceUnit) || - Writer.Context->getLangOpts().BuildingPCHWithObjectFile) && + (Writer.Context->getLangOpts().BuildingPCHWithObjectFile && + Writer.Context->getLangOpts().ModulesCodegen)) && Writer.Context->GetGVALinkageForVariable(D) == GVA_StrongExternal); } Record.push_back(ModulesCodegen); @@ -2451,9 +2453,8 @@ bool ModulesCodegen = false; if (!FD->isDependentContext()) { Optional Linkage; - if ((Writer->WritingModule && - Writer->WritingModule->Kind == Module::ModuleInterfaceUnit) || - Writer->Context->getLangOpts().BuildingPCHWithObjectFile) { + if (Writer->WritingModule && + Writer->WritingModule->Kind == Module::ModuleInterfaceUnit) { // When building a C++ Modules TS module interface unit, a strong // definition in the module interface is provided by the compilation of // that module interface unit, not by its users. (Inline functions are Index: clang/test/PCH/Inputs/declspec-selectany-pch.h =================================================================== --- /dev/null +++ clang/test/PCH/Inputs/declspec-selectany-pch.h @@ -0,0 +1,7 @@ +struct CD3DX12_DEFAULT {}; +extern const __declspec(selectany) CD3DX12_DEFAULT D3D12_DEFAULT; + +struct CD3DX12_CPU_DESCRIPTOR_HANDLE { + CD3DX12_CPU_DESCRIPTOR_HANDLE(CD3DX12_DEFAULT) { ptr = 0; } + size_t ptr; +}; Index: clang/test/PCH/Inputs/declspec-selectany-pch.cpp =================================================================== --- /dev/null +++ clang/test/PCH/Inputs/declspec-selectany-pch.cpp @@ -0,0 +1 @@ +#include "declspec-selectany-pch.h" Index: clang/test/PCH/declspec-selectany.cpp =================================================================== --- /dev/null +++ clang/test/PCH/declspec-selectany.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cl /Yc%S/Inputs/declspec-selectany-pch.h %S/Inputs/declspec-selectany-pch.cpp /c /Fo%t.obj /Fp%t.pch +// RUN: %clang_cl /Yu%S/Inputs/declspec-selectany-pch.h %s /I%S/Inputs /c /Fo%t.obj /Fp%t.pch + +#include "declspec-selectany-pch.h" + +int main() { + CD3DX12_CPU_DESCRIPTOR_HANDLE desc = CD3DX12_CPU_DESCRIPTOR_HANDLE(D3D12_DEFAULT); + return desc.ptr; +}