Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 1 | //===--- BreakableToken.cpp - Format C++ code -----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief Contains implementation of BreakableToken class and classes derived |
| 12 | /// from it. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "format-token-breaker" |
| 17 | |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 18 | #include "BreakableToken.h" |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 19 | #include "clang/Basic/CharInfo.h" |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 20 | #include "clang/Format/Format.h" |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | |
| 25 | namespace clang { |
| 26 | namespace format { |
| 27 | |
Daniel Jasper | 580da27 | 2013-10-30 07:36:40 +0000 | [diff] [blame] | 28 | static const char *const Blanks = " \t\v\f\r"; |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 29 | static bool IsBlank(char C) { |
| 30 | switch (C) { |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 31 | case ' ': |
| 32 | case '\t': |
| 33 | case '\v': |
| 34 | case '\f': |
Daniel Jasper | 580da27 | 2013-10-30 07:36:40 +0000 | [diff] [blame] | 35 | case '\r': |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 36 | return true; |
| 37 | default: |
| 38 | return false; |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
Craig Topper | bfb5c40 | 2013-07-01 03:38:29 +0000 | [diff] [blame] | 42 | static BreakableToken::Split getCommentSplit(StringRef Text, |
| 43 | unsigned ContentStartColumn, |
| 44 | unsigned ColumnLimit, |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 45 | unsigned TabWidth, |
Craig Topper | bfb5c40 | 2013-07-01 03:38:29 +0000 | [diff] [blame] | 46 | encoding::Encoding Encoding) { |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 47 | if (ColumnLimit <= ContentStartColumn + 1) |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 48 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 49 | |
| 50 | unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 51 | unsigned MaxSplitBytes = 0; |
| 52 | |
| 53 | for (unsigned NumChars = 0; |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 54 | NumChars < MaxSplit && MaxSplitBytes < Text.size();) { |
| 55 | unsigned BytesInChar = |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 56 | encoding::getCodePointNumBytes(Text[MaxSplitBytes], Encoding); |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 57 | NumChars += |
| 58 | encoding::columnWidthWithTabs(Text.substr(MaxSplitBytes, BytesInChar), |
| 59 | ContentStartColumn, TabWidth, Encoding); |
| 60 | MaxSplitBytes += BytesInChar; |
| 61 | } |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 62 | |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 63 | StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 64 | if (SpaceOffset == StringRef::npos || |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 65 | // Don't break at leading whitespace. |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 66 | Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos) { |
Manuel Klimek | ae1fbfb | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 67 | // Make sure that we don't break at leading whitespace that |
| 68 | // reaches past MaxSplit. |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 69 | StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(Blanks); |
Manuel Klimek | ae1fbfb | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 70 | if (FirstNonWhitespace == StringRef::npos) |
| 71 | // If the comment is only whitespace, we cannot split. |
| 72 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 73 | SpaceOffset = Text.find_first_of( |
| 74 | Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace)); |
Manuel Klimek | ae1fbfb | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 75 | } |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 76 | if (SpaceOffset != StringRef::npos && SpaceOffset != 0) { |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 77 | StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks); |
| 78 | StringRef AfterCut = Text.substr(SpaceOffset).ltrim(Blanks); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 79 | return BreakableToken::Split(BeforeCut.size(), |
| 80 | AfterCut.begin() - BeforeCut.end()); |
| 81 | } |
| 82 | return BreakableToken::Split(StringRef::npos, 0); |
| 83 | } |
| 84 | |
Craig Topper | bfb5c40 | 2013-07-01 03:38:29 +0000 | [diff] [blame] | 85 | static BreakableToken::Split getStringSplit(StringRef Text, |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 86 | unsigned UsedColumns, |
Craig Topper | bfb5c40 | 2013-07-01 03:38:29 +0000 | [diff] [blame] | 87 | unsigned ColumnLimit, |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 88 | unsigned TabWidth, |
Craig Topper | bfb5c40 | 2013-07-01 03:38:29 +0000 | [diff] [blame] | 89 | encoding::Encoding Encoding) { |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 90 | // FIXME: Reduce unit test case. |
| 91 | if (Text.empty()) |
| 92 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 93 | if (ColumnLimit <= UsedColumns) |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 94 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 71d95d6 | 2013-11-26 10:38:53 +0000 | [diff] [blame^] | 95 | unsigned MaxSplit = ColumnLimit - UsedColumns; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 96 | StringRef::size_type SpaceOffset = 0; |
| 97 | StringRef::size_type SlashOffset = 0; |
Alexander Kornienko | 7285207 | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 98 | StringRef::size_type WordStartOffset = 0; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 99 | StringRef::size_type SplitPoint = 0; |
| 100 | for (unsigned Chars = 0;;) { |
| 101 | unsigned Advance; |
| 102 | if (Text[0] == '\\') { |
| 103 | Advance = encoding::getEscapeSequenceLength(Text); |
| 104 | Chars += Advance; |
| 105 | } else { |
| 106 | Advance = encoding::getCodePointNumBytes(Text[0], Encoding); |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 107 | Chars += encoding::columnWidthWithTabs( |
| 108 | Text.substr(0, Advance), UsedColumns + Chars, TabWidth, Encoding); |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Alexander Kornienko | 71d95d6 | 2013-11-26 10:38:53 +0000 | [diff] [blame^] | 111 | if (Chars > MaxSplit || Text.size() == Advance) |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 112 | break; |
| 113 | |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 114 | if (IsBlank(Text[0])) |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 115 | SpaceOffset = SplitPoint; |
| 116 | if (Text[0] == '/') |
| 117 | SlashOffset = SplitPoint; |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 118 | if (Advance == 1 && !isAlphanumeric(Text[0])) |
Alexander Kornienko | 7285207 | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 119 | WordStartOffset = SplitPoint; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 120 | |
| 121 | SplitPoint += Advance; |
| 122 | Text = Text.substr(Advance); |
| 123 | } |
| 124 | |
| 125 | if (SpaceOffset != 0) |
| 126 | return BreakableToken::Split(SpaceOffset + 1, 0); |
| 127 | if (SlashOffset != 0) |
| 128 | return BreakableToken::Split(SlashOffset + 1, 0); |
Alexander Kornienko | 7285207 | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 129 | if (WordStartOffset != 0) |
| 130 | return BreakableToken::Split(WordStartOffset + 1, 0); |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 131 | if (SplitPoint != 0) |
| 132 | return BreakableToken::Split(SplitPoint, 0); |
| 133 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 136 | unsigned BreakableSingleLineToken::getLineCount() const { return 1; } |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 137 | |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 138 | unsigned BreakableSingleLineToken::getLineLengthAfterSplit( |
| 139 | unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const { |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 140 | return StartColumn + Prefix.size() + Postfix.size() + |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 141 | encoding::columnWidthWithTabs(Line.substr(Offset, Length), |
| 142 | StartColumn + Prefix.size(), |
| 143 | Style.TabWidth, Encoding); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 146 | BreakableSingleLineToken::BreakableSingleLineToken( |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 147 | const FormatToken &Tok, unsigned IndentLevel, unsigned StartColumn, |
| 148 | StringRef Prefix, StringRef Postfix, bool InPPDirective, |
| 149 | encoding::Encoding Encoding, const FormatStyle &Style) |
| 150 | : BreakableToken(Tok, IndentLevel, InPPDirective, Encoding, Style), |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 151 | StartColumn(StartColumn), Prefix(Prefix), Postfix(Postfix) { |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 152 | assert(Tok.TokenText.startswith(Prefix) && Tok.TokenText.endswith(Postfix)); |
| 153 | Line = Tok.TokenText.substr( |
| 154 | Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size()); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 157 | BreakableStringLiteral::BreakableStringLiteral( |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 158 | const FormatToken &Tok, unsigned IndentLevel, unsigned StartColumn, |
| 159 | StringRef Prefix, StringRef Postfix, bool InPPDirective, |
| 160 | encoding::Encoding Encoding, const FormatStyle &Style) |
| 161 | : BreakableSingleLineToken(Tok, IndentLevel, StartColumn, Prefix, Postfix, |
| 162 | InPPDirective, Encoding, Style) {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 163 | |
| 164 | BreakableToken::Split |
| 165 | BreakableStringLiteral::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 166 | unsigned ColumnLimit) const { |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 167 | return getStringSplit(Line.substr(TailOffset), |
| 168 | StartColumn + Prefix.size() + Postfix.size(), |
| 169 | ColumnLimit, Style.TabWidth, Encoding); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 172 | void BreakableStringLiteral::insertBreak(unsigned LineIndex, |
| 173 | unsigned TailOffset, Split Split, |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 174 | WhitespaceManager &Whitespaces) { |
| 175 | Whitespaces.replaceWhitespaceInToken( |
| 176 | Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix, |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 177 | Prefix, InPPDirective, 1, IndentLevel, StartColumn); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 180 | static StringRef getLineCommentPrefix(StringRef Comment) { |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 181 | static const char *const KnownPrefixes[] = { "/// ", "///", "// ", "//" }; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 182 | for (size_t i = 0, e = llvm::array_lengthof(KnownPrefixes); i != e; ++i) |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 183 | if (Comment.startswith(KnownPrefixes[i])) |
| 184 | return KnownPrefixes[i]; |
| 185 | return ""; |
| 186 | } |
| 187 | |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 188 | BreakableLineComment::BreakableLineComment( |
| 189 | const FormatToken &Token, unsigned IndentLevel, unsigned StartColumn, |
| 190 | bool InPPDirective, encoding::Encoding Encoding, const FormatStyle &Style) |
| 191 | : BreakableSingleLineToken(Token, IndentLevel, StartColumn, |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 192 | getLineCommentPrefix(Token.TokenText), "", |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 193 | InPPDirective, Encoding, Style) { |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 194 | OriginalPrefix = Prefix; |
| 195 | if (Token.TokenText.size() > Prefix.size() && |
| 196 | isAlphanumeric(Token.TokenText[Prefix.size()])) { |
| 197 | if (Prefix == "//") |
| 198 | Prefix = "// "; |
| 199 | else if (Prefix == "///") |
| 200 | Prefix = "/// "; |
| 201 | } |
| 202 | } |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 203 | |
| 204 | BreakableToken::Split |
| 205 | BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 206 | unsigned ColumnLimit) const { |
| 207 | return getCommentSplit(Line.substr(TailOffset), StartColumn + Prefix.size(), |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 208 | ColumnLimit, Style.TabWidth, Encoding); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 211 | void BreakableLineComment::insertBreak(unsigned LineIndex, unsigned TailOffset, |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 212 | Split Split, |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 213 | WhitespaceManager &Whitespaces) { |
| 214 | Whitespaces.replaceWhitespaceInToken( |
| 215 | Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second, |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 216 | Postfix, Prefix, InPPDirective, /*Newlines=*/1, IndentLevel, StartColumn); |
| 217 | } |
| 218 | |
| 219 | void BreakableLineComment::replaceWhitespace(unsigned LineIndex, |
| 220 | unsigned TailOffset, Split Split, |
| 221 | WhitespaceManager &Whitespaces) { |
| 222 | Whitespaces.replaceWhitespaceInToken( |
| 223 | Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second, "", |
| 224 | "", /*InPPDirective=*/false, /*Newlines=*/0, /*IndentLevel=*/0, |
| 225 | /*Spaces=*/1); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
| 229 | BreakableLineComment::replaceWhitespaceBefore(unsigned LineIndex, |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 230 | WhitespaceManager &Whitespaces) { |
| 231 | if (OriginalPrefix != Prefix) { |
| 232 | Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "", |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 233 | /*InPPDirective=*/false, |
| 234 | /*Newlines=*/0, /*IndentLevel=*/0, |
| 235 | /*Spaces=*/1); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 239 | BreakableBlockComment::BreakableBlockComment( |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 240 | const FormatToken &Token, unsigned IndentLevel, unsigned StartColumn, |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 241 | unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective, |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 242 | encoding::Encoding Encoding, const FormatStyle &Style) |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 243 | : BreakableToken(Token, IndentLevel, InPPDirective, Encoding, Style) { |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 244 | StringRef TokenText(Token.TokenText); |
| 245 | assert(TokenText.startswith("/*") && TokenText.endswith("*/")); |
| 246 | TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n"); |
| 247 | |
| 248 | int IndentDelta = StartColumn - OriginalStartColumn; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 249 | LeadingWhitespace.resize(Lines.size()); |
| 250 | StartOfLineColumn.resize(Lines.size()); |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 251 | StartOfLineColumn[0] = StartColumn + 2; |
| 252 | for (size_t i = 1; i < Lines.size(); ++i) |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 253 | adjustWhitespace(i, IndentDelta); |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 254 | |
| 255 | Decoration = "* "; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 256 | if (Lines.size() == 1 && !FirstInLine) { |
| 257 | // Comments for which FirstInLine is false can start on arbitrary column, |
| 258 | // and available horizontal space can be too small to align consecutive |
| 259 | // lines with the first one. |
| 260 | // FIXME: We could, probably, align them to current indentation level, but |
| 261 | // now we just wrap them without stars. |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 262 | Decoration = ""; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 263 | } |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 264 | for (size_t i = 1, e = Lines.size(); i < e && !Decoration.empty(); ++i) { |
| 265 | // If the last line is empty, the closing "*/" will have a star. |
| 266 | if (i + 1 == e && Lines[i].empty()) |
| 267 | break; |
| 268 | while (!Lines[i].startswith(Decoration)) |
| 269 | Decoration = Decoration.substr(0, Decoration.size() - 1); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 270 | } |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 271 | |
| 272 | LastLineNeedsDecoration = true; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 273 | IndentAtLineBreak = StartOfLineColumn[0] + 1; |
| 274 | for (size_t i = 1; i < Lines.size(); ++i) { |
| 275 | if (Lines[i].empty()) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 276 | if (i + 1 == Lines.size()) { |
| 277 | // Empty last line means that we already have a star as a part of the |
| 278 | // trailing */. We also need to preserve whitespace, so that */ is |
| 279 | // correctly indented. |
| 280 | LastLineNeedsDecoration = false; |
| 281 | } else if (Decoration.empty()) { |
| 282 | // For all other lines, set the start column to 0 if they're empty, so |
| 283 | // we do not insert trailing whitespace anywhere. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 284 | StartOfLineColumn[i] = 0; |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 285 | } |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 286 | continue; |
| 287 | } |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 288 | // The first line already excludes the star. |
| 289 | // For all other lines, adjust the line to exclude the star and |
| 290 | // (optionally) the first whitespace. |
| 291 | StartOfLineColumn[i] += Decoration.size(); |
| 292 | Lines[i] = Lines[i].substr(Decoration.size()); |
| 293 | LeadingWhitespace[i] += Decoration.size(); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 294 | IndentAtLineBreak = std::min<int>(IndentAtLineBreak, StartOfLineColumn[i]); |
| 295 | } |
Daniel Jasper | ce257f2 | 2013-05-30 17:27:48 +0000 | [diff] [blame] | 296 | IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size()); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 297 | DEBUG({ |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 298 | llvm::dbgs() << "IndentAtLineBreak " << IndentAtLineBreak << "\n"; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 299 | for (size_t i = 0; i < Lines.size(); ++i) { |
| 300 | llvm::dbgs() << i << " |" << Lines[i] << "| " << LeadingWhitespace[i] |
| 301 | << "\n"; |
| 302 | } |
| 303 | }); |
| 304 | } |
| 305 | |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 306 | void BreakableBlockComment::adjustWhitespace(unsigned LineIndex, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 307 | int IndentDelta) { |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 308 | // When in a preprocessor directive, the trailing backslash in a block comment |
| 309 | // is not needed, but can serve a purpose of uniformity with necessary escaped |
| 310 | // newlines outside the comment. In this case we remove it here before |
| 311 | // trimming the trailing whitespace. The backslash will be re-added later when |
| 312 | // inserting a line break. |
| 313 | size_t EndOfPreviousLine = Lines[LineIndex - 1].size(); |
| 314 | if (InPPDirective && Lines[LineIndex - 1].endswith("\\")) |
| 315 | --EndOfPreviousLine; |
| 316 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 317 | // Calculate the end of the non-whitespace text in the previous line. |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 318 | EndOfPreviousLine = |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 319 | Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 320 | if (EndOfPreviousLine == StringRef::npos) |
| 321 | EndOfPreviousLine = 0; |
| 322 | else |
| 323 | ++EndOfPreviousLine; |
| 324 | // Calculate the start of the non-whitespace text in the current line. |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 325 | size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 326 | if (StartOfLine == StringRef::npos) |
| 327 | StartOfLine = Lines[LineIndex].size(); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 328 | |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 329 | StringRef Whitespace = Lines[LineIndex].substr(0, StartOfLine); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 330 | // Adjust Lines to only contain relevant text. |
| 331 | Lines[LineIndex - 1] = Lines[LineIndex - 1].substr(0, EndOfPreviousLine); |
| 332 | Lines[LineIndex] = Lines[LineIndex].substr(StartOfLine); |
| 333 | // Adjust LeadingWhitespace to account all whitespace between the lines |
| 334 | // to the current line. |
| 335 | LeadingWhitespace[LineIndex] = |
| 336 | Lines[LineIndex].begin() - Lines[LineIndex - 1].end(); |
Manuel Klimek | 34d1515 | 2013-05-28 10:01:59 +0000 | [diff] [blame] | 337 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 338 | // Adjust the start column uniformly accross all lines. |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 339 | StartOfLineColumn[LineIndex] = std::max<int>( |
| 340 | 0, |
| 341 | encoding::columnWidthWithTabs(Whitespace, 0, Style.TabWidth, Encoding) + |
| 342 | IndentDelta); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | unsigned BreakableBlockComment::getLineCount() const { return Lines.size(); } |
| 346 | |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 347 | unsigned BreakableBlockComment::getLineLengthAfterSplit( |
| 348 | unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const { |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 349 | unsigned ContentStartColumn = getContentStartColumn(LineIndex, Offset); |
| 350 | return ContentStartColumn + |
| 351 | encoding::columnWidthWithTabs(Lines[LineIndex].substr(Offset, Length), |
| 352 | ContentStartColumn, Style.TabWidth, |
| 353 | Encoding) + |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 354 | // The last line gets a "*/" postfix. |
| 355 | (LineIndex + 1 == Lines.size() ? 2 : 0); |
| 356 | } |
| 357 | |
| 358 | BreakableToken::Split |
| 359 | BreakableBlockComment::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 360 | unsigned ColumnLimit) const { |
| 361 | return getCommentSplit(Lines[LineIndex].substr(TailOffset), |
| 362 | getContentStartColumn(LineIndex, TailOffset), |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 363 | ColumnLimit, Style.TabWidth, Encoding); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset, |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 367 | Split Split, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 368 | WhitespaceManager &Whitespaces) { |
| 369 | StringRef Text = Lines[LineIndex].substr(TailOffset); |
| 370 | StringRef Prefix = Decoration; |
| 371 | if (LineIndex + 1 == Lines.size() && |
| 372 | Text.size() == Split.first + Split.second) { |
| 373 | // For the last line we need to break before "*/", but not to add "* ". |
| 374 | Prefix = ""; |
| 375 | } |
| 376 | |
| 377 | unsigned BreakOffsetInToken = |
| 378 | Text.data() - Tok.TokenText.data() + Split.first; |
| 379 | unsigned CharsToRemove = Split.second; |
Manuel Klimek | 8910d19 | 2013-05-30 07:45:53 +0000 | [diff] [blame] | 380 | assert(IndentAtLineBreak >= Decoration.size()); |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 381 | Whitespaces.replaceWhitespaceInToken( |
| 382 | Tok, BreakOffsetInToken, CharsToRemove, "", Prefix, InPPDirective, 1, |
| 383 | IndentLevel, IndentAtLineBreak - Decoration.size()); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 386 | void BreakableBlockComment::replaceWhitespace(unsigned LineIndex, |
| 387 | unsigned TailOffset, Split Split, |
| 388 | WhitespaceManager &Whitespaces) { |
| 389 | StringRef Text = Lines[LineIndex].substr(TailOffset); |
| 390 | unsigned BreakOffsetInToken = |
| 391 | Text.data() - Tok.TokenText.data() + Split.first; |
| 392 | unsigned CharsToRemove = Split.second; |
| 393 | Whitespaces.replaceWhitespaceInToken( |
| 394 | Tok, BreakOffsetInToken, CharsToRemove, "", "", /*InPPDirective=*/false, |
| 395 | /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1); |
| 396 | } |
| 397 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 398 | void |
| 399 | BreakableBlockComment::replaceWhitespaceBefore(unsigned LineIndex, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 400 | WhitespaceManager &Whitespaces) { |
| 401 | if (LineIndex == 0) |
| 402 | return; |
| 403 | StringRef Prefix = Decoration; |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 404 | if (Lines[LineIndex].empty()) { |
| 405 | if (LineIndex + 1 == Lines.size()) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 406 | if (!LastLineNeedsDecoration) { |
| 407 | // If the last line was empty, we don't need a prefix, as the */ will |
| 408 | // line up with the decoration (if it exists). |
| 409 | Prefix = ""; |
| 410 | } |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 411 | } else if (!Decoration.empty()) { |
| 412 | // For other empty lines, if we do have a decoration, adapt it to not |
| 413 | // contain a trailing whitespace. |
| 414 | Prefix = Prefix.substr(0, 1); |
| 415 | } |
Daniel Jasper | 51fb2b2 | 2013-05-30 06:40:07 +0000 | [diff] [blame] | 416 | } else { |
| 417 | if (StartOfLineColumn[LineIndex] == 1) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 418 | // This line starts immediately after the decorating *. |
Daniel Jasper | 51fb2b2 | 2013-05-30 06:40:07 +0000 | [diff] [blame] | 419 | Prefix = Prefix.substr(0, 1); |
| 420 | } |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 421 | } |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 422 | |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 423 | unsigned WhitespaceOffsetInToken = Lines[LineIndex].data() - |
| 424 | Tok.TokenText.data() - |
| 425 | LeadingWhitespace[LineIndex]; |
Manuel Klimek | 8910d19 | 2013-05-30 07:45:53 +0000 | [diff] [blame] | 426 | assert(StartOfLineColumn[LineIndex] >= Prefix.size()); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 427 | Whitespaces.replaceWhitespaceInToken( |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 428 | Tok, WhitespaceOffsetInToken, LeadingWhitespace[LineIndex], "", Prefix, |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 429 | InPPDirective, 1, IndentLevel, |
| 430 | StartOfLineColumn[LineIndex] - Prefix.size()); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | unsigned |
| 434 | BreakableBlockComment::getContentStartColumn(unsigned LineIndex, |
| 435 | unsigned TailOffset) const { |
| 436 | // If we break, we always break at the predefined indent. |
| 437 | if (TailOffset != 0) |
| 438 | return IndentAtLineBreak; |
| 439 | return StartOfLineColumn[LineIndex]; |
| 440 | } |
| 441 | |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 442 | } // namespace format |
| 443 | } // namespace clang |