2013-04-15 22:28:00 +08:00
|
|
|
//===--- WhitespaceManager.cpp - Format C++ code --------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file implements WhitespaceManager class.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "WhitespaceManager.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2014-05-09 21:11:16 +08:00
|
|
|
bool WhitespaceManager::Change::IsBeforeInFile::
|
|
|
|
operator()(const Change &C1, const Change &C2) const {
|
2013-05-22 20:51:29 +08:00
|
|
|
return SourceMgr.isBeforeInTranslationUnit(
|
|
|
|
C1.OriginalWhitespaceRange.getBegin(),
|
|
|
|
C2.OriginalWhitespaceRange.getBegin());
|
|
|
|
}
|
2013-04-25 16:56:26 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
WhitespaceManager::Change::Change(
|
2015-10-04 12:53:55 +08:00
|
|
|
bool CreateReplacement, SourceRange OriginalWhitespaceRange,
|
2014-04-18 00:12:46 +08:00
|
|
|
unsigned IndentLevel, int Spaces, unsigned StartOfTokenColumn,
|
2013-09-28 00:14:22 +08:00
|
|
|
unsigned NewlinesBefore, StringRef PreviousLinePostfix,
|
2015-10-01 18:06:54 +08:00
|
|
|
StringRef CurrentLinePrefix, tok::TokenKind Kind, bool ContinuesPPDirective,
|
2016-01-12 00:27:16 +08:00
|
|
|
bool IsStartOfDeclName, bool IsInsideToken)
|
2013-05-22 20:51:29 +08:00
|
|
|
: CreateReplacement(CreateReplacement),
|
|
|
|
OriginalWhitespaceRange(OriginalWhitespaceRange),
|
|
|
|
StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
|
|
|
|
PreviousLinePostfix(PreviousLinePostfix),
|
|
|
|
CurrentLinePrefix(CurrentLinePrefix), Kind(Kind),
|
2015-10-01 18:06:54 +08:00
|
|
|
ContinuesPPDirective(ContinuesPPDirective),
|
|
|
|
IsStartOfDeclName(IsStartOfDeclName), IndentLevel(IndentLevel),
|
2016-01-12 00:27:16 +08:00
|
|
|
Spaces(Spaces), IsInsideToken(IsInsideToken), IsTrailingComment(false),
|
|
|
|
TokenLength(0), PreviousEndOfTokenColumn(0), EscapedNewlineColumn(0),
|
2015-03-03 22:21:48 +08:00
|
|
|
StartOfBlockComment(nullptr), IndentationOffset(0) {}
|
2013-05-22 20:51:29 +08:00
|
|
|
|
2013-10-12 05:25:45 +08:00
|
|
|
void WhitespaceManager::reset() {
|
|
|
|
Changes.clear();
|
|
|
|
Replaces.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines,
|
2013-09-28 00:14:22 +08:00
|
|
|
unsigned IndentLevel, unsigned Spaces,
|
2013-05-22 20:51:29 +08:00
|
|
|
unsigned StartOfTokenColumn,
|
|
|
|
bool InPPDirective) {
|
2013-10-12 05:25:45 +08:00
|
|
|
if (Tok.Finalized)
|
|
|
|
return;
|
|
|
|
Tok.Decision = (Newlines > 0) ? FD_Break : FD_Continue;
|
2015-10-01 18:06:54 +08:00
|
|
|
Changes.push_back(
|
2016-01-09 23:56:53 +08:00
|
|
|
Change(/*CreateReplacement=*/true, Tok.WhitespaceRange, IndentLevel,
|
|
|
|
Spaces, StartOfTokenColumn, Newlines, "", "", Tok.Tok.getKind(),
|
|
|
|
InPPDirective && !Tok.IsFirst,
|
2016-01-12 00:27:16 +08:00
|
|
|
Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName),
|
|
|
|
/*IsInsideToken=*/false));
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
void WhitespaceManager::addUntouchableToken(const FormatToken &Tok,
|
|
|
|
bool InPPDirective) {
|
2013-10-12 05:25:45 +08:00
|
|
|
if (Tok.Finalized)
|
|
|
|
return;
|
2016-01-09 23:56:53 +08:00
|
|
|
Changes.push_back(Change(
|
|
|
|
/*CreateReplacement=*/false, Tok.WhitespaceRange, /*IndentLevel=*/0,
|
|
|
|
/*Spaces=*/0, Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
|
|
|
|
Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst,
|
2016-01-12 00:27:16 +08:00
|
|
|
Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName),
|
|
|
|
/*IsInsideToken=*/false));
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-12 00:01:49 +08:00
|
|
|
void WhitespaceManager::replaceWhitespaceInToken(
|
|
|
|
const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
|
|
|
|
StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
|
2014-04-18 00:12:46 +08:00
|
|
|
unsigned Newlines, unsigned IndentLevel, int Spaces) {
|
2013-10-12 05:25:45 +08:00
|
|
|
if (Tok.Finalized)
|
|
|
|
return;
|
2014-04-18 00:12:46 +08:00
|
|
|
SourceLocation Start = Tok.getStartOfNonWhitespace().getLocWithOffset(Offset);
|
2013-05-22 20:51:29 +08:00
|
|
|
Changes.push_back(Change(
|
2014-04-18 00:12:46 +08:00
|
|
|
true, SourceRange(Start, Start.getLocWithOffset(ReplaceChars)),
|
|
|
|
IndentLevel, Spaces, std::max(0, Spaces), Newlines, PreviousPostfix,
|
2016-01-12 00:27:16 +08:00
|
|
|
CurrentPrefix, Tok.is(TT_LineComment) ? tok::comment : tok::unknown,
|
2015-10-01 18:06:54 +08:00
|
|
|
InPPDirective && !Tok.IsFirst,
|
2016-01-12 00:27:16 +08:00
|
|
|
Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName),
|
|
|
|
/*IsInsideToken=*/Newlines == 0));
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
const tooling::Replacements &WhitespaceManager::generateReplacements() {
|
2013-05-22 20:51:29 +08:00
|
|
|
if (Changes.empty())
|
|
|
|
return Replaces;
|
|
|
|
|
|
|
|
std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr));
|
|
|
|
calculateLineBreakInformation();
|
2015-10-01 18:06:54 +08:00
|
|
|
alignConsecutiveDeclarations();
|
2015-04-29 21:06:49 +08:00
|
|
|
alignConsecutiveAssignments();
|
2013-05-22 20:51:29 +08:00
|
|
|
alignTrailingComments();
|
2013-04-25 16:56:26 +08:00
|
|
|
alignEscapedNewlines();
|
2013-05-22 20:51:29 +08:00
|
|
|
generateChanges();
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
return Replaces;
|
|
|
|
}
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
void WhitespaceManager::calculateLineBreakInformation() {
|
|
|
|
Changes[0].PreviousEndOfTokenColumn = 0;
|
2016-01-12 00:27:16 +08:00
|
|
|
Change *LastOutsideTokenChange = &Changes[0];
|
2013-05-22 20:51:29 +08:00
|
|
|
for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
|
|
|
|
unsigned OriginalWhitespaceStart =
|
|
|
|
SourceMgr.getFileOffset(Changes[i].OriginalWhitespaceRange.getBegin());
|
|
|
|
unsigned PreviousOriginalWhitespaceEnd = SourceMgr.getFileOffset(
|
|
|
|
Changes[i - 1].OriginalWhitespaceRange.getEnd());
|
2013-07-08 22:34:09 +08:00
|
|
|
Changes[i - 1].TokenLength = OriginalWhitespaceStart -
|
|
|
|
PreviousOriginalWhitespaceEnd +
|
|
|
|
Changes[i].PreviousLinePostfix.size() +
|
|
|
|
Changes[i - 1].CurrentLinePrefix.size();
|
2013-05-22 20:51:29 +08:00
|
|
|
|
2016-01-12 00:27:16 +08:00
|
|
|
// If there are multiple changes in this token, sum up all the changes until
|
|
|
|
// the end of the line.
|
|
|
|
if (Changes[i - 1].IsInsideToken)
|
|
|
|
LastOutsideTokenChange->TokenLength +=
|
|
|
|
Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
|
|
|
|
else
|
|
|
|
LastOutsideTokenChange = &Changes[i - 1];
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
Changes[i].PreviousEndOfTokenColumn =
|
|
|
|
Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
|
|
|
|
|
|
|
|
Changes[i - 1].IsTrailingComment =
|
2016-01-12 00:27:16 +08:00
|
|
|
(Changes[i].NewlinesBefore > 0 || Changes[i].Kind == tok::eof ||
|
|
|
|
(Changes[i].IsInsideToken && Changes[i].Kind == tok::comment)) &&
|
2013-05-22 20:51:29 +08:00
|
|
|
Changes[i - 1].Kind == tok::comment;
|
|
|
|
}
|
2013-05-22 22:01:08 +08:00
|
|
|
// FIXME: The last token is currently not always an eof token; in those
|
|
|
|
// cases, setting TokenLength of the last token to 0 is wrong.
|
|
|
|
Changes.back().TokenLength = 0;
|
2013-05-22 20:51:29 +08:00
|
|
|
Changes.back().IsTrailingComment = Changes.back().Kind == tok::comment;
|
2014-04-18 00:12:46 +08:00
|
|
|
|
|
|
|
const WhitespaceManager::Change *LastBlockComment = nullptr;
|
|
|
|
for (auto &Change : Changes) {
|
2016-01-12 00:27:16 +08:00
|
|
|
// Reset the IsTrailingComment flag for changes inside of trailing comments
|
|
|
|
// so they don't get realigned later.
|
|
|
|
if (Change.IsInsideToken)
|
|
|
|
Change.IsTrailingComment = false;
|
2014-04-18 00:12:46 +08:00
|
|
|
Change.StartOfBlockComment = nullptr;
|
|
|
|
Change.IndentationOffset = 0;
|
|
|
|
if (Change.Kind == tok::comment) {
|
|
|
|
LastBlockComment = &Change;
|
|
|
|
} else if (Change.Kind == tok::unknown) {
|
|
|
|
if ((Change.StartOfBlockComment = LastBlockComment))
|
|
|
|
Change.IndentationOffset =
|
|
|
|
Change.StartOfTokenColumn -
|
|
|
|
Change.StartOfBlockComment->StartOfTokenColumn;
|
|
|
|
} else {
|
|
|
|
LastBlockComment = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
// Align a single sequence of tokens, see AlignTokens below.
|
|
|
|
template <typename F>
|
|
|
|
static void
|
|
|
|
AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches,
|
|
|
|
SmallVector<WhitespaceManager::Change, 16> &Changes) {
|
|
|
|
bool FoundMatchOnLine = false;
|
2015-09-22 17:32:00 +08:00
|
|
|
int Shift = 0;
|
2015-04-29 21:06:49 +08:00
|
|
|
for (unsigned i = Start; i != End; ++i) {
|
2015-09-22 17:32:00 +08:00
|
|
|
if (Changes[i].NewlinesBefore > 0) {
|
2015-12-01 20:00:43 +08:00
|
|
|
FoundMatchOnLine = false;
|
2015-09-22 17:32:00 +08:00
|
|
|
Shift = 0;
|
|
|
|
}
|
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
// If this is the first matching token to be aligned, remember by how many
|
2015-09-22 17:32:00 +08:00
|
|
|
// spaces it has to be shifted, so the rest of the changes on the line are
|
|
|
|
// shifted by the same amount
|
2015-12-01 20:00:43 +08:00
|
|
|
if (!FoundMatchOnLine && Matches(Changes[i])) {
|
|
|
|
FoundMatchOnLine = true;
|
2015-04-29 21:06:49 +08:00
|
|
|
Shift = Column - Changes[i].StartOfTokenColumn;
|
2015-09-22 17:32:00 +08:00
|
|
|
Changes[i].Spaces += Shift;
|
2015-04-29 21:06:49 +08:00
|
|
|
}
|
2015-09-22 17:32:00 +08:00
|
|
|
|
2015-04-29 21:06:49 +08:00
|
|
|
assert(Shift >= 0);
|
2015-09-22 17:32:00 +08:00
|
|
|
Changes[i].StartOfTokenColumn += Shift;
|
2015-04-29 21:06:49 +08:00
|
|
|
if (i + 1 != Changes.size())
|
|
|
|
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
// Walk through all of the changes and find sequences of matching tokens to
|
|
|
|
// align. To do so, keep track of the lines and whether or not a matching token
|
|
|
|
// was found on a line. If a matching token is found, extend the current
|
|
|
|
// sequence. If the current line cannot be part of a sequence, e.g. because
|
|
|
|
// there is an empty line before it or it contains only non-matching tokens,
|
|
|
|
// finalize the previous sequence.
|
|
|
|
template <typename F>
|
|
|
|
static void AlignTokens(const FormatStyle &Style, F &&Matches,
|
|
|
|
SmallVector<WhitespaceManager::Change, 16> &Changes) {
|
2015-10-01 18:06:54 +08:00
|
|
|
unsigned MinColumn = 0;
|
|
|
|
unsigned MaxColumn = UINT_MAX;
|
2015-12-01 20:00:43 +08:00
|
|
|
|
|
|
|
// Line number of the start and the end of the current token sequence.
|
2015-10-01 18:06:54 +08:00
|
|
|
unsigned StartOfSequence = 0;
|
|
|
|
unsigned EndOfSequence = 0;
|
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
// Keep track of the nesting level of matching tokens, i.e. the number of
|
|
|
|
// surrounding (), [], or {}. We will only align a sequence of matching
|
|
|
|
// token that share the same scope depth.
|
|
|
|
//
|
|
|
|
// FIXME: This could use FormatToken::NestingLevel information, but there is
|
|
|
|
// an outstanding issue wrt the brace scopes.
|
|
|
|
unsigned NestingLevelOfLastMatch = 0;
|
|
|
|
unsigned NestingLevel = 0;
|
|
|
|
|
|
|
|
// Keep track of the number of commas before the matching tokens, we will only
|
|
|
|
// align a sequence of matching tokens if they are preceded by the same number
|
|
|
|
// of commas.
|
|
|
|
unsigned CommasBeforeLastMatch = 0;
|
|
|
|
unsigned CommasBeforeMatch = 0;
|
|
|
|
|
|
|
|
// Whether a matching token has been found on the current line.
|
|
|
|
bool FoundMatchOnLine = false;
|
|
|
|
|
|
|
|
// Aligns a sequence of matching tokens, on the MinColumn column.
|
|
|
|
//
|
|
|
|
// Sequences start from the first matching token to align, and end at the
|
|
|
|
// first token of the first line that doesn't need to be aligned.
|
|
|
|
//
|
|
|
|
// We need to adjust the StartOfTokenColumn of each Change that is on a line
|
|
|
|
// containing any matching token to be aligned and located after such token.
|
|
|
|
auto AlignCurrentSequence = [&] {
|
2015-10-01 18:06:54 +08:00
|
|
|
if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
|
2015-12-01 20:00:43 +08:00
|
|
|
AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
|
|
|
|
Changes);
|
2015-10-01 18:06:54 +08:00
|
|
|
MinColumn = 0;
|
|
|
|
MaxColumn = UINT_MAX;
|
|
|
|
StartOfSequence = 0;
|
|
|
|
EndOfSequence = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
|
|
|
|
if (Changes[i].NewlinesBefore != 0) {
|
2015-12-01 20:00:43 +08:00
|
|
|
CommasBeforeMatch = 0;
|
2015-10-01 18:06:54 +08:00
|
|
|
EndOfSequence = i;
|
2015-12-01 20:00:43 +08:00
|
|
|
// If there is a blank line, or if the last line didn't contain any
|
|
|
|
// matching token, the sequence ends here.
|
|
|
|
if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine)
|
|
|
|
AlignCurrentSequence();
|
|
|
|
|
|
|
|
FoundMatchOnLine = false;
|
2015-10-01 18:06:54 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
if (Changes[i].Kind == tok::comma) {
|
|
|
|
++CommasBeforeMatch;
|
|
|
|
} else if (Changes[i].Kind == tok::r_brace ||
|
|
|
|
Changes[i].Kind == tok::r_paren ||
|
|
|
|
Changes[i].Kind == tok::r_square) {
|
|
|
|
--NestingLevel;
|
|
|
|
} else if (Changes[i].Kind == tok::l_brace ||
|
|
|
|
Changes[i].Kind == tok::l_paren ||
|
|
|
|
Changes[i].Kind == tok::l_square) {
|
|
|
|
// We want sequences to skip over child scopes if possible, but not the
|
|
|
|
// other way around.
|
|
|
|
NestingLevelOfLastMatch = std::min(NestingLevelOfLastMatch, NestingLevel);
|
|
|
|
++NestingLevel;
|
2015-10-01 18:06:54 +08:00
|
|
|
}
|
2015-12-01 20:00:43 +08:00
|
|
|
|
|
|
|
if (!Matches(Changes[i]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If there is more than one matching token per line, or if the number of
|
|
|
|
// preceding commas, or the scope depth, do not match anymore, end the
|
|
|
|
// sequence.
|
|
|
|
if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch ||
|
|
|
|
NestingLevel != NestingLevelOfLastMatch)
|
|
|
|
AlignCurrentSequence();
|
|
|
|
|
|
|
|
CommasBeforeLastMatch = CommasBeforeMatch;
|
|
|
|
NestingLevelOfLastMatch = NestingLevel;
|
|
|
|
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;
|
|
|
|
|
|
|
|
// If we are restricted by the maximum column width, end the sequence.
|
|
|
|
if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
|
|
|
|
CommasBeforeLastMatch != CommasBeforeMatch) {
|
|
|
|
AlignCurrentSequence();
|
|
|
|
StartOfSequence = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
MinColumn = std::max(MinColumn, ChangeMinColumn);
|
|
|
|
MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
|
2015-10-01 18:06:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EndOfSequence = Changes.size();
|
2015-12-01 20:00:43 +08:00
|
|
|
AlignCurrentSequence();
|
2015-10-01 18:06:54 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
void WhitespaceManager::alignConsecutiveAssignments() {
|
|
|
|
if (!Style.AlignConsecutiveAssignments)
|
|
|
|
return;
|
2015-10-01 18:06:54 +08:00
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
AlignTokens(Style,
|
|
|
|
[&](const Change &C) {
|
|
|
|
// Do not align on equal signs that are first on a line.
|
|
|
|
if (C.NewlinesBefore > 0)
|
|
|
|
return false;
|
2015-10-01 18:06:54 +08:00
|
|
|
|
2015-12-01 20:00:43 +08:00
|
|
|
// Do not align on equal signs that are last on a line.
|
|
|
|
if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return C.Kind == tok::equal;
|
|
|
|
},
|
|
|
|
Changes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WhitespaceManager::alignConsecutiveDeclarations() {
|
|
|
|
if (!Style.AlignConsecutiveDeclarations)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// FIXME: Currently we don't handle properly the PointerAlignment: Right
|
|
|
|
// The * and & are not aligned and are left dangling. Something has to be done
|
|
|
|
// about it, but it raises the question of alignment of code like:
|
|
|
|
// const char* const* v1;
|
|
|
|
// float const* v2;
|
|
|
|
// SomeVeryLongType const& v3;
|
|
|
|
|
|
|
|
AlignTokens(Style, [](Change const &C) { return C.IsStartOfDeclName; },
|
|
|
|
Changes);
|
2015-10-01 18:06:54 +08:00
|
|
|
}
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
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) {
|
2014-04-18 00:12:46 +08:00
|
|
|
if (Changes[i].StartOfBlockComment)
|
|
|
|
continue;
|
|
|
|
Newlines += Changes[i].NewlinesBefore;
|
|
|
|
if (!Changes[i].IsTrailingComment)
|
|
|
|
continue;
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
|
|
|
|
unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
|
2016-01-09 23:56:53 +08:00
|
|
|
|
|
|
|
// If we don't create a replacement for this change, we have to consider
|
|
|
|
// it to be immovable.
|
|
|
|
if (!Changes[i].CreateReplacement)
|
|
|
|
ChangeMaxColumn = ChangeMinColumn;
|
|
|
|
|
2014-04-27 18:03:19 +08:00
|
|
|
if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
|
|
|
|
ChangeMaxColumn -= 2;
|
2014-04-18 00:12:46 +08:00
|
|
|
// If this comment follows an } in column 0, it probably documents the
|
|
|
|
// closing of a namespace and we don't want to align it.
|
|
|
|
bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
|
|
|
|
Changes[i - 1].Kind == tok::r_brace &&
|
|
|
|
Changes[i - 1].StartOfTokenColumn == 0;
|
|
|
|
bool WasAlignedWithStartOfNextLine = false;
|
|
|
|
if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
|
2015-01-07 22:00:11 +08:00
|
|
|
unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
|
|
|
|
Changes[i].OriginalWhitespaceRange.getEnd());
|
2014-04-18 00:12:46 +08:00
|
|
|
for (unsigned j = i + 1; j != e; ++j) {
|
2016-02-01 19:20:55 +08:00
|
|
|
if (Changes[j].Kind == tok::comment ||
|
|
|
|
Changes[j].Kind == tok::unknown)
|
|
|
|
// Skip over comments and unknown tokens. "unknown tokens are used for
|
|
|
|
// the continuation of multiline comments.
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
|
|
|
|
Changes[j].OriginalWhitespaceRange.getEnd());
|
|
|
|
// The start of the next token was previously aligned with the
|
|
|
|
// start of this comment.
|
|
|
|
WasAlignedWithStartOfNextLine =
|
|
|
|
CommentColumn == NextColumn ||
|
|
|
|
CommentColumn == NextColumn + Style.IndentWidth;
|
|
|
|
break;
|
2013-11-09 07:31:14 +08:00
|
|
|
}
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
2014-04-18 00:12:46 +08:00
|
|
|
if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
|
|
|
|
alignTrailingComments(StartOfSequence, i, MinColumn);
|
|
|
|
MinColumn = ChangeMinColumn;
|
|
|
|
MaxColumn = ChangeMinColumn;
|
|
|
|
StartOfSequence = i;
|
|
|
|
} else if (BreakBeforeNext || Newlines > 1 ||
|
|
|
|
(ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
|
|
|
|
// Break the comment sequence if the previous line did not end
|
|
|
|
// in a trailing comment.
|
|
|
|
(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) ||
|
|
|
|
// Never start a sequence with a comment at the beginning of
|
|
|
|
// the line.
|
|
|
|
(Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
|
|
|
|
Newlines = 0;
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
|
|
|
alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
|
|
|
|
unsigned Column) {
|
|
|
|
for (unsigned i = Start; i != End; ++i) {
|
2014-04-18 00:12:46 +08:00
|
|
|
int Shift = 0;
|
2013-05-22 20:51:29 +08:00
|
|
|
if (Changes[i].IsTrailingComment) {
|
2014-04-18 00:12:46 +08:00
|
|
|
Shift = Column - Changes[i].StartOfTokenColumn;
|
|
|
|
}
|
|
|
|
if (Changes[i].StartOfBlockComment) {
|
|
|
|
Shift = Changes[i].IndentationOffset +
|
|
|
|
Changes[i].StartOfBlockComment->StartOfTokenColumn -
|
|
|
|
Changes[i].StartOfTokenColumn;
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
2014-04-18 00:12:46 +08:00
|
|
|
assert(Shift >= 0);
|
|
|
|
Changes[i].Spaces += Shift;
|
|
|
|
if (i + 1 != End)
|
|
|
|
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
|
|
|
|
Changes[i].StartOfTokenColumn += Shift;
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WhitespaceManager::alignEscapedNewlines() {
|
2013-08-28 17:07:32 +08:00
|
|
|
unsigned MaxEndOfLine =
|
|
|
|
Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
|
2013-05-22 20:51:29 +08:00
|
|
|
unsigned StartOfMacro = 0;
|
|
|
|
for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
|
|
|
|
Change &C = Changes[i];
|
|
|
|
if (C.NewlinesBefore > 0) {
|
|
|
|
if (C.ContinuesPPDirective) {
|
2013-08-28 17:07:32 +08:00
|
|
|
MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
|
2013-05-22 20:51:29 +08:00
|
|
|
} else {
|
|
|
|
alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
|
2013-08-28 17:07:32 +08:00
|
|
|
MaxEndOfLine = Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
|
2013-05-22 20:51:29 +08:00
|
|
|
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::generateChanges() {
|
|
|
|
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
|
|
|
|
const Change &C = Changes[i];
|
2015-01-29 18:47:14 +08:00
|
|
|
if (i > 0) {
|
|
|
|
assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() !=
|
|
|
|
C.OriginalWhitespaceRange.getBegin() &&
|
|
|
|
"Generating two replacements for the same location");
|
|
|
|
}
|
2013-05-22 20:51:29 +08:00
|
|
|
if (C.CreateReplacement) {
|
2013-09-11 20:25:57 +08:00
|
|
|
std::string ReplacementText = C.PreviousLinePostfix;
|
|
|
|
if (C.ContinuesPPDirective)
|
|
|
|
appendNewlineText(ReplacementText, C.NewlinesBefore,
|
|
|
|
C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn);
|
|
|
|
else
|
|
|
|
appendNewlineText(ReplacementText, C.NewlinesBefore);
|
2014-04-18 00:12:46 +08:00
|
|
|
appendIndentText(ReplacementText, C.IndentLevel, std::max(0, C.Spaces),
|
|
|
|
C.StartOfTokenColumn - std::max(0, C.Spaces));
|
2013-09-11 20:25:57 +08:00
|
|
|
ReplacementText.append(C.CurrentLinePrefix);
|
2013-05-22 20:51:29 +08:00
|
|
|
storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 12:53:55 +08:00
|
|
|
void WhitespaceManager::storeReplacement(SourceRange Range,
|
2013-05-22 20:51:29 +08:00
|
|
|
StringRef Text) {
|
|
|
|
unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
|
|
|
|
SourceMgr.getFileOffset(Range.getBegin());
|
|
|
|
// Don't create a replacement, if it does not change anything.
|
|
|
|
if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
|
2013-07-08 22:34:09 +08:00
|
|
|
WhitespaceLength) == Text)
|
2013-05-22 20:51:29 +08:00
|
|
|
return;
|
|
|
|
Replaces.insert(tooling::Replacement(
|
|
|
|
SourceMgr, CharSourceRange::getCharRange(Range), Text));
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2013-09-11 20:25:57 +08:00
|
|
|
void WhitespaceManager::appendNewlineText(std::string &Text,
|
|
|
|
unsigned Newlines) {
|
|
|
|
for (unsigned i = 0; i < Newlines; ++i)
|
|
|
|
Text.append(UseCRLF ? "\r\n" : "\n");
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2013-09-11 20:25:57 +08:00
|
|
|
void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines,
|
|
|
|
unsigned PreviousEndOfTokenColumn,
|
|
|
|
unsigned EscapedNewlineColumn) {
|
2013-06-12 00:01:49 +08:00
|
|
|
if (Newlines > 0) {
|
2013-04-15 22:28:00 +08:00
|
|
|
unsigned Offset =
|
2013-05-22 20:51:29 +08:00
|
|
|
std::min<int>(EscapedNewlineColumn - 1, PreviousEndOfTokenColumn);
|
2013-06-12 00:01:49 +08:00
|
|
|
for (unsigned i = 0; i < Newlines; ++i) {
|
2015-05-29 03:55:49 +08:00
|
|
|
Text.append(EscapedNewlineColumn - Offset - 1, ' ');
|
2013-09-11 20:25:57 +08:00
|
|
|
Text.append(UseCRLF ? "\\\r\n" : "\\\n");
|
2013-04-15 22:28:00 +08:00
|
|
|
Offset = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-05-13 17:22:11 +08:00
|
|
|
}
|
|
|
|
|
2013-09-28 00:14:22 +08:00
|
|
|
void WhitespaceManager::appendIndentText(std::string &Text,
|
|
|
|
unsigned IndentLevel, unsigned Spaces,
|
2013-09-27 17:45:40 +08:00
|
|
|
unsigned WhitespaceStartColumn) {
|
2013-09-28 00:14:22 +08:00
|
|
|
switch (Style.UseTab) {
|
|
|
|
case FormatStyle::UT_Never:
|
2015-05-29 03:55:49 +08:00
|
|
|
Text.append(Spaces, ' ');
|
2013-09-28 00:14:22 +08:00
|
|
|
break;
|
|
|
|
case FormatStyle::UT_Always: {
|
2013-09-27 17:45:40 +08:00
|
|
|
unsigned FirstTabWidth =
|
|
|
|
Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
|
|
|
|
// Indent with tabs only when there's at least one full tab.
|
|
|
|
if (FirstTabWidth + Style.TabWidth <= Spaces) {
|
|
|
|
Spaces -= FirstTabWidth;
|
|
|
|
Text.append("\t");
|
|
|
|
}
|
2015-05-29 03:55:49 +08:00
|
|
|
Text.append(Spaces / Style.TabWidth, '\t');
|
|
|
|
Text.append(Spaces % Style.TabWidth, ' ');
|
2013-09-28 00:14:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FormatStyle::UT_ForIndentation:
|
|
|
|
if (WhitespaceStartColumn == 0) {
|
|
|
|
unsigned Indentation = IndentLevel * Style.IndentWidth;
|
2013-09-28 00:40:11 +08:00
|
|
|
// This happens, e.g. when a line in a block comment is indented less than
|
|
|
|
// the first one.
|
2013-09-28 00:14:22 +08:00
|
|
|
if (Indentation > Spaces)
|
|
|
|
Indentation = Spaces;
|
|
|
|
unsigned Tabs = Indentation / Style.TabWidth;
|
2015-05-29 03:55:49 +08:00
|
|
|
Text.append(Tabs, '\t');
|
2013-09-28 00:14:22 +08:00
|
|
|
Spaces -= Tabs * Style.TabWidth;
|
|
|
|
}
|
2015-05-29 03:55:49 +08:00
|
|
|
Text.append(Spaces, ' ');
|
2013-09-28 00:14:22 +08:00
|
|
|
break;
|
2013-09-11 20:25:57 +08:00
|
|
|
}
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|