[clang] [clang-format] Fix a bug in parsing C-style cast lambda (PR #136099)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 17 00:39:10 PDT 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/136099
Fix #135959
>From 53033b278f89a508d079614eadd58b103211d5dd Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 17 Apr 2025 00:35:19 -0700
Subject: [PATCH] [clang-format] Fix a bug in parsing C-style cast lambda
Fix #135959
---
clang/lib/Format/UnwrappedLineParser.cpp | 3 ++-
clang/unittests/Format/TokenAnnotatorTest.cpp | 5 +++++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index b9430d4389feb..4442e965e0f51 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2371,7 +2371,8 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
if ((Previous && ((Previous->Tok.getIdentifierInfo() &&
!Previous->isOneOf(tok::kw_return, tok::kw_co_await,
tok::kw_co_yield, tok::kw_co_return)) ||
- Previous->closesScope())) ||
+ (Previous->closesScope() &&
+ !Previous->endsSequence(tok::r_paren, tok::greater)))) ||
LeftSquare->isCppStructuredBinding(IsCpp)) {
return false;
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 2c7319ccefec2..7a07fbd0250be 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2159,6 +2159,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsLambdas) {
// FIXME:
// EXPECT_TOKEN(Tokens[13], tok::l_paren, TT_LambdaDefinitionLParen);
EXPECT_TOKEN(Tokens[17], tok::l_brace, TT_LambdaLBrace);
+
+ Tokens = annotate("auto foo{(std::function<int()>)[] { return 0; }};");
+ ASSERT_EQ(Tokens.size(), 23u) << Tokens;
+ EXPECT_TOKEN(Tokens[13], tok::l_square, TT_LambdaLSquare);
+ EXPECT_TOKEN(Tokens[15], tok::l_brace, TT_LambdaLBrace);
}
TEST_F(TokenAnnotatorTest, UnderstandsFunctionAnnotations) {
More information about the cfe-commits
mailing list