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