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.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
#define DEBUG_TYPE "format-token-breaker"
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
#include "BreakableToken.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>
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-07-01 11:38:29 +08:00
|
|
|
static BreakableToken::Split getStringSplit(StringRef Text,
|
2013-09-17 04:20:49 +08:00
|
|
|
unsigned UsedColumns,
|
2013-07-01 11:38:29 +08:00
|
|
|
unsigned ColumnLimit,
|
2013-09-05 22:08:34 +08:00
|
|
|
unsigned TabWidth,
|
2013-07-01 11:38:29 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-11-26 18:38:53 +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
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const {
|
2013-06-05 22:09:10 +08:00
|
|
|
return StartColumn + Prefix.size() + Postfix.size() +
|
2013-09-05 22:08:34 +08:00
|
|
|
encoding::columnWidthWithTabs(Line.substr(Offset, Length),
|
|
|
|
StartColumn + Prefix.size(),
|
|
|
|
Style.TabWidth, Encoding);
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
|
|
|
|
2013-06-14 19:46:10 +08:00
|
|
|
BreakableSingleLineToken::BreakableSingleLineToken(
|
2013-09-28 00:14:22 +08:00
|
|
|
const FormatToken &Tok, unsigned IndentLevel, unsigned StartColumn,
|
|
|
|
StringRef Prefix, StringRef Postfix, bool InPPDirective,
|
|
|
|
encoding::Encoding Encoding, const FormatStyle &Style)
|
|
|
|
: BreakableToken(Tok, IndentLevel, 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(
|
2013-09-28 00:14:22 +08:00
|
|
|
const FormatToken &Tok, unsigned IndentLevel, unsigned StartColumn,
|
|
|
|
StringRef Prefix, StringRef Postfix, bool InPPDirective,
|
|
|
|
encoding::Encoding Encoding, const FormatStyle &Style)
|
|
|
|
: BreakableSingleLineToken(Tok, IndentLevel, 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,
|
2014-01-14 17:53:07 +08:00
|
|
|
Prefix, InPPDirective, 1, IndentLevel, LeadingSpaces);
|
2013-06-12 00:01:49 +08:00
|
|
|
}
|
|
|
|
|
2014-03-10 21:14:56 +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-05-27 23:23:34 +08:00
|
|
|
}
|
|
|
|
|
2013-09-28 00:14:22 +08:00
|
|
|
BreakableLineComment::BreakableLineComment(
|
|
|
|
const FormatToken &Token, unsigned IndentLevel, unsigned StartColumn,
|
|
|
|
bool InPPDirective, encoding::Encoding Encoding, const FormatStyle &Style)
|
|
|
|
: BreakableSingleLineToken(Token, IndentLevel, StartColumn,
|
2014-03-10 21:14:56 +08:00
|
|
|
getLineCommentIndentPrefix(Token.TokenText), "",
|
2013-09-05 22:08:34 +08:00
|
|
|
InPPDirective, Encoding, Style) {
|
2013-06-12 00:01:49 +08:00
|
|
|
OriginalPrefix = Prefix;
|
|
|
|
if (Token.TokenText.size() > Prefix.size() &&
|
|
|
|
isAlphanumeric(Token.TokenText[Prefix.size()])) {
|
|
|
|
if (Prefix == "//")
|
|
|
|
Prefix = "// ";
|
|
|
|
else if (Prefix == "///")
|
|
|
|
Prefix = "/// ";
|
|
|
|
}
|
|
|
|
}
|
2013-05-27 23:23:34 +08:00
|
|
|
|
|
|
|
BreakableToken::Split
|
|
|
|
BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset,
|
|
|
|
unsigned ColumnLimit) const {
|
|
|
|
return getCommentSplit(Line.substr(TailOffset), StartColumn + Prefix.size(),
|
2013-09-05 22:08:34 +08:00
|
|
|
ColumnLimit, Style.TabWidth, Encoding);
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
|
|
|
|
2013-06-12 00:01:49 +08:00
|
|
|
void BreakableLineComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
|
2013-06-14 19:46:10 +08:00
|
|
|
Split Split,
|
2013-06-12 00:01:49 +08:00
|
|
|
WhitespaceManager &Whitespaces) {
|
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
|
|
|
Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second,
|
2013-11-13 01:50:13 +08:00
|
|
|
Postfix, Prefix, InPPDirective, /*Newlines=*/1, IndentLevel, StartColumn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BreakableLineComment::replaceWhitespace(unsigned LineIndex,
|
|
|
|
unsigned TailOffset, Split Split,
|
|
|
|
WhitespaceManager &Whitespaces) {
|
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
|
|
|
Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second, "",
|
|
|
|
"", /*InPPDirective=*/false, /*Newlines=*/0, /*IndentLevel=*/0,
|
|
|
|
/*Spaces=*/1);
|
2013-06-12 00:01:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BreakableLineComment::replaceWhitespaceBefore(unsigned LineIndex,
|
|
|
|
WhitespaceManager &Whitespaces) {
|
|
|
|
if (OriginalPrefix != Prefix) {
|
|
|
|
Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "",
|
2013-11-13 01:50:13 +08:00
|
|
|
/*InPPDirective=*/false,
|
|
|
|
/*Newlines=*/0, /*IndentLevel=*/0,
|
|
|
|
/*Spaces=*/1);
|
2013-06-12 00:01:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 22:09:10 +08:00
|
|
|
BreakableBlockComment::BreakableBlockComment(
|
2013-09-28 00:14:22 +08:00
|
|
|
const FormatToken &Token, unsigned IndentLevel, 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)
|
2013-09-28 00:14:22 +08:00
|
|
|
: BreakableToken(Token, IndentLevel, InPPDirective, Encoding, Style) {
|
2013-05-27 23:23:34 +08:00
|
|
|
StringRef TokenText(Token.TokenText);
|
|
|
|
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;
|
|
|
|
LeadingWhitespace.resize(Lines.size());
|
|
|
|
StartOfLineColumn.resize(Lines.size());
|
2013-07-08 22:12:07 +08:00
|
|
|
StartOfLineColumn[0] = StartColumn + 2;
|
|
|
|
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
|
|
|
|
|
|
|
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.
|
|
|
|
if (i + 1 == e && Lines[i].empty())
|
|
|
|
break;
|
|
|
|
while (!Lines[i].startswith(Decoration))
|
|
|
|
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;
|
2013-05-27 23:23:34 +08:00
|
|
|
IndentAtLineBreak = StartOfLineColumn[0] + 1;
|
|
|
|
for (size_t i = 1; i < Lines.size(); ++i) {
|
|
|
|
if (Lines[i].empty()) {
|
2013-07-08 22:12:07 +08:00
|
|
|
if (i + 1 == Lines.size()) {
|
|
|
|
// 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;
|
|
|
|
} 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.
|
2013-05-27 23:23:34 +08:00
|
|
|
StartOfLineColumn[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
|
|
|
}
|
2013-07-08 22:12:07 +08:00
|
|
|
// The first line already excludes the star.
|
|
|
|
// For all other lines, adjust the line to exclude the star and
|
|
|
|
// (optionally) the first whitespace.
|
|
|
|
StartOfLineColumn[i] += Decoration.size();
|
|
|
|
Lines[i] = Lines[i].substr(Decoration.size());
|
|
|
|
LeadingWhitespace[i] += Decoration.size();
|
2013-05-27 23:23:34 +08:00
|
|
|
IndentAtLineBreak = std::min<int>(IndentAtLineBreak, StartOfLineColumn[i]);
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
2013-05-31 01:27:48 +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) {
|
|
|
|
llvm::dbgs() << i << " |" << Lines[i] << "| " << LeadingWhitespace[i]
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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)
|
|
|
|
StartOfLine = Lines[LineIndex].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.
|
|
|
|
Lines[LineIndex - 1] = Lines[LineIndex - 1].substr(0, EndOfPreviousLine);
|
|
|
|
Lines[LineIndex] = Lines[LineIndex].substr(StartOfLine);
|
|
|
|
// Adjust LeadingWhitespace to account all whitespace between the lines
|
|
|
|
// to the current line.
|
|
|
|
LeadingWhitespace[LineIndex] =
|
|
|
|
Lines[LineIndex].begin() - Lines[LineIndex - 1].end();
|
2013-05-28 18:01:59 +08:00
|
|
|
|
2013-12-06 00:25:25 +08:00
|
|
|
// Adjust the start column uniformly across all lines.
|
2013-09-10 17:38:25 +08:00
|
|
|
StartOfLineColumn[LineIndex] = std::max<int>(
|
|
|
|
0,
|
|
|
|
encoding::columnWidthWithTabs(Whitespace, 0, Style.TabWidth, Encoding) +
|
|
|
|
IndentDelta);
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
unsigned BreakableBlockComment::getLineCount() const { return Lines.size(); }
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-06-08 00:02:52 +08:00
|
|
|
unsigned BreakableBlockComment::getLineLengthAfterSplit(
|
|
|
|
unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const {
|
2013-09-05 22:08:34 +08:00
|
|
|
unsigned ContentStartColumn = getContentStartColumn(LineIndex, Offset);
|
|
|
|
return ContentStartColumn +
|
|
|
|
encoding::columnWidthWithTabs(Lines[LineIndex].substr(Offset, Length),
|
|
|
|
ContentStartColumn, Style.TabWidth,
|
|
|
|
Encoding) +
|
2013-05-27 23:23:34 +08:00
|
|
|
// The last line gets a "*/" postfix.
|
|
|
|
(LineIndex + 1 == Lines.size() ? 2 : 0);
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
BreakableToken::Split
|
|
|
|
BreakableBlockComment::getSplit(unsigned LineIndex, unsigned TailOffset,
|
|
|
|
unsigned ColumnLimit) const {
|
|
|
|
return getCommentSplit(Lines[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
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2013-04-15 22:28:00 +08:00
|
|
|
StringRef Text = Lines[LineIndex].substr(TailOffset);
|
2013-05-27 23:23:34 +08:00
|
|
|
StringRef Prefix = Decoration;
|
|
|
|
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 = "";
|
|
|
|
}
|
2013-05-22 20:51:29 +08:00
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
unsigned BreakOffsetInToken =
|
|
|
|
Text.data() - Tok.TokenText.data() + Split.first;
|
|
|
|
unsigned CharsToRemove = Split.second;
|
2013-05-30 15:45:53 +08:00
|
|
|
assert(IndentAtLineBreak >= Decoration.size());
|
2013-09-28 00:14:22 +08:00
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
|
|
|
Tok, BreakOffsetInToken, CharsToRemove, "", Prefix, InPPDirective, 1,
|
|
|
|
IndentLevel, IndentAtLineBreak - Decoration.size());
|
2013-05-27 23:23:34 +08:00
|
|
|
}
|
|
|
|
|
2013-11-13 01:50:13 +08:00
|
|
|
void BreakableBlockComment::replaceWhitespace(unsigned LineIndex,
|
|
|
|
unsigned TailOffset, Split Split,
|
|
|
|
WhitespaceManager &Whitespaces) {
|
|
|
|
StringRef Text = Lines[LineIndex].substr(TailOffset);
|
|
|
|
unsigned BreakOffsetInToken =
|
|
|
|
Text.data() - Tok.TokenText.data() + Split.first;
|
|
|
|
unsigned CharsToRemove = Split.second;
|
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
|
|
|
Tok, BreakOffsetInToken, CharsToRemove, "", "", /*InPPDirective=*/false,
|
|
|
|
/*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1);
|
|
|
|
}
|
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
void
|
|
|
|
BreakableBlockComment::replaceWhitespaceBefore(unsigned LineIndex,
|
|
|
|
WhitespaceManager &Whitespaces) {
|
|
|
|
if (LineIndex == 0)
|
2013-04-15 22:28:00 +08:00
|
|
|
return;
|
2013-05-27 23:23:34 +08:00
|
|
|
StringRef Prefix = Decoration;
|
2013-05-28 16:55:01 +08:00
|
|
|
if (Lines[LineIndex].empty()) {
|
|
|
|
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 {
|
|
|
|
if (StartOfLineColumn[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
|
|
|
}
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-07-08 22:34:09 +08:00
|
|
|
unsigned WhitespaceOffsetInToken = Lines[LineIndex].data() -
|
|
|
|
Tok.TokenText.data() -
|
|
|
|
LeadingWhitespace[LineIndex];
|
2013-05-30 15:45:53 +08:00
|
|
|
assert(StartOfLineColumn[LineIndex] >= Prefix.size());
|
2013-06-12 00:01:49 +08:00
|
|
|
Whitespaces.replaceWhitespaceInToken(
|
2013-05-27 23:23:34 +08:00
|
|
|
Tok, WhitespaceOffsetInToken, LeadingWhitespace[LineIndex], "", Prefix,
|
2013-09-28 00:14:22 +08:00
|
|
|
InPPDirective, 1, IndentLevel,
|
|
|
|
StartOfLineColumn[LineIndex] - Prefix.size());
|
2013-04-18 01:34:05 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return StartOfLineColumn[LineIndex];
|
2013-04-18 01:34:05 +08:00
|
|
|
}
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|