[clang] [clang-repl] Undo the enumerators of an unscoped enumeration (PR #228370)

Jonas Rembser via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 23:25:32 PST 2026


================
@@ -1789,12 +1789,20 @@ void DeclContext::removeDecl(Decl *D) {
       StoredDeclsMap *Map = DC->getPrimaryContext()->LookupPtr;
       if (Map) {
         StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
-        assert(Pos != Map->end() && "no lookup entry for decl");
-        StoredDeclsList &List = Pos->second;
-        List.remove(ND);
-        // Clean up the entry if there are no more decls.
-        if (List.isNull())
-          Map->erase(Pos);
+        // In C, the lookup table of the translation unit can miss a declaration
+        // that was declared outside of it, like a structure declared in
+        // another one (see makeDeclVisibleInContextWithFlags).
+        assert((Pos != Map->end() ||
----------------
guitargeek wrote:

Yes. Without the change to this file, `code-undo-tag-members.c` fails on exactly this assertion when the second `%undo` undoes `A`. It is the only Interpreter test that fails:

```
clang/lib/AST/DeclBase.cpp:1792: void clang::DeclContext::removeDecl(Decl *): Assertion `Pos != Map->end() && "no lookup entry for decl"' failed.
 #9 clang::DeclContext::removeDecl(clang::Decl*)
#10 clang::ASTDeclUnmerger::VisitNamedDecl(clang::NamedDecl*)
#11 clang::NamedDecl* clang::ASTDeclUnmerger::VisitRedeclarable<clang::TagDecl>(clang::Redeclarable<clang::TagDecl>*)
#12 clang::ASTDeclUnmerger::VisitTagDecl(clang::TagDecl*)
#13 clang::ASTDeclUnmerger::VisitDeclContext(clang::DeclContext*)
#14 clang::ASTDeclUnmerger::VisitTagDecl(clang::TagDecl*)
#15 clang::ASTDeclUnmerger::VisitDeclContext(clang::DeclContext*)
#16 clang::IncrementalParser::CleanUpPTU(clang::TranslationUnitDecl*)
#17 clang::Interpreter::Undo(unsigned int)
```

With the change, I logged which declarations take the new path across all Interpreter tests: only `B`, `Red` and `E`, all from this test. All three are missing from the lookup table of the translation unit, and the test is C: that's exactly the case the assert now allows. The `1` before the undo is what builds that table (the first value printing looks up the value printing functions in the translation unit). Without it, or with it before `A`, the assertion doesn't fire even without the change.

https://github.com/llvm/llvm-project/pull/228370


More information about the cfe-commits mailing list