Skip to content

Commit 4eeba53

Browse files
owencamahesh-attarde
authored andcommitted
[clang-format] Fix bugs in annotating arrows and square brackets (llvm#160973)
Fixes llvm#160518
1 parent 49d6ba4 commit 4eeba53

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -833,11 +833,6 @@ class AnnotatingParser {
833833
if (Parent && Parent->is(TT_PointerOrReference))
834834
Parent->overwriteFixedType(TT_BinaryOperator);
835835
}
836-
// An arrow after an ObjC method expression is not a lambda arrow.
837-
if (CurrentToken->is(TT_ObjCMethodExpr) && CurrentToken->Next &&
838-
CurrentToken->Next->is(TT_LambdaArrow)) {
839-
CurrentToken->Next->overwriteFixedType(TT_Unknown);
840-
}
841836
Left->MatchingParen = CurrentToken;
842837
CurrentToken->MatchingParen = Left;
843838
// FirstObjCSelectorName is set when a colon is found. This does

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ bool UnwrappedLineParser::tryToParseLambda() {
22682268
if (!tryToParseLambdaIntroducer())
22692269
return false;
22702270

2271-
bool SeenArrow = false;
2271+
FormatToken *Arrow = nullptr;
22722272
bool InTemplateParameterList = false;
22732273

22742274
while (FormatTok->isNot(tok::l_brace)) {
@@ -2343,17 +2343,13 @@ bool UnwrappedLineParser::tryToParseLambda() {
23432343
case tok::ellipsis:
23442344
case tok::kw_true:
23452345
case tok::kw_false:
2346-
if (SeenArrow || InTemplateParameterList) {
2346+
if (Arrow || InTemplateParameterList) {
23472347
nextToken();
23482348
break;
23492349
}
23502350
return true;
23512351
case tok::arrow:
2352-
// This might or might not actually be a lambda arrow (this could be an
2353-
// ObjC method invocation followed by a dereferencing arrow). We might
2354-
// reset this back to TT_Unknown in TokenAnnotator.
2355-
FormatTok->setFinalizedType(TT_LambdaArrow);
2356-
SeenArrow = true;
2352+
Arrow = FormatTok;
23572353
nextToken();
23582354
break;
23592355
case tok::kw_requires: {
@@ -2375,6 +2371,9 @@ bool UnwrappedLineParser::tryToParseLambda() {
23752371
FormatTok->setFinalizedType(TT_LambdaLBrace);
23762372
LSquare.setFinalizedType(TT_LambdaLSquare);
23772373

2374+
if (Arrow)
2375+
Arrow->setFinalizedType(TT_LambdaArrow);
2376+
23782377
NestedLambdas.push_back(Line->SeenDecltypeAuto);
23792378
parseChildBlock();
23802379
assert(!NestedLambdas.empty());
@@ -2388,11 +2387,6 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
23882387
const FormatToken *LeftSquare = FormatTok;
23892388
nextToken();
23902389
if (Previous) {
2391-
if (Previous->Tok.getIdentifierInfo() &&
2392-
!Previous->isOneOf(tok::kw_return, tok::kw_co_await, tok::kw_co_yield,
2393-
tok::kw_co_return)) {
2394-
return false;
2395-
}
23962390
if (Previous->closesScope()) {
23972391
// Not a potential C-style cast.
23982392
if (Previous->isNot(tok::r_paren))
@@ -2402,6 +2396,13 @@ bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
24022396
// and `int (*)()`.
24032397
if (!BeforeRParen || !BeforeRParen->isOneOf(tok::greater, tok::r_paren))
24042398
return false;
2399+
} else if (Previous->is(tok::star)) {
2400+
Previous = Previous->getPreviousNonComment();
2401+
}
2402+
if (Previous && Previous->Tok.getIdentifierInfo() &&
2403+
!Previous->isOneOf(tok::kw_return, tok::kw_co_await, tok::kw_co_yield,
2404+
tok::kw_co_return)) {
2405+
return false;
24052406
}
24062407
}
24072408
if (LeftSquare->isCppStructuredBinding(IsCpp))

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsLambdas) {
22372237
ASSERT_EQ(Tokens.size(), 21u) << Tokens;
22382238
EXPECT_TOKEN(Tokens[11], tok::l_square, TT_LambdaLSquare);
22392239
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_LambdaLBrace);
2240+
2241+
Tokens = annotate("SomeFunction({[]() -> int *[] { return {}; }});");
2242+
ASSERT_EQ(Tokens.size(), 22u) << Tokens;
2243+
EXPECT_TOKEN(Tokens[3], tok::l_square, TT_LambdaLSquare);
2244+
EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_LambdaDefinitionLParen);
2245+
EXPECT_TOKEN(Tokens[10], tok::l_square, TT_ArraySubscriptLSquare);
22402246
}
22412247

22422248
TEST_F(TokenAnnotatorTest, UnderstandsFunctionAnnotations) {
@@ -4159,6 +4165,14 @@ TEST_F(TokenAnnotatorTest, LineCommentTrailingBackslash) {
41594165
EXPECT_TOKEN(Tokens[1], tok::comment, TT_LineComment);
41604166
}
41614167

4168+
TEST_F(TokenAnnotatorTest, ArrowAfterSubscript) {
4169+
auto Tokens =
4170+
annotate("return (getStructType()->getElements())[eIdx]->getName();");
4171+
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
4172+
// Not TT_LambdaArrow.
4173+
EXPECT_TOKEN(Tokens[13], tok::arrow, TT_Unknown);
4174+
}
4175+
41624176
TEST_F(TokenAnnotatorTest, QtProperty) {
41634177
auto Style = getLLVMStyle();
41644178
Style.AllowBreakBeforeQtProperty = true;

0 commit comments

Comments
 (0)