[clang] b258009 - [clang-repl] Restore the lookup of out-of-line definitions' predecessors (#229780)

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 03:54:03 PST 2026


Author: Jonas Rembser
Date: 2026-11-07T13:53:58+02:00
New Revision: b258009541e2f350b917f798de424c4423a9764a

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

LOG: [clang-repl] Restore the lookup of out-of-line definitions' predecessors (#229780)

Undoing the out-of-line definition of a member removes it from the
lookup table of its semantic context, its class or namespace, and then
makes its surviving previous declaration visible again. But the previous
declaration was made visible in the lookup table of the lexical context,
the enclosing context of the definition. The member was then not found
in its class or namespace anymore, but in the enclosing context instead:

  struct K { static int s; };
  int K::s = 4;
  %undo
  int K::s = 5;  // error: no member named 's' in 'K'

Make the previous declaration visible in its own semantic context.

Assisted-by: Claude Opus 5.5

Added: 
    clang/test/Interpreter/code-undo-out-of-line.cpp

Modified: 
    clang/lib/Interpreter/IncrementalParser.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index 4d1ba3473d48ef..9b3ff8b55fb5f1 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -200,11 +200,6 @@ class ASTDeclUnmerger : public DeclVisitor<ASTDeclUnmerger> {
   Sema &S;
   TranslationUnitDecl *DiscardedTU;
 
-  template <typename DeclT> void withdraw(Redeclarable<DeclT> *DBase) {
-    if (NamedDecl *Prev = findSurvivor(static_cast<DeclT *>(DBase)))
-      unlinkRedeclChain(S.getASTContext(), DBase, Prev);
-  }
-
   /// The newest declaration of whatever D redeclares that still lives outside
   /// the DiscardedTU, or null if DiscardedTU introduced the name.
   NamedDecl *findSurvivor(NamedDecl *D) const {
@@ -257,22 +252,12 @@ class ASTDeclUnmerger : public DeclVisitor<ASTDeclUnmerger> {
   /// and restore the redeclaration chain to previous state
   void VisitDeclContext(DeclContext *DC) {
     llvm::SmallVector<Decl *, 8> Members(DC->decls());
-    llvm::SmallVector<NamedDecl *, 8> Survivors;
     for (Decl *M : Members) {
-      if (auto *ND = dyn_cast<NamedDecl>(M))
-        if (NamedDecl *Prev = findSurvivor(ND))
-          Survivors.push_back(Prev);
-      Visit(M);          // restore redecls
-      DC->removeDecl(M); // remove from lookup
-      if (auto *ND = dyn_cast<NamedDecl>(M))
-        removeFromLookups(ND);
+      Visit(M); // restore redecls and lookup
+      // VisitNamedDecl() removes the named decls.
+      if (!isa<NamedDecl>(M))
+        DC->removeDecl(M);
     }
-
-    // Restore lookup for the surviving predecessor
-    // of any removed decl that had a surviving predecessor
-    DeclContext *Primary = DC->getPrimaryContext();
-    for (NamedDecl *Prev : Survivors)
-      Primary->makeDeclVisibleInContext(Prev);
   }
 
 public:
@@ -284,21 +269,50 @@ class ASTDeclUnmerger : public DeclVisitor<ASTDeclUnmerger> {
       VisitDeclContext(DC);
   }
 
-  void VisitFunctionDecl(FunctionDecl *D) { withdraw(D); }
-  void VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { withdraw(D); }
-  void VisitTypedefNameDecl(TypedefNameDecl *D) { withdraw(D); }
-  void VisitUsingShadowDecl(UsingShadowDecl *D) { withdraw(D); }
-  void VisitVarDecl(VarDecl *D) { withdraw(D); }
+  /// Remove ND from its context and the lookup tables, and make the
+  /// declaration it redeclares visible again. Called by VisitRedeclarable()
+  /// before it restores the redeclaration chain, which hides that declaration.
+  void VisitNamedDecl(NamedDecl *ND) {
+    // The templated decl of a template is not a member of its context: the
+    // template is.
+    DeclContext *DC = ND->getLexicalDeclContext();
+    if (!DC->containsDecl(ND))
+      return;
+    NamedDecl *Prev = findSurvivor(ND);
+    DC->removeDecl(ND);
+    removeFromLookups(ND);
+    // Like removeDecl(), use the semantic context: the out-of-line definition
+    // of a member is in DC, but it was removed from the lookup table of its
+    // class or namespace.
+    if (Prev)
+      Prev->getDeclContext()->makeDeclVisibleInContext(Prev);
+  }
 
-  void VisitTagDecl(TagDecl *D) {
+  /// Remove D like VisitNamedDecl() and restore its redeclaration chain to the
+  /// previous state. Returns the surviving previous declaration, if any.
+  /// DeclVisitor doesn't dispatch to it: Redeclarable is not a Decl class.
+  template <typename DeclT>
+  NamedDecl *VisitRedeclarable(Redeclarable<DeclT> *DBase) {
+    auto *D = static_cast<DeclT *>(DBase);
+    VisitNamedDecl(D);
     NamedDecl *Prev = findSurvivor(D);
-    if (!Prev)
-      return;
-    unlinkRedeclChain(S.getASTContext(), D, Prev);
+    if (Prev)
+      unlinkRedeclChain(S.getASTContext(), DBase, Prev);
+    return Prev;
+  }
+
+  void VisitFunctionDecl(FunctionDecl *D) { VisitRedeclarable(D); }
+  void VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { VisitRedeclarable(D); }
+  void VisitTypedefNameDecl(TypedefNameDecl *D) { VisitRedeclarable(D); }
+  void VisitUsingShadowDecl(UsingShadowDecl *D) { VisitRedeclarable(D); }
+  void VisitVarDecl(VarDecl *D) { VisitRedeclarable(D); }
+
+  void VisitTagDecl(TagDecl *D) {
+    NamedDecl *Prev = VisitRedeclarable(D);
 
     // A class definition is kept in DefinitionData outside the
     // redeclaration chain
-    auto *RD = dyn_cast<CXXRecordDecl>(Prev);
+    auto *RD = dyn_cast_if_present<CXXRecordDecl>(Prev);
     if (!RD)
       return;
     if (CXXRecordDecl *Def = RD->getDefinition();
@@ -308,7 +322,7 @@ class ASTDeclUnmerger : public DeclVisitor<ASTDeclUnmerger> {
   }
 
   void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
-    withdraw(D);
+    VisitRedeclarable(D);
     Visit(D->getTemplatedDecl());
   }
 
@@ -318,7 +332,7 @@ class ASTDeclUnmerger : public DeclVisitor<ASTDeclUnmerger> {
     // PTU2: namespace outer { namespace ns { class Foo { ... }; error; } }
     // Foo's redeclaration needs to be restored
     VisitDeclContext(D);
-    withdraw(D);
+    VisitRedeclarable(D);
   }
 
   void VisitTranslationUnitDecl(TranslationUnitDecl *D) { VisitDeclContext(D); }

diff  --git a/clang/test/Interpreter/code-undo-out-of-line.cpp b/clang/test/Interpreter/code-undo-out-of-line.cpp
new file mode 100644
index 00000000000000..af54fb7fe0836a
--- /dev/null
+++ b/clang/test/Interpreter/code-undo-out-of-line.cpp
@@ -0,0 +1,36 @@
+// RUN: cat %s | clang-repl | FileCheck %s
+
+// Undoing the out-of-line definition of a member restores its previous
+// declaration in the lookup table of its class or namespace, not in the
+// enclosing context of the definition.
+
+struct K { static int s; };
+int K::s = 4;
+%undo
+int K::s = 5;
+K::s
+// CHECK: (int) 5
+
+struct C { int m(); };
+int C::m() { return 1; }
+%undo
+int C::m() { return 2; }
+C().m()
+// CHECK-NEXT: (int) 2
+
+namespace M { int f(); extern int v; }
+int M::f() { return 1; }
+int M::v = 1;
+%undo
+%undo
+int M::f() { return 3; }
+int M::v = 4;
+M::f() + M::v
+// CHECK-NEXT: (int) 7
+
+// The members are not visible in the enclosing context.
+int s = 8, m = 9, f = 10, v = 11;
+s + m + f + v
+// CHECK-NEXT: (int) 38
+
+%quit


        


More information about the cfe-commits mailing list