/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Format/BreakableToken.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- BreakableToken.cpp - Format C++ code -----------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | /// |
9 | | /// \file |
10 | | /// Contains implementation of BreakableToken class and classes derived |
11 | | /// from it. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | #include "BreakableToken.h" |
16 | | #include "ContinuationIndenter.h" |
17 | | #include "clang/Basic/CharInfo.h" |
18 | | #include "clang/Format/Format.h" |
19 | | #include "llvm/ADT/STLExtras.h" |
20 | | #include "llvm/Support/Debug.h" |
21 | | #include <algorithm> |
22 | | |
23 | | #define DEBUG_TYPE "format-token-breaker" |
24 | | |
25 | | namespace clang { |
26 | | namespace format { |
27 | | |
28 | | static constexpr StringRef Blanks = " \t\v\f\r"; |
29 | 13.6k | static bool IsBlank(char C) { |
30 | 13.6k | switch (C) { |
31 | 1.96k | case ' ': |
32 | 1.97k | case '\t': |
33 | 1.97k | case '\v': |
34 | 1.97k | case '\f': |
35 | 1.97k | case '\r': |
36 | 1.97k | return true; |
37 | 11.7k | default: |
38 | 11.7k | return false; |
39 | 13.6k | } |
40 | 13.6k | } |
41 | | |
42 | | static StringRef getLineCommentIndentPrefix(StringRef Comment, |
43 | 17.1k | const FormatStyle &Style) { |
44 | 17.1k | static constexpr StringRef KnownCStylePrefixes[] = {"///<", "//!<", "///", |
45 | 17.1k | "//!", "//:", "//"}; |
46 | 17.1k | static constexpr StringRef KnownTextProtoPrefixes[] = {"####", "###", "##", |
47 | 17.1k | "//", "#"}; |
48 | 17.1k | ArrayRef<StringRef> KnownPrefixes(KnownCStylePrefixes); |
49 | 17.1k | if (Style.Language == FormatStyle::LK_TextProto) |
50 | 264 | KnownPrefixes = KnownTextProtoPrefixes; |
51 | | |
52 | 17.1k | assert( |
53 | 17.1k | llvm::is_sorted(KnownPrefixes, [](StringRef Lhs, StringRef Rhs) noexcept { |
54 | 17.1k | return Lhs.size() > Rhs.size(); |
55 | 17.1k | })); |
56 | | |
57 | 101k | for (StringRef KnownPrefix : KnownPrefixes)17.1k { |
58 | 101k | if (Comment.startswith(KnownPrefix)) { |
59 | 17.1k | const auto PrefixLength = |
60 | 17.1k | Comment.find_first_not_of(' ', KnownPrefix.size()); |
61 | 17.1k | return Comment.substr(0, PrefixLength); |
62 | 17.1k | } |
63 | 101k | } |
64 | 0 | return {}; |
65 | 17.1k | } |
66 | | |
67 | | static BreakableToken::Split |
68 | | getCommentSplit(StringRef Text, unsigned ContentStartColumn, |
69 | | unsigned ColumnLimit, unsigned TabWidth, |
70 | | encoding::Encoding Encoding, const FormatStyle &Style, |
71 | 4.02k | bool DecorationEndsWithStar = false) { |
72 | 4.02k | LLVM_DEBUG(llvm::dbgs() << "Comment split: \"" << Text |
73 | 4.02k | << "\", Column limit: " << ColumnLimit |
74 | 4.02k | << ", Content start: " << ContentStartColumn << "\n"); |
75 | 4.02k | if (ColumnLimit <= ContentStartColumn + 1) |
76 | 786 | return BreakableToken::Split(StringRef::npos, 0); |
77 | | |
78 | 3.24k | unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1; |
79 | 3.24k | unsigned MaxSplitBytes = 0; |
80 | | |
81 | 3.24k | for (unsigned NumChars = 0; |
82 | 69.1k | NumChars < MaxSplit && MaxSplitBytes < Text.size()66.0k ;) { |
83 | 65.9k | unsigned BytesInChar = |
84 | 65.9k | encoding::getCodePointNumBytes(Text[MaxSplitBytes], Encoding); |
85 | 65.9k | NumChars += |
86 | 65.9k | encoding::columnWidthWithTabs(Text.substr(MaxSplitBytes, BytesInChar), |
87 | 65.9k | ContentStartColumn, TabWidth, Encoding); |
88 | 65.9k | MaxSplitBytes += BytesInChar; |
89 | 65.9k | } |
90 | | |
91 | | // In JavaScript, some @tags can be followed by {, and machinery that parses |
92 | | // these comments will fail to understand the comment if followed by a line |
93 | | // break. So avoid ever breaking before a {. |
94 | 3.24k | if (Style.isJavaScript()) { |
95 | 117 | StringRef::size_type SpaceOffset = |
96 | 117 | Text.find_first_of(Blanks, MaxSplitBytes); |
97 | 117 | if (SpaceOffset != StringRef::npos && SpaceOffset + 1 < Text.size()55 && |
98 | 117 | Text[SpaceOffset + 1] == '{'40 ) { |
99 | 18 | MaxSplitBytes = SpaceOffset + 1; |
100 | 18 | } |
101 | 117 | } |
102 | | |
103 | 3.24k | StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes); |
104 | | |
105 | 3.24k | static const auto kNumberedListRegexp = llvm::Regex("^[1-9][0-9]?\\."); |
106 | | // Some spaces are unacceptable to break on, rewind past them. |
107 | 3.36k | while (SpaceOffset != StringRef::npos) { |
108 | | // If a line-comment ends with `\`, the next line continues the comment, |
109 | | // whether or not it starts with `//`. This is confusing and triggers |
110 | | // -Wcomment. |
111 | | // Avoid introducing multiline comments by not allowing a break right |
112 | | // after '\'. |
113 | 2.14k | if (Style.isCpp()) { |
114 | 1.97k | StringRef::size_type LastNonBlank = |
115 | 1.97k | Text.find_last_not_of(Blanks, SpaceOffset); |
116 | 1.97k | if (LastNonBlank != StringRef::npos && Text[LastNonBlank] == '\\'1.94k ) { |
117 | 38 | SpaceOffset = Text.find_last_of(Blanks, LastNonBlank); |
118 | 38 | continue; |
119 | 38 | } |
120 | 1.97k | } |
121 | | |
122 | | // Do not split before a number followed by a dot: this would be interpreted |
123 | | // as a numbered list, which would prevent re-flowing in subsequent passes. |
124 | 2.11k | if (kNumberedListRegexp.match(Text.substr(SpaceOffset).ltrim(Blanks))) { |
125 | 4 | SpaceOffset = Text.find_last_of(Blanks, SpaceOffset); |
126 | 4 | continue; |
127 | 4 | } |
128 | | |
129 | | // Avoid ever breaking before a @tag or a { in JavaScript. |
130 | 2.10k | if (Style.isJavaScript() && SpaceOffset + 1 < Text.size()129 && |
131 | 2.10k | (119 Text[SpaceOffset + 1] == '{'119 || Text[SpaceOffset + 1] == '@'57 )) { |
132 | 79 | SpaceOffset = Text.find_last_of(Blanks, SpaceOffset); |
133 | 79 | continue; |
134 | 79 | } |
135 | | |
136 | 2.02k | break; |
137 | 2.10k | } |
138 | | |
139 | 3.24k | if (SpaceOffset == StringRef::npos || |
140 | | // Don't break at leading whitespace. |
141 | 3.24k | Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos2.02k ) { |
142 | | // Make sure that we don't break at leading whitespace that |
143 | | // reaches past MaxSplit. |
144 | 1.24k | StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(Blanks); |
145 | 1.24k | if (FirstNonWhitespace == StringRef::npos) { |
146 | | // If the comment is only whitespace, we cannot split. |
147 | 15 | return BreakableToken::Split(StringRef::npos, 0); |
148 | 15 | } |
149 | 1.22k | SpaceOffset = Text.find_first_of( |
150 | 1.22k | Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace)); |
151 | 1.22k | } |
152 | 3.22k | if (SpaceOffset != StringRef::npos && SpaceOffset != 02.62k ) { |
153 | | // adaptStartOfLine will break after lines starting with /** if the comment |
154 | | // is broken anywhere. Avoid emitting this break twice here. |
155 | | // Example: in /** longtextcomesherethatbreaks */ (with ColumnLimit 20) will |
156 | | // insert a break after /**, so this code must not insert the same break. |
157 | 2.62k | if (SpaceOffset == 1 && Text[SpaceOffset - 1] == '*'3 ) |
158 | 2 | return BreakableToken::Split(StringRef::npos, 0); |
159 | 2.62k | StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks); |
160 | 2.62k | StringRef AfterCut = Text.substr(SpaceOffset); |
161 | | // Don't trim the leading blanks if it would create a */ after the break. |
162 | 2.62k | if (!DecorationEndsWithStar || AfterCut.size() <= 16 || AfterCut[1] != '/'6 ) |
163 | 2.62k | AfterCut = AfterCut.ltrim(Blanks); |
164 | 2.62k | return BreakableToken::Split(BeforeCut.size(), |
165 | 2.62k | AfterCut.begin() - BeforeCut.end()); |
166 | 2.62k | } |
167 | 600 | return BreakableToken::Split(StringRef::npos, 0); |
168 | 3.22k | } |
169 | | |
170 | | static BreakableToken::Split |
171 | | getStringSplit(StringRef Text, unsigned UsedColumns, unsigned ColumnLimit, |
172 | 1.28k | unsigned TabWidth, encoding::Encoding Encoding) { |
173 | | // FIXME: Reduce unit test case. |
174 | 1.28k | if (Text.empty()) |
175 | 0 | return BreakableToken::Split(StringRef::npos, 0); |
176 | 1.28k | if (ColumnLimit <= UsedColumns) |
177 | 5 | return BreakableToken::Split(StringRef::npos, 0); |
178 | 1.27k | unsigned MaxSplit = ColumnLimit - UsedColumns; |
179 | 1.27k | StringRef::size_type SpaceOffset = 0; |
180 | 1.27k | StringRef::size_type SlashOffset = 0; |
181 | 1.27k | StringRef::size_type WordStartOffset = 0; |
182 | 1.27k | StringRef::size_type SplitPoint = 0; |
183 | 14.9k | for (unsigned Chars = 0;;) { |
184 | 14.9k | unsigned Advance; |
185 | 14.9k | if (Text[0] == '\\') { |
186 | 35 | Advance = encoding::getEscapeSequenceLength(Text); |
187 | 35 | Chars += Advance; |
188 | 14.9k | } else { |
189 | 14.9k | Advance = encoding::getCodePointNumBytes(Text[0], Encoding); |
190 | 14.9k | Chars += encoding::columnWidthWithTabs( |
191 | 14.9k | Text.substr(0, Advance), UsedColumns + Chars, TabWidth, Encoding); |
192 | 14.9k | } |
193 | | |
194 | 14.9k | if (Chars > MaxSplit || Text.size() <= Advance13.7k ) |
195 | 1.27k | break; |
196 | | |
197 | 13.6k | if (IsBlank(Text[0])) |
198 | 1.97k | SpaceOffset = SplitPoint; |
199 | 13.6k | if (Text[0] == '/') |
200 | 22 | SlashOffset = SplitPoint; |
201 | 13.6k | if (Advance == 1 && !isAlphanumeric(Text[0])13.5k ) |
202 | 2.22k | WordStartOffset = SplitPoint; |
203 | | |
204 | 13.6k | SplitPoint += Advance; |
205 | 13.6k | Text = Text.substr(Advance); |
206 | 13.6k | } |
207 | | |
208 | 1.27k | if (SpaceOffset != 0) |
209 | 688 | return BreakableToken::Split(SpaceOffset + 1, 0); |
210 | 590 | if (SlashOffset != 0) |
211 | 12 | return BreakableToken::Split(SlashOffset + 1, 0); |
212 | 578 | if (WordStartOffset != 0) |
213 | 14 | return BreakableToken::Split(WordStartOffset + 1, 0); |
214 | 564 | if (SplitPoint != 0) |
215 | 544 | return BreakableToken::Split(SplitPoint, 0); |
216 | 20 | return BreakableToken::Split(StringRef::npos, 0); |
217 | 564 | } |
218 | | |
219 | 21.3k | bool switchesFormatting(const FormatToken &Token) { |
220 | 21.3k | assert((Token.is(TT_BlockComment) || Token.is(TT_LineComment)) && |
221 | 21.3k | "formatting regions are switched by comment tokens"); |
222 | 21.3k | StringRef Content = Token.TokenText.substr(2).ltrim(); |
223 | 21.3k | return Content.startswith("clang-format on") || |
224 | 21.3k | Content.startswith("clang-format off")21.2k ; |
225 | 21.3k | } |
226 | | |
227 | | unsigned |
228 | | BreakableToken::getLengthAfterCompression(unsigned RemainingTokenColumns, |
229 | 1.66k | Split Split) const { |
230 | | // Example: consider the content |
231 | | // lala lala |
232 | | // - RemainingTokenColumns is the original number of columns, 10; |
233 | | // - Split is (4, 2), denoting the two spaces between the two words; |
234 | | // |
235 | | // We compute the number of columns when the split is compressed into a single |
236 | | // space, like: |
237 | | // lala lala |
238 | | // |
239 | | // FIXME: Correctly measure the length of whitespace in Split.second so it |
240 | | // works with tabs. |
241 | 1.66k | return RemainingTokenColumns + 1 - Split.second; |
242 | 1.66k | } |
243 | | |
244 | 22.3k | unsigned BreakableStringLiteral::getLineCount() const { return 1; } |
245 | | |
246 | | unsigned BreakableStringLiteral::getRangeLength(unsigned LineIndex, |
247 | | unsigned Offset, |
248 | | StringRef::size_type Length, |
249 | 0 | unsigned StartColumn) const { |
250 | 0 | llvm_unreachable("Getting the length of a part of the string literal " |
251 | 0 | "indicates that the code tries to reflow it."); |
252 | 0 | } |
253 | | |
254 | | unsigned |
255 | | BreakableStringLiteral::getRemainingLength(unsigned LineIndex, unsigned Offset, |
256 | 12.4k | unsigned StartColumn) const { |
257 | 12.4k | return UnbreakableTailLength + Postfix.size() + |
258 | 12.4k | encoding::columnWidthWithTabs(Line.substr(Offset), StartColumn, |
259 | 12.4k | Style.TabWidth, Encoding); |
260 | 12.4k | } |
261 | | |
262 | | unsigned BreakableStringLiteral::getContentStartColumn(unsigned LineIndex, |
263 | 12.4k | bool Break) const { |
264 | 12.4k | return StartColumn + Prefix.size(); |
265 | 12.4k | } |
266 | | |
267 | | BreakableStringLiteral::BreakableStringLiteral( |
268 | | const FormatToken &Tok, unsigned StartColumn, StringRef Prefix, |
269 | | StringRef Postfix, unsigned UnbreakableTailLength, bool InPPDirective, |
270 | | encoding::Encoding Encoding, const FormatStyle &Style) |
271 | | : BreakableToken(Tok, InPPDirective, Encoding, Style), |
272 | | StartColumn(StartColumn), Prefix(Prefix), Postfix(Postfix), |
273 | 11.1k | UnbreakableTailLength(UnbreakableTailLength) { |
274 | 11.1k | assert(Tok.TokenText.startswith(Prefix) && Tok.TokenText.endswith(Postfix)); |
275 | 11.1k | Line = Tok.TokenText.substr( |
276 | 11.1k | Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size()); |
277 | 11.1k | } |
278 | | |
279 | | BreakableToken::Split BreakableStringLiteral::getSplit( |
280 | | unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit, |
281 | 1.28k | unsigned ContentStartColumn, const llvm::Regex &CommentPragmasRegex) const { |
282 | 1.28k | return getStringSplit(Line.substr(TailOffset), ContentStartColumn, |
283 | 1.28k | ColumnLimit - Postfix.size(), Style.TabWidth, Encoding); |
284 | 1.28k | } |
285 | | |
286 | | void BreakableStringLiteral::insertBreak(unsigned LineIndex, |
287 | | unsigned TailOffset, Split Split, |
288 | | unsigned ContentIndent, |
289 | 126 | WhitespaceManager &Whitespaces) const { |
290 | 126 | Whitespaces.replaceWhitespaceInToken( |
291 | 126 | Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix, |
292 | 126 | Prefix, InPPDirective, 1, StartColumn); |
293 | 126 | } |
294 | | |
295 | | BreakableComment::BreakableComment(const FormatToken &Token, |
296 | | unsigned StartColumn, bool InPPDirective, |
297 | | encoding::Encoding Encoding, |
298 | | const FormatStyle &Style) |
299 | | : BreakableToken(Token, InPPDirective, Encoding, Style), |
300 | 18.6k | StartColumn(StartColumn) {} |
301 | | |
302 | 37.2k | unsigned BreakableComment::getLineCount() const { return Lines.size(); } |
303 | | |
304 | | BreakableToken::Split |
305 | | BreakableComment::getSplit(unsigned LineIndex, unsigned TailOffset, |
306 | | unsigned ColumnLimit, unsigned ContentStartColumn, |
307 | 3.00k | const llvm::Regex &CommentPragmasRegex) const { |
308 | | // Don't break lines matching the comment pragmas regex. |
309 | 3.00k | if (CommentPragmasRegex.match(Content[LineIndex])) |
310 | 0 | return Split(StringRef::npos, 0); |
311 | 3.00k | return getCommentSplit(Content[LineIndex].substr(TailOffset), |
312 | 3.00k | ContentStartColumn, ColumnLimit, Style.TabWidth, |
313 | 3.00k | Encoding, Style); |
314 | 3.00k | } |
315 | | |
316 | | void BreakableComment::compressWhitespace( |
317 | | unsigned LineIndex, unsigned TailOffset, Split Split, |
318 | 32 | WhitespaceManager &Whitespaces) const { |
319 | 32 | StringRef Text = Content[LineIndex].substr(TailOffset); |
320 | | // Text is relative to the content line, but Whitespaces operates relative to |
321 | | // the start of the corresponding token, so compute the start of the Split |
322 | | // that needs to be compressed into a single space relative to the start of |
323 | | // its token. |
324 | 32 | unsigned BreakOffsetInToken = |
325 | 32 | Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first; |
326 | 32 | unsigned CharsToRemove = Split.second; |
327 | 32 | Whitespaces.replaceWhitespaceInToken( |
328 | 32 | tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "", "", |
329 | 32 | /*InPPDirective=*/false, /*Newlines=*/0, /*Spaces=*/1); |
330 | 32 | } |
331 | | |
332 | 4.08k | const FormatToken &BreakableComment::tokenAt(unsigned LineIndex) const { |
333 | 4.08k | return Tokens[LineIndex] ? *Tokens[LineIndex]1.49k : Tok2.59k ; |
334 | 4.08k | } |
335 | | |
336 | 833 | static bool mayReflowContent(StringRef Content) { |
337 | 833 | Content = Content.trim(Blanks); |
338 | | // Lines starting with '@' commonly have special meaning. |
339 | | // Lines starting with '-', '-#', '+' or '*' are bulleted/numbered lists. |
340 | 833 | bool hasSpecialMeaningPrefix = false; |
341 | 833 | for (StringRef Prefix : |
342 | 6.53k | {"@", "TODO", "FIXME", "XXX", "-# ", "- ", "+ ", "* "}) { |
343 | 6.53k | if (Content.startswith(Prefix)) { |
344 | 30 | hasSpecialMeaningPrefix = true; |
345 | 30 | break; |
346 | 30 | } |
347 | 6.53k | } |
348 | | |
349 | | // Numbered lists may also start with a number followed by '.' |
350 | | // To avoid issues if a line starts with a number which is actually the end |
351 | | // of a previous line, we only consider numbers with up to 2 digits. |
352 | 833 | static const auto kNumberedListRegexp = llvm::Regex("^[1-9][0-9]?\\. "); |
353 | 833 | hasSpecialMeaningPrefix = |
354 | 833 | hasSpecialMeaningPrefix || kNumberedListRegexp.match(Content)803 ; |
355 | | |
356 | | // Simple heuristic for what to reflow: content should contain at least two |
357 | | // characters and either the first or second character must be |
358 | | // non-punctuation. |
359 | 833 | return Content.size() >= 2 && !hasSpecialMeaningPrefix714 && |
360 | 833 | !Content.endswith("\\")682 && |
361 | | // Note that this is UTF-8 safe, since if isPunctuation(Content[0]) is |
362 | | // true, then the first code point must be 1 byte long. |
363 | 833 | (682 !isPunctuation(Content[0])682 || !isPunctuation(Content[1])6 ); |
364 | 833 | } |
365 | | |
366 | | BreakableBlockComment::BreakableBlockComment( |
367 | | const FormatToken &Token, unsigned StartColumn, |
368 | | unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective, |
369 | | encoding::Encoding Encoding, const FormatStyle &Style, bool UseCRLF) |
370 | | : BreakableComment(Token, StartColumn, InPPDirective, Encoding, Style), |
371 | | DelimitersOnNewline(false), |
372 | 3.36k | UnbreakableTailLength(Token.UnbreakableTailLength) { |
373 | 3.36k | assert(Tok.is(TT_BlockComment) && |
374 | 3.36k | "block comment section must start with a block comment"); |
375 | | |
376 | 3.36k | StringRef TokenText(Tok.TokenText); |
377 | 3.36k | assert(TokenText.startswith("/*") && TokenText.endswith("*/")); |
378 | 3.36k | TokenText.substr(2, TokenText.size() - 4) |
379 | 3.36k | .split(Lines, UseCRLF ? "\r\n"8 : "\n"3.35k ); |
380 | | |
381 | 3.36k | int IndentDelta = StartColumn - OriginalStartColumn; |
382 | 3.36k | Content.resize(Lines.size()); |
383 | 3.36k | Content[0] = Lines[0]; |
384 | 3.36k | ContentColumn.resize(Lines.size()); |
385 | | // Account for the initial '/*'. |
386 | 3.36k | ContentColumn[0] = StartColumn + 2; |
387 | 3.36k | Tokens.resize(Lines.size()); |
388 | 4.56k | for (size_t i = 1; i < Lines.size(); ++i1.20k ) |
389 | 1.20k | adjustWhitespace(i, IndentDelta); |
390 | | |
391 | | // Align decorations with the column of the star on the first line, |
392 | | // that is one column after the start "/*". |
393 | 3.36k | DecorationColumn = StartColumn + 1; |
394 | | |
395 | | // Account for comment decoration patterns like this: |
396 | | // |
397 | | // /* |
398 | | // ** blah blah blah |
399 | | // */ |
400 | 3.36k | if (Lines.size() >= 2 && Content[1].startswith("**")577 && |
401 | 3.36k | static_cast<unsigned>(ContentColumn[1]) == StartColumn10 ) { |
402 | 6 | DecorationColumn = StartColumn; |
403 | 6 | } |
404 | | |
405 | 3.36k | Decoration = "* "; |
406 | 3.36k | if (Lines.size() == 1 && !FirstInLine2.78k ) { |
407 | | // Comments for which FirstInLine is false can start on arbitrary column, |
408 | | // and available horizontal space can be too small to align consecutive |
409 | | // lines with the first one. |
410 | | // FIXME: We could, probably, align them to current indentation level, but |
411 | | // now we just wrap them without stars. |
412 | 2.23k | Decoration = ""; |
413 | 2.23k | } |
414 | 4.02k | for (size_t i = 1, e = Content.size(); i < e && !Decoration.empty()1.06k ; ++i663 ) { |
415 | 880 | const StringRef &Text = Content[i]; |
416 | 880 | if (i + 1 == e) { |
417 | | // If the last line is empty, the closing "*/" will have a star. |
418 | 396 | if (Text.empty()) |
419 | 217 | break; |
420 | 484 | } else if (!Text.empty() && Decoration.startswith(Text)470 ) { |
421 | 19 | continue; |
422 | 19 | } |
423 | 1.21k | while (644 !Text.startswith(Decoration)) |
424 | 570 | Decoration = Decoration.drop_back(1); |
425 | 644 | } |
426 | | |
427 | 3.36k | LastLineNeedsDecoration = true; |
428 | 3.36k | IndentAtLineBreak = ContentColumn[0] + 1; |
429 | 4.56k | for (size_t i = 1, e = Lines.size(); i < e; ++i1.20k ) { |
430 | 1.20k | if (Content[i].empty()) { |
431 | 405 | if (i + 1 == e) { |
432 | | // Empty last line means that we already have a star as a part of the |
433 | | // trailing */. We also need to preserve whitespace, so that */ is |
434 | | // correctly indented. |
435 | 389 | LastLineNeedsDecoration = false; |
436 | | // Align the star in the last '*/' with the stars on the previous lines. |
437 | 389 | if (e >= 2 && !Decoration.empty()) |
438 | 217 | ContentColumn[i] = DecorationColumn; |
439 | 389 | } else if (16 Decoration.empty()16 ) { |
440 | | // For all other lines, set the start column to 0 if they're empty, so |
441 | | // we do not insert trailing whitespace anywhere. |
442 | 16 | ContentColumn[i] = 0; |
443 | 16 | } |
444 | 405 | continue; |
445 | 405 | } |
446 | | |
447 | | // The first line already excludes the star. |
448 | | // The last line excludes the star if LastLineNeedsDecoration is false. |
449 | | // For all other lines, adjust the line to exclude the star and |
450 | | // (optionally) the first whitespace. |
451 | 800 | unsigned DecorationSize = Decoration.startswith(Content[i]) |
452 | 800 | ? Content[i].size()21 |
453 | 800 | : Decoration.size()779 ; |
454 | 800 | if (DecorationSize) |
455 | 387 | ContentColumn[i] = DecorationColumn + DecorationSize; |
456 | 800 | Content[i] = Content[i].substr(DecorationSize); |
457 | 800 | if (!Decoration.startswith(Content[i])) { |
458 | 771 | IndentAtLineBreak = |
459 | 771 | std::min<int>(IndentAtLineBreak, std::max(0, ContentColumn[i])); |
460 | 771 | } |
461 | 800 | } |
462 | 3.36k | IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size()); |
463 | | |
464 | | // Detect a multiline jsdoc comment and set DelimitersOnNewline in that case. |
465 | 3.36k | if (Style.isJavaScript() || Style.Language == FormatStyle::LK_Java3.18k ) { |
466 | 200 | if ((Lines[0] == "*" || Lines[0].startswith("* ")110 ) && Lines.size() > 1150 ) { |
467 | | // This is a multiline jsdoc comment. |
468 | 102 | DelimitersOnNewline = true; |
469 | 102 | } else if (98 Lines[0].startswith("* ")98 && Lines.size() == 148 ) { |
470 | | // Detect a long single-line comment, like: |
471 | | // /** long long long */ |
472 | | // Below, '2' is the width of '*/'. |
473 | 48 | unsigned EndColumn = |
474 | 48 | ContentColumn[0] + |
475 | 48 | encoding::columnWidthWithTabs(Lines[0], ContentColumn[0], |
476 | 48 | Style.TabWidth, Encoding) + |
477 | 48 | 2; |
478 | 48 | DelimitersOnNewline = EndColumn > Style.ColumnLimit; |
479 | 48 | } |
480 | 200 | } |
481 | | |
482 | 3.36k | LLVM_DEBUG({ |
483 | 3.36k | llvm::dbgs() << "IndentAtLineBreak " << IndentAtLineBreak << "\n"; |
484 | 3.36k | llvm::dbgs() << "DelimitersOnNewline " << DelimitersOnNewline << "\n"; |
485 | 3.36k | for (size_t i = 0; i < Lines.size(); ++i) { |
486 | 3.36k | llvm::dbgs() << i << " |" << Content[i] << "| " |
487 | 3.36k | << "CC=" << ContentColumn[i] << "| " |
488 | 3.36k | << "IN=" << (Content[i].data() - Lines[i].data()) << "\n"; |
489 | 3.36k | } |
490 | 3.36k | }); |
491 | 3.36k | } |
492 | | |
493 | | BreakableToken::Split BreakableBlockComment::getSplit( |
494 | | unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit, |
495 | 1.02k | unsigned ContentStartColumn, const llvm::Regex &CommentPragmasRegex) const { |
496 | | // Don't break lines matching the comment pragmas regex. |
497 | 1.02k | if (CommentPragmasRegex.match(Content[LineIndex])) |
498 | 6 | return Split(StringRef::npos, 0); |
499 | 1.02k | return getCommentSplit(Content[LineIndex].substr(TailOffset), |
500 | 1.02k | ContentStartColumn, ColumnLimit, Style.TabWidth, |
501 | 1.02k | Encoding, Style, Decoration.endswith("*")); |
502 | 1.02k | } |
503 | | |
504 | | void BreakableBlockComment::adjustWhitespace(unsigned LineIndex, |
505 | 1.20k | int IndentDelta) { |
506 | | // When in a preprocessor directive, the trailing backslash in a block comment |
507 | | // is not needed, but can serve a purpose of uniformity with necessary escaped |
508 | | // newlines outside the comment. In this case we remove it here before |
509 | | // trimming the trailing whitespace. The backslash will be re-added later when |
510 | | // inserting a line break. |
511 | 1.20k | size_t EndOfPreviousLine = Lines[LineIndex - 1].size(); |
512 | 1.20k | if (InPPDirective && Lines[LineIndex - 1].endswith("\\")22 ) |
513 | 14 | --EndOfPreviousLine; |
514 | | |
515 | | // Calculate the end of the non-whitespace text in the previous line. |
516 | 1.20k | EndOfPreviousLine = |
517 | 1.20k | Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine); |
518 | 1.20k | if (EndOfPreviousLine == StringRef::npos) |
519 | 262 | EndOfPreviousLine = 0; |
520 | 943 | else |
521 | 943 | ++EndOfPreviousLine; |
522 | | // Calculate the start of the non-whitespace text in the current line. |
523 | 1.20k | size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks); |
524 | 1.20k | if (StartOfLine == StringRef::npos) |
525 | 405 | StartOfLine = Lines[LineIndex].size(); |
526 | | |
527 | 1.20k | StringRef Whitespace = Lines[LineIndex].substr(0, StartOfLine); |
528 | | // Adjust Lines to only contain relevant text. |
529 | 1.20k | size_t PreviousContentOffset = |
530 | 1.20k | Content[LineIndex - 1].data() - Lines[LineIndex - 1].data(); |
531 | 1.20k | Content[LineIndex - 1] = Lines[LineIndex - 1].substr( |
532 | 1.20k | PreviousContentOffset, EndOfPreviousLine - PreviousContentOffset); |
533 | 1.20k | Content[LineIndex] = Lines[LineIndex].substr(StartOfLine); |
534 | | |
535 | | // Adjust the start column uniformly across all lines. |
536 | 1.20k | ContentColumn[LineIndex] = |
537 | 1.20k | encoding::columnWidthWithTabs(Whitespace, 0, Style.TabWidth, Encoding) + |
538 | 1.20k | IndentDelta; |
539 | 1.20k | } |
540 | | |
541 | | unsigned BreakableBlockComment::getRangeLength(unsigned LineIndex, |
542 | | unsigned Offset, |
543 | | StringRef::size_type Length, |
544 | 5.97k | unsigned StartColumn) const { |
545 | 5.97k | return encoding::columnWidthWithTabs( |
546 | 5.97k | Content[LineIndex].substr(Offset, Length), StartColumn, Style.TabWidth, |
547 | 5.97k | Encoding); |
548 | 5.97k | } |
549 | | |
550 | | unsigned BreakableBlockComment::getRemainingLength(unsigned LineIndex, |
551 | | unsigned Offset, |
552 | 5.40k | unsigned StartColumn) const { |
553 | 5.40k | unsigned LineLength = |
554 | 5.40k | UnbreakableTailLength + |
555 | 5.40k | getRangeLength(LineIndex, Offset, StringRef::npos, StartColumn); |
556 | 5.40k | if (LineIndex + 1 == Lines.size()) { |
557 | 3.77k | LineLength += 2; |
558 | | // We never need a decoration when breaking just the trailing "*/" postfix. |
559 | 3.77k | bool HasRemainingText = Offset < Content[LineIndex].size(); |
560 | 3.77k | if (!HasRemainingText) { |
561 | 666 | bool HasDecoration = Lines[LineIndex].ltrim().startswith(Decoration); |
562 | 666 | if (HasDecoration) |
563 | 427 | LineLength -= Decoration.size(); |
564 | 666 | } |
565 | 3.77k | } |
566 | 5.40k | return LineLength; |
567 | 5.40k | } |
568 | | |
569 | | unsigned BreakableBlockComment::getContentStartColumn(unsigned LineIndex, |
570 | 4.94k | bool Break) const { |
571 | 4.94k | if (Break) |
572 | 448 | return IndentAtLineBreak; |
573 | 4.49k | return std::max(0, ContentColumn[LineIndex]); |
574 | 4.94k | } |
575 | | |
576 | | const llvm::StringSet<> |
577 | | BreakableBlockComment::ContentIndentingJavadocAnnotations = { |
578 | | "@param", "@return", "@returns", "@throws", "@type", "@template", |
579 | | "@see", "@deprecated", "@define", "@exports", "@mods", "@private", |
580 | | }; |
581 | | |
582 | 491 | unsigned BreakableBlockComment::getContentIndent(unsigned LineIndex) const { |
583 | 491 | if (Style.Language != FormatStyle::LK_Java && !Style.isJavaScript()469 ) |
584 | 404 | return 0; |
585 | | // The content at LineIndex 0 of a comment like: |
586 | | // /** line 0 */ |
587 | | // is "* line 0", so we need to skip over the decoration in that case. |
588 | 87 | StringRef ContentWithNoDecoration = Content[LineIndex]; |
589 | 87 | if (LineIndex == 0 && ContentWithNoDecoration.startswith("*")35 ) |
590 | 35 | ContentWithNoDecoration = ContentWithNoDecoration.substr(1).ltrim(Blanks); |
591 | 87 | StringRef FirstWord = ContentWithNoDecoration.substr( |
592 | 87 | 0, ContentWithNoDecoration.find_first_of(Blanks)); |
593 | 87 | if (ContentIndentingJavadocAnnotations.contains(FirstWord)) |
594 | 56 | return Style.ContinuationIndentWidth; |
595 | 31 | return 0; |
596 | 87 | } |
597 | | |
598 | | void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset, |
599 | | Split Split, unsigned ContentIndent, |
600 | 164 | WhitespaceManager &Whitespaces) const { |
601 | 164 | StringRef Text = Content[LineIndex].substr(TailOffset); |
602 | 164 | StringRef Prefix = Decoration; |
603 | | // We need this to account for the case when we have a decoration "* " for all |
604 | | // the lines except for the last one, where the star in "*/" acts as a |
605 | | // decoration. |
606 | 164 | unsigned LocalIndentAtLineBreak = IndentAtLineBreak; |
607 | 164 | if (LineIndex + 1 == Lines.size() && |
608 | 164 | Text.size() == Split.first + Split.second69 ) { |
609 | | // For the last line we need to break before "*/", but not to add "* ". |
610 | 17 | Prefix = ""; |
611 | 17 | if (LocalIndentAtLineBreak >= 2) |
612 | 17 | LocalIndentAtLineBreak -= 2; |
613 | 17 | } |
614 | | // The split offset is from the beginning of the line. Convert it to an offset |
615 | | // from the beginning of the token text. |
616 | 164 | unsigned BreakOffsetInToken = |
617 | 164 | Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first; |
618 | 164 | unsigned CharsToRemove = Split.second; |
619 | 164 | assert(LocalIndentAtLineBreak >= Prefix.size()); |
620 | 164 | std::string PrefixWithTrailingIndent = std::string(Prefix); |
621 | 164 | PrefixWithTrailingIndent.append(ContentIndent, ' '); |
622 | 164 | Whitespaces.replaceWhitespaceInToken( |
623 | 164 | tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "", |
624 | 164 | PrefixWithTrailingIndent, InPPDirective, /*Newlines=*/1, |
625 | 164 | /*Spaces=*/LocalIndentAtLineBreak + ContentIndent - |
626 | 164 | PrefixWithTrailingIndent.size()); |
627 | 164 | } |
628 | | |
629 | | BreakableToken::Split BreakableBlockComment::getReflowSplit( |
630 | 208 | unsigned LineIndex, const llvm::Regex &CommentPragmasRegex) const { |
631 | 208 | if (!mayReflow(LineIndex, CommentPragmasRegex)) |
632 | 119 | return Split(StringRef::npos, 0); |
633 | | |
634 | | // If we're reflowing into a line with content indent, only reflow the next |
635 | | // line if its starting whitespace matches the content indent. |
636 | 89 | size_t Trimmed = Content[LineIndex].find_first_not_of(Blanks); |
637 | 89 | if (LineIndex) { |
638 | 89 | unsigned PreviousContentIndent = getContentIndent(LineIndex - 1); |
639 | 89 | if (PreviousContentIndent && Trimmed != StringRef::npos6 && |
640 | 89 | Trimmed != PreviousContentIndent6 ) { |
641 | 4 | return Split(StringRef::npos, 0); |
642 | 4 | } |
643 | 89 | } |
644 | | |
645 | 85 | return Split(0, Trimmed != StringRef::npos ? Trimmed : 00 ); |
646 | 89 | } |
647 | | |
648 | 3.36k | bool BreakableBlockComment::introducesBreakBeforeToken() const { |
649 | | // A break is introduced when we want delimiters on newline. |
650 | 3.36k | return DelimitersOnNewline && |
651 | 3.36k | Lines[0].substr(1).find_first_not_of(Blanks) != StringRef::npos125 ; |
652 | 3.36k | } |
653 | | |
654 | | void BreakableBlockComment::reflow(unsigned LineIndex, |
655 | 29 | WhitespaceManager &Whitespaces) const { |
656 | 29 | StringRef TrimmedContent = Content[LineIndex].ltrim(Blanks); |
657 | | // Here we need to reflow. |
658 | 29 | assert(Tokens[LineIndex - 1] == Tokens[LineIndex] && |
659 | 29 | "Reflowing whitespace within a token"); |
660 | | // This is the offset of the end of the last line relative to the start of |
661 | | // the token text in the token. |
662 | 29 | unsigned WhitespaceOffsetInToken = Content[LineIndex - 1].data() + |
663 | 29 | Content[LineIndex - 1].size() - |
664 | 29 | tokenAt(LineIndex).TokenText.data(); |
665 | 29 | unsigned WhitespaceLength = TrimmedContent.data() - |
666 | 29 | tokenAt(LineIndex).TokenText.data() - |
667 | 29 | WhitespaceOffsetInToken; |
668 | 29 | Whitespaces.replaceWhitespaceInToken( |
669 | 29 | tokenAt(LineIndex), WhitespaceOffsetInToken, |
670 | 29 | /*ReplaceChars=*/WhitespaceLength, /*PreviousPostfix=*/"", |
671 | 29 | /*CurrentPrefix=*/ReflowPrefix, InPPDirective, /*Newlines=*/0, |
672 | 29 | /*Spaces=*/0); |
673 | 29 | } |
674 | | |
675 | | void BreakableBlockComment::adaptStartOfLine( |
676 | 1.79k | unsigned LineIndex, WhitespaceManager &Whitespaces) const { |
677 | 1.79k | if (LineIndex == 0) { |
678 | 1.28k | if (DelimitersOnNewline) { |
679 | | // Since we're breaking at index 1 below, the break position and the |
680 | | // break length are the same. |
681 | | // Note: this works because getCommentSplit is careful never to split at |
682 | | // the beginning of a line. |
683 | 55 | size_t BreakLength = Lines[0].substr(1).find_first_not_of(Blanks); |
684 | 55 | if (BreakLength != StringRef::npos) { |
685 | 12 | insertBreak(LineIndex, 0, Split(1, BreakLength), /*ContentIndent=*/0, |
686 | 12 | Whitespaces); |
687 | 12 | } |
688 | 55 | } |
689 | 1.28k | return; |
690 | 1.28k | } |
691 | | // Here no reflow with the previous line will happen. |
692 | | // Fix the decoration of the line at LineIndex. |
693 | 514 | StringRef Prefix = Decoration; |
694 | 514 | if (Content[LineIndex].empty()) { |
695 | 193 | if (LineIndex + 1 == Lines.size()) { |
696 | 176 | if (!LastLineNeedsDecoration) { |
697 | | // If the last line was empty, we don't need a prefix, as the */ will |
698 | | // line up with the decoration (if it exists). |
699 | 175 | Prefix = ""; |
700 | 175 | } |
701 | 176 | } else if (17 !Decoration.empty()17 ) { |
702 | | // For other empty lines, if we do have a decoration, adapt it to not |
703 | | // contain a trailing whitespace. |
704 | 9 | Prefix = Prefix.substr(0, 1); |
705 | 9 | } |
706 | 321 | } else if (ContentColumn[LineIndex] == 1) { |
707 | | // This line starts immediately after the decorating *. |
708 | 9 | Prefix = Prefix.substr(0, 1); |
709 | 9 | } |
710 | | // This is the offset of the end of the last line relative to the start of the |
711 | | // token text in the token. |
712 | 514 | unsigned WhitespaceOffsetInToken = Content[LineIndex - 1].data() + |
713 | 514 | Content[LineIndex - 1].size() - |
714 | 514 | tokenAt(LineIndex).TokenText.data(); |
715 | 514 | unsigned WhitespaceLength = Content[LineIndex].data() - |
716 | 514 | tokenAt(LineIndex).TokenText.data() - |
717 | 514 | WhitespaceOffsetInToken; |
718 | 514 | Whitespaces.replaceWhitespaceInToken( |
719 | 514 | tokenAt(LineIndex), WhitespaceOffsetInToken, WhitespaceLength, "", Prefix, |
720 | 514 | InPPDirective, /*Newlines=*/1, ContentColumn[LineIndex] - Prefix.size()); |
721 | 514 | } |
722 | | |
723 | | BreakableToken::Split |
724 | 3.36k | BreakableBlockComment::getSplitAfterLastLine(unsigned TailOffset) const { |
725 | 3.36k | if (DelimitersOnNewline) { |
726 | | // Replace the trailing whitespace of the last line with a newline. |
727 | | // In case the last line is empty, the ending '*/' is already on its own |
728 | | // line. |
729 | 125 | StringRef Line = Content.back().substr(TailOffset); |
730 | 125 | StringRef TrimmedLine = Line.rtrim(Blanks); |
731 | 125 | if (!TrimmedLine.empty()) |
732 | 12 | return Split(TrimmedLine.size(), Line.size() - TrimmedLine.size()); |
733 | 125 | } |
734 | 3.35k | return Split(StringRef::npos, 0); |
735 | 3.36k | } |
736 | | |
737 | | bool BreakableBlockComment::mayReflow( |
738 | 208 | unsigned LineIndex, const llvm::Regex &CommentPragmasRegex) const { |
739 | | // Content[LineIndex] may exclude the indent after the '*' decoration. In that |
740 | | // case, we compute the start of the comment pragma manually. |
741 | 208 | StringRef IndentContent = Content[LineIndex]; |
742 | 208 | if (Lines[LineIndex].ltrim(Blanks).startswith("*")) |
743 | 91 | IndentContent = Lines[LineIndex].ltrim(Blanks).substr(1); |
744 | 208 | return LineIndex > 0 && !CommentPragmasRegex.match(IndentContent) && |
745 | 208 | mayReflowContent(Content[LineIndex])206 && !Tok.Finalized89 && |
746 | 208 | !switchesFormatting(tokenAt(LineIndex))89 ; |
747 | 208 | } |
748 | | |
749 | | BreakableLineCommentSection::BreakableLineCommentSection( |
750 | | const FormatToken &Token, unsigned StartColumn, bool InPPDirective, |
751 | | encoding::Encoding Encoding, const FormatStyle &Style) |
752 | 15.2k | : BreakableComment(Token, StartColumn, InPPDirective, Encoding, Style) { |
753 | 15.2k | assert(Tok.is(TT_LineComment) && |
754 | 15.2k | "line comment section must start with a line comment"); |
755 | 15.2k | FormatToken *LineTok = nullptr; |
756 | 15.2k | const int Minimum = Style.SpacesInLineCommentPrefix.Minimum; |
757 | | // How many spaces we changed in the first line of the section, this will be |
758 | | // applied in all following lines |
759 | 15.2k | int FirstLineSpaceChange = 0; |
760 | 15.2k | for (const FormatToken *CurrentTok = &Tok; |
761 | 22.2k | CurrentTok && CurrentTok->is(TT_LineComment)17.0k ; |
762 | 17.0k | CurrentTok = CurrentTok->Next7.04k ) { |
763 | 17.0k | LastLineTok = LineTok; |
764 | 17.0k | StringRef TokenText(CurrentTok->TokenText); |
765 | 17.0k | assert((TokenText.startswith("//") || TokenText.startswith("#")) && |
766 | 17.0k | "unsupported line comment prefix, '//' and '#' are supported"); |
767 | 17.0k | size_t FirstLineIndex = Lines.size(); |
768 | 17.0k | TokenText.split(Lines, "\n"); |
769 | 17.0k | Content.resize(Lines.size()); |
770 | 17.0k | ContentColumn.resize(Lines.size()); |
771 | 17.0k | PrefixSpaceChange.resize(Lines.size()); |
772 | 17.0k | Tokens.resize(Lines.size()); |
773 | 17.0k | Prefix.resize(Lines.size()); |
774 | 17.0k | OriginalPrefix.resize(Lines.size()); |
775 | 34.1k | for (size_t i = FirstLineIndex, e = Lines.size(); i < e; ++i17.1k ) { |
776 | 17.1k | Lines[i] = Lines[i].ltrim(Blanks); |
777 | 17.1k | StringRef IndentPrefix = getLineCommentIndentPrefix(Lines[i], Style); |
778 | 17.1k | OriginalPrefix[i] = IndentPrefix; |
779 | 17.1k | const int SpacesInPrefix = llvm::count(IndentPrefix, ' '); |
780 | | |
781 | | // This lambda also considers multibyte character that is not handled in |
782 | | // functions like isPunctuation provided by CharInfo. |
783 | 17.1k | const auto NoSpaceBeforeFirstCommentChar = [&]() { |
784 | 14.4k | assert(Lines[i].size() > IndentPrefix.size()); |
785 | 14.4k | const char FirstCommentChar = Lines[i][IndentPrefix.size()]; |
786 | 14.4k | const unsigned FirstCharByteSize = |
787 | 14.4k | encoding::getCodePointNumBytes(FirstCommentChar, Encoding); |
788 | 14.4k | if (encoding::columnWidth( |
789 | 14.4k | Lines[i].substr(IndentPrefix.size(), FirstCharByteSize), |
790 | 14.4k | Encoding) != 1) { |
791 | 40 | return false; |
792 | 40 | } |
793 | | // In C-like comments, add a space before #. For example this is useful |
794 | | // to preserve the relative indentation when commenting out code with |
795 | | // #includes. |
796 | | // |
797 | | // In languages using # as the comment leader such as proto, don't |
798 | | // add a space to support patterns like: |
799 | | // ######### |
800 | | // # section |
801 | | // ######### |
802 | 14.4k | if (FirstCommentChar == '#' && !TokenText.startswith("#")20 ) |
803 | 12 | return false; |
804 | 14.4k | return FirstCommentChar == '\\' || isPunctuation(FirstCommentChar)14.3k || |
805 | 14.4k | isHorizontalWhitespace(FirstCommentChar)13.9k ; |
806 | 14.4k | }; |
807 | | |
808 | | // On the first line of the comment section we calculate how many spaces |
809 | | // are to be added or removed, all lines after that just get only the |
810 | | // change and we will not look at the maximum anymore. Additionally to the |
811 | | // actual first line, we calculate that when the non space Prefix changes, |
812 | | // e.g. from "///" to "//". |
813 | 17.1k | if (i == 0 || OriginalPrefix[i].rtrim(Blanks) != |
814 | 15.2k | OriginalPrefix[i - 1].rtrim(Blanks)) { |
815 | 15.2k | if (SpacesInPrefix < Minimum && Lines[i].size() > IndentPrefix.size()3.09k && |
816 | 15.2k | !NoSpaceBeforeFirstCommentChar()252 ) { |
817 | 176 | FirstLineSpaceChange = Minimum - SpacesInPrefix; |
818 | 15.0k | } else if (static_cast<unsigned>(SpacesInPrefix) > |
819 | 15.0k | Style.SpacesInLineCommentPrefix.Maximum) { |
820 | 80 | FirstLineSpaceChange = |
821 | 80 | Style.SpacesInLineCommentPrefix.Maximum - SpacesInPrefix; |
822 | 14.9k | } else { |
823 | 14.9k | FirstLineSpaceChange = 0; |
824 | 14.9k | } |
825 | 15.2k | } |
826 | | |
827 | 17.1k | if (Lines[i].size() != IndentPrefix.size()) { |
828 | 14.2k | PrefixSpaceChange[i] = FirstLineSpaceChange; |
829 | | |
830 | 14.2k | if (SpacesInPrefix + PrefixSpaceChange[i] < Minimum) { |
831 | 104 | PrefixSpaceChange[i] += |
832 | 104 | Minimum - (SpacesInPrefix + PrefixSpaceChange[i]); |
833 | 104 | } |
834 | | |
835 | 14.2k | assert(Lines[i].size() > IndentPrefix.size()); |
836 | 14.2k | const auto FirstNonSpace = Lines[i][IndentPrefix.size()]; |
837 | 14.2k | const bool IsFormatComment = LineTok && switchesFormatting(*LineTok)1.83k ; |
838 | 14.2k | const bool LineRequiresLeadingSpace = |
839 | 14.2k | !NoSpaceBeforeFirstCommentChar() || |
840 | 14.2k | (463 FirstNonSpace == '}'463 && FirstLineSpaceChange != 048 ); |
841 | 14.2k | const bool AllowsSpaceChange = |
842 | 14.2k | !IsFormatComment && |
843 | 14.2k | (14.2k SpacesInPrefix != 014.2k || LineRequiresLeadingSpace357 ); |
844 | | |
845 | 14.2k | if (PrefixSpaceChange[i] > 0 && AllowsSpaceChange364 ) { |
846 | 272 | Prefix[i] = IndentPrefix.str(); |
847 | 272 | Prefix[i].append(PrefixSpaceChange[i], ' '); |
848 | 13.9k | } else if (PrefixSpaceChange[i] < 0 && AllowsSpaceChange136 ) { |
849 | 136 | Prefix[i] = IndentPrefix |
850 | 136 | .drop_back(std::min<std::size_t>( |
851 | 136 | -PrefixSpaceChange[i], SpacesInPrefix)) |
852 | 136 | .str(); |
853 | 13.8k | } else { |
854 | 13.8k | Prefix[i] = IndentPrefix.str(); |
855 | 13.8k | } |
856 | 14.2k | } else { |
857 | | // If the IndentPrefix is the whole line, there is no content and we |
858 | | // drop just all space |
859 | 2.87k | Prefix[i] = IndentPrefix.drop_back(SpacesInPrefix).str(); |
860 | 2.87k | } |
861 | | |
862 | 17.1k | Tokens[i] = LineTok; |
863 | 17.1k | Content[i] = Lines[i].substr(IndentPrefix.size()); |
864 | 17.1k | ContentColumn[i] = |
865 | 17.1k | StartColumn + encoding::columnWidthWithTabs(Prefix[i], StartColumn, |
866 | 17.1k | Style.TabWidth, Encoding); |
867 | | |
868 | | // Calculate the end of the non-whitespace text in this line. |
869 | 17.1k | size_t EndOfLine = Content[i].find_last_not_of(Blanks); |
870 | 17.1k | if (EndOfLine == StringRef::npos) |
871 | 2.87k | EndOfLine = Content[i].size(); |
872 | 14.2k | else |
873 | 14.2k | ++EndOfLine; |
874 | 17.1k | Content[i] = Content[i].substr(0, EndOfLine); |
875 | 17.1k | } |
876 | 17.0k | LineTok = CurrentTok->Next; |
877 | 17.0k | if (CurrentTok->Next && !CurrentTok->Next->ContinuesLineCommentSection11.9k ) { |
878 | | // A line comment section needs to broken by a line comment that is |
879 | | // preceded by at least two newlines. Note that we put this break here |
880 | | // instead of breaking at a previous stage during parsing, since that |
881 | | // would split the contents of the enum into two unwrapped lines in this |
882 | | // example, which is undesirable: |
883 | | // enum A { |
884 | | // a, // comment about a |
885 | | // |
886 | | // // comment about b |
887 | | // b |
888 | | // }; |
889 | | // |
890 | | // FIXME: Consider putting separate line comment sections as children to |
891 | | // the unwrapped line instead. |
892 | 10.0k | break; |
893 | 10.0k | } |
894 | 17.0k | } |
895 | 15.2k | } |
896 | | |
897 | | unsigned |
898 | | BreakableLineCommentSection::getRangeLength(unsigned LineIndex, unsigned Offset, |
899 | | StringRef::size_type Length, |
900 | 21.2k | unsigned StartColumn) const { |
901 | 21.2k | return encoding::columnWidthWithTabs( |
902 | 21.2k | Content[LineIndex].substr(Offset, Length), StartColumn, Style.TabWidth, |
903 | 21.2k | Encoding); |
904 | 21.2k | } |
905 | | |
906 | | unsigned |
907 | | BreakableLineCommentSection::getContentStartColumn(unsigned LineIndex, |
908 | 17.7k | bool /*Break*/) const { |
909 | 17.7k | return ContentColumn[LineIndex]; |
910 | 17.7k | } |
911 | | |
912 | | void BreakableLineCommentSection::insertBreak( |
913 | | unsigned LineIndex, unsigned TailOffset, Split Split, |
914 | 486 | unsigned ContentIndent, WhitespaceManager &Whitespaces) const { |
915 | 486 | StringRef Text = Content[LineIndex].substr(TailOffset); |
916 | | // Compute the offset of the split relative to the beginning of the token |
917 | | // text. |
918 | 486 | unsigned BreakOffsetInToken = |
919 | 486 | Text.data() - tokenAt(LineIndex).TokenText.data() + Split.first; |
920 | 486 | unsigned CharsToRemove = Split.second; |
921 | 486 | Whitespaces.replaceWhitespaceInToken( |
922 | 486 | tokenAt(LineIndex), BreakOffsetInToken, CharsToRemove, "", |
923 | 486 | Prefix[LineIndex], InPPDirective, /*Newlines=*/1, |
924 | 486 | /*Spaces=*/ContentColumn[LineIndex] - Prefix[LineIndex].size()); |
925 | 486 | } |
926 | | |
927 | | BreakableComment::Split BreakableLineCommentSection::getReflowSplit( |
928 | 627 | unsigned LineIndex, const llvm::Regex &CommentPragmasRegex) const { |
929 | 627 | if (!mayReflow(LineIndex, CommentPragmasRegex)) |
930 | 92 | return Split(StringRef::npos, 0); |
931 | | |
932 | 535 | size_t Trimmed = Content[LineIndex].find_first_not_of(Blanks); |
933 | | |
934 | | // In a line comment section each line is a separate token; thus, after a |
935 | | // split we replace all whitespace before the current line comment token |
936 | | // (which does not need to be included in the split), plus the start of the |
937 | | // line up to where the content starts. |
938 | 535 | return Split(0, Trimmed != StringRef::npos ? Trimmed : 00 ); |
939 | 627 | } |
940 | | |
941 | | void BreakableLineCommentSection::reflow(unsigned LineIndex, |
942 | 224 | WhitespaceManager &Whitespaces) const { |
943 | 224 | if (LineIndex > 0 && Tokens[LineIndex] != Tokens[LineIndex - 1]) { |
944 | | // Reflow happens between tokens. Replace the whitespace between the |
945 | | // tokens by the empty string. |
946 | 223 | Whitespaces.replaceWhitespace( |
947 | 223 | *Tokens[LineIndex], /*Newlines=*/0, /*Spaces=*/0, |
948 | 223 | /*StartOfTokenColumn=*/StartColumn, /*IsAligned=*/true, |
949 | 223 | /*InPPDirective=*/false); |
950 | 223 | } else if (1 LineIndex > 01 ) { |
951 | | // In case we're reflowing after the '\' in: |
952 | | // |
953 | | // // line comment \ |
954 | | // // line 2 |
955 | | // |
956 | | // the reflow happens inside the single comment token (it is a single line |
957 | | // comment with an unescaped newline). |
958 | | // Replace the whitespace between the '\' and '//' with the empty string. |
959 | | // |
960 | | // Offset points to after the '\' relative to start of the token. |
961 | 1 | unsigned Offset = Lines[LineIndex - 1].data() + |
962 | 1 | Lines[LineIndex - 1].size() - |
963 | 1 | tokenAt(LineIndex - 1).TokenText.data(); |
964 | | // WhitespaceLength is the number of chars between the '\' and the '//' on |
965 | | // the next line. |
966 | 1 | unsigned WhitespaceLength = |
967 | 1 | Lines[LineIndex].data() - tokenAt(LineIndex).TokenText.data() - Offset; |
968 | 1 | Whitespaces.replaceWhitespaceInToken(*Tokens[LineIndex], Offset, |
969 | 1 | /*ReplaceChars=*/WhitespaceLength, |
970 | 1 | /*PreviousPostfix=*/"", |
971 | 1 | /*CurrentPrefix=*/"", |
972 | 1 | /*InPPDirective=*/false, |
973 | 1 | /*Newlines=*/0, |
974 | 1 | /*Spaces=*/0); |
975 | 1 | } |
976 | | // Replace the indent and prefix of the token with the reflow prefix. |
977 | 224 | unsigned Offset = |
978 | 224 | Lines[LineIndex].data() - tokenAt(LineIndex).TokenText.data(); |
979 | 224 | unsigned WhitespaceLength = |
980 | 224 | Content[LineIndex].data() - Lines[LineIndex].data(); |
981 | 224 | Whitespaces.replaceWhitespaceInToken(*Tokens[LineIndex], Offset, |
982 | 224 | /*ReplaceChars=*/WhitespaceLength, |
983 | 224 | /*PreviousPostfix=*/"", |
984 | 224 | /*CurrentPrefix=*/ReflowPrefix, |
985 | 224 | /*InPPDirective=*/false, |
986 | 224 | /*Newlines=*/0, |
987 | 224 | /*Spaces=*/0); |
988 | 224 | } |
989 | | |
990 | | void BreakableLineCommentSection::adaptStartOfLine( |
991 | 3.92k | unsigned LineIndex, WhitespaceManager &Whitespaces) const { |
992 | | // If this is the first line of a token, we need to inform Whitespace Manager |
993 | | // about it: either adapt the whitespace range preceding it, or mark it as an |
994 | | // untouchable token. |
995 | | // This happens for instance here: |
996 | | // // line 1 \ |
997 | | // // line 2 |
998 | 3.92k | if (LineIndex > 0 && Tokens[LineIndex] != Tokens[LineIndex - 1]634 ) { |
999 | | // This is the first line for the current token, but no reflow with the |
1000 | | // previous token is necessary. However, we still may need to adjust the |
1001 | | // start column. Note that ContentColumn[LineIndex] is the expected |
1002 | | // content column after a possible update to the prefix, hence the prefix |
1003 | | // length change is included. |
1004 | 631 | unsigned LineColumn = |
1005 | 631 | ContentColumn[LineIndex] - |
1006 | 631 | (Content[LineIndex].data() - Lines[LineIndex].data()) + |
1007 | 631 | (OriginalPrefix[LineIndex].size() - Prefix[LineIndex].size()); |
1008 | | |
1009 | | // We always want to create a replacement instead of adding an untouchable |
1010 | | // token, even if LineColumn is the same as the original column of the |
1011 | | // token. This is because WhitespaceManager doesn't align trailing |
1012 | | // comments if they are untouchable. |
1013 | 631 | Whitespaces.replaceWhitespace(*Tokens[LineIndex], |
1014 | 631 | /*Newlines=*/1, |
1015 | 631 | /*Spaces=*/LineColumn, |
1016 | 631 | /*StartOfTokenColumn=*/LineColumn, |
1017 | 631 | /*IsAligned=*/true, |
1018 | 631 | /*InPPDirective=*/false); |
1019 | 631 | } |
1020 | 3.92k | if (OriginalPrefix[LineIndex] != Prefix[LineIndex]) { |
1021 | | // Adjust the prefix if necessary. |
1022 | 185 | const auto SpacesToRemove = -std::min(PrefixSpaceChange[LineIndex], 0); |
1023 | 185 | const auto SpacesToAdd = std::max(PrefixSpaceChange[LineIndex], 0); |
1024 | 185 | Whitespaces.replaceWhitespaceInToken( |
1025 | 185 | tokenAt(LineIndex), OriginalPrefix[LineIndex].size() - SpacesToRemove, |
1026 | 185 | /*ReplaceChars=*/SpacesToRemove, "", "", /*InPPDirective=*/false, |
1027 | 185 | /*Newlines=*/0, /*Spaces=*/SpacesToAdd); |
1028 | 185 | } |
1029 | 3.92k | } |
1030 | | |
1031 | 15.2k | void BreakableLineCommentSection::updateNextToken(LineState &State) const { |
1032 | 15.2k | if (LastLineTok) |
1033 | 809 | State.NextToken = LastLineTok->Next; |
1034 | 15.2k | } |
1035 | | |
1036 | | bool BreakableLineCommentSection::mayReflow( |
1037 | 627 | unsigned LineIndex, const llvm::Regex &CommentPragmasRegex) const { |
1038 | | // Line comments have the indent as part of the prefix, so we need to |
1039 | | // recompute the start of the line. |
1040 | 627 | StringRef IndentContent = Content[LineIndex]; |
1041 | 627 | if (Lines[LineIndex].startswith("//")) |
1042 | 617 | IndentContent = Lines[LineIndex].substr(2); |
1043 | | // FIXME: Decide whether we want to reflow non-regular indents: |
1044 | | // Currently, we only reflow when the OriginalPrefix[LineIndex] matches the |
1045 | | // OriginalPrefix[LineIndex-1]. That means we don't reflow |
1046 | | // // text that protrudes |
1047 | | // // into text with different indent |
1048 | | // We do reflow in that case in block comments. |
1049 | 627 | return LineIndex > 0 && !CommentPragmasRegex.match(IndentContent) && |
1050 | 627 | mayReflowContent(Content[LineIndex]) && !Tok.Finalized591 && |
1051 | 627 | !switchesFormatting(tokenAt(LineIndex))591 && |
1052 | 627 | OriginalPrefix[LineIndex] == OriginalPrefix[LineIndex - 1]591 ; |
1053 | 627 | } |
1054 | | |
1055 | | } // namespace format |
1056 | | } // namespace clang |