xref: /minix3/external/bsd/llvm/dist/clang/lib/Format/BreakableToken.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- BreakableToken.cpp - Format C++ code -----------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc ///
10f4a2713aSLionel Sambuc /// \file
11f4a2713aSLionel Sambuc /// \brief Contains implementation of BreakableToken class and classes derived
12f4a2713aSLionel Sambuc /// from it.
13f4a2713aSLionel Sambuc ///
14f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc #include "BreakableToken.h"
17f4a2713aSLionel Sambuc #include "clang/Basic/CharInfo.h"
18f4a2713aSLionel Sambuc #include "clang/Format/Format.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
21f4a2713aSLionel Sambuc #include <algorithm>
22f4a2713aSLionel Sambuc 
23*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "format-token-breaker"
24*0a6a1f1dSLionel Sambuc 
25f4a2713aSLionel Sambuc namespace clang {
26f4a2713aSLionel Sambuc namespace format {
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc static const char *const Blanks = " \t\v\f\r";
IsBlank(char C)29f4a2713aSLionel Sambuc static bool IsBlank(char C) {
30f4a2713aSLionel Sambuc   switch (C) {
31f4a2713aSLionel Sambuc   case ' ':
32f4a2713aSLionel Sambuc   case '\t':
33f4a2713aSLionel Sambuc   case '\v':
34f4a2713aSLionel Sambuc   case '\f':
35f4a2713aSLionel Sambuc   case '\r':
36f4a2713aSLionel Sambuc     return true;
37f4a2713aSLionel Sambuc   default:
38f4a2713aSLionel Sambuc     return false;
39f4a2713aSLionel Sambuc   }
40f4a2713aSLionel Sambuc }
41f4a2713aSLionel Sambuc 
getCommentSplit(StringRef Text,unsigned ContentStartColumn,unsigned ColumnLimit,unsigned TabWidth,encoding::Encoding Encoding)42f4a2713aSLionel Sambuc static BreakableToken::Split getCommentSplit(StringRef Text,
43f4a2713aSLionel Sambuc                                              unsigned ContentStartColumn,
44f4a2713aSLionel Sambuc                                              unsigned ColumnLimit,
45f4a2713aSLionel Sambuc                                              unsigned TabWidth,
46f4a2713aSLionel Sambuc                                              encoding::Encoding Encoding) {
47f4a2713aSLionel Sambuc   if (ColumnLimit <= ContentStartColumn + 1)
48f4a2713aSLionel Sambuc     return BreakableToken::Split(StringRef::npos, 0);
49f4a2713aSLionel Sambuc 
50f4a2713aSLionel Sambuc   unsigned MaxSplit = ColumnLimit - ContentStartColumn + 1;
51f4a2713aSLionel Sambuc   unsigned MaxSplitBytes = 0;
52f4a2713aSLionel Sambuc 
53f4a2713aSLionel Sambuc   for (unsigned NumChars = 0;
54f4a2713aSLionel Sambuc        NumChars < MaxSplit && MaxSplitBytes < Text.size();) {
55f4a2713aSLionel Sambuc     unsigned BytesInChar =
56f4a2713aSLionel Sambuc         encoding::getCodePointNumBytes(Text[MaxSplitBytes], Encoding);
57f4a2713aSLionel Sambuc     NumChars +=
58f4a2713aSLionel Sambuc         encoding::columnWidthWithTabs(Text.substr(MaxSplitBytes, BytesInChar),
59f4a2713aSLionel Sambuc                                       ContentStartColumn, TabWidth, Encoding);
60f4a2713aSLionel Sambuc     MaxSplitBytes += BytesInChar;
61f4a2713aSLionel Sambuc   }
62f4a2713aSLionel Sambuc 
63f4a2713aSLionel Sambuc   StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes);
64f4a2713aSLionel Sambuc   if (SpaceOffset == StringRef::npos ||
65f4a2713aSLionel Sambuc       // Don't break at leading whitespace.
66f4a2713aSLionel Sambuc       Text.find_last_not_of(Blanks, SpaceOffset) == StringRef::npos) {
67f4a2713aSLionel Sambuc     // Make sure that we don't break at leading whitespace that
68f4a2713aSLionel Sambuc     // reaches past MaxSplit.
69f4a2713aSLionel Sambuc     StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(Blanks);
70f4a2713aSLionel Sambuc     if (FirstNonWhitespace == StringRef::npos)
71f4a2713aSLionel Sambuc       // If the comment is only whitespace, we cannot split.
72f4a2713aSLionel Sambuc       return BreakableToken::Split(StringRef::npos, 0);
73f4a2713aSLionel Sambuc     SpaceOffset = Text.find_first_of(
74f4a2713aSLionel Sambuc         Blanks, std::max<unsigned>(MaxSplitBytes, FirstNonWhitespace));
75f4a2713aSLionel Sambuc   }
76f4a2713aSLionel Sambuc   if (SpaceOffset != StringRef::npos && SpaceOffset != 0) {
77f4a2713aSLionel Sambuc     StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks);
78f4a2713aSLionel Sambuc     StringRef AfterCut = Text.substr(SpaceOffset).ltrim(Blanks);
79f4a2713aSLionel Sambuc     return BreakableToken::Split(BeforeCut.size(),
80f4a2713aSLionel Sambuc                                  AfterCut.begin() - BeforeCut.end());
81f4a2713aSLionel Sambuc   }
82f4a2713aSLionel Sambuc   return BreakableToken::Split(StringRef::npos, 0);
83f4a2713aSLionel Sambuc }
84f4a2713aSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc static BreakableToken::Split
getStringSplit(StringRef Text,unsigned UsedColumns,unsigned ColumnLimit,unsigned TabWidth,encoding::Encoding Encoding)86*0a6a1f1dSLionel Sambuc getStringSplit(StringRef Text, unsigned UsedColumns, unsigned ColumnLimit,
87*0a6a1f1dSLionel Sambuc                unsigned TabWidth, encoding::Encoding Encoding) {
88f4a2713aSLionel Sambuc   // FIXME: Reduce unit test case.
89f4a2713aSLionel Sambuc   if (Text.empty())
90f4a2713aSLionel Sambuc     return BreakableToken::Split(StringRef::npos, 0);
91f4a2713aSLionel Sambuc   if (ColumnLimit <= UsedColumns)
92f4a2713aSLionel Sambuc     return BreakableToken::Split(StringRef::npos, 0);
93*0a6a1f1dSLionel Sambuc   unsigned MaxSplit = ColumnLimit - UsedColumns;
94f4a2713aSLionel Sambuc   StringRef::size_type SpaceOffset = 0;
95f4a2713aSLionel Sambuc   StringRef::size_type SlashOffset = 0;
96f4a2713aSLionel Sambuc   StringRef::size_type WordStartOffset = 0;
97f4a2713aSLionel Sambuc   StringRef::size_type SplitPoint = 0;
98f4a2713aSLionel Sambuc   for (unsigned Chars = 0;;) {
99f4a2713aSLionel Sambuc     unsigned Advance;
100f4a2713aSLionel Sambuc     if (Text[0] == '\\') {
101f4a2713aSLionel Sambuc       Advance = encoding::getEscapeSequenceLength(Text);
102f4a2713aSLionel Sambuc       Chars += Advance;
103f4a2713aSLionel Sambuc     } else {
104f4a2713aSLionel Sambuc       Advance = encoding::getCodePointNumBytes(Text[0], Encoding);
105f4a2713aSLionel Sambuc       Chars += encoding::columnWidthWithTabs(
106f4a2713aSLionel Sambuc           Text.substr(0, Advance), UsedColumns + Chars, TabWidth, Encoding);
107f4a2713aSLionel Sambuc     }
108f4a2713aSLionel Sambuc 
109*0a6a1f1dSLionel Sambuc     if (Chars > MaxSplit || Text.size() == Advance)
110f4a2713aSLionel Sambuc       break;
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc     if (IsBlank(Text[0]))
113f4a2713aSLionel Sambuc       SpaceOffset = SplitPoint;
114f4a2713aSLionel Sambuc     if (Text[0] == '/')
115f4a2713aSLionel Sambuc       SlashOffset = SplitPoint;
116f4a2713aSLionel Sambuc     if (Advance == 1 && !isAlphanumeric(Text[0]))
117f4a2713aSLionel Sambuc       WordStartOffset = SplitPoint;
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc     SplitPoint += Advance;
120f4a2713aSLionel Sambuc     Text = Text.substr(Advance);
121f4a2713aSLionel Sambuc   }
122f4a2713aSLionel Sambuc 
123f4a2713aSLionel Sambuc   if (SpaceOffset != 0)
124f4a2713aSLionel Sambuc     return BreakableToken::Split(SpaceOffset + 1, 0);
125f4a2713aSLionel Sambuc   if (SlashOffset != 0)
126f4a2713aSLionel Sambuc     return BreakableToken::Split(SlashOffset + 1, 0);
127f4a2713aSLionel Sambuc   if (WordStartOffset != 0)
128f4a2713aSLionel Sambuc     return BreakableToken::Split(WordStartOffset + 1, 0);
129f4a2713aSLionel Sambuc   if (SplitPoint != 0)
130f4a2713aSLionel Sambuc     return BreakableToken::Split(SplitPoint, 0);
131f4a2713aSLionel Sambuc   return BreakableToken::Split(StringRef::npos, 0);
132f4a2713aSLionel Sambuc }
133f4a2713aSLionel Sambuc 
getLineCount() const134f4a2713aSLionel Sambuc unsigned BreakableSingleLineToken::getLineCount() const { return 1; }
135f4a2713aSLionel Sambuc 
getLineLengthAfterSplit(unsigned LineIndex,unsigned Offset,StringRef::size_type Length) const136f4a2713aSLionel Sambuc unsigned BreakableSingleLineToken::getLineLengthAfterSplit(
137f4a2713aSLionel Sambuc     unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const {
138f4a2713aSLionel Sambuc   return StartColumn + Prefix.size() + Postfix.size() +
139f4a2713aSLionel Sambuc          encoding::columnWidthWithTabs(Line.substr(Offset, Length),
140f4a2713aSLionel Sambuc                                        StartColumn + Prefix.size(),
141f4a2713aSLionel Sambuc                                        Style.TabWidth, Encoding);
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc 
BreakableSingleLineToken(const FormatToken & Tok,unsigned IndentLevel,unsigned StartColumn,StringRef Prefix,StringRef Postfix,bool InPPDirective,encoding::Encoding Encoding,const FormatStyle & Style)144f4a2713aSLionel Sambuc BreakableSingleLineToken::BreakableSingleLineToken(
145f4a2713aSLionel Sambuc     const FormatToken &Tok, unsigned IndentLevel, unsigned StartColumn,
146f4a2713aSLionel Sambuc     StringRef Prefix, StringRef Postfix, bool InPPDirective,
147f4a2713aSLionel Sambuc     encoding::Encoding Encoding, const FormatStyle &Style)
148f4a2713aSLionel Sambuc     : BreakableToken(Tok, IndentLevel, InPPDirective, Encoding, Style),
149f4a2713aSLionel Sambuc       StartColumn(StartColumn), Prefix(Prefix), Postfix(Postfix) {
150*0a6a1f1dSLionel Sambuc   assert(Tok.TokenText.endswith(Postfix));
151f4a2713aSLionel Sambuc   Line = Tok.TokenText.substr(
152f4a2713aSLionel Sambuc       Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size());
153f4a2713aSLionel Sambuc }
154f4a2713aSLionel Sambuc 
BreakableStringLiteral(const FormatToken & Tok,unsigned IndentLevel,unsigned StartColumn,StringRef Prefix,StringRef Postfix,bool InPPDirective,encoding::Encoding Encoding,const FormatStyle & Style)155f4a2713aSLionel Sambuc BreakableStringLiteral::BreakableStringLiteral(
156f4a2713aSLionel Sambuc     const FormatToken &Tok, unsigned IndentLevel, unsigned StartColumn,
157f4a2713aSLionel Sambuc     StringRef Prefix, StringRef Postfix, bool InPPDirective,
158f4a2713aSLionel Sambuc     encoding::Encoding Encoding, const FormatStyle &Style)
159f4a2713aSLionel Sambuc     : BreakableSingleLineToken(Tok, IndentLevel, StartColumn, Prefix, Postfix,
160f4a2713aSLionel Sambuc                                InPPDirective, Encoding, Style) {}
161f4a2713aSLionel Sambuc 
162f4a2713aSLionel Sambuc BreakableToken::Split
getSplit(unsigned LineIndex,unsigned TailOffset,unsigned ColumnLimit) const163f4a2713aSLionel Sambuc BreakableStringLiteral::getSplit(unsigned LineIndex, unsigned TailOffset,
164f4a2713aSLionel Sambuc                                  unsigned ColumnLimit) const {
165f4a2713aSLionel Sambuc   return getStringSplit(Line.substr(TailOffset),
166f4a2713aSLionel Sambuc                         StartColumn + Prefix.size() + Postfix.size(),
167f4a2713aSLionel Sambuc                         ColumnLimit, Style.TabWidth, Encoding);
168f4a2713aSLionel Sambuc }
169f4a2713aSLionel Sambuc 
insertBreak(unsigned LineIndex,unsigned TailOffset,Split Split,WhitespaceManager & Whitespaces)170f4a2713aSLionel Sambuc void BreakableStringLiteral::insertBreak(unsigned LineIndex,
171f4a2713aSLionel Sambuc                                          unsigned TailOffset, Split Split,
172f4a2713aSLionel Sambuc                                          WhitespaceManager &Whitespaces) {
173*0a6a1f1dSLionel Sambuc   unsigned LeadingSpaces = StartColumn;
174*0a6a1f1dSLionel Sambuc   // The '@' of an ObjC string literal (@"Test") does not become part of the
175*0a6a1f1dSLionel Sambuc   // string token.
176*0a6a1f1dSLionel Sambuc   // FIXME: It might be a cleaner solution to merge the tokens as a
177*0a6a1f1dSLionel Sambuc   // precomputation step.
178*0a6a1f1dSLionel Sambuc   if (Prefix.startswith("@"))
179*0a6a1f1dSLionel Sambuc     --LeadingSpaces;
180f4a2713aSLionel Sambuc   Whitespaces.replaceWhitespaceInToken(
181f4a2713aSLionel Sambuc       Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix,
182*0a6a1f1dSLionel Sambuc       Prefix, InPPDirective, 1, IndentLevel, LeadingSpaces);
183f4a2713aSLionel Sambuc }
184f4a2713aSLionel Sambuc 
getLineCommentIndentPrefix(StringRef Comment)185*0a6a1f1dSLionel Sambuc static StringRef getLineCommentIndentPrefix(StringRef Comment) {
186*0a6a1f1dSLionel Sambuc   static const char *const KnownPrefixes[] = { "///", "//" };
187*0a6a1f1dSLionel Sambuc   StringRef LongestPrefix;
188*0a6a1f1dSLionel Sambuc   for (StringRef KnownPrefix : KnownPrefixes) {
189*0a6a1f1dSLionel Sambuc     if (Comment.startswith(KnownPrefix)) {
190*0a6a1f1dSLionel Sambuc       size_t PrefixLength = KnownPrefix.size();
191*0a6a1f1dSLionel Sambuc       while (PrefixLength < Comment.size() && Comment[PrefixLength] == ' ')
192*0a6a1f1dSLionel Sambuc         ++PrefixLength;
193*0a6a1f1dSLionel Sambuc       if (PrefixLength > LongestPrefix.size())
194*0a6a1f1dSLionel Sambuc         LongestPrefix = Comment.substr(0, PrefixLength);
195*0a6a1f1dSLionel Sambuc     }
196*0a6a1f1dSLionel Sambuc   }
197*0a6a1f1dSLionel Sambuc   return LongestPrefix;
198f4a2713aSLionel Sambuc }
199f4a2713aSLionel Sambuc 
BreakableLineComment(const FormatToken & Token,unsigned IndentLevel,unsigned StartColumn,bool InPPDirective,encoding::Encoding Encoding,const FormatStyle & Style)200f4a2713aSLionel Sambuc BreakableLineComment::BreakableLineComment(
201f4a2713aSLionel Sambuc     const FormatToken &Token, unsigned IndentLevel, unsigned StartColumn,
202f4a2713aSLionel Sambuc     bool InPPDirective, encoding::Encoding Encoding, const FormatStyle &Style)
203f4a2713aSLionel Sambuc     : BreakableSingleLineToken(Token, IndentLevel, StartColumn,
204*0a6a1f1dSLionel Sambuc                                getLineCommentIndentPrefix(Token.TokenText), "",
205f4a2713aSLionel Sambuc                                InPPDirective, Encoding, Style) {
206f4a2713aSLionel Sambuc   OriginalPrefix = Prefix;
207f4a2713aSLionel Sambuc   if (Token.TokenText.size() > Prefix.size() &&
208f4a2713aSLionel Sambuc       isAlphanumeric(Token.TokenText[Prefix.size()])) {
209f4a2713aSLionel Sambuc     if (Prefix == "//")
210f4a2713aSLionel Sambuc       Prefix = "// ";
211f4a2713aSLionel Sambuc     else if (Prefix == "///")
212f4a2713aSLionel Sambuc       Prefix = "/// ";
213f4a2713aSLionel Sambuc   }
214f4a2713aSLionel Sambuc }
215f4a2713aSLionel Sambuc 
216f4a2713aSLionel Sambuc BreakableToken::Split
getSplit(unsigned LineIndex,unsigned TailOffset,unsigned ColumnLimit) const217f4a2713aSLionel Sambuc BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset,
218f4a2713aSLionel Sambuc                                unsigned ColumnLimit) const {
219f4a2713aSLionel Sambuc   return getCommentSplit(Line.substr(TailOffset), StartColumn + Prefix.size(),
220f4a2713aSLionel Sambuc                          ColumnLimit, Style.TabWidth, Encoding);
221f4a2713aSLionel Sambuc }
222f4a2713aSLionel Sambuc 
insertBreak(unsigned LineIndex,unsigned TailOffset,Split Split,WhitespaceManager & Whitespaces)223f4a2713aSLionel Sambuc void BreakableLineComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
224f4a2713aSLionel Sambuc                                        Split Split,
225f4a2713aSLionel Sambuc                                        WhitespaceManager &Whitespaces) {
226f4a2713aSLionel Sambuc   Whitespaces.replaceWhitespaceInToken(
227f4a2713aSLionel Sambuc       Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second,
228f4a2713aSLionel Sambuc       Postfix, Prefix, InPPDirective, /*Newlines=*/1, IndentLevel, StartColumn);
229f4a2713aSLionel Sambuc }
230f4a2713aSLionel Sambuc 
replaceWhitespace(unsigned LineIndex,unsigned TailOffset,Split Split,WhitespaceManager & Whitespaces)231f4a2713aSLionel Sambuc void BreakableLineComment::replaceWhitespace(unsigned LineIndex,
232f4a2713aSLionel Sambuc                                              unsigned TailOffset, Split Split,
233f4a2713aSLionel Sambuc                                              WhitespaceManager &Whitespaces) {
234f4a2713aSLionel Sambuc   Whitespaces.replaceWhitespaceInToken(
235f4a2713aSLionel Sambuc       Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second, "",
236f4a2713aSLionel Sambuc       "", /*InPPDirective=*/false, /*Newlines=*/0, /*IndentLevel=*/0,
237f4a2713aSLionel Sambuc       /*Spaces=*/1);
238f4a2713aSLionel Sambuc }
239f4a2713aSLionel Sambuc 
240f4a2713aSLionel Sambuc void
replaceWhitespaceBefore(unsigned LineIndex,WhitespaceManager & Whitespaces)241f4a2713aSLionel Sambuc BreakableLineComment::replaceWhitespaceBefore(unsigned LineIndex,
242f4a2713aSLionel Sambuc                                               WhitespaceManager &Whitespaces) {
243f4a2713aSLionel Sambuc   if (OriginalPrefix != Prefix) {
244f4a2713aSLionel Sambuc     Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "",
245f4a2713aSLionel Sambuc                                          /*InPPDirective=*/false,
246f4a2713aSLionel Sambuc                                          /*Newlines=*/0, /*IndentLevel=*/0,
247f4a2713aSLionel Sambuc                                          /*Spaces=*/1);
248f4a2713aSLionel Sambuc   }
249f4a2713aSLionel Sambuc }
250f4a2713aSLionel Sambuc 
BreakableBlockComment(const FormatToken & Token,unsigned IndentLevel,unsigned StartColumn,unsigned OriginalStartColumn,bool FirstInLine,bool InPPDirective,encoding::Encoding Encoding,const FormatStyle & Style)251f4a2713aSLionel Sambuc BreakableBlockComment::BreakableBlockComment(
252f4a2713aSLionel Sambuc     const FormatToken &Token, unsigned IndentLevel, unsigned StartColumn,
253f4a2713aSLionel Sambuc     unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective,
254f4a2713aSLionel Sambuc     encoding::Encoding Encoding, const FormatStyle &Style)
255f4a2713aSLionel Sambuc     : BreakableToken(Token, IndentLevel, InPPDirective, Encoding, Style) {
256f4a2713aSLionel Sambuc   StringRef TokenText(Token.TokenText);
257f4a2713aSLionel Sambuc   assert(TokenText.startswith("/*") && TokenText.endswith("*/"));
258f4a2713aSLionel Sambuc   TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n");
259f4a2713aSLionel Sambuc 
260f4a2713aSLionel Sambuc   int IndentDelta = StartColumn - OriginalStartColumn;
261f4a2713aSLionel Sambuc   LeadingWhitespace.resize(Lines.size());
262f4a2713aSLionel Sambuc   StartOfLineColumn.resize(Lines.size());
263f4a2713aSLionel Sambuc   StartOfLineColumn[0] = StartColumn + 2;
264f4a2713aSLionel Sambuc   for (size_t i = 1; i < Lines.size(); ++i)
265f4a2713aSLionel Sambuc     adjustWhitespace(i, IndentDelta);
266f4a2713aSLionel Sambuc 
267f4a2713aSLionel Sambuc   Decoration = "* ";
268f4a2713aSLionel Sambuc   if (Lines.size() == 1 && !FirstInLine) {
269f4a2713aSLionel Sambuc     // Comments for which FirstInLine is false can start on arbitrary column,
270f4a2713aSLionel Sambuc     // and available horizontal space can be too small to align consecutive
271f4a2713aSLionel Sambuc     // lines with the first one.
272f4a2713aSLionel Sambuc     // FIXME: We could, probably, align them to current indentation level, but
273f4a2713aSLionel Sambuc     // now we just wrap them without stars.
274f4a2713aSLionel Sambuc     Decoration = "";
275f4a2713aSLionel Sambuc   }
276f4a2713aSLionel Sambuc   for (size_t i = 1, e = Lines.size(); i < e && !Decoration.empty(); ++i) {
277f4a2713aSLionel Sambuc     // If the last line is empty, the closing "*/" will have a star.
278f4a2713aSLionel Sambuc     if (i + 1 == e && Lines[i].empty())
279f4a2713aSLionel Sambuc       break;
280f4a2713aSLionel Sambuc     while (!Lines[i].startswith(Decoration))
281f4a2713aSLionel Sambuc       Decoration = Decoration.substr(0, Decoration.size() - 1);
282f4a2713aSLionel Sambuc   }
283f4a2713aSLionel Sambuc 
284f4a2713aSLionel Sambuc   LastLineNeedsDecoration = true;
285f4a2713aSLionel Sambuc   IndentAtLineBreak = StartOfLineColumn[0] + 1;
286f4a2713aSLionel Sambuc   for (size_t i = 1; i < Lines.size(); ++i) {
287f4a2713aSLionel Sambuc     if (Lines[i].empty()) {
288f4a2713aSLionel Sambuc       if (i + 1 == Lines.size()) {
289f4a2713aSLionel Sambuc         // Empty last line means that we already have a star as a part of the
290f4a2713aSLionel Sambuc         // trailing */. We also need to preserve whitespace, so that */ is
291f4a2713aSLionel Sambuc         // correctly indented.
292f4a2713aSLionel Sambuc         LastLineNeedsDecoration = false;
293f4a2713aSLionel Sambuc       } else if (Decoration.empty()) {
294f4a2713aSLionel Sambuc         // For all other lines, set the start column to 0 if they're empty, so
295f4a2713aSLionel Sambuc         // we do not insert trailing whitespace anywhere.
296f4a2713aSLionel Sambuc         StartOfLineColumn[i] = 0;
297f4a2713aSLionel Sambuc       }
298f4a2713aSLionel Sambuc       continue;
299f4a2713aSLionel Sambuc     }
300f4a2713aSLionel Sambuc     // The first line already excludes the star.
301f4a2713aSLionel Sambuc     // For all other lines, adjust the line to exclude the star and
302f4a2713aSLionel Sambuc     // (optionally) the first whitespace.
303f4a2713aSLionel Sambuc     StartOfLineColumn[i] += Decoration.size();
304f4a2713aSLionel Sambuc     Lines[i] = Lines[i].substr(Decoration.size());
305f4a2713aSLionel Sambuc     LeadingWhitespace[i] += Decoration.size();
306*0a6a1f1dSLionel Sambuc     IndentAtLineBreak =
307*0a6a1f1dSLionel Sambuc         std::min<int>(IndentAtLineBreak, std::max(0, StartOfLineColumn[i]));
308f4a2713aSLionel Sambuc   }
309f4a2713aSLionel Sambuc   IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size());
310f4a2713aSLionel Sambuc   DEBUG({
311f4a2713aSLionel Sambuc     llvm::dbgs() << "IndentAtLineBreak " << IndentAtLineBreak << "\n";
312f4a2713aSLionel Sambuc     for (size_t i = 0; i < Lines.size(); ++i) {
313f4a2713aSLionel Sambuc       llvm::dbgs() << i << " |" << Lines[i] << "| " << LeadingWhitespace[i]
314f4a2713aSLionel Sambuc                    << "\n";
315f4a2713aSLionel Sambuc     }
316f4a2713aSLionel Sambuc   });
317f4a2713aSLionel Sambuc }
318f4a2713aSLionel Sambuc 
adjustWhitespace(unsigned LineIndex,int IndentDelta)319f4a2713aSLionel Sambuc void BreakableBlockComment::adjustWhitespace(unsigned LineIndex,
320f4a2713aSLionel Sambuc                                              int IndentDelta) {
321f4a2713aSLionel Sambuc   // When in a preprocessor directive, the trailing backslash in a block comment
322f4a2713aSLionel Sambuc   // is not needed, but can serve a purpose of uniformity with necessary escaped
323f4a2713aSLionel Sambuc   // newlines outside the comment. In this case we remove it here before
324f4a2713aSLionel Sambuc   // trimming the trailing whitespace. The backslash will be re-added later when
325f4a2713aSLionel Sambuc   // inserting a line break.
326f4a2713aSLionel Sambuc   size_t EndOfPreviousLine = Lines[LineIndex - 1].size();
327f4a2713aSLionel Sambuc   if (InPPDirective && Lines[LineIndex - 1].endswith("\\"))
328f4a2713aSLionel Sambuc     --EndOfPreviousLine;
329f4a2713aSLionel Sambuc 
330f4a2713aSLionel Sambuc   // Calculate the end of the non-whitespace text in the previous line.
331f4a2713aSLionel Sambuc   EndOfPreviousLine =
332f4a2713aSLionel Sambuc       Lines[LineIndex - 1].find_last_not_of(Blanks, EndOfPreviousLine);
333f4a2713aSLionel Sambuc   if (EndOfPreviousLine == StringRef::npos)
334f4a2713aSLionel Sambuc     EndOfPreviousLine = 0;
335f4a2713aSLionel Sambuc   else
336f4a2713aSLionel Sambuc     ++EndOfPreviousLine;
337f4a2713aSLionel Sambuc   // Calculate the start of the non-whitespace text in the current line.
338f4a2713aSLionel Sambuc   size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks);
339f4a2713aSLionel Sambuc   if (StartOfLine == StringRef::npos)
340f4a2713aSLionel Sambuc     StartOfLine = Lines[LineIndex].size();
341f4a2713aSLionel Sambuc 
342f4a2713aSLionel Sambuc   StringRef Whitespace = Lines[LineIndex].substr(0, StartOfLine);
343f4a2713aSLionel Sambuc   // Adjust Lines to only contain relevant text.
344f4a2713aSLionel Sambuc   Lines[LineIndex - 1] = Lines[LineIndex - 1].substr(0, EndOfPreviousLine);
345f4a2713aSLionel Sambuc   Lines[LineIndex] = Lines[LineIndex].substr(StartOfLine);
346f4a2713aSLionel Sambuc   // Adjust LeadingWhitespace to account all whitespace between the lines
347f4a2713aSLionel Sambuc   // to the current line.
348f4a2713aSLionel Sambuc   LeadingWhitespace[LineIndex] =
349f4a2713aSLionel Sambuc       Lines[LineIndex].begin() - Lines[LineIndex - 1].end();
350f4a2713aSLionel Sambuc 
351*0a6a1f1dSLionel Sambuc   // Adjust the start column uniformly across all lines.
352*0a6a1f1dSLionel Sambuc   StartOfLineColumn[LineIndex] =
353f4a2713aSLionel Sambuc       encoding::columnWidthWithTabs(Whitespace, 0, Style.TabWidth, Encoding) +
354*0a6a1f1dSLionel Sambuc       IndentDelta;
355f4a2713aSLionel Sambuc }
356f4a2713aSLionel Sambuc 
getLineCount() const357f4a2713aSLionel Sambuc unsigned BreakableBlockComment::getLineCount() const { return Lines.size(); }
358f4a2713aSLionel Sambuc 
getLineLengthAfterSplit(unsigned LineIndex,unsigned Offset,StringRef::size_type Length) const359f4a2713aSLionel Sambuc unsigned BreakableBlockComment::getLineLengthAfterSplit(
360f4a2713aSLionel Sambuc     unsigned LineIndex, unsigned Offset, StringRef::size_type Length) const {
361f4a2713aSLionel Sambuc   unsigned ContentStartColumn = getContentStartColumn(LineIndex, Offset);
362f4a2713aSLionel Sambuc   return ContentStartColumn +
363f4a2713aSLionel Sambuc          encoding::columnWidthWithTabs(Lines[LineIndex].substr(Offset, Length),
364f4a2713aSLionel Sambuc                                        ContentStartColumn, Style.TabWidth,
365f4a2713aSLionel Sambuc                                        Encoding) +
366f4a2713aSLionel Sambuc          // The last line gets a "*/" postfix.
367f4a2713aSLionel Sambuc          (LineIndex + 1 == Lines.size() ? 2 : 0);
368f4a2713aSLionel Sambuc }
369f4a2713aSLionel Sambuc 
370f4a2713aSLionel Sambuc BreakableToken::Split
getSplit(unsigned LineIndex,unsigned TailOffset,unsigned ColumnLimit) const371f4a2713aSLionel Sambuc BreakableBlockComment::getSplit(unsigned LineIndex, unsigned TailOffset,
372f4a2713aSLionel Sambuc                                 unsigned ColumnLimit) const {
373f4a2713aSLionel Sambuc   return getCommentSplit(Lines[LineIndex].substr(TailOffset),
374f4a2713aSLionel Sambuc                          getContentStartColumn(LineIndex, TailOffset),
375f4a2713aSLionel Sambuc                          ColumnLimit, Style.TabWidth, Encoding);
376f4a2713aSLionel Sambuc }
377f4a2713aSLionel Sambuc 
insertBreak(unsigned LineIndex,unsigned TailOffset,Split Split,WhitespaceManager & Whitespaces)378f4a2713aSLionel Sambuc void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
379f4a2713aSLionel Sambuc                                         Split Split,
380f4a2713aSLionel Sambuc                                         WhitespaceManager &Whitespaces) {
381f4a2713aSLionel Sambuc   StringRef Text = Lines[LineIndex].substr(TailOffset);
382f4a2713aSLionel Sambuc   StringRef Prefix = Decoration;
383f4a2713aSLionel Sambuc   if (LineIndex + 1 == Lines.size() &&
384f4a2713aSLionel Sambuc       Text.size() == Split.first + Split.second) {
385f4a2713aSLionel Sambuc     // For the last line we need to break before "*/", but not to add "* ".
386f4a2713aSLionel Sambuc     Prefix = "";
387f4a2713aSLionel Sambuc   }
388f4a2713aSLionel Sambuc 
389f4a2713aSLionel Sambuc   unsigned BreakOffsetInToken =
390f4a2713aSLionel Sambuc       Text.data() - Tok.TokenText.data() + Split.first;
391f4a2713aSLionel Sambuc   unsigned CharsToRemove = Split.second;
392f4a2713aSLionel Sambuc   assert(IndentAtLineBreak >= Decoration.size());
393f4a2713aSLionel Sambuc   Whitespaces.replaceWhitespaceInToken(
394f4a2713aSLionel Sambuc       Tok, BreakOffsetInToken, CharsToRemove, "", Prefix, InPPDirective, 1,
395f4a2713aSLionel Sambuc       IndentLevel, IndentAtLineBreak - Decoration.size());
396f4a2713aSLionel Sambuc }
397f4a2713aSLionel Sambuc 
replaceWhitespace(unsigned LineIndex,unsigned TailOffset,Split Split,WhitespaceManager & Whitespaces)398f4a2713aSLionel Sambuc void BreakableBlockComment::replaceWhitespace(unsigned LineIndex,
399f4a2713aSLionel Sambuc                                               unsigned TailOffset, Split Split,
400f4a2713aSLionel Sambuc                                               WhitespaceManager &Whitespaces) {
401f4a2713aSLionel Sambuc   StringRef Text = Lines[LineIndex].substr(TailOffset);
402f4a2713aSLionel Sambuc   unsigned BreakOffsetInToken =
403f4a2713aSLionel Sambuc       Text.data() - Tok.TokenText.data() + Split.first;
404f4a2713aSLionel Sambuc   unsigned CharsToRemove = Split.second;
405f4a2713aSLionel Sambuc   Whitespaces.replaceWhitespaceInToken(
406f4a2713aSLionel Sambuc       Tok, BreakOffsetInToken, CharsToRemove, "", "", /*InPPDirective=*/false,
407f4a2713aSLionel Sambuc       /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1);
408f4a2713aSLionel Sambuc }
409f4a2713aSLionel Sambuc 
410f4a2713aSLionel Sambuc void
replaceWhitespaceBefore(unsigned LineIndex,WhitespaceManager & Whitespaces)411f4a2713aSLionel Sambuc BreakableBlockComment::replaceWhitespaceBefore(unsigned LineIndex,
412f4a2713aSLionel Sambuc                                                WhitespaceManager &Whitespaces) {
413f4a2713aSLionel Sambuc   if (LineIndex == 0)
414f4a2713aSLionel Sambuc     return;
415f4a2713aSLionel Sambuc   StringRef Prefix = Decoration;
416f4a2713aSLionel Sambuc   if (Lines[LineIndex].empty()) {
417f4a2713aSLionel Sambuc     if (LineIndex + 1 == Lines.size()) {
418f4a2713aSLionel Sambuc       if (!LastLineNeedsDecoration) {
419f4a2713aSLionel Sambuc         // If the last line was empty, we don't need a prefix, as the */ will
420f4a2713aSLionel Sambuc         // line up with the decoration (if it exists).
421f4a2713aSLionel Sambuc         Prefix = "";
422f4a2713aSLionel Sambuc       }
423f4a2713aSLionel Sambuc     } else if (!Decoration.empty()) {
424f4a2713aSLionel Sambuc       // For other empty lines, if we do have a decoration, adapt it to not
425f4a2713aSLionel Sambuc       // contain a trailing whitespace.
426f4a2713aSLionel Sambuc       Prefix = Prefix.substr(0, 1);
427f4a2713aSLionel Sambuc     }
428f4a2713aSLionel Sambuc   } else {
429f4a2713aSLionel Sambuc     if (StartOfLineColumn[LineIndex] == 1) {
430f4a2713aSLionel Sambuc       // This line starts immediately after the decorating *.
431f4a2713aSLionel Sambuc       Prefix = Prefix.substr(0, 1);
432f4a2713aSLionel Sambuc     }
433f4a2713aSLionel Sambuc   }
434f4a2713aSLionel Sambuc 
435f4a2713aSLionel Sambuc   unsigned WhitespaceOffsetInToken = Lines[LineIndex].data() -
436f4a2713aSLionel Sambuc                                      Tok.TokenText.data() -
437f4a2713aSLionel Sambuc                                      LeadingWhitespace[LineIndex];
438f4a2713aSLionel Sambuc   Whitespaces.replaceWhitespaceInToken(
439f4a2713aSLionel Sambuc       Tok, WhitespaceOffsetInToken, LeadingWhitespace[LineIndex], "", Prefix,
440f4a2713aSLionel Sambuc       InPPDirective, 1, IndentLevel,
441f4a2713aSLionel Sambuc       StartOfLineColumn[LineIndex] - Prefix.size());
442f4a2713aSLionel Sambuc }
443f4a2713aSLionel Sambuc 
444f4a2713aSLionel Sambuc unsigned
getContentStartColumn(unsigned LineIndex,unsigned TailOffset) const445f4a2713aSLionel Sambuc BreakableBlockComment::getContentStartColumn(unsigned LineIndex,
446f4a2713aSLionel Sambuc                                              unsigned TailOffset) const {
447f4a2713aSLionel Sambuc   // If we break, we always break at the predefined indent.
448f4a2713aSLionel Sambuc   if (TailOffset != 0)
449f4a2713aSLionel Sambuc     return IndentAtLineBreak;
450*0a6a1f1dSLionel Sambuc   return std::max(0, StartOfLineColumn[LineIndex]);
451f4a2713aSLionel Sambuc }
452f4a2713aSLionel Sambuc 
453f4a2713aSLionel Sambuc } // namespace format
454f4a2713aSLionel Sambuc } // namespace clang
455