@@ -1042,68 +1042,67 @@ void WhitespaceManager::alignChainedConditionals() {
10421042}
10431043
10441044void WhitespaceManager::alignTrailingComments () {
1045- unsigned MinColumn = 0 ;
1046- unsigned MaxColumn = UINT_MAX ;
1047- unsigned StartOfSequence = 0 ;
1045+ const int Size = Changes. size () ;
1046+ int MinColumn = 0 ;
1047+ int StartOfSequence = 0 ;
10481048 bool BreakBeforeNext = false ;
1049- unsigned Newlines = 0 ;
1050- unsigned int NewLineThreshold = 1 ;
1049+ int NewLineThreshold = 1 ;
10511050 if (Style.AlignTrailingComments .Kind == FormatStyle::TCAS_Always)
10521051 NewLineThreshold = Style.AlignTrailingComments .OverEmptyLines + 1 ;
10531052
1054- for (unsigned i = 0 , e = Changes.size (); i != e; ++i) {
1055- if (Changes[i].StartOfBlockComment )
1053+ for (int I = 0 , MaxColumn = INT_MAX, Newlines = 0 ; I < Size; ++I) {
1054+ auto &C = Changes[I];
1055+ if (C.StartOfBlockComment )
10561056 continue ;
1057- Newlines += Changes[i] .NewlinesBefore ;
1058- if (!Changes[i] .IsTrailingComment )
1057+ Newlines += C .NewlinesBefore ;
1058+ if (!C .IsTrailingComment )
10591059 continue ;
10601060
10611061 if (Style.AlignTrailingComments .Kind == FormatStyle::TCAS_Leave) {
1062- auto OriginalSpaces =
1063- Changes[i].OriginalWhitespaceRange .getEnd ().getRawEncoding () -
1064- Changes[i].OriginalWhitespaceRange .getBegin ().getRawEncoding () -
1065- Changes[i].Tok ->NewlinesBefore ;
1066- unsigned RestoredLineLength = Changes[i].StartOfTokenColumn +
1067- Changes[i].TokenLength + OriginalSpaces;
1062+ const int OriginalSpaces =
1063+ C.OriginalWhitespaceRange .getEnd ().getRawEncoding () -
1064+ C.OriginalWhitespaceRange .getBegin ().getRawEncoding () -
1065+ C.Tok ->NewlinesBefore ;
1066+ assert (OriginalSpaces >= 0 );
1067+ const auto RestoredLineLength =
1068+ C.StartOfTokenColumn + C.TokenLength + OriginalSpaces;
10681069 // If leaving comments makes the line exceed the column limit, give up to
10691070 // leave the comments.
1070- if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit != 0 )
1071+ if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit > 0 )
10711072 break ;
1072- Changes[i] .Spaces = OriginalSpaces;
1073+ C .Spaces = OriginalSpaces;
10731074 continue ;
10741075 }
10751076
1076- unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn ;
1077- unsigned ChangeMaxColumn;
1078-
1079- if (Style.ColumnLimit == 0 )
1080- ChangeMaxColumn = UINT_MAX;
1081- else if (Style.ColumnLimit >= Changes[i].TokenLength )
1082- ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength ;
1083- else
1084- ChangeMaxColumn = ChangeMinColumn;
1077+ const int ChangeMinColumn = C.StartOfTokenColumn ;
1078+ int ChangeMaxColumn;
10851079
10861080 // If we don't create a replacement for this change, we have to consider
10871081 // it to be immovable.
1088- if (!Changes[i].CreateReplacement )
1082+ if (!C.CreateReplacement )
1083+ ChangeMaxColumn = ChangeMinColumn;
1084+ else if (Style.ColumnLimit == 0 )
1085+ ChangeMaxColumn = INT_MAX;
1086+ else if (Style.ColumnLimit >= C.TokenLength )
1087+ ChangeMaxColumn = Style.ColumnLimit - C.TokenLength ;
1088+ else
10891089 ChangeMaxColumn = ChangeMinColumn;
10901090
1091- if (i + 1 != e && Changes[i + 1 ].ContinuesPPDirective )
1091+ if (I + 1 < Size && Changes[I + 1 ].ContinuesPPDirective ) {
10921092 ChangeMaxColumn -= 2 ;
1093+ assert (ChangeMaxColumn >= 0 );
1094+ }
10931095
1094- // We don't want to align namespace end comments.
1095- bool DontAlignThisComment = i > 0 && Changes[i].NewlinesBefore == 0 &&
1096- Changes[i - 1 ].Tok ->is (TT_NamespaceRBrace);
10971096 bool WasAlignedWithStartOfNextLine = false ;
1098- if (Changes[i] .NewlinesBefore >= 1 ) { // A comment on its own line.
1099- unsigned CommentColumn = SourceMgr. getSpellingColumnNumber (
1100- Changes[i] .OriginalWhitespaceRange .getEnd ());
1101- for (unsigned j = i + 1 ; j != e ; ++j ) {
1102- if (Changes[j ].Tok ->is (tok::comment))
1097+ if (C .NewlinesBefore >= 1 ) { // A comment on its own line.
1098+ const auto CommentColumn =
1099+ SourceMgr. getSpellingColumnNumber (C .OriginalWhitespaceRange .getEnd ());
1100+ for (int J = I + 1 ; J < Size ; ++J ) {
1101+ if (Changes[J ].Tok ->is (tok::comment))
11031102 continue ;
11041103
1105- unsigned NextColumn = SourceMgr.getSpellingColumnNumber (
1106- Changes[j ].OriginalWhitespaceRange .getEnd ());
1104+ const auto NextColumn = SourceMgr.getSpellingColumnNumber (
1105+ Changes[J ].OriginalWhitespaceRange .getEnd ());
11071106 // The start of the next token was previously aligned with the
11081107 // start of this comment.
11091108 WasAlignedWithStartOfNextLine =
@@ -1112,34 +1111,39 @@ void WhitespaceManager::alignTrailingComments() {
11121111 break ;
11131112 }
11141113 }
1114+
1115+ // We don't want to align namespace end comments.
1116+ const bool DontAlignThisComment =
1117+ I > 0 && C.NewlinesBefore == 0 &&
1118+ Changes[I - 1 ].Tok ->is (TT_NamespaceRBrace);
11151119 if (Style.AlignTrailingComments .Kind == FormatStyle::TCAS_Never ||
11161120 DontAlignThisComment) {
1117- alignTrailingComments (StartOfSequence, i , MinColumn);
1121+ alignTrailingComments (StartOfSequence, I , MinColumn);
11181122 MinColumn = ChangeMinColumn;
11191123 MaxColumn = ChangeMinColumn;
1120- StartOfSequence = i ;
1124+ StartOfSequence = I ;
11211125 } else if (BreakBeforeNext || Newlines > NewLineThreshold ||
11221126 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
11231127 // Break the comment sequence if the previous line did not end
11241128 // in a trailing comment.
1125- (Changes[i] .NewlinesBefore == 1 && i > 0 &&
1126- !Changes[i - 1 ].IsTrailingComment ) ||
1129+ (C .NewlinesBefore == 1 && I > 0 &&
1130+ !Changes[I - 1 ].IsTrailingComment ) ||
11271131 WasAlignedWithStartOfNextLine) {
1128- alignTrailingComments (StartOfSequence, i , MinColumn);
1132+ alignTrailingComments (StartOfSequence, I , MinColumn);
11291133 MinColumn = ChangeMinColumn;
11301134 MaxColumn = ChangeMaxColumn;
1131- StartOfSequence = i ;
1135+ StartOfSequence = I ;
11321136 } else {
11331137 MinColumn = std::max (MinColumn, ChangeMinColumn);
11341138 MaxColumn = std::min (MaxColumn, ChangeMaxColumn);
11351139 }
1136- BreakBeforeNext = (i == 0 ) || (Changes[i] .NewlinesBefore > 1 ) ||
1140+ BreakBeforeNext = (I == 0 ) || (C .NewlinesBefore > 1 ) ||
11371141 // Never start a sequence with a comment at the beginning
11381142 // of the line.
1139- (Changes[i] .NewlinesBefore == 1 && StartOfSequence == i );
1143+ (C .NewlinesBefore == 1 && StartOfSequence == I );
11401144 Newlines = 0 ;
11411145 }
1142- alignTrailingComments (StartOfSequence, Changes. size () , MinColumn);
1146+ alignTrailingComments (StartOfSequence, Size , MinColumn);
11431147}
11441148
11451149void WhitespaceManager::alignTrailingComments (unsigned Start, unsigned End,
0 commit comments