15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SmallVector.h"
33 return C.Tok->IndentLevel;
41 return SourceMgr.isBeforeInTranslationUnit(
46 SourceMgr.isBeforeInTranslationUnit(
72 unsigned StartOfTokenColumn,
75 unsigned IndentedFromColumn) {
80 Spaces, StartOfTokenColumn, IndentedFromColumn,
82 InPPDirective && !
Tok.IsFirst,
91 Tok,
false,
Tok.WhitespaceRange, 0,
92 Tok.OriginalColumn, 0,
Tok.NewlinesBefore,
"",
"",
93 nullptr, InPPDirective && !
Tok.IsFirst,
99 return Replaces.add(Replacement);
103 size_t LF =
Text.count(
'\n');
104 size_t CR =
Text.count(
'\r') * 2;
105 return LF == CR ? DefaultToCRLF : CR > LF;
110 StringRef PreviousPostfix, StringRef CurrentPrefix,
bool InPPDirective,
119 PreviousPostfix, CurrentPrefix,
120 &
Tok, InPPDirective && !
Tok.IsFirst,
129 calculateLineBreakInformation();
130 alignConsecutiveMacros();
131 alignConsecutiveShortCaseStatements(
true);
132 alignConsecutiveShortCaseStatements(
false);
133 alignConsecutiveDeclarations();
134 alignConsecutiveBitFields();
135 alignConsecutiveAssignments();
136 if (Style.isTableGen()) {
137 alignConsecutiveTableGenBreakingDAGArgColons();
138 alignConsecutiveTableGenCondOperatorColons();
139 alignConsecutiveTableGenDefinitions();
141 alignChainedConditionals();
142 alignTrailingComments();
143 alignEscapedNewlines();
144 alignArrayInitializers();
150void WhitespaceManager::calculateLineBreakInformation() {
151 Changes[0].PreviousEndOfTokenColumn = 0;
152 Change *LastOutsideTokenChange = &Changes[0];
153 for (
unsigned I = 1, e = Changes.size(); I != e; ++I) {
154 auto &
C = Changes[I];
155 auto &P = Changes[I - 1];
156 auto &PrevTokLength = P.TokenLength;
158 C.OriginalWhitespaceRange.getBegin();
160 P.OriginalWhitespaceRange.getEnd();
161 unsigned OriginalWhitespaceStartOffset =
162 SourceMgr.getFileOffset(OriginalWhitespaceStart);
163 unsigned PreviousOriginalWhitespaceEndOffset =
164 SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
165 assert(PreviousOriginalWhitespaceEndOffset <=
166 OriginalWhitespaceStartOffset);
167 const char *
const PreviousOriginalWhitespaceEndData =
168 SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
169 StringRef
Text(PreviousOriginalWhitespaceEndData,
170 SourceMgr.getCharacterData(OriginalWhitespaceStart) -
171 PreviousOriginalWhitespaceEndData);
193 auto NewlinePos =
Text.find_first_of(
'\n');
194 if (NewlinePos == StringRef::npos) {
195 PrevTokLength = OriginalWhitespaceStartOffset -
196 PreviousOriginalWhitespaceEndOffset +
197 C.PreviousLinePostfix.size() + P.CurrentLinePrefix.size();
198 if (!P.IsInsideToken)
199 PrevTokLength = std::min(PrevTokLength, P.Tok->ColumnWidth);
201 PrevTokLength = NewlinePos + P.CurrentLinePrefix.size();
206 if (P.IsInsideToken && P.NewlinesBefore == 0)
207 LastOutsideTokenChange->TokenLength += PrevTokLength + P.Spaces;
209 LastOutsideTokenChange = &P;
211 C.PreviousEndOfTokenColumn = P.StartOfTokenColumn + PrevTokLength;
213 P.IsTrailingComment =
214 (
C.NewlinesBefore > 0 ||
C.Tok->is(tok::eof) ||
215 (
C.IsInsideToken &&
C.Tok->is(tok::comment))) &&
216 P.Tok->is(tok::comment) &&
247 OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
251 Changes.back().TokenLength = 0;
252 Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
254 const WhitespaceManager::Change *LastBlockComment =
nullptr;
255 for (
auto &
Change : Changes) {
265 LastBlockComment = &
Change;
272 LastBlockComment =
nullptr;
280 SmallVector<bool, 16> ScopeStack;
281 int ConditionalsLevel = 0;
282 for (
auto &
Change : Changes) {
284 bool isNestedConditional =
289 if (isNestedConditional)
291 ScopeStack.push_back(isNestedConditional);
297 if (ScopeStack.pop_back_val())
308 auto &FirstChange = Changes[Start];
309 const int ColumnChange = Spaces - FirstChange.Spaces;
311 if (ColumnChange == 0)
314 FirstChange.Spaces += ColumnChange;
315 FirstChange.StartOfTokenColumn += ColumnChange;
317 for (
auto I = Start + 1; I < Changes.size(); I++) {
318 auto &Change = Changes[I];
320 Change.PreviousEndOfTokenColumn += ColumnChange;
322 if (Change.NewlinesBefore > 0)
325 Change.StartOfTokenColumn += ColumnChange;
335 assert(Delta > 0 || (
abs(Delta) <= Changes[Start].Spaces));
345 unsigned Column,
bool RightJustify,
348 unsigned OriginalMatchColumn = 0;
376 for (
unsigned i = Start; i != End; ++i) {
377 auto &CurrentChange = Changes[i];
378 if (!Matches.empty() && Matches[0] < i)
379 Matches.consume_front();
380 assert(Matches.empty() || Matches[0] >= i);
381 while (!ScopeStack.empty() &&
382 CurrentChange.indentAndNestingLevel() < ScopeStack.back()) {
383 ScopeStack.pop_back();
387 if (ScopeStack.size() == 1u && CurrentChange.NewlinesBefore != 0u &&
388 CurrentChange.indentAndNestingLevel() > ScopeStack[0] &&
389 CurrentChange.IndentedFromColumn < OriginalMatchColumn) {
390 ScopeStack.push_back(CurrentChange.indentAndNestingLevel());
393 bool InsideNestedScope =
394 !ScopeStack.empty() &&
395 (CurrentChange.indentAndNestingLevel() > ScopeStack[0] ||
396 (CurrentChange.indentAndNestingLevel() == ScopeStack[0] &&
397 CurrentChange.IndentedFromColumn >= OriginalMatchColumn));
399 if (CurrentChange.NewlinesBefore > 0 && !InsideNestedScope)
405 if (!Matches.empty() && Matches[0] == i) {
406 OriginalMatchColumn = CurrentChange.StartOfTokenColumn;
407 Shift =
Column - (RightJustify ? CurrentChange.TokenLength : 0) -
408 CurrentChange.StartOfTokenColumn;
409 ScopeStack = {CurrentChange.indentAndNestingLevel()};
418 if ((!Matches.empty() && Matches[0] == i) ||
419 (ScopeStack.size() == 1u && CurrentChange.NewlinesBefore > 0 &&
420 InsideNestedScope)) {
421 CurrentChange.IndentedFromColumn += Shift;
427 CurrentChange.Spaces >=
428 static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) ||
429 CurrentChange.Tok->is(tok::eof));
433 if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
434 Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
435 CurrentChange.Spaces != 0 &&
436 CurrentChange.Tok->isNoneOf(tok::equal, tok::r_paren,
437 TT_TemplateCloser)) {
438 const bool ReferenceNotRightAligned =
439 Style.ReferenceAlignment != FormatStyle::RAS_Right &&
440 Style.ReferenceAlignment != FormatStyle::RAS_Pointer;
444 assert(Changes[
Previous].
Tok->isPointerOrReference());
446 if (ReferenceNotRightAligned)
448 }
else if (Style.PointerAlignment != FormatStyle::PAS_Right) {
460enum class AlignStrategy {
Normal,
Macro, CaseBody, CaseColon };
503template <
typename F, AlignStrategy Strategy = AlignStrategy::Normal>
507 const FormatStyle::AlignConsecutiveStyle &ACS = {},
508 bool RightJustify =
false) {
519 unsigned WidthLeft = 0;
522 unsigned WidthAnchor = 0;
525 unsigned WidthRight = 0;
528 unsigned StartOfSequence = 0;
529 unsigned EndOfSequence = 0;
536 const auto IndentAndNestingLevel =
537 StartAt < Changes.size() ? Changes[StartAt].indentAndNestingLevel()
538 : std::tuple<unsigned, unsigned, unsigned>();
543 unsigned CommasBeforeLastMatch = 0;
544 unsigned CommasBeforeMatch = 0;
547 std::optional<unsigned> MatchingColumn;
550 bool LineIsComment =
true;
559 auto AlignCurrentSequence = [&] {
560 if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
562 WidthLeft + WidthAnchor, RightJustify, MatchedIndices,
570 MatchedIndices.clear();
573 unsigned I = StartAt;
574 const auto E = Changes.size();
575 for (
const auto LoopEnd = Strategy == AlignStrategy::CaseBody ? E - 1 : E;
577 auto &CurrentChange = Changes[I];
578 if (CurrentChange.indentAndNestingLevel() < IndentAndNestingLevel)
581 if (CurrentChange.NewlinesBefore != 0) {
582 CommasBeforeMatch = 0;
586 bool EmptyLineBreak =
587 (CurrentChange.NewlinesBefore > 1) && !ACS.AcrossEmptyLines;
592 !MatchingColumn && !(LineIsComment && ACS.AcrossComments);
594 if (EmptyLineBreak || NoMatchBreak)
595 AlignCurrentSequence();
599 if (MatchingColumn && CurrentChange.IndentedFromColumn < *MatchingColumn)
600 MatchingColumn.reset();
601 LineIsComment =
true;
604 if (CurrentChange.Tok->isNot(tok::comment))
605 LineIsComment =
false;
607 if constexpr (Strategy == AlignStrategy::Normal) {
608 if (CurrentChange.Tok->is(tok::comma)) {
610 }
else if (CurrentChange.indentAndNestingLevel() >
611 IndentAndNestingLevel) {
614 Style, Matches, Changes, I, ACS, RightJustify);
620 if (!Matches(CurrentChange))
623 const auto IndexToAlign = Strategy == AlignStrategy::CaseBody ? I + 1 : I;
624 const auto &ChangeToAlign = Changes[IndexToAlign];
625 const auto [AlignTheToken,
626 ShiftAlignment] = [&]() -> std::pair<bool, bool> {
628 case AlignStrategy::CaseBody: {
629 if (ChangeToAlign.NewlinesBefore == 0)
630 return {
true,
false};
631 const auto *
Tok = ChangeToAlign.Tok;
632 if (
Tok->is(tok::comment) && ACS.AcrossComments)
633 Tok =
Tok->getNextNonComment();
634 return {
false,
Tok &&
Tok->isOneOf(tok::kw_case, tok::kw_default)};
636 case AlignStrategy::CaseColon: {
637 if (I + 1 == LoopEnd)
638 return {
true,
false};
639 const auto &NextChange = Changes[I + 1];
640 if (NextChange.NewlinesBefore == 0 ||
641 (CurrentChange.Tok->Next &&
642 CurrentChange.Tok->Next->isTrailingComment())) {
643 return {
true,
false};
645 const auto *
Tok = NextChange.Tok;
646 if (
Tok->is(tok::comment) && ACS.AcrossComments)
647 Tok =
Tok->getNextNonComment();
648 return {
Tok &&
Tok->isOneOf(tok::kw_case, tok::kw_default),
false};
651 return {
true,
false};
655 if (!AlignTheToken && !ShiftAlignment)
660 if ((ChangeToAlign.NewlinesBefore == 0U && MatchingColumn) ||
661 CommasBeforeMatch != CommasBeforeLastMatch) {
662 MatchedIndices.push_back(IndexToAlign);
663 AlignCurrentSequence();
666 CommasBeforeLastMatch = CommasBeforeMatch;
667 MatchingColumn = AlignTheToken ? ChangeToAlign.StartOfTokenColumn
668 : std::numeric_limits<unsigned>::max();
670 if (StartOfSequence == 0 && AlignTheToken)
671 StartOfSequence = IndexToAlign;
673 unsigned ChangeWidthLeft = ChangeToAlign.StartOfTokenColumn;
674 unsigned ChangeWidthAnchor = 0;
675 unsigned ChangeWidthRight = 0;
676 unsigned CurrentChangeWidthRight = 0;
677 if (!AlignTheToken) {
681 ChangeWidthLeft = CurrentChange.StartOfTokenColumn +
682 CurrentChange.TokenLength +
686 if (ACS.PadOperators)
687 ChangeWidthAnchor = ChangeToAlign.TokenLength;
689 ChangeWidthLeft += ChangeToAlign.TokenLength;
691 CurrentChangeWidthRight = ChangeToAlign.TokenLength;
692 const FormatToken *MatchingParenToEncounter =
nullptr;
693 for (
unsigned J = IndexToAlign + 1;
694 J != E && (Changes[J].NewlinesBefore == 0 ||
695 MatchingParenToEncounter || Changes[J].AlignedTo);
697 const auto &
Change = Changes[J];
700 if (
Tok->MatchingParen) {
701 if (
Tok->isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
702 TT_TemplateOpener) &&
703 !MatchingParenToEncounter) {
708 MatchingParenToEncounter =
Tok->MatchingParen;
709 }
else if (MatchingParenToEncounter ==
Tok->MatchingParen) {
710 MatchingParenToEncounter =
nullptr;
716 std::max(ChangeWidthRight, CurrentChangeWidthRight);
717 const auto ChangeWidthStart = ChangeWidthLeft + ChangeWidthAnchor;
728 CurrentChangeWidthRight =
Change.
Spaces - ChangeWidthStart;
742 ChangeWidthRight = std::max(ChangeWidthRight, CurrentChangeWidthRight);
746 unsigned NewLeft = std::max(ChangeWidthLeft, WidthLeft);
747 unsigned NewAnchor = std::max(ChangeWidthAnchor, WidthAnchor);
748 unsigned NewRight = std::max(ChangeWidthRight, WidthRight);
750 if (Style.ColumnLimit != 0 &&
751 Style.ColumnLimit < NewLeft + NewAnchor + NewRight) {
752 AlignCurrentSequence();
753 StartOfSequence = AlignTheToken ? IndexToAlign : 0;
754 WidthLeft = ChangeWidthLeft;
755 WidthAnchor = ChangeWidthAnchor;
756 WidthRight = ChangeWidthRight;
759 WidthAnchor = NewAnchor;
760 WidthRight = NewRight;
763 MatchedIndices.push_back(IndexToAlign);
768 for (EndOfSequence = I;
769 EndOfSequence < E && Changes[EndOfSequence].NewlinesBefore == 0;
772 AlignCurrentSequence();
778void WhitespaceManager::alignConsecutiveMacros() {
779 if (!Style.AlignConsecutiveMacros.Enabled)
782 auto AlignMacrosMatches = [](
const Change &
C) {
786 if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
789 Current = Current->Previous;
793 if (Current->is(tok::r_paren)) {
802 }
else if (Current->Next->SpacesRequiredBefore != 1) {
808 return Current->endsSequence(tok::identifier, tok::pp_define);
812 Style, AlignMacrosMatches, Changes, 0, Style.AlignConsecutiveMacros);
815void WhitespaceManager::alignConsecutiveAssignments() {
816 if (!Style.AlignConsecutiveAssignments.Enabled)
823 if (
C.NewlinesBefore > 0)
827 if (&
C != &Changes.back() && (&
C + 1)->NewlinesBefore > 0)
835 return Style.AlignConsecutiveAssignments.AlignCompound
837 : (
C.Tok->is(tok::equal) ||
841 (Style.isVerilog() &&
C.Tok->is(tok::lessequal) &&
844 Changes, 0, Style.AlignConsecutiveAssignments,
848void WhitespaceManager::alignConsecutiveBitFields() {
849 alignConsecutiveColons(Style.AlignConsecutiveBitFields, TT_BitFieldColon);
852void WhitespaceManager::alignConsecutiveColons(
853 const FormatStyle::AlignConsecutiveStyle &AlignStyle,
TokenType Type) {
854 if (!AlignStyle.Enabled)
861 if (
C.NewlinesBefore > 0)
865 if (&
C != &Changes.back() && (&
C + 1)->NewlinesBefore > 0)
868 return C.Tok->is(
Type);
870 Changes, 0, AlignStyle);
873void WhitespaceManager::alignConsecutiveShortCaseStatements(
bool IsExpr) {
874 if (!Style.AlignConsecutiveShortCaseStatements.Enabled ||
875 !(IsExpr ? Style.AllowShortCaseExpressionOnASingleLine
876 : Style.AllowShortCaseLabelsOnASingleLine)) {
880 const auto Type = IsExpr ? TT_CaseLabelArrow : TT_CaseLabelColon;
881 const auto &Option = Style.AlignConsecutiveShortCaseStatements;
882 const bool AlignArrowOrColon =
883 IsExpr ? Option.AlignCaseArrows : Option.AlignCaseColons;
885 FormatStyle::AlignConsecutiveStyle AlignStyle{};
886 AlignStyle.AcrossComments = Option.AcrossComments;
887 AlignStyle.AcrossEmptyLines = Option.AcrossEmptyLines;
890 if (AlignArrowOrColon) {
892 Style, Matches, Changes, 0, AlignStyle);
895 Style, Matches, Changes, 0, AlignStyle);
899void WhitespaceManager::alignConsecutiveTableGenBreakingDAGArgColons() {
900 alignConsecutiveColons(Style.AlignConsecutiveTableGenBreakingDAGArgColons,
901 TT_TableGenDAGArgListColonToAlign);
904void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
905 alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
906 TT_TableGenCondOperatorColon);
909void WhitespaceManager::alignConsecutiveTableGenDefinitions() {
910 alignConsecutiveColons(Style.AlignConsecutiveTableGenDefinitionColons,
911 TT_InheritanceColon);
914void WhitespaceManager::alignConsecutiveDeclarations() {
915 if (!Style.AlignConsecutiveDeclarations.Enabled)
921 if (
C.Tok->is(TT_FunctionTypeLParen))
922 return Style.AlignConsecutiveDeclarations.AlignFunctionPointers;
923 if (
C.Tok->is(TT_FunctionDeclarationName))
924 return Style.AlignConsecutiveDeclarations.AlignFunctionDeclarations;
925 if (
C.Tok->isNot(TT_StartOfName))
927 if (
C.Tok->Previous &&
928 C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
932 if (
Next->is(tok::comment))
934 if (
Next->is(TT_PointerOrReference))
936 if (!
Next->Tok.getIdentifierInfo())
938 if (
Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
945 Changes, 0, Style.AlignConsecutiveDeclarations);
948void WhitespaceManager::alignChainedConditionals() {
949 if (Style.BreakBeforeTernaryOperators) {
954 return C.Tok->is(TT_ConditionalExpr) &&
955 ((
C.Tok->is(tok::question) && !
C.NewlinesBefore) ||
956 (
C.Tok->is(tok::colon) &&
C.Tok->Next &&
957 (
C.Tok->Next->FakeLParens.empty() ||
962 static auto AlignWrappedOperand = [](
Change const &
C) {
966 (
C.Tok->FakeLParens.empty() ||
973 if (AlignWrappedOperand(
C))
974 C.StartOfTokenColumn -= 2;
981 return (
C.Tok->is(TT_ConditionalExpr) &&
C.Tok->is(tok::question) &&
982 &
C != &Changes.back() && (&
C + 1)->NewlinesBefore == 0 &&
983 !(&
C + 1)->IsTrailingComment) ||
984 AlignWrappedOperand(
C);
990void WhitespaceManager::alignTrailingComments() {
991 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Never)
994 const int Size = Changes.size();
999 int StartOfSequence = 0;
1000 bool BreakBeforeNext =
false;
1001 bool IsInPP = Changes.front().Tok->Tok.is(tok::hash);
1002 int NewLineThreshold = 1;
1003 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Always)
1004 NewLineThreshold = Style.AlignTrailingComments.OverEmptyLines + 1;
1007 auto &
C = Changes[I];
1008 if (
C.StartOfBlockComment)
1010 if (
C.NewlinesBefore != 0) {
1012 const bool WasInPP = std::exchange(
1013 IsInPP,
C.Tok->Tok.is(tok::hash) || (IsInPP &&
C.IsTrailingComment) ||
1014 C.ContinuesPPDirective);
1015 if (IsInPP != WasInPP && !Style.AlignTrailingComments.AlignPPAndNotPP) {
1016 alignTrailingComments(StartOfSequence, I, MinColumn);
1019 StartOfSequence = I;
1023 if (!
C.IsTrailingComment)
1026 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
1027 const int OriginalSpaces =
1028 C.OriginalWhitespaceRange.getEnd().getRawEncoding() -
1029 C.OriginalWhitespaceRange.getBegin().getRawEncoding() -
1030 C.Tok->LastNewlineOffset;
1031 assert(OriginalSpaces >= 0);
1032 const auto RestoredLineLength =
1033 C.StartOfTokenColumn +
C.TokenLength + OriginalSpaces;
1036 if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit > 0)
1040 C.NewlinesBefore > 0 ?
C.Tok->OriginalColumn : OriginalSpaces;
1041 setChangeSpaces(I, Spaces);
1045 const int ChangeMinColumn =
C.StartOfTokenColumn;
1046 int ChangeMaxColumn;
1050 if (!
C.CreateReplacement)
1051 ChangeMaxColumn = ChangeMinColumn;
1052 else if (Style.ColumnLimit == 0)
1054 else if (Style.ColumnLimit >=
C.TokenLength)
1055 ChangeMaxColumn = Style.ColumnLimit -
C.TokenLength;
1057 ChangeMaxColumn = ChangeMinColumn;
1059 if (I + 1 < Size && Changes[I + 1].ContinuesPPDirective &&
1060 ChangeMaxColumn >= 2) {
1061 ChangeMaxColumn -= 2;
1064 bool WasAlignedWithStartOfNextLine =
false;
1065 if (
C.NewlinesBefore >= 1) {
1066 const auto CommentColumn =
1067 SourceMgr.getSpellingColumnNumber(
C.OriginalWhitespaceRange.getEnd());
1068 for (
int J = I + 1; J <
Size; ++J) {
1069 if (Changes[J].
Tok->is(tok::comment))
1073 C.AlignedTo =
C.Tok->getPrevious(tok::comment);
1074 const auto NextColumn = SourceMgr.getSpellingColumnNumber(
1075 Changes[J].OriginalWhitespaceRange.getEnd());
1078 WasAlignedWithStartOfNextLine =
1079 CommentColumn == NextColumn ||
1080 CommentColumn == NextColumn + Style.IndentWidth;
1087 auto DontAlignThisComment = [](
const auto *
Tok) {
1088 if (
Tok->is(tok::semi)) {
1089 Tok =
Tok->getPreviousNonComment();
1093 if (
Tok->is(tok::r_paren)) {
1095 Tok =
Tok->MatchingParen;
1098 Tok =
Tok->getPreviousNonComment();
1101 if (
Tok->is(TT_DoWhile)) {
1102 const auto *Prev =
Tok->getPreviousNonComment();
1111 if (
Tok->isNot(tok::r_brace))
1114 while (
Tok->Previous &&
Tok->Previous->is(tok::r_brace))
1116 return Tok->NewlinesBefore > 0;
1119 if (I > 0 &&
C.NewlinesBefore == 0 &&
1120 DontAlignThisComment(Changes[I - 1].
Tok)) {
1121 alignTrailingComments(StartOfSequence, I, MinColumn);
1126 StartOfSequence = I + 1;
1127 }
else if (BreakBeforeNext ||
Newlines > NewLineThreshold ||
1128 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
1131 (
C.NewlinesBefore == 1 && I > 0 &&
1132 !Changes[I - 1].IsTrailingComment) ||
1133 WasAlignedWithStartOfNextLine) {
1134 alignTrailingComments(StartOfSequence, I, MinColumn);
1135 MinColumn = ChangeMinColumn;
1136 MaxColumn = ChangeMaxColumn;
1137 StartOfSequence = I;
1139 MinColumn = std::max(MinColumn, ChangeMinColumn);
1140 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
1142 BreakBeforeNext = (I == 0) || (
C.NewlinesBefore > 1) ||
1145 (
C.NewlinesBefore == 1 && StartOfSequence == I);
1148 alignTrailingComments(StartOfSequence, Size, MinColumn);
1151void WhitespaceManager::alignTrailingComments(
unsigned Start,
unsigned End,
1153 for (
unsigned i = Start; i != End; ++i) {
1155 if (Changes[i].IsTrailingComment)
1157 if (Changes[i].StartOfBlockComment) {
1158 Shift = Changes[i].IndentationOffset +
1159 Changes[i].StartOfBlockComment->StartOfTokenColumn -
1160 Changes[i].StartOfTokenColumn;
1165 setChangeSpaces(i, Changes[i].Spaces + Shift);
1169void WhitespaceManager::alignEscapedNewlines() {
1170 const auto Align = Style.AlignEscapedNewlines;
1171 if (Align == FormatStyle::ENAS_DontAlign)
1174 const bool WithLastLine = Align == FormatStyle::ENAS_LeftWithLastLine;
1175 const bool AlignLeft = Align == FormatStyle::ENAS_Left || WithLastLine;
1176 const auto MaxColumn = Style.ColumnLimit;
1177 unsigned MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
1178 unsigned StartOfMacro = 0;
1179 for (
unsigned i = 1, e = Changes.size(); i < e; ++i) {
1181 if (
C.NewlinesBefore == 0 && (!WithLastLine ||
C.Tok->isNot(tok::eof)))
1184 const auto BackslashColumn =
C.PreviousEndOfTokenColumn + 2;
1185 if (InPPDirective ||
1186 (WithLastLine && (MaxColumn == 0 || BackslashColumn <= MaxColumn))) {
1187 MaxEndOfLine = std::max(BackslashColumn, MaxEndOfLine);
1189 if (!InPPDirective) {
1190 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
1191 MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
1195 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
1198void WhitespaceManager::alignEscapedNewlines(
unsigned Start,
unsigned End,
1200 for (
unsigned i = Start; i < End; ++i) {
1202 if (
C.NewlinesBefore > 0) {
1203 assert(
C.ContinuesPPDirective);
1204 if (
C.PreviousEndOfTokenColumn + 1 >
Column)
1205 C.EscapedNewlineColumn = 0;
1207 C.EscapedNewlineColumn =
Column;
1212void WhitespaceManager::alignArrayInitializers() {
1213 if (Style.AlignArrayOfStructures == FormatStyle::AIAS_None)
1216 for (
unsigned ChangeIndex = 1U, ChangeEnd = Changes.size();
1217 ChangeIndex < ChangeEnd; ++ChangeIndex) {
1218 auto &
C = Changes[ChangeIndex];
1219 if (
C.Tok->IsArrayInitializer) {
1220 bool FoundComplete =
false;
1221 for (
unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
1223 const auto *
Tok = Changes[InsideIndex].Tok;
1224 if (
Tok->is(tok::pp_define))
1226 if (
Tok ==
C.Tok->MatchingParen) {
1227 alignArrayInitializers(ChangeIndex, InsideIndex + 1);
1228 ChangeIndex = InsideIndex + 1;
1229 FoundComplete =
true;
1234 ChangeIndex = ChangeEnd;
1239void WhitespaceManager::alignArrayInitializers(
unsigned Start,
unsigned End) {
1241 if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Right)
1242 alignArrayInitializersRightJustified(getCells(Start, End));
1243 else if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Left)
1244 alignArrayInitializersLeftJustified(getCells(Start, End));
1247void WhitespaceManager::alignArrayInitializersRightJustified(
1248 CellDescriptions &&CellDescs) {
1249 if (!CellDescs.isRectangular())
1252 const int BracePadding =
1253 Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? 0 : 1;
1254 auto &Cells = CellDescs.Cells;
1256 auto *CellIter = Cells.begin();
1257 for (
auto i = 0U; i < CellDescs.CellCounts[0]; ++i, ++CellIter) {
1258 unsigned NetWidth = 0U;
1259 if (isSplitCell(*CellIter))
1260 NetWidth = getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1261 auto CellWidth = getMaximumCellWidth(CellIter, NetWidth);
1263 if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
1267 const auto *
Next = CellIter;
1271 Changes[
Next->Index].NewlinesBefore = 0;
1272 setChangeSpaces(
Next->Index, BracePadding);
1278 if (CellIter != Cells.begin()) {
1280 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1281 auto MaxNetWidth = getMaximumNetWidth(
1282 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1283 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1284 if (ThisNetWidth < MaxNetWidth)
1285 setChangeSpaces(CellIter->Index, MaxNetWidth - ThisNetWidth);
1287 auto Offset = std::distance(Cells.begin(), CellIter);
1288 for (
const auto *
Next = CellIter->NextColumnElement;
Next;
1290 if (RowCount >= CellDescs.CellCounts.size())
1292 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1293 auto *End = Start + Offset;
1294 ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1295 if (ThisNetWidth < MaxNetWidth)
1296 setChangeSpaces(
Next->Index, MaxNetWidth - ThisNetWidth);
1302 calculateCellWidth(CellIter->Index, CellIter->EndIndex,
true) +
1304 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1305 int Spaces = (CellWidth - (ThisWidth + NetWidth));
1306 Spaces += (i > 0) ? 1 : BracePadding;
1308 setChangeSpaces(CellIter->Index, Spaces);
1310 alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
1311 for (
const auto *
Next = CellIter->NextColumnElement;
Next;
1314 calculateCellWidth(
Next->Index,
Next->EndIndex,
true) + NetWidth;
1315 if (Changes[
Next->Index].NewlinesBefore == 0) {
1316 int Spaces = (CellWidth - ThisWidth);
1317 Spaces += (i > 0) ? 1 : BracePadding;
1319 setChangeSpaces(
Next->Index, Spaces);
1321 alignToStartOfCell(
Next->Index,
Next->EndIndex);
1327void WhitespaceManager::alignArrayInitializersLeftJustified(
1328 CellDescriptions &&CellDescs) {
1330 if (!CellDescs.isRectangular())
1333 const int BracePadding =
1334 Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? 0 : 1;
1335 auto &Cells = CellDescs.Cells;
1337 auto *CellIter = Cells.begin();
1343 setChangeSpaces(
Next->Index, Spaces);
1346 for (
auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
1347 auto MaxNetWidth = getMaximumNetWidth(
1348 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1349 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1351 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1352 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1354 MaxNetWidth - ThisNetWidth +
1355 (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1
1357 setChangeSpaces(CellIter->Index, Spaces);
1360 auto Offset = std::distance(Cells.begin(), CellIter);
1361 for (
const auto *
Next = CellIter->NextColumnElement;
Next;
1363 if (RowCount >= CellDescs.CellCounts.size())
1365 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1366 auto *End = Start + Offset;
1367 auto ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1368 if (Changes[
Next->Index].NewlinesBefore == 0) {
1370 MaxNetWidth - ThisNetWidth +
1371 (Changes[
Next->Index].Tok->isNot(tok::r_brace) ? 1 : BracePadding);
1372 setChangeSpaces(
Next->Index, Spaces);
1379bool WhitespaceManager::isSplitCell(
const CellDescription &Cell) {
1382 for (
const auto *
Next = Cell.NextColumnElement;
Next;
1390WhitespaceManager::CellDescriptions WhitespaceManager::getCells(
unsigned Start,
1395 SmallVector<unsigned> CellCounts;
1396 unsigned InitialSpaces = 0;
1397 unsigned InitialTokenLength = 0;
1398 unsigned EndSpaces = 0;
1399 SmallVector<CellDescription> Cells;
1401 for (
unsigned i = Start; i < End; ++i) {
1402 auto &
C = Changes[i];
1403 if (
C.Tok->is(tok::l_brace))
1405 else if (
C.Tok->is(tok::r_brace))
1408 if (
C.Tok->is(tok::l_brace)) {
1411 if (InitialSpaces == 0) {
1412 InitialSpaces =
C.Spaces +
C.TokenLength;
1413 InitialTokenLength =
C.TokenLength;
1415 for (; Changes[j].NewlinesBefore == 0 && j > Start; --j) {
1416 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1417 InitialTokenLength += Changes[j].TokenLength;
1419 if (
C.NewlinesBefore == 0) {
1420 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1421 InitialTokenLength += Changes[j].TokenLength;
1424 }
else if (
C.Tok->is(tok::comma)) {
1426 Cells.back().EndIndex = i;
1427 if (
const auto *
Next =
C.Tok->getNextNonComment();
1428 Next &&
Next->isNot(tok::r_brace)) {
1432 }
else if (Depth == 1) {
1435 Cells.back().EndIndex = i;
1436 Cells.push_back(CellDescription{i, ++Cell, i + 1,
false,
nullptr});
1437 CellCounts.push_back(
C.Tok->Previous->isNot(tok::comma) ? Cell + 1
1440 const auto *NextNonComment =
C.Tok->getNextNonComment();
1441 while (NextNonComment && NextNonComment->is(tok::comma))
1442 NextNonComment = NextNonComment->getNextNonComment();
1444 while (j < End && Changes[j].
Tok != NextNonComment)
1447 Changes[j].
Tok->isNot(tok::r_brace)) {
1448 Changes[j].NewlinesBefore = 1;
1450 setChangeSpaces(j, InitialSpaces - InitialTokenLength);
1452 }
else if (
C.Tok->is(tok::comment) &&
C.Tok->NewlinesBefore == 0) {
1454 setChangeSpaces(i, Changes[i - 1].
Tok->is(tok::comma) ? 1 : 2);
1455 }
else if (
C.Tok->is(tok::l_brace)) {
1459 for (; j > 0 && !Changes[j].Tok->ArrayInitializerLineStart; --j)
1461 EndSpaces = Changes[j].Spaces;
1463 }
else if (Depth == 0 &&
C.Tok->is(tok::r_brace)) {
1464 C.NewlinesBefore = 1;
1465 setChangeSpaces(i, EndSpaces);
1467 if (
C.Tok->StartsColumn) {
1470 bool HasSplit =
false;
1487 if ((j - 1) > Start && Changes[j].
Tok->is(tok::comma) &&
1488 Changes[j - 1].NewlinesBefore > 0) {
1490 auto LineLimit = Changes[j].Spaces + Changes[j].TokenLength;
1491 if (LineLimit < Style.ColumnLimit) {
1492 Changes[i].NewlinesBefore = 0;
1493 setChangeSpaces(i, 1);
1498 setChangeSpaces(i, InitialSpaces);
1502 if (Changes[i].
Tok !=
C.Tok)
1504 Cells.push_back(CellDescription{i, Cell, i, HasSplit,
nullptr});
1508 return linkCells({Cells, CellCounts, InitialSpaces});
1511unsigned WhitespaceManager::calculateCellWidth(
unsigned Start,
unsigned End,
1512 bool WithSpaces)
const {
1513 unsigned CellWidth = 0;
1514 for (
auto i = Start; i < End; i++) {
1517 CellWidth += Changes[i].TokenLength;
1518 CellWidth += (WithSpaces ? Changes[i].Spaces : 0);
1523void WhitespaceManager::alignToStartOfCell(
unsigned Start,
unsigned End) {
1524 if ((End - Start) <= 1)
1528 for (
auto i = Start + 1; i < End; i++)
1530 setChangeSpaces(i, Changes[Start].Spaces);
1533WhitespaceManager::CellDescriptions
1534WhitespaceManager::linkCells(CellDescriptions &&CellDesc) {
1535 auto &Cells = CellDesc.Cells;
1536 for (
auto *CellIter = Cells.begin(); CellIter != Cells.end(); ++CellIter) {
1537 if (!CellIter->NextColumnElement && (CellIter + 1) != Cells.end()) {
1538 for (
auto *NextIter = CellIter + 1; NextIter != Cells.end(); ++NextIter) {
1539 if (NextIter->Cell == CellIter->Cell) {
1540 CellIter->NextColumnElement = &(*NextIter);
1546 return std::move(CellDesc);
1549void WhitespaceManager::setChangeSpaces(
unsigned Start,
unsigned Spaces) {
1553void WhitespaceManager::generateChanges() {
1554 for (
unsigned i = 0, e = Changes.size(); i != e; ++i) {
1557 auto Last = Changes[i - 1].OriginalWhitespaceRange;
1558 auto New = Changes[i].OriginalWhitespaceRange;
1600 if (
Last.getBegin() ==
New.getBegin() &&
1601 (
Last.getEnd() !=
Last.getBegin() ||
1602 New.getEnd() ==
New.getBegin())) {
1606 if (
C.CreateReplacement) {
1607 std::string ReplacementText =
C.PreviousLinePostfix;
1608 if (
C.ContinuesPPDirective) {
1609 appendEscapedNewlineText(ReplacementText,
C.NewlinesBefore,
1610 C.PreviousEndOfTokenColumn,
1611 C.EscapedNewlineColumn);
1613 appendNewlineText(ReplacementText,
C);
1618 appendIndentText(ReplacementText,
IndentLevel, std::max(0,
C.Spaces),
1619 std::max((
int)
C.StartOfTokenColumn,
C.Spaces) -
1620 std::max(0,
C.Spaces),
1622 C.Tok->AppliedIndentLevel =
1623 C.AlignedTo ?
IndentLevel : std::max(0,
C.Spaces) / Style.IndentWidth;
1624 ReplacementText.append(
C.CurrentLinePrefix);
1625 storeReplacement(
C.OriginalWhitespaceRange, ReplacementText);
1630void WhitespaceManager::storeReplacement(SourceRange Range, StringRef
Text) {
1631 unsigned WhitespaceLength = SourceMgr.getFileOffset(
Range.getEnd()) -
1632 SourceMgr.getFileOffset(
Range.getBegin());
1634 if (StringRef(SourceMgr.getCharacterData(
Range.getBegin()),
1635 WhitespaceLength) ==
Text) {
1638 auto Err = Replaces.add(tooling::Replacement(
1643 llvm::errs() << llvm::toString(std::move(Err)) <<
"\n";
1648void WhitespaceManager::appendNewlineText(std::string &
Text,
const Change &
C) {
1649 if (
C.NewlinesBefore <= 0)
1652 StringRef Newline = UseCRLF ?
"\r\n" :
"\n";
1653 Text.append(Newline);
1655 if (
C.Tok->HasFormFeedBefore)
1658 for (
unsigned I = 1; I <
C.NewlinesBefore; ++I)
1659 Text.append(Newline);
1662void WhitespaceManager::appendEscapedNewlineText(
1663 std::string &
Text,
unsigned Newlines,
unsigned PreviousEndOfTokenColumn,
1664 unsigned EscapedNewlineColumn) {
1667 std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
1668 for (
unsigned i = 0; i <
Newlines; ++i) {
1669 Text.append(Spaces,
' ');
1670 Text.append(UseCRLF ?
"\\\r\n" :
"\\\n");
1671 Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
1676void WhitespaceManager::appendIndentText(std::string &
Text,
1678 unsigned WhitespaceStartColumn,
1680 switch (Style.UseTab) {
1681 case FormatStyle::UT_Never:
1682 Text.append(Spaces,
' ');
1684 case FormatStyle::UT_Always: {
1685 if (Style.TabWidth) {
1686 unsigned FirstTabWidth =
1687 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
1690 if (Spaces < FirstTabWidth || Spaces == 1) {
1691 Text.append(Spaces,
' ');
1695 Spaces -= FirstTabWidth;
1698 Text.append(Spaces / Style.TabWidth,
'\t');
1699 Text.append(Spaces % Style.TabWidth,
' ');
1700 }
else if (Spaces == 1) {
1701 Text.append(Spaces,
' ');
1705 case FormatStyle::UT_ForIndentation:
1706 if (WhitespaceStartColumn == 0) {
1707 unsigned Indentation =
IndentLevel * Style.IndentWidth;
1708 Spaces = appendTabIndent(
Text, Spaces, Indentation);
1710 Text.append(Spaces,
' ');
1712 case FormatStyle::UT_ForContinuationAndIndentation:
1713 if (WhitespaceStartColumn == 0)
1714 Spaces = appendTabIndent(
Text, Spaces, Spaces);
1715 Text.append(Spaces,
' ');
1717 case FormatStyle::UT_AlignWithSpaces:
1718 if (WhitespaceStartColumn == 0) {
1719 unsigned Indentation =
1720 IsAligned ?
IndentLevel * Style.IndentWidth : Spaces;
1721 Spaces = appendTabIndent(
Text, Spaces, Indentation);
1723 Text.append(Spaces,
' ');
1728unsigned WhitespaceManager::appendTabIndent(std::string &
Text,
unsigned Spaces,
1729 unsigned Indentation) {
1732 if (Indentation > Spaces)
1733 Indentation = Spaces;
1734 if (Style.TabWidth) {
1735 unsigned Tabs = Indentation / Style.TabWidth;
1736 Text.append(Tabs,
'\t');
1737 Spaces -= Tabs * Style.TabWidth;
WhitespaceManager class manages whitespace around tokens and their replacements.
__DEVICE__ long long abs(long long __n)
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.
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.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Result
The result type of a method or function.
@ Type
The name was classified as a type.