2013-04-15 22:28:00 +08:00
|
|
|
//===--- BreakableToken.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 Contains implementation of BreakableToken class and classes derived
|
|
|
|
/// from it.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "BreakableToken.h"
|
2017-01-25 21:58:58 +08:00
|
|
|
#include "ContinuationIndenter.h"
|
2013-06-12 00:01:49 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2013-05-27 23:23:34 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2013-04-18 01:34:05 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-05-27 23:23:34 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2013-04-15 22:28:00 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2014-04-22 11:17:02 +08:00
|
|
|
#define DEBUG_TYPE "format-token-breaker"
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
2013-05-27 23:23:34 +08:00
|
|
|
|
2013-10-30 15:36:40 +08:00
|
|
|
static const char *const Blanks = " \t\v\f\r";
|
2013-06-20 21:58:37 +08:00
|
|
|
static bool IsBlank(char C) {
|
|
|
|
switch (C) {
|
2013-07-08 22:34:09 +08:00
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\v':
|
|
|
|
case '\f':
|
2013-10-30 15:36:40 +08:00
|
|
|
case '\r':
|
2013-07-08 22:34:09 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
2013-06-20 21:58:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
static StringRef getLineCommentIndentPrefix(StringRef Comment) {
|
|
|
|
static const char *const KnownPrefixes[] = {"///", "//", "//!"};
|
|
|
|
StringRef LongestPrefix;
|
|
|
|
for (StringRef KnownPrefix : KnownPrefixes) {
|
|
|
|
if (Comment.startswith(KnownPrefix)) {
|
|
|
|
size_t PrefixLength = KnownPrefix.size();
|
|
|
|
while (PrefixLength < Comment.size() && Comment[PrefixLength] == ' ')
|
|
|
|
++PrefixLength;
|
|
|
|
if (PrefixLength > LongestPrefix.size())
|
|
|
|
LongestPrefix = Comment.substr(0, PrefixLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return LongestPrefix;
|
|
|
|
}
|
|
|
|
|
2013-07-01 11:38:29 +08:00
|
|
|
static BreakableToken::Split getCommentSplit(StringRef Text,
|
|
|
|
unsigned ContentStartColumn,
|
|
|
|
unsigned ColumnLimit,
|
2013-09-05 22:08:34 +08:00
|
|
|
unsigned TabWidth,
|
2013-07-01 11:38:29 +08:00
|
|
|
encoding::Encoding Encoding) {
|
2013-04-18 01:34:05 +08:00
|
|
|
if (ColumnLimit <= ContentStartColumn + 1)
|
2013-05-27 23:23:34 +08:00
|
|
|
return BreakableToken::Split(StringRef::npos, 0);
|
2013-04-18 01:34:05 +08:00
|
|
|
|
|
|
|
unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1;
|
2013-06-05 22:09:10 +08:00
|
|
|
unsigned MaxSplitBytes = 0;
|
|
|
|
|
|
|
|
for (unsigned NumChars = 0;
|
2013-09-05 22:08:34 +08:00
|
|
|
NumChars < MaxSplit && MaxSplitBytes < Text.size();) {
|
|
|
|
unsigned BytesInChar =
|
2013-06-05 22:09:10 +08:00
|
|
|
encoding::getCodePointNumBytes(Text[MaxSplitBytes], Encoding);
|
2013-09-05 22:08:34 +08:00
|
|
|
NumChars +=
|
|
|
|
encoding::columnWidthWithTabs(Text.substr(MaxSplitBytes, BytesInChar),
|
|
|
|
ContentStartColumn, TabWidth, Encoding);
|
|
|
|
MaxSplitBytes += BytesInChar;
|
|
|
|
}
|
2013-06-05 22:09:10 +08:00
|
|
|
|
2013-06-20 21:58:37 +08:00
|
|
|
StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes);
|
2013-04-18 01:34:05 +08:00
|
|
|
if (SpaceOffset == StringRef::npos ||
|
2013-05-27 23:23:34 +08:00
|
|
|
// Don't break at leading whitespace.
|
2013-06-20 21:58:37 +08:00
|
|
|
Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos) {
|
2013-05-30 06:06:18 +08:00
|
|
|
// Make sure that we don't break at leading whitespace that
|
|
|
|
// reaches past MaxSplit.
|
2013-06-20 21:58:37 +08:00
|
|
|
StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(Blanks);
|
2013-05-30 06:06:18 +08:00
|
|
|
if (FirstNonWhitespace == StringRef::npos)
|
|
|
|
// If the comment is only whitespace, we cannot split.
|
|
|
|
return BreakableToken::Split(StringRef::npos, 0);
|
2013-06-20 21:58:37 +08:00
|
|
|
SpaceOffset = Text.find_first_of(
|
|
|
|
Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace));
|
2013-05-30 06:06:18 +08:00
|
|
|
}
|
2013-04-18 01:34:05 +08:00
|
|
|
if (SpaceOffset != StringRef::npos && SpaceOffset != 0) {
|
2013-06-20 21:58:37 +08:00
|
|
|
StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks);
|
|
|
|
StringRef AfterCut = Text.substr(SpaceOffset).ltrim(Blanks);
|
2013-04-18 01:34:05 +08:00
|
|
|
return BreakableToken::Split(BeforeCut.size(),
|
|
|
|
AfterCut.begin() - BeforeCut.end());
|
|
|
|
}
|
|
|
|
return BreakableToken::Split(StringRef::npos, 0);
|
|
|
|
}
|
|
|
|
|
2014-05-09 21:11:16 +08:00
|
|
|
static BreakableToken::Split
|
|
|
|
getStringSplit(StringRef Text, unsigned UsedColumns, unsigned ColumnLimit,
|
|
|
|
unsigned TabWidth, encoding::Encoding Encoding) {
|
2013-05-27 23:23:34 +08:00
|
|
|
// FIXME: Reduce unit test case.
|
|
|
|
if (Text.empty())
|
|
|
|
return BreakableToken::Split(StringRef::npos, 0);
|
2013-09-17 04:20:49 +08:00
|
|
|
if (ColumnLimit <= UsedColumns)
|
2013-06-05 22:09:10 +08:00
|
|
|
return BreakableToken::Split(StringRef::npos, 0);
|
2013-11-26 18:38:53 +08:00
|
|
|
unsigned MaxSplit = ColumnLimit - UsedColumns;
|
2013-06-05 22:09:10 +08:00
|
|
|
StringRef::size_type SpaceOffset = 0;
|
|
|
|
StringRef::size_type SlashOffset = 0;
|
2013-06-19 22:22:47 +08:00
|
|
|
StringRef::size_type WordStartOffset = 0;
|
2013-06-05 22:09:10 +08:00
|
|
|
StringRef::size_type SplitPoint = 0;
|
|
|
|
for (unsigned Chars = 0;;) {
|
|
|
|
unsigned Advance;
|
|
|
|
if (Text[0] == '\\') {
|
|
|
|
Advance = encoding::getEscapeSequenceLength(Text);
|
|
|
|
Chars += Advance;
|
|
|
|
} else {
|
|
|
|
Advance = encoding::getCodePointNumBytes(Text[0], Encoding);
|
2013-09-17 04:20:49 +08:00
|
|
|
Chars += encoding::columnWidthWithTabs(
|
|
|
|
Text.substr(0, Advance), UsedColumns + Chars, TabWidth, Encoding);
|
2013-06-05 22:09:10 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:50:35 +08:00
|
|
|
if (Chars > MaxSplit || Text.size() <= Advance)
|
2013-06-05 22:09:10 +08:00
|
|
|
break;
|
|
|
|
|
2013-06-20 21:58:37 +08:00
|
|
|
if (IsBlank(Text[0]))
|
2013-06-05 22:09:10 +08:00
|
|
|
SpaceOffset = SplitPoint;
|
|
|
|
if (Text[0] == '/')
|
|
|
|
SlashOffset = SplitPoint;
|
2013-06-20 21:58:37 +08:00
|
|
|
if (Advance == 1 && !isAlphanumeric(Text[0]))
|
2013-06-19 22:22:47 +08:00
|
|
|
WordStartOffset = SplitPoint;
|
2013-06-05 22:09:10 +08:00
|
|
|
|
|
|
|
SplitPoint += Advance;
|
|
|
|
Text = Text.substr(Advance);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SpaceOffset != 0)
|
2013-05-27 23:23:34 +08:00
|
|
|
return BreakableToken::Split(SpaceOffset + 1, 0);
|
2013-06-05 22:09:10 +08:00
|
|
|
if (SlashOffset != 0)
|
2013-05-27 23:23:34 +08:00
|
|
|
return BreakableToken::Split(SlashOffset + 1, 0);
|
2013-06-19 22:22:47 +08:00
|
|
|
if (WordStartOffset != 0)
|
|
|
|
return BreakableToken::Split(WordStartOffset + 1, 0);
|
2013-06-05 22:09:10 +08:00
|
|
|
if (SplitPoint != 0)
|
|
|
|
return BreakableToken::Split(SplitPoint, 0);
|
|
|
|
return BreakableToken::Split(StringRef::npos, 0);
|
2013-04-18 01:34:05 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
bool switchesFormatting(const FormatToken &Token) {
|
|
|
|
assert((Token.is(TT_BlockComment) || Token.is(TT_LineComment)) &&
|
|
|
|
"formatting regions are switched by comment tokens");
|
|
|
|
StringRef Content = Token.TokenText.substr(2).ltrim();
|
|
|
|
return Content.startswith("clang-format on") ||
|
|
|
|
Content.startswith("clang-format off");
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
BreakableToken::getLineLengthAfterCompression(unsigned RemainingTokenColumns,
|
|
|
|
Split Split) const {
|
|
|
|
// Example: consider the content
|
|
|
|
// lala lala
|
|
|
|
// - RemainingTokenColumns is the original number of columns, 10;
|
|
|
|
// - Split is (4, 2), denoting the two spaces between the two words;
|
|
|
|
//
|
|
|
|
// We compute the number of columns when the split is compressed into a single
|
|
|
|
// space, like:
|
|
|
|
// lala lala
|
|
|
|
return RemainingTokenColumns + 1 - Split.second;
|
|
|
|
}
|
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
unsigned BreakableSingleLineToken::getLineCount() const { return 1; }
|
|
|
|
|
2013-06-08 00:02:52 +08:00
|
|
|
unsigned BreakableSingleLineToken::getLineLengthAfterSplit(
|
2017-01-25 21:58:58 +08:00
|
|
|
unsigned LineIndex, unsigned TailOffset,
|
|
|
|
StringRef::size_type Length) const {
|
2013-06-05 22:09:10 +08:00
|
|
|
return StartColumn + Prefix.size() + Postfix.size() +
|
2017-01-25 21:58:58 +08:00
|
|
|
encoding::columnWidthWithTabs(Line.substr(TailOffset, Length),
|
2013-09-05 22:08:34 +08:00
|
|
|
StartColumn + Prefix.size(),
|
|
|
|
Style.TabWidth, Encoding);
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
|
|
|
|
2013-06-14 19:46:10 +08:00
|
|
|
BreakableSingleLineToken::BreakableSingleLineToken(
|
2017-01-31 19:25:01 +08:00
|
|
|
const FormatToken &Tok, unsigned StartColumn, StringRef Prefix,
|
|
|
|
StringRef Postfix, bool InPPDirective, encoding::Encoding Encoding,
|
|
|
|
const FormatStyle &Style)
|
|
|
|
: BreakableToken(Tok, InPPDirective, Encoding, Style),
|
2013-09-05 22:08:34 +08:00
|
|
|
StartColumn(StartColumn), Prefix(Prefix), Postfix(Postfix) {
|
2014-01-09 22:18:12 +08:00
|
|
|
assert(Tok.TokenText.endswith(Postfix));
|
2013-05-27 23:23:34 +08:00
|
|
|
Line = Tok.TokenText.substr(
|
|
|
|
Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size());
|
|
|
|
}
|
|
|
|
|
2013-09-17 04:20:49 +08:00
|
|
|
BreakableStringLiteral::BreakableStringLiteral(
|
2017-01-31 19:25:01 +08:00
|
|
|
const FormatToken &Tok, unsigned StartColumn, StringRef Prefix,
|
|
|
|
StringRef Postfix, bool InPPDirective, encoding::Encoding Encoding,
|
|
|
|
const FormatStyle &Style)
|
|
|
|
: BreakableSingleLineToken(Tok, StartColumn, Prefix, Postfix, InPPDirective,
|
|
|
|
Encoding, Style) {}
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
BreakableToken::Split
|
|
|
|
BreakableStringLiteral::getSplit(unsigned LineIndex, unsigned TailOffset,
|
|
|
|
unsigned ColumnLimit) const {
|
2013-09-17 04:20:49 +08:00
|
|
|
return getStringSplit(Line.substr(TailOffset),
|
|
|
|
StartColumn + Prefix.size() + Postfix.size(),
|
|
|
|
ColumnLimit, Style.TabWidth, Encoding);
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-06-12 00:01:49 +08:00
|
|
|
void BreakableStringLiteral::insertBreak(unsigned LineIndex,
|
|
|
|
unsigned TailOffset, Split Split,
|
|
|
|
WhitespaceManager &Whitespaces) {
|
2014-01-14 17:53:07 +08:00
|
|
|
unsigned LeadingSpaces = StartColumn;
|
|
|
|
// The '@' of an ObjC string literal (@"Test") does not become part of the
|
|
|
|
// string token.
|
|
|
|
// FIXME: It might be a cleaner solution to merge the tokens as a
|
|
|
|
// precomputation step.
|
|
|
|
if (Prefix.startswith("@"))
|
|
|
|
--LeadingSpaces;
|
2013-06-12 00:01:49 +08:00
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
|
|
|
Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix,
|
2017-01-31 19:25:01 +08:00
|
|
|
Prefix, InPPDirective, 1, LeadingSpaces);
|
2013-06-12 00:01:49 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
BreakableComment::BreakableComment(const FormatToken &Token,
|
2017-01-31 19:25:01 +08:00
|
|
|
unsigned StartColumn,
|
2017-02-21 18:54:50 +08:00
|
|
|
bool InPPDirective,
|
2017-01-25 21:58:58 +08:00
|
|
|
encoding::Encoding Encoding,
|
|
|
|
const FormatStyle &Style)
|
2017-01-31 19:25:01 +08:00
|
|
|
: BreakableToken(Token, InPPDirective, Encoding, Style),
|
2017-02-21 18:54:50 +08:00
|
|
|
StartColumn(StartColumn) {}
|
2017-01-25 21:58:58 +08:00
|
|
|
|
|
|
|
unsigned BreakableComment::getLineCount() const { return Lines.size(); }
|
|
|
|
|
|
|
|
BreakableToken::Split BreakableComment::getSplit(unsigned LineIndex,
|
|
|
|
unsigned TailOffset,
|
|
|
|
unsigned ColumnLimit) const {
|
|
|
|
return getCommentSplit(Content[LineIndex].substr(TailOffset),
|
|
|
|
getContentStartColumn(LineIndex, TailOffset),
|
2013-09-05 22:08:34 +08:00
|
|
|
ColumnLimit, Style.TabWidth, Encoding);
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
void BreakableComment::compressWhitespace(unsigned LineIndex,
|
|
|
|
unsigned TailOffset, Split Split,
|
|
|
|
WhitespaceManager &Whitespaces) {
|
|
|
|
StringRef Text = Content[LineIndex].substr(TailOffset);
|
|
|
|
// Text is relative to the content line, but Whitespaces operates relative to
|
|
|
|
// the start of the corresponding token, so compute the start of the Split
|
|
|
|
// that needs to be compressed into a single space relative to the start of
|
|
|
|
// its token.
|
|
|
|
unsigned BreakOffsetInToken =
|
|
|
|
Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first;
|
|
|
|
unsigned CharsToRemove = Split.second;
|
2013-06-12 00:01:49 +08:00
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
2017-01-25 21:58:58 +08:00
|
|
|
tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "", "",
|
2017-01-31 19:25:01 +08:00
|
|
|
/*InPPDirective=*/false, /*Newlines=*/0, /*Spaces=*/1);
|
2013-11-13 01:50:13 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
BreakableToken::Split
|
|
|
|
BreakableComment::getReflowSplit(StringRef Text, StringRef ReflowPrefix,
|
|
|
|
unsigned PreviousEndColumn,
|
|
|
|
unsigned ColumnLimit) const {
|
|
|
|
unsigned ReflowStartColumn = PreviousEndColumn + ReflowPrefix.size();
|
|
|
|
StringRef TrimmedText = Text.rtrim(Blanks);
|
|
|
|
// This is the width of the resulting line in case the full line of Text gets
|
|
|
|
// reflown up starting at ReflowStartColumn.
|
|
|
|
unsigned FullWidth = ReflowStartColumn + encoding::columnWidthWithTabs(
|
|
|
|
TrimmedText, ReflowStartColumn,
|
|
|
|
Style.TabWidth, Encoding);
|
|
|
|
// If the full line fits up, we return a reflow split after it,
|
|
|
|
// otherwise we compute the largest piece of text that fits after
|
|
|
|
// ReflowStartColumn.
|
|
|
|
Split ReflowSplit =
|
|
|
|
FullWidth <= ColumnLimit
|
|
|
|
? Split(TrimmedText.size(), Text.size() - TrimmedText.size())
|
|
|
|
: getCommentSplit(Text, ReflowStartColumn, ColumnLimit,
|
|
|
|
Style.TabWidth, Encoding);
|
|
|
|
|
|
|
|
// We need to be extra careful here, because while it's OK to keep a long line
|
|
|
|
// if it can't be broken into smaller pieces (like when the first word of a
|
|
|
|
// long line is longer than the column limit), it's not OK to reflow that long
|
|
|
|
// word up. So we recompute the size of the previous line after reflowing and
|
|
|
|
// only return the reflow split if that's under the line limit.
|
|
|
|
if (ReflowSplit.first != StringRef::npos &&
|
|
|
|
// Check if the width of the newly reflown line is under the limit.
|
|
|
|
PreviousEndColumn + ReflowPrefix.size() +
|
|
|
|
encoding::columnWidthWithTabs(Text.substr(0, ReflowSplit.first),
|
|
|
|
PreviousEndColumn +
|
|
|
|
ReflowPrefix.size(),
|
|
|
|
Style.TabWidth, Encoding) <=
|
|
|
|
ColumnLimit) {
|
|
|
|
return ReflowSplit;
|
2013-06-12 00:01:49 +08:00
|
|
|
}
|
2017-01-25 21:58:58 +08:00
|
|
|
return Split(StringRef::npos, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const FormatToken &BreakableComment::tokenAt(unsigned LineIndex) const {
|
|
|
|
return Tokens[LineIndex] ? *Tokens[LineIndex] : Tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool mayReflowContent(StringRef Content) {
|
|
|
|
Content = Content.trim(Blanks);
|
2017-02-02 18:52:08 +08:00
|
|
|
// Lines starting with '@' commonly have special meaning.
|
|
|
|
static const SmallVector<StringRef, 4> kSpecialMeaningPrefixes = {
|
|
|
|
"@", "TODO", "FIXME", "XXX"};
|
|
|
|
bool hasSpecialMeaningPrefix = false;
|
|
|
|
for (StringRef Prefix : kSpecialMeaningPrefixes) {
|
|
|
|
if (Content.startswith(Prefix)) {
|
|
|
|
hasSpecialMeaningPrefix = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-25 21:58:58 +08:00
|
|
|
// Simple heuristic for what to reflow: content should contain at least two
|
|
|
|
// characters and either the first or second character must be
|
|
|
|
// non-punctuation.
|
2017-02-02 18:52:08 +08:00
|
|
|
return Content.size() >= 2 && !hasSpecialMeaningPrefix &&
|
|
|
|
!Content.endswith("\\") &&
|
2017-01-25 21:58:58 +08:00
|
|
|
// Note that this is UTF-8 safe, since if isPunctuation(Content[0]) is
|
|
|
|
// true, then the first code point must be 1 byte long.
|
|
|
|
(!isPunctuation(Content[0]) || !isPunctuation(Content[1]));
|
|
|
|
}
|
|
|
|
|
2013-06-05 22:09:10 +08:00
|
|
|
BreakableBlockComment::BreakableBlockComment(
|
2017-01-31 19:25:01 +08:00
|
|
|
const FormatToken &Token, unsigned StartColumn,
|
2013-06-14 19:46:10 +08:00
|
|
|
unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective,
|
2013-09-05 22:08:34 +08:00
|
|
|
encoding::Encoding Encoding, const FormatStyle &Style)
|
2017-02-21 18:54:50 +08:00
|
|
|
: BreakableComment(Token, StartColumn, InPPDirective, Encoding, Style) {
|
2017-01-25 21:58:58 +08:00
|
|
|
assert(Tok.is(TT_BlockComment) &&
|
|
|
|
"block comment section must start with a block comment");
|
|
|
|
|
|
|
|
StringRef TokenText(Tok.TokenText);
|
2013-05-27 23:23:34 +08:00
|
|
|
assert(TokenText.startswith("/*") && TokenText.endswith("*/"));
|
2013-04-15 22:28:00 +08:00
|
|
|
TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n");
|
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
int IndentDelta = StartColumn - OriginalStartColumn;
|
2017-01-25 21:58:58 +08:00
|
|
|
Content.resize(Lines.size());
|
|
|
|
Content[0] = Lines[0];
|
|
|
|
ContentColumn.resize(Lines.size());
|
|
|
|
// Account for the initial '/*'.
|
|
|
|
ContentColumn[0] = StartColumn + 2;
|
|
|
|
Tokens.resize(Lines.size());
|
2013-07-08 22:12:07 +08:00
|
|
|
for (size_t i = 1; i < Lines.size(); ++i)
|
2013-09-05 22:08:34 +08:00
|
|
|
adjustWhitespace(i, IndentDelta);
|
2013-07-08 22:12:07 +08:00
|
|
|
|
2017-02-16 20:39:31 +08:00
|
|
|
// Align decorations with the column of the star on the first line,
|
|
|
|
// that is one column after the start "/*".
|
|
|
|
DecorationColumn = StartColumn + 1;
|
|
|
|
|
|
|
|
// Account for comment decoration patterns like this:
|
|
|
|
//
|
|
|
|
// /*
|
|
|
|
// ** blah blah blah
|
|
|
|
// */
|
|
|
|
if (Lines.size() >= 2 && Content[1].startswith("**") &&
|
|
|
|
static_cast<unsigned>(ContentColumn[1]) == StartColumn) {
|
|
|
|
DecorationColumn = StartColumn;
|
|
|
|
}
|
|
|
|
|
2013-07-08 22:12:07 +08:00
|
|
|
Decoration = "* ";
|
2013-05-27 23:23:34 +08:00
|
|
|
if (Lines.size() == 1 && !FirstInLine) {
|
|
|
|
// Comments for which FirstInLine is false can start on arbitrary column,
|
|
|
|
// and available horizontal space can be too small to align consecutive
|
|
|
|
// lines with the first one.
|
|
|
|
// FIXME: We could, probably, align them to current indentation level, but
|
|
|
|
// now we just wrap them without stars.
|
2013-07-08 22:12:07 +08:00
|
|
|
Decoration = "";
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
2013-07-08 22:12:07 +08:00
|
|
|
for (size_t i = 1, e = Lines.size(); i < e && !Decoration.empty(); ++i) {
|
|
|
|
// If the last line is empty, the closing "*/" will have a star.
|
2017-01-25 21:58:58 +08:00
|
|
|
if (i + 1 == e && Content[i].empty())
|
2013-07-08 22:12:07 +08:00
|
|
|
break;
|
2017-01-25 21:58:58 +08:00
|
|
|
if (!Content[i].empty() && i + 1 != e &&
|
|
|
|
Decoration.startswith(Content[i]))
|
2015-05-06 15:17:22 +08:00
|
|
|
continue;
|
2017-01-25 21:58:58 +08:00
|
|
|
while (!Content[i].startswith(Decoration))
|
2013-07-08 22:12:07 +08:00
|
|
|
Decoration = Decoration.substr(0, Decoration.size() - 1);
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
2013-07-08 22:12:07 +08:00
|
|
|
|
|
|
|
LastLineNeedsDecoration = true;
|
2017-01-25 21:58:58 +08:00
|
|
|
IndentAtLineBreak = ContentColumn[0] + 1;
|
|
|
|
for (size_t i = 1, e = Lines.size(); i < e; ++i) {
|
|
|
|
if (Content[i].empty()) {
|
|
|
|
if (i + 1 == e) {
|
2013-07-08 22:12:07 +08:00
|
|
|
// Empty last line means that we already have a star as a part of the
|
|
|
|
// trailing */. We also need to preserve whitespace, so that */ is
|
|
|
|
// correctly indented.
|
|
|
|
LastLineNeedsDecoration = false;
|
2017-02-16 20:39:31 +08:00
|
|
|
// Align the star in the last '*/' with the stars on the previous lines.
|
|
|
|
if (e >= 2 && !Decoration.empty()) {
|
|
|
|
ContentColumn[i] = DecorationColumn;
|
|
|
|
}
|
2013-07-08 22:12:07 +08:00
|
|
|
} else if (Decoration.empty()) {
|
|
|
|
// For all other lines, set the start column to 0 if they're empty, so
|
|
|
|
// we do not insert trailing whitespace anywhere.
|
2017-01-25 21:58:58 +08:00
|
|
|
ContentColumn[i] = 0;
|
2013-07-08 22:12:07 +08:00
|
|
|
}
|
2013-05-27 23:23:34 +08:00
|
|
|
continue;
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
2015-05-06 15:17:22 +08:00
|
|
|
|
2013-07-08 22:12:07 +08:00
|
|
|
// The first line already excludes the star.
|
2017-02-16 20:39:31 +08:00
|
|
|
// The last line excludes the star if LastLineNeedsDecoration is false.
|
2013-07-08 22:12:07 +08:00
|
|
|
// For all other lines, adjust the line to exclude the star and
|
|
|
|
// (optionally) the first whitespace.
|
2017-01-25 21:58:58 +08:00
|
|
|
unsigned DecorationSize = Decoration.startswith(Content[i])
|
|
|
|
? Content[i].size()
|
|
|
|
: Decoration.size();
|
2017-02-16 20:39:31 +08:00
|
|
|
if (DecorationSize) {
|
|
|
|
ContentColumn[i] = DecorationColumn + DecorationSize;
|
|
|
|
}
|
2017-01-25 21:58:58 +08:00
|
|
|
Content[i] = Content[i].substr(DecorationSize);
|
|
|
|
if (!Decoration.startswith(Content[i]))
|
2015-05-06 15:17:22 +08:00
|
|
|
IndentAtLineBreak =
|
2017-01-25 21:58:58 +08:00
|
|
|
std::min<int>(IndentAtLineBreak, std::max(0, ContentColumn[i]));
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
2017-01-25 21:58:58 +08:00
|
|
|
IndentAtLineBreak =
|
|
|
|
std::max<unsigned>(IndentAtLineBreak, Decoration.size());
|
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
DEBUG({
|
2013-07-08 22:12:07 +08:00
|
|
|
llvm::dbgs() << "IndentAtLineBreak " << IndentAtLineBreak << "\n";
|
2013-05-27 23:23:34 +08:00
|
|
|
for (size_t i = 0; i < Lines.size(); ++i) {
|
2017-01-25 21:58:58 +08:00
|
|
|
llvm::dbgs() << i << " |" << Content[i] << "| "
|
2017-02-16 20:39:31 +08:00
|
|
|
<< "CC=" << ContentColumn[i] << "| "
|
|
|
|
<< "IN=" << (Content[i].data() - Lines[i].data()) << "\n";
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-09-05 22:08:34 +08:00
|
|
|
void BreakableBlockComment::adjustWhitespace(unsigned LineIndex,
|
2013-05-27 23:23:34 +08:00
|
|
|
int IndentDelta) {
|
2013-06-14 19:46:10 +08:00
|
|
|
// When in a preprocessor directive, the trailing backslash in a block comment
|
|
|
|
// is not needed, but can serve a purpose of uniformity with necessary escaped
|
|
|
|
// newlines outside the comment. In this case we remove it here before
|
|
|
|
// trimming the trailing whitespace. The backslash will be re-added later when
|
|
|
|
// inserting a line break.
|
|
|
|
size_t EndOfPreviousLine = Lines[LineIndex - 1].size();
|
|
|
|
if (InPPDirective && Lines[LineIndex - 1].endswith("\\"))
|
|
|
|
--EndOfPreviousLine;
|
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
// Calculate the end of the non-whitespace text in the previous line.
|
2013-06-14 19:46:10 +08:00
|
|
|
EndOfPreviousLine =
|
2013-06-20 21:58:37 +08:00
|
|
|
Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine);
|
2013-05-27 23:23:34 +08:00
|
|
|
if (EndOfPreviousLine == StringRef::npos)
|
|
|
|
EndOfPreviousLine = 0;
|
|
|
|
else
|
|
|
|
++EndOfPreviousLine;
|
|
|
|
// Calculate the start of the non-whitespace text in the current line.
|
2013-06-20 21:58:37 +08:00
|
|
|
size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks);
|
2013-05-27 23:23:34 +08:00
|
|
|
if (StartOfLine == StringRef::npos)
|
2015-06-17 20:23:15 +08:00
|
|
|
StartOfLine = Lines[LineIndex].rtrim("\r\n").size();
|
2013-04-18 01:34:05 +08:00
|
|
|
|
2013-09-05 22:08:34 +08:00
|
|
|
StringRef Whitespace = Lines[LineIndex].substr(0, StartOfLine);
|
2013-05-27 23:23:34 +08:00
|
|
|
// Adjust Lines to only contain relevant text.
|
2017-01-25 21:58:58 +08:00
|
|
|
size_t PreviousContentOffset =
|
|
|
|
Content[LineIndex - 1].data() - Lines[LineIndex - 1].data();
|
|
|
|
Content[LineIndex - 1] = Lines[LineIndex - 1].substr(
|
|
|
|
PreviousContentOffset, EndOfPreviousLine - PreviousContentOffset);
|
|
|
|
Content[LineIndex] = Lines[LineIndex].substr(StartOfLine);
|
2013-05-28 18:01:59 +08:00
|
|
|
|
2013-12-06 00:25:25 +08:00
|
|
|
// Adjust the start column uniformly across all lines.
|
2017-01-25 21:58:58 +08:00
|
|
|
ContentColumn[LineIndex] =
|
2013-09-10 17:38:25 +08:00
|
|
|
encoding::columnWidthWithTabs(Whitespace, 0, Style.TabWidth, Encoding) +
|
2014-04-18 00:12:46 +08:00
|
|
|
IndentDelta;
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-08 00:02:52 +08:00
|
|
|
unsigned BreakableBlockComment::getLineLengthAfterSplit(
|
2017-01-25 21:58:58 +08:00
|
|
|
unsigned LineIndex, unsigned TailOffset,
|
|
|
|
StringRef::size_type Length) const {
|
|
|
|
unsigned ContentStartColumn = getContentStartColumn(LineIndex, TailOffset);
|
|
|
|
unsigned LineLength =
|
|
|
|
ContentStartColumn + encoding::columnWidthWithTabs(
|
|
|
|
Content[LineIndex].substr(TailOffset, Length),
|
|
|
|
ContentStartColumn, Style.TabWidth, Encoding);
|
|
|
|
// The last line gets a "*/" postfix.
|
|
|
|
if (LineIndex + 1 == Lines.size()) {
|
|
|
|
LineLength += 2;
|
|
|
|
// We never need a decoration when breaking just the trailing "*/" postfix.
|
|
|
|
// Note that checking that Length == 0 is not enough, since Length could
|
|
|
|
// also be StringRef::npos.
|
|
|
|
if (Content[LineIndex].substr(TailOffset, Length).empty()) {
|
|
|
|
LineLength -= Decoration.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return LineLength;
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
|
2013-06-14 19:46:10 +08:00
|
|
|
Split Split,
|
2013-05-27 23:23:34 +08:00
|
|
|
WhitespaceManager &Whitespaces) {
|
2017-01-25 21:58:58 +08:00
|
|
|
StringRef Text = Content[LineIndex].substr(TailOffset);
|
2013-05-27 23:23:34 +08:00
|
|
|
StringRef Prefix = Decoration;
|
2017-01-25 21:58:58 +08:00
|
|
|
// We need this to account for the case when we have a decoration "* " for all
|
|
|
|
// the lines except for the last one, where the star in "*/" acts as a
|
|
|
|
// decoration.
|
|
|
|
unsigned LocalIndentAtLineBreak = IndentAtLineBreak;
|
2013-05-27 23:23:34 +08:00
|
|
|
if (LineIndex + 1 == Lines.size() &&
|
|
|
|
Text.size() == Split.first + Split.second) {
|
|
|
|
// For the last line we need to break before "*/", but not to add "* ".
|
|
|
|
Prefix = "";
|
2017-01-25 21:58:58 +08:00
|
|
|
if (LocalIndentAtLineBreak >= 2)
|
|
|
|
LocalIndentAtLineBreak -= 2;
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
2017-01-25 21:58:58 +08:00
|
|
|
// The split offset is from the beginning of the line. Convert it to an offset
|
|
|
|
// from the beginning of the token text.
|
2013-05-27 23:23:34 +08:00
|
|
|
unsigned BreakOffsetInToken =
|
2017-01-25 21:58:58 +08:00
|
|
|
Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first;
|
2013-05-27 23:23:34 +08:00
|
|
|
unsigned CharsToRemove = Split.second;
|
2017-01-25 21:58:58 +08:00
|
|
|
assert(LocalIndentAtLineBreak >= Prefix.size());
|
2013-09-28 00:14:22 +08:00
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
2017-01-25 21:58:58 +08:00
|
|
|
tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "", Prefix,
|
2017-01-31 19:25:01 +08:00
|
|
|
InPPDirective, /*Newlines=*/1,
|
2017-01-25 21:58:58 +08:00
|
|
|
/*Spaces=*/LocalIndentAtLineBreak - Prefix.size());
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
BreakableToken::Split BreakableBlockComment::getSplitBefore(
|
|
|
|
unsigned LineIndex,
|
|
|
|
unsigned PreviousEndColumn,
|
2017-02-02 23:32:19 +08:00
|
|
|
unsigned ColumnLimit,
|
|
|
|
llvm::Regex &CommentPragmasRegex) const {
|
|
|
|
if (!mayReflow(LineIndex, CommentPragmasRegex))
|
2017-01-25 21:58:58 +08:00
|
|
|
return Split(StringRef::npos, 0);
|
|
|
|
StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks);
|
|
|
|
return getReflowSplit(TrimmedContent, ReflowPrefix, PreviousEndColumn,
|
|
|
|
ColumnLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned BreakableBlockComment::getReflownColumn(
|
|
|
|
StringRef Content,
|
|
|
|
unsigned LineIndex,
|
|
|
|
unsigned PreviousEndColumn) const {
|
|
|
|
unsigned StartColumn = PreviousEndColumn + ReflowPrefix.size();
|
|
|
|
// If this is the last line, it will carry around its '*/' postfix.
|
|
|
|
unsigned PostfixLength = (LineIndex + 1 == Lines.size() ? 2 : 0);
|
|
|
|
// The line is composed of previous text, reflow prefix, reflown text and
|
|
|
|
// postfix.
|
|
|
|
unsigned ReflownColumn =
|
|
|
|
StartColumn + encoding::columnWidthWithTabs(Content, StartColumn,
|
|
|
|
Style.TabWidth, Encoding) +
|
|
|
|
PostfixLength;
|
|
|
|
return ReflownColumn;
|
2013-11-13 01:50:13 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
unsigned BreakableBlockComment::getLineLengthAfterSplitBefore(
|
|
|
|
unsigned LineIndex, unsigned TailOffset,
|
|
|
|
unsigned PreviousEndColumn,
|
|
|
|
unsigned ColumnLimit,
|
|
|
|
Split SplitBefore) const {
|
2017-01-31 22:31:44 +08:00
|
|
|
if (SplitBefore.first == StringRef::npos ||
|
|
|
|
// Block comment line contents contain the trailing whitespace after the
|
|
|
|
// decoration, so the need of left trim. Note that this behavior is
|
|
|
|
// consistent with the breaking of block comments where the indentation of
|
|
|
|
// a broken line is uniform across all the lines of the block comment.
|
|
|
|
SplitBefore.first + SplitBefore.second <
|
|
|
|
Content[LineIndex].ltrim().size()) {
|
|
|
|
// A piece of line, not the whole, gets reflown.
|
|
|
|
return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
|
2017-01-25 21:58:58 +08:00
|
|
|
} else {
|
|
|
|
// The whole line gets reflown, need to check if we need to insert a break
|
|
|
|
// for the postfix or not.
|
|
|
|
StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks);
|
|
|
|
unsigned ReflownColumn =
|
|
|
|
getReflownColumn(TrimmedContent, LineIndex, PreviousEndColumn);
|
|
|
|
if (ReflownColumn <= ColumnLimit) {
|
|
|
|
return ReflownColumn;
|
|
|
|
}
|
|
|
|
return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
|
|
|
|
}
|
|
|
|
}
|
2015-06-17 21:08:06 +08:00
|
|
|
void BreakableBlockComment::replaceWhitespaceBefore(
|
2017-01-25 21:58:58 +08:00
|
|
|
unsigned LineIndex, unsigned PreviousEndColumn, unsigned ColumnLimit,
|
|
|
|
Split SplitBefore, WhitespaceManager &Whitespaces) {
|
|
|
|
if (LineIndex == 0) return;
|
|
|
|
StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks);
|
|
|
|
if (SplitBefore.first != StringRef::npos) {
|
|
|
|
// Here we need to reflow.
|
|
|
|
assert(Tokens[LineIndex - 1] == Tokens[LineIndex] &&
|
|
|
|
"Reflowing whitespace within a token");
|
|
|
|
// This is the offset of the end of the last line relative to the start of
|
|
|
|
// the token text in the token.
|
|
|
|
unsigned WhitespaceOffsetInToken = Content[LineIndex - 1].data() +
|
|
|
|
Content[LineIndex - 1].size() -
|
|
|
|
tokenAt(LineIndex).TokenText.data();
|
|
|
|
unsigned WhitespaceLength = TrimmedContent.data() -
|
|
|
|
tokenAt(LineIndex).TokenText.data() -
|
|
|
|
WhitespaceOffsetInToken;
|
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
|
|
|
tokenAt(LineIndex), WhitespaceOffsetInToken,
|
2017-01-31 19:25:01 +08:00
|
|
|
/*ReplaceChars=*/WhitespaceLength, /*PreviousPostfix=*/"",
|
|
|
|
/*CurrentPrefix=*/ReflowPrefix, InPPDirective, /*Newlines=*/0,
|
|
|
|
/*Spaces=*/0);
|
2017-01-25 21:58:58 +08:00
|
|
|
// Check if we need to also insert a break at the whitespace range.
|
|
|
|
// For this we first adapt the reflow split relative to the beginning of the
|
|
|
|
// content.
|
|
|
|
// Note that we don't need a penalty for this break, since it doesn't change
|
|
|
|
// the total number of lines.
|
|
|
|
Split BreakSplit = SplitBefore;
|
|
|
|
BreakSplit.first += TrimmedContent.data() - Content[LineIndex].data();
|
|
|
|
unsigned ReflownColumn =
|
|
|
|
getReflownColumn(TrimmedContent, LineIndex, PreviousEndColumn);
|
|
|
|
if (ReflownColumn > ColumnLimit) {
|
|
|
|
insertBreak(LineIndex, 0, BreakSplit, Whitespaces);
|
|
|
|
}
|
2013-04-15 22:28:00 +08:00
|
|
|
return;
|
2017-01-25 21:58:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Here no reflow with the previous line will happen.
|
|
|
|
// Fix the decoration of the line at LineIndex.
|
2013-05-27 23:23:34 +08:00
|
|
|
StringRef Prefix = Decoration;
|
2017-01-25 21:58:58 +08:00
|
|
|
if (Content[LineIndex].empty()) {
|
2013-05-28 16:55:01 +08:00
|
|
|
if (LineIndex + 1 == Lines.size()) {
|
2013-07-08 22:12:07 +08:00
|
|
|
if (!LastLineNeedsDecoration) {
|
|
|
|
// If the last line was empty, we don't need a prefix, as the */ will
|
|
|
|
// line up with the decoration (if it exists).
|
|
|
|
Prefix = "";
|
|
|
|
}
|
2013-05-28 16:55:01 +08:00
|
|
|
} else if (!Decoration.empty()) {
|
|
|
|
// For other empty lines, if we do have a decoration, adapt it to not
|
|
|
|
// contain a trailing whitespace.
|
|
|
|
Prefix = Prefix.substr(0, 1);
|
|
|
|
}
|
2013-05-30 14:40:07 +08:00
|
|
|
} else {
|
2017-01-25 21:58:58 +08:00
|
|
|
if (ContentColumn[LineIndex] == 1) {
|
2013-07-08 22:12:07 +08:00
|
|
|
// This line starts immediately after the decorating *.
|
2013-05-30 14:40:07 +08:00
|
|
|
Prefix = Prefix.substr(0, 1);
|
|
|
|
}
|
2013-05-28 16:55:01 +08:00
|
|
|
}
|
2017-01-25 21:58:58 +08:00
|
|
|
// This is the offset of the end of the last line relative to the start of the
|
|
|
|
// token text in the token.
|
|
|
|
unsigned WhitespaceOffsetInToken = Content[LineIndex - 1].data() +
|
|
|
|
Content[LineIndex - 1].size() -
|
|
|
|
tokenAt(LineIndex).TokenText.data();
|
|
|
|
unsigned WhitespaceLength = Content[LineIndex].data() -
|
|
|
|
tokenAt(LineIndex).TokenText.data() -
|
|
|
|
WhitespaceOffsetInToken;
|
2013-06-12 00:01:49 +08:00
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
2017-01-31 19:25:01 +08:00
|
|
|
tokenAt(LineIndex), WhitespaceOffsetInToken, WhitespaceLength, "", Prefix,
|
|
|
|
InPPDirective, /*Newlines=*/1, ContentColumn[LineIndex] - Prefix.size());
|
2013-04-18 01:34:05 +08:00
|
|
|
}
|
|
|
|
|
2017-02-02 23:32:19 +08:00
|
|
|
bool BreakableBlockComment::mayReflow(unsigned LineIndex,
|
|
|
|
llvm::Regex &CommentPragmasRegex) const {
|
|
|
|
// Content[LineIndex] may exclude the indent after the '*' decoration. In that
|
|
|
|
// case, we compute the start of the comment pragma manually.
|
|
|
|
StringRef IndentContent = Content[LineIndex];
|
|
|
|
if (Lines[LineIndex].ltrim(Blanks).startswith("*")) {
|
|
|
|
IndentContent = Lines[LineIndex].ltrim(Blanks).substr(1);
|
|
|
|
}
|
|
|
|
return LineIndex > 0 && !CommentPragmasRegex.match(IndentContent) &&
|
|
|
|
mayReflowContent(Content[LineIndex]) && !Tok.Finalized &&
|
|
|
|
!switchesFormatting(tokenAt(LineIndex));
|
|
|
|
}
|
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
unsigned
|
|
|
|
BreakableBlockComment::getContentStartColumn(unsigned LineIndex,
|
|
|
|
unsigned TailOffset) const {
|
|
|
|
// If we break, we always break at the predefined indent.
|
|
|
|
if (TailOffset != 0)
|
|
|
|
return IndentAtLineBreak;
|
2017-01-25 21:58:58 +08:00
|
|
|
return std::max(0, ContentColumn[LineIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
BreakableLineCommentSection::BreakableLineCommentSection(
|
2017-01-31 19:25:01 +08:00
|
|
|
const FormatToken &Token, unsigned StartColumn,
|
2017-01-25 21:58:58 +08:00
|
|
|
unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective,
|
|
|
|
encoding::Encoding Encoding, const FormatStyle &Style)
|
2017-02-21 18:54:50 +08:00
|
|
|
: BreakableComment(Token, StartColumn, InPPDirective, Encoding, Style) {
|
2017-01-25 21:58:58 +08:00
|
|
|
assert(Tok.is(TT_LineComment) &&
|
|
|
|
"line comment section must start with a line comment");
|
|
|
|
FormatToken *LineTok = nullptr;
|
|
|
|
for (const FormatToken *CurrentTok = &Tok;
|
|
|
|
CurrentTok && CurrentTok->is(TT_LineComment);
|
|
|
|
CurrentTok = CurrentTok->Next) {
|
|
|
|
LastLineTok = LineTok;
|
|
|
|
StringRef TokenText(CurrentTok->TokenText);
|
|
|
|
assert(TokenText.startswith("//"));
|
|
|
|
size_t FirstLineIndex = Lines.size();
|
|
|
|
TokenText.split(Lines, "\n");
|
|
|
|
Content.resize(Lines.size());
|
|
|
|
ContentColumn.resize(Lines.size());
|
|
|
|
OriginalContentColumn.resize(Lines.size());
|
|
|
|
Tokens.resize(Lines.size());
|
|
|
|
Prefix.resize(Lines.size());
|
|
|
|
OriginalPrefix.resize(Lines.size());
|
|
|
|
for (size_t i = FirstLineIndex, e = Lines.size(); i < e; ++i) {
|
2017-01-31 05:00:01 +08:00
|
|
|
// We need to trim the blanks in case this is not the first line in a
|
|
|
|
// multiline comment. Then the indent is included in Lines[i].
|
|
|
|
StringRef IndentPrefix =
|
|
|
|
getLineCommentIndentPrefix(Lines[i].ltrim(Blanks));
|
|
|
|
assert(IndentPrefix.startswith("//"));
|
2017-01-25 21:58:58 +08:00
|
|
|
OriginalPrefix[i] = Prefix[i] = IndentPrefix;
|
|
|
|
if (Lines[i].size() > Prefix[i].size() &&
|
|
|
|
isAlphanumeric(Lines[i][Prefix[i].size()])) {
|
|
|
|
if (Prefix[i] == "//")
|
|
|
|
Prefix[i] = "// ";
|
|
|
|
else if (Prefix[i] == "///")
|
|
|
|
Prefix[i] = "/// ";
|
|
|
|
else if (Prefix[i] == "//!")
|
|
|
|
Prefix[i] = "//! ";
|
|
|
|
}
|
|
|
|
|
|
|
|
Tokens[i] = LineTok;
|
|
|
|
Content[i] = Lines[i].substr(IndentPrefix.size());
|
|
|
|
OriginalContentColumn[i] =
|
|
|
|
StartColumn +
|
|
|
|
encoding::columnWidthWithTabs(OriginalPrefix[i],
|
|
|
|
StartColumn,
|
|
|
|
Style.TabWidth,
|
|
|
|
Encoding);
|
|
|
|
ContentColumn[i] =
|
|
|
|
StartColumn +
|
|
|
|
encoding::columnWidthWithTabs(Prefix[i],
|
|
|
|
StartColumn,
|
|
|
|
Style.TabWidth,
|
|
|
|
Encoding);
|
|
|
|
|
|
|
|
// Calculate the end of the non-whitespace text in this line.
|
|
|
|
size_t EndOfLine = Content[i].find_last_not_of(Blanks);
|
|
|
|
if (EndOfLine == StringRef::npos)
|
|
|
|
EndOfLine = Content[i].size();
|
|
|
|
else
|
|
|
|
++EndOfLine;
|
|
|
|
Content[i] = Content[i].substr(0, EndOfLine);
|
|
|
|
}
|
|
|
|
LineTok = CurrentTok->Next;
|
2017-02-02 22:36:50 +08:00
|
|
|
if (CurrentTok->Next && !CurrentTok->Next->ContinuesLineCommentSection) {
|
2017-01-31 21:32:38 +08:00
|
|
|
// A line comment section needs to broken by a line comment that is
|
|
|
|
// preceded by at least two newlines. Note that we put this break here
|
|
|
|
// instead of breaking at a previous stage during parsing, since that
|
|
|
|
// would split the contents of the enum into two unwrapped lines in this
|
|
|
|
// example, which is undesirable:
|
|
|
|
// enum A {
|
|
|
|
// a, // comment about a
|
|
|
|
//
|
|
|
|
// // comment about b
|
|
|
|
// b
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// FIXME: Consider putting separate line comment sections as children to
|
|
|
|
// the unwrapped line instead.
|
|
|
|
break;
|
|
|
|
}
|
2017-01-25 21:58:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned BreakableLineCommentSection::getLineLengthAfterSplit(
|
|
|
|
unsigned LineIndex, unsigned TailOffset,
|
|
|
|
StringRef::size_type Length) const {
|
|
|
|
unsigned ContentStartColumn =
|
|
|
|
(TailOffset == 0 ? ContentColumn[LineIndex]
|
|
|
|
: OriginalContentColumn[LineIndex]);
|
|
|
|
return ContentStartColumn + encoding::columnWidthWithTabs(
|
|
|
|
Content[LineIndex].substr(TailOffset, Length),
|
|
|
|
ContentStartColumn, Style.TabWidth, Encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BreakableLineCommentSection::insertBreak(unsigned LineIndex,
|
|
|
|
unsigned TailOffset, Split Split,
|
|
|
|
WhitespaceManager &Whitespaces) {
|
|
|
|
StringRef Text = Content[LineIndex].substr(TailOffset);
|
|
|
|
// Compute the offset of the split relative to the beginning of the token
|
|
|
|
// text.
|
|
|
|
unsigned BreakOffsetInToken =
|
|
|
|
Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first;
|
|
|
|
unsigned CharsToRemove = Split.second;
|
|
|
|
// Compute the size of the new indent, including the size of the new prefix of
|
|
|
|
// the newly broken line.
|
|
|
|
unsigned IndentAtLineBreak = OriginalContentColumn[LineIndex] +
|
|
|
|
Prefix[LineIndex].size() -
|
|
|
|
OriginalPrefix[LineIndex].size();
|
|
|
|
assert(IndentAtLineBreak >= Prefix[LineIndex].size());
|
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
|
|
|
tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "",
|
2017-01-31 19:25:01 +08:00
|
|
|
Prefix[LineIndex], InPPDirective, /*Newlines=*/1,
|
2017-01-25 21:58:58 +08:00
|
|
|
/*Spaces=*/IndentAtLineBreak - Prefix[LineIndex].size());
|
|
|
|
}
|
|
|
|
|
|
|
|
BreakableComment::Split BreakableLineCommentSection::getSplitBefore(
|
2017-02-02 23:32:19 +08:00
|
|
|
unsigned LineIndex, unsigned PreviousEndColumn, unsigned ColumnLimit,
|
|
|
|
llvm::Regex &CommentPragmasRegex) const {
|
|
|
|
if (!mayReflow(LineIndex, CommentPragmasRegex))
|
|
|
|
return Split(StringRef::npos, 0);
|
2017-01-25 21:58:58 +08:00
|
|
|
return getReflowSplit(Content[LineIndex], ReflowPrefix, PreviousEndColumn,
|
|
|
|
ColumnLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned BreakableLineCommentSection::getLineLengthAfterSplitBefore(
|
|
|
|
unsigned LineIndex, unsigned TailOffset,
|
|
|
|
unsigned PreviousEndColumn,
|
|
|
|
unsigned ColumnLimit,
|
|
|
|
Split SplitBefore) const {
|
|
|
|
if (SplitBefore.first == StringRef::npos ||
|
|
|
|
SplitBefore.first + SplitBefore.second < Content[LineIndex].size()) {
|
|
|
|
// A piece of line, not the whole line, gets reflown.
|
|
|
|
return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
|
|
|
|
} else {
|
|
|
|
// The whole line gets reflown.
|
|
|
|
unsigned StartColumn = PreviousEndColumn + ReflowPrefix.size();
|
|
|
|
return StartColumn + encoding::columnWidthWithTabs(Content[LineIndex],
|
|
|
|
StartColumn,
|
|
|
|
Style.TabWidth,
|
|
|
|
Encoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BreakableLineCommentSection::replaceWhitespaceBefore(
|
|
|
|
unsigned LineIndex, unsigned PreviousEndColumn, unsigned ColumnLimit,
|
|
|
|
Split SplitBefore, WhitespaceManager &Whitespaces) {
|
|
|
|
// If this is the first line of a token, we need to inform Whitespace Manager
|
|
|
|
// about it: either adapt the whitespace range preceding it, or mark it as an
|
|
|
|
// untouchable token.
|
|
|
|
// This happens for instance here:
|
|
|
|
// // line 1 \
|
|
|
|
// // line 2
|
|
|
|
if (LineIndex > 0 && Tokens[LineIndex] != Tokens[LineIndex - 1]) {
|
|
|
|
if (SplitBefore.first != StringRef::npos) {
|
|
|
|
// Reflow happens between tokens. Replace the whitespace between the
|
|
|
|
// tokens by the empty string.
|
2017-01-31 19:25:01 +08:00
|
|
|
Whitespaces.replaceWhitespace(
|
|
|
|
*Tokens[LineIndex], /*Newlines=*/0, /*Spaces=*/0,
|
|
|
|
/*StartOfTokenColumn=*/StartColumn, /*InPPDirective=*/false);
|
2017-01-25 21:58:58 +08:00
|
|
|
// Replace the indent and prefix of the token with the reflow prefix.
|
|
|
|
unsigned WhitespaceLength =
|
|
|
|
Content[LineIndex].data() - tokenAt(LineIndex).TokenText.data();
|
|
|
|
Whitespaces.replaceWhitespaceInToken(*Tokens[LineIndex],
|
|
|
|
/*Offset=*/0,
|
|
|
|
/*ReplaceChars=*/WhitespaceLength,
|
|
|
|
/*PreviousPostfix=*/"",
|
|
|
|
/*CurrentPrefix=*/ReflowPrefix,
|
|
|
|
/*InPPDirective=*/false,
|
|
|
|
/*Newlines=*/0,
|
|
|
|
/*Spaces=*/0);
|
|
|
|
} else {
|
|
|
|
// This is the first line for the current token, but no reflow with the
|
|
|
|
// previous token is necessary. However, we still may need to adjust the
|
2017-01-31 23:40:15 +08:00
|
|
|
// start column. Note that ContentColumn[LineIndex] is the expected
|
|
|
|
// content column after a possible update to the prefix, hence the prefix
|
|
|
|
// length change is included.
|
2017-01-25 21:58:58 +08:00
|
|
|
unsigned LineColumn =
|
|
|
|
ContentColumn[LineIndex] -
|
2017-01-31 23:40:15 +08:00
|
|
|
(Content[LineIndex].data() - Lines[LineIndex].data()) +
|
|
|
|
(OriginalPrefix[LineIndex].size() - Prefix[LineIndex].size());
|
[clang-format] Fix regression about not aligning trailing comments in case they were previously aligned, but at different indent.
Summary:
Comment reflower was adding untouchable tokens in case two consecutive comment lines are aligned in the source code. This disallows the whitespace manager to re-indent them later.
source:
```
int i = f(abc, // line 1
d, // line 2
// line 3
b);
```
Since line 2 and line 3 are aligned, the reflower was marking line 3 as untouchable; however the three comment lines need to be re-aligned.
output before:
```
int i = f(abc, // line 1
d, // line 2
// line 3
b);
```
output after:
```
int i = f(abc, // line 1
d, // line 2
// line 3
b);
```
Reviewers: djasper
Reviewed By: djasper
Subscribers: sammccall, cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D29383
llvm-svn: 293755
2017-02-01 18:10:04 +08:00
|
|
|
|
|
|
|
// We always want to create a replacement instead of adding an untouchable
|
|
|
|
// token, even if LineColumn is the same as the original column of the
|
|
|
|
// token. This is because WhitespaceManager doesn't align trailing
|
|
|
|
// comments if they are untouchable.
|
|
|
|
Whitespaces.replaceWhitespace(*Tokens[LineIndex],
|
|
|
|
/*Newlines=*/1,
|
|
|
|
/*Spaces=*/LineColumn,
|
|
|
|
/*StartOfTokenColumn=*/LineColumn,
|
|
|
|
/*InPPDirective=*/false);
|
2017-01-25 21:58:58 +08:00
|
|
|
}
|
2017-01-31 23:40:15 +08:00
|
|
|
}
|
|
|
|
if (OriginalPrefix[LineIndex] != Prefix[LineIndex]) {
|
|
|
|
// Adjust the prefix if necessary.
|
2017-01-25 21:58:58 +08:00
|
|
|
|
|
|
|
// Take care of the space possibly introduced after a decoration.
|
|
|
|
assert(Prefix[LineIndex] == (OriginalPrefix[LineIndex] + " ").str() &&
|
2017-01-31 23:40:15 +08:00
|
|
|
"Expecting a line comment prefix to differ from original by at most "
|
|
|
|
"a space");
|
2017-01-25 21:58:58 +08:00
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
|
|
|
tokenAt(LineIndex), OriginalPrefix[LineIndex].size(), 0, "", "",
|
2017-01-31 19:25:01 +08:00
|
|
|
/*InPPDirective=*/false, /*Newlines=*/0, /*Spaces=*/1);
|
2017-01-25 21:58:58 +08:00
|
|
|
}
|
|
|
|
// Add a break after a reflow split has been introduced, if necessary.
|
|
|
|
// Note that this break doesn't need to be penalized, since it doesn't change
|
|
|
|
// the number of lines.
|
|
|
|
if (SplitBefore.first != StringRef::npos &&
|
|
|
|
SplitBefore.first + SplitBefore.second < Content[LineIndex].size()) {
|
|
|
|
insertBreak(LineIndex, 0, SplitBefore, Whitespaces);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BreakableLineCommentSection::updateNextToken(LineState& State) const {
|
|
|
|
if (LastLineTok) {
|
|
|
|
State.NextToken = LastLineTok->Next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 23:32:19 +08:00
|
|
|
bool BreakableLineCommentSection::mayReflow(
|
|
|
|
unsigned LineIndex, llvm::Regex &CommentPragmasRegex) const {
|
|
|
|
// Line comments have the indent as part of the prefix, so we need to
|
|
|
|
// recompute the start of the line.
|
|
|
|
StringRef IndentContent = Content[LineIndex];
|
|
|
|
if (Lines[LineIndex].startswith("//")) {
|
|
|
|
IndentContent = Lines[LineIndex].substr(2);
|
|
|
|
}
|
|
|
|
return LineIndex > 0 && !CommentPragmasRegex.match(IndentContent) &&
|
|
|
|
mayReflowContent(Content[LineIndex]) && !Tok.Finalized &&
|
|
|
|
!switchesFormatting(tokenAt(LineIndex)) &&
|
|
|
|
OriginalPrefix[LineIndex] == OriginalPrefix[LineIndex - 1];
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
unsigned
|
|
|
|
BreakableLineCommentSection::getContentStartColumn(unsigned LineIndex,
|
|
|
|
unsigned TailOffset) const {
|
|
|
|
if (TailOffset != 0) {
|
|
|
|
return OriginalContentColumn[LineIndex];
|
|
|
|
}
|
|
|
|
return ContentColumn[LineIndex];
|
2013-04-18 01:34:05 +08:00
|
|
|
}
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|