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