[clang] [clang-repl] Fix error recovery while PTU cleanup (PR #127467)

Anutosh Bhat via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 10 23:01:51 PDT 2025


https://github.com/anutosh491 updated https://github.com/llvm/llvm-project/pull/127467

>From 6ff448ed506e0ef75db2c9974a628a965e85df2f Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Mon, 17 Feb 2025 15:33:20 +0530
Subject: [PATCH 1/3] Fix error recovery while PTU cleanup

---
 clang/lib/Interpreter/IncrementalParser.cpp     | 4 ++--
 clang/unittests/Interpreter/InterpreterTest.cpp | 7 +++++++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index e43cea1baf43a..1ebef0e434b3d 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -178,8 +178,8 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl *MostRecentTU) {
     if (!ND)
       continue;
     // Check if we need to clean up the IdResolver chain.
-    if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
-        !D->getLangOpts().CPlusPlus)
+    if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
+        !D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
       S.IdResolver.RemoveDecl(ND);
   }
 }
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index 578f1d4c0eac6..56ab155ebf5a4 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,6 +114,13 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
+
+  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; };").takeError();
+  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression cannot have a capture-default"));
+  EXPECT_EQ("Parsing failed.", llvm::toString(std::move(Err)));
+
+  RecoverErr = Interp->Parse("int validVar = 10;");
+  EXPECT_TRUE(!!RecoverErr);
 }
 
 // Here we test whether the user can mix declarations and statements. The

>From 8b9a112721c45662374fd9995af6da4031507c89 Mon Sep 17 00:00:00 2001
From: Anutosh Bhat <andersonbhat491 at gmail.com>
Date: Wed, 26 Feb 2025 16:55:31 +0530
Subject: [PATCH 2/3] Update IncrementalParser.cpp

---
 clang/lib/Interpreter/IncrementalParser.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index 1ebef0e434b3d..774219d9fc9ab 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -175,11 +175,11 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl *MostRecentTU) {
   // FIXME: We should de-allocate MostRecentTU
   for (Decl *D : MostRecentTU->decls()) {
     auto *ND = dyn_cast<NamedDecl>(D);
-    if (!ND)
+    if (!ND || ND->getDeclName().isEmpty()) 
       continue;
     // Check if we need to clean up the IdResolver chain.
-    if (!ND->getDeclName().isEmpty() && ND->getDeclName().getFETokenInfo() &&
-        !D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
+    if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&
+        !D->getLangOpts().CPlusPlus)
       S.IdResolver.RemoveDecl(ND);
   }
 }

>From dae6ca63f0d443a6ec0df12e1abef183203c12de Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Tue, 11 Mar 2025 11:29:32 +0530
Subject: [PATCH 3/3] Add tests in lambda.cpp

---
 clang/test/Interpreter/lambda.cpp               | 11 +++++++++--
 clang/unittests/Interpreter/InterpreterTest.cpp |  7 -------
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/clang/test/Interpreter/lambda.cpp b/clang/test/Interpreter/lambda.cpp
index df75274a050b2..135f5fe604ae5 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,8 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
+// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify | FileCheck %s
+
 extern "C" int printf(const char *, ...);
 
 auto l1 = []() { printf("ONE\n"); return 42; };
@@ -14,4 +15,10 @@ auto r2 = l2();
 auto r3 = l2();
 // CHECK: TWO
 
-%quit
+// Verify non-local lambda capture error is correctly reported
+int x = 42;
+
+// expected-error at +1 {{non-local lambda expression cannot have a capture-default}}
+auto capture = [&]() { return x * 2; };
+
+%quit
\ No newline at end of file
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index 56ab155ebf5a4..578f1d4c0eac6 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -114,13 +114,6 @@ TEST_F(InterpreterTest, Errors) {
 
   RecoverErr = Interp->Parse("var1 = 424;");
   EXPECT_TRUE(!!RecoverErr);
-
-  Err = Interp->Parse("int x = 5; auto capture = [&]() { return x * 2; };").takeError();
-  EXPECT_THAT(DiagnosticOutput, HasSubstr("error: non-local lambda expression cannot have a capture-default"));
-  EXPECT_EQ("Parsing failed.", llvm::toString(std::move(Err)));
-
-  RecoverErr = Interp->Parse("int validVar = 10;");
-  EXPECT_TRUE(!!RecoverErr);
 }
 
 // Here we test whether the user can mix declarations and statements. The



More information about the cfe-commits mailing list