Skip to content

Commit 80303cb

Browse files
authored
[clang-format] Handle attributes before lambda return arrow (#94119)
Fixes #92657.
1 parent 5367b2c commit 80303cb

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,7 @@ bool UnwrappedLineParser::tryToParseLambda() {
22362236
bool InTemplateParameterList = false;
22372237

22382238
while (FormatTok->isNot(tok::l_brace)) {
2239-
if (FormatTok->isTypeName(LangOpts)) {
2239+
if (FormatTok->isTypeName(LangOpts) || FormatTok->isAttribute()) {
22402240
nextToken();
22412241
continue;
22422242
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22664,6 +22664,7 @@ TEST_F(FormatTest, FormatsLambdas) {
2266422664
verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});");
2266522665
verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});");
2266622666
verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});");
22667+
verifyFormat("foo([&](u32 bar) __attribute__((always_inline)) -> void {});");
2266722668
verifyFormat("return int{[x = x]() { return x; }()};");
2266822669

2266922670
// Lambdas with explicit template argument lists.

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsLambdas) {
15771577
EXPECT_TOKEN(Tokens[2], tok::arrow, TT_TrailingReturnArrow);
15781578
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_LambdaLBrace);
15791579

1580+
Tokens = annotate("foo([&](u32 bar) __attribute__((attr)) -> void {});");
1581+
ASSERT_EQ(Tokens.size(), 22u) << Tokens;
1582+
EXPECT_TOKEN(Tokens[2], tok::l_square, TT_LambdaLSquare);
1583+
EXPECT_TOKEN(Tokens[15], tok::arrow, TT_TrailingReturnArrow);
1584+
EXPECT_TOKEN(Tokens[17], tok::l_brace, TT_LambdaLBrace);
1585+
15801586
Tokens = annotate("[] <typename T> () {}");
15811587
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
15821588
EXPECT_TOKEN(Tokens[0], tok::l_square, TT_LambdaLSquare);

0 commit comments

Comments
 (0)