@@ -2639,32 +2639,44 @@ class Formatter : public TokenAnalyzer {
2639
2639
2640
2640
int countVariableAlignments (const SmallVectorImpl<AnnotatedLine *> &Lines) {
2641
2641
int AlignmentDiff = 0 ;
2642
+
2642
2643
for (const AnnotatedLine *Line : Lines) {
2643
2644
AlignmentDiff += countVariableAlignments (Line->Children );
2644
- for (FormatToken *Tok = Line->First ; Tok && Tok->Next ; Tok = Tok->Next ) {
2645
+
2646
+ for (const auto *Tok = Line->getFirstNonComment (); Tok; Tok = Tok->Next ) {
2645
2647
if (Tok->isNot (TT_PointerOrReference))
2646
2648
continue ;
2647
- // Don't treat space in `void foo() &&` or `void() &&` as evidence.
2648
- if (const auto *Prev = Tok->getPreviousNonComment ()) {
2649
- if (Prev->is (tok::r_paren) && Prev->MatchingParen ) {
2650
- if (const auto *Func =
2651
- Prev->MatchingParen ->getPreviousNonComment ()) {
2652
- if (Func->isOneOf (TT_FunctionDeclarationName, TT_StartOfName,
2653
- TT_OverloadedOperator) ||
2654
- Func->isTypeName (LangOpts)) {
2655
- continue ;
2656
- }
2657
- }
2658
- }
2649
+
2650
+ const auto *Prev = Tok->Previous ;
2651
+ const bool PrecededByName = Prev && Prev->Tok .getIdentifierInfo ();
2652
+ const bool SpaceBefore = Tok->hasWhitespaceBefore ();
2653
+
2654
+ // e.g. `int **`, `int*&`, etc.
2655
+ while (Tok->Next && Tok->Next ->is (TT_PointerOrReference))
2656
+ Tok = Tok->Next ;
2657
+
2658
+ const auto *Next = Tok->Next ;
2659
+ const bool FollowedByName = Next && Next->Tok .getIdentifierInfo ();
2660
+ const bool SpaceAfter = Next && Next->hasWhitespaceBefore ();
2661
+
2662
+ if ((!PrecededByName && !FollowedByName) ||
2663
+ // e.g. `int * i` or `int*i`
2664
+ (PrecededByName && FollowedByName && SpaceBefore == SpaceAfter)) {
2665
+ continue ;
2659
2666
}
2660
- bool SpaceBefore = Tok->hasWhitespaceBefore ();
2661
- bool SpaceAfter = Tok->Next ->hasWhitespaceBefore ();
2662
- if (SpaceBefore && !SpaceAfter)
2667
+
2668
+ if ((PrecededByName && SpaceBefore) ||
2669
+ (FollowedByName && !SpaceAfter)) {
2670
+ // Right alignment.
2663
2671
++AlignmentDiff;
2664
- if (!SpaceBefore && SpaceAfter)
2672
+ } else if ((PrecededByName && !SpaceBefore) ||
2673
+ (FollowedByName && SpaceAfter)) {
2674
+ // Left alignment.
2665
2675
--AlignmentDiff;
2676
+ }
2666
2677
}
2667
2678
}
2679
+
2668
2680
return AlignmentDiff;
2669
2681
}
2670
2682
0 commit comments