15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SmallVector.h"
55 unsigned StartOfTokenColumn,
56 bool IsAligned,
bool InPPDirective) {
61 Spaces, StartOfTokenColumn, Newlines,
"",
"",
62 IsAligned, InPPDirective && !Tok.
IsFirst,
70 Changes.push_back(
Change(Tok,
false,
73 false, InPPDirective && !Tok.
IsFirst,
79 return Replaces.
add(Replacement);
83 size_t LF =
Text.count(
'\n');
84 size_t CR =
Text.count(
'\r') * 2;
85 return LF == CR ? DefaultToCRLF : CR > LF;
89 const FormatToken &Tok,
unsigned Offset,
unsigned ReplaceChars,
90 StringRef PreviousPostfix, StringRef CurrentPrefix,
bool InPPDirective,
91 unsigned Newlines,
int Spaces) {
98 std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
99 true, InPPDirective && !Tok.
IsFirst,
108 calculateLineBreakInformation();
109 alignConsecutiveMacros();
110 alignConsecutiveShortCaseStatements();
111 alignConsecutiveDeclarations();
112 alignConsecutiveBitFields();
113 alignConsecutiveAssignments();
115 alignConsecutiveTableGenBreakingDAGArgColons();
116 alignConsecutiveTableGenCondOperatorColons();
117 alignConsecutiveTableGenDefinitions();
119 alignChainedConditionals();
120 alignTrailingComments();
121 alignEscapedNewlines();
122 alignArrayInitializers();
128void WhitespaceManager::calculateLineBreakInformation() {
129 Changes[0].PreviousEndOfTokenColumn = 0;
130 Change *LastOutsideTokenChange = &Changes[0];
131 for (
unsigned i = 1, e = Changes.size(); i != e; ++i) {
133 Changes[i].OriginalWhitespaceRange.getBegin();
135 Changes[i - 1].OriginalWhitespaceRange.getEnd();
136 unsigned OriginalWhitespaceStartOffset =
138 unsigned PreviousOriginalWhitespaceEndOffset =
140 assert(PreviousOriginalWhitespaceEndOffset <=
141 OriginalWhitespaceStartOffset);
142 const char *
const PreviousOriginalWhitespaceEndData =
144 StringRef
Text(PreviousOriginalWhitespaceEndData,
146 PreviousOriginalWhitespaceEndData);
168 auto NewlinePos =
Text.find_first_of(
'\n');
169 if (NewlinePos == StringRef::npos) {
170 Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
171 PreviousOriginalWhitespaceEndOffset +
172 Changes[i].PreviousLinePostfix.size() +
173 Changes[i - 1].CurrentLinePrefix.size();
175 Changes[i - 1].TokenLength =
176 NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
181 if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0) {
182 LastOutsideTokenChange->TokenLength +=
183 Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
185 LastOutsideTokenChange = &Changes[i - 1];
188 Changes[i].PreviousEndOfTokenColumn =
189 Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
191 Changes[i - 1].IsTrailingComment =
192 (Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
193 (Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
194 Changes[i - 1].Tok->is(tok::comment) &&
225 OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
229 Changes.back().TokenLength = 0;
230 Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
232 const WhitespaceManager::Change *LastBlockComment =
nullptr;
233 for (
auto &Change : Changes) {
237 if (Change.IsInsideToken && Change.NewlinesBefore == 0)
238 Change.IsTrailingComment =
false;
239 Change.StartOfBlockComment =
nullptr;
240 Change.IndentationOffset = 0;
241 if (Change.Tok->is(tok::comment)) {
242 if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken) {
243 LastBlockComment = &Change;
244 }
else if ((Change.StartOfBlockComment = LastBlockComment)) {
245 Change.IndentationOffset =
246 Change.StartOfTokenColumn -
247 Change.StartOfBlockComment->StartOfTokenColumn;
250 LastBlockComment =
nullptr;
258 SmallVector<bool, 16> ScopeStack;
259 int ConditionalsLevel = 0;
260 for (
auto &Change : Changes) {
261 for (
unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; ++i) {
262 bool isNestedConditional =
264 !(i == 0 && Change.Tok->Previous &&
265 Change.Tok->Previous->is(TT_ConditionalExpr) &&
266 Change.Tok->Previous->is(tok::colon));
267 if (isNestedConditional)
269 ScopeStack.push_back(isNestedConditional);
272 Change.ConditionalsLevel = ConditionalsLevel;
274 for (
unsigned i = Change.Tok->FakeRParens; i > 0 && ScopeStack.size(); --i)
275 if (ScopeStack.pop_back_val())
287 unsigned Column,
bool RightJustify, F &&Matches,
289 bool FoundMatchOnLine =
false;
314 for (
unsigned i = Start; i != End; ++i) {
315 auto &CurrentChange = Changes[i];
316 if (ScopeStack.size() != 0 &&
317 CurrentChange.indentAndNestingLevel() <
318 Changes[ScopeStack.back()].indentAndNestingLevel()) {
319 ScopeStack.pop_back();
324 unsigned PreviousNonComment = i - 1;
325 while (PreviousNonComment > Start &&
326 Changes[PreviousNonComment].Tok->is(tok::comment)) {
327 --PreviousNonComment;
329 if (i != Start && CurrentChange.indentAndNestingLevel() >
330 Changes[PreviousNonComment].indentAndNestingLevel()) {
331 ScopeStack.push_back(i);
334 bool InsideNestedScope = ScopeStack.size() != 0;
335 bool ContinuedStringLiteral = i > Start &&
336 CurrentChange.Tok->is(tok::string_literal) &&
337 Changes[i - 1].Tok->is(tok::string_literal);
338 bool SkipMatchCheck = InsideNestedScope || ContinuedStringLiteral;
340 if (CurrentChange.NewlinesBefore > 0 && !SkipMatchCheck) {
342 FoundMatchOnLine =
false;
348 if (!FoundMatchOnLine && !SkipMatchCheck && Matches(CurrentChange)) {
349 FoundMatchOnLine =
true;
350 Shift =
Column - (RightJustify ? CurrentChange.TokenLength : 0) -
351 CurrentChange.StartOfTokenColumn;
352 CurrentChange.Spaces += Shift;
355 if (CurrentChange.NewlinesBefore == 0) {
356 CurrentChange.Spaces =
357 std::max(CurrentChange.Spaces,
358 static_cast<int>(CurrentChange.Tok->SpacesRequiredBefore));
367 if (InsideNestedScope && CurrentChange.NewlinesBefore > 0) {
368 unsigned ScopeStart = ScopeStack.back();
369 auto ShouldShiftBeAdded = [&] {
371 if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
375 if (Changes[ScopeStart - 1].Tok->is(TT_LambdaLBrace))
379 if (ScopeStart > Start + 1 &&
380 Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) {
385 if (ScopeStart > Start + 1 &&
386 Changes[ScopeStart - 2].Tok->isOneOf(tok::identifier,
387 TT_TemplateCloser) &&
388 Changes[ScopeStart - 1].Tok->is(tok::l_paren) &&
389 Changes[ScopeStart].Tok->isNot(TT_LambdaLSquare)) {
390 if (CurrentChange.Tok->MatchingParen &&
391 CurrentChange.Tok->MatchingParen->is(TT_LambdaLBrace)) {
394 if (Changes[ScopeStart].NewlinesBefore > 0)
396 if (CurrentChange.Tok->is(tok::l_brace) &&
400 return Style.BinPackArguments;
404 if (CurrentChange.Tok->is(TT_ConditionalExpr))
408 if (CurrentChange.Tok->is(TT_DesignatedInitializerPeriod))
412 if (CurrentChange.Tok->Previous &&
413 CurrentChange.Tok->Previous->is(TT_ConditionalExpr)) {
418 if (ScopeStart > Start + 1 &&
419 Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
420 Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
421 CurrentChange.Tok->is(tok::l_brace) &&
427 if (ScopeStart > Start + 1 &&
428 Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
429 Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
430 CurrentChange.Tok->isNot(tok::r_brace)) {
431 for (
unsigned OuterScopeStart : llvm::reverse(ScopeStack)) {
433 if (OuterScopeStart > Start &&
434 Changes[OuterScopeStart - 1].Tok->is(TT_LambdaLBrace)) {
438 if (Changes[ScopeStart].NewlinesBefore > 0)
444 if (Changes[ScopeStart - 1].Tok->is(TT_TemplateOpener))
450 if (ShouldShiftBeAdded())
451 CurrentChange.Spaces += Shift;
454 if (ContinuedStringLiteral)
455 CurrentChange.Spaces += Shift;
458 assert(Shift > 0 || Changes[i].NewlinesBefore > 0 ||
459 CurrentChange.Spaces >=
460 static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) ||
461 CurrentChange.Tok->is(tok::eof));
463 CurrentChange.StartOfTokenColumn += Shift;
464 if (i + 1 != Changes.size())
465 Changes[i + 1].PreviousEndOfTokenColumn += Shift;
471 CurrentChange.Spaces != 0 && CurrentChange.Tok->isNot(tok::equal)) {
472 const bool ReferenceNotRightAligned =
478 assert(Changes[
Previous].Tok->isPointerOrReference());
479 if (Changes[
Previous].Tok->isNot(tok::star)) {
480 if (ReferenceNotRightAligned)
485 Changes[
Previous + 1].Spaces -= Shift;
487 Changes[
Previous].StartOfTokenColumn += Shift;
529 bool RightJustify =
false) {
538 unsigned WidthLeft = 0;
541 unsigned WidthAnchor = 0;
544 unsigned WidthRight = 0;
547 unsigned StartOfSequence = 0;
548 unsigned EndOfSequence = 0;
552 auto IndentAndNestingLevel = StartAt < Changes.size()
553 ? Changes[StartAt].indentAndNestingLevel()
554 : std::tuple<unsigned, unsigned, unsigned>();
559 unsigned CommasBeforeLastMatch = 0;
560 unsigned CommasBeforeMatch = 0;
563 bool FoundMatchOnLine =
false;
566 bool LineIsComment =
true;
575 auto AlignCurrentSequence = [&] {
576 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
578 WidthLeft + WidthAnchor, RightJustify, Matches,
588 unsigned i = StartAt;
589 for (
unsigned e = Changes.size(); i != e; ++i) {
590 auto &CurrentChange = Changes[i];
591 if (CurrentChange.indentAndNestingLevel() < IndentAndNestingLevel)
594 if (CurrentChange.NewlinesBefore != 0) {
595 CommasBeforeMatch = 0;
599 bool EmptyLineBreak =
600 (CurrentChange.NewlinesBefore > 1) && !ACS.AcrossEmptyLines;
605 !FoundMatchOnLine && !(LineIsComment && ACS.AcrossComments);
607 if (EmptyLineBreak || NoMatchBreak)
608 AlignCurrentSequence();
612 if (i == 0 || CurrentChange.Tok->isNot(tok::string_literal) ||
613 Changes[i - 1].Tok->isNot(tok::string_literal)) {
614 FoundMatchOnLine =
false;
616 LineIsComment =
true;
619 if (CurrentChange.Tok->isNot(tok::comment))
620 LineIsComment =
false;
622 if (CurrentChange.Tok->is(tok::comma)) {
624 }
else if (CurrentChange.indentAndNestingLevel() > IndentAndNestingLevel) {
627 AlignTokens(Style, Matches, Changes, i, ACS, RightJustify);
632 if (!Matches(CurrentChange))
637 if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
638 AlignCurrentSequence();
640 CommasBeforeLastMatch = CommasBeforeMatch;
641 FoundMatchOnLine =
true;
643 if (StartOfSequence == 0)
646 unsigned ChangeWidthLeft = CurrentChange.StartOfTokenColumn;
647 unsigned ChangeWidthAnchor = 0;
648 unsigned ChangeWidthRight = 0;
650 if (ACS.PadOperators)
651 ChangeWidthAnchor = CurrentChange.TokenLength;
653 ChangeWidthLeft += CurrentChange.TokenLength;
655 ChangeWidthRight = CurrentChange.TokenLength;
656 for (
unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
657 ChangeWidthRight += Changes[j].Spaces;
663 if (!Changes[j].IsInsideToken)
664 ChangeWidthRight += Changes[j].TokenLength;
668 unsigned NewLeft = std::max(ChangeWidthLeft, WidthLeft);
669 unsigned NewAnchor = std::max(ChangeWidthAnchor, WidthAnchor);
670 unsigned NewRight = std::max(ChangeWidthRight, WidthRight);
672 if (Style.ColumnLimit != 0 &&
673 Style.ColumnLimit < NewLeft + NewAnchor + NewRight) {
674 AlignCurrentSequence();
676 WidthLeft = ChangeWidthLeft;
677 WidthAnchor = ChangeWidthAnchor;
678 WidthRight = ChangeWidthRight;
681 WidthAnchor = NewAnchor;
682 WidthRight = NewRight;
687 AlignCurrentSequence();
699 unsigned &StartOfSequence,
unsigned &EndOfSequence,
unsigned &MinColumn,
702 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
703 bool FoundMatchOnLine =
false;
706 for (
unsigned I = StartOfSequence; I != EndOfSequence; ++I) {
707 if (Changes[I].NewlinesBefore > 0) {
709 FoundMatchOnLine =
false;
715 if (!FoundMatchOnLine && Matches(Changes[I])) {
716 FoundMatchOnLine =
true;
717 Shift = MinColumn - Changes[I].StartOfTokenColumn;
718 Changes[I].Spaces += Shift;
722 Changes[I].StartOfTokenColumn += Shift;
723 if (I + 1 != Changes.size())
724 Changes[I + 1].PreviousEndOfTokenColumn += Shift;
733void WhitespaceManager::alignConsecutiveMacros() {
734 if (!Style.AlignConsecutiveMacros.Enabled)
737 auto AlignMacrosMatches = [](
const Change &
C) {
738 const FormatToken *Current =
C.Tok;
739 unsigned SpacesRequiredBefore = 1;
741 if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
744 Current = Current->Previous;
748 if (Current->is(tok::r_paren) && Current->MatchingParen) {
749 Current = Current->MatchingParen->Previous;
750 SpacesRequiredBefore = 0;
753 if (!Current || Current->isNot(tok::identifier))
756 if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
763 return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
766 unsigned MinColumn = 0;
769 unsigned StartOfSequence = 0;
770 unsigned EndOfSequence = 0;
773 bool FoundMatchOnLine =
false;
776 bool LineIsComment =
true;
779 for (
unsigned E = Changes.size(); I != E; ++I) {
780 if (Changes[I].NewlinesBefore != 0) {
784 bool EmptyLineBreak = (Changes[I].NewlinesBefore > 1) &&
785 !Style.AlignConsecutiveMacros.AcrossEmptyLines;
791 !(LineIsComment && Style.AlignConsecutiveMacros.AcrossComments);
793 if (EmptyLineBreak || NoMatchBreak) {
795 AlignMacrosMatches, Changes);
799 FoundMatchOnLine =
false;
800 LineIsComment =
true;
803 if (Changes[I].Tok->isNot(tok::comment))
804 LineIsComment =
false;
806 if (!AlignMacrosMatches(Changes[I]))
809 FoundMatchOnLine =
true;
811 if (StartOfSequence == 0)
814 unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
815 MinColumn = std::max(MinColumn, ChangeMinColumn);
820 AlignMacrosMatches, Changes);
823void WhitespaceManager::alignConsecutiveAssignments() {
824 if (!Style.AlignConsecutiveAssignments.Enabled)
829 [&](
const Change &
C) {
831 if (
C.NewlinesBefore > 0)
835 if (&
C != &Changes.back() && (&
C + 1)->NewlinesBefore > 0)
839 FormatToken *
Previous =
C.Tok->getPreviousNonComment();
843 return Style.AlignConsecutiveAssignments.AlignCompound
845 : (
C.Tok->is(tok::equal) ||
849 (Style.isVerilog() &&
C.Tok->is(tok::lessequal) &&
852 Changes, 0, Style.AlignConsecutiveAssignments,
856void WhitespaceManager::alignConsecutiveBitFields() {
857 alignConsecutiveColons(Style.AlignConsecutiveBitFields, TT_BitFieldColon);
860void WhitespaceManager::alignConsecutiveColons(
861 const FormatStyle::AlignConsecutiveStyle &AlignStyle,
TokenType Type) {
862 if (!AlignStyle.Enabled)
867 [&](Change
const &
C) {
869 if (
C.NewlinesBefore > 0)
873 if (&
C != &Changes.back() && (&
C + 1)->NewlinesBefore > 0)
876 return C.Tok->is(Type);
878 Changes, 0, AlignStyle);
881void WhitespaceManager::alignConsecutiveShortCaseStatements() {
882 if (!Style.AlignConsecutiveShortCaseStatements.Enabled ||
883 !Style.AllowShortCaseLabelsOnASingleLine) {
887 auto Matches = [&](
const Change &
C) {
888 if (Style.AlignConsecutiveShortCaseStatements.AlignCaseColons)
889 return C.Tok->is(TT_CaseLabelColon);
895 return !
C.IsInsideToken &&
C.Tok->Previous &&
896 C.Tok->Previous->is(TT_CaseLabelColon);
899 unsigned MinColumn = 0;
904 unsigned MinEmptyCaseColumn = 0;
907 unsigned StartOfSequence = 0;
908 unsigned EndOfSequence = 0;
911 bool FoundMatchOnLine =
false;
913 bool LineIsComment =
true;
914 bool LineIsEmptyCase =
false;
917 for (
unsigned E = Changes.size(); I != E; ++I) {
918 if (Changes[I].NewlinesBefore != 0) {
920 bool EmptyLineBreak =
921 (Changes[I].NewlinesBefore > 1) &&
922 !Style.AlignConsecutiveShortCaseStatements.AcrossEmptyLines;
929 Style.AlignConsecutiveShortCaseStatements.AcrossComments) &&
932 if (EmptyLineBreak || NoMatchBreak) {
935 MinEmptyCaseColumn = 0;
939 FoundMatchOnLine =
false;
940 LineIsComment =
true;
941 LineIsEmptyCase =
false;
944 if (Changes[I].Tok->isNot(tok::comment))
945 LineIsComment =
false;
947 if (Changes[I].Tok->is(TT_CaseLabelColon)) {
949 !Changes[I].Tok->Next || Changes[I].Tok->Next->isTrailingComment();
951 if (LineIsEmptyCase) {
952 if (Style.AlignConsecutiveShortCaseStatements.AlignCaseColons) {
954 std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn);
957 std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn + 2);
962 if (!Matches(Changes[I]))
968 FoundMatchOnLine =
true;
970 if (StartOfSequence == 0)
973 EndOfSequence = I + 1;
975 MinColumn = std::max(MinColumn, Changes[I].StartOfTokenColumn);
978 MinColumn = std::max(MinColumn, MinEmptyCaseColumn);
985void WhitespaceManager::alignConsecutiveTableGenBreakingDAGArgColons() {
986 alignConsecutiveColons(Style.AlignConsecutiveTableGenBreakingDAGArgColons,
987 TT_TableGenDAGArgListColonToAlign);
990void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
991 alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
992 TT_TableGenCondOperatorColon);
995void WhitespaceManager::alignConsecutiveTableGenDefinitions() {
996 alignConsecutiveColons(Style.AlignConsecutiveTableGenDefinitionColons,
997 TT_InheritanceColon);
1000void WhitespaceManager::alignConsecutiveDeclarations() {
1001 if (!Style.AlignConsecutiveDeclarations.Enabled)
1006 [&](Change
const &
C) {
1007 if (Style.AlignConsecutiveDeclarations.AlignFunctionPointers) {
1008 for (const auto *Prev = C.Tok->Previous; Prev; Prev = Prev->Previous)
1009 if (Prev->is(tok::equal))
1011 if (C.Tok->is(TT_FunctionTypeLParen))
1014 if (
C.Tok->is(TT_FunctionDeclarationName))
1016 if (
C.Tok->isNot(TT_StartOfName))
1018 if (
C.Tok->Previous &&
1019 C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
1022 for (FormatToken *Next =
C.Tok->Next; Next; Next = Next->Next) {
1023 if (Next->is(tok::comment))
1025 if (Next->is(TT_PointerOrReference))
1027 if (!Next->Tok.getIdentifierInfo())
1029 if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
1030 tok::kw_operator)) {
1036 Changes, 0, Style.AlignConsecutiveDeclarations);
1039void WhitespaceManager::alignChainedConditionals() {
1040 if (Style.BreakBeforeTernaryOperators) {
1043 [](Change
const &
C) {
1045 return C.Tok->is(TT_ConditionalExpr) &&
1046 ((
C.Tok->is(tok::question) && !
C.NewlinesBefore) ||
1047 (
C.Tok->is(tok::colon) &&
C.Tok->Next &&
1048 (
C.Tok->Next->FakeLParens.size() == 0 ||
1053 static auto AlignWrappedOperand = [](Change
const &
C) {
1054 FormatToken *
Previous =
C.Tok->getPreviousNonComment();
1057 (
C.Tok->FakeLParens.size() == 0 ||
1063 for (Change &
C : Changes)
1064 if (AlignWrappedOperand(
C))
1065 C.StartOfTokenColumn -= 2;
1068 [
this](Change
const &
C) {
1072 return (
C.Tok->is(TT_ConditionalExpr) &&
C.Tok->is(tok::question) &&
1073 &
C != &Changes.back() && (&
C + 1)->NewlinesBefore == 0 &&
1074 !(&
C + 1)->IsTrailingComment) ||
1075 AlignWrappedOperand(
C);
1081void WhitespaceManager::alignTrailingComments() {
1085 const int Size = Changes.size();
1087 int StartOfSequence = 0;
1088 bool BreakBeforeNext =
false;
1089 int NewLineThreshold = 1;
1091 NewLineThreshold = Style.AlignTrailingComments.OverEmptyLines + 1;
1093 for (
int I = 0, MaxColumn =
INT_MAX, Newlines = 0; I <
Size; ++I) {
1094 auto &
C = Changes[I];
1095 if (
C.StartOfBlockComment)
1097 Newlines +=
C.NewlinesBefore;
1098 if (!
C.IsTrailingComment)
1102 const int OriginalSpaces =
1103 C.OriginalWhitespaceRange.getEnd().getRawEncoding() -
1104 C.OriginalWhitespaceRange.getBegin().getRawEncoding() -
1105 C.Tok->LastNewlineOffset;
1106 assert(OriginalSpaces >= 0);
1107 const auto RestoredLineLength =
1108 C.StartOfTokenColumn +
C.TokenLength + OriginalSpaces;
1111 if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit > 0)
1113 C.Spaces = OriginalSpaces;
1117 const int ChangeMinColumn =
C.StartOfTokenColumn;
1118 int ChangeMaxColumn;
1122 if (!
C.CreateReplacement)
1123 ChangeMaxColumn = ChangeMinColumn;
1124 else if (Style.ColumnLimit == 0)
1126 else if (Style.ColumnLimit >=
C.TokenLength)
1127 ChangeMaxColumn = Style.ColumnLimit -
C.TokenLength;
1129 ChangeMaxColumn = ChangeMinColumn;
1131 if (I + 1 < Size && Changes[I + 1].ContinuesPPDirective &&
1132 ChangeMaxColumn >= 2) {
1133 ChangeMaxColumn -= 2;
1136 bool WasAlignedWithStartOfNextLine =
false;
1137 if (
C.NewlinesBefore >= 1) {
1138 const auto CommentColumn =
1140 for (
int J = I + 1; J <
Size; ++J) {
1141 if (Changes[J].Tok->is(tok::comment))
1145 Changes[J].OriginalWhitespaceRange.getEnd());
1148 WasAlignedWithStartOfNextLine =
1149 CommentColumn == NextColumn ||
1150 CommentColumn == NextColumn + Style.IndentWidth;
1157 auto DontAlignThisComment = [](
const auto *Tok) {
1158 if (Tok->is(tok::semi)) {
1159 Tok = Tok->getPreviousNonComment();
1163 if (Tok->is(tok::r_paren)) {
1165 Tok = Tok->MatchingParen;
1168 Tok = Tok->getPreviousNonComment();
1171 if (Tok->is(TT_DoWhile)) {
1172 const auto *Prev = Tok->getPreviousNonComment();
1181 if (Tok->isNot(tok::r_brace))
1184 while (Tok->Previous && Tok->Previous->is(tok::r_brace))
1185 Tok = Tok->Previous;
1186 return Tok->NewlinesBefore > 0;
1189 if (I > 0 &&
C.NewlinesBefore == 0 &&
1190 DontAlignThisComment(Changes[I - 1].Tok)) {
1191 alignTrailingComments(StartOfSequence, I, MinColumn);
1196 StartOfSequence = I + 1;
1197 }
else if (BreakBeforeNext || Newlines > NewLineThreshold ||
1198 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
1201 (
C.NewlinesBefore == 1 && I > 0 &&
1202 !Changes[I - 1].IsTrailingComment) ||
1203 WasAlignedWithStartOfNextLine) {
1204 alignTrailingComments(StartOfSequence, I, MinColumn);
1205 MinColumn = ChangeMinColumn;
1206 MaxColumn = ChangeMaxColumn;
1207 StartOfSequence = I;
1209 MinColumn = std::max(MinColumn, ChangeMinColumn);
1210 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
1212 BreakBeforeNext = (I == 0) || (
C.NewlinesBefore > 1) ||
1215 (
C.NewlinesBefore == 1 && StartOfSequence == I);
1218 alignTrailingComments(StartOfSequence, Size, MinColumn);
1221void WhitespaceManager::alignTrailingComments(
unsigned Start,
unsigned End,
1223 for (
unsigned i = Start; i != End; ++i) {
1225 if (Changes[i].IsTrailingComment)
1227 if (Changes[i].StartOfBlockComment) {
1228 Shift = Changes[i].IndentationOffset +
1229 Changes[i].StartOfBlockComment->StartOfTokenColumn -
1230 Changes[i].StartOfTokenColumn;
1234 Changes[i].Spaces +=
Shift;
1235 if (i + 1 != Changes.size())
1236 Changes[i + 1].PreviousEndOfTokenColumn +=
Shift;
1237 Changes[i].StartOfTokenColumn +=
Shift;
1241void WhitespaceManager::alignEscapedNewlines() {
1246 unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
1247 unsigned StartOfMacro = 0;
1248 for (
unsigned i = 1, e = Changes.size(); i < e; ++i) {
1249 Change &
C = Changes[i];
1250 if (
C.NewlinesBefore > 0) {
1251 if (
C.ContinuesPPDirective) {
1252 MaxEndOfLine = std::max(
C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
1254 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
1255 MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
1260 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
1263void WhitespaceManager::alignEscapedNewlines(
unsigned Start,
unsigned End,
1265 for (
unsigned i = Start; i < End; ++i) {
1266 Change &
C = Changes[i];
1267 if (
C.NewlinesBefore > 0) {
1268 assert(
C.ContinuesPPDirective);
1269 if (
C.PreviousEndOfTokenColumn + 1 >
Column)
1270 C.EscapedNewlineColumn = 0;
1272 C.EscapedNewlineColumn =
Column;
1277void WhitespaceManager::alignArrayInitializers() {
1281 for (
unsigned ChangeIndex = 1U, ChangeEnd = Changes.size();
1282 ChangeIndex < ChangeEnd; ++ChangeIndex) {
1283 auto &
C = Changes[ChangeIndex];
1284 if (
C.Tok->IsArrayInitializer) {
1285 bool FoundComplete =
false;
1286 for (
unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
1288 if (Changes[InsideIndex].Tok ==
C.Tok->MatchingParen) {
1289 alignArrayInitializers(ChangeIndex, InsideIndex + 1);
1290 ChangeIndex = InsideIndex + 1;
1291 FoundComplete =
true;
1296 ChangeIndex = ChangeEnd;
1301void WhitespaceManager::alignArrayInitializers(
unsigned Start,
unsigned End) {
1304 alignArrayInitializersRightJustified(getCells(Start, End));
1306 alignArrayInitializersLeftJustified(getCells(Start, End));
1309void WhitespaceManager::alignArrayInitializersRightJustified(
1310 CellDescriptions &&CellDescs) {
1311 if (!CellDescs.isRectangular())
1314 const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
1315 auto &Cells = CellDescs.Cells;
1317 auto *CellIter = Cells.begin();
1318 for (
auto i = 0
U; i < CellDescs.CellCounts[0]; ++i, ++CellIter) {
1319 unsigned NetWidth = 0
U;
1320 if (isSplitCell(*CellIter))
1321 NetWidth = getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1322 auto CellWidth = getMaximumCellWidth(CellIter, NetWidth);
1324 if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
1328 const auto *Next = CellIter;
1330 const FormatToken *
Previous = Changes[Next->Index].Tok->Previous;
1332 Changes[Next->Index].Spaces = BracePadding;
1333 Changes[Next->Index].NewlinesBefore = 0;
1335 Next = Next->NextColumnElement;
1339 if (CellIter != Cells.begin()) {
1341 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1342 auto MaxNetWidth = getMaximumNetWidth(
1343 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1344 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1345 if (ThisNetWidth < MaxNetWidth)
1346 Changes[CellIter->Index].Spaces = (MaxNetWidth - ThisNetWidth);
1348 auto Offset = std::distance(Cells.begin(), CellIter);
1349 for (
const auto *Next = CellIter->NextColumnElement; Next;
1350 Next = Next->NextColumnElement) {
1351 if (RowCount >= CellDescs.CellCounts.size())
1353 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1354 auto *End = Start + Offset;
1355 ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1356 if (ThisNetWidth < MaxNetWidth)
1357 Changes[Next->Index].Spaces = (MaxNetWidth - ThisNetWidth);
1363 calculateCellWidth(CellIter->Index, CellIter->EndIndex,
true) +
1365 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1366 Changes[CellIter->Index].Spaces = (CellWidth - (ThisWidth + NetWidth));
1367 Changes[CellIter->Index].Spaces += (i > 0) ? 1 : BracePadding;
1369 alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
1370 for (
const auto *Next = CellIter->NextColumnElement; Next;
1371 Next = Next->NextColumnElement) {
1373 calculateCellWidth(Next->Index, Next->EndIndex,
true) + NetWidth;
1374 if (Changes[Next->Index].NewlinesBefore == 0) {
1375 Changes[Next->Index].Spaces = (CellWidth - ThisWidth);
1376 Changes[Next->Index].Spaces += (i > 0) ? 1 : BracePadding;
1378 alignToStartOfCell(Next->Index, Next->EndIndex);
1384void WhitespaceManager::alignArrayInitializersLeftJustified(
1385 CellDescriptions &&CellDescs) {
1387 if (!CellDescs.isRectangular())
1390 const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
1391 auto &Cells = CellDescs.Cells;
1393 auto *CellIter = Cells.begin();
1395 for (
const auto *Next = CellIter; Next; Next = Next->NextColumnElement) {
1396 auto &Change = Changes[Next->Index];
1398 Change.NewlinesBefore == 0 ? BracePadding : CellDescs.InitialSpaces;
1401 for (
auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
1402 auto MaxNetWidth = getMaximumNetWidth(
1403 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1404 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1406 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1407 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1408 Changes[CellIter->Index].Spaces =
1409 MaxNetWidth - ThisNetWidth +
1410 (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1
1414 auto Offset = std::distance(Cells.begin(), CellIter);
1415 for (
const auto *Next = CellIter->NextColumnElement; Next;
1416 Next = Next->NextColumnElement) {
1417 if (RowCount >= CellDescs.CellCounts.size())
1419 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1420 auto *End = Start + Offset;
1421 auto ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1422 if (Changes[Next->Index].NewlinesBefore == 0) {
1423 Changes[Next->Index].Spaces =
1424 MaxNetWidth - ThisNetWidth +
1425 (Changes[Next->Index].Tok->isNot(tok::r_brace) ? 1 : BracePadding);
1432bool WhitespaceManager::isSplitCell(
const CellDescription &Cell) {
1435 for (
const auto *Next = Cell.NextColumnElement; Next;
1436 Next = Next->NextColumnElement) {
1443WhitespaceManager::CellDescriptions WhitespaceManager::getCells(
unsigned Start,
1448 SmallVector<unsigned> CellCounts;
1449 unsigned InitialSpaces = 0;
1450 unsigned InitialTokenLength = 0;
1451 unsigned EndSpaces = 0;
1452 SmallVector<CellDescription> Cells;
1453 const FormatToken *MatchingParen =
nullptr;
1454 for (
unsigned i = Start; i < End; ++i) {
1455 auto &
C = Changes[i];
1456 if (
C.Tok->is(tok::l_brace))
1458 else if (
C.Tok->is(tok::r_brace))
1461 if (
C.Tok->is(tok::l_brace)) {
1463 MatchingParen =
C.Tok->MatchingParen;
1464 if (InitialSpaces == 0) {
1465 InitialSpaces =
C.Spaces +
C.TokenLength;
1466 InitialTokenLength =
C.TokenLength;
1468 for (; Changes[j].NewlinesBefore == 0 && j > Start; --j) {
1469 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1470 InitialTokenLength += Changes[j].TokenLength;
1472 if (
C.NewlinesBefore == 0) {
1473 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1474 InitialTokenLength += Changes[j].TokenLength;
1477 }
else if (
C.Tok->is(tok::comma)) {
1479 Cells.back().EndIndex = i;
1480 if (
const auto *Next =
C.Tok->getNextNonComment();
1481 Next && Next->isNot(tok::r_brace)) {
1485 }
else if (Depth == 1) {
1486 if (
C.Tok == MatchingParen) {
1488 Cells.back().EndIndex = i;
1489 Cells.push_back(CellDescription{i, ++Cell, i + 1,
false,
nullptr});
1490 CellCounts.push_back(
C.Tok->Previous->isNot(tok::comma) ? Cell + 1
1493 const auto *NextNonComment =
C.Tok->getNextNonComment();
1494 while (NextNonComment && NextNonComment->is(tok::comma))
1495 NextNonComment = NextNonComment->getNextNonComment();
1497 while (j < End && Changes[j].Tok != NextNonComment)
1499 if (j < End && Changes[j].NewlinesBefore == 0 &&
1500 Changes[j].Tok->isNot(tok::r_brace)) {
1501 Changes[j].NewlinesBefore = 1;
1503 Changes[j].Spaces = InitialSpaces - InitialTokenLength;
1505 }
else if (
C.Tok->is(tok::comment) &&
C.Tok->NewlinesBefore == 0) {
1507 C.Spaces = Changes[i - 1].Tok->is(tok::comma) ? 1 : 2;
1508 }
else if (
C.Tok->is(tok::l_brace)) {
1512 for (; j > 0 && !Changes[j].Tok->ArrayInitializerLineStart; --j)
1514 EndSpaces = Changes[j].Spaces;
1516 }
else if (Depth == 0 &&
C.Tok->is(tok::r_brace)) {
1517 C.NewlinesBefore = 1;
1518 C.Spaces = EndSpaces;
1520 if (
C.Tok->StartsColumn) {
1523 bool HasSplit =
false;
1524 if (Changes[i].NewlinesBefore > 0) {
1540 if ((j - 1) > Start && Changes[j].Tok->is(tok::comma) &&
1541 Changes[j - 1].NewlinesBefore > 0) {
1543 auto LineLimit = Changes[j].Spaces + Changes[j].TokenLength;
1544 if (LineLimit < Style.ColumnLimit) {
1545 Changes[i].NewlinesBefore = 0;
1546 Changes[i].Spaces = 1;
1550 while (Changes[i].NewlinesBefore > 0 && Changes[i].Tok ==
C.Tok) {
1551 Changes[i].Spaces = InitialSpaces;
1555 if (Changes[i].Tok !=
C.Tok)
1557 Cells.push_back(CellDescription{i, Cell, i, HasSplit,
nullptr});
1561 return linkCells({Cells, CellCounts, InitialSpaces});
1564unsigned WhitespaceManager::calculateCellWidth(
unsigned Start,
unsigned End,
1565 bool WithSpaces)
const {
1566 unsigned CellWidth = 0;
1567 for (
auto i = Start; i < End; i++) {
1568 if (Changes[i].NewlinesBefore > 0)
1570 CellWidth += Changes[i].TokenLength;
1571 CellWidth += (WithSpaces ? Changes[i].Spaces : 0);
1576void WhitespaceManager::alignToStartOfCell(
unsigned Start,
unsigned End) {
1577 if ((End - Start) <= 1)
1581 for (
auto i = Start + 1; i < End; i++)
1582 if (Changes[i].NewlinesBefore > 0)
1583 Changes[i].Spaces = Changes[Start].Spaces;
1586WhitespaceManager::CellDescriptions
1587WhitespaceManager::linkCells(CellDescriptions &&CellDesc) {
1588 auto &Cells = CellDesc.Cells;
1589 for (
auto *CellIter = Cells.begin(); CellIter != Cells.end(); ++CellIter) {
1590 if (!CellIter->NextColumnElement && (CellIter + 1) != Cells.end()) {
1591 for (
auto *NextIter = CellIter + 1; NextIter != Cells.end(); ++NextIter) {
1592 if (NextIter->Cell == CellIter->Cell) {
1593 CellIter->NextColumnElement = &(*NextIter);
1599 return std::move(CellDesc);
1602void WhitespaceManager::generateChanges() {
1603 for (
unsigned i = 0, e = Changes.size(); i != e; ++i) {
1604 const Change &
C = Changes[i];
1606 auto Last = Changes[i - 1].OriginalWhitespaceRange;
1607 auto New = Changes[i].OriginalWhitespaceRange;
1649 if (
Last.getBegin() == New.getBegin() &&
1650 (
Last.getEnd() !=
Last.getBegin() ||
1651 New.getEnd() == New.getBegin())) {
1655 if (
C.CreateReplacement) {
1656 std::string ReplacementText =
C.PreviousLinePostfix;
1657 if (
C.ContinuesPPDirective) {
1658 appendEscapedNewlineText(ReplacementText,
C.NewlinesBefore,
1659 C.PreviousEndOfTokenColumn,
1660 C.EscapedNewlineColumn);
1662 appendNewlineText(ReplacementText,
C.NewlinesBefore);
1667 ReplacementText,
C.Tok->IndentLevel, std::max(0,
C.Spaces),
1668 std::max((
int)
C.StartOfTokenColumn,
C.Spaces) - std::max(0,
C.Spaces),
1670 ReplacementText.append(
C.CurrentLinePrefix);
1671 storeReplacement(
C.OriginalWhitespaceRange, ReplacementText);
1676void WhitespaceManager::storeReplacement(SourceRange Range, StringRef
Text) {
1681 WhitespaceLength) ==
Text) {
1684 auto Err = Replaces.
add(tooling::Replacement(
1689 llvm::errs() << llvm::toString(std::move(Err)) <<
"\n";
1694void WhitespaceManager::appendNewlineText(std::string &
Text,
1695 unsigned Newlines) {
1697 Text.reserve(
Text.size() + 2 * Newlines);
1698 for (
unsigned i = 0; i < Newlines; ++i)
1699 Text.append(
"\r\n");
1701 Text.append(Newlines,
'\n');
1705void WhitespaceManager::appendEscapedNewlineText(
1706 std::string &
Text,
unsigned Newlines,
unsigned PreviousEndOfTokenColumn,
1707 unsigned EscapedNewlineColumn) {
1710 std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
1711 for (
unsigned i = 0; i < Newlines; ++i) {
1712 Text.append(Spaces,
' ');
1713 Text.append(UseCRLF ?
"\\\r\n" :
"\\\n");
1714 Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
1719void WhitespaceManager::appendIndentText(std::string &
Text,
1720 unsigned IndentLevel,
unsigned Spaces,
1721 unsigned WhitespaceStartColumn,
1723 switch (Style.UseTab) {
1725 Text.append(Spaces,
' ');
1728 if (Style.TabWidth) {
1729 unsigned FirstTabWidth =
1730 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
1733 if (Spaces < FirstTabWidth || Spaces == 1) {
1734 Text.append(Spaces,
' ');
1738 Spaces -= FirstTabWidth;
1741 Text.append(Spaces / Style.TabWidth,
'\t');
1742 Text.append(Spaces % Style.TabWidth,
' ');
1743 }
else if (Spaces == 1) {
1744 Text.append(Spaces,
' ');
1749 if (WhitespaceStartColumn == 0) {
1750 unsigned Indentation = IndentLevel * Style.IndentWidth;
1751 Spaces = appendTabIndent(
Text, Spaces, Indentation);
1753 Text.append(Spaces,
' ');
1756 if (WhitespaceStartColumn == 0)
1757 Spaces = appendTabIndent(
Text, Spaces, Spaces);
1758 Text.append(Spaces,
' ');
1761 if (WhitespaceStartColumn == 0) {
1762 unsigned Indentation =
1763 IsAligned ? IndentLevel * Style.IndentWidth : Spaces;
1764 Spaces = appendTabIndent(
Text, Spaces, Indentation);
1766 Text.append(Spaces,
' ');
1771unsigned WhitespaceManager::appendTabIndent(std::string &
Text,
unsigned Spaces,
1772 unsigned Indentation) {
1775 if (Indentation > Spaces)
1776 Indentation = Spaces;
1777 if (Style.TabWidth) {
1778 unsigned Tabs = Indentation / Style.TabWidth;
1779 Text.append(Tabs,
'\t');
1780 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.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
for(const auto &A :T->param_types())