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) { |
Daniel Jasper | 174b012 | 2014-01-09 14:18:12 +0000 | [diff] [blame] | 152 | assert(Tok.TokenText.endswith(Postfix)); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 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) { |
Daniel Jasper | d07c2ee | 2014-01-14 09:53:07 +0000 | [diff] [blame^] | 175 | unsigned LeadingSpaces = StartColumn; |
| 176 | // The '@' of an ObjC string literal (@"Test") does not become part of the |
| 177 | // string token. |
| 178 | // FIXME: It might be a cleaner solution to merge the tokens as a |
| 179 | // precomputation step. |
| 180 | if (Prefix.startswith("@")) |
| 181 | --LeadingSpaces; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 182 | Whitespaces.replaceWhitespaceInToken( |
| 183 | Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix, |
Daniel Jasper | d07c2ee | 2014-01-14 09:53:07 +0000 | [diff] [blame^] | 184 | Prefix, InPPDirective, 1, IndentLevel, LeadingSpaces); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 187 | static StringRef getLineCommentPrefix(StringRef Comment) { |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 188 | static const char *const KnownPrefixes[] = { "/// ", "///", "// ", "//" }; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 189 | 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] | 190 | if (Comment.startswith(KnownPrefixes[i])) |
| 191 | return KnownPrefixes[i]; |
| 192 | return ""; |
| 193 | } |
| 194 | |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 195 | BreakableLineComment::BreakableLineComment( |
| 196 | const FormatToken &Token, unsigned IndentLevel, unsigned StartColumn, |
| 197 | bool InPPDirective, encoding::Encoding Encoding, const FormatStyle &Style) |
| 198 | : BreakableSingleLineToken(Token, IndentLevel, StartColumn, |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 199 | getLineCommentPrefix(Token.TokenText), "", |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 200 | InPPDirective, Encoding, Style) { |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 201 | OriginalPrefix = Prefix; |
| 202 | if (Token.TokenText.size() > Prefix.size() && |
| 203 | isAlphanumeric(Token.TokenText[Prefix.size()])) { |
| 204 | if (Prefix == "//") |
| 205 | Prefix = "// "; |
| 206 | else if (Prefix == "///") |
| 207 | Prefix = "/// "; |
| 208 | } |
| 209 | } |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 210 | |
| 211 | BreakableToken::Split |
| 212 | BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 213 | unsigned ColumnLimit) const { |
| 214 | return getCommentSplit(Line.substr(TailOffset), StartColumn + Prefix.size(), |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 215 | ColumnLimit, Style.TabWidth, Encoding); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 218 | void BreakableLineComment::insertBreak(unsigned LineIndex, unsigned TailOffset, |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 219 | Split Split, |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 220 | WhitespaceManager &Whitespaces) { |
| 221 | Whitespaces.replaceWhitespaceInToken( |
| 222 | Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second, |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 223 | Postfix, Prefix, InPPDirective, /*Newlines=*/1, IndentLevel, StartColumn); |
| 224 | } |
| 225 | |
| 226 | void BreakableLineComment::replaceWhitespace(unsigned LineIndex, |
| 227 | unsigned TailOffset, Split Split, |
| 228 | WhitespaceManager &Whitespaces) { |
| 229 | Whitespaces.replaceWhitespaceInToken( |
| 230 | Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second, "", |
| 231 | "", /*InPPDirective=*/false, /*Newlines=*/0, /*IndentLevel=*/0, |
| 232 | /*Spaces=*/1); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void |
| 236 | BreakableLineComment::replaceWhitespaceBefore(unsigned LineIndex, |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 237 | WhitespaceManager &Whitespaces) { |
| 238 | if (OriginalPrefix != Prefix) { |
| 239 | Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "", |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 240 | /*InPPDirective=*/false, |
| 241 | /*Newlines=*/0, /*IndentLevel=*/0, |
| 242 | /*Spaces=*/1); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 246 | BreakableBlockComment::BreakableBlockComment( |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 247 | const FormatToken &Token, unsigned IndentLevel, unsigned StartColumn, |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 248 | unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective, |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 249 | encoding::Encoding Encoding, const FormatStyle &Style) |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 250 | : BreakableToken(Token, IndentLevel, InPPDirective, Encoding, Style) { |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 251 | StringRef TokenText(Token.TokenText); |
| 252 | assert(TokenText.startswith("/*") && TokenText.endswith("*/")); |
| 253 | TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n"); |
| 254 | |
| 255 | int IndentDelta = StartColumn - OriginalStartColumn; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 256 | LeadingWhitespace.resize(Lines.size()); |
| 257 | StartOfLineColumn.resize(Lines.size()); |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 258 | StartOfLineColumn[0] = StartColumn + 2; |
| 259 | for (size_t i = 1; i < Lines.size(); ++i) |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 260 | adjustWhitespace(i, IndentDelta); |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 261 | |
| 262 | Decoration = "* "; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 263 | if (Lines.size() == 1 && !FirstInLine) { |
| 264 | // Comments for which FirstInLine is false can start on arbitrary column, |
| 265 | // and available horizontal space can be too small to align consecutive |
| 266 | // lines with the first one. |
| 267 | // FIXME: We could, probably, align them to current indentation level, but |
| 268 | // now we just wrap them without stars. |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 269 | Decoration = ""; |
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 | for (size_t i = 1, e = Lines.size(); i < e && !Decoration.empty(); ++i) { |
| 272 | // If the last line is empty, the closing "*/" will have a star. |
| 273 | if (i + 1 == e && Lines[i].empty()) |
| 274 | break; |
| 275 | while (!Lines[i].startswith(Decoration)) |
| 276 | Decoration = Decoration.substr(0, Decoration.size() - 1); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 277 | } |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 278 | |
| 279 | LastLineNeedsDecoration = true; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 280 | IndentAtLineBreak = StartOfLineColumn[0] + 1; |
| 281 | for (size_t i = 1; i < Lines.size(); ++i) { |
| 282 | if (Lines[i].empty()) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 283 | if (i + 1 == Lines.size()) { |
| 284 | // Empty last line means that we already have a star as a part of the |
| 285 | // trailing */. We also need to preserve whitespace, so that */ is |
| 286 | // correctly indented. |
| 287 | LastLineNeedsDecoration = false; |
| 288 | } else if (Decoration.empty()) { |
| 289 | // For all other lines, set the start column to 0 if they're empty, so |
| 290 | // we do not insert trailing whitespace anywhere. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 291 | StartOfLineColumn[i] = 0; |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 292 | } |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 293 | continue; |
| 294 | } |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 295 | // The first line already excludes the star. |
| 296 | // For all other lines, adjust the line to exclude the star and |
| 297 | // (optionally) the first whitespace. |
| 298 | StartOfLineColumn[i] += Decoration.size(); |
| 299 | Lines[i] = Lines[i].substr(Decoration.size()); |
| 300 | LeadingWhitespace[i] += Decoration.size(); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 301 | IndentAtLineBreak = std::min<int>(IndentAtLineBreak, StartOfLineColumn[i]); |
| 302 | } |
Daniel Jasper | ce257f2 | 2013-05-30 17:27:48 +0000 | [diff] [blame] | 303 | IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size()); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 304 | DEBUG({ |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 305 | llvm::dbgs() << "IndentAtLineBreak " << IndentAtLineBreak << "\n"; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 306 | for (size_t i = 0; i < Lines.size(); ++i) { |
| 307 | llvm::dbgs() << i << " |" << Lines[i] << "| " << LeadingWhitespace[i] |
| 308 | << "\n"; |
| 309 | } |
| 310 | }); |
| 311 | } |
| 312 | |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 313 | void BreakableBlockComment::adjustWhitespace(unsigned LineIndex, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 314 | int IndentDelta) { |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 315 | // When in a preprocessor directive, the trailing backslash in a block comment |
| 316 | // is not needed, but can serve a purpose of uniformity with necessary escaped |
| 317 | // newlines outside the comment. In this case we remove it here before |
| 318 | // trimming the trailing whitespace. The backslash will be re-added later when |
| 319 | // inserting a line break. |
| 320 | size_t EndOfPreviousLine = Lines[LineIndex - 1].size(); |
| 321 | if (InPPDirective && Lines[LineIndex - 1].endswith("\\")) |
| 322 | --EndOfPreviousLine; |
| 323 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 324 | // Calculate the end of the non-whitespace text in the previous line. |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 325 | EndOfPreviousLine = |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 326 | Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 327 | if (EndOfPreviousLine == StringRef::npos) |
| 328 | EndOfPreviousLine = 0; |
| 329 | else |
| 330 | ++EndOfPreviousLine; |
| 331 | // Calculate the start of the non-whitespace text in the current line. |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 332 | size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 333 | if (StartOfLine == StringRef::npos) |
| 334 | StartOfLine = Lines[LineIndex].size(); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 335 | |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 336 | StringRef Whitespace = Lines[LineIndex].substr(0, StartOfLine); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 337 | // Adjust Lines to only contain relevant text. |
| 338 | Lines[LineIndex - 1] = Lines[LineIndex - 1].substr(0, EndOfPreviousLine); |
| 339 | Lines[LineIndex] = Lines[LineIndex].substr(StartOfLine); |
| 340 | // Adjust LeadingWhitespace to account all whitespace between the lines |
| 341 | // to the current line. |
| 342 | LeadingWhitespace[LineIndex] = |
| 343 | Lines[LineIndex].begin() - Lines[LineIndex - 1].end(); |
Manuel Klimek | 34d1515 | 2013-05-28 10:01:59 +0000 | [diff] [blame] | 344 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 345 | // Adjust the start column uniformly across all lines. |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 346 | StartOfLineColumn[LineIndex] = std::max<int>( |
| 347 | 0, |
| 348 | encoding::columnWidthWithTabs(Whitespace, 0, Style.TabWidth, Encoding) + |
| 349 | IndentDelta); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | unsigned BreakableBlockComment::getLineCount() const { return Lines.size(); } |
| 353 | |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 354 | unsigned BreakableBlockComment::getLineLengthAfterSplit( |
| 355 | unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const { |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 356 | unsigned ContentStartColumn = getContentStartColumn(LineIndex, Offset); |
| 357 | return ContentStartColumn + |
| 358 | encoding::columnWidthWithTabs(Lines[LineIndex].substr(Offset, Length), |
| 359 | ContentStartColumn, Style.TabWidth, |
| 360 | Encoding) + |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 361 | // The last line gets a "*/" postfix. |
| 362 | (LineIndex + 1 == Lines.size() ? 2 : 0); |
| 363 | } |
| 364 | |
| 365 | BreakableToken::Split |
| 366 | BreakableBlockComment::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 367 | unsigned ColumnLimit) const { |
| 368 | return getCommentSplit(Lines[LineIndex].substr(TailOffset), |
| 369 | getContentStartColumn(LineIndex, TailOffset), |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 370 | ColumnLimit, Style.TabWidth, Encoding); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset, |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 374 | Split Split, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 375 | WhitespaceManager &Whitespaces) { |
| 376 | StringRef Text = Lines[LineIndex].substr(TailOffset); |
| 377 | StringRef Prefix = Decoration; |
| 378 | if (LineIndex + 1 == Lines.size() && |
| 379 | Text.size() == Split.first + Split.second) { |
| 380 | // For the last line we need to break before "*/", but not to add "* ". |
| 381 | Prefix = ""; |
| 382 | } |
| 383 | |
| 384 | unsigned BreakOffsetInToken = |
| 385 | Text.data() - Tok.TokenText.data() + Split.first; |
| 386 | unsigned CharsToRemove = Split.second; |
Manuel Klimek | 8910d19 | 2013-05-30 07:45:53 +0000 | [diff] [blame] | 387 | assert(IndentAtLineBreak >= Decoration.size()); |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 388 | Whitespaces.replaceWhitespaceInToken( |
| 389 | Tok, BreakOffsetInToken, CharsToRemove, "", Prefix, InPPDirective, 1, |
| 390 | IndentLevel, IndentAtLineBreak - Decoration.size()); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 393 | void BreakableBlockComment::replaceWhitespace(unsigned LineIndex, |
| 394 | unsigned TailOffset, Split Split, |
| 395 | WhitespaceManager &Whitespaces) { |
| 396 | StringRef Text = Lines[LineIndex].substr(TailOffset); |
| 397 | unsigned BreakOffsetInToken = |
| 398 | Text.data() - Tok.TokenText.data() + Split.first; |
| 399 | unsigned CharsToRemove = Split.second; |
| 400 | Whitespaces.replaceWhitespaceInToken( |
| 401 | Tok, BreakOffsetInToken, CharsToRemove, "", "", /*InPPDirective=*/false, |
| 402 | /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1); |
| 403 | } |
| 404 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 405 | void |
| 406 | BreakableBlockComment::replaceWhitespaceBefore(unsigned LineIndex, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 407 | WhitespaceManager &Whitespaces) { |
| 408 | if (LineIndex == 0) |
| 409 | return; |
| 410 | StringRef Prefix = Decoration; |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 411 | if (Lines[LineIndex].empty()) { |
| 412 | if (LineIndex + 1 == Lines.size()) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 413 | if (!LastLineNeedsDecoration) { |
| 414 | // If the last line was empty, we don't need a prefix, as the */ will |
| 415 | // line up with the decoration (if it exists). |
| 416 | Prefix = ""; |
| 417 | } |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 418 | } else if (!Decoration.empty()) { |
| 419 | // For other empty lines, if we do have a decoration, adapt it to not |
| 420 | // contain a trailing whitespace. |
| 421 | Prefix = Prefix.substr(0, 1); |
| 422 | } |
Daniel Jasper | 51fb2b2 | 2013-05-30 06:40:07 +0000 | [diff] [blame] | 423 | } else { |
| 424 | if (StartOfLineColumn[LineIndex] == 1) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 425 | // This line starts immediately after the decorating *. |
Daniel Jasper | 51fb2b2 | 2013-05-30 06:40:07 +0000 | [diff] [blame] | 426 | Prefix = Prefix.substr(0, 1); |
| 427 | } |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 428 | } |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 429 | |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 430 | unsigned WhitespaceOffsetInToken = Lines[LineIndex].data() - |
| 431 | Tok.TokenText.data() - |
| 432 | LeadingWhitespace[LineIndex]; |
Manuel Klimek | 8910d19 | 2013-05-30 07:45:53 +0000 | [diff] [blame] | 433 | assert(StartOfLineColumn[LineIndex] >= Prefix.size()); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 434 | Whitespaces.replaceWhitespaceInToken( |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 435 | Tok, WhitespaceOffsetInToken, LeadingWhitespace[LineIndex], "", Prefix, |
Alexander Kornienko | 3c3d09c | 2013-09-27 16:14:22 +0000 | [diff] [blame] | 436 | InPPDirective, 1, IndentLevel, |
| 437 | StartOfLineColumn[LineIndex] - Prefix.size()); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | unsigned |
| 441 | BreakableBlockComment::getContentStartColumn(unsigned LineIndex, |
| 442 | unsigned TailOffset) const { |
| 443 | // If we break, we always break at the predefined indent. |
| 444 | if (TailOffset != 0) |
| 445 | return IndentAtLineBreak; |
| 446 | return StartOfLineColumn[LineIndex]; |
| 447 | } |
| 448 | |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 449 | } // namespace format |
| 450 | } // namespace clang |