@@ -2661,18 +2661,27 @@ class AnnotatingParser {
2661
2661
2662
2662
// / Determine whether ')' is ending a cast.
2663
2663
bool rParenEndsCast (const FormatToken &Tok) {
2664
+ assert (Tok.is (tok::r_paren));
2665
+
2666
+ if (!Tok.MatchingParen || !Tok.Previous )
2667
+ return false ;
2668
+
2664
2669
// C-style casts are only used in C++, C# and Java.
2665
- if (!Style. isCSharp () && !IsCpp && Style.Language != FormatStyle::LK_Java)
2670
+ if (!IsCpp && !Style. isCSharp () && Style.Language != FormatStyle::LK_Java)
2666
2671
return false ;
2667
2672
2673
+ const auto *LParen = Tok.MatchingParen ;
2674
+ const auto *BeforeRParen = Tok.Previous ;
2675
+ const auto *AfterRParen = Tok.Next ;
2676
+
2668
2677
// Empty parens aren't casts and there are no casts at the end of the line.
2669
- if (Tok. Previous == Tok. MatchingParen || !Tok. Next || !Tok. MatchingParen )
2678
+ if (BeforeRParen == LParen || !AfterRParen )
2670
2679
return false ;
2671
2680
2672
- if (Tok. MatchingParen ->is (TT_OverloadedOperatorLParen))
2681
+ if (LParen ->is (TT_OverloadedOperatorLParen))
2673
2682
return false ;
2674
2683
2675
- FormatToken *LeftOfParens = Tok. MatchingParen ->getPreviousNonComment ();
2684
+ auto *LeftOfParens = LParen ->getPreviousNonComment ();
2676
2685
if (LeftOfParens) {
2677
2686
// If there is a closing parenthesis left of the current
2678
2687
// parentheses, look past it as these might be chained casts.
@@ -2728,37 +2737,38 @@ class AnnotatingParser {
2728
2737
}
2729
2738
}
2730
2739
2731
- if (Tok. Next ->is (tok::question) ||
2732
- (Tok. Next ->is (tok::ampamp) && !Tok. Previous ->isTypeName (LangOpts))) {
2740
+ if (AfterRParen ->is (tok::question) ||
2741
+ (AfterRParen ->is (tok::ampamp) && !BeforeRParen ->isTypeName (LangOpts))) {
2733
2742
return false ;
2734
2743
}
2735
2744
2736
2745
// `foreach((A a, B b) in someList)` should not be seen as a cast.
2737
- if (Tok. Next ->is (Keywords.kw_in ) && Style.isCSharp ())
2746
+ if (AfterRParen ->is (Keywords.kw_in ) && Style.isCSharp ())
2738
2747
return false ;
2739
2748
2740
2749
// Functions which end with decorations like volatile, noexcept are unlikely
2741
2750
// to be casts.
2742
- if (Tok. Next ->isOneOf (tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2743
- tok::kw_requires, tok::kw_throw, tok::arrow,
2744
- Keywords.kw_override , Keywords.kw_final ) ||
2745
- isCppAttribute (IsCpp, *Tok. Next )) {
2751
+ if (AfterRParen ->isOneOf (tok::kw_noexcept, tok::kw_volatile, tok::kw_const,
2752
+ tok::kw_requires, tok::kw_throw, tok::arrow,
2753
+ Keywords.kw_override , Keywords.kw_final ) ||
2754
+ isCppAttribute (IsCpp, *AfterRParen )) {
2746
2755
return false ;
2747
2756
}
2748
2757
2749
2758
// As Java has no function types, a "(" after the ")" likely means that this
2750
2759
// is a cast.
2751
- if (Style.Language == FormatStyle::LK_Java && Tok. Next ->is (tok::l_paren))
2760
+ if (Style.Language == FormatStyle::LK_Java && AfterRParen ->is (tok::l_paren))
2752
2761
return true ;
2753
2762
2754
2763
// If a (non-string) literal follows, this is likely a cast.
2755
- if (Tok.Next ->isOneOf (tok::kw_sizeof, tok::kw_alignof) ||
2756
- (Tok.Next ->Tok .isLiteral () && Tok.Next ->isNot (tok::string_literal))) {
2764
+ if (AfterRParen->isOneOf (tok::kw_sizeof, tok::kw_alignof) ||
2765
+ (AfterRParen->Tok .isLiteral () &&
2766
+ AfterRParen->isNot (tok::string_literal))) {
2757
2767
return true ;
2758
2768
}
2759
2769
2760
2770
// Heuristically try to determine whether the parentheses contain a type.
2761
- auto IsQualifiedPointerOrReference = [](FormatToken *T,
2771
+ auto IsQualifiedPointerOrReference = [](const FormatToken *T,
2762
2772
const LangOptions &LangOpts) {
2763
2773
// This is used to handle cases such as x = (foo *const)&y;
2764
2774
assert (!T->isTypeName (LangOpts) && " Should have already been checked" );
@@ -2791,12 +2801,11 @@ class AnnotatingParser {
2791
2801
return T && T->is (TT_PointerOrReference);
2792
2802
};
2793
2803
bool ParensAreType =
2794
- !Tok.Previous ||
2795
- Tok.Previous ->isOneOf (TT_TemplateCloser, TT_TypeDeclarationParen) ||
2796
- Tok.Previous ->isTypeName (LangOpts) ||
2797
- IsQualifiedPointerOrReference (Tok.Previous , LangOpts);
2804
+ BeforeRParen->isOneOf (TT_TemplateCloser, TT_TypeDeclarationParen) ||
2805
+ BeforeRParen->isTypeName (LangOpts) ||
2806
+ IsQualifiedPointerOrReference (BeforeRParen, LangOpts);
2798
2807
bool ParensCouldEndDecl =
2799
- Tok. Next ->isOneOf (tok::equal, tok::semi, tok::l_brace, tok::greater);
2808
+ AfterRParen ->isOneOf (tok::equal, tok::semi, tok::l_brace, tok::greater);
2800
2809
if (ParensAreType && !ParensCouldEndDecl)
2801
2810
return true ;
2802
2811
@@ -2808,49 +2817,49 @@ class AnnotatingParser {
2808
2817
2809
2818
// Certain token types inside the parentheses mean that this can't be a
2810
2819
// cast.
2811
- for (const FormatToken *Token = Tok.MatchingParen ->Next ; Token != &Tok;
2812
- Token = Token->Next ) {
2820
+ for (const auto *Token = LParen->Next ; Token != &Tok; Token = Token->Next )
2813
2821
if (Token->is (TT_BinaryOperator))
2814
2822
return false ;
2815
- }
2816
2823
2817
2824
// If the following token is an identifier or 'this', this is a cast. All
2818
2825
// cases where this can be something else are handled above.
2819
- if (Tok. Next ->isOneOf (tok::identifier, tok::kw_this))
2826
+ if (AfterRParen ->isOneOf (tok::identifier, tok::kw_this))
2820
2827
return true ;
2821
2828
2822
2829
// Look for a cast `( x ) (`.
2823
- if (Tok. Next ->is (tok::l_paren) && Tok. Previous && Tok. Previous ->Previous ) {
2824
- if (Tok. Previous ->is (tok::identifier) &&
2825
- Tok. Previous ->Previous ->is (tok::l_paren)) {
2830
+ if (AfterRParen ->is (tok::l_paren) && BeforeRParen ->Previous ) {
2831
+ if (BeforeRParen ->is (tok::identifier) &&
2832
+ BeforeRParen ->Previous ->is (tok::l_paren)) {
2826
2833
return true ;
2827
2834
}
2828
2835
}
2829
2836
2830
- if (!Tok. Next ->Next )
2837
+ if (!AfterRParen ->Next )
2831
2838
return false ;
2832
2839
2833
2840
// If the next token after the parenthesis is a unary operator, assume
2834
2841
// that this is cast, unless there are unexpected tokens inside the
2835
2842
// parenthesis.
2836
- const bool NextIsAmpOrStar = Tok. Next ->isOneOf (tok::amp, tok::star);
2837
- if (!(Tok. Next ->isUnaryOperator () || NextIsAmpOrStar) ||
2838
- Tok. Next ->is (tok::plus) ||
2839
- !Tok. Next ->Next ->isOneOf (tok::identifier, tok::numeric_constant)) {
2843
+ const bool NextIsAmpOrStar = AfterRParen ->isOneOf (tok::amp, tok::star);
2844
+ if (!(AfterRParen ->isUnaryOperator () || NextIsAmpOrStar) ||
2845
+ AfterRParen ->is (tok::plus) ||
2846
+ !AfterRParen ->Next ->isOneOf (tok::identifier, tok::numeric_constant)) {
2840
2847
return false ;
2841
2848
}
2849
+
2842
2850
if (NextIsAmpOrStar &&
2843
- (Tok. Next ->Next ->is (tok::numeric_constant) || Line.InPPDirective )) {
2851
+ (AfterRParen ->Next ->is (tok::numeric_constant) || Line.InPPDirective )) {
2844
2852
return false ;
2845
2853
}
2846
- if (Line.InPPDirective && Tok.Next ->is (tok::minus))
2854
+
2855
+ if (Line.InPPDirective && AfterRParen->is (tok::minus))
2847
2856
return false ;
2857
+
2848
2858
// Search for unexpected tokens.
2849
- for (FormatToken *Prev = Tok.Previous ; Prev != Tok.MatchingParen ;
2850
- Prev = Prev->Previous ) {
2859
+ for (auto *Prev = BeforeRParen; Prev != LParen; Prev = Prev->Previous )
2851
2860
if (!Prev->isOneOf (tok::kw_const, tok::identifier, tok::coloncolon))
2852
2861
return false ;
2853
- }
2862
+
2854
2863
return true ;
2855
2864
}
2856
2865
0 commit comments