[clang] e44fff5 - [clang-repl] Undo the enumerators of an unscoped enumeration (#228370)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 7 23:59:34 PST 2026
Author: Jonas Rembser
Date: 2026-11-08T09:59:29+02:00
New Revision: e44fff5decc7889bdf6b15bac4ff36563a6dfdbe
URL: https://github.com/llvm/llvm-project/commit/e44fff5decc7889bdf6b15bac4ff36563a6dfdbe
DIFF: https://github.com/llvm/llvm-project/commit/e44fff5decc7889bdf6b15bac4ff36563a6dfdbe.diff
LOG: [clang-repl] Undo the enumerators of an unscoped enumeration (#228370)
The enumerators of an unscoped enumeration are in the lookup table of
the enclosing context. Undoing the input that declared the enumeration
removed the enumeration, but its enumerators stayed visible:
```
clang-repl> enum E { Red };
clang-repl> %undo
clang-repl> int Red = 5;
error: redefinition of 'Red' as different kind of symbol
```
`ASTDeclUnmerger` now also visits the declarations of an undone
enumeration definition.
**Test:** new lit test `code-undo-enumerators.cpp` with three cases: an
enumerator reused as a variable, an enumeration redefined, and an
enumeration in a namespace. It checks printed values only, not
`printf()` output, whose order isn't reliable when stdout is a pipe
(#228358).
Context: these patches come from moving [ROOT](https://root.cern) from
Cling to clang-repl. ROOT builds a Cling-compatible layer on
`clang::Interpreter`, and its test suite found the issues below.
Assisted-by: Claude Opus 5.5
Added:
clang/test/Interpreter/code-undo-enumerators.cpp
clang/test/Interpreter/code-undo-tag-members.c
Modified:
clang/lib/AST/DeclBase.cpp
clang/lib/Interpreter/IncrementalParser.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index a3ddcb3df01b64..aac5beec9a741c 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -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() ||
+ (DC->isTranslationUnit() &&
+ !getParentASTContext().getLangOpts().CPlusPlus)) &&
+ "no lookup entry for decl");
+ if (Pos != Map->end()) {
+ 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/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index 9b3ff8b55fb5f1..ebd949b3d385d4 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -308,6 +308,12 @@ class ASTDeclUnmerger : public DeclVisitor<ASTDeclUnmerger> {
void VisitVarDecl(VarDecl *D) { VisitRedeclarable(D); }
void VisitTagDecl(TagDecl *D) {
+ // Some members of a definition are visible outside of it: the enumerators
+ // of an unscoped enumeration, and in C the tags and enumerators declared
+ // in a structure or union.
+ if (D->isThisDeclarationADefinition())
+ VisitDeclContext(D);
+
NamedDecl *Prev = VisitRedeclarable(D);
// A class definition is kept in DefinitionData outside the
diff --git a/clang/test/Interpreter/code-undo-enumerators.cpp b/clang/test/Interpreter/code-undo-enumerators.cpp
new file mode 100644
index 00000000000000..5e3c9f9444fb81
--- /dev/null
+++ b/clang/test/Interpreter/code-undo-enumerators.cpp
@@ -0,0 +1,25 @@
+// RUN: cat %s | clang-repl | FileCheck %s
+
+// Undoing an unscoped enumeration also undoes its enumerators, which are
+// visible in the enclosing context. Only value printing is checked here:
+// the output of printf() is buffered separately and may come out of order.
+
+enum E { Red, Green };
+%undo
+int Red = 5;
+Red
+// CHECK: (int) 5
+
+enum E2 { Blue };
+%undo
+enum E2 : unsigned { Blue = 7 };
+Blue
+// CHECK-NEXT: (E2) (Blue) : unsigned int 7
+
+namespace N { enum F { Yellow = 1 }; }
+%undo
+namespace N { int Yellow = 8; }
+N::Yellow
+// CHECK-NEXT: (int) 8
+
+%quit
diff --git a/clang/test/Interpreter/code-undo-tag-members.c b/clang/test/Interpreter/code-undo-tag-members.c
new file mode 100644
index 00000000000000..6590dba0ed960e
--- /dev/null
+++ b/clang/test/Interpreter/code-undo-tag-members.c
@@ -0,0 +1,29 @@
+// REQUIRES: host-supports-jit
+// RUN: cat %s | clang-repl -Xcc -xc | FileCheck %s
+
+// In C, the tags and enumerators declared in a structure or union are visible
+// in the enclosing scope. Undoing the structure also undoes them.
+//
+// The first value printing looks up the value printing functions in the
+// translation unit. That builds its lookup table, which then misses B, E and
+// Red because they were declared inside of A.
+
+struct A { struct B { int x; } b; enum E { Red } e; enum { Green } g; };
+1
+%undo
+%undo
+// CHECK: (int) 1
+struct B { double y; };
+enum E { Blue = 3 };
+int Red = 5;
+int Green = 6;
+(struct B){1.5}.y
+// CHECK-NEXT: (double) 1.5000000
+Blue
+// CHECK-NEXT: (int) 3
+Red
+// CHECK-NEXT: (int) 5
+Green
+// CHECK-NEXT: (int) 6
+
+%quit
More information about the cfe-commits
mailing list