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 | |
| 16 | #include "BreakableToken.h" |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 17 | #include "ContinuationIndenter.h" |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 18 | #include "clang/Basic/CharInfo.h" |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 19 | #include "clang/Format/Format.h" |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "format-token-breaker" |
| 25 | |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 26 | namespace clang { |
| 27 | namespace format { |
| 28 | |
Daniel Jasper | 580da27 | 2013-10-30 07:36:40 +0000 | [diff] [blame] | 29 | static const char *const Blanks = " \t\v\f\r"; |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 30 | static bool IsBlank(char C) { |
| 31 | switch (C) { |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 32 | case ' ': |
| 33 | case '\t': |
| 34 | case '\v': |
| 35 | case '\f': |
Daniel Jasper | 580da27 | 2013-10-30 07:36:40 +0000 | [diff] [blame] | 36 | case '\r': |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 37 | return true; |
| 38 | default: |
| 39 | return false; |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 43 | static StringRef getLineCommentIndentPrefix(StringRef Comment) { |
| 44 | static const char *const KnownPrefixes[] = {"///", "//", "//!"}; |
| 45 | StringRef LongestPrefix; |
| 46 | for (StringRef KnownPrefix : KnownPrefixes) { |
| 47 | if (Comment.startswith(KnownPrefix)) { |
| 48 | size_t PrefixLength = KnownPrefix.size(); |
| 49 | while (PrefixLength < Comment.size() && Comment[PrefixLength] == ' ') |
| 50 | ++PrefixLength; |
| 51 | if (PrefixLength > LongestPrefix.size()) |
| 52 | LongestPrefix = Comment.substr(0, PrefixLength); |
| 53 | } |
| 54 | } |
| 55 | return LongestPrefix; |
| 56 | } |
| 57 | |
Craig Topper | bfb5c40 | 2013-07-01 03:38:29 +0000 | [diff] [blame] | 58 | static BreakableToken::Split getCommentSplit(StringRef Text, |
| 59 | unsigned ContentStartColumn, |
| 60 | unsigned ColumnLimit, |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 61 | unsigned TabWidth, |
Craig Topper | bfb5c40 | 2013-07-01 03:38:29 +0000 | [diff] [blame] | 62 | encoding::Encoding Encoding) { |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 63 | if (ColumnLimit <= ContentStartColumn + 1) |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 64 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 65 | |
| 66 | unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 67 | unsigned MaxSplitBytes = 0; |
| 68 | |
| 69 | for (unsigned NumChars = 0; |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 70 | NumChars < MaxSplit && MaxSplitBytes < Text.size();) { |
| 71 | unsigned BytesInChar = |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 72 | encoding::getCodePointNumBytes(Text[MaxSplitBytes], Encoding); |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 73 | NumChars += |
| 74 | encoding::columnWidthWithTabs(Text.substr(MaxSplitBytes, BytesInChar), |
| 75 | ContentStartColumn, TabWidth, Encoding); |
| 76 | MaxSplitBytes += BytesInChar; |
| 77 | } |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 78 | |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 79 | StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 80 | if (SpaceOffset == StringRef::npos || |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 81 | // Don't break at leading whitespace. |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 82 | Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos) { |
Manuel Klimek | ae1fbfb | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 83 | // Make sure that we don't break at leading whitespace that |
| 84 | // reaches past MaxSplit. |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 85 | StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(Blanks); |
Manuel Klimek | ae1fbfb | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 86 | if (FirstNonWhitespace == StringRef::npos) |
| 87 | // If the comment is only whitespace, we cannot split. |
| 88 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 89 | SpaceOffset = Text.find_first_of( |
| 90 | Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace)); |
Manuel Klimek | ae1fbfb | 2013-05-29 22:06:18 +0000 | [diff] [blame] | 91 | } |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 92 | if (SpaceOffset != StringRef::npos && SpaceOffset != 0) { |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 93 | StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks); |
| 94 | StringRef AfterCut = Text.substr(SpaceOffset).ltrim(Blanks); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 95 | return BreakableToken::Split(BeforeCut.size(), |
| 96 | AfterCut.begin() - BeforeCut.end()); |
| 97 | } |
| 98 | return BreakableToken::Split(StringRef::npos, 0); |
| 99 | } |
| 100 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 101 | static BreakableToken::Split |
| 102 | getStringSplit(StringRef Text, unsigned UsedColumns, unsigned ColumnLimit, |
| 103 | unsigned TabWidth, encoding::Encoding Encoding) { |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 104 | // FIXME: Reduce unit test case. |
| 105 | if (Text.empty()) |
| 106 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 107 | if (ColumnLimit <= UsedColumns) |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 108 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 71d95d6 | 2013-11-26 10:38:53 +0000 | [diff] [blame] | 109 | unsigned MaxSplit = ColumnLimit - UsedColumns; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 110 | StringRef::size_type SpaceOffset = 0; |
| 111 | StringRef::size_type SlashOffset = 0; |
Alexander Kornienko | 7285207 | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 112 | StringRef::size_type WordStartOffset = 0; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 113 | StringRef::size_type SplitPoint = 0; |
| 114 | for (unsigned Chars = 0;;) { |
| 115 | unsigned Advance; |
| 116 | if (Text[0] == '\\') { |
| 117 | Advance = encoding::getEscapeSequenceLength(Text); |
| 118 | Chars += Advance; |
| 119 | } else { |
| 120 | Advance = encoding::getCodePointNumBytes(Text[0], Encoding); |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 121 | Chars += encoding::columnWidthWithTabs( |
| 122 | Text.substr(0, Advance), UsedColumns + Chars, TabWidth, Encoding); |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Daniel Jasper | e4b48c6 | 2015-01-21 19:50:35 +0000 | [diff] [blame] | 125 | if (Chars > MaxSplit || Text.size() <= Advance) |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 126 | break; |
| 127 | |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 128 | if (IsBlank(Text[0])) |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 129 | SpaceOffset = SplitPoint; |
| 130 | if (Text[0] == '/') |
| 131 | SlashOffset = SplitPoint; |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 132 | if (Advance == 1 && !isAlphanumeric(Text[0])) |
Alexander Kornienko | 7285207 | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 133 | WordStartOffset = SplitPoint; |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 134 | |
| 135 | SplitPoint += Advance; |
| 136 | Text = Text.substr(Advance); |
| 137 | } |
| 138 | |
| 139 | if (SpaceOffset != 0) |
| 140 | return BreakableToken::Split(SpaceOffset + 1, 0); |
| 141 | if (SlashOffset != 0) |
| 142 | return BreakableToken::Split(SlashOffset + 1, 0); |
Alexander Kornienko | 7285207 | 2013-06-19 14:22:47 +0000 | [diff] [blame] | 143 | if (WordStartOffset != 0) |
| 144 | return BreakableToken::Split(WordStartOffset + 1, 0); |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 145 | if (SplitPoint != 0) |
| 146 | return BreakableToken::Split(SplitPoint, 0); |
| 147 | return BreakableToken::Split(StringRef::npos, 0); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 150 | bool switchesFormatting(const FormatToken &Token) { |
| 151 | assert((Token.is(TT_BlockComment) || Token.is(TT_LineComment)) && |
| 152 | "formatting regions are switched by comment tokens"); |
| 153 | StringRef Content = Token.TokenText.substr(2).ltrim(); |
| 154 | return Content.startswith("clang-format on") || |
| 155 | Content.startswith("clang-format off"); |
| 156 | } |
| 157 | |
| 158 | unsigned |
| 159 | BreakableToken::getLineLengthAfterCompression(unsigned RemainingTokenColumns, |
| 160 | Split Split) const { |
| 161 | // Example: consider the content |
| 162 | // lala lala |
| 163 | // - RemainingTokenColumns is the original number of columns, 10; |
| 164 | // - Split is (4, 2), denoting the two spaces between the two words; |
| 165 | // |
| 166 | // We compute the number of columns when the split is compressed into a single |
| 167 | // space, like: |
| 168 | // lala lala |
| 169 | return RemainingTokenColumns + 1 - Split.second; |
| 170 | } |
| 171 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 172 | unsigned BreakableSingleLineToken::getLineCount() const { return 1; } |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 173 | |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 174 | unsigned BreakableSingleLineToken::getLineLengthAfterSplit( |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 175 | unsigned LineIndex, unsigned TailOffset, |
| 176 | StringRef::size_type Length) const { |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 177 | return StartColumn + Prefix.size() + Postfix.size() + |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 178 | encoding::columnWidthWithTabs(Line.substr(TailOffset, Length), |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 179 | StartColumn + Prefix.size(), |
| 180 | Style.TabWidth, Encoding); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 183 | BreakableSingleLineToken::BreakableSingleLineToken( |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 184 | const FormatToken &Tok, unsigned StartColumn, StringRef Prefix, |
| 185 | StringRef Postfix, bool InPPDirective, encoding::Encoding Encoding, |
| 186 | const FormatStyle &Style) |
| 187 | : BreakableToken(Tok, InPPDirective, Encoding, Style), |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 188 | StartColumn(StartColumn), Prefix(Prefix), Postfix(Postfix) { |
Daniel Jasper | 174b012 | 2014-01-09 14:18:12 +0000 | [diff] [blame] | 189 | assert(Tok.TokenText.endswith(Postfix)); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 190 | Line = Tok.TokenText.substr( |
| 191 | Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size()); |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 194 | BreakableStringLiteral::BreakableStringLiteral( |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 195 | const FormatToken &Tok, unsigned StartColumn, StringRef Prefix, |
| 196 | StringRef Postfix, bool InPPDirective, encoding::Encoding Encoding, |
| 197 | const FormatStyle &Style) |
| 198 | : BreakableSingleLineToken(Tok, StartColumn, Prefix, Postfix, InPPDirective, |
| 199 | Encoding, Style) {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 200 | |
| 201 | BreakableToken::Split |
| 202 | BreakableStringLiteral::getSplit(unsigned LineIndex, unsigned TailOffset, |
| 203 | unsigned ColumnLimit) const { |
Alexander Kornienko | 81e3294 | 2013-09-16 20:20:49 +0000 | [diff] [blame] | 204 | return getStringSplit(Line.substr(TailOffset), |
| 205 | StartColumn + Prefix.size() + Postfix.size(), |
| 206 | ColumnLimit, Style.TabWidth, Encoding); |
Alexander Kornienko | 9e90b62 | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 209 | void BreakableStringLiteral::insertBreak(unsigned LineIndex, |
| 210 | unsigned TailOffset, Split Split, |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 211 | WhitespaceManager &Whitespaces) { |
Daniel Jasper | d07c2ee | 2014-01-14 09:53:07 +0000 | [diff] [blame] | 212 | unsigned LeadingSpaces = StartColumn; |
| 213 | // The '@' of an ObjC string literal (@"Test") does not become part of the |
| 214 | // string token. |
| 215 | // FIXME: It might be a cleaner solution to merge the tokens as a |
| 216 | // precomputation step. |
| 217 | if (Prefix.startswith("@")) |
| 218 | --LeadingSpaces; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 219 | Whitespaces.replaceWhitespaceInToken( |
| 220 | Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix, |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 221 | Prefix, InPPDirective, 1, LeadingSpaces); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 224 | BreakableComment::BreakableComment(const FormatToken &Token, |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 225 | unsigned StartColumn, |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 226 | unsigned OriginalStartColumn, |
| 227 | bool FirstInLine, bool InPPDirective, |
| 228 | encoding::Encoding Encoding, |
| 229 | const FormatStyle &Style) |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 230 | : BreakableToken(Token, InPPDirective, Encoding, Style), |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 231 | StartColumn(StartColumn), OriginalStartColumn(OriginalStartColumn), |
| 232 | FirstInLine(FirstInLine) {} |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 233 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 234 | unsigned BreakableComment::getLineCount() const { return Lines.size(); } |
| 235 | |
| 236 | BreakableToken::Split BreakableComment::getSplit(unsigned LineIndex, |
| 237 | unsigned TailOffset, |
| 238 | unsigned ColumnLimit) const { |
| 239 | return getCommentSplit(Content[LineIndex].substr(TailOffset), |
| 240 | getContentStartColumn(LineIndex, TailOffset), |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 241 | ColumnLimit, Style.TabWidth, Encoding); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 244 | void BreakableComment::compressWhitespace(unsigned LineIndex, |
| 245 | unsigned TailOffset, Split Split, |
| 246 | WhitespaceManager &Whitespaces) { |
| 247 | StringRef Text = Content[LineIndex].substr(TailOffset); |
| 248 | // Text is relative to the content line, but Whitespaces operates relative to |
| 249 | // the start of the corresponding token, so compute the start of the Split |
| 250 | // that needs to be compressed into a single space relative to the start of |
| 251 | // its token. |
| 252 | unsigned BreakOffsetInToken = |
| 253 | Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first; |
| 254 | unsigned CharsToRemove = Split.second; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 255 | Whitespaces.replaceWhitespaceInToken( |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 256 | tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "", "", |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 257 | /*InPPDirective=*/false, /*Newlines=*/0, /*Spaces=*/1); |
Alexander Kornienko | 875395f | 2013-11-12 17:50:13 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 260 | BreakableToken::Split |
| 261 | BreakableComment::getReflowSplit(StringRef Text, StringRef ReflowPrefix, |
| 262 | unsigned PreviousEndColumn, |
| 263 | unsigned ColumnLimit) const { |
| 264 | unsigned ReflowStartColumn = PreviousEndColumn + ReflowPrefix.size(); |
| 265 | StringRef TrimmedText = Text.rtrim(Blanks); |
| 266 | // This is the width of the resulting line in case the full line of Text gets |
| 267 | // reflown up starting at ReflowStartColumn. |
| 268 | unsigned FullWidth = ReflowStartColumn + encoding::columnWidthWithTabs( |
| 269 | TrimmedText, ReflowStartColumn, |
| 270 | Style.TabWidth, Encoding); |
| 271 | // If the full line fits up, we return a reflow split after it, |
| 272 | // otherwise we compute the largest piece of text that fits after |
| 273 | // ReflowStartColumn. |
| 274 | Split ReflowSplit = |
| 275 | FullWidth <= ColumnLimit |
| 276 | ? Split(TrimmedText.size(), Text.size() - TrimmedText.size()) |
| 277 | : getCommentSplit(Text, ReflowStartColumn, ColumnLimit, |
| 278 | Style.TabWidth, Encoding); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 279 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 280 | // We need to be extra careful here, because while it's OK to keep a long line |
| 281 | // if it can't be broken into smaller pieces (like when the first word of a |
| 282 | // long line is longer than the column limit), it's not OK to reflow that long |
| 283 | // word up. So we recompute the size of the previous line after reflowing and |
| 284 | // only return the reflow split if that's under the line limit. |
| 285 | if (ReflowSplit.first != StringRef::npos && |
| 286 | // Check if the width of the newly reflown line is under the limit. |
| 287 | PreviousEndColumn + ReflowPrefix.size() + |
| 288 | encoding::columnWidthWithTabs(Text.substr(0, ReflowSplit.first), |
| 289 | PreviousEndColumn + |
| 290 | ReflowPrefix.size(), |
| 291 | Style.TabWidth, Encoding) <= |
| 292 | ColumnLimit) { |
| 293 | return ReflowSplit; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 294 | } |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 295 | return Split(StringRef::npos, 0); |
| 296 | } |
| 297 | |
| 298 | const FormatToken &BreakableComment::tokenAt(unsigned LineIndex) const { |
| 299 | return Tokens[LineIndex] ? *Tokens[LineIndex] : Tok; |
| 300 | } |
| 301 | |
| 302 | static bool mayReflowContent(StringRef Content) { |
| 303 | Content = Content.trim(Blanks); |
Krasimir Georgiev | 28912c0 | 2017-02-02 10:52:08 +0000 | [diff] [blame^] | 304 | // Lines starting with '@' commonly have special meaning. |
| 305 | static const SmallVector<StringRef, 4> kSpecialMeaningPrefixes = { |
| 306 | "@", "TODO", "FIXME", "XXX"}; |
| 307 | bool hasSpecialMeaningPrefix = false; |
| 308 | for (StringRef Prefix : kSpecialMeaningPrefixes) { |
| 309 | if (Content.startswith(Prefix)) { |
| 310 | hasSpecialMeaningPrefix = true; |
| 311 | break; |
| 312 | } |
| 313 | } |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 314 | // Simple heuristic for what to reflow: content should contain at least two |
| 315 | // characters and either the first or second character must be |
| 316 | // non-punctuation. |
Krasimir Georgiev | 28912c0 | 2017-02-02 10:52:08 +0000 | [diff] [blame^] | 317 | return Content.size() >= 2 && !hasSpecialMeaningPrefix && |
| 318 | !Content.endswith("\\") && |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 319 | // Note that this is UTF-8 safe, since if isPunctuation(Content[0]) is |
| 320 | // true, then the first code point must be 1 byte long. |
| 321 | (!isPunctuation(Content[0]) || !isPunctuation(Content[1])); |
| 322 | } |
| 323 | |
| 324 | bool BreakableComment::mayReflow(unsigned LineIndex) const { |
| 325 | return LineIndex > 0 && mayReflowContent(Content[LineIndex]) && |
| 326 | !Tok.Finalized && !switchesFormatting(tokenAt(LineIndex)) && |
| 327 | (!Tok.is(TT_LineComment) || |
| 328 | OriginalPrefix[LineIndex] == OriginalPrefix[LineIndex - 1]); |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Alexander Kornienko | ffcc010 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 331 | BreakableBlockComment::BreakableBlockComment( |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 332 | const FormatToken &Token, unsigned StartColumn, |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 333 | unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective, |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 334 | encoding::Encoding Encoding, const FormatStyle &Style) |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 335 | : BreakableComment(Token, StartColumn, OriginalStartColumn, FirstInLine, |
| 336 | InPPDirective, Encoding, Style) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 337 | assert(Tok.is(TT_BlockComment) && |
| 338 | "block comment section must start with a block comment"); |
| 339 | |
| 340 | StringRef TokenText(Tok.TokenText); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 341 | assert(TokenText.startswith("/*") && TokenText.endswith("*/")); |
| 342 | TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n"); |
| 343 | |
| 344 | int IndentDelta = StartColumn - OriginalStartColumn; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 345 | Content.resize(Lines.size()); |
| 346 | Content[0] = Lines[0]; |
| 347 | ContentColumn.resize(Lines.size()); |
| 348 | // Account for the initial '/*'. |
| 349 | ContentColumn[0] = StartColumn + 2; |
| 350 | Tokens.resize(Lines.size()); |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 351 | for (size_t i = 1; i < Lines.size(); ++i) |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 352 | adjustWhitespace(i, IndentDelta); |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 353 | |
| 354 | Decoration = "* "; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 355 | if (Lines.size() == 1 && !FirstInLine) { |
| 356 | // Comments for which FirstInLine is false can start on arbitrary column, |
| 357 | // and available horizontal space can be too small to align consecutive |
| 358 | // lines with the first one. |
| 359 | // FIXME: We could, probably, align them to current indentation level, but |
| 360 | // now we just wrap them without stars. |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 361 | Decoration = ""; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 362 | } |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 363 | for (size_t i = 1, e = Lines.size(); i < e && !Decoration.empty(); ++i) { |
| 364 | // If the last line is empty, the closing "*/" will have a star. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 365 | if (i + 1 == e && Content[i].empty()) |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 366 | break; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 367 | if (!Content[i].empty() && i + 1 != e && |
| 368 | Decoration.startswith(Content[i])) |
Daniel Jasper | 6d9b88d | 2015-05-06 07:17:22 +0000 | [diff] [blame] | 369 | continue; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 370 | while (!Content[i].startswith(Decoration)) |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 371 | Decoration = Decoration.substr(0, Decoration.size() - 1); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 372 | } |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 373 | |
| 374 | LastLineNeedsDecoration = true; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 375 | IndentAtLineBreak = ContentColumn[0] + 1; |
| 376 | for (size_t i = 1, e = Lines.size(); i < e; ++i) { |
| 377 | if (Content[i].empty()) { |
| 378 | if (i + 1 == e) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 379 | // Empty last line means that we already have a star as a part of the |
| 380 | // trailing */. We also need to preserve whitespace, so that */ is |
| 381 | // correctly indented. |
| 382 | LastLineNeedsDecoration = false; |
| 383 | } else if (Decoration.empty()) { |
| 384 | // For all other lines, set the start column to 0 if they're empty, so |
| 385 | // we do not insert trailing whitespace anywhere. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 386 | ContentColumn[i] = 0; |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 387 | } |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 388 | continue; |
| 389 | } |
Daniel Jasper | 6d9b88d | 2015-05-06 07:17:22 +0000 | [diff] [blame] | 390 | |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 391 | // The first line already excludes the star. |
| 392 | // For all other lines, adjust the line to exclude the star and |
| 393 | // (optionally) the first whitespace. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 394 | unsigned DecorationSize = Decoration.startswith(Content[i]) |
| 395 | ? Content[i].size() |
| 396 | : Decoration.size(); |
| 397 | ContentColumn[i] += DecorationSize; |
| 398 | Content[i] = Content[i].substr(DecorationSize); |
| 399 | if (!Decoration.startswith(Content[i])) |
Daniel Jasper | 6d9b88d | 2015-05-06 07:17:22 +0000 | [diff] [blame] | 400 | IndentAtLineBreak = |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 401 | std::min<int>(IndentAtLineBreak, std::max(0, ContentColumn[i])); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 402 | } |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 403 | IndentAtLineBreak = |
| 404 | std::max<unsigned>(IndentAtLineBreak, Decoration.size()); |
| 405 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 406 | DEBUG({ |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 407 | llvm::dbgs() << "IndentAtLineBreak " << IndentAtLineBreak << "\n"; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 408 | for (size_t i = 0; i < Lines.size(); ++i) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 409 | llvm::dbgs() << i << " |" << Content[i] << "| " |
| 410 | << (Content[i].data() - Lines[i].data()) << "\n"; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 411 | } |
| 412 | }); |
| 413 | } |
| 414 | |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 415 | void BreakableBlockComment::adjustWhitespace(unsigned LineIndex, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 416 | int IndentDelta) { |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 417 | // When in a preprocessor directive, the trailing backslash in a block comment |
| 418 | // is not needed, but can serve a purpose of uniformity with necessary escaped |
| 419 | // newlines outside the comment. In this case we remove it here before |
| 420 | // trimming the trailing whitespace. The backslash will be re-added later when |
| 421 | // inserting a line break. |
| 422 | size_t EndOfPreviousLine = Lines[LineIndex - 1].size(); |
| 423 | if (InPPDirective && Lines[LineIndex - 1].endswith("\\")) |
| 424 | --EndOfPreviousLine; |
| 425 | |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 426 | // Calculate the end of the non-whitespace text in the previous line. |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 427 | EndOfPreviousLine = |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 428 | Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 429 | if (EndOfPreviousLine == StringRef::npos) |
| 430 | EndOfPreviousLine = 0; |
| 431 | else |
| 432 | ++EndOfPreviousLine; |
| 433 | // Calculate the start of the non-whitespace text in the current line. |
Alexander Kornienko | b93062e | 2013-06-20 13:58:37 +0000 | [diff] [blame] | 434 | size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 435 | if (StartOfLine == StringRef::npos) |
Daniel Jasper | d6e6188 | 2015-06-17 12:23:15 +0000 | [diff] [blame] | 436 | StartOfLine = Lines[LineIndex].rtrim("\r\n").size(); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 437 | |
Alexander Kornienko | ebb43ca | 2013-09-05 14:08:34 +0000 | [diff] [blame] | 438 | StringRef Whitespace = Lines[LineIndex].substr(0, StartOfLine); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 439 | // Adjust Lines to only contain relevant text. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 440 | size_t PreviousContentOffset = |
| 441 | Content[LineIndex - 1].data() - Lines[LineIndex - 1].data(); |
| 442 | Content[LineIndex - 1] = Lines[LineIndex - 1].substr( |
| 443 | PreviousContentOffset, EndOfPreviousLine - PreviousContentOffset); |
| 444 | Content[LineIndex] = Lines[LineIndex].substr(StartOfLine); |
Manuel Klimek | 34d1515 | 2013-05-28 10:01:59 +0000 | [diff] [blame] | 445 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 446 | // Adjust the start column uniformly across all lines. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 447 | ContentColumn[LineIndex] = |
Alexander Kornienko | 39856b7 | 2013-09-10 09:38:25 +0000 | [diff] [blame] | 448 | encoding::columnWidthWithTabs(Whitespace, 0, Style.TabWidth, Encoding) + |
Alexander Kornienko | 67d9c8c | 2014-04-17 16:12:46 +0000 | [diff] [blame] | 449 | IndentDelta; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Alexander Kornienko | dd7ece5 | 2013-06-07 16:02:52 +0000 | [diff] [blame] | 452 | unsigned BreakableBlockComment::getLineLengthAfterSplit( |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 453 | unsigned LineIndex, unsigned TailOffset, |
| 454 | StringRef::size_type Length) const { |
| 455 | unsigned ContentStartColumn = getContentStartColumn(LineIndex, TailOffset); |
| 456 | unsigned LineLength = |
| 457 | ContentStartColumn + encoding::columnWidthWithTabs( |
| 458 | Content[LineIndex].substr(TailOffset, Length), |
| 459 | ContentStartColumn, Style.TabWidth, Encoding); |
| 460 | // The last line gets a "*/" postfix. |
| 461 | if (LineIndex + 1 == Lines.size()) { |
| 462 | LineLength += 2; |
| 463 | // We never need a decoration when breaking just the trailing "*/" postfix. |
| 464 | // Note that checking that Length == 0 is not enough, since Length could |
| 465 | // also be StringRef::npos. |
| 466 | if (Content[LineIndex].substr(TailOffset, Length).empty()) { |
| 467 | LineLength -= Decoration.size(); |
| 468 | } |
| 469 | } |
| 470 | return LineLength; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset, |
Alexander Kornienko | be63390 | 2013-06-14 11:46:10 +0000 | [diff] [blame] | 474 | Split Split, |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 475 | WhitespaceManager &Whitespaces) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 476 | StringRef Text = Content[LineIndex].substr(TailOffset); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 477 | StringRef Prefix = Decoration; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 478 | // We need this to account for the case when we have a decoration "* " for all |
| 479 | // the lines except for the last one, where the star in "*/" acts as a |
| 480 | // decoration. |
| 481 | unsigned LocalIndentAtLineBreak = IndentAtLineBreak; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 482 | if (LineIndex + 1 == Lines.size() && |
| 483 | Text.size() == Split.first + Split.second) { |
| 484 | // For the last line we need to break before "*/", but not to add "* ". |
| 485 | Prefix = ""; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 486 | if (LocalIndentAtLineBreak >= 2) |
| 487 | LocalIndentAtLineBreak -= 2; |
| 488 | } |
| 489 | // The split offset is from the beginning of the line. Convert it to an offset |
| 490 | // from the beginning of the token text. |
| 491 | unsigned BreakOffsetInToken = |
| 492 | Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first; |
| 493 | unsigned CharsToRemove = Split.second; |
| 494 | assert(LocalIndentAtLineBreak >= Prefix.size()); |
| 495 | Whitespaces.replaceWhitespaceInToken( |
| 496 | tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "", Prefix, |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 497 | InPPDirective, /*Newlines=*/1, |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 498 | /*Spaces=*/LocalIndentAtLineBreak - Prefix.size()); |
| 499 | } |
| 500 | |
| 501 | BreakableToken::Split BreakableBlockComment::getSplitBefore( |
| 502 | unsigned LineIndex, |
| 503 | unsigned PreviousEndColumn, |
| 504 | unsigned ColumnLimit) const { |
| 505 | if (!mayReflow(LineIndex)) |
| 506 | return Split(StringRef::npos, 0); |
| 507 | StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks); |
| 508 | return getReflowSplit(TrimmedContent, ReflowPrefix, PreviousEndColumn, |
| 509 | ColumnLimit); |
| 510 | } |
| 511 | |
| 512 | unsigned BreakableBlockComment::getReflownColumn( |
| 513 | StringRef Content, |
| 514 | unsigned LineIndex, |
| 515 | unsigned PreviousEndColumn) const { |
| 516 | unsigned StartColumn = PreviousEndColumn + ReflowPrefix.size(); |
| 517 | // If this is the last line, it will carry around its '*/' postfix. |
| 518 | unsigned PostfixLength = (LineIndex + 1 == Lines.size() ? 2 : 0); |
| 519 | // The line is composed of previous text, reflow prefix, reflown text and |
| 520 | // postfix. |
| 521 | unsigned ReflownColumn = |
| 522 | StartColumn + encoding::columnWidthWithTabs(Content, StartColumn, |
| 523 | Style.TabWidth, Encoding) + |
| 524 | PostfixLength; |
| 525 | return ReflownColumn; |
| 526 | } |
| 527 | |
| 528 | unsigned BreakableBlockComment::getLineLengthAfterSplitBefore( |
| 529 | unsigned LineIndex, unsigned TailOffset, |
| 530 | unsigned PreviousEndColumn, |
| 531 | unsigned ColumnLimit, |
| 532 | Split SplitBefore) const { |
Krasimir Georgiev | af1b962 | 2017-01-31 14:31:44 +0000 | [diff] [blame] | 533 | if (SplitBefore.first == StringRef::npos || |
| 534 | // Block comment line contents contain the trailing whitespace after the |
| 535 | // decoration, so the need of left trim. Note that this behavior is |
| 536 | // consistent with the breaking of block comments where the indentation of |
| 537 | // a broken line is uniform across all the lines of the block comment. |
| 538 | SplitBefore.first + SplitBefore.second < |
| 539 | Content[LineIndex].ltrim().size()) { |
| 540 | // A piece of line, not the whole, gets reflown. |
| 541 | return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 542 | } else { |
| 543 | // The whole line gets reflown, need to check if we need to insert a break |
| 544 | // for the postfix or not. |
| 545 | StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks); |
| 546 | unsigned ReflownColumn = |
| 547 | getReflownColumn(TrimmedContent, LineIndex, PreviousEndColumn); |
| 548 | if (ReflownColumn <= ColumnLimit) { |
| 549 | return ReflownColumn; |
| 550 | } |
| 551 | return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); |
| 552 | } |
| 553 | } |
| 554 | void BreakableBlockComment::replaceWhitespaceBefore( |
| 555 | unsigned LineIndex, unsigned PreviousEndColumn, unsigned ColumnLimit, |
| 556 | Split SplitBefore, WhitespaceManager &Whitespaces) { |
| 557 | if (LineIndex == 0) return; |
| 558 | StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks); |
| 559 | if (SplitBefore.first != StringRef::npos) { |
| 560 | // Here we need to reflow. |
| 561 | assert(Tokens[LineIndex - 1] == Tokens[LineIndex] && |
| 562 | "Reflowing whitespace within a token"); |
| 563 | // This is the offset of the end of the last line relative to the start of |
| 564 | // the token text in the token. |
| 565 | unsigned WhitespaceOffsetInToken = Content[LineIndex - 1].data() + |
| 566 | Content[LineIndex - 1].size() - |
| 567 | tokenAt(LineIndex).TokenText.data(); |
| 568 | unsigned WhitespaceLength = TrimmedContent.data() - |
| 569 | tokenAt(LineIndex).TokenText.data() - |
| 570 | WhitespaceOffsetInToken; |
| 571 | Whitespaces.replaceWhitespaceInToken( |
| 572 | tokenAt(LineIndex), WhitespaceOffsetInToken, |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 573 | /*ReplaceChars=*/WhitespaceLength, /*PreviousPostfix=*/"", |
| 574 | /*CurrentPrefix=*/ReflowPrefix, InPPDirective, /*Newlines=*/0, |
| 575 | /*Spaces=*/0); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 576 | // Check if we need to also insert a break at the whitespace range. |
| 577 | // For this we first adapt the reflow split relative to the beginning of the |
| 578 | // content. |
| 579 | // Note that we don't need a penalty for this break, since it doesn't change |
| 580 | // the total number of lines. |
| 581 | Split BreakSplit = SplitBefore; |
| 582 | BreakSplit.first += TrimmedContent.data() - Content[LineIndex].data(); |
| 583 | unsigned ReflownColumn = |
| 584 | getReflownColumn(TrimmedContent, LineIndex, PreviousEndColumn); |
| 585 | if (ReflownColumn > ColumnLimit) { |
| 586 | insertBreak(LineIndex, 0, BreakSplit, Whitespaces); |
| 587 | } |
| 588 | return; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 591 | // Here no reflow with the previous line will happen. |
| 592 | // Fix the decoration of the line at LineIndex. |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 593 | StringRef Prefix = Decoration; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 594 | if (Content[LineIndex].empty()) { |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 595 | if (LineIndex + 1 == Lines.size()) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 596 | if (!LastLineNeedsDecoration) { |
| 597 | // If the last line was empty, we don't need a prefix, as the */ will |
| 598 | // line up with the decoration (if it exists). |
| 599 | Prefix = ""; |
| 600 | } |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 601 | } else if (!Decoration.empty()) { |
| 602 | // For other empty lines, if we do have a decoration, adapt it to not |
| 603 | // contain a trailing whitespace. |
| 604 | Prefix = Prefix.substr(0, 1); |
| 605 | } |
Daniel Jasper | 51fb2b2 | 2013-05-30 06:40:07 +0000 | [diff] [blame] | 606 | } else { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 607 | if (ContentColumn[LineIndex] == 1) { |
Alexander Kornienko | 614d96a | 2013-07-08 14:12:07 +0000 | [diff] [blame] | 608 | // This line starts immediately after the decorating *. |
Daniel Jasper | 51fb2b2 | 2013-05-30 06:40:07 +0000 | [diff] [blame] | 609 | Prefix = Prefix.substr(0, 1); |
| 610 | } |
Manuel Klimek | 281dcbe | 2013-05-28 08:55:01 +0000 | [diff] [blame] | 611 | } |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 612 | // This is the offset of the end of the last line relative to the start of the |
| 613 | // token text in the token. |
| 614 | unsigned WhitespaceOffsetInToken = Content[LineIndex - 1].data() + |
| 615 | Content[LineIndex - 1].size() - |
| 616 | tokenAt(LineIndex).TokenText.data(); |
| 617 | unsigned WhitespaceLength = Content[LineIndex].data() - |
| 618 | tokenAt(LineIndex).TokenText.data() - |
| 619 | WhitespaceOffsetInToken; |
Alexander Kornienko | 555efc3 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 620 | Whitespaces.replaceWhitespaceInToken( |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 621 | tokenAt(LineIndex), WhitespaceOffsetInToken, WhitespaceLength, "", Prefix, |
| 622 | InPPDirective, /*Newlines=*/1, ContentColumn[LineIndex] - Prefix.size()); |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | unsigned |
| 626 | BreakableBlockComment::getContentStartColumn(unsigned LineIndex, |
| 627 | unsigned TailOffset) const { |
| 628 | // If we break, we always break at the predefined indent. |
| 629 | if (TailOffset != 0) |
| 630 | return IndentAtLineBreak; |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 631 | return std::max(0, ContentColumn[LineIndex]); |
| 632 | } |
| 633 | |
| 634 | BreakableLineCommentSection::BreakableLineCommentSection( |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 635 | const FormatToken &Token, unsigned StartColumn, |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 636 | unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective, |
| 637 | encoding::Encoding Encoding, const FormatStyle &Style) |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 638 | : BreakableComment(Token, StartColumn, OriginalStartColumn, FirstInLine, |
| 639 | InPPDirective, Encoding, Style) { |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 640 | assert(Tok.is(TT_LineComment) && |
| 641 | "line comment section must start with a line comment"); |
| 642 | FormatToken *LineTok = nullptr; |
| 643 | for (const FormatToken *CurrentTok = &Tok; |
| 644 | CurrentTok && CurrentTok->is(TT_LineComment); |
| 645 | CurrentTok = CurrentTok->Next) { |
| 646 | LastLineTok = LineTok; |
| 647 | StringRef TokenText(CurrentTok->TokenText); |
| 648 | assert(TokenText.startswith("//")); |
| 649 | size_t FirstLineIndex = Lines.size(); |
| 650 | TokenText.split(Lines, "\n"); |
| 651 | Content.resize(Lines.size()); |
| 652 | ContentColumn.resize(Lines.size()); |
| 653 | OriginalContentColumn.resize(Lines.size()); |
| 654 | Tokens.resize(Lines.size()); |
| 655 | Prefix.resize(Lines.size()); |
| 656 | OriginalPrefix.resize(Lines.size()); |
| 657 | for (size_t i = FirstLineIndex, e = Lines.size(); i < e; ++i) { |
Krasimir Georgiev | e518e0b | 2017-01-30 21:00:01 +0000 | [diff] [blame] | 658 | // We need to trim the blanks in case this is not the first line in a |
| 659 | // multiline comment. Then the indent is included in Lines[i]. |
| 660 | StringRef IndentPrefix = |
| 661 | getLineCommentIndentPrefix(Lines[i].ltrim(Blanks)); |
| 662 | assert(IndentPrefix.startswith("//")); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 663 | OriginalPrefix[i] = Prefix[i] = IndentPrefix; |
| 664 | if (Lines[i].size() > Prefix[i].size() && |
| 665 | isAlphanumeric(Lines[i][Prefix[i].size()])) { |
| 666 | if (Prefix[i] == "//") |
| 667 | Prefix[i] = "// "; |
| 668 | else if (Prefix[i] == "///") |
| 669 | Prefix[i] = "/// "; |
| 670 | else if (Prefix[i] == "//!") |
| 671 | Prefix[i] = "//! "; |
| 672 | } |
| 673 | |
| 674 | Tokens[i] = LineTok; |
| 675 | Content[i] = Lines[i].substr(IndentPrefix.size()); |
| 676 | OriginalContentColumn[i] = |
| 677 | StartColumn + |
| 678 | encoding::columnWidthWithTabs(OriginalPrefix[i], |
| 679 | StartColumn, |
| 680 | Style.TabWidth, |
| 681 | Encoding); |
| 682 | ContentColumn[i] = |
| 683 | StartColumn + |
| 684 | encoding::columnWidthWithTabs(Prefix[i], |
| 685 | StartColumn, |
| 686 | Style.TabWidth, |
| 687 | Encoding); |
| 688 | |
| 689 | // Calculate the end of the non-whitespace text in this line. |
| 690 | size_t EndOfLine = Content[i].find_last_not_of(Blanks); |
| 691 | if (EndOfLine == StringRef::npos) |
| 692 | EndOfLine = Content[i].size(); |
| 693 | else |
| 694 | ++EndOfLine; |
| 695 | Content[i] = Content[i].substr(0, EndOfLine); |
| 696 | } |
| 697 | LineTok = CurrentTok->Next; |
Krasimir Georgiev | 753625b | 2017-01-31 13:32:38 +0000 | [diff] [blame] | 698 | if (CurrentTok->Next && CurrentTok->Next->NewlinesBefore > 1) { |
| 699 | // A line comment section needs to broken by a line comment that is |
| 700 | // preceded by at least two newlines. Note that we put this break here |
| 701 | // instead of breaking at a previous stage during parsing, since that |
| 702 | // would split the contents of the enum into two unwrapped lines in this |
| 703 | // example, which is undesirable: |
| 704 | // enum A { |
| 705 | // a, // comment about a |
| 706 | // |
| 707 | // // comment about b |
| 708 | // b |
| 709 | // }; |
| 710 | // |
| 711 | // FIXME: Consider putting separate line comment sections as children to |
| 712 | // the unwrapped line instead. |
| 713 | break; |
| 714 | } |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
| 718 | unsigned BreakableLineCommentSection::getLineLengthAfterSplit( |
| 719 | unsigned LineIndex, unsigned TailOffset, |
| 720 | StringRef::size_type Length) const { |
| 721 | unsigned ContentStartColumn = |
| 722 | (TailOffset == 0 ? ContentColumn[LineIndex] |
| 723 | : OriginalContentColumn[LineIndex]); |
| 724 | return ContentStartColumn + encoding::columnWidthWithTabs( |
| 725 | Content[LineIndex].substr(TailOffset, Length), |
| 726 | ContentStartColumn, Style.TabWidth, Encoding); |
| 727 | } |
| 728 | |
| 729 | void BreakableLineCommentSection::insertBreak(unsigned LineIndex, |
| 730 | unsigned TailOffset, Split Split, |
| 731 | WhitespaceManager &Whitespaces) { |
| 732 | StringRef Text = Content[LineIndex].substr(TailOffset); |
| 733 | // Compute the offset of the split relative to the beginning of the token |
| 734 | // text. |
| 735 | unsigned BreakOffsetInToken = |
| 736 | Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first; |
| 737 | unsigned CharsToRemove = Split.second; |
| 738 | // Compute the size of the new indent, including the size of the new prefix of |
| 739 | // the newly broken line. |
| 740 | unsigned IndentAtLineBreak = OriginalContentColumn[LineIndex] + |
| 741 | Prefix[LineIndex].size() - |
| 742 | OriginalPrefix[LineIndex].size(); |
| 743 | assert(IndentAtLineBreak >= Prefix[LineIndex].size()); |
| 744 | Whitespaces.replaceWhitespaceInToken( |
| 745 | tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "", |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 746 | Prefix[LineIndex], InPPDirective, /*Newlines=*/1, |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 747 | /*Spaces=*/IndentAtLineBreak - Prefix[LineIndex].size()); |
| 748 | } |
| 749 | |
| 750 | BreakableComment::Split BreakableLineCommentSection::getSplitBefore( |
| 751 | unsigned LineIndex, |
| 752 | unsigned PreviousEndColumn, |
| 753 | unsigned ColumnLimit) const { |
| 754 | if (!mayReflow(LineIndex)) return Split(StringRef::npos, 0); |
| 755 | return getReflowSplit(Content[LineIndex], ReflowPrefix, PreviousEndColumn, |
| 756 | ColumnLimit); |
| 757 | } |
| 758 | |
| 759 | unsigned BreakableLineCommentSection::getLineLengthAfterSplitBefore( |
| 760 | unsigned LineIndex, unsigned TailOffset, |
| 761 | unsigned PreviousEndColumn, |
| 762 | unsigned ColumnLimit, |
| 763 | Split SplitBefore) const { |
| 764 | if (SplitBefore.first == StringRef::npos || |
| 765 | SplitBefore.first + SplitBefore.second < Content[LineIndex].size()) { |
| 766 | // A piece of line, not the whole line, gets reflown. |
| 767 | return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); |
| 768 | } else { |
| 769 | // The whole line gets reflown. |
| 770 | unsigned StartColumn = PreviousEndColumn + ReflowPrefix.size(); |
| 771 | return StartColumn + encoding::columnWidthWithTabs(Content[LineIndex], |
| 772 | StartColumn, |
| 773 | Style.TabWidth, |
| 774 | Encoding); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | void BreakableLineCommentSection::replaceWhitespaceBefore( |
| 779 | unsigned LineIndex, unsigned PreviousEndColumn, unsigned ColumnLimit, |
| 780 | Split SplitBefore, WhitespaceManager &Whitespaces) { |
| 781 | // If this is the first line of a token, we need to inform Whitespace Manager |
| 782 | // about it: either adapt the whitespace range preceding it, or mark it as an |
| 783 | // untouchable token. |
| 784 | // This happens for instance here: |
| 785 | // // line 1 \ |
| 786 | // // line 2 |
| 787 | if (LineIndex > 0 && Tokens[LineIndex] != Tokens[LineIndex - 1]) { |
| 788 | if (SplitBefore.first != StringRef::npos) { |
| 789 | // Reflow happens between tokens. Replace the whitespace between the |
| 790 | // tokens by the empty string. |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 791 | Whitespaces.replaceWhitespace( |
| 792 | *Tokens[LineIndex], /*Newlines=*/0, /*Spaces=*/0, |
| 793 | /*StartOfTokenColumn=*/StartColumn, /*InPPDirective=*/false); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 794 | // Replace the indent and prefix of the token with the reflow prefix. |
| 795 | unsigned WhitespaceLength = |
| 796 | Content[LineIndex].data() - tokenAt(LineIndex).TokenText.data(); |
| 797 | Whitespaces.replaceWhitespaceInToken(*Tokens[LineIndex], |
| 798 | /*Offset=*/0, |
| 799 | /*ReplaceChars=*/WhitespaceLength, |
| 800 | /*PreviousPostfix=*/"", |
| 801 | /*CurrentPrefix=*/ReflowPrefix, |
| 802 | /*InPPDirective=*/false, |
| 803 | /*Newlines=*/0, |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 804 | /*Spaces=*/0); |
| 805 | } else { |
| 806 | // This is the first line for the current token, but no reflow with the |
| 807 | // previous token is necessary. However, we still may need to adjust the |
Krasimir Georgiev | b796ceb | 2017-01-31 15:40:15 +0000 | [diff] [blame] | 808 | // start column. Note that ContentColumn[LineIndex] is the expected |
| 809 | // content column after a possible update to the prefix, hence the prefix |
| 810 | // length change is included. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 811 | unsigned LineColumn = |
| 812 | ContentColumn[LineIndex] - |
Krasimir Georgiev | b796ceb | 2017-01-31 15:40:15 +0000 | [diff] [blame] | 813 | (Content[LineIndex].data() - Lines[LineIndex].data()) + |
| 814 | (OriginalPrefix[LineIndex].size() - Prefix[LineIndex].size()); |
Krasimir Georgiev | 13dbaa0 | 2017-02-01 10:10:04 +0000 | [diff] [blame] | 815 | |
| 816 | // We always want to create a replacement instead of adding an untouchable |
| 817 | // token, even if LineColumn is the same as the original column of the |
| 818 | // token. This is because WhitespaceManager doesn't align trailing |
| 819 | // comments if they are untouchable. |
| 820 | Whitespaces.replaceWhitespace(*Tokens[LineIndex], |
| 821 | /*Newlines=*/1, |
| 822 | /*Spaces=*/LineColumn, |
| 823 | /*StartOfTokenColumn=*/LineColumn, |
| 824 | /*InPPDirective=*/false); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 825 | } |
Krasimir Georgiev | b796ceb | 2017-01-31 15:40:15 +0000 | [diff] [blame] | 826 | } |
| 827 | if (OriginalPrefix[LineIndex] != Prefix[LineIndex]) { |
| 828 | // Adjust the prefix if necessary. |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 829 | |
| 830 | // Take care of the space possibly introduced after a decoration. |
| 831 | assert(Prefix[LineIndex] == (OriginalPrefix[LineIndex] + " ").str() && |
Krasimir Georgiev | b796ceb | 2017-01-31 15:40:15 +0000 | [diff] [blame] | 832 | "Expecting a line comment prefix to differ from original by at most " |
| 833 | "a space"); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 834 | Whitespaces.replaceWhitespaceInToken( |
| 835 | tokenAt(LineIndex), OriginalPrefix[LineIndex].size(), 0, "", "", |
Daniel Jasper | 7d42f3f | 2017-01-31 11:25:01 +0000 | [diff] [blame] | 836 | /*InPPDirective=*/false, /*Newlines=*/0, /*Spaces=*/1); |
Krasimir Georgiev | 9183422 | 2017-01-25 13:58:58 +0000 | [diff] [blame] | 837 | } |
| 838 | // Add a break after a reflow split has been introduced, if necessary. |
| 839 | // Note that this break doesn't need to be penalized, since it doesn't change |
| 840 | // the number of lines. |
| 841 | if (SplitBefore.first != StringRef::npos && |
| 842 | SplitBefore.first + SplitBefore.second < Content[LineIndex].size()) { |
| 843 | insertBreak(LineIndex, 0, SplitBefore, Whitespaces); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | void BreakableLineCommentSection::updateNextToken(LineState& State) const { |
| 848 | if (LastLineTok) { |
| 849 | State.NextToken = LastLineTok->Next; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | unsigned |
| 854 | BreakableLineCommentSection::getContentStartColumn(unsigned LineIndex, |
| 855 | unsigned TailOffset) const { |
| 856 | if (TailOffset != 0) { |
| 857 | return OriginalContentColumn[LineIndex]; |
| 858 | } |
| 859 | return ContentColumn[LineIndex]; |
Manuel Klimek | 9043c74 | 2013-05-27 15:23:34 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Alexander Kornienko | cb45bc1 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 862 | } // namespace format |
| 863 | } // namespace clang |