#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());
}
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)
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)
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)
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();
alignConsecutiveDeclarations();
alignConsecutiveBitFields();
alignConsecutiveAssignments();
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) {
SourceLocation OriginalWhitespaceStart =
Changes[i].OriginalWhitespaceRange.getBegin();
SourceLocation PreviousOriginalWhitespaceEnd =
Changes[i - 1].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) {
Changes[i - 1].TokenLength = OriginalWhitespaceStartOffset -
PreviousOriginalWhitespaceEndOffset +
Changes[i].PreviousLinePostfix.size() +
Changes[i - 1].CurrentLinePrefix.size();
} else {
Changes[i - 1].TokenLength =
NewlinePos + Changes[i - 1].CurrentLinePrefix.size();
}
if (Changes[i - 1].IsInsideToken && Changes[i - 1].NewlinesBefore == 0) {
LastOutsideTokenChange->TokenLength +=
Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
} else {
LastOutsideTokenChange = &Changes[i - 1];
}
Changes[i].PreviousEndOfTokenColumn =
Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
Changes[i - 1].IsTrailingComment =
(Changes[i].NewlinesBefore > 0 || Changes[i].Tok->is(tok::eof) ||
(Changes[i].IsInsideToken && Changes[i].Tok->is(tok::comment))) &&
Changes[i - 1].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) {
if (ScopeStack.size() != 0 &&
Changes[i].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 && Changes[i].indentAndNestingLevel() >
Changes[PreviousNonComment].indentAndNestingLevel()) {
ScopeStack.push_back(i);
}
bool InsideNestedScope = ScopeStack.size() != 0;
bool ContinuedStringLiteral = i > Start &&
Changes[i].Tok->is(tok::string_literal) &&
Changes[i - 1].Tok->is(tok::string_literal);
bool SkipMatchCheck = InsideNestedScope || ContinuedStringLiteral;
if (Changes[i].NewlinesBefore > 0 && !SkipMatchCheck) {
Shift = 0;
FoundMatchOnLine = false;
}
if (!FoundMatchOnLine && !SkipMatchCheck && Matches(Changes[i])) {
FoundMatchOnLine = true;
Shift = Column - (RightJustify ? Changes[i].TokenLength : 0) -
Changes[i].StartOfTokenColumn;
Changes[i].Spaces += Shift;
if (Changes[i].NewlinesBefore == 0) {
Changes[i].Spaces =
std::max(Changes[i].Spaces,
static_cast<int>(Changes[i].Tok->SpacesRequiredBefore));
}
}
if (InsideNestedScope && Changes[i].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->is(tok::identifier) &&
Changes[ScopeStart - 1].Tok->is(tok::l_paren) &&
Changes[ScopeStart].Tok->isNot(TT_LambdaLSquare)) {
if (Changes[i].Tok->MatchingParen &&
Changes[i].Tok->MatchingParen->is(TT_LambdaLBrace)) {
return false;
}
if (Changes[ScopeStart].NewlinesBefore > 0)
return false;
if (Changes[i].Tok->is(tok::l_brace) &&
Changes[i].Tok->is(BK_BracedInit)) {
return true;
}
return Style.BinPackArguments;
}
if (Changes[i].Tok->is(TT_ConditionalExpr))
return true;
if (Changes[i].Tok->is(TT_DesignatedInitializerPeriod))
return true;
if (Changes[i].Tok->Previous &&
Changes[i].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) &&
Changes[i].Tok->is(tok::l_brace) &&
Changes[i].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) &&
Changes[i].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;
}
return false;
};
if (ShouldShiftBeAdded())
Changes[i].Spaces += Shift;
}
if (ContinuedStringLiteral)
Changes[i].Spaces += Shift;
assert(Shift >= 0 || Changes[i].NewlinesBefore > 0 ||
Changes[i].Spaces >=
static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) ||
Changes[i].Tok->is(tok::eof));
Changes[i].StartOfTokenColumn += Shift;
if (i + 1 != Changes.size())
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
if (Style.PointerAlignment == FormatStyle::PAS_Right &&
Changes[i].Spaces != 0) {
for (int Previous = i - 1;
Previous >= 0 &&
Changes[Previous].Tok->getType() == TT_PointerOrReference;
--Previous) {
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) {
if (Changes[i].indentAndNestingLevel() < IndentAndNestingLevel)
break;
if (Changes[i].NewlinesBefore != 0) {
CommasBeforeMatch = 0;
EndOfSequence = i;
bool EmptyLineBreak =
(Changes[i].NewlinesBefore > 1) && !ACS.AcrossEmptyLines;
bool NoMatchBreak =
!FoundMatchOnLine && !(LineIsComment && ACS.AcrossComments);
if (EmptyLineBreak || NoMatchBreak)
AlignCurrentSequence();
if (i == 0 || !Changes[i].Tok->is(tok::string_literal) ||
!Changes[i - 1].Tok->is(tok::string_literal)) {
FoundMatchOnLine = false;
}
LineIsComment = true;
}
if (!Changes[i].Tok->is(tok::comment))
LineIsComment = false;
if (Changes[i].Tok->is(tok::comma)) {
++CommasBeforeMatch;
} else if (Changes[i].indentAndNestingLevel() > IndentAndNestingLevel) {
unsigned StoppedAt = AlignTokens(Style, Matches, Changes, i, ACS);
i = StoppedAt - 1;
continue;
}
if (!Matches(Changes[i]))
continue;
if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch)
AlignCurrentSequence();
CommasBeforeLastMatch = CommasBeforeMatch;
FoundMatchOnLine = true;
if (StartOfSequence == 0)
StartOfSequence = i;
unsigned ChangeWidthLeft = Changes[i].StartOfTokenColumn;
unsigned ChangeWidthAnchor = 0;
unsigned ChangeWidthRight = 0;
if (RightJustify)
if (ACS.PadOperators)
ChangeWidthAnchor = Changes[i].TokenLength;
else
ChangeWidthLeft += Changes[i].TokenLength;
else
ChangeWidthRight = Changes[i].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 AlignMacroSequence(
unsigned &StartOfSequence, unsigned &EndOfSequence, unsigned &MinColumn,
unsigned &MaxColumn, bool &FoundMatchOnLine,
std::function<bool(const WhitespaceManager::Change &C)> AlignMacrosMatches,
SmallVector<WhitespaceManager::Change, 16> &Changes) {
if (StartOfSequence > 0 && StartOfSequence < EndOfSequence) {
FoundMatchOnLine = false;
int Shift = 0;
for (unsigned I = StartOfSequence; I != EndOfSequence; ++I) {
if (Changes[I].NewlinesBefore > 0) {
Shift = 0;
FoundMatchOnLine = false;
}
if (!FoundMatchOnLine && AlignMacrosMatches(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;
MaxColumn = UINT_MAX;
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->is(tok::identifier))
return false;
if (!Current->Previous || !Current->Previous->is(tok::pp_define))
return false;
return Current->Next->SpacesRequiredBefore == SpacesRequiredBefore;
};
unsigned MinColumn = 0;
unsigned MaxColumn = UINT_MAX;
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) {
AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
FoundMatchOnLine, AlignMacrosMatches, Changes);
}
FoundMatchOnLine = false;
LineIsComment = true;
}
if (!Changes[I].Tok->is(tok::comment))
LineIsComment = false;
if (!AlignMacrosMatches(Changes[I]))
continue;
FoundMatchOnLine = true;
if (StartOfSequence == 0)
StartOfSequence = I;
unsigned ChangeMinColumn = Changes[I].StartOfTokenColumn;
int LineLengthAfter = -Changes[I].Spaces;
for (unsigned j = I; j != E && Changes[j].NewlinesBefore == 0; ++j)
LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
MinColumn = std::max(MinColumn, ChangeMinColumn);
MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
}
EndOfSequence = I;
AlignMacroSequence(StartOfSequence, EndOfSequence, MinColumn, MaxColumn,
FoundMatchOnLine, 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);
},
Changes, 0, Style.AlignConsecutiveAssignments,
true);
}
void WhitespaceManager::alignConsecutiveBitFields() {
if (!Style.AlignConsecutiveBitFields.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(TT_BitFieldColon);
},
Changes, 0, Style.AlignConsecutiveBitFields);
}
void WhitespaceManager::alignConsecutiveDeclarations() {
if (!Style.AlignConsecutiveDeclarations.Enabled)
return;
AlignTokens(
Style,
[](Change const &C) {
if (C.Tok->isOneOf(TT_FunctionDeclarationName, tok::kw_operator))
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() {
unsigned MinColumn = 0;
unsigned MaxColumn = UINT_MAX;
unsigned StartOfSequence = 0;
bool BreakBeforeNext = false;
unsigned Newlines = 0;
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
if (Changes[i].StartOfBlockComment)
continue;
Newlines += Changes[i].NewlinesBefore;
if (!Changes[i].IsTrailingComment)
continue;
unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
unsigned ChangeMaxColumn;
if (Style.ColumnLimit == 0)
ChangeMaxColumn = UINT_MAX;
else if (Style.ColumnLimit >= Changes[i].TokenLength)
ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
else
ChangeMaxColumn = ChangeMinColumn;
if (!Changes[i].CreateReplacement)
ChangeMaxColumn = ChangeMinColumn;
if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
ChangeMaxColumn -= 2;
bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
Changes[i - 1].Tok->is(tok::r_brace) &&
Changes[i - 1].StartOfTokenColumn == 0;
bool WasAlignedWithStartOfNextLine = false;
if (Changes[i].NewlinesBefore == 1) { unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
Changes[i].OriginalWhitespaceRange.getEnd());
for (unsigned j = i + 1; j != e; ++j) {
if (Changes[j].Tok->is(tok::comment))
continue;
unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
Changes[j].OriginalWhitespaceRange.getEnd());
WasAlignedWithStartOfNextLine =
CommentColumn == NextColumn ||
CommentColumn == NextColumn + Style.IndentWidth;
break;
}
}
if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
alignTrailingComments(StartOfSequence, i, MinColumn);
MinColumn = ChangeMinColumn;
MaxColumn = ChangeMinColumn;
StartOfSequence = i;
} else if (BreakBeforeNext || Newlines > 1 ||
(ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
(Changes[i].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) || (Changes[i].NewlinesBefore > 1) ||
(Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
Newlines = 0;
}
alignTrailingComments(StartOfSequence, Changes.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() {
if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
return;
bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
unsigned StartOfMacro = 0;
for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
Change &C = Changes[i];
if (C.NewlinesBefore > 0) {
if (C.ContinuesPPDirective) {
MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
} else {
alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
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;
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 = 0;
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 != nullptr;
Next = Next->NextColumnElement) {
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 : 0;
}
alignToStartOfCell(CellIter->Index, CellIter->EndIndex);
for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
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 : 0;
}
alignToStartOfCell(Next->Index, Next->EndIndex);
}
}
}
}
void WhitespaceManager::alignArrayInitializersLeftJustified(
CellDescriptions &&CellDescs) {
if (!CellDescs.isRectangular())
return;
auto &Cells = CellDescs.Cells;
auto *CellIter = Cells.begin();
if (Changes[CellIter->Index].NewlinesBefore == 0)
Changes[CellIter->Index].Spaces = 0;
else
Changes[CellIter->Index].Spaces = 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 : 0);
}
auto RowCount = 1U;
auto Offset = std::distance(Cells.begin(), CellIter);
for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
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 : 0);
}
++RowCount;
}
}
}
bool WhitespaceManager::isSplitCell(const CellDescription &Cell) {
if (Cell.HasSplit)
return true;
for (const auto *Next = Cell.NextColumnElement; Next != nullptr;
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 (C.Tok->getNextNonComment()->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->is(tok::comma))
NextNonComment = NextNonComment->getNextNonComment();
auto j = i;
while (Changes[j].Tok != NextNonComment && j < End)
++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.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 == nullptr &&
((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 && Changes[i - 1].OriginalWhitespaceRange.getBegin() ==
C.OriginalWhitespaceRange.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;
}
} }