-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[clang-format] Fix a bug in parsing C-style cast of lambdas #136099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFix #135959 Full diff: https://github.com/llvm/llvm-project/pull/136099.diff 2 Files Affected:
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) {
|
@@ -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)))) || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And if it is casted to int(*)
?
I know we didn't annotate stuff here, it is hard to detect if this is a cast, but is there maybe a different way which would be a bit more resilient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the handling of casting to a function pointer, the only other case I can think of.
Fix #135959