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