Also remove the empty StoredDeclsList entry from the lookup table
authorVassil Vassilev <v.g.vassilev@gmail.com>
Fri, 27 May 2022 11:16:24 +0000 (11:16 +0000)
committerVassil Vassilev <v.g.vassilev@gmail.com>
Fri, 27 May 2022 12:36:37 +0000 (12:36 +0000)
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
clang/unittests/AST/ASTImporterTest.cpp

index d5ad636..13dd6da 100644 (file)
@@ -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()));
   }
index 896e3cb..f196523 100644 (file)
@@ -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