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

Anutosh Bhat via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 12 04:14:00 PDT 2025


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

>From bd1b0b2a14afeb73755db3a7deb6bffd4f50778c 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/2] Fix error recovery while PTU cleanup

---
 clang/lib/Interpreter/IncrementalParser.cpp |  2 +-
 clang/test/Interpreter/lambda.cpp           | 11 +++++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp b/clang/lib/Interpreter/IncrementalParser.cpp
index 41d6304bd5f65..6343f17ed822a 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -176,7 +176,7 @@ 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().getFETokenInfo() && !D->getLangOpts().ObjC &&
diff --git a/clang/test/Interpreter/lambda.cpp b/clang/test/Interpreter/lambda.cpp
index df75274a050b2..8f49f870fddb6 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 -Xcc -O2 | 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

>From 1963cc341cd356f06a6c582235e47ac3cf144a63 Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Wed, 12 Mar 2025 16:43:45 +0530
Subject: [PATCH 2/2] Fix test as per review

---
 clang/test/Interpreter/lambda.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/clang/test/Interpreter/lambda.cpp b/clang/test/Interpreter/lambda.cpp
index 8f49f870fddb6..7e8dcd61f20c8 100644
--- a/clang/test/Interpreter/lambda.cpp
+++ b/clang/test/Interpreter/lambda.cpp
@@ -1,7 +1,7 @@
 // REQUIRES: host-supports-jit
 // UNSUPPORTED: system-aix
 // RUN: cat %s | clang-repl | FileCheck %s
-// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck %s
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck %s
 
 extern "C" int printf(const char *, ...);
 
@@ -21,4 +21,7 @@ int x = 42;
 // expected-error at +1 {{non-local lambda expression cannot have a capture-default}}
 auto capture = [&]() { return x * 2; };
 
+// Ensure valid C++ code before exiting
+x = 100;
+
 %quit
\ No newline at end of file



More information about the cfe-commits mailing list