[clang] Fix error recovery while PTU cleanup (PR #127467)
Anutosh Bhat via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 17 02:08:08 PST 2025
https://github.com/anutosh491 created https://github.com/llvm/llvm-project/pull/127467
Fixes #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"
```
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.

>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] 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
More information about the cfe-commits
mailing list