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