#include "WhitespaceManager.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include <algorithm>
namespace clang {
namespace format {
bool WhitespaceManager::Change::IsBeforeInFile::operator()(
const Change &C1, const Change &C2) const {
return SourceMgr.isBeforeInTranslationUnit(
C1.OriginalWhitespaceRange.getBegin(),
C2.OriginalWhitespaceRange.getBegin()) ||
(C1.OriginalWhitespaceRange.getBegin() ==
C2.OriginalWhitespaceRange.getBegin() &&
SourceMgr.isBeforeInTranslationUnit(
C1.OriginalWhitespaceRange.getEnd(),
C2.OriginalWhitespaceRange.getEnd()));
}
WhitespaceManager::Change::Change(const FormatToken &Tok,
bool CreateReplacement,
SourceRange OriginalWhitespaceRange,
int Spaces, unsigned StartOfTokenColumn,
unsigned NewlinesBefore,
StringRef PreviousLinePostfix,
StringRef CurrentLinePrefix, bool IsAligned,
bool ContinuesPPDirective, bool IsInsideToken)
: Tok(&Tok), CreateReplacement(CreateReplacement),
OriginalWhitespaceRange(OriginalWhitespaceRange),
StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
PreviousLinePostfix(PreviousLinePostfix),
CurrentLinePrefix(CurrentLinePrefix), IsAligned(IsAligned),
ContinuesPPDirective(ContinuesPPDirective), Spaces(Spaces),
IsInsideToken(IsInsideToken), IsTrailingComment(false), TokenLength(0),
PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
StartOfBlockComment(nullptr), IndentationOffset(0), ConditionalsLevel(0) {
}
void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
unsigned Spaces,
unsigned StartOfTokenColumn,
bool IsAligned, bool InPPDirective) {
if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
return;
Tok.setDecision((Newlines > 0) ? FD_Break : FD_Continue);
Changes.push_back(Change(Tok, true, Tok.WhitespaceRange,
Spaces, StartOfTokenColumn, Newlines, "", "",
IsAligned, InPPDirective && !Tok.IsFirst,
false));
}
void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
bool InPPDirective) {
if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
return;
Changes.push_back(Change(Tok, false,
Tok.WhitespaceRange, 0,
Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
false, InPPDirective && !Tok.IsFirst,
false));
}
llvm::Error
WhitespaceManager::addReplacement(const tooling::Replacement &Replacement) {
return Replaces.add(Replacement);
}
bool WhitespaceManager::inputUsesCRLF(StringRef Text, bool DefaultToCRLF) {
size_t LF = Text.count('\n');
size_t CR = Text.count('\r') * 2;
return LF == CR ? DefaultToCRLF : CR > LF;
}
void WhitespaceManager::replaceWhitespaceInToken(
const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
unsigned Newlines, int Spaces) {
if (Tok.Finalized || (Tok.MacroCtx && Tok.MacroCtx->Role == MR_ExpandedArg))
return;
SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
Changes.push_back(
Change(Tok, true,
SourceRange(Start, Start.getLocWithOffset(ReplaceChars)), Spaces,
std::max(0, Spaces), Newlines, PreviousPostfix, CurrentPrefix,
true, InPPDirective && !Tok.IsFirst,
true));
}
const tooling::Replacements &WhitespaceManager::generateReplacements() {
if (Changes.empty())
return Replaces;
llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr));
calculateLineBreakInformation();
alignConsecutiveMacros();
alignConsecutiveShortCaseStatements(true);
alignConsecutiveShortCaseStatements(false);
alignConsecutiveDeclarations();
alignConsecutiveBitFields();
alignConsecutiveAssignments();
if (Style.isTableGen()) {
alignConsecutiveTableGenBreakingDAGArgColons();
alignConsecutiveTableGenCondOperatorColons();
alignConsecutiveTableGenDefinitions();
}
alignChainedConditionals();
alignTrailingComments();
alignEscapedNewlines();
alignArrayInitializers();
generateChanges();
return Replaces;
}
void WhitespaceManager::calculateLineBreakInformation() {
Changes[0].PreviousEndOfTokenColumn = 0;
Change *LastOutsideTokenChange = &Changes[0];
for (unsigned I = 1, e = Changes.size(); I != e; ++I) {
auto &C = Changes[I];
auto &P = Changes[I - 1];
auto &PrevTokLength = P.TokenLength;
SourceLocation OriginalWhitespaceStart =
C.OriginalWhitespaceRange.getBegin();
SourceLocation PreviousOriginalWhitespaceEnd =
P.OriginalWhitespaceRange.getEnd();
unsigned OriginalWhitespaceStartOffset =
SourceMgr.getFileOffset(OriginalWhitespaceStart);
unsigned PreviousOriginalWhitespaceEndOffset =
SourceMgr.getFileOffset(PreviousOriginalWhitespaceEnd);
assert(PreviousOriginalWhitespaceEndOffset <=
OriginalWhitespaceStartOffset);
const char *const PreviousOriginalWhitespaceEndData =
SourceMgr.getCharacterData(PreviousOriginalWhitespaceEnd);
StringRef Text(PreviousOriginalWhitespaceEndData,
SourceMgr.getCharacterData(OriginalWhitespaceStart) -
PreviousOriginalWhitespaceEndData);
auto NewlinePos = Text.find_first_of('\n');
if (NewlinePos == StringRef::npos) {
PrevTokLength = OriginalWhitespaceStartOffset -
PreviousOriginalWhitespaceEndOffset +
C.PreviousLinePostfix.size() + P.CurrentLinePrefix.size();
if (!P.IsInsideToken)
PrevTokLength = std::min(PrevTokLength, P.Tok->ColumnWidth);
} else {
PrevTokLength = NewlinePos + P.CurrentLinePrefix.size();
}
if (P.IsInsideToken && P.NewlinesBefore == 0)
LastOutsideTokenChange->TokenLength += PrevTokLength + P.Spaces;
else
LastOutsideTokenChange = &P;
C.PreviousEndOfTokenColumn = P.StartOfTokenColumn + PrevTokLength;
P.IsTrailingComment =
(C.NewlinesBefore > 0 || C.Tok->is(tok::eof) ||
(C.IsInsideToken && C.Tok->is(tok::comment))) &&
P.Tok->is(tok::comment) &&
OriginalWhitespaceStart != PreviousOriginalWhitespaceEnd;
}
Changes.back().TokenLength = 0;
Changes.back().IsTrailingComment = Changes.back().Tok->is(tok::comment);
const WhitespaceManager::Change *LastBlockComment = nullptr;
for (auto &Change : Changes) {
if (Change.IsInsideToken && Change.NewlinesBefore == 0)
Change.IsTrailingComment = false;
Change.StartOfBlockComment = nullptr;
Change.IndentationOffset = 0;
if (Change.Tok->is(tok::comment)) {
if (Change.Tok->is(TT_LineComment) || !Change.IsInsideToken) {
LastBlockComment = &Change;
} else if ((Change.StartOfBlockComment = LastBlockComment)) {
Change.IndentationOffset =
Change.StartOfTokenColumn -
Change.StartOfBlockComment->StartOfTokenColumn;
}
} else {
LastBlockComment = nullptr;
}
}
SmallVector<bool, 16> ScopeStack;
int ConditionalsLevel = 0;
for (auto &Change : Changes) {
for (unsigned i = 0, e = Change.Tok->FakeLParens.size(); i != e; ++i) {
bool isNestedConditional =
Change.Tok->FakeLParens[e - 1 - i] == prec::Conditional &&
!(i == 0 && Change.Tok->Previous &&
Change.Tok->Previous->is(TT_ConditionalExpr) &&
Change.Tok->Previous->is(tok::colon));
if (isNestedConditional)
++ConditionalsLevel;
ScopeStack.push_back(isNestedConditional);
}
Change.ConditionalsLevel = ConditionalsLevel;
for (unsigned i = Change.Tok->FakeRParens; i > 0 && ScopeStack.size(); --i)
if (ScopeStack.pop_back_val())
--ConditionalsLevel;
}
}
template <typename F>
static void
AlignTokenSequence(const FormatStyle &Style, unsigned Start, unsigned End,
unsigned Column, bool RightJustify, F &&Matches,
SmallVector<WhitespaceManager::Change, 16> &Changes) {
bool FoundMatchOnLine = false;
int Shift = 0;
SmallVector<unsigned, 16> ScopeStack;
for (unsigned i = Start; i != End; ++i) {
auto &CurrentChange = Changes[i];
if (ScopeStack.size() != 0 &&
CurrentChange.indentAndNestingLevel() <
Changes[ScopeStack.back()].indentAndNestingLevel()) {
ScopeStack.pop_back();
}
unsigned PreviousNonComment = i - 1;
while (PreviousNonComment > Start &&
Changes[PreviousNonComment].Tok->is(tok::comment)) {
--PreviousNonComment;
}
if (i != Start && CurrentChange.indentAndNestingLevel() >
Changes[PreviousNonComment].indentAndNestingLevel()) {
ScopeStack.push_back(i);
}
bool InsideNestedScope = ScopeStack.size() != 0;
bool ContinuedStringLiteral = i > Start &&
CurrentChange.Tok->is(tok::string_literal) &&
Changes[i - 1].Tok->is(tok::string_literal);
bool SkipMatchCheck = InsideNestedScope || ContinuedStringLiteral;
if (CurrentChange.NewlinesBefore > 0 && !SkipMatchCheck) {
Shift = 0;
FoundMatchOnLine = false;
}
if (!FoundMatchOnLine && !SkipMatchCheck && Matches(CurrentChange)) {
FoundMatchOnLine = true;
Shift = Column - (RightJustify ? CurrentChange.TokenLength : 0) -
CurrentChange.StartOfTokenColumn;
CurrentChange.Spaces += Shift;
if (CurrentChange.NewlinesBefore == 0) {
CurrentChange.Spaces =
std::max(CurrentChange.Spaces,
static_cast<int>(CurrentChange.Tok->SpacesRequiredBefore));
}
}
if (Shift == 0)
continue;
if (InsideNestedScope && CurrentChange.NewlinesBefore > 0) {
unsigned ScopeStart = ScopeStack.back();
auto ShouldShiftBeAdded = [&] {
if (Changes[ScopeStart - 1].Tok->is(TT_FunctionDeclarationName))
return true;
if (Changes[ScopeStart - 1].Tok->is(TT_LambdaLBrace))
return false;
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->is(TT_FunctionDeclarationName)) {
return true;
}
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->isOneOf(tok::identifier,
TT_TemplateCloser) &&
Changes[ScopeStart - 1].Tok->is(tok::l_paren) &&
Changes[ScopeStart].Tok->isNot(TT_LambdaLSquare)) {
if (CurrentChange.Tok->MatchingParen &&
CurrentChange.Tok->MatchingParen->is(TT_LambdaLBrace)) {
return false;
}
if (Changes[ScopeStart].NewlinesBefore > 0)
return false;
if (CurrentChange.Tok->is(tok::l_brace) &&
CurrentChange.Tok->is(BK_BracedInit)) {
return true;
}
return Style.BinPackArguments;
}
if (CurrentChange.Tok->is(TT_ConditionalExpr))
return true;
if (CurrentChange.Tok->is(TT_DesignatedInitializerPeriod))
return true;
if (CurrentChange.Tok->Previous &&
CurrentChange.Tok->Previous->is(TT_ConditionalExpr)) {
return true;
}
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->is(tok::identifier) &&
Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
CurrentChange.Tok->is(tok::l_brace) &&
CurrentChange.Tok->is(BK_BracedInit)) {
return true;
}
if (ScopeStart > Start + 1 &&
Changes[ScopeStart - 2].Tok->isNot(tok::identifier) &&
Changes[ScopeStart - 1].Tok->is(tok::l_brace) &&
CurrentChange.Tok->isNot(tok::r_brace)) {
for (unsigned OuterScopeStart : llvm::reverse(ScopeStack)) {
if (OuterScopeStart > Start &&
Changes[OuterScopeStart - 1].Tok->is(TT_LambdaLBrace)) {
return false;
}
}
if (Changes[ScopeStart].NewlinesBefore > 0)
return false;
return true;
}
if (Changes[ScopeStart - 1].Tok->is(TT_TemplateOpener))
return true;
return false;
};
if (ShouldShiftBeAdded())
CurrentChange.Spaces += Shift;
}
if (ContinuedStringLiteral)
CurrentChange.Spaces += Shift;
assert(Shift > 0 || Changes[i].NewlinesBefore > 0 ||
CurrentChange.Spaces >=
static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) ||
CurrentChange.Tok->is(tok::eof));
CurrentChange.StartOfTokenColumn += Shift;
if (i + 1 != Changes.size())
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
if ((Style.PointerAlignment == FormatStyle::PAS_Right ||
Style.ReferenceAlignment == FormatStyle::RAS_Right) &&
CurrentChange.Spaces != 0 &&
!CurrentChange.Tok->isOneOf(tok::equal, tok::r_paren,
TT_TemplateCloser)) {
const bool ReferenceNotRightAligned =
Style.ReferenceAlignment != FormatStyle::RAS_Right &&
Style.ReferenceAlignment != FormatStyle::RAS_Pointer;
for (int Previous = i - 1;
Previous >= 0 && Changes[Previous].Tok->is(TT_PointerOrReference);
--Previous) {
assert(Changes[Previous].Tok->isPointerOrReference());
if (Changes[Previous].Tok->isNot(tok::star)) {
if (ReferenceNotRightAligned)
continue;
} else if (Style.PointerAlignment != FormatStyle::PAS_Right) {
continue;
}
Changes[Previous + 1].Spaces -= Shift;
Changes[Previous].Spaces += Shift;
Changes[Previous].StartOfTokenColumn += Shift;
}
}
}
}
template <typename F>
static unsigned AlignTokens(const FormatStyle &Style, F &&Matches,
SmallVector<WhitespaceManager::Change, 16> &Changes,
unsigned StartAt,
const FormatStyle::AlignConsecutiveStyle &ACS = {},
bool RightJustify = false) {
unsigned WidthLeft = 0;
unsigned WidthAnchor = 0;
unsigned WidthRight = 0;
unsigned StartOfSequence = 0;
unsigned EndOfSequence = 0;
auto IndentAndNestingLevel = StartAt < Changes.size()
? Changes[StartAt].indentAndNestingLevel()
: std::tuple<unsigned, unsigned, unsigned>();
unsigned CommasBeforeLastMatch = 0;
unsigned CommasBeforeMatch = 0;
bool FoundMatchOnLine = false;
bool LineIsComment = true;
auto AlignCurrentSequence = [&] {
if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
AlignTokenSequence(Style, StartOfSequence, EndOfSequence,
WidthLeft + WidthAnchor, RightJustify, Matches,
Changes);
}
WidthLeft = 0;
WidthAnchor = 0;
WidthRight = 0;
StartOfSequence = 0;
EndOfSequence = 0;
};
unsigned i = StartAt;
for (unsigned e = Changes.size(); i != e; ++i) {
auto &CurrentChange = Changes[i];
if (CurrentChange.indentAndNestingLevel() < IndentAndNestingLevel)
break;
if (CurrentChange.NewlinesBefore != 0) {
CommasBeforeMatch = 0;
EndOfSequence = i;
bool EmptyLineBreak =
(CurrentChange.NewlinesBefore > 1) && !ACS.AcrossEmptyLines;
bool NoMatchBreak =
!FoundMatchOnLine && !(LineIsComment && ACS.AcrossComments);
if (EmptyLineBreak || NoMatchBreak)
AlignCurrentSequence();
if (i == 0 || CurrentChange.Tok->isNot(tok::string_literal) ||
Changes[i - 1].Tok->isNot(tok::string_literal)) {
FoundMatchOnLine = false;
}
LineIsComment = true;
}
if (CurrentChange.Tok->isNot(tok::comment))
LineIsComment = false;
if (CurrentChange.Tok->is(tok::comma)) {
++CommasBeforeMatch;
} else if (CurrentChange.indentAndNestingLevel() > IndentAndNestingLevel) {
unsigned StoppedAt =
AlignTokens(Style, Matches, Changes, i, ACS, RightJustify);
i = StoppedAt - 1;
continue;
}
if (!Matches(CurrentChange))
continue;
if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
AlignCurrentSequence();
CommasBeforeLastMatch = CommasBeforeMatch;
FoundMatchOnLine = true;
if (StartOfSequence == 0)
StartOfSequence = i;
unsigned ChangeWidthLeft = CurrentChange.StartOfTokenColumn;
unsigned ChangeWidthAnchor = 0;
unsigned ChangeWidthRight = 0;
if (RightJustify)
if (ACS.PadOperators)
ChangeWidthAnchor = CurrentChange.TokenLength;
else
ChangeWidthLeft += CurrentChange.TokenLength;
else
ChangeWidthRight = CurrentChange.TokenLength;
for (unsigned j = i + 1; j != e && Changes[j].NewlinesBefore == 0; ++j) {
ChangeWidthRight += Changes[j].Spaces;
if (!Changes[j].IsInsideToken)
ChangeWidthRight += Changes[j].TokenLength;
}
unsigned NewLeft = std::max(ChangeWidthLeft, WidthLeft);
unsigned NewAnchor = std::max(ChangeWidthAnchor, WidthAnchor);
unsigned NewRight = std::max(ChangeWidthRight, WidthRight);
if (Style.ColumnLimit != 0 &&
Style.ColumnLimit < NewLeft + NewAnchor + NewRight) {
AlignCurrentSequence();
StartOfSequence = i;
WidthLeft = ChangeWidthLeft;
WidthAnchor = ChangeWidthAnchor;
WidthRight = ChangeWidthRight;
} else {
WidthLeft = NewLeft;
WidthAnchor = NewAnchor;
WidthRight = NewRight;
}
}
EndOfSequence = i;
AlignCurrentSequence();
return i;
}
static void AlignMatchingTokenSequence(
unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn,
std::function<bool(const WhitespaceManager::Change &C)> Matches,
SmallVector<WhitespaceManager::Change, 16> &Changes) {
if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
bool FoundMatchOnLine = false;
int Shift = 0;
for (unsigned I = StartOfSequence; I != EndOfSequence; ++I) {
if (Changes[I].NewlinesBefore > 0) {
Shift = 0;
FoundMatchOnLine = false;
}
if (!FoundMatchOnLine && Matches(Changes[I])) {
FoundMatchOnLine = true;
Shift = MinColumn - Changes[I].StartOfTokenColumn;
Changes[I].Spaces += Shift;
}
assert(Shift >= 0);
Changes[I].StartOfTokenColumn += Shift;
if (I + 1 != Changes.size())
Changes[I + 1].PreviousEndOfTokenColumn += Shift;
}
}
MinColumn = 0;
StartOfSequence = 0;
EndOfSequence = 0;
}
void WhitespaceManager::alignConsecutiveMacros() {
if (!Style.AlignConsecutiveMacros.Enabled)
return;
auto AlignMacrosMatches = [](const Change &C) {
const FormatToken *Current = C.Tok;
unsigned SpacesRequiredBefore = 1;
if (Current->SpacesRequiredBefore == 0 || !Current->Previous)
return false;
Current = Current->Previous;
if (Current->is(tok::r_paren) && Current->MatchingParen) {
Current = Current->MatchingParen->Previous;
SpacesRequiredBefore = 0;
}
if (!Current || Current->isNot(tok::identifier))
return false;
if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
return false;
return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
};
unsigned MinColumn = 0;
unsigned StartOfSequence = 0;
unsigned EndOfSequence = 0;
bool FoundMatchOnLine = false;
bool LineIsComment = true;
unsigned I = 0;
for (unsigned E = Changes.size(); I != E; ++I) {
if (Changes[I].NewlinesBefore != 0) {
EndOfSequence = I;
bool EmptyLineBreak = (Changes[I].NewlinesBefore > 1) &&
!Style.AlignConsecutiveMacros.AcrossEmptyLines;
bool NoMatchBreak =
!FoundMatchOnLine &&
!(LineIsComment && Style.AlignConsecutiveMacros.AcrossComments);
if (EmptyLineBreak || NoMatchBreak) {
AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn,
AlignMacrosMatches, Changes);
}
FoundMatchOnLine = false;
LineIsComment = true;
}
if (Changes[I].Tok->isNot(tok::comment))
LineIsComment = false;
if (!AlignMacrosMatches(Changes[I]))
continue;
FoundMatchOnLine = true;
if (StartOfSequence == 0)
StartOfSequence = I;
unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
MinColumn = std::max(MinColumn, ChangeMinColumn);
}
EndOfSequence = I;
AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn,
AlignMacrosMatches, Changes);
}
void WhitespaceManager::alignConsecutiveAssignments() {
if (!Style.AlignConsecutiveAssignments.Enabled)
return;
AlignTokens(
Style,
[&](const Change &C) {
if (C.NewlinesBefore > 0)
return false;
if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
return false;
FormatToken *Previous = C.Tok->getPreviousNonComment();
if (Previous && Previous->is(tok::kw_operator))
return false;
return Style.AlignConsecutiveAssignments.AlignCompound
? C.Tok->getPrecedence() == prec::Assignment
: (C.Tok->is(tok::equal) ||
(Style.isVerilog() && C.Tok->is(tok::lessequal) &&
C.Tok->getPrecedence() == prec::Assignment));
},
Changes, 0, Style.AlignConsecutiveAssignments,
true);
}
void WhitespaceManager::alignConsecutiveBitFields() {
alignConsecutiveColons(Style.AlignConsecutiveBitFields, TT_BitFieldColon);
}
void WhitespaceManager::alignConsecutiveColons(
const FormatStyle::AlignConsecutiveStyle &AlignStyle, TokenType Type) {
if (!AlignStyle.Enabled)
return;
AlignTokens(
Style,
[&](Change const &C) {
if (C.NewlinesBefore > 0)
return false;
if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
return false;
return C.Tok->is(Type);
},
Changes, 0, AlignStyle);
}
void WhitespaceManager::alignConsecutiveShortCaseStatements(bool IsExpr) {
if (!Style.AlignConsecutiveShortCaseStatements.Enabled ||
!(IsExpr ? Style.AllowShortCaseExpressionOnASingleLine
: Style.AllowShortCaseLabelsOnASingleLine)) {
return;
}
const auto Type = IsExpr ? TT_CaseLabelArrow : TT_CaseLabelColon;
const auto &Option = Style.AlignConsecutiveShortCaseStatements;
const bool AlignArrowOrColon =
IsExpr ? Option.AlignCaseArrows : Option.AlignCaseColons;
auto Matches = [&](const Change &C) {
if (AlignArrowOrColon)
return C.Tok->is(Type);
return !C.IsInsideToken && C.Tok->Previous && C.Tok->Previous->is(Type);
};
unsigned MinColumn = 0;
unsigned MinEmptyCaseColumn = 0;
unsigned StartOfSequence = 0;
unsigned EndOfSequence = 0;
bool FoundMatchOnLine = false;
bool LineIsComment = true;
bool LineIsEmptyCase = false;
unsigned I = 0;
for (unsigned E = Changes.size(); I != E; ++I) {
if (Changes[I].NewlinesBefore != 0) {
bool EmptyLineBreak =
(Changes[I].NewlinesBefore > 1) &&
!Style.AlignConsecutiveShortCaseStatements.AcrossEmptyLines;
bool NoMatchBreak =
!FoundMatchOnLine &&
!(LineIsComment &&
Style.AlignConsecutiveShortCaseStatements.AcrossComments) &&
!LineIsEmptyCase;
if (EmptyLineBreak || NoMatchBreak) {
AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn,
Matches, Changes);
MinEmptyCaseColumn = 0;
}
FoundMatchOnLine = false;
LineIsComment = true;
LineIsEmptyCase = false;
}
if (Changes[I].Tok->isNot(tok::comment))
LineIsComment = false;
if (Changes[I].Tok->is(Type)) {
LineIsEmptyCase =
!Changes[I].Tok->Next || Changes[I].Tok->Next->isTrailingComment();
if (LineIsEmptyCase) {
if (Style.AlignConsecutiveShortCaseStatements.AlignCaseColons) {
MinEmptyCaseColumn =
std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn);
} else {
MinEmptyCaseColumn =
std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn + 2);
}
}
}
if (!Matches(Changes[I]))
continue;
if (LineIsEmptyCase)
continue;
FoundMatchOnLine = true;
if (StartOfSequence == 0)
StartOfSequence = I;
EndOfSequence = I + 1;
MinColumn = std::max(MinColumn, Changes[I].StartOfTokenColumn);
MinColumn = std::max(MinColumn, MinEmptyCaseColumn);
}
AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
Changes);
}
void WhitespaceManager::alignConsecutiveTableGenBreakingDAGArgColons() {
alignConsecutiveColons(Style.AlignConsecutiveTableGenBreakingDAGArgColons,
TT_TableGenDAGArgListColonToAlign);
}
void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
TT_TableGenCondOperatorColon);
}
void WhitespaceManager::alignConsecutiveTableGenDefinitions() {
alignConsecutiveColons(Style.AlignConsecutiveTableGenDefinitionColons,
TT_InheritanceColon);
}
void WhitespaceManager::alignConsecutiveDeclarations() {
if (!Style.AlignConsecutiveDeclarations.Enabled)
return;
AlignTokens(
Style,
[&](Change const &C) {
if (Style.AlignConsecutiveDeclarations.AlignFunctionPointers) {
for (const auto *Prev = C.Tok->Previous; Prev; Prev = Prev->Previous)
if (Prev->is(tok::equal))
return false;
if (C.Tok->is(TT_FunctionTypeLParen))
return true;
}
if (C.Tok->is(TT_FunctionDeclarationName))
return true;
if (C.Tok->isNot(TT_StartOfName))
return false;
if (C.Tok->Previous &&
C.Tok->Previous->is(TT_StatementAttributeLikeMacro))
return false;
for (FormatToken *Next = C.Tok->Next; Next; Next = Next->Next) {
if (Next->is(tok::comment))
continue;
if (Next->is(TT_PointerOrReference))
return false;
if (!Next->Tok.getIdentifierInfo())
break;
if (Next->isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
tok::kw_operator)) {
return false;
}
}
return true;
},
Changes, 0, Style.AlignConsecutiveDeclarations);
}
void WhitespaceManager::alignChainedConditionals() {
if (Style.BreakBeforeTernaryOperators) {
AlignTokens(
Style,
[](Change const &C) {
return C.Tok->is(TT_ConditionalExpr) &&
((C.Tok->is(tok::question) && !C.NewlinesBefore) ||
(C.Tok->is(tok::colon) && C.Tok->Next &&
(C.Tok->Next->FakeLParens.size() == 0 ||
C.Tok->Next->FakeLParens.back() != prec::Conditional)));
},
Changes, 0);
} else {
static auto AlignWrappedOperand = [](Change const &C) {
FormatToken *Previous = C.Tok->getPreviousNonComment();
return C.NewlinesBefore && Previous && Previous->is(TT_ConditionalExpr) &&
(Previous->is(tok::colon) &&
(C.Tok->FakeLParens.size() == 0 ||
C.Tok->FakeLParens.back() != prec::Conditional));
};
for (Change &C : Changes)
if (AlignWrappedOperand(C))
C.StartOfTokenColumn -= 2;
AlignTokens(
Style,
[this](Change const &C) {
return (C.Tok->is(TT_ConditionalExpr) && C.Tok->is(tok::question) &&
&C != &Changes.back() && (&C + 1)->NewlinesBefore == 0 &&
!(&C + 1)->IsTrailingComment) ||
AlignWrappedOperand(C);
},
Changes, 0);
}
}
void WhitespaceManager::alignTrailingComments() {
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Never)
return;
const int Size = Changes.size();
int MinColumn = 0;
int StartOfSequence = 0;
bool BreakBeforeNext = false;
int NewLineThreshold = 1;
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Always)
NewLineThreshold = Style.AlignTrailingComments.OverEmptyLines + 1;
for (int I = 0, MaxColumn = INT_MAX, Newlines = 0; I < Size; ++I) {
auto &C = Changes[I];
if (C.StartOfBlockComment)
continue;
Newlines += C.NewlinesBefore;
if (!C.IsTrailingComment)
continue;
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
const int OriginalSpaces =
C.OriginalWhitespaceRange.getEnd().getRawEncoding() -
C.OriginalWhitespaceRange.getBegin().getRawEncoding() -
C.Tok->LastNewlineOffset;
assert(OriginalSpaces >= 0);
const auto RestoredLineLength =
C.StartOfTokenColumn + C.TokenLength + OriginalSpaces;
if (RestoredLineLength >= Style.ColumnLimit && Style.ColumnLimit > 0)
break;
C.Spaces = C.NewlinesBefore > 0 ? C.Tok->OriginalColumn : OriginalSpaces;
continue;
}
const int ChangeMinColumn = C.StartOfTokenColumn;
int ChangeMaxColumn;
if (!C.CreateReplacement)
ChangeMaxColumn = ChangeMinColumn;
else if (Style.ColumnLimit == 0)
ChangeMaxColumn = INT_MAX;
else if (Style.ColumnLimit >= C.TokenLength)
ChangeMaxColumn = Style.ColumnLimit - C.TokenLength;
else
ChangeMaxColumn = ChangeMinColumn;
if (I + 1 < Size && Changes[I + 1].ContinuesPPDirective &&
ChangeMaxColumn >= 2) {
ChangeMaxColumn -= 2;
}
bool WasAlignedWithStartOfNextLine = false;
if (C.NewlinesBefore >= 1) {
const auto CommentColumn =
SourceMgr.getSpellingColumnNumber(C.OriginalWhitespaceRange.getEnd());
for (int J = I + 1; J < Size; ++J) {
if (Changes[J].Tok->is(tok::comment))
continue;
const auto NextColumn = SourceMgr.getSpellingColumnNumber(
Changes[J].OriginalWhitespaceRange.getEnd());
WasAlignedWithStartOfNextLine =
CommentColumn == NextColumn ||
CommentColumn == NextColumn + Style.IndentWidth;
break;
}
}
auto DontAlignThisComment = [](const auto *Tok) {
if (Tok->is(tok::semi)) {
Tok = Tok->getPreviousNonComment();
if (!Tok)
return false;
}
if (Tok->is(tok::r_paren)) {
Tok = Tok->MatchingParen;
if (!Tok)
return false;
Tok = Tok->getPreviousNonComment();
if (!Tok)
return false;
if (Tok->is(TT_DoWhile)) {
const auto *Prev = Tok->getPreviousNonComment();
if (!Prev) {
return true;
}
Tok = Prev;
}
}
if (Tok->isNot(tok::r_brace))
return false;
while (Tok->Previous && Tok->Previous->is(tok::r_brace))
Tok = Tok->Previous;
return Tok->NewlinesBefore > 0;
};
if (I > 0 && C.NewlinesBefore == 0 &&
DontAlignThisComment(Changes[I - 1].Tok)) {
alignTrailingComments(StartOfSequence, I, MinColumn);
MinColumn = 0;
MaxColumn = INT_MAX;
StartOfSequence = I + 1;
} else if (BreakBeforeNext || Newlines > NewLineThreshold ||
(ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
(C.NewlinesBefore == 1 && I > 0 &&
!Changes[I - 1].IsTrailingComment) ||
WasAlignedWithStartOfNextLine) {
alignTrailingComments(StartOfSequence, I, MinColumn);
MinColumn = ChangeMinColumn;
MaxColumn = ChangeMaxColumn;
StartOfSequence = I;
} else {
MinColumn = std::max(MinColumn, ChangeMinColumn);
MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
}
BreakBeforeNext = (I == 0) || (C.NewlinesBefore > 1) ||
(C.NewlinesBefore == 1 && StartOfSequence == I);
Newlines = 0;
}
alignTrailingComments(StartOfSequence, Size, MinColumn);
}
void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
unsigned Column) {
for (unsigned i = Start; i != End; ++i) {
int Shift = 0;
if (Changes[i].IsTrailingComment)
Shift = Column - Changes[i].StartOfTokenColumn;
if (Changes[i].StartOfBlockComment) {
Shift = Changes[i].IndentationOffset +
Changes[i].StartOfBlockComment->StartOfTokenColumn -
Changes[i].StartOfTokenColumn;
}
if (Shift <= 0)
continue;
Changes[i].Spaces += Shift;
if (i + 1 != Changes.size())
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
Changes[i].StartOfTokenColumn += Shift;
}
}
void WhitespaceManager::alignEscapedNewlines() {
const auto Align = Style.AlignEscapedNewlines;
if (Align == FormatStyle::ENAS_DontAlign)
return;
const bool WithLastLine = Align == FormatStyle::ENAS_LeftWithLastLine;
const bool AlignLeft = Align == FormatStyle::ENAS_Left || WithLastLine;
const auto MaxColumn = Style.ColumnLimit;
unsigned MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
unsigned StartOfMacro = 0;
for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
Change &C = Changes[i];
if (C.NewlinesBefore == 0 && (!WithLastLine || C.Tok->isNot(tok::eof)))
continue;
const bool InPPDirective = C.ContinuesPPDirective;
const auto BackslashColumn = C.PreviousEndOfTokenColumn + 2;
if (InPPDirective ||
(WithLastLine && (MaxColumn == 0 || BackslashColumn <= MaxColumn))) {
MaxEndOfLine = std::max(BackslashColumn, MaxEndOfLine);
}
if (!InPPDirective) {
alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
MaxEndOfLine = AlignLeft ? 0 : MaxColumn;
StartOfMacro = i;
}
}
alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
}
void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
unsigned Column) {
for (unsigned i = Start; i < End; ++i) {
Change &C = Changes[i];
if (C.NewlinesBefore > 0) {
assert(C.ContinuesPPDirective);
if (C.PreviousEndOfTokenColumn + 1 > Column)
C.EscapedNewlineColumn = 0;
else
C.EscapedNewlineColumn = Column;
}
}
}
void WhitespaceManager::alignArrayInitializers() {
if (Style.AlignArrayOfStructures == FormatStyle::AIAS_None)
return;
for (unsigned ChangeIndex = 1U, ChangeEnd = Changes.size();
ChangeIndex < ChangeEnd; ++ChangeIndex) {
auto &C = Changes[ChangeIndex];
if (C.Tok->IsArrayInitializer) {
bool FoundComplete = false;
for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
++InsideIndex) {
if (Changes[InsideIndex].Tok == C.Tok->MatchingParen) {
alignArrayInitializers(ChangeIndex, InsideIndex + 1);
ChangeIndex = InsideIndex + 1;
FoundComplete = true;
break;
}
}
if (!FoundComplete)
ChangeIndex = ChangeEnd;
}
}
}
void WhitespaceManager::alignArrayInitializers(unsigned Start, unsigned End) {
if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Right)
alignArrayInitializersRightJustified(getCells(Start, End));
else if (Style.AlignArrayOfStructures == FormatStyle::AIAS_Left)
alignArrayInitializersLeftJustified(getCells(Start, End));
}
void WhitespaceManager::alignArrayInitializersRightJustified(
CellDescriptions &&CellDescs) {
if (!CellDescs.isRectangular())
return;
const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
auto &Cells = CellDescs.Cells;
auto *CellIter = Cells.begin();
for (auto i = 0U; i < CellDescs.CellCounts[0]; ++i, ++CellIter) {
unsigned NetWidth = 0U;
if (isSplitCell(*CellIter))
NetWidth = getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
auto CellWidth = getMaximumCellWidth(CellIter, NetWidth);
if (Changes[CellIter->Index].Tok->is(tok::r_brace)) {
const auto *Next = CellIter;
do {
const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
if (Previous && Previous->isNot(TT_LineComment)) {
Changes[Next->Index].Spaces = BracePadding;
Changes[Next->Index].NewlinesBefore = 0;
}
Next = Next->NextColumnElement;
} while (Next);
if (CellIter != Cells.begin()) {
auto ThisNetWidth =
getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
auto MaxNetWidth = getMaximumNetWidth(
Cells.begin(), CellIter, CellDescs.InitialSpaces,
CellDescs.CellCounts[0], CellDescs.CellCounts.size());
if (ThisNetWidth < MaxNetWidth)
Changes[CellIter->Index].Spaces = (MaxNetWidth - ThisNetWidth);
auto RowCount = 1U;
auto Offset = std::distance(Cells.begin(), CellIter);
for (const auto *Next = CellIter->NextColumnElement; Next;
Next = Next->NextColumnElement) {
if (RowCount >= CellDescs.CellCounts.size())
break;
auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
auto *End = Start + Offset;
ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
if (ThisNetWidth < MaxNetWidth)
Changes[Next->Index].Spaces = (MaxNetWidth - ThisNetWidth);
++RowCount;
}
}
} else {
auto ThisWidth =
calculateCellWidth(CellIter->Index, CellIter->EndIndex, true) +
NetWidth;
if (Changes[CellIter->Index].NewlinesBefore == 0) {
Changes[CellIter->Index].Spaces = (CellWidth - (ThisWidth + NetWidth));
Changes[CellIter->Index].Spaces += (i > 0) ? 1 : BracePadding;
}
alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
for (const auto *Next = CellIter->NextColumnElement; Next;
Next = Next->NextColumnElement) {
ThisWidth =
calculateCellWidth(Next->Index, Next->EndIndex, true) + NetWidth;
if (Changes[Next->Index].NewlinesBefore == 0) {
Changes[Next->Index].Spaces = (CellWidth - ThisWidth);
Changes[Next->Index].Spaces += (i > 0) ? 1 : BracePadding;
}
alignToStartOfCell(Next->Index, Next->EndIndex);
}
}
}
}
void WhitespaceManager::alignArrayInitializersLeftJustified(
CellDescriptions &&CellDescs) {
if (!CellDescs.isRectangular())
return;
const int BracePadding = Style.Cpp11BracedListStyle ? 0 : 1;
auto &Cells = CellDescs.Cells;
auto *CellIter = Cells.begin();
for (const auto *Next = CellIter; Next; Next = Next->NextColumnElement) {
auto &Change = Changes[Next->Index];
Change.Spaces =
Change.NewlinesBefore == 0 ? BracePadding : CellDescs.InitialSpaces;
}
++CellIter;
for (auto i = 1U; i < CellDescs.CellCounts[0]; i++, ++CellIter) {
auto MaxNetWidth = getMaximumNetWidth(
Cells.begin(), CellIter, CellDescs.InitialSpaces,
CellDescs.CellCounts[0], CellDescs.CellCounts.size());
auto ThisNetWidth =
getNetWidth(Cells.begin(), CellIter, CellDescs.InitialSpaces);
if (Changes[CellIter->Index].NewlinesBefore == 0) {
Changes[CellIter->Index].Spaces =
MaxNetWidth - ThisNetWidth +
(Changes[CellIter->Index].Tok->isNot(tok::r_brace) ? 1
: BracePadding);
}
auto RowCount = 1U;
auto Offset = std::distance(Cells.begin(), CellIter);
for (const auto *Next = CellIter->NextColumnElement; Next;
Next = Next->NextColumnElement) {
if (RowCount >= CellDescs.CellCounts.size())
break;
auto *Start = (Cells.begin() + RowCount * CellDescs.CellCounts[0]);
auto *End = Start + Offset;
auto ThisNetWidth = getNetWidth(Start, End, CellDescs.InitialSpaces);
if (Changes[Next->Index].NewlinesBefore == 0) {
Changes[Next->Index].Spaces =
MaxNetWidth - ThisNetWidth +
(Changes[Next->Index].Tok->isNot(tok::r_brace) ? 1 : BracePadding);
}
++RowCount;
}
}
}
bool WhitespaceManager::isSplitCell(const CellDescription &Cell) {
if (Cell.HasSplit)
return true;
for (const auto *Next = Cell.NextColumnElement; Next;
Next = Next->NextColumnElement) {
if (Next->HasSplit)
return true;
}
return false;
}
WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
unsigned End) {
unsigned Depth = 0;
unsigned Cell = 0;
SmallVector<unsigned> CellCounts;
unsigned InitialSpaces = 0;
unsigned InitialTokenLength = 0;
unsigned EndSpaces = 0;
SmallVector<CellDescription> Cells;
const FormatToken *MatchingParen = nullptr;
for (unsigned i = Start; i < End; ++i) {
auto &C = Changes[i];
if (C.Tok->is(tok::l_brace))
++Depth;
else if (C.Tok->is(tok::r_brace))
--Depth;
if (Depth == 2) {
if (C.Tok->is(tok::l_brace)) {
Cell = 0;
MatchingParen = C.Tok->MatchingParen;
if (InitialSpaces == 0) {
InitialSpaces = C.Spaces + C.TokenLength;
InitialTokenLength = C.TokenLength;
auto j = i - 1;
for (; Changes[j].NewlinesBefore == 0 && j > Start; --j) {
InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
InitialTokenLength += Changes[j].TokenLength;
}
if (C.NewlinesBefore == 0) {
InitialSpaces += Changes[j].Spaces + Changes[j].TokenLength;
InitialTokenLength += Changes[j].TokenLength;
}
}
} else if (C.Tok->is(tok::comma)) {
if (!Cells.empty())
Cells.back().EndIndex = i;
if (const auto *Next = C.Tok->getNextNonComment();
Next && Next->isNot(tok::r_brace)) {
++Cell;
}
}
} else if (Depth == 1) {
if (C.Tok == MatchingParen) {
if (!Cells.empty())
Cells.back().EndIndex = i;
Cells.push_back(CellDescription{i, ++Cell, i + 1, false, nullptr});
CellCounts.push_back(C.Tok->Previous->isNot(tok::comma) ? Cell + 1
: Cell);
const auto *NextNonComment = C.Tok->getNextNonComment();
while (NextNonComment && NextNonComment->is(tok::comma))
NextNonComment = NextNonComment->getNextNonComment();
auto j = i;
while (j < End && Changes[j].Tok != NextNonComment)
++j;
if (j < End && Changes[j].NewlinesBefore == 0 &&
Changes[j].Tok->isNot(tok::r_brace)) {
Changes[j].NewlinesBefore = 1;
Changes[j].Spaces = InitialSpaces - InitialTokenLength;
}
} else if (C.Tok->is(tok::comment) && C.Tok->NewlinesBefore == 0) {
C.Spaces = Changes[i - 1].Tok->is(tok::comma) ? 1 : 2;
} else if (C.Tok->is(tok::l_brace)) {
auto j = i - 1;
for (; j > 0 && !Changes[j].Tok->ArrayInitializerLineStart; --j)
;
EndSpaces = Changes[j].Spaces;
}
} else if (Depth == 0 && C.Tok->is(tok::r_brace)) {
C.NewlinesBefore = 1;
C.Spaces = EndSpaces;
}
if (C.Tok->StartsColumn) {
bool HasSplit = false;
if (Changes[i].NewlinesBefore > 0) {
auto j = i - 1;
if ((j - 1) > Start && Changes[j].Tok->is(tok::comma) &&
Changes[j - 1].NewlinesBefore > 0) {
--j;
auto LineLimit = Changes[j].Spaces + Changes[j].TokenLength;
if (LineLimit < Style.ColumnLimit) {
Changes[i].NewlinesBefore = 0;
Changes[i].Spaces = 1;
}
}
}
while (Changes[i].NewlinesBefore > 0 && Changes[i].Tok == C.Tok) {
Changes[i].Spaces = InitialSpaces;
++i;
HasSplit = true;
}
if (Changes[i].Tok != C.Tok)
--i;
Cells.push_back(CellDescription{i, Cell, i, HasSplit, nullptr});
}
}
return linkCells({Cells, CellCounts, InitialSpaces});
}
unsigned WhitespaceManager::calculateCellWidth(unsigned Start, unsigned End,
bool WithSpaces) const {
unsigned CellWidth = 0;
for (auto i = Start; i < End; i++) {
if (Changes[i].NewlinesBefore > 0)
CellWidth = 0;
CellWidth += Changes[i].TokenLength;
CellWidth += (WithSpaces ? Changes[i].Spaces : 0);
}
return CellWidth;
}
void WhitespaceManager::alignToStartOfCell(unsigned Start, unsigned End) {
if ((End - Start) <= 1)
return;
for (auto i = Start + 1; i < End; i++)
if (Changes[i].NewlinesBefore > 0)
Changes[i].Spaces = Changes[Start].Spaces;
}
WhitespaceManager::CellDescriptions
WhitespaceManager::linkCells(CellDescriptions &&CellDesc) {
auto &Cells = CellDesc.Cells;
for (auto *CellIter = Cells.begin(); CellIter != Cells.end(); ++CellIter) {
if (!CellIter->NextColumnElement && (CellIter + 1) != Cells.end()) {
for (auto *NextIter = CellIter + 1; NextIter != Cells.end(); ++NextIter) {
if (NextIter->Cell == CellIter->Cell) {
CellIter->NextColumnElement = &(*NextIter);
break;
}
}
}
}
return std::move(CellDesc);
}
void WhitespaceManager::generateChanges() {
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
const Change &C = Changes[i];
if (i > 0) {
auto Last = Changes[i - 1].OriginalWhitespaceRange;
auto New = Changes[i].OriginalWhitespaceRange;
if (Last.getBegin() == New.getBegin() &&
(Last.getEnd() != Last.getBegin() ||
New.getEnd() == New.getBegin())) {
continue;
}
}
if (C.CreateReplacement) {
std::string ReplacementText = C.PreviousLinePostfix;
if (C.ContinuesPPDirective) {
appendEscapedNewlineText(ReplacementText, C.NewlinesBefore,
C.PreviousEndOfTokenColumn,
C.EscapedNewlineColumn);
} else {
appendNewlineText(ReplacementText, C.NewlinesBefore);
}
appendIndentText(
ReplacementText, C.Tok->IndentLevel, std::max(0, C.Spaces),
std::max((int)C.StartOfTokenColumn, C.Spaces) - std::max(0, C.Spaces),
C.IsAligned);
ReplacementText.append(C.CurrentLinePrefix);
storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
}
}
}
void WhitespaceManager::storeReplacement(SourceRange Range, StringRef Text) {
unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
SourceMgr.getFileOffset(Range.getBegin());
if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
WhitespaceLength) == Text) {
return;
}
auto Err = Replaces.add(tooling::Replacement(
SourceMgr, CharSourceRange::getCharRange(Range), Text));
if (Err) {
llvm::errs() << llvm::toString(std::move(Err)) << "\n";
assert(false);
}
}
void WhitespaceManager::appendNewlineText(std::string &Text,
unsigned Newlines) {
if (UseCRLF) {
Text.reserve(Text.size() + 2 * Newlines);
for (unsigned i = 0; i < Newlines; ++i)
Text.append("\r\n");
} else {
Text.append(Newlines, '\n');
}
}
void WhitespaceManager::appendEscapedNewlineText(
std::string &Text, unsigned Newlines, unsigned PreviousEndOfTokenColumn,
unsigned EscapedNewlineColumn) {
if (Newlines > 0) {
unsigned Spaces =
std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1);
for (unsigned i = 0; i < Newlines; ++i) {
Text.append(Spaces, ' ');
Text.append(UseCRLF ? "\\\r\n" : "\\\n");
Spaces = std::max<int>(0, EscapedNewlineColumn - 1);
}
}
}
void WhitespaceManager::appendIndentText(std::string &Text,
unsigned IndentLevel, unsigned Spaces,
unsigned WhitespaceStartColumn,
bool IsAligned) {
switch (Style.UseTab) {
case FormatStyle::UT_Never:
Text.append(Spaces, ' ');
break;
case FormatStyle::UT_Always: {
if (Style.TabWidth) {
unsigned FirstTabWidth =
Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
if (Spaces < FirstTabWidth || Spaces == 1) {
Text.append(Spaces, ' ');
break;
}
Spaces -= FirstTabWidth;
Text.append("\t");
Text.append(Spaces / Style.TabWidth, '\t');
Text.append(Spaces % Style.TabWidth, ' ');
} else if (Spaces == 1) {
Text.append(Spaces, ' ');
}
break;
}
case FormatStyle::UT_ForIndentation:
if (WhitespaceStartColumn == 0) {
unsigned Indentation = IndentLevel * Style.IndentWidth;
Spaces = appendTabIndent(Text, Spaces, Indentation);
}
Text.append(Spaces, ' ');
break;
case FormatStyle::UT_ForContinuationAndIndentation:
if (WhitespaceStartColumn == 0)
Spaces = appendTabIndent(Text, Spaces, Spaces);
Text.append(Spaces, ' ');
break;
case FormatStyle::UT_AlignWithSpaces:
if (WhitespaceStartColumn == 0) {
unsigned Indentation =
IsAligned ? IndentLevel * Style.IndentWidth : Spaces;
Spaces = appendTabIndent(Text, Spaces, Indentation);
}
Text.append(Spaces, ' ');
break;
}
}
unsigned WhitespaceManager::appendTabIndent(std::string &Text, unsigned Spaces,
unsigned Indentation) {
if (Indentation > Spaces)
Indentation = Spaces;
if (Style.TabWidth) {
unsigned Tabs = Indentation / Style.TabWidth;
Text.append(Tabs, '\t');
Spaces -= Tabs * Style.TabWidth;
}
return Spaces;
}
}
}