Skip to content

Commit 3b4c51b

Browse files
authored
[clang-repl] Fix error recovery while PTU cleanup (llvm#127467)
Fixes llvm#123300 What is seen ``` clang-repl> int x = 42; clang-repl> auto capture = [&]() { return x * 2; }; In file included from <<< inputs >>>:1: input_line_4:1:17: error: non-local lambda expression cannot have a capture-default 1 | auto capture = [&]() { return x * 2; }; | ^ zsh: segmentation fault clang-repl --Xcc="-v" (lldb) bt * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x8) * frame #0: 0x0000000107b4f8b8 libclang-cpp.19.1.dylib`clang::IncrementalParser::CleanUpPTU(clang::PartialTranslationUnit&) + 988 frame #1: 0x0000000107b4f1b4 libclang-cpp.19.1.dylib`clang::IncrementalParser::ParseOrWrapTopLevelDecl() + 416 frame #2: 0x0000000107b4fb94 libclang-cpp.19.1.dylib`clang::IncrementalParser::Parse(llvm::StringRef) + 612 frame #3: 0x0000000107b52fec libclang-cpp.19.1.dylib`clang::Interpreter::ParseAndExecute(llvm::StringRef, clang::Value*) + 180 frame #4: 0x0000000100003498 clang-repl`main + 3560 frame #5: 0x000000018d39a0e0 dyld`start + 2360 ``` Though the error is justified, we shouldn't be interested in exiting through a segfault in such cases. The issue is that empty named decls weren't being taken care of resulting into this assert https://github.com/llvm/llvm-project/blob/c1a229252617ed58f943bf3f4698bd8204ee0f04/clang/include/clang/AST/DeclarationName.h#L503 Can also be seen when the example is attempted through xeus-cpp-lite. ![image](https://github.com/user-attachments/assets/9b0e6ead-138e-4b06-9ad9-fcb9f8d5bf6e)
1 parent a4b9e82 commit 3b4c51b

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

clang/lib/Interpreter/IncrementalParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void IncrementalParser::CleanUpPTU(TranslationUnitDecl *MostRecentTU) {
176176
// FIXME: We should de-allocate MostRecentTU
177177
for (Decl *D : MostRecentTU->decls()) {
178178
auto *ND = dyn_cast<NamedDecl>(D);
179-
if (!ND)
179+
if (!ND || ND->getDeclName().isEmpty())
180180
continue;
181181
// Check if we need to clean up the IdResolver chain.
182182
if (ND->getDeclName().getFETokenInfo() && !D->getLangOpts().ObjC &&

clang/test/Interpreter/lambda.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// REQUIRES: host-supports-jit
22
// UNSUPPORTED: system-aix
33
// RUN: cat %s | clang-repl | FileCheck %s
4-
// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
4+
// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify -Xcc -O2 | FileCheck %s
5+
56
extern "C" int printf(const char *, ...);
67

78
auto l1 = []() { printf("ONE\n"); return 42; };
@@ -14,4 +15,14 @@ auto r2 = l2();
1415
auto r3 = l2();
1516
// CHECK: TWO
1617

17-
%quit
18+
// Verify non-local lambda capture error is correctly reported
19+
int x = 42;
20+
21+
// expected-error {{non-local lambda expression cannot have a capture-default}}
22+
auto capture = [&]() { return x * 2; };
23+
24+
// Ensure interpreter continues and x is still valid
25+
printf("x = %d\n", x);
26+
// CHECK: x = 42
27+
28+
%quit

0 commit comments

Comments
 (0)