blob: 816fd237bbac4f4ebf878ee5deade5a1ca5e7080 [file] [log] [blame]
Alexander Kornienko70ce7882013-04-15 14:28:00 +00001//===--- BreakableToken.cpp - Format C++ code -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief Contains implementation of BreakableToken class and classes derived
12/// from it.
13///
14//===----------------------------------------------------------------------===//
15
Manuel Klimekde008c02013-05-27 15:23:34 +000016#define DEBUG_TYPE "format-token-breaker"
17
Alexander Kornienko70ce7882013-04-15 14:28:00 +000018#include "BreakableToken.h"
Alexander Kornienko2b2faa52013-06-11 16:01:49 +000019#include "clang/Basic/CharInfo.h"
Manuel Klimekde008c02013-05-27 15:23:34 +000020#include "clang/Format/Format.h"
Alexander Kornienko919398b2013-04-17 17:34:05 +000021#include "llvm/ADT/STLExtras.h"
Manuel Klimekde008c02013-05-27 15:23:34 +000022#include "llvm/Support/Debug.h"
Alexander Kornienko70ce7882013-04-15 14:28:00 +000023#include <algorithm>
24
25namespace clang {
26namespace format {
27
Craig Toppercf1e2162013-06-30 22:44:02 +000028static const char *const Blanks = " \t\v\f";
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000029static bool IsBlank(char C) {
30 switch (C) {
Daniel Jasper2a409b62013-07-08 14:34:09 +000031 case ' ':
32 case '\t':
33 case '\v':
34 case '\f':
35 return true;
36 default:
37 return false;
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000038 }
39}
40
Craig Topperb8f71642013-07-01 03:38:29 +000041static BreakableToken::Split getCommentSplit(StringRef Text,
42 unsigned ContentStartColumn,
43 unsigned ColumnLimit,
44 encoding::Encoding Encoding) {
Alexander Kornienko919398b2013-04-17 17:34:05 +000045 if (ColumnLimit <= ContentStartColumn + 1)
Manuel Klimekde008c02013-05-27 15:23:34 +000046 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko919398b2013-04-17 17:34:05 +000047
48 unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1;
Alexander Kornienko00895102013-06-05 14:09:10 +000049 unsigned MaxSplitBytes = 0;
50
51 for (unsigned NumChars = 0;
52 NumChars < MaxSplit && MaxSplitBytes < Text.size(); ++NumChars)
53 MaxSplitBytes +=
54 encoding::getCodePointNumBytes(Text[MaxSplitBytes], Encoding);
55
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000056 StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes);
Alexander Kornienko919398b2013-04-17 17:34:05 +000057 if (SpaceOffset == StringRef::npos ||
Manuel Klimekde008c02013-05-27 15:23:34 +000058 // Don't break at leading whitespace.
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000059 Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos) {
Manuel Klimekbe9ed772013-05-29 22:06:18 +000060 // Make sure that we don't break at leading whitespace that
61 // reaches past MaxSplit.
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000062 StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(Blanks);
Manuel Klimekbe9ed772013-05-29 22:06:18 +000063 if (FirstNonWhitespace == StringRef::npos)
64 // If the comment is only whitespace, we cannot split.
65 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000066 SpaceOffset = Text.find_first_of(
67 Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace));
Manuel Klimekbe9ed772013-05-29 22:06:18 +000068 }
Alexander Kornienko919398b2013-04-17 17:34:05 +000069 if (SpaceOffset != StringRef::npos && SpaceOffset != 0) {
Alexander Kornienko8afa39b2013-06-20 13:58:37 +000070 StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks);
71 StringRef AfterCut = Text.substr(SpaceOffset).ltrim(Blanks);
Alexander Kornienko919398b2013-04-17 17:34:05 +000072 return BreakableToken::Split(BeforeCut.size(),
73 AfterCut.begin() - BeforeCut.end());
74 }
75 return BreakableToken::Split(StringRef::npos, 0);
76}
77
Craig Topperb8f71642013-07-01 03:38:29 +000078static BreakableToken::Split getStringSplit(StringRef Text,
79 unsigned ContentStartColumn,
80 unsigned ColumnLimit,
81 encoding::Encoding Encoding) {
Manuel Klimekde008c02013-05-27 15:23:34 +000082 // FIXME: Reduce unit test case.
83 if (Text.empty())
84 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko00895102013-06-05 14:09:10 +000085 if (ColumnLimit <= ContentStartColumn)
Manuel Klimekde008c02013-05-27 15:23:34 +000086 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko00895102013-06-05 14:09:10 +000087 unsigned MaxSplit =
88 std::min<unsigned>(ColumnLimit - ContentStartColumn,
89 encoding::getCodePointCount(Text, Encoding) - 1);
90 StringRef::size_type SpaceOffset = 0;
91 StringRef::size_type SlashOffset = 0;
Alexander Kornienkof2b2c7d2013-06-19 14:22:47 +000092 StringRef::size_type WordStartOffset = 0;
Alexander Kornienko00895102013-06-05 14:09:10 +000093 StringRef::size_type SplitPoint = 0;
94 for (unsigned Chars = 0;;) {
95 unsigned Advance;
96 if (Text[0] == '\\') {
97 Advance = encoding::getEscapeSequenceLength(Text);
98 Chars += Advance;
99 } else {
100 Advance = encoding::getCodePointNumBytes(Text[0], Encoding);
101 Chars += 1;
102 }
103
104 if (Chars > MaxSplit)
105 break;
106
Alexander Kornienko8afa39b2013-06-20 13:58:37 +0000107 if (IsBlank(Text[0]))
Alexander Kornienko00895102013-06-05 14:09:10 +0000108 SpaceOffset = SplitPoint;
109 if (Text[0] == '/')
110 SlashOffset = SplitPoint;
Alexander Kornienko8afa39b2013-06-20 13:58:37 +0000111 if (Advance == 1 && !isAlphanumeric(Text[0]))
Alexander Kornienkof2b2c7d2013-06-19 14:22:47 +0000112 WordStartOffset = SplitPoint;
Alexander Kornienko00895102013-06-05 14:09:10 +0000113
114 SplitPoint += Advance;
115 Text = Text.substr(Advance);
116 }
117
118 if (SpaceOffset != 0)
119 return BreakableToken::Split(SpaceOffset + 1, 0);
120 if (SlashOffset != 0)
121 return BreakableToken::Split(SlashOffset + 1, 0);
Alexander Kornienkof2b2c7d2013-06-19 14:22:47 +0000122 if (WordStartOffset != 0)
123 return BreakableToken::Split(WordStartOffset + 1, 0);
Alexander Kornienko00895102013-06-05 14:09:10 +0000124 if (SplitPoint != 0)
125 return BreakableToken::Split(SplitPoint, 0);
126 return BreakableToken::Split(StringRef::npos, 0);
Alexander Kornienko919398b2013-04-17 17:34:05 +0000127}
128
Manuel Klimekde008c02013-05-27 15:23:34 +0000129unsigned BreakableSingleLineToken::getLineCount() const { return 1; }
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000130
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000131unsigned BreakableSingleLineToken::getLineLengthAfterSplit(
132 unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const {
Alexander Kornienko00895102013-06-05 14:09:10 +0000133 return StartColumn + Prefix.size() + Postfix.size() +
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000134 encoding::getCodePointCount(Line.substr(Offset, Length), Encoding);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000135}
136
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000137BreakableSingleLineToken::BreakableSingleLineToken(
138 const FormatToken &Tok, unsigned StartColumn, StringRef Prefix,
139 StringRef Postfix, bool InPPDirective, encoding::Encoding Encoding)
140 : BreakableToken(Tok, InPPDirective, Encoding), StartColumn(StartColumn),
141 Prefix(Prefix), Postfix(Postfix) {
Manuel Klimekde008c02013-05-27 15:23:34 +0000142 assert(Tok.TokenText.startswith(Prefix) && Tok.TokenText.endswith(Postfix));
143 Line = Tok.TokenText.substr(
144 Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size());
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000145}
146
Manuel Klimekde008c02013-05-27 15:23:34 +0000147BreakableStringLiteral::BreakableStringLiteral(const FormatToken &Tok,
Alexander Kornienko00895102013-06-05 14:09:10 +0000148 unsigned StartColumn,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000149 bool InPPDirective,
Alexander Kornienko00895102013-06-05 14:09:10 +0000150 encoding::Encoding Encoding)
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000151 : BreakableSingleLineToken(Tok, StartColumn, "\"", "\"", InPPDirective,
152 Encoding) {}
Manuel Klimekde008c02013-05-27 15:23:34 +0000153
154BreakableToken::Split
155BreakableStringLiteral::getSplit(unsigned LineIndex, unsigned TailOffset,
156 unsigned ColumnLimit) const {
Alexander Kornienko00895102013-06-05 14:09:10 +0000157 return getStringSplit(Line.substr(TailOffset), StartColumn + 2, ColumnLimit,
158 Encoding);
Alexander Kornienko919398b2013-04-17 17:34:05 +0000159}
160
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000161void BreakableStringLiteral::insertBreak(unsigned LineIndex,
162 unsigned TailOffset, Split Split,
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000163 WhitespaceManager &Whitespaces) {
164 Whitespaces.replaceWhitespaceInToken(
165 Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix,
166 Prefix, InPPDirective, 1, StartColumn);
167}
168
Manuel Klimekde008c02013-05-27 15:23:34 +0000169static StringRef getLineCommentPrefix(StringRef Comment) {
Alexander Kornienko919398b2013-04-17 17:34:05 +0000170 const char *KnownPrefixes[] = { "/// ", "///", "// ", "//" };
Manuel Klimekde008c02013-05-27 15:23:34 +0000171 for (size_t i = 0, e = llvm::array_lengthof(KnownPrefixes); i != e; ++i)
Alexander Kornienko919398b2013-04-17 17:34:05 +0000172 if (Comment.startswith(KnownPrefixes[i]))
173 return KnownPrefixes[i];
174 return "";
175}
176
Manuel Klimekde008c02013-05-27 15:23:34 +0000177BreakableLineComment::BreakableLineComment(const FormatToken &Token,
Alexander Kornienko00895102013-06-05 14:09:10 +0000178 unsigned StartColumn,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000179 bool InPPDirective,
Alexander Kornienko00895102013-06-05 14:09:10 +0000180 encoding::Encoding Encoding)
Manuel Klimekde008c02013-05-27 15:23:34 +0000181 : BreakableSingleLineToken(Token, StartColumn,
Alexander Kornienko00895102013-06-05 14:09:10 +0000182 getLineCommentPrefix(Token.TokenText), "",
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000183 InPPDirective, Encoding) {
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000184 OriginalPrefix = Prefix;
185 if (Token.TokenText.size() > Prefix.size() &&
186 isAlphanumeric(Token.TokenText[Prefix.size()])) {
187 if (Prefix == "//")
188 Prefix = "// ";
189 else if (Prefix == "///")
190 Prefix = "/// ";
191 }
192}
Manuel Klimekde008c02013-05-27 15:23:34 +0000193
194BreakableToken::Split
195BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset,
196 unsigned ColumnLimit) const {
197 return getCommentSplit(Line.substr(TailOffset), StartColumn + Prefix.size(),
Alexander Kornienko00895102013-06-05 14:09:10 +0000198 ColumnLimit, Encoding);
Manuel Klimekde008c02013-05-27 15:23:34 +0000199}
200
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000201void BreakableLineComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000202 Split Split,
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000203 WhitespaceManager &Whitespaces) {
204 Whitespaces.replaceWhitespaceInToken(
205 Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second,
206 Postfix, Prefix, InPPDirective, 1, StartColumn);
207}
208
209void
210BreakableLineComment::replaceWhitespaceBefore(unsigned LineIndex,
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000211 WhitespaceManager &Whitespaces) {
212 if (OriginalPrefix != Prefix) {
213 Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "",
214 false, 0, 1);
215 }
216}
217
Alexander Kornienko00895102013-06-05 14:09:10 +0000218BreakableBlockComment::BreakableBlockComment(
219 const FormatStyle &Style, const FormatToken &Token, unsigned StartColumn,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000220 unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective,
221 encoding::Encoding Encoding)
222 : BreakableToken(Token, InPPDirective, Encoding) {
Manuel Klimekde008c02013-05-27 15:23:34 +0000223 StringRef TokenText(Token.TokenText);
224 assert(TokenText.startswith("/*") && TokenText.endswith("*/"));
225 TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n");
226
227 int IndentDelta = StartColumn - OriginalStartColumn;
Manuel Klimekde008c02013-05-27 15:23:34 +0000228 LeadingWhitespace.resize(Lines.size());
229 StartOfLineColumn.resize(Lines.size());
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000230 StartOfLineColumn[0] = StartColumn + 2;
231 for (size_t i = 1; i < Lines.size(); ++i)
232 adjustWhitespace(Style, i, IndentDelta);
233
234 Decoration = "* ";
Manuel Klimekde008c02013-05-27 15:23:34 +0000235 if (Lines.size() == 1 && !FirstInLine) {
236 // Comments for which FirstInLine is false can start on arbitrary column,
237 // and available horizontal space can be too small to align consecutive
238 // lines with the first one.
239 // FIXME: We could, probably, align them to current indentation level, but
240 // now we just wrap them without stars.
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000241 Decoration = "";
Manuel Klimekde008c02013-05-27 15:23:34 +0000242 }
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000243 for (size_t i = 1, e = Lines.size(); i < e && !Decoration.empty(); ++i) {
244 // If the last line is empty, the closing "*/" will have a star.
245 if (i + 1 == e && Lines[i].empty())
246 break;
247 while (!Lines[i].startswith(Decoration))
248 Decoration = Decoration.substr(0, Decoration.size() - 1);
Manuel Klimekde008c02013-05-27 15:23:34 +0000249 }
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000250
251 LastLineNeedsDecoration = true;
Manuel Klimekde008c02013-05-27 15:23:34 +0000252 IndentAtLineBreak = StartOfLineColumn[0] + 1;
253 for (size_t i = 1; i < Lines.size(); ++i) {
254 if (Lines[i].empty()) {
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000255 if (i + 1 == Lines.size()) {
256 // Empty last line means that we already have a star as a part of the
257 // trailing */. We also need to preserve whitespace, so that */ is
258 // correctly indented.
259 LastLineNeedsDecoration = false;
260 } else if (Decoration.empty()) {
261 // For all other lines, set the start column to 0 if they're empty, so
262 // we do not insert trailing whitespace anywhere.
Manuel Klimekde008c02013-05-27 15:23:34 +0000263 StartOfLineColumn[i] = 0;
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000264 }
Manuel Klimekde008c02013-05-27 15:23:34 +0000265 continue;
266 }
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000267 // The first line already excludes the star.
268 // For all other lines, adjust the line to exclude the star and
269 // (optionally) the first whitespace.
270 StartOfLineColumn[i] += Decoration.size();
271 Lines[i] = Lines[i].substr(Decoration.size());
272 LeadingWhitespace[i] += Decoration.size();
Manuel Klimekde008c02013-05-27 15:23:34 +0000273 IndentAtLineBreak = std::min<int>(IndentAtLineBreak, StartOfLineColumn[i]);
274 }
Daniel Jaspercb4b40b2013-05-30 17:27:48 +0000275 IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size());
Manuel Klimekde008c02013-05-27 15:23:34 +0000276 DEBUG({
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000277 llvm::dbgs() << "IndentAtLineBreak " << IndentAtLineBreak << "\n";
Manuel Klimekde008c02013-05-27 15:23:34 +0000278 for (size_t i = 0; i < Lines.size(); ++i) {
279 llvm::dbgs() << i << " |" << Lines[i] << "| " << LeadingWhitespace[i]
280 << "\n";
281 }
282 });
283}
284
285void BreakableBlockComment::adjustWhitespace(const FormatStyle &Style,
286 unsigned LineIndex,
287 int IndentDelta) {
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000288 // When in a preprocessor directive, the trailing backslash in a block comment
289 // is not needed, but can serve a purpose of uniformity with necessary escaped
290 // newlines outside the comment. In this case we remove it here before
291 // trimming the trailing whitespace. The backslash will be re-added later when
292 // inserting a line break.
293 size_t EndOfPreviousLine = Lines[LineIndex - 1].size();
294 if (InPPDirective && Lines[LineIndex - 1].endswith("\\"))
295 --EndOfPreviousLine;
296
Manuel Klimekde008c02013-05-27 15:23:34 +0000297 // Calculate the end of the non-whitespace text in the previous line.
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000298 EndOfPreviousLine =
Alexander Kornienko8afa39b2013-06-20 13:58:37 +0000299 Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine);
Manuel Klimekde008c02013-05-27 15:23:34 +0000300 if (EndOfPreviousLine == StringRef::npos)
301 EndOfPreviousLine = 0;
302 else
303 ++EndOfPreviousLine;
304 // Calculate the start of the non-whitespace text in the current line.
Alexander Kornienko8afa39b2013-06-20 13:58:37 +0000305 size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks);
Manuel Klimekde008c02013-05-27 15:23:34 +0000306 if (StartOfLine == StringRef::npos)
307 StartOfLine = Lines[LineIndex].size();
Manuel Klimekde008c02013-05-27 15:23:34 +0000308
309 // Adjust Lines to only contain relevant text.
310 Lines[LineIndex - 1] = Lines[LineIndex - 1].substr(0, EndOfPreviousLine);
311 Lines[LineIndex] = Lines[LineIndex].substr(StartOfLine);
312 // Adjust LeadingWhitespace to account all whitespace between the lines
313 // to the current line.
314 LeadingWhitespace[LineIndex] =
315 Lines[LineIndex].begin() - Lines[LineIndex - 1].end();
Manuel Klimekd63312b2013-05-28 10:01:59 +0000316
317 // FIXME: We currently count tabs as 1 character. To solve this, we need to
318 // get the correct indentation width of the start of the comment, which
319 // requires correct counting of the tab expansions before the comment, and
320 // a configurable tab width. Since the current implementation only breaks
321 // if leading tabs are intermixed with spaces, that is not a high priority.
322
Manuel Klimekde008c02013-05-27 15:23:34 +0000323 // Adjust the start column uniformly accross all lines.
Manuel Klimekd63312b2013-05-28 10:01:59 +0000324 StartOfLineColumn[LineIndex] = std::max<int>(0, StartOfLine + IndentDelta);
Manuel Klimekde008c02013-05-27 15:23:34 +0000325}
326
327unsigned BreakableBlockComment::getLineCount() const { return Lines.size(); }
328
Alexander Kornienko2785b9a2013-06-07 16:02:52 +0000329unsigned BreakableBlockComment::getLineLengthAfterSplit(
330 unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const {
331 return getContentStartColumn(LineIndex, Offset) +
332 encoding::getCodePointCount(Lines[LineIndex].substr(Offset, Length),
Alexander Kornienko00895102013-06-05 14:09:10 +0000333 Encoding) +
Manuel Klimekde008c02013-05-27 15:23:34 +0000334 // The last line gets a "*/" postfix.
335 (LineIndex + 1 == Lines.size() ? 2 : 0);
336}
337
338BreakableToken::Split
339BreakableBlockComment::getSplit(unsigned LineIndex, unsigned TailOffset,
340 unsigned ColumnLimit) const {
341 return getCommentSplit(Lines[LineIndex].substr(TailOffset),
342 getContentStartColumn(LineIndex, TailOffset),
Alexander Kornienko00895102013-06-05 14:09:10 +0000343 ColumnLimit, Encoding);
Manuel Klimekde008c02013-05-27 15:23:34 +0000344}
345
346void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienko16a0ec62013-06-14 11:46:10 +0000347 Split Split,
Manuel Klimekde008c02013-05-27 15:23:34 +0000348 WhitespaceManager &Whitespaces) {
349 StringRef Text = Lines[LineIndex].substr(TailOffset);
350 StringRef Prefix = Decoration;
351 if (LineIndex + 1 == Lines.size() &&
352 Text.size() == Split.first + Split.second) {
353 // For the last line we need to break before "*/", but not to add "* ".
354 Prefix = "";
355 }
356
357 unsigned BreakOffsetInToken =
358 Text.data() - Tok.TokenText.data() + Split.first;
359 unsigned CharsToRemove = Split.second;
Manuel Klimekb6dba332013-05-30 07:45:53 +0000360 assert(IndentAtLineBreak >= Decoration.size());
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000361 Whitespaces.replaceWhitespaceInToken(Tok, BreakOffsetInToken, CharsToRemove,
362 "", Prefix, InPPDirective, 1,
363 IndentAtLineBreak - Decoration.size());
Manuel Klimekde008c02013-05-27 15:23:34 +0000364}
365
366void
367BreakableBlockComment::replaceWhitespaceBefore(unsigned LineIndex,
Manuel Klimekde008c02013-05-27 15:23:34 +0000368 WhitespaceManager &Whitespaces) {
369 if (LineIndex == 0)
370 return;
371 StringRef Prefix = Decoration;
Manuel Klimekc5cc4bf2013-05-28 08:55:01 +0000372 if (Lines[LineIndex].empty()) {
373 if (LineIndex + 1 == Lines.size()) {
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000374 if (!LastLineNeedsDecoration) {
375 // If the last line was empty, we don't need a prefix, as the */ will
376 // line up with the decoration (if it exists).
377 Prefix = "";
378 }
Manuel Klimekc5cc4bf2013-05-28 08:55:01 +0000379 } else if (!Decoration.empty()) {
380 // For other empty lines, if we do have a decoration, adapt it to not
381 // contain a trailing whitespace.
382 Prefix = Prefix.substr(0, 1);
383 }
Daniel Jaspere2c482f2013-05-30 06:40:07 +0000384 } else {
385 if (StartOfLineColumn[LineIndex] == 1) {
Alexander Kornienko1659ded2013-07-08 14:12:07 +0000386 // This line starts immediately after the decorating *.
Daniel Jaspere2c482f2013-05-30 06:40:07 +0000387 Prefix = Prefix.substr(0, 1);
388 }
Manuel Klimekc5cc4bf2013-05-28 08:55:01 +0000389 }
Manuel Klimekde008c02013-05-27 15:23:34 +0000390
Daniel Jasper2a409b62013-07-08 14:34:09 +0000391 unsigned WhitespaceOffsetInToken = Lines[LineIndex].data() -
392 Tok.TokenText.data() -
393 LeadingWhitespace[LineIndex];
Manuel Klimekb6dba332013-05-30 07:45:53 +0000394 assert(StartOfLineColumn[LineIndex] >= Prefix.size());
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000395 Whitespaces.replaceWhitespaceInToken(
Manuel Klimekde008c02013-05-27 15:23:34 +0000396 Tok, WhitespaceOffsetInToken, LeadingWhitespace[LineIndex], "", Prefix,
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000397 InPPDirective, 1, StartOfLineColumn[LineIndex] - Prefix.size());
Manuel Klimekde008c02013-05-27 15:23:34 +0000398}
399
400unsigned
401BreakableBlockComment::getContentStartColumn(unsigned LineIndex,
402 unsigned TailOffset) const {
403 // If we break, we always break at the predefined indent.
404 if (TailOffset != 0)
405 return IndentAtLineBreak;
406 return StartOfLineColumn[LineIndex];
407}
408
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000409} // namespace format
410} // namespace clang