@@ -2016,8 +2016,7 @@ class AnnotatingParser {
2016
2016
Style.Language == FormatStyle::LK_Java) {
2017
2017
Current.setType (TT_LambdaArrow);
2018
2018
} else if (Current.is (tok::arrow) && AutoFound &&
2019
- (Line.MightBeFunctionDecl || Line.InPPDirective ) &&
2020
- Current.NestingLevel == 0 &&
2019
+ Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2021
2020
!Current.Previous ->isOneOf (tok::kw_operator, tok::identifier)) {
2022
2021
// not auto operator->() -> xxx;
2023
2022
Current.setType (TT_TrailingReturnArrow);
@@ -3252,7 +3251,8 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
3252
3251
// This function heuristically determines whether 'Current' starts the name of a
3253
3252
// function declaration.
3254
3253
static bool isFunctionDeclarationName (bool IsCpp, const FormatToken &Current,
3255
- const AnnotatedLine &Line) {
3254
+ const AnnotatedLine &Line,
3255
+ FormatToken *&ClosingParen) {
3256
3256
assert (Current.Previous );
3257
3257
3258
3258
if (Current.is (TT_FunctionDeclarationName))
@@ -3344,16 +3344,16 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
3344
3344
// Check whether parameter list can belong to a function declaration.
3345
3345
if (!Next || Next->isNot (tok::l_paren) || !Next->MatchingParen )
3346
3346
return false ;
3347
+ ClosingParen = Next->MatchingParen ;
3348
+ assert (ClosingParen->is (tok::r_paren));
3347
3349
// If the lines ends with "{", this is likely a function definition.
3348
3350
if (Line.Last ->is (tok::l_brace))
3349
3351
return true ;
3350
- if (Next->Next == Next-> MatchingParen )
3352
+ if (Next->Next == ClosingParen )
3351
3353
return true ; // Empty parentheses.
3352
3354
// If there is an &/&& after the r_paren, this is likely a function.
3353
- if (Next->MatchingParen ->Next &&
3354
- Next->MatchingParen ->Next ->is (TT_PointerOrReference)) {
3355
+ if (ClosingParen->Next && ClosingParen->Next ->is (TT_PointerOrReference))
3355
3356
return true ;
3356
- }
3357
3357
3358
3358
// Check for K&R C function definitions (and C++ function definitions with
3359
3359
// unnamed parameters), e.g.:
@@ -3370,7 +3370,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
3370
3370
return true ;
3371
3371
}
3372
3372
3373
- for (const FormatToken *Tok = Next->Next ; Tok && Tok != Next-> MatchingParen ;
3373
+ for (const FormatToken *Tok = Next->Next ; Tok && Tok != ClosingParen ;
3374
3374
Tok = Tok->Next ) {
3375
3375
if (Tok->is (TT_TypeDeclarationParen))
3376
3376
return true ;
@@ -3442,11 +3442,12 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
3442
3442
calculateArrayInitializerColumnList (Line);
3443
3443
3444
3444
bool LineIsFunctionDeclaration = false ;
3445
+ FormatToken *ClosingParen = nullptr ;
3445
3446
for (FormatToken *Tok = Current, *AfterLastAttribute = nullptr ; Tok;
3446
3447
Tok = Tok->Next ) {
3447
3448
if (Tok->Previous ->EndsCppAttributeGroup )
3448
3449
AfterLastAttribute = Tok;
3449
- if (isFunctionDeclarationName (Style.isCpp (), *Tok, Line)) {
3450
+ if (isFunctionDeclarationName (Style.isCpp (), *Tok, Line, ClosingParen )) {
3450
3451
LineIsFunctionDeclaration = true ;
3451
3452
Tok->setFinalizedType (TT_FunctionDeclarationName);
3452
3453
if (AfterLastAttribute &&
@@ -3458,29 +3459,38 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
3458
3459
}
3459
3460
}
3460
3461
3461
- if (Style.isCpp () && !LineIsFunctionDeclaration) {
3462
- // Annotate */&/&& in `operator` function calls as binary operators.
3463
- for (const auto *Tok = Line.First ; Tok; Tok = Tok->Next ) {
3464
- if (Tok->isNot (tok::kw_operator))
3465
- continue ;
3466
- do {
3467
- Tok = Tok->Next ;
3468
- } while (Tok && Tok->isNot (TT_OverloadedOperatorLParen));
3469
- if (!Tok)
3470
- break ;
3471
- const auto *LeftParen = Tok;
3472
- for (Tok = Tok->Next ; Tok && Tok != LeftParen->MatchingParen ;
3473
- Tok = Tok->Next ) {
3474
- if (Tok->isNot (tok::identifier))
3475
- continue ;
3476
- auto *Next = Tok->Next ;
3477
- const bool NextIsBinaryOperator =
3478
- Next && Next->isOneOf (tok::star, tok::amp, tok::ampamp) &&
3479
- Next->Next && Next->Next ->is (tok::identifier);
3480
- if (!NextIsBinaryOperator)
3462
+ if (Style.isCpp ()) {
3463
+ if (!LineIsFunctionDeclaration) {
3464
+ // Annotate */&/&& in `operator` function calls as binary operators.
3465
+ for (const auto *Tok = Line.First ; Tok; Tok = Tok->Next ) {
3466
+ if (Tok->isNot (tok::kw_operator))
3481
3467
continue ;
3482
- Next->setType (TT_BinaryOperator);
3483
- Tok = Next;
3468
+ do {
3469
+ Tok = Tok->Next ;
3470
+ } while (Tok && Tok->isNot (TT_OverloadedOperatorLParen));
3471
+ if (!Tok)
3472
+ break ;
3473
+ const auto *LeftParen = Tok;
3474
+ for (Tok = Tok->Next ; Tok && Tok != LeftParen->MatchingParen ;
3475
+ Tok = Tok->Next ) {
3476
+ if (Tok->isNot (tok::identifier))
3477
+ continue ;
3478
+ auto *Next = Tok->Next ;
3479
+ const bool NextIsBinaryOperator =
3480
+ Next && Next->isOneOf (tok::star, tok::amp, tok::ampamp) &&
3481
+ Next->Next && Next->Next ->is (tok::identifier);
3482
+ if (!NextIsBinaryOperator)
3483
+ continue ;
3484
+ Next->setType (TT_BinaryOperator);
3485
+ Tok = Next;
3486
+ }
3487
+ }
3488
+ } else if (ClosingParen) {
3489
+ for (auto *Tok = ClosingParen->Next ; Tok; Tok = Tok->Next ) {
3490
+ if (Tok->is (tok::arrow)) {
3491
+ Tok->setType (TT_TrailingReturnArrow);
3492
+ break ;
3493
+ }
3484
3494
}
3485
3495
}
3486
3496
}
0 commit comments