Skip to content

[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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Apr 17, 2025

Fix #135959

@llvmbot
Copy link
Member

llvmbot commented Apr 17, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fix #135959


Full diff: https://github.com/llvm/llvm-project/pull/136099.diff

2 Files Affected:

  • (modified) clang/lib/Format/UnwrappedLineParser.cpp (+2-1)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+5)
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)))) ||
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@owenca owenca changed the title [clang-format] Fix a bug in parsing C-style cast lambda [clang-format] Fix a bug in parsing C-style cast of lambdas Apr 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-format] Incorrect formatting when applying C-style cast to lambda expression
3 participants