52 unsigned StartOfTokenColumn,
53 bool IsAligned,
bool InPPDirective) {
58 Spaces, StartOfTokenColumn, Newlines,
"",
"",
59 IsAligned, InPPDirective && !Tok.
IsFirst,
67 Changes.push_back(
Change(Tok,
false,
70 false, InPPDirective && !Tok.
IsFirst,
76 return Replaces.
add(Replacement);
80 size_t LF =
Text.count(
'\n');
81 size_t CR =
Text.count(
'\r') * 2;
82 return LF == CR ? DefaultToCRLF : CR > LF;
86 const FormatToken &Tok,
unsigned Offset,
unsigned ReplaceChars,
87 StringRef PreviousPostfix, StringRef CurrentPrefix,
bool InPPDirective,
88 unsigned Newlines,
int Spaces) {
95 std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
96 true, InPPDirective && !Tok.
IsFirst,
105 calculateLineBreakInformation();
106 alignConsecutiveMacros();
107 alignConsecutiveShortCaseStatements();
108 alignConsecutiveDeclarations();
109 alignConsecutiveBitFields();
110 alignConsecutiveAssignments();
112 alignConsecutiveTableGenCondOperatorColons();
113 alignConsecutiveTableGenDefinitions();
115 alignChainedConditionals();
116 alignTrailingComments();
117 alignEscapedNewlines();
118 alignArrayInitializers();
124void WhitespaceManager::calculateLineBreakInformation() {
125 Changes[0].PreviousEndOfTokenColumn = 0;
126 Change *LastOutsideTokenChange = &Changes[0];
127 for (
unsigned i = 1, e = Changes.size(); i != e; ++i) {
129 Changes[i].OriginalWhitespaceRange.getBegin();
131 Changes[i - 1].OriginalWhitespaceRange.getEnd();
132 unsigned OriginalWhitespaceStartOffset =
134 unsigned PreviousOriginalWhitespaceEndOffset =
136 assert(PreviousOriginalWhitespaceEndOffset <=
137 OriginalWhitespaceStartOffset);
138 const char *
const PreviousOriginalWhitespaceEndData =
140 StringRef
Text(PreviousOriginalWhitespaceEndData,
142 PreviousOriginalWhitespaceEndData);
164 auto NewlinePos =
Text.find_first_of(
'\n');
165 if (NewlinePos == StringRef::npos) {
166 Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
167 PreviousOriginalWhitespaceEndOffset +
168 Changes[i].PreviousLinePostfix.size() +
169 Changes[i - 1].CurrentLinePrefix.size();
171 Changes[i - 1].TokenLength =
172 NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
177 if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0) {
178 LastOutsideTokenChange->TokenLength +=
179 Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
181 LastOutsideTokenChange = &Changes[i - 1];
184 Changes[i].PreviousEndOfTokenColumn =
185 Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
187 Changes[i - 1].IsTrailingComment =
188 (Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
189 (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
190 Changes[i - 1].Tok->is(tok::comment) &&
221 OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
225 Changes.back().TokenLength = 0;
226 Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
228 const WhitespaceManager::Change *LastBlockComment =
nullptr;
229 for (
auto &Change : Changes) {
233 if (Change.IsInsideToken && Change.NewlinesBefore == 0)
234 Change.IsTrailingComment =
false;
235 Change.StartOfBlockComment =
nullptr;
236 Change.IndentationOffset = 0;
237 if (Change.Tok->is(tok::comment)) {
238 if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken) {
239 LastBlockComment = &Change;
240 }
else if ((Change.StartOfBlockComment = LastBlockComment)) {
241 Change.IndentationOffset =
242 Change.StartOfTokenColumn -
243 Change.StartOfBlockComment->StartOfTokenColumn;
246 LastBlockComment =
nullptr;
254 SmallVector<bool, 16> ScopeStack;
255 int ConditionalsLevel = 0;
256 for (
auto &Change : Changes) {
257 for (
unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; ++i) {
258 bool isNestedConditional =
260 !(i == 0 && Change.Tok->Previous &&
261 Change.Tok->Previous->is(TT_ConditionalExpr) &&
262 Change.Tok->Previous->is(tok::colon));
263 if (isNestedConditional)
265 ScopeStack.push_back(isNestedConditional);
268 Change.ConditionalsLevel = ConditionalsLevel;
270 for (
unsigned i = Change.Tok->FakeRParens; i > 0 && ScopeStack.size(); --i)
271 if (ScopeStack.pop_back_val())
283 unsigned Column,
bool RightJustify, F &&Matches,
285 bool FoundMatchOnLine =
false;
310 for (
unsigned i = Start; i != End; ++i) {
311 auto &CurrentChange = Changes[i];
312 if (ScopeStack.size() != 0 &&
313 CurrentChange.indentAndNestingLevel() <
314 Changes[ScopeStack.back()].indentAndNestingLevel()) {
315 ScopeStack.pop_back();
320 unsigned PreviousNonComment = i - 1;
321 while (PreviousNonComment > Start &&
322 Changes[PreviousNonComment].Tok->is(tok::comment)) {
323 --PreviousNonComment;
325 if (i != Start && CurrentChange.indentAndNestingLevel() >
326 Changes[PreviousNonComment].indentAndNestingLevel()) {
327 ScopeStack.push_back(i);
330 bool InsideNestedScope = ScopeStack.size() != 0;
331 bool ContinuedStringLiteral = i > Start &&
332 CurrentChange.Tok->is(tok::string_literal) &&
333 Changes[i - 1].Tok->is(tok::string_literal);
334 bool SkipMatchCheck = InsideNestedScope || ContinuedStringLiteral;
336 if (CurrentChange.NewlinesBefore > 0 && !SkipMatchCheck) {
338 FoundMatchOnLine =
false;
344 if (!FoundMatchOnLine && !SkipMatchCheck && Matches(CurrentChange)) {
345 FoundMatchOnLine =
true;
346 Shift =
Column - (RightJustify ? CurrentChange.TokenLength : 0) -
347 CurrentChange.StartOfTokenColumn;
348 CurrentChange.Spaces += Shift;
351 if (CurrentChange.NewlinesBefore == 0) {
352 CurrentChange.Spaces =
353 std::max(CurrentChange.Spaces,
354 static_cast<int>(CurrentChange.Tok->SpacesRequiredBefore));
363 if (InsideNestedScope && CurrentChange.NewlinesBefore > 0) {
364 unsigned ScopeStart = ScopeStack.back();
365 auto ShouldShiftBeAdded = [&] {
367 if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
371 if (Changes[ScopeStart - 1].Tok->is(TT_LambdaLBrace))
375 if (ScopeStart > Start + 1 &&
376 Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) {
381 if (ScopeStart > Start + 1 &&
382 Changes[ScopeStart - 2].Tok->isOneOf(tok::identifier,
383 TT_TemplateCloser) &&
384 Changes[ScopeStart - 1].Tok->is(tok::l_paren) &&
385 Changes[ScopeStart].Tok->isNot(TT_LambdaLSquare)) {
386 if (CurrentChange.Tok->MatchingParen &&
387 CurrentChange.Tok->MatchingParen->is(TT_LambdaLBrace)) {
390 if (Changes[ScopeStart].NewlinesBefore > 0)
392 if (CurrentChange.Tok->is(tok::l_brace) &&
396 return Style.BinPackArguments;
400 if (CurrentChange.Tok->is(TT_ConditionalExpr))
404 if (CurrentChange.Tok->is(TT_DesignatedInitializerPeriod))
408 if (CurrentChange.Tok->Previous &&
409 CurrentChange.Tok->Previous->is(TT_ConditionalExpr)) {
414 if (ScopeStart > Start + 1 &&
415 Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
416 Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
417 CurrentChange.Tok->is(tok::l_brace) &&
423 if (ScopeStart > Start + 1 &&
424 Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
425 Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
426 CurrentChange.Tok->isNot(tok::r_brace)) {
427 for (
unsigned OuterScopeStart : llvm::reverse(ScopeStack)) {
429 if (OuterScopeStart > Start &&
430 Changes[OuterScopeStart - 1].Tok->is(TT_LambdaLBrace)) {
434 if (Changes[ScopeStart].NewlinesBefore > 0)
440 if (Changes[ScopeStart - 1].Tok->is(TT_TemplateOpener))
446 if (ShouldShiftBeAdded())
447 CurrentChange.Spaces += Shift;
450 if (ContinuedStringLiteral)
451 CurrentChange.Spaces += Shift;
454 assert(Shift > 0 || Changes[i].NewlinesBefore > 0 ||
455 CurrentChange.Spaces >=
456 static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) ||
457 CurrentChange.Tok->is(tok::eof));
459 CurrentChange.StartOfTokenColumn += Shift;
460 if (i + 1 != Changes.size())
461 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
466 CurrentChange.Spaces != 0) {
467 const bool ReferenceNotRightAligned =
472 Changes[
Previous].Tok->getType() == TT_PointerOrReference;
474 assert(Changes[
Previous].Tok->isPointerOrReference());
475 if (Changes[
Previous].Tok->isNot(tok::star)) {
476 if (ReferenceNotRightAligned)
481 Changes[
Previous + 1].Spaces -= Shift;
483 Changes[
Previous].StartOfTokenColumn += Shift;
525 bool RightJustify =
false) {
534 unsigned WidthLeft = 0;
537 unsigned WidthAnchor = 0;
540 unsigned WidthRight = 0;
543 unsigned StartOfSequence = 0;
544 unsigned EndOfSequence = 0;
548 auto IndentAndNestingLevel = StartAt < Changes.size()
549 ? Changes[StartAt].indentAndNestingLevel()
550 : std::tuple<unsigned, unsigned, unsigned>();
555 unsigned CommasBeforeLastMatch = 0;
556 unsigned CommasBeforeMatch = 0;
559 bool FoundMatchOnLine =
false;
562 bool LineIsComment =
true;
571 auto AlignCurrentSequence = [&] {
572 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
574 WidthLeft + WidthAnchor, RightJustify, Matches,
584 unsigned i = StartAt;
585 for (
unsigned e = Changes.size(); i != e; ++i) {
586 auto &CurrentChange = Changes[i];
587 if (CurrentChange.indentAndNestingLevel() < IndentAndNestingLevel)
590 if (CurrentChange.NewlinesBefore != 0) {
591 CommasBeforeMatch = 0;
595 bool EmptyLineBreak =
596 (CurrentChange.NewlinesBefore > 1) && !ACS.AcrossEmptyLines;
601 !FoundMatchOnLine && !(LineIsComment && ACS.AcrossComments);
603 if (EmptyLineBreak || NoMatchBreak)
604 AlignCurrentSequence();
608 if (i == 0 || CurrentChange.Tok->isNot(tok::string_literal) ||
609 Changes[i - 1].Tok->isNot(tok::string_literal)) {
610 FoundMatchOnLine =
false;
612 LineIsComment =
true;
615 if (CurrentChange.Tok->isNot(tok::comment))
616 LineIsComment =
false;
618 if (CurrentChange.Tok->is(tok::comma)) {
620 }
else if (CurrentChange.indentAndNestingLevel() > IndentAndNestingLevel) {
623 AlignTokens(Style, Matches, Changes, i, ACS, RightJustify);
628 if (!Matches(CurrentChange))
633 if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
634 AlignCurrentSequence();
636 CommasBeforeLastMatch = CommasBeforeMatch;
637 FoundMatchOnLine =
true;
639 if (StartOfSequence == 0)
642 unsigned ChangeWidthLeft = CurrentChange.StartOfTokenColumn;
643 unsigned ChangeWidthAnchor = 0;
644 unsigned ChangeWidthRight = 0;
646 if (ACS.PadOperators)
647 ChangeWidthAnchor = CurrentChange.TokenLength;
649 ChangeWidthLeft += CurrentChange.TokenLength;
651 ChangeWidthRight = CurrentChange.TokenLength;
652 for (
unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
653 ChangeWidthRight += Changes[j].Spaces;
659 if (!Changes[j].IsInsideToken)
660 ChangeWidthRight += Changes[j].TokenLength;
664 unsigned NewLeft = std::max(ChangeWidthLeft, WidthLeft);
665 unsigned NewAnchor = std::max(ChangeWidthAnchor, WidthAnchor);
666 unsigned NewRight = std::max(ChangeWidthRight, WidthRight);
668 if (Style.ColumnLimit != 0 &&
669 Style.ColumnLimit < NewLeft + NewAnchor + NewRight) {
670 AlignCurrentSequence();
672 WidthLeft = ChangeWidthLeft;
673 WidthAnchor = ChangeWidthAnchor;
674 WidthRight = ChangeWidthRight;
677 WidthAnchor = NewAnchor;
678 WidthRight = NewRight;
683 AlignCurrentSequence();
695 unsigned &StartOfSequence,
unsigned &EndOfSequence,
unsigned &MinColumn,
698 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
699 bool FoundMatchOnLine =
false;
702 for (
unsigned I = StartOfSequence; I != EndOfSequence; ++I) {
703 if (Changes[I].NewlinesBefore > 0) {
705 FoundMatchOnLine =
false;
711 if (!FoundMatchOnLine && Matches(Changes[I])) {
712 FoundMatchOnLine =
true;
713 Shift = MinColumn - Changes[I].StartOfTokenColumn;
714 Changes[I].Spaces += Shift;
718 Changes[I].StartOfTokenColumn += Shift;
719 if (I + 1 != Changes.size())
720 Changes[I + 1].PreviousEndOfTokenColumn += Shift;
729void WhitespaceManager::alignConsecutiveMacros() {
730 if (!Style.AlignConsecutiveMacros.Enabled)
733 auto AlignMacrosMatches = [](
const Change &
C) {
734 const FormatToken *Current =
C.Tok;
735 unsigned SpacesRequiredBefore = 1;
737 if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
740 Current = Current->Previous;
744 if (Current->is(tok::r_paren) && Current->MatchingParen) {
745 Current = Current->MatchingParen->Previous;
746 SpacesRequiredBefore = 0;
749 if (!Current || Current->isNot(tok::identifier))
752 if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
759 return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
762 unsigned MinColumn = 0;
765 unsigned StartOfSequence = 0;
766 unsigned EndOfSequence = 0;
769 bool FoundMatchOnLine =
false;
772 bool LineIsComment =
true;
775 for (
unsigned E = Changes.size(); I != E; ++I) {
776 if (Changes[I].NewlinesBefore != 0) {
780 bool EmptyLineBreak = (Changes[I].NewlinesBefore > 1) &&
781 !Style.AlignConsecutiveMacros.AcrossEmptyLines;
787 !(LineIsComment && Style.AlignConsecutiveMacros.AcrossComments);
789 if (EmptyLineBreak || NoMatchBreak) {
791 AlignMacrosMatches, Changes);
795 FoundMatchOnLine =
false;
796 LineIsComment =
true;
799 if (Changes[I].Tok->isNot(tok::comment))
800 LineIsComment =
false;
802 if (!AlignMacrosMatches(Changes[I]))
805 FoundMatchOnLine =
true;
807 if (StartOfSequence == 0)
810 unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
811 MinColumn = std::max(MinColumn, ChangeMinColumn);
816 AlignMacrosMatches, Changes);
819void WhitespaceManager::alignConsecutiveAssignments() {
820 if (!Style.AlignConsecutiveAssignments.Enabled)
825 [&](
const Change &
C) {
827 if (
C.NewlinesBefore > 0)
831 if (&
C != &Changes.back() && (&
C + 1)->NewlinesBefore > 0)
835 FormatToken *
Previous =
C.Tok->getPreviousNonComment();
839 return Style.AlignConsecutiveAssignments.AlignCompound
841 : (
C.Tok->is(tok::equal) ||
845 (Style.isVerilog() &&
C.Tok->is(tok::lessequal) &&
848 Changes, 0, Style.AlignConsecutiveAssignments,
852void WhitespaceManager::alignConsecutiveBitFields() {
853 alignConsecutiveColons(Style.AlignConsecutiveBitFields, TT_BitFieldColon);
856void WhitespaceManager::alignConsecutiveColons(
857 const FormatStyle::AlignConsecutiveStyle &AlignStyle,
TokenType Type) {
858 if (!AlignStyle.Enabled)
863 [&](Change
const &
C) {
865 if (
C.NewlinesBefore > 0)
869 if (&
C != &Changes.back() && (&
C + 1)->NewlinesBefore > 0)
872 return C.Tok->is(Type);
874 Changes, 0, AlignStyle);
877void WhitespaceManager::alignConsecutiveShortCaseStatements() {
878 if (!Style.AlignConsecutiveShortCaseStatements.Enabled ||
879 !Style.AllowShortCaseLabelsOnASingleLine) {
883 auto Matches = [&](
const Change &
C) {
884 if (Style.AlignConsecutiveShortCaseStatements.AlignCaseColons)
885 return C.Tok->is(TT_CaseLabelColon);
891 return !
C.IsInsideToken &&
C.Tok->Previous &&
892 C.Tok->Previous->is(TT_CaseLabelColon);
895 unsigned MinColumn = 0;
900 unsigned MinEmptyCaseColumn = 0;
903 unsigned StartOfSequence = 0;
904 unsigned EndOfSequence = 0;
907 bool FoundMatchOnLine =
false;
909 bool LineIsComment =
true;
910 bool LineIsEmptyCase =
false;
913 for (
unsigned E = Changes.size(); I != E; ++I) {
914 if (Changes[I].NewlinesBefore != 0) {
916 bool EmptyLineBreak =
917 (Changes[I].NewlinesBefore > 1) &&
918 !Style.AlignConsecutiveShortCaseStatements.AcrossEmptyLines;
925 Style.AlignConsecutiveShortCaseStatements.AcrossComments) &&
928 if (EmptyLineBreak || NoMatchBreak) {
931 MinEmptyCaseColumn = 0;
935 FoundMatchOnLine =
false;
936 LineIsComment =
true;
937 LineIsEmptyCase =
false;
940 if (Changes[I].Tok->isNot(tok::comment))
941 LineIsComment =
false;
943 if (Changes[I].Tok->is(TT_CaseLabelColon)) {
945 !Changes[I].Tok->Next || Changes[I].Tok->Next->isTrailingComment();
947 if (LineIsEmptyCase) {
948 if (Style.AlignConsecutiveShortCaseStatements.AlignCaseColons) {
950 std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn);
953 std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn + 2);
958 if (!Matches(Changes[I]))
964 FoundMatchOnLine =
true;
966 if (StartOfSequence == 0)
969 EndOfSequence = I + 1;
971 MinColumn = std::max(MinColumn, Changes[I].StartOfTokenColumn);
974 MinColumn = std::max(MinColumn, MinEmptyCaseColumn);
981void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
982 alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
983 TT_TableGenCondOperatorColon);
986void WhitespaceManager::alignConsecutiveTableGenDefinitions() {
987 alignConsecutiveColons(Style.AlignConsecutiveTableGenDefinitionColons,
988 TT_InheritanceColon);
991void WhitespaceManager::alignConsecutiveDeclarations() {
992 if (!Style.AlignConsecutiveDeclarations.Enabled)
997 [&](Change
const &
C) {
998 if (Style.AlignConsecutiveDeclarations.AlignFunctionPointers) {
999 for (const auto *Prev = C.Tok->Previous; Prev; Prev = Prev->Previous)
1000 if (Prev->is(tok::equal))
1002 if (C.Tok->is(TT_FunctionTypeLParen))
1005 if (
C.Tok->is(TT_FunctionDeclarationName))
1007 if (
C.Tok->isNot(TT_StartOfName))
1009 if (
C.Tok->Previous &&
1010 C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
1013 for (FormatToken *Next =
C.Tok->Next; Next; Next = Next->Next) {
1014 if (Next->is(tok::comment))
1016 if (Next->is(TT_PointerOrReference))
1018 if (!Next->Tok.getIdentifierInfo())
1020 if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
1021 tok::kw_operator)) {
1027 Changes, 0, Style.AlignConsecutiveDeclarations);
1030void WhitespaceManager::alignChainedConditionals() {
1031 if (Style.BreakBeforeTernaryOperators) {
1034 [](Change
const &
C) {
1036 return C.Tok->is(TT_ConditionalExpr) &&
1037 ((
C.Tok->is(tok::question) && !
C.NewlinesBefore) ||
1038 (
C.Tok->is(tok::colon) &&
C.Tok->Next &&
1039 (
C.Tok->Next->FakeLParens.size() == 0 ||
1044 static auto AlignWrappedOperand = [](Change
const &
C) {
1045 FormatToken *
Previous =
C.Tok->getPreviousNonComment();
1048 (
C.Tok->FakeLParens.size() == 0 ||
1054 for (Change &
C : Changes)
1055 if (AlignWrappedOperand(
C))
1056 C.StartOfTokenColumn -= 2;
1059 [
this](Change
const &
C) {
1063 return (
C.Tok->is(TT_ConditionalExpr) &&
C.Tok->is(tok::question) &&
1064 &
C != &Changes.back() && (&
C + 1)->NewlinesBefore == 0 &&
1065 !(&
C + 1)->IsTrailingComment) ||
1066 AlignWrappedOperand(
C);
1072void WhitespaceManager::alignTrailingComments() {
1076 const int Size = Changes.size();
1078 int StartOfSequence = 0;
1079 bool BreakBeforeNext =
false;
1080 int NewLineThreshold = 1;
1082 NewLineThreshold = Style.AlignTrailingComments.OverEmptyLines + 1;
1084 for (
int I = 0, MaxColumn =
INT_MAX, Newlines = 0; I <
Size; ++I) {
1085 auto &
C = Changes[I];
1086 if (
C.StartOfBlockComment)
1088 Newlines +=
C.NewlinesBefore;
1089 if (!
C.IsTrailingComment)
1093 const int OriginalSpaces =
1094 C.OriginalWhitespaceRange.getEnd().getRawEncoding() -
1095 C.OriginalWhitespaceRange.getBegin().getRawEncoding() -
1096 C.Tok->LastNewlineOffset;
1097 assert(OriginalSpaces >= 0);
1098 const auto RestoredLineLength =
1099 C.StartOfTokenColumn +
C.TokenLength + OriginalSpaces;
1102 if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit > 0)
1104 C.Spaces = OriginalSpaces;
1108 const int ChangeMinColumn =
C.StartOfTokenColumn;
1109 int ChangeMaxColumn;
1113 if (!
C.CreateReplacement)
1114 ChangeMaxColumn = ChangeMinColumn;
1115 else if (Style.ColumnLimit == 0)
1117 else if (Style.ColumnLimit >=
C.TokenLength)
1118 ChangeMaxColumn = Style.ColumnLimit -
C.TokenLength;
1120 ChangeMaxColumn = ChangeMinColumn;
1122 if (I + 1 < Size && Changes[I + 1].ContinuesPPDirective &&
1123 ChangeMaxColumn >= 2) {
1124 ChangeMaxColumn -= 2;
1127 bool WasAlignedWithStartOfNextLine =
false;
1128 if (
C.NewlinesBefore >= 1) {
1129 const auto CommentColumn =
1131 for (
int J = I + 1; J <
Size; ++J) {
1132 if (Changes[J].Tok->is(tok::comment))
1136 Changes[J].OriginalWhitespaceRange.getEnd());
1139 WasAlignedWithStartOfNextLine =
1140 CommentColumn == NextColumn ||
1141 CommentColumn == NextColumn + Style.IndentWidth;
1148 auto DontAlignThisComment = [](
const auto *Tok) {
1149 if (Tok->is(tok::semi)) {
1150 Tok = Tok->getPreviousNonComment();
1154 if (Tok->is(tok::r_paren)) {
1156 Tok = Tok->MatchingParen;
1159 Tok = Tok->getPreviousNonComment();
1162 if (Tok->is(TT_DoWhile)) {
1163 const auto *Prev = Tok->getPreviousNonComment();
1172 if (Tok->isNot(tok::r_brace))
1175 while (Tok->Previous && Tok->Previous->is(tok::r_brace))
1176 Tok = Tok->Previous;
1177 return Tok->NewlinesBefore > 0;
1180 if (I > 0 &&
C.NewlinesBefore == 0 &&
1181 DontAlignThisComment(Changes[I - 1].Tok)) {
1182 alignTrailingComments(StartOfSequence, I, MinColumn);
1187 StartOfSequence = I + 1;
1188 }
else if (BreakBeforeNext || Newlines > NewLineThreshold ||
1189 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
1192 (
C.NewlinesBefore == 1 && I > 0 &&
1193 !Changes[I - 1].IsTrailingComment) ||
1194 WasAlignedWithStartOfNextLine) {
1195 alignTrailingComments(StartOfSequence, I, MinColumn);
1196 MinColumn = ChangeMinColumn;
1197 MaxColumn = ChangeMaxColumn;
1198 StartOfSequence = I;
1200 MinColumn = std::max(MinColumn, ChangeMinColumn);
1201 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
1203 BreakBeforeNext = (I == 0) || (
C.NewlinesBefore > 1) ||
1206 (
C.NewlinesBefore == 1 && StartOfSequence == I);
1209 alignTrailingComments(StartOfSequence, Size, MinColumn);
1212void WhitespaceManager::alignTrailingComments(
unsigned Start,
unsigned End,
1214 for (
unsigned i = Start; i != End; ++i) {
1216 if (Changes[i].IsTrailingComment)
1218 if (Changes[i].StartOfBlockComment) {
1219 Shift = Changes[i].IndentationOffset +
1220 Changes[i].StartOfBlockComment->StartOfTokenColumn -
1221 Changes[i].StartOfTokenColumn;
1225 Changes[i].Spaces +=
Shift;
1226 if (i + 1 != Changes.size())
1227 Changes[i + 1].PreviousEndOfTokenColumn +=
Shift;
1228 Changes[i].StartOfTokenColumn +=
Shift;
1232void WhitespaceManager::alignEscapedNewlines() {
1237 unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
1238 unsigned StartOfMacro = 0;
1239 for (
unsigned i = 1, e = Changes.size(); i < e; ++i) {
1240 Change &
C = Changes[i];
1241 if (
C.NewlinesBefore > 0) {
1242 if (
C.ContinuesPPDirective) {
1243 MaxEndOfLine = std::max(
C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
1245 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
1246 MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
1251 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
1254void WhitespaceManager::alignEscapedNewlines(
unsigned Start,
unsigned End,
1256 for (
unsigned i = Start; i < End; ++i) {
1257 Change &
C = Changes[i];
1258 if (
C.NewlinesBefore > 0) {
1259 assert(
C.ContinuesPPDirective);
1260 if (
C.PreviousEndOfTokenColumn + 1 >
Column)
1261 C.EscapedNewlineColumn = 0;
1263 C.EscapedNewlineColumn =
Column;
1268void WhitespaceManager::alignArrayInitializers() {
1272 for (
unsigned ChangeIndex = 1U, ChangeEnd = Changes.size();
1273 ChangeIndex < ChangeEnd; ++ChangeIndex) {
1274 auto &
C = Changes[ChangeIndex];
1275 if (
C.Tok->IsArrayInitializer) {
1276 bool FoundComplete =
false;
1277 for (
unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
1279 if (Changes[InsideIndex].Tok ==
C.Tok->MatchingParen) {
1280 alignArrayInitializers(ChangeIndex, InsideIndex + 1);
1281 ChangeIndex = InsideIndex + 1;
1282 FoundComplete =
true;
1287 ChangeIndex = ChangeEnd;
1292void WhitespaceManager::alignArrayInitializers(
unsigned Start,
unsigned End) {
1295 alignArrayInitializersRightJustified(getCells(Start, End));
1297 alignArrayInitializersLeftJustified(getCells(Start, End));
1300void WhitespaceManager::alignArrayInitializersRightJustified(
1301 CellDescriptions &&CellDescs) {
1302 if (!CellDescs.isRectangular())
1305 const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
1306 auto &Cells = CellDescs.Cells;
1308 auto *CellIter = Cells.begin();
1309 for (
auto i = 0
U; i < CellDescs.CellCounts[0]; ++i, ++CellIter) {
1310 unsigned NetWidth = 0
U;
1311 if (isSplitCell(*CellIter))
1312 NetWidth = getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1313 auto CellWidth = getMaximumCellWidth(CellIter, NetWidth);
1315 if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
1319 const auto *Next = CellIter;
1321 const FormatToken *
Previous = Changes[Next->Index].Tok->Previous;
1323 Changes[Next->Index].Spaces = BracePadding;
1324 Changes[Next->Index].NewlinesBefore = 0;
1326 Next = Next->NextColumnElement;
1330 if (CellIter != Cells.begin()) {
1332 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1333 auto MaxNetWidth = getMaximumNetWidth(
1334 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1335 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1336 if (ThisNetWidth < MaxNetWidth)
1337 Changes[CellIter->Index].Spaces = (MaxNetWidth - ThisNetWidth);
1339 auto Offset = std::distance(Cells.begin(), CellIter);
1340 for (
const auto *Next = CellIter->NextColumnElement; Next;
1341 Next = Next->NextColumnElement) {
1342 if (RowCount >= CellDescs.CellCounts.size())
1344 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1345 auto *End = Start + Offset;
1346 ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1347 if (ThisNetWidth < MaxNetWidth)
1348 Changes[Next->Index].Spaces = (MaxNetWidth - ThisNetWidth);
1354 calculateCellWidth(CellIter->Index, CellIter->EndIndex,
true) +
1356 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1357 Changes[CellIter->Index].Spaces = (CellWidth - (ThisWidth + NetWidth));
1358 Changes[CellIter->Index].Spaces += (i > 0) ? 1 : BracePadding;
1360 alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
1361 for (
const auto *Next = CellIter->NextColumnElement; Next;
1362 Next = Next->NextColumnElement) {
1364 calculateCellWidth(Next->Index, Next->EndIndex,
true) + NetWidth;
1365 if (Changes[Next->Index].NewlinesBefore == 0) {
1366 Changes[Next->Index].Spaces = (CellWidth - ThisWidth);
1367 Changes[Next->Index].Spaces += (i > 0) ? 1 : BracePadding;
1369 alignToStartOfCell(Next->Index, Next->EndIndex);
1375void WhitespaceManager::alignArrayInitializersLeftJustified(
1376 CellDescriptions &&CellDescs) {
1378 if (!CellDescs.isRectangular())
1381 const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
1382 auto &Cells = CellDescs.Cells;
1384 auto *CellIter = Cells.begin();
1386 for (
const auto *Next = CellIter; Next; Next = Next->NextColumnElement) {
1387 auto &Change = Changes[Next->Index];
1389 Change.NewlinesBefore == 0 ? BracePadding : CellDescs.InitialSpaces;
1392 for (
auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
1393 auto MaxNetWidth = getMaximumNetWidth(
1394 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1395 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1397 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1398 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1399 Changes[CellIter->Index].Spaces =
1400 MaxNetWidth - ThisNetWidth +
1401 (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1
1405 auto Offset = std::distance(Cells.begin(), CellIter);
1406 for (
const auto *Next = CellIter->NextColumnElement; Next;
1407 Next = Next->NextColumnElement) {
1408 if (RowCount >= CellDescs.CellCounts.size())
1410 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1411 auto *End = Start + Offset;
1412 auto ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1413 if (Changes[Next->Index].NewlinesBefore == 0) {
1414 Changes[Next->Index].Spaces =
1415 MaxNetWidth - ThisNetWidth +
1416 (Changes[Next->Index].Tok->isNot(tok::r_brace) ? 1 : BracePadding);
1423bool WhitespaceManager::isSplitCell(
const CellDescription &Cell) {
1426 for (
const auto *Next = Cell.NextColumnElement; Next;
1427 Next = Next->NextColumnElement) {
1434WhitespaceManager::CellDescriptions WhitespaceManager::getCells(
unsigned Start,
1439 SmallVector<unsigned> CellCounts;
1440 unsigned InitialSpaces = 0;
1441 unsigned InitialTokenLength = 0;
1442 unsigned EndSpaces = 0;
1443 SmallVector<CellDescription> Cells;
1444 const FormatToken *MatchingParen =
nullptr;
1445 for (
unsigned i = Start; i < End; ++i) {
1446 auto &
C = Changes[i];
1447 if (
C.Tok->is(tok::l_brace))
1449 else if (
C.Tok->is(tok::r_brace))
1452 if (
C.Tok->is(tok::l_brace)) {
1454 MatchingParen =
C.Tok->MatchingParen;
1455 if (InitialSpaces == 0) {
1456 InitialSpaces =
C.Spaces +
C.TokenLength;
1457 InitialTokenLength =
C.TokenLength;
1459 for (; Changes[j].NewlinesBefore == 0 && j > Start; --j) {
1460 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1461 InitialTokenLength += Changes[j].TokenLength;
1463 if (
C.NewlinesBefore == 0) {
1464 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1465 InitialTokenLength += Changes[j].TokenLength;
1468 }
else if (
C.Tok->is(tok::comma)) {
1470 Cells.back().EndIndex = i;
1471 if (
const auto *Next =
C.Tok->getNextNonComment();
1472 Next && Next->isNot(tok::r_brace)) {
1476 }
else if (Depth == 1) {
1477 if (
C.Tok == MatchingParen) {
1479 Cells.back().EndIndex = i;
1480 Cells.push_back(CellDescription{i, ++Cell, i + 1,
false,
nullptr});
1481 CellCounts.push_back(
C.Tok->Previous->isNot(tok::comma) ? Cell + 1
1484 const auto *NextNonComment =
C.Tok->getNextNonComment();
1485 while (NextNonComment->is(tok::comma))
1486 NextNonComment = NextNonComment->getNextNonComment();
1488 while (j < End && Changes[j].Tok != NextNonComment)
1490 if (j < End && Changes[j].NewlinesBefore == 0 &&
1491 Changes[j].Tok->isNot(tok::r_brace)) {
1492 Changes[j].NewlinesBefore = 1;
1494 Changes[j].Spaces = InitialSpaces - InitialTokenLength;
1496 }
else if (
C.Tok->is(tok::comment) &&
C.Tok->NewlinesBefore == 0) {
1498 C.Spaces = Changes[i - 1].Tok->is(tok::comma) ? 1 : 2;
1499 }
else if (
C.Tok->is(tok::l_brace)) {
1503 for (; j > 0 && !Changes[j].Tok->ArrayInitializerLineStart; --j)
1505 EndSpaces = Changes[j].Spaces;
1507 }
else if (Depth == 0 &&
C.Tok->is(tok::r_brace)) {
1508 C.NewlinesBefore = 1;
1509 C.Spaces = EndSpaces;
1511 if (
C.Tok->StartsColumn) {
1514 bool HasSplit =
false;
1515 if (Changes[i].NewlinesBefore > 0) {
1531 if ((j - 1) > Start && Changes[j].Tok->is(tok::comma) &&
1532 Changes[j - 1].NewlinesBefore > 0) {
1534 auto LineLimit = Changes[j].Spaces + Changes[j].TokenLength;
1535 if (LineLimit < Style.ColumnLimit) {
1536 Changes[i].NewlinesBefore = 0;
1537 Changes[i].Spaces = 1;
1541 while (Changes[i].NewlinesBefore > 0 && Changes[i].Tok ==
C.Tok) {
1542 Changes[i].Spaces = InitialSpaces;
1546 if (Changes[i].Tok !=
C.Tok)
1548 Cells.push_back(CellDescription{i, Cell, i, HasSplit,
nullptr});
1552 return linkCells({Cells, CellCounts, InitialSpaces});
1555unsigned WhitespaceManager::calculateCellWidth(
unsigned Start,
unsigned End,
1556 bool WithSpaces)
const {
1557 unsigned CellWidth = 0;
1558 for (
auto i = Start; i < End; i++) {
1559 if (Changes[i].NewlinesBefore > 0)
1561 CellWidth += Changes[i].TokenLength;
1562 CellWidth += (WithSpaces ? Changes[i].Spaces : 0);
1567void WhitespaceManager::alignToStartOfCell(
unsigned Start,
unsigned End) {
1568 if ((End - Start) <= 1)
1572 for (
auto i = Start + 1; i < End; i++)
1573 if (Changes[i].NewlinesBefore > 0)
1574 Changes[i].Spaces = Changes[Start].Spaces;
1577WhitespaceManager::CellDescriptions
1578WhitespaceManager::linkCells(CellDescriptions &&CellDesc) {
1579 auto &Cells = CellDesc.Cells;
1580 for (
auto *CellIter = Cells.begin(); CellIter != Cells.end(); ++CellIter) {
1581 if (!CellIter->NextColumnElement && (CellIter + 1) != Cells.end()) {
1582 for (
auto *NextIter = CellIter + 1; NextIter != Cells.end(); ++NextIter) {
1583 if (NextIter->Cell == CellIter->Cell) {
1584 CellIter->NextColumnElement = &(*NextIter);
1590 return std::move(CellDesc);
1593void WhitespaceManager::generateChanges() {
1594 for (
unsigned i = 0, e = Changes.size(); i != e; ++i) {
1595 const Change &
C = Changes[i];
1597 auto Last = Changes[i - 1].OriginalWhitespaceRange;
1598 auto New = Changes[i].OriginalWhitespaceRange;
1640 if (
Last.getBegin() == New.getBegin() &&
1641 (
Last.getEnd() !=
Last.getBegin() ||
1642 New.getEnd() == New.getBegin())) {
1646 if (
C.CreateReplacement) {
1647 std::string ReplacementText =
C.PreviousLinePostfix;
1648 if (
C.ContinuesPPDirective) {
1649 appendEscapedNewlineText(ReplacementText,
C.NewlinesBefore,
1650 C.PreviousEndOfTokenColumn,
1651 C.EscapedNewlineColumn);
1653 appendNewlineText(ReplacementText,
C.NewlinesBefore);
1658 ReplacementText,
C.Tok->IndentLevel, std::max(0,
C.Spaces),
1659 std::max((
int)
C.StartOfTokenColumn,
C.Spaces) - std::max(0,
C.Spaces),
1661 ReplacementText.append(
C.CurrentLinePrefix);
1662 storeReplacement(
C.OriginalWhitespaceRange, ReplacementText);
1667void WhitespaceManager::storeReplacement(SourceRange Range, StringRef
Text) {
1672 WhitespaceLength) ==
Text) {
1675 auto Err = Replaces.
add(tooling::Replacement(
1680 llvm::errs() << llvm::toString(std::move(Err)) <<
"\n";
1685void WhitespaceManager::appendNewlineText(std::string &
Text,
1686 unsigned Newlines) {
1688 Text.reserve(
Text.size() + 2 * Newlines);
1689 for (
unsigned i = 0; i < Newlines; ++i)
1690 Text.append(
"\r\n");
1692 Text.append(Newlines,
'\n');
1696void WhitespaceManager::appendEscapedNewlineText(
1697 std::string &
Text,
unsigned Newlines,
unsigned PreviousEndOfTokenColumn,
1698 unsigned EscapedNewlineColumn) {
1701 std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
1702 for (
unsigned i = 0; i < Newlines; ++i) {
1703 Text.append(Spaces,
' ');
1704 Text.append(UseCRLF ?
"\\\r\n" :
"\\\n");
1705 Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
1710void WhitespaceManager::appendIndentText(std::string &
Text,
1711 unsigned IndentLevel,
unsigned Spaces,
1712 unsigned WhitespaceStartColumn,
1714 switch (Style.UseTab) {
1716 Text.append(Spaces,
' ');
1719 if (Style.TabWidth) {
1720 unsigned FirstTabWidth =
1721 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
1724 if (Spaces < FirstTabWidth || Spaces == 1) {
1725 Text.append(Spaces,
' ');
1729 Spaces -= FirstTabWidth;
1732 Text.append(Spaces / Style.TabWidth,
'\t');
1733 Text.append(Spaces % Style.TabWidth,
' ');
1734 }
else if (Spaces == 1) {
1735 Text.append(Spaces,
' ');
1740 if (WhitespaceStartColumn == 0) {
1741 unsigned Indentation = IndentLevel * Style.IndentWidth;
1742 Spaces = appendTabIndent(
Text, Spaces, Indentation);
1744 Text.append(Spaces,
' ');
1747 if (WhitespaceStartColumn == 0)
1748 Spaces = appendTabIndent(
Text, Spaces, Spaces);
1749 Text.append(Spaces,
' ');
1752 if (WhitespaceStartColumn == 0) {
1753 unsigned Indentation =
1754 IsAligned ? IndentLevel * Style.IndentWidth : Spaces;
1755 Spaces = appendTabIndent(
Text, Spaces, Indentation);
1757 Text.append(Spaces,
' ');
1762unsigned WhitespaceManager::appendTabIndent(std::string &
Text,
unsigned Spaces,
1763 unsigned Indentation) {
1766 if (Indentation > Spaces)
1767 Indentation = Spaces;
1768 if (Style.TabWidth) {
1769 unsigned Tabs = Indentation / Style.TabWidth;
1770 Text.append(Tabs,
'\t');
1771 Spaces -= Tabs * Style.TabWidth;
WhitespaceManager class manages whitespace around tokens and their replacements.
static CharSourceRange getCharRange(SourceRange R)
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
const char * getCharacterData(SourceLocation SL, bool *Invalid=nullptr) const
Return a pointer to the start of the specified location in the appropriate spelling MemoryBuffer.
unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
The JSON file list parser is used to communicate input to InstallAPI.
for(auto typeArg :T->getTypeArgsAsWritten())
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))