@@ -126,7 +126,9 @@ class AnnotatingParser {
126
126
const AdditionalKeywords &Keywords,
127
127
SmallVector<ScopeType> &Scopes)
128
128
: Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false ),
129
- IsCpp (Style.isCpp()), Keywords(Keywords), Scopes(Scopes) {
129
+ IsCpp (Style.isCpp()), LangOpts(getFormattingLangOpts(Style)),
130
+ Keywords(Keywords), Scopes(Scopes) {
131
+ assert (IsCpp == LangOpts.CXXOperatorNames );
130
132
Contexts.push_back (Context (tok::unknown, 1 , /* IsExpression=*/ false ));
131
133
resetTokenMetadata ();
132
134
}
@@ -562,7 +564,7 @@ class AnnotatingParser {
562
564
(CurrentToken->is (tok::l_paren) && CurrentToken->Next &&
563
565
CurrentToken->Next ->isOneOf (tok::star, tok::amp, tok::caret));
564
566
if ((CurrentToken->Previous ->isOneOf (tok::kw_const, tok::kw_auto) ||
565
- CurrentToken->Previous ->isTypeName (IsCpp )) &&
567
+ CurrentToken->Previous ->isTypeName (LangOpts )) &&
566
568
!(CurrentToken->is (tok::l_brace) ||
567
569
(CurrentToken->is (tok::l_paren) && !ProbablyFunctionTypeLParen))) {
568
570
Contexts.back ().IsExpression = false ;
@@ -2624,7 +2626,7 @@ class AnnotatingParser {
2624
2626
return true ;
2625
2627
2626
2628
// MyClass a;
2627
- if (PreviousNotConst->isTypeName (IsCpp ))
2629
+ if (PreviousNotConst->isTypeName (LangOpts ))
2628
2630
return true ;
2629
2631
2630
2632
// type[] a in Java
@@ -2728,7 +2730,7 @@ class AnnotatingParser {
2728
2730
}
2729
2731
2730
2732
if (Tok.Next ->is (tok::question) ||
2731
- (Tok.Next ->is (tok::ampamp) && !Tok.Previous ->isTypeName (IsCpp ))) {
2733
+ (Tok.Next ->is (tok::ampamp) && !Tok.Previous ->isTypeName (LangOpts ))) {
2732
2734
return false ;
2733
2735
}
2734
2736
@@ -2757,9 +2759,10 @@ class AnnotatingParser {
2757
2759
}
2758
2760
2759
2761
// Heuristically try to determine whether the parentheses contain a type.
2760
- auto IsQualifiedPointerOrReference = [](FormatToken *T, bool IsCpp) {
2762
+ auto IsQualifiedPointerOrReference = [](FormatToken *T,
2763
+ const LangOptions &LangOpts) {
2761
2764
// This is used to handle cases such as x = (foo *const)&y;
2762
- assert (!T->isTypeName (IsCpp ) && " Should have already been checked" );
2765
+ assert (!T->isTypeName (LangOpts ) && " Should have already been checked" );
2763
2766
// Strip trailing qualifiers such as const or volatile when checking
2764
2767
// whether the parens could be a cast to a pointer/reference type.
2765
2768
while (T) {
@@ -2791,8 +2794,8 @@ class AnnotatingParser {
2791
2794
bool ParensAreType =
2792
2795
!Tok.Previous ||
2793
2796
Tok.Previous ->isOneOf (TT_TemplateCloser, TT_TypeDeclarationParen) ||
2794
- Tok.Previous ->isTypeName (IsCpp ) ||
2795
- IsQualifiedPointerOrReference (Tok.Previous , IsCpp );
2797
+ Tok.Previous ->isTypeName (LangOpts ) ||
2798
+ IsQualifiedPointerOrReference (Tok.Previous , LangOpts );
2796
2799
bool ParensCouldEndDecl =
2797
2800
Tok.Next ->isOneOf (tok::equal, tok::semi, tok::l_brace, tok::greater);
2798
2801
if (ParensAreType && !ParensCouldEndDecl)
@@ -3065,6 +3068,7 @@ class AnnotatingParser {
3065
3068
FormatToken *CurrentToken;
3066
3069
bool AutoFound;
3067
3070
bool IsCpp;
3071
+ LangOptions LangOpts;
3068
3072
const AdditionalKeywords &Keywords;
3069
3073
3070
3074
SmallVector<ScopeType> &Scopes;
@@ -3639,7 +3643,8 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
3639
3643
3640
3644
// This function heuristically determines whether 'Current' starts the name of a
3641
3645
// function declaration.
3642
- static bool isFunctionDeclarationName (bool IsCpp, const FormatToken &Current,
3646
+ static bool isFunctionDeclarationName (const LangOptions &LangOpts,
3647
+ const FormatToken &Current,
3643
3648
const AnnotatedLine &Line,
3644
3649
FormatToken *&ClosingParen) {
3645
3650
assert (Current.Previous );
@@ -3658,7 +3663,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
3658
3663
}
3659
3664
3660
3665
auto skipOperatorName =
3661
- [IsCpp ](const FormatToken *Next) -> const FormatToken * {
3666
+ [&LangOpts ](const FormatToken *Next) -> const FormatToken * {
3662
3667
for (; Next; Next = Next->Next ) {
3663
3668
if (Next->is (TT_OverloadedOperatorLParen))
3664
3669
return Next;
@@ -3677,7 +3682,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
3677
3682
Next = Next->Next ;
3678
3683
continue ;
3679
3684
}
3680
- if ((Next->isTypeName (IsCpp ) || Next->is (tok::identifier)) &&
3685
+ if ((Next->isTypeName (LangOpts ) || Next->is (tok::identifier)) &&
3681
3686
Next->Next && Next->Next ->isPointerOrReference ()) {
3682
3687
// For operator void*(), operator char*(), operator Foo*().
3683
3688
Next = Next->Next ;
@@ -3693,8 +3698,10 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
3693
3698
return nullptr ;
3694
3699
};
3695
3700
3701
+ const auto *Next = Current.Next ;
3702
+ const bool IsCpp = LangOpts.CXXOperatorNames ;
3703
+
3696
3704
// Find parentheses of parameter list.
3697
- const FormatToken *Next = Current.Next ;
3698
3705
if (Current.is (tok::kw_operator)) {
3699
3706
if (Previous.Tok .getIdentifierInfo () &&
3700
3707
!Previous.isOneOf (tok::kw_return, tok::kw_co_return)) {
@@ -3774,7 +3781,7 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
3774
3781
Tok = Tok->MatchingParen ;
3775
3782
continue ;
3776
3783
}
3777
- if (Tok->is (tok::kw_const) || Tok->isTypeName (IsCpp ) ||
3784
+ if (Tok->is (tok::kw_const) || Tok->isTypeName (LangOpts ) ||
3778
3785
Tok->isOneOf (TT_PointerOrReference, TT_StartOfName, tok::ellipsis)) {
3779
3786
return true ;
3780
3787
}
@@ -3837,7 +3844,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
3837
3844
AfterLastAttribute = Tok;
3838
3845
if (const bool IsCtorOrDtor = Tok->is (TT_CtorDtorDeclName);
3839
3846
IsCtorOrDtor ||
3840
- isFunctionDeclarationName (IsCpp , *Tok, Line, ClosingParen)) {
3847
+ isFunctionDeclarationName (LangOpts , *Tok, Line, ClosingParen)) {
3841
3848
if (!IsCtorOrDtor)
3842
3849
Tok->setFinalizedType (TT_FunctionDeclarationName);
3843
3850
LineIsFunctionDeclaration = true ;
@@ -4447,7 +4454,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
4447
4454
if (Left.Tok .isLiteral ())
4448
4455
return true ;
4449
4456
// for (auto a = 0, b = 0; const auto & c : {1, 2, 3})
4450
- if (Left.isTypeOrIdentifier (IsCpp ) && Right.Next && Right.Next ->Next &&
4457
+ if (Left.isTypeOrIdentifier (LangOpts ) && Right.Next && Right.Next ->Next &&
4451
4458
Right.Next ->Next ->is (TT_RangeBasedForLoopColon)) {
4452
4459
return getTokenPointerOrReferenceAlignment (Right) !=
4453
4460
FormatStyle::PAS_Left;
@@ -4490,7 +4497,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
4490
4497
if (Right.is (tok::l_brace) && Right.is (BK_Block))
4491
4498
return true ;
4492
4499
// for (auto a = 0, b = 0; const auto& c : {1, 2, 3})
4493
- if (BeforeLeft && BeforeLeft->isTypeOrIdentifier (IsCpp ) && Right.Next &&
4500
+ if (BeforeLeft && BeforeLeft->isTypeOrIdentifier (LangOpts ) && Right.Next &&
4494
4501
Right.Next ->is (TT_RangeBasedForLoopColon)) {
4495
4502
return getTokenPointerOrReferenceAlignment (Left) !=
4496
4503
FormatStyle::PAS_Right;
@@ -4534,7 +4541,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
4534
4541
if (Right.isPointerOrReference ()) {
4535
4542
const FormatToken *Previous = &Left;
4536
4543
while (Previous && Previous->isNot (tok::kw_operator)) {
4537
- if (Previous->is (tok::identifier) || Previous->isTypeName (IsCpp )) {
4544
+ if (Previous->is (tok::identifier) || Previous->isTypeName (LangOpts )) {
4538
4545
Previous = Previous->getPreviousNonComment ();
4539
4546
continue ;
4540
4547
}
@@ -4723,7 +4730,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
4723
4730
if (!Style.isVerilog () &&
4724
4731
(Left.isOneOf (tok::identifier, tok::greater, tok::r_square,
4725
4732
tok::r_paren) ||
4726
- Left.isTypeName (IsCpp )) &&
4733
+ Left.isTypeName (LangOpts )) &&
4727
4734
Right.is (tok::l_brace) && Right.getNextNonComment () &&
4728
4735
Right.isNot (BK_Block)) {
4729
4736
return false ;
0 commit comments