[clang] [clang-format] Fix a bug in parsing C-style cast lambda (PR #136099)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 17 00:39:45 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

<details>
<summary>Changes</summary>

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
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) {

``````````

</details>


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


More information about the cfe-commits mailing list