diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index f30a6ff78f9e..8b7863107b4a 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -2467,14 +2467,18 @@ void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { /// it, apply them to D. This is a bit tricky because PD can have attributes /// specified in many different places, and we need to find and apply them all. void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { - // Handle #pragma weak - if (NamedDecl *ND = dyn_cast(D)) { - if (ND->hasLinkage()) { - WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier()); - if (W != WeakInfo()) { - // Identifier referenced by #pragma weak before it was declared - DeclApplyPragmaWeak(S, ND, W); - WeakUndeclaredIdentifiers[ND->getIdentifier()] = W; + // It's valid to "forward-declare" #pragma weak, in which case we + // have to do this. + if (!WeakUndeclaredIdentifiers.empty()) { + if (NamedDecl *ND = dyn_cast(D)) { + if (IdentifierInfo *Id = ND->getIdentifier()) { + llvm::DenseMap::iterator I + = WeakUndeclaredIdentifiers.find(Id); + if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { + WeakInfo W = I->second; + DeclApplyPragmaWeak(S, ND, W); + WeakUndeclaredIdentifiers[Id] = W; + } } } }