diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index af19e89fd9d0..633538f3129e 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -5550,6 +5550,10 @@ bool ASTContext::DeclIsRequiredFunctionOrFileScopedVar(const Decl *D) { } else if (!isa(D)) return false; + // Weak references don't produce any output by themselves. + if (D->hasAttr()) + return false; + // Aliases and used decls are required. if (D->hasAttr() || D->hasAttr()) return true; @@ -5587,6 +5591,9 @@ bool ASTContext::DeclIsRequiredFunctionOrFileScopedVar(const Decl *D) { const VarDecl *VD = cast(D); assert(VD->isFileVarDecl() && "Expected file scoped var"); + if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) + return false; + // Structs that have non-trivial constructors or destructors are required. // FIXME: Handle references. diff --git a/clang/test/PCH/cxx-templates.h b/clang/test/PCH/cxx-templates.h index 47fa11eb0805..92932199fa82 100644 --- a/clang/test/PCH/cxx-templates.h +++ b/clang/test/PCH/cxx-templates.h @@ -109,3 +109,10 @@ template struct S_PR7660 { void g(void (*)(T)); }; template class C_PR7670; template<> class C_PR7670; template<> class C_PR7670; + +template +struct S2 { + static bool V; +}; + +extern template class S2;