[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 02:43:32 PST 2026


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

>From ed5cd2037676f54bd12b8da3c18d227c074f7e2e Mon Sep 17 00:00:00 2001
From: Jonas Rembser <jonas.rembser at cern.ch>
Date: Thu, 1 Oct 2026 21:06:53 +0000
Subject: [PATCH 1/3] [clang-repl] Undo the enumerators of an unscoped
 enumeration

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 from that context, but its enumerators stayed
visible:

  enum E { Red };
  %undo
  int Red = 5;   // error: redefinition of 'Red' as different kind of symbol

Remove the enumerators of an undone enumeration definition, too.

Assisted-by: Claude Opus 5.5
---
 clang/lib/Interpreter/IncrementalParser.cpp   |  6 +++++
 .../Interpreter/code-undo-enumerators.cpp     | 25 +++++++++++++++++++
 2 files changed, 31 insertions(+)
 create mode 100644 clang/test/Interpreter/code-undo-enumerators.cpp

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index 4d1ba3473d48ef..5a0245faad0ff7 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -291,6 +291,12 @@ class ASTDeclUnmerger : public DeclVisitor<ASTDeclUnmerger> {
   void VisitVarDecl(VarDecl *D) { withdraw(D); }
 
   void VisitTagDecl(TagDecl *D) {
+    // The enumerators of an unscoped enumeration are visible in the enclosing
+    // context.
+    if (auto *ED = dyn_cast<EnumDecl>(D);
+        ED && ED->isThisDeclarationADefinition())
+      VisitDeclContext(ED);
+
     NamedDecl *Prev = findSurvivor(D);
     if (!Prev)
       return;
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

>From b28d1cd8dbd44432464032675aac8bdebc3d51f7 Mon Sep 17 00:00:00 2001
From: Jonas Rembser <jonas.rembser at cern.ch>
Date: Sat, 7 Nov 2026 09:47:11 +0000
Subject: [PATCH 2/3] [clang][AST] Let removeDecl() skip a C declaration
 missing from the TU lookup

In C, a declaration whose semantic context is the translation unit but
that was declared in another context, like a structure declared in
another structure, is only added to the lookup table of the translation
unit if that table already exists (see the FIXME in
makeDeclVisibleInContextWithFlags()). A table that is built later misses
it, because buildLookup() only collects the lexical declarations of the
translation unit.

DeclContext::removeDecl() asserted that the declaration is in the table,
and without assertions it dereferenced the end iterator. Skip the missing
entry instead.

Assisted-by: Claude Opus 5.5
---
 clang/lib/AST/DeclBase.cpp | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

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()));
   }

>From 27e556c2064347c217d122a9bb4b0e53992df549 Mon Sep 17 00:00:00 2001
From: Jonas Rembser <jonas.rembser at cern.ch>
Date: Sat, 7 Nov 2026 09:47:13 +0000
Subject: [PATCH 3/3] [clang-repl] Undo the members of every tag definition

Visit the members of every tag definition that is undone, not only of
enumerations. Besides the enumerators of an unscoped enumeration, this
also undoes the tags and enumerators that C makes visible outside of the
structure or union they are declared in:

  struct A { struct B { int x; } b; enum E { Red } e; };
  %undo
  struct B { double y; };   // error: redefinition of 'B'

Assisted-by: Claude Opus 5.5
---
 clang/lib/Interpreter/IncrementalParser.cpp   | 10 +++----
 .../test/Interpreter/code-undo-tag-members.c  | 29 +++++++++++++++++++
 2 files changed, 34 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Interpreter/code-undo-tag-members.c

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index 5a0245faad0ff7..12c50b91a66407 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -291,11 +291,11 @@ class ASTDeclUnmerger : public DeclVisitor<ASTDeclUnmerger> {
   void VisitVarDecl(VarDecl *D) { withdraw(D); }
 
   void VisitTagDecl(TagDecl *D) {
-    // The enumerators of an unscoped enumeration are visible in the enclosing
-    // context.
-    if (auto *ED = dyn_cast<EnumDecl>(D);
-        ED && ED->isThisDeclarationADefinition())
-      VisitDeclContext(ED);
+    // 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 = findSurvivor(D);
     if (!Prev)
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