@@ -2014,8 +2014,7 @@ class AnnotatingParser {
2014
2014
Style.Language == FormatStyle::LK_Java) {
2015
2015
Current.setType (TT_LambdaArrow);
2016
2016
} else if (Current.is (tok::arrow) && AutoFound &&
2017
- (Line.MightBeFunctionDecl || Line.InPPDirective ) &&
2018
- Current.NestingLevel == 0 &&
2017
+ Line.MightBeFunctionDecl && Current.NestingLevel == 0 &&
2019
2018
!Current.Previous ->isOneOf (tok::kw_operator, tok::identifier)) {
2020
2019
// not auto operator->() -> xxx;
2021
2020
Current.setType (TT_TrailingReturnArrow);
@@ -3250,7 +3249,8 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
3250
3249
// This function heuristically determines whether 'Current' starts the name of a
3251
3250
// function declaration.
3252
3251
static bool isFunctionDeclarationName (bool IsCpp, const FormatToken &Current,
3253
- const AnnotatedLine &Line) {
3252
+ const AnnotatedLine &Line,
3253
+ FormatToken *&ClosingParen) {
3254
3254
assert (Current.Previous );
3255
3255
3256
3256
if (Current.is (TT_FunctionDeclarationName))
@@ -3336,16 +3336,16 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
3336
3336
// Check whether parameter list can belong to a function declaration.
3337
3337
if (!Next || Next->isNot (tok::l_paren) || !Next->MatchingParen )
3338
3338
return false ;
3339
+ ClosingParen = Next->MatchingParen ;
3340
+ assert (ClosingParen->is (tok::r_paren));
3339
3341
// If the lines ends with "{", this is likely a function definition.
3340
3342
if (Line.Last ->is (tok::l_brace))
3341
3343
return true ;
3342
- if (Next->Next == Next-> MatchingParen )
3344
+ if (Next->Next == ClosingParen )
3343
3345
return true ; // Empty parentheses.
3344
3346
// If there is an &/&& after the r_paren, this is likely a function.
3345
- if (Next->MatchingParen ->Next &&
3346
- Next->MatchingParen ->Next ->is (TT_PointerOrReference)) {
3347
+ if (ClosingParen->Next && ClosingParen->Next ->is (TT_PointerOrReference))
3347
3348
return true ;
3348
- }
3349
3349
3350
3350
// Check for K&R C function definitions (and C++ function definitions with
3351
3351
// unnamed parameters), e.g.:
@@ -3362,7 +3362,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
3362
3362
return true ;
3363
3363
}
3364
3364
3365
- for (const FormatToken *Tok = Next->Next ; Tok && Tok != Next-> MatchingParen ;
3365
+ for (const FormatToken *Tok = Next->Next ; Tok && Tok != ClosingParen ;
3366
3366
Tok = Tok->Next ) {
3367
3367
if (Tok->is (TT_TypeDeclarationParen))
3368
3368
return true ;
@@ -3434,11 +3434,12 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
3434
3434
calculateArrayInitializerColumnList (Line);
3435
3435
3436
3436
bool LineIsFunctionDeclaration = false ;
3437
+ FormatToken *ClosingParen = nullptr ;
3437
3438
for (FormatToken *Tok = Current, *AfterLastAttribute = nullptr ; Tok;
3438
3439
Tok = Tok->Next ) {
3439
3440
if (Tok->Previous ->EndsCppAttributeGroup )
3440
3441
AfterLastAttribute = Tok;
3441
- if (isFunctionDeclarationName (Style.isCpp (), *Tok, Line)) {
3442
+ if (isFunctionDeclarationName (Style.isCpp (), *Tok, Line, ClosingParen )) {
3442
3443
LineIsFunctionDeclaration = true ;
3443
3444
Tok->setFinalizedType (TT_FunctionDeclarationName);
3444
3445
if (AfterLastAttribute &&
@@ -3450,29 +3451,38 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
3450
3451
}
3451
3452
}
3452
3453
3453
- if (Style.isCpp () && !LineIsFunctionDeclaration) {
3454
- // Annotate */&/&& in `operator` function calls as binary operators.
3455
- for (const auto *Tok = Line.First ; Tok; Tok = Tok->Next ) {
3456
- if (Tok->isNot (tok::kw_operator))
3457
- continue ;
3458
- do {
3459
- Tok = Tok->Next ;
3460
- } while (Tok && Tok->isNot (TT_OverloadedOperatorLParen));
3461
- if (!Tok)
3462
- break ;
3463
- const auto *LeftParen = Tok;
3464
- for (Tok = Tok->Next ; Tok && Tok != LeftParen->MatchingParen ;
3465
- Tok = Tok->Next ) {
3466
- if (Tok->isNot (tok::identifier))
3467
- continue ;
3468
- auto *Next = Tok->Next ;
3469
- const bool NextIsBinaryOperator =
3470
- Next && Next->isOneOf (tok::star, tok::amp, tok::ampamp) &&
3471
- Next->Next && Next->Next ->is (tok::identifier);
3472
- if (!NextIsBinaryOperator)
3454
+ if (Style.isCpp ()) {
3455
+ if (!LineIsFunctionDeclaration) {
3456
+ // Annotate */&/&& in `operator` function calls as binary operators.
3457
+ for (const auto *Tok = Line.First ; Tok; Tok = Tok->Next ) {
3458
+ if (Tok->isNot (tok::kw_operator))
3473
3459
continue ;
3474
- Next->setType (TT_BinaryOperator);
3475
- Tok = Next;
3460
+ do {
3461
+ Tok = Tok->Next ;
3462
+ } while (Tok && Tok->isNot (TT_OverloadedOperatorLParen));
3463
+ if (!Tok)
3464
+ break ;
3465
+ const auto *LeftParen = Tok;
3466
+ for (Tok = Tok->Next ; Tok && Tok != LeftParen->MatchingParen ;
3467
+ Tok = Tok->Next ) {
3468
+ if (Tok->isNot (tok::identifier))
3469
+ continue ;
3470
+ auto *Next = Tok->Next ;
3471
+ const bool NextIsBinaryOperator =
3472
+ Next && Next->isOneOf (tok::star, tok::amp, tok::ampamp) &&
3473
+ Next->Next && Next->Next ->is (tok::identifier);
3474
+ if (!NextIsBinaryOperator)
3475
+ continue ;
3476
+ Next->setType (TT_BinaryOperator);
3477
+ Tok = Next;
3478
+ }
3479
+ }
3480
+ } else if (ClosingParen) {
3481
+ for (auto *Tok = ClosingParen->Next ; Tok; Tok = Tok->Next ) {
3482
+ if (Tok->is (tok::arrow)) {
3483
+ Tok->setType (TT_TrailingReturnArrow);
3484
+ break ;
3485
+ }
3476
3486
}
3477
3487
}
3478
3488
}
0 commit comments