Index: clang/include/clang/AST/DeclBase.h =================================================================== --- clang/include/clang/AST/DeclBase.h +++ clang/include/clang/AST/DeclBase.h @@ -2427,6 +2427,8 @@ bool Deserialize = false) const; private: + void addDeclImpl(Decl *D, bool Internal); + /// Whether this declaration context has had externally visible /// storage added since the last lookup. In this case, \c LookupPtr's /// invariant may not hold and needs to be fixed before we perform Index: clang/lib/AST/DeclBase.cpp =================================================================== --- clang/lib/AST/DeclBase.cpp +++ clang/lib/AST/DeclBase.cpp @@ -1570,20 +1570,25 @@ } } -void DeclContext::addDecl(Decl *D) { +void DeclContext::addDeclImpl(Decl *D, bool Internal) { + DeclContext *PrimaryContext = nullptr; + NamedDecl *ND = nullptr; + // Calculate the primary context before adding the decl. Calculating the + // primary context might end up unintentionally adding the new decl to the + // same lookup we will manually add it at the end of this function. + if ((ND = dyn_cast(D))) + PrimaryContext = ND->getDeclContext()->getPrimaryContext(); + addHiddenDecl(D); - if (auto *ND = dyn_cast(D)) - ND->getDeclContext()->getPrimaryContext()-> - makeDeclVisibleInContextWithFlags(ND, false, true); + if (ND) + PrimaryContext->makeDeclVisibleInContextWithFlags(ND, Internal, true); } -void DeclContext::addDeclInternal(Decl *D) { - addHiddenDecl(D); +void DeclContext::addDecl(Decl *D) { addDeclImpl(D, /*Internal*/ false); } - if (auto *ND = dyn_cast(D)) - ND->getDeclContext()->getPrimaryContext()-> - makeDeclVisibleInContextWithFlags(ND, true, true); +void DeclContext::addDeclInternal(Decl *D) { + addDeclImpl(D, /*Internal*/ true); } /// buildLookup - Build the lookup data structure with all of the