From 8c0eb32d2aa0bc73c176d7b25f47bdf37f967d3b Mon Sep 17 00:00:00 2001 From: Vassil Vassilev Date: Fri, 27 May 2022 11:16:24 +0000 Subject: [PATCH] Also remove the empty StoredDeclsList entry from the lookup table In case where we have removed all declarations for a given declaration name entry we should remove the whole StoredDeclsMap entry. This patch improves consistency in the lookup tables and helps cling/clang-repl error recovery. Differential revision: https://reviews.llvm.org/D119675 --- clang/lib/AST/DeclBase.cpp | 6 +++++- clang/unittests/AST/ASTImporterTest.cpp | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp index d5ad636..13dd6da 100644 --- a/clang/lib/AST/DeclBase.cpp +++ b/clang/lib/AST/DeclBase.cpp @@ -1537,7 +1537,11 @@ void DeclContext::removeDecl(Decl *D) { if (Map) { StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName()); assert(Pos != Map->end() && "no lookup entry for decl"); - Pos->second.remove(ND); + StoredDeclsList &List = Pos->second; + List.remove(ND); + // Clean up the entry if there are no more decls. + if (List.isNull()) + Map->erase(Pos); } } while (DC->isTransparentContext() && (DC = DC->getParent())); } diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index 896e3cb..f196523 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4279,6 +4279,10 @@ TEST_P(DeclContextTest, // This asserts in the old implementation. DC->removeDecl(A0); EXPECT_FALSE(DC->containsDecl(A0)); + + // Make sure we do not leave a StoredDeclsList with no entries. + DC->removeDecl(A1); + ASSERT_EQ(Map->find(A1->getDeclName()), Map->end()); } struct ImportFunctionTemplateSpecializations -- 2.7.4