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