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 &&
817 !Style.AlignConsecutiveAssignments.EnumAssignments) {
825 if (
C.NewlinesBefore > 0)
829 if (&
C != &Changes.back() && (&
C + 1)->NewlinesBefore > 0)
833 if (Style.AlignConsecutiveAssignments.EnumAssignments &&
834 C.Tok->is(TT_EnumEqual)) {
838 if (!Style.AlignConsecutiveAssignments.Enabled)
846 return Style.AlignConsecutiveAssignments.AlignCompound
848 : (
C.Tok->is(tok::equal) ||
852 (Style.isVerilog() &&
C.Tok->is(tok::lessequal) &&
855 Changes, 0, Style.AlignConsecutiveAssignments,
859void WhitespaceManager::alignConsecutiveBitFields() {
860 alignConsecutiveColons(Style.AlignConsecutiveBitFields, TT_BitFieldColon);
863void WhitespaceManager::alignConsecutiveColons(
864 const FormatStyle::AlignConsecutiveStyle &AlignStyle,
TokenType Type) {
865 if (!AlignStyle.Enabled)
872 if (
C.NewlinesBefore > 0)
876 if (&
C != &Changes.back() && (&
C + 1)->NewlinesBefore > 0)
879 return C.Tok->is(
Type);
881 Changes, 0, AlignStyle);
884void WhitespaceManager::alignConsecutiveShortCaseStatements(
bool IsExpr) {
885 if (!Style.AlignConsecutiveShortCaseStatements.Enabled ||
886 !(IsExpr ? Style.AllowShortCaseExpressionOnASingleLine
887 : Style.AllowShortCaseLabelsOnASingleLine)) {
891 const auto Type = IsExpr ? TT_CaseLabelArrow : TT_CaseLabelColon;
892 const auto &Option = Style.AlignConsecutiveShortCaseStatements;
893 const bool AlignArrowOrColon =
894 IsExpr ? Option.AlignCaseArrows : Option.AlignCaseColons;
896 FormatStyle::AlignConsecutiveStyle AlignStyle{};
897 AlignStyle.AcrossComments = Option.AcrossComments;
898 AlignStyle.AcrossEmptyLines = Option.AcrossEmptyLines;
901 if (AlignArrowOrColon) {
903 Style, Matches, Changes, 0, AlignStyle);
906 Style, Matches, Changes, 0, AlignStyle);
910void WhitespaceManager::alignConsecutiveTableGenBreakingDAGArgColons() {
911 alignConsecutiveColons(Style.AlignConsecutiveTableGenBreakingDAGArgColons,
912 TT_TableGenDAGArgListColonToAlign);
915void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
916 alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
917 TT_TableGenCondOperatorColon);
920void WhitespaceManager::alignConsecutiveTableGenDefinitions() {
921 alignConsecutiveColons(Style.AlignConsecutiveTableGenDefinitionColons,
922 TT_InheritanceColon);
925void WhitespaceManager::alignConsecutiveDeclarations() {
926 if (!Style.AlignConsecutiveDeclarations.Enabled)
932 if (
C.Tok->is(TT_FunctionTypeLParen))
933 return Style.AlignConsecutiveDeclarations.AlignFunctionPointers;
934 if (
C.Tok->is(TT_FunctionDeclarationName))
935 return Style.AlignConsecutiveDeclarations.AlignFunctionDeclarations;
936 if (
C.Tok->isNot(TT_StartOfName))
938 if (
C.Tok->Previous &&
939 C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
943 if (
Next->is(tok::comment))
945 if (
Next->is(TT_PointerOrReference))
947 if (!
Next->Tok.getIdentifierInfo())
949 if (
Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
956 Changes, 0, Style.AlignConsecutiveDeclarations);
959void WhitespaceManager::alignChainedConditionals() {
960 if (Style.BreakBeforeTernaryOperators) {
965 return C.Tok->is(TT_ConditionalExpr) &&
966 ((
C.Tok->is(tok::question) && !
C.NewlinesBefore) ||
967 (
C.Tok->is(tok::colon) &&
C.Tok->Next &&
968 (
C.Tok->Next->FakeLParens.empty() ||
973 static auto AlignWrappedOperand = [](
Change const &
C) {
977 (
C.Tok->FakeLParens.empty() ||
984 if (AlignWrappedOperand(
C))
985 C.StartOfTokenColumn -= 2;
992 return (
C.Tok->is(TT_ConditionalExpr) &&
C.Tok->is(tok::question) &&
993 &
C != &Changes.back() && (&
C + 1)->NewlinesBefore == 0 &&
994 !(&
C + 1)->IsTrailingComment) ||
995 AlignWrappedOperand(
C);
1001void WhitespaceManager::alignTrailingComments() {
1002 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Never)
1005 const int Size = Changes.size();
1010 int StartOfSequence = 0;
1011 bool BreakBeforeNext =
false;
1012 bool IsInPP = Changes.front().Tok->Tok.is(tok::hash);
1013 int NewLineThreshold = 1;
1014 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Always)
1015 NewLineThreshold = Style.AlignTrailingComments.OverEmptyLines + 1;
1018 auto &
C = Changes[I];
1019 if (
C.StartOfBlockComment)
1021 if (
C.NewlinesBefore != 0) {
1023 const bool WasInPP = std::exchange(
1024 IsInPP,
C.Tok->Tok.is(tok::hash) || (IsInPP &&
C.IsTrailingComment) ||
1025 C.ContinuesPPDirective);
1026 if (IsInPP != WasInPP && !Style.AlignTrailingComments.AlignPPAndNotPP) {
1027 alignTrailingComments(StartOfSequence, I, MinColumn);
1030 StartOfSequence = I;
1034 if (!
C.IsTrailingComment)
1037 if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
1038 const int OriginalSpaces =
1039 C.OriginalWhitespaceRange.getEnd().getRawEncoding() -
1040 C.OriginalWhitespaceRange.getBegin().getRawEncoding() -
1041 C.Tok->LastNewlineOffset;
1042 assert(OriginalSpaces >= 0);
1043 const auto RestoredLineLength =
1044 C.StartOfTokenColumn +
C.TokenLength + OriginalSpaces;
1047 if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit > 0)
1051 C.NewlinesBefore > 0 ?
C.Tok->OriginalColumn : OriginalSpaces;
1052 setChangeSpaces(I, Spaces);
1056 const int ChangeMinColumn =
C.StartOfTokenColumn;
1057 int ChangeMaxColumn;
1061 if (!
C.CreateReplacement)
1062 ChangeMaxColumn = ChangeMinColumn;
1063 else if (Style.ColumnLimit == 0)
1065 else if (Style.ColumnLimit >=
C.TokenLength)
1066 ChangeMaxColumn = Style.ColumnLimit -
C.TokenLength;
1068 ChangeMaxColumn = ChangeMinColumn;
1070 if (I + 1 < Size && Changes[I + 1].ContinuesPPDirective &&
1071 ChangeMaxColumn >= 2) {
1072 ChangeMaxColumn -= 2;
1075 bool WasAlignedWithStartOfNextLine =
false;
1076 if (
C.NewlinesBefore >= 1) {
1077 const auto CommentColumn =
1078 SourceMgr.getSpellingColumnNumber(
C.OriginalWhitespaceRange.getEnd());
1079 for (
int J = I + 1; J <
Size; ++J) {
1080 if (Changes[J].
Tok->is(tok::comment))
1084 C.AlignedTo =
C.Tok->getPrevious(tok::comment);
1085 const auto NextColumn = SourceMgr.getSpellingColumnNumber(
1086 Changes[J].OriginalWhitespaceRange.getEnd());
1089 WasAlignedWithStartOfNextLine =
1090 CommentColumn == NextColumn ||
1091 CommentColumn == NextColumn + Style.IndentWidth;
1098 auto DontAlignThisComment = [](
const auto *
Tok) {
1099 if (
Tok->is(tok::semi)) {
1100 Tok =
Tok->getPreviousNonComment();
1104 if (
Tok->is(tok::r_paren)) {
1106 Tok =
Tok->MatchingParen;
1109 Tok =
Tok->getPreviousNonComment();
1112 if (
Tok->is(TT_DoWhile)) {
1113 const auto *Prev =
Tok->getPreviousNonComment();
1122 if (
Tok->isNot(tok::r_brace))
1125 while (
Tok->Previous &&
Tok->Previous->is(tok::r_brace))
1127 return Tok->NewlinesBefore > 0;
1130 if (I > 0 &&
C.NewlinesBefore == 0 &&
1131 DontAlignThisComment(Changes[I - 1].
Tok)) {
1132 alignTrailingComments(StartOfSequence, I, MinColumn);
1137 StartOfSequence = I + 1;
1138 }
else if (BreakBeforeNext ||
Newlines > NewLineThreshold ||
1139 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
1142 (
C.NewlinesBefore == 1 && I > 0 &&
1143 !Changes[I - 1].IsTrailingComment) ||
1144 WasAlignedWithStartOfNextLine) {
1145 alignTrailingComments(StartOfSequence, I, MinColumn);
1146 MinColumn = ChangeMinColumn;
1147 MaxColumn = ChangeMaxColumn;
1148 StartOfSequence = I;
1150 MinColumn = std::max(MinColumn, ChangeMinColumn);
1151 MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
1153 BreakBeforeNext = (I == 0) || (
C.NewlinesBefore > 1) ||
1156 (
C.NewlinesBefore == 1 && StartOfSequence == I);
1159 alignTrailingComments(StartOfSequence, Size, MinColumn);
1162void WhitespaceManager::alignTrailingComments(
unsigned Start,
unsigned End,
1164 for (
unsigned i = Start; i != End; ++i) {
1166 if (Changes[i].IsTrailingComment)
1168 if (Changes[i].StartOfBlockComment) {
1169 Shift = Changes[i].IndentationOffset +
1170 Changes[i].StartOfBlockComment->StartOfTokenColumn -
1171 Changes[i].StartOfTokenColumn;
1176 setChangeSpaces(i, Changes[i].Spaces + Shift);
1180void WhitespaceManager::alignEscapedNewlines() {
1181 const auto Align = Style.AlignEscapedNewlines;
1182 if (Align == FormatStyle::ENAS_DontAlign)
1185 const bool WithLastLine = Align == FormatStyle::ENAS_LeftWithLastLine;
1186 const bool AlignLeft = Align == FormatStyle::ENAS_Left || WithLastLine;
1187 const auto MaxColumn = Style.ColumnLimit;
1188 unsigned MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
1189 unsigned StartOfMacro = 0;
1190 for (
unsigned i = 1, e = Changes.size(); i < e; ++i) {
1192 if (
C.NewlinesBefore == 0 && (!WithLastLine ||
C.Tok->isNot(tok::eof)))
1195 const auto BackslashColumn =
C.PreviousEndOfTokenColumn + 2;
1196 if (InPPDirective ||
1197 (WithLastLine && (MaxColumn == 0 || BackslashColumn <= MaxColumn))) {
1198 MaxEndOfLine = std::max(BackslashColumn, MaxEndOfLine);
1200 if (!InPPDirective) {
1201 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
1202 MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
1206 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
1209void WhitespaceManager::alignEscapedNewlines(
unsigned Start,
unsigned End,
1211 for (
unsigned i = Start; i < End; ++i) {
1213 if (
C.NewlinesBefore > 0) {
1214 assert(
C.ContinuesPPDirective);
1215 if (
C.PreviousEndOfTokenColumn + 1 >
Column)
1216 C.EscapedNewlineColumn = 0;
1218 C.EscapedNewlineColumn =
Column;
1223void WhitespaceManager::alignArrayInitializers() {
1224 if (Style.AlignArrayOfStructures == FormatStyle::AIAS_None)
1227 for (
unsigned ChangeIndex = 1U, ChangeEnd = Changes.size();
1228 ChangeIndex < ChangeEnd; ++ChangeIndex) {
1229 auto &
C = Changes[ChangeIndex];
1230 if (
C.Tok->IsArrayInitializer) {
1231 bool FoundComplete =
false;
1232 for (
unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
1234 const auto *
Tok = Changes[InsideIndex].Tok;
1235 if (
Tok->is(tok::pp_define))
1237 if (
Tok ==
C.Tok->MatchingParen) {
1238 alignArrayInitializers(ChangeIndex, InsideIndex + 1);
1239 ChangeIndex = InsideIndex + 1;
1240 FoundComplete =
true;
1245 ChangeIndex = ChangeEnd;
1250void WhitespaceManager::alignArrayInitializers(
unsigned Start,
unsigned End) {
1252 if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Right)
1253 alignArrayInitializersRightJustified(getCells(Start, End));
1254 else if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Left)
1255 alignArrayInitializersLeftJustified(getCells(Start, End));
1258void WhitespaceManager::alignArrayInitializersRightJustified(
1259 CellDescriptions &&CellDescs) {
1260 if (!CellDescs.isRectangular())
1263 const int BracePadding =
1264 Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? 0 : 1;
1265 auto &Cells = CellDescs.Cells;
1267 auto *CellIter = Cells.begin();
1268 for (
auto i = 0U; i < CellDescs.CellCounts[0]; ++i, ++CellIter) {
1269 unsigned NetWidth = 0U;
1270 if (isSplitCell(*CellIter))
1271 NetWidth = getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1272 auto CellWidth = getMaximumCellWidth(CellIter, NetWidth);
1274 if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
1278 const auto *
Next = CellIter;
1282 Changes[
Next->Index].NewlinesBefore = 0;
1283 setChangeSpaces(
Next->Index, BracePadding);
1289 if (CellIter != Cells.begin()) {
1291 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1292 auto MaxNetWidth = getMaximumNetWidth(
1293 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1294 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1295 if (ThisNetWidth < MaxNetWidth)
1296 setChangeSpaces(CellIter->Index, MaxNetWidth - ThisNetWidth);
1298 auto Offset = std::distance(Cells.begin(), CellIter);
1299 for (
const auto *
Next = CellIter->NextColumnElement;
Next;
1301 if (RowCount >= CellDescs.CellCounts.size())
1303 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1304 auto *End = Start + Offset;
1305 ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1306 if (ThisNetWidth < MaxNetWidth)
1307 setChangeSpaces(
Next->Index, MaxNetWidth - ThisNetWidth);
1313 calculateCellWidth(CellIter->Index, CellIter->EndIndex,
true) +
1315 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1316 int Spaces = (CellWidth - (ThisWidth + NetWidth));
1317 Spaces += (i > 0) ? 1 : BracePadding;
1319 setChangeSpaces(CellIter->Index, Spaces);
1321 alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
1322 for (
const auto *
Next = CellIter->NextColumnElement;
Next;
1325 calculateCellWidth(
Next->Index,
Next->EndIndex,
true) + NetWidth;
1326 if (Changes[
Next->Index].NewlinesBefore == 0) {
1327 int Spaces = (CellWidth - ThisWidth);
1328 Spaces += (i > 0) ? 1 : BracePadding;
1330 setChangeSpaces(
Next->Index, Spaces);
1332 alignToStartOfCell(
Next->Index,
Next->EndIndex);
1338void WhitespaceManager::alignArrayInitializersLeftJustified(
1339 CellDescriptions &&CellDescs) {
1341 if (!CellDescs.isRectangular())
1344 const int BracePadding =
1345 Style.Cpp11BracedListStyle != FormatStyle::BLS_Block ? 0 : 1;
1346 auto &Cells = CellDescs.Cells;
1348 auto *CellIter = Cells.begin();
1354 setChangeSpaces(
Next->Index, Spaces);
1357 for (
auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
1358 auto MaxNetWidth = getMaximumNetWidth(
1359 Cells.begin(), CellIter, CellDescs.InitialSpaces,
1360 CellDescs.CellCounts[0], CellDescs.CellCounts.size());
1362 getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
1363 if (Changes[CellIter->Index].NewlinesBefore == 0) {
1365 MaxNetWidth - ThisNetWidth +
1366 (Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1
1368 setChangeSpaces(CellIter->Index, Spaces);
1371 auto Offset = std::distance(Cells.begin(), CellIter);
1372 for (
const auto *
Next = CellIter->NextColumnElement;
Next;
1374 if (RowCount >= CellDescs.CellCounts.size())
1376 auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
1377 auto *End = Start + Offset;
1378 auto ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
1379 if (Changes[
Next->Index].NewlinesBefore == 0) {
1381 MaxNetWidth - ThisNetWidth +
1382 (Changes[
Next->Index].Tok->isNot(tok::r_brace) ? 1 : BracePadding);
1383 setChangeSpaces(
Next->Index, Spaces);
1390bool WhitespaceManager::isSplitCell(
const CellDescription &Cell) {
1393 for (
const auto *
Next = Cell.NextColumnElement;
Next;
1401WhitespaceManager::CellDescriptions WhitespaceManager::getCells(
unsigned Start,
1406 SmallVector<unsigned> CellCounts;
1407 unsigned InitialSpaces = 0;
1408 unsigned InitialTokenLength = 0;
1409 unsigned EndSpaces = 0;
1410 SmallVector<CellDescription> Cells;
1412 for (
unsigned i = Start; i < End; ++i) {
1413 auto &
C = Changes[i];
1414 if (
C.Tok->is(tok::l_brace))
1416 else if (
C.Tok->is(tok::r_brace))
1419 if (
C.Tok->is(tok::l_brace)) {
1422 if (InitialSpaces == 0) {
1423 InitialSpaces =
C.Spaces +
C.TokenLength;
1424 InitialTokenLength =
C.TokenLength;
1426 for (; Changes[j].NewlinesBefore == 0 && j > Start; --j) {
1427 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1428 InitialTokenLength += Changes[j].TokenLength;
1430 if (
C.NewlinesBefore == 0) {
1431 InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
1432 InitialTokenLength += Changes[j].TokenLength;
1435 }
else if (
C.Tok->is(tok::comma)) {
1437 Cells.back().EndIndex = i;
1438 if (
const auto *
Next =
C.Tok->getNextNonComment();
1439 Next &&
Next->isNot(tok::r_brace)) {
1443 }
else if (Depth == 1) {
1446 Cells.back().EndIndex = i;
1447 Cells.push_back(CellDescription{i, ++Cell, i + 1,
false,
nullptr});
1448 CellCounts.push_back(
C.Tok->Previous->isNot(tok::comma) ? Cell + 1
1451 const auto *NextNonComment =
C.Tok->getNextNonComment();
1452 while (NextNonComment && NextNonComment->is(tok::comma))
1453 NextNonComment = NextNonComment->getNextNonComment();
1455 while (j < End && Changes[j].
Tok != NextNonComment)
1458 Changes[j].
Tok->isNot(tok::r_brace)) {
1459 Changes[j].NewlinesBefore = 1;
1461 setChangeSpaces(j, InitialSpaces - InitialTokenLength);
1463 }
else if (
C.Tok->is(tok::comment) &&
C.Tok->NewlinesBefore == 0) {
1465 setChangeSpaces(i, Changes[i - 1].
Tok->is(tok::comma) ? 1 : 2);
1466 }
else if (
C.Tok->is(tok::l_brace)) {
1470 for (; j > 0 && !Changes[j].Tok->ArrayInitializerLineStart; --j)
1472 EndSpaces = Changes[j].Spaces;
1474 }
else if (Depth == 0 &&
C.Tok->is(tok::r_brace)) {
1475 C.NewlinesBefore = 1;
1476 setChangeSpaces(i, EndSpaces);
1478 if (
C.Tok->StartsColumn) {
1481 bool HasSplit =
false;
1498 if ((j - 1) > Start && Changes[j].
Tok->is(tok::comma) &&
1499 Changes[j - 1].NewlinesBefore > 0) {
1501 auto LineLimit = Changes[j].Spaces + Changes[j].TokenLength;
1502 if (LineLimit < Style.ColumnLimit) {
1503 Changes[i].NewlinesBefore = 0;
1504 setChangeSpaces(i, 1);
1509 setChangeSpaces(i, InitialSpaces);
1513 if (Changes[i].
Tok !=
C.Tok)
1515 Cells.push_back(CellDescription{i, Cell, i, HasSplit,
nullptr});
1519 return linkCells({Cells, CellCounts, InitialSpaces});
1522unsigned WhitespaceManager::calculateCellWidth(
unsigned Start,
unsigned End,
1523 bool WithSpaces)
const {
1524 unsigned CellWidth = 0;
1525 for (
auto i = Start; i < End; i++) {
1528 CellWidth += Changes[i].TokenLength;
1529 CellWidth += (WithSpaces ? Changes[i].Spaces : 0);
1534void WhitespaceManager::alignToStartOfCell(
unsigned Start,
unsigned End) {
1535 if ((End - Start) <= 1)
1539 for (
auto i = Start + 1; i < End; i++)
1541 setChangeSpaces(i, Changes[Start].Spaces);
1544WhitespaceManager::CellDescriptions
1545WhitespaceManager::linkCells(CellDescriptions &&CellDesc) {
1546 auto &Cells = CellDesc.Cells;
1547 for (
auto *CellIter = Cells.begin(); CellIter != Cells.end(); ++CellIter) {
1548 if (!CellIter->NextColumnElement && (CellIter + 1) != Cells.end()) {
1549 for (
auto *NextIter = CellIter + 1; NextIter != Cells.end(); ++NextIter) {
1550 if (NextIter->Cell == CellIter->Cell) {
1551 CellIter->NextColumnElement = &(*NextIter);
1557 return std::move(CellDesc);
1560void WhitespaceManager::setChangeSpaces(
unsigned Start,
unsigned Spaces) {
1564void WhitespaceManager::generateChanges() {
1565 for (
unsigned i = 0, e = Changes.size(); i != e; ++i) {
1568 auto Last = Changes[i - 1].OriginalWhitespaceRange;
1569 auto New = Changes[i].OriginalWhitespaceRange;
1611 if (
Last.getBegin() ==
New.getBegin() &&
1612 (
Last.getEnd() !=
Last.getBegin() ||
1613 New.getEnd() ==
New.getBegin())) {
1617 if (
C.CreateReplacement) {
1618 std::string ReplacementText =
C.PreviousLinePostfix;
1619 if (
C.ContinuesPPDirective) {
1620 appendEscapedNewlineText(ReplacementText,
C.NewlinesBefore,
1621 C.PreviousEndOfTokenColumn,
1622 C.EscapedNewlineColumn);
1624 appendNewlineText(ReplacementText,
C);
1629 appendIndentText(ReplacementText,
IndentLevel, std::max(0,
C.Spaces),
1630 std::max((
int)
C.StartOfTokenColumn,
C.Spaces) -
1631 std::max(0,
C.Spaces),
1633 C.Tok->AppliedIndentLevel =
1634 C.AlignedTo ?
IndentLevel : std::max(0,
C.Spaces) / Style.IndentWidth;
1635 ReplacementText.append(
C.CurrentLinePrefix);
1636 storeReplacement(
C.OriginalWhitespaceRange, ReplacementText);
1641void WhitespaceManager::storeReplacement(SourceRange Range, StringRef
Text) {
1642 unsigned WhitespaceLength = SourceMgr.getFileOffset(
Range.getEnd()) -
1643 SourceMgr.getFileOffset(
Range.getBegin());
1645 if (StringRef(SourceMgr.getCharacterData(
Range.getBegin()),
1646 WhitespaceLength) ==
Text) {
1649 auto Err = Replaces.add(tooling::Replacement(
1654 llvm::errs() << llvm::toString(std::move(Err)) <<
"\n";
1659void WhitespaceManager::appendNewlineText(std::string &
Text,
const Change &
C) {
1660 if (
C.NewlinesBefore <= 0)
1663 StringRef Newline = UseCRLF ?
"\r\n" :
"\n";
1664 Text.append(Newline);
1666 if (
C.Tok->HasFormFeedBefore)
1669 for (
unsigned I = 1; I <
C.NewlinesBefore; ++I)
1670 Text.append(Newline);
1673void WhitespaceManager::appendEscapedNewlineText(
1674 std::string &
Text,
unsigned Newlines,
unsigned PreviousEndOfTokenColumn,
1675 unsigned EscapedNewlineColumn) {
1678 std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
1679 for (
unsigned i = 0; i <
Newlines; ++i) {
1680 Text.append(Spaces,
' ');
1681 Text.append(UseCRLF ?
"\\\r\n" :
"\\\n");
1682 Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
1687void WhitespaceManager::appendIndentText(std::string &
Text,
1689 unsigned WhitespaceStartColumn,
1691 switch (Style.UseTab) {
1692 case FormatStyle::UT_Never:
1693 Text.append(Spaces,
' ');
1695 case FormatStyle::UT_Always: {
1696 if (Style.TabWidth) {
1697 unsigned FirstTabWidth =
1698 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
1701 if (Spaces < FirstTabWidth || Spaces == 1) {
1702 Text.append(Spaces,
' ');
1706 Spaces -= FirstTabWidth;
1709 Text.append(Spaces / Style.TabWidth,
'\t');
1710 Text.append(Spaces % Style.TabWidth,
' ');
1711 }
else if (Spaces == 1) {
1712 Text.append(Spaces,
' ');
1716 case FormatStyle::UT_ForIndentation:
1717 if (WhitespaceStartColumn == 0) {
1718 unsigned Indentation =
IndentLevel * Style.IndentWidth;
1719 Spaces = appendTabIndent(
Text, Spaces, Indentation);
1721 Text.append(Spaces,
' ');
1723 case FormatStyle::UT_ForContinuationAndIndentation:
1724 if (WhitespaceStartColumn == 0)
1725 Spaces = appendTabIndent(
Text, Spaces, Spaces);
1726 Text.append(Spaces,
' ');
1728 case FormatStyle::UT_AlignWithSpaces:
1729 if (WhitespaceStartColumn == 0) {
1730 unsigned Indentation =
1731 IsAligned ?
IndentLevel * Style.IndentWidth : Spaces;
1732 Spaces = appendTabIndent(
Text, Spaces, Indentation);
1734 Text.append(Spaces,
' ');
1739unsigned WhitespaceManager::appendTabIndent(std::string &
Text,
unsigned Spaces,
1740 unsigned Indentation) {
1743 if (Indentation > Spaces)
1744 Indentation = Spaces;
1745 if (Style.TabWidth) {
1746 unsigned Tabs = Indentation / Style.TabWidth;
1747 Text.append(Tabs,
'\t');
1748 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.