[clang] 8c0eb32 - Also remove the empty StoredDeclsList entry from the lookup table

Vassil Vassilev via cfe-commits cfe-commits at lists.llvm.org
Fri May 27 05:37:05 PDT 2022


Author: Vassil Vassilev
Date: 2022-05-27T12:36:37Z
New Revision: 8c0eb32d2aa0bc73c176d7b25f47bdf37f967d3b

URL: https://github.com/llvm/llvm-project/commit/8c0eb32d2aa0bc73c176d7b25f47bdf37f967d3b
DIFF: https://github.com/llvm/llvm-project/commit/8c0eb32d2aa0bc73c176d7b25f47bdf37f967d3b.diff

LOG: 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

Added: 
    

Modified: 
    clang/lib/AST/DeclBase.cpp
    clang/unittests/AST/ASTImporterTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index d5ad636810544..13dd6da3f24fe 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 896e3cb7a956c..f1965235c9a6d 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


        


More information about the cfe-commits mailing list