annotate clang/lib/Format/BreakableToken.h @ 176:de4ac79aef9d

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 17:13:11 +0900
parents 1d019706d866
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- BreakableToken.h - Format C++ code ---------------------*- C++ -*-===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 ///
anatofuz
parents:
diff changeset
9 /// \file
anatofuz
parents:
diff changeset
10 /// Declares BreakableToken, BreakableStringLiteral, BreakableComment,
anatofuz
parents:
diff changeset
11 /// BreakableBlockComment and BreakableLineCommentSection classes, that contain
anatofuz
parents:
diff changeset
12 /// token type-specific logic to break long lines in tokens and reflow content
anatofuz
parents:
diff changeset
13 /// between tokens.
anatofuz
parents:
diff changeset
14 ///
anatofuz
parents:
diff changeset
15 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 #ifndef LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H
anatofuz
parents:
diff changeset
18 #define LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 #include "Encoding.h"
anatofuz
parents:
diff changeset
21 #include "TokenAnnotator.h"
anatofuz
parents:
diff changeset
22 #include "WhitespaceManager.h"
anatofuz
parents:
diff changeset
23 #include "llvm/ADT/StringSet.h"
anatofuz
parents:
diff changeset
24 #include "llvm/Support/Regex.h"
anatofuz
parents:
diff changeset
25 #include <utility>
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 namespace clang {
anatofuz
parents:
diff changeset
28 namespace format {
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 /// Checks if \p Token switches formatting, like /* clang-format off */.
anatofuz
parents:
diff changeset
31 /// \p Token must be a comment.
anatofuz
parents:
diff changeset
32 bool switchesFormatting(const FormatToken &Token);
anatofuz
parents:
diff changeset
33
anatofuz
parents:
diff changeset
34 struct FormatStyle;
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 /// Base class for tokens / ranges of tokens that can allow breaking
anatofuz
parents:
diff changeset
37 /// within the tokens - for example, to avoid whitespace beyond the column
anatofuz
parents:
diff changeset
38 /// limit, or to reflow text.
anatofuz
parents:
diff changeset
39 ///
anatofuz
parents:
diff changeset
40 /// Generally, a breakable token consists of logical lines, addressed by a line
anatofuz
parents:
diff changeset
41 /// index. For example, in a sequence of line comments, each line comment is its
anatofuz
parents:
diff changeset
42 /// own logical line; similarly, for a block comment, each line in the block
anatofuz
parents:
diff changeset
43 /// comment is on its own logical line.
anatofuz
parents:
diff changeset
44 ///
anatofuz
parents:
diff changeset
45 /// There are two methods to compute the layout of the token:
anatofuz
parents:
diff changeset
46 /// - getRangeLength measures the number of columns needed for a range of text
anatofuz
parents:
diff changeset
47 /// within a logical line, and
anatofuz
parents:
diff changeset
48 /// - getContentStartColumn returns the start column at which we want the
anatofuz
parents:
diff changeset
49 /// content of a logical line to start (potentially after introducing a line
anatofuz
parents:
diff changeset
50 /// break).
anatofuz
parents:
diff changeset
51 ///
anatofuz
parents:
diff changeset
52 /// The mechanism to adapt the layout of the breakable token is organised
anatofuz
parents:
diff changeset
53 /// around the concept of a \c Split, which is a whitespace range that signifies
anatofuz
parents:
diff changeset
54 /// a position of the content of a token where a reformatting might be done.
anatofuz
parents:
diff changeset
55 ///
anatofuz
parents:
diff changeset
56 /// Operating with splits is divided into two operations:
anatofuz
parents:
diff changeset
57 /// - getSplit, for finding a split starting at a position,
anatofuz
parents:
diff changeset
58 /// - insertBreak, for executing the split using a whitespace manager.
anatofuz
parents:
diff changeset
59 ///
anatofuz
parents:
diff changeset
60 /// There is a pair of operations that are used to compress a long whitespace
anatofuz
parents:
diff changeset
61 /// range with a single space if that will bring the line length under the
anatofuz
parents:
diff changeset
62 /// column limit:
anatofuz
parents:
diff changeset
63 /// - getLineLengthAfterCompression, for calculating the size in columns of the
anatofuz
parents:
diff changeset
64 /// line after a whitespace range has been compressed, and
anatofuz
parents:
diff changeset
65 /// - compressWhitespace, for executing the whitespace compression using a
anatofuz
parents:
diff changeset
66 /// whitespace manager; note that the compressed whitespace may be in the
anatofuz
parents:
diff changeset
67 /// middle of the original line and of the reformatted line.
anatofuz
parents:
diff changeset
68 ///
anatofuz
parents:
diff changeset
69 /// For tokens where the whitespace before each line needs to be also
anatofuz
parents:
diff changeset
70 /// reformatted, for example for tokens supporting reflow, there are analogous
anatofuz
parents:
diff changeset
71 /// operations that might be executed before the main line breaking occurs:
anatofuz
parents:
diff changeset
72 /// - getReflowSplit, for finding a split such that the content preceding it
anatofuz
parents:
diff changeset
73 /// needs to be specially reflown,
anatofuz
parents:
diff changeset
74 /// - reflow, for executing the split using a whitespace manager,
anatofuz
parents:
diff changeset
75 /// - introducesBreakBefore, for checking if reformatting the beginning
anatofuz
parents:
diff changeset
76 /// of the content introduces a line break before it,
anatofuz
parents:
diff changeset
77 /// - adaptStartOfLine, for executing the reflow using a whitespace
anatofuz
parents:
diff changeset
78 /// manager.
anatofuz
parents:
diff changeset
79 ///
anatofuz
parents:
diff changeset
80 /// For tokens that require the whitespace after the last line to be
anatofuz
parents:
diff changeset
81 /// reformatted, for example in multiline jsdoc comments that require the
anatofuz
parents:
diff changeset
82 /// trailing '*/' to be on a line of itself, there are analogous operations
anatofuz
parents:
diff changeset
83 /// that might be executed after the last line has been reformatted:
anatofuz
parents:
diff changeset
84 /// - getSplitAfterLastLine, for finding a split after the last line that needs
anatofuz
parents:
diff changeset
85 /// to be reflown,
anatofuz
parents:
diff changeset
86 /// - replaceWhitespaceAfterLastLine, for executing the reflow using a
anatofuz
parents:
diff changeset
87 /// whitespace manager.
anatofuz
parents:
diff changeset
88 ///
anatofuz
parents:
diff changeset
89 class BreakableToken {
anatofuz
parents:
diff changeset
90 public:
anatofuz
parents:
diff changeset
91 /// Contains starting character index and length of split.
anatofuz
parents:
diff changeset
92 typedef std::pair<StringRef::size_type, unsigned> Split;
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 virtual ~BreakableToken() {}
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 /// Returns the number of lines in this token in the original code.
anatofuz
parents:
diff changeset
97 virtual unsigned getLineCount() const = 0;
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 /// Returns the number of columns required to format the text in the
anatofuz
parents:
diff changeset
100 /// byte range [\p Offset, \p Offset \c + \p Length).
anatofuz
parents:
diff changeset
101 ///
anatofuz
parents:
diff changeset
102 /// \p Offset is the byte offset from the start of the content of the line
anatofuz
parents:
diff changeset
103 /// at \p LineIndex.
anatofuz
parents:
diff changeset
104 ///
anatofuz
parents:
diff changeset
105 /// \p StartColumn is the column at which the text starts in the formatted
anatofuz
parents:
diff changeset
106 /// file, needed to compute tab stops correctly.
anatofuz
parents:
diff changeset
107 virtual unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
anatofuz
parents:
diff changeset
108 StringRef::size_type Length,
anatofuz
parents:
diff changeset
109 unsigned StartColumn) const = 0;
anatofuz
parents:
diff changeset
110
anatofuz
parents:
diff changeset
111 /// Returns the number of columns required to format the text following
anatofuz
parents:
diff changeset
112 /// the byte \p Offset in the line \p LineIndex, including potentially
anatofuz
parents:
diff changeset
113 /// unbreakable sequences of tokens following after the end of the token.
anatofuz
parents:
diff changeset
114 ///
anatofuz
parents:
diff changeset
115 /// \p Offset is the byte offset from the start of the content of the line
anatofuz
parents:
diff changeset
116 /// at \p LineIndex.
anatofuz
parents:
diff changeset
117 ///
anatofuz
parents:
diff changeset
118 /// \p StartColumn is the column at which the text starts in the formatted
anatofuz
parents:
diff changeset
119 /// file, needed to compute tab stops correctly.
anatofuz
parents:
diff changeset
120 ///
anatofuz
parents:
diff changeset
121 /// For breakable tokens that never use extra space at the end of a line, this
anatofuz
parents:
diff changeset
122 /// is equivalent to getRangeLength with a Length of StringRef::npos.
anatofuz
parents:
diff changeset
123 virtual unsigned getRemainingLength(unsigned LineIndex, unsigned Offset,
anatofuz
parents:
diff changeset
124 unsigned StartColumn) const {
anatofuz
parents:
diff changeset
125 return getRangeLength(LineIndex, Offset, StringRef::npos, StartColumn);
anatofuz
parents:
diff changeset
126 }
anatofuz
parents:
diff changeset
127
anatofuz
parents:
diff changeset
128 /// Returns the column at which content in line \p LineIndex starts,
anatofuz
parents:
diff changeset
129 /// assuming no reflow.
anatofuz
parents:
diff changeset
130 ///
anatofuz
parents:
diff changeset
131 /// If \p Break is true, returns the column at which the line should start
anatofuz
parents:
diff changeset
132 /// after the line break.
anatofuz
parents:
diff changeset
133 /// If \p Break is false, returns the column at which the line itself will
anatofuz
parents:
diff changeset
134 /// start.
anatofuz
parents:
diff changeset
135 virtual unsigned getContentStartColumn(unsigned LineIndex,
anatofuz
parents:
diff changeset
136 bool Break) const = 0;
anatofuz
parents:
diff changeset
137
anatofuz
parents:
diff changeset
138 /// Returns additional content indent required for the second line after the
anatofuz
parents:
diff changeset
139 /// content at line \p LineIndex is broken.
anatofuz
parents:
diff changeset
140 ///
anatofuz
parents:
diff changeset
141 // (Next lines do not start with `///` since otherwise -Wdocumentation picks
anatofuz
parents:
diff changeset
142 // up the example annotations and generates warnings for them)
anatofuz
parents:
diff changeset
143 // For example, Javadoc @param annotations require and indent of 4 spaces and
anatofuz
parents:
diff changeset
144 // in this example getContentIndex(1) returns 4.
anatofuz
parents:
diff changeset
145 // /**
anatofuz
parents:
diff changeset
146 // * @param loooooooooooooong line
anatofuz
parents:
diff changeset
147 // * continuation
anatofuz
parents:
diff changeset
148 // */
anatofuz
parents:
diff changeset
149 virtual unsigned getContentIndent(unsigned LineIndex) const { return 0; }
anatofuz
parents:
diff changeset
150
anatofuz
parents:
diff changeset
151 /// Returns a range (offset, length) at which to break the line at
anatofuz
parents:
diff changeset
152 /// \p LineIndex, if previously broken at \p TailOffset. If possible, do not
anatofuz
parents:
diff changeset
153 /// violate \p ColumnLimit, assuming the text starting at \p TailOffset in
anatofuz
parents:
diff changeset
154 /// the token is formatted starting at ContentStartColumn in the reformatted
anatofuz
parents:
diff changeset
155 /// file.
anatofuz
parents:
diff changeset
156 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
anatofuz
parents:
diff changeset
157 unsigned ColumnLimit, unsigned ContentStartColumn,
anatofuz
parents:
diff changeset
158 const llvm::Regex &CommentPragmasRegex) const = 0;
anatofuz
parents:
diff changeset
159
anatofuz
parents:
diff changeset
160 /// Emits the previously retrieved \p Split via \p Whitespaces.
anatofuz
parents:
diff changeset
161 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
anatofuz
parents:
diff changeset
162 unsigned ContentIndent,
anatofuz
parents:
diff changeset
163 WhitespaceManager &Whitespaces) const = 0;
anatofuz
parents:
diff changeset
164
anatofuz
parents:
diff changeset
165 /// Returns the number of columns needed to format
anatofuz
parents:
diff changeset
166 /// \p RemainingTokenColumns, assuming that Split is within the range measured
anatofuz
parents:
diff changeset
167 /// by \p RemainingTokenColumns, and that the whitespace in Split is reduced
anatofuz
parents:
diff changeset
168 /// to a single space.
anatofuz
parents:
diff changeset
169 unsigned getLengthAfterCompression(unsigned RemainingTokenColumns,
anatofuz
parents:
diff changeset
170 Split Split) const;
anatofuz
parents:
diff changeset
171
anatofuz
parents:
diff changeset
172 /// Replaces the whitespace range described by \p Split with a single
anatofuz
parents:
diff changeset
173 /// space.
anatofuz
parents:
diff changeset
174 virtual void compressWhitespace(unsigned LineIndex, unsigned TailOffset,
anatofuz
parents:
diff changeset
175 Split Split,
anatofuz
parents:
diff changeset
176 WhitespaceManager &Whitespaces) const = 0;
anatofuz
parents:
diff changeset
177
anatofuz
parents:
diff changeset
178 /// Returns whether the token supports reflowing text.
anatofuz
parents:
diff changeset
179 virtual bool supportsReflow() const { return false; }
anatofuz
parents:
diff changeset
180
anatofuz
parents:
diff changeset
181 /// Returns a whitespace range (offset, length) of the content at \p
anatofuz
parents:
diff changeset
182 /// LineIndex such that the content of that line is reflown to the end of the
anatofuz
parents:
diff changeset
183 /// previous one.
anatofuz
parents:
diff changeset
184 ///
anatofuz
parents:
diff changeset
185 /// Returning (StringRef::npos, 0) indicates reflowing is not possible.
anatofuz
parents:
diff changeset
186 ///
anatofuz
parents:
diff changeset
187 /// The range will include any whitespace preceding the specified line's
anatofuz
parents:
diff changeset
188 /// content.
anatofuz
parents:
diff changeset
189 ///
anatofuz
parents:
diff changeset
190 /// If the split is not contained within one token, for example when reflowing
anatofuz
parents:
diff changeset
191 /// line comments, returns (0, <length>).
anatofuz
parents:
diff changeset
192 virtual Split getReflowSplit(unsigned LineIndex,
anatofuz
parents:
diff changeset
193 const llvm::Regex &CommentPragmasRegex) const {
anatofuz
parents:
diff changeset
194 return Split(StringRef::npos, 0);
anatofuz
parents:
diff changeset
195 }
anatofuz
parents:
diff changeset
196
anatofuz
parents:
diff changeset
197 /// Reflows the current line into the end of the previous one.
anatofuz
parents:
diff changeset
198 virtual void reflow(unsigned LineIndex,
anatofuz
parents:
diff changeset
199 WhitespaceManager &Whitespaces) const {}
anatofuz
parents:
diff changeset
200
anatofuz
parents:
diff changeset
201 /// Returns whether there will be a line break at the start of the
anatofuz
parents:
diff changeset
202 /// token.
anatofuz
parents:
diff changeset
203 virtual bool introducesBreakBeforeToken() const { return false; }
anatofuz
parents:
diff changeset
204
anatofuz
parents:
diff changeset
205 /// Replaces the whitespace between \p LineIndex-1 and \p LineIndex.
anatofuz
parents:
diff changeset
206 virtual void adaptStartOfLine(unsigned LineIndex,
anatofuz
parents:
diff changeset
207 WhitespaceManager &Whitespaces) const {}
anatofuz
parents:
diff changeset
208
anatofuz
parents:
diff changeset
209 /// Returns a whitespace range (offset, length) of the content at
anatofuz
parents:
diff changeset
210 /// the last line that needs to be reformatted after the last line has been
anatofuz
parents:
diff changeset
211 /// reformatted.
anatofuz
parents:
diff changeset
212 ///
anatofuz
parents:
diff changeset
213 /// A result having offset == StringRef::npos means that no reformat is
anatofuz
parents:
diff changeset
214 /// necessary.
anatofuz
parents:
diff changeset
215 virtual Split getSplitAfterLastLine(unsigned TailOffset) const {
anatofuz
parents:
diff changeset
216 return Split(StringRef::npos, 0);
anatofuz
parents:
diff changeset
217 }
anatofuz
parents:
diff changeset
218
anatofuz
parents:
diff changeset
219 /// Replaces the whitespace from \p SplitAfterLastLine on the last line
anatofuz
parents:
diff changeset
220 /// after the last line has been formatted by performing a reformatting.
anatofuz
parents:
diff changeset
221 void replaceWhitespaceAfterLastLine(unsigned TailOffset,
anatofuz
parents:
diff changeset
222 Split SplitAfterLastLine,
anatofuz
parents:
diff changeset
223 WhitespaceManager &Whitespaces) const {
anatofuz
parents:
diff changeset
224 insertBreak(getLineCount() - 1, TailOffset, SplitAfterLastLine,
anatofuz
parents:
diff changeset
225 /*ContentIndent=*/0, Whitespaces);
anatofuz
parents:
diff changeset
226 }
anatofuz
parents:
diff changeset
227
anatofuz
parents:
diff changeset
228 /// Updates the next token of \p State to the next token after this
anatofuz
parents:
diff changeset
229 /// one. This can be used when this token manages a set of underlying tokens
anatofuz
parents:
diff changeset
230 /// as a unit and is responsible for the formatting of the them.
anatofuz
parents:
diff changeset
231 virtual void updateNextToken(LineState &State) const {}
anatofuz
parents:
diff changeset
232
anatofuz
parents:
diff changeset
233 protected:
anatofuz
parents:
diff changeset
234 BreakableToken(const FormatToken &Tok, bool InPPDirective,
anatofuz
parents:
diff changeset
235 encoding::Encoding Encoding, const FormatStyle &Style)
anatofuz
parents:
diff changeset
236 : Tok(Tok), InPPDirective(InPPDirective), Encoding(Encoding),
anatofuz
parents:
diff changeset
237 Style(Style) {}
anatofuz
parents:
diff changeset
238
anatofuz
parents:
diff changeset
239 const FormatToken &Tok;
anatofuz
parents:
diff changeset
240 const bool InPPDirective;
anatofuz
parents:
diff changeset
241 const encoding::Encoding Encoding;
anatofuz
parents:
diff changeset
242 const FormatStyle &Style;
anatofuz
parents:
diff changeset
243 };
anatofuz
parents:
diff changeset
244
anatofuz
parents:
diff changeset
245 class BreakableStringLiteral : public BreakableToken {
anatofuz
parents:
diff changeset
246 public:
anatofuz
parents:
diff changeset
247 /// Creates a breakable token for a single line string literal.
anatofuz
parents:
diff changeset
248 ///
anatofuz
parents:
diff changeset
249 /// \p StartColumn specifies the column in which the token will start
anatofuz
parents:
diff changeset
250 /// after formatting.
anatofuz
parents:
diff changeset
251 BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn,
anatofuz
parents:
diff changeset
252 StringRef Prefix, StringRef Postfix,
anatofuz
parents:
diff changeset
253 unsigned UnbreakableTailLength, bool InPPDirective,
anatofuz
parents:
diff changeset
254 encoding::Encoding Encoding, const FormatStyle &Style);
anatofuz
parents:
diff changeset
255
anatofuz
parents:
diff changeset
256 Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
anatofuz
parents:
diff changeset
257 unsigned ContentStartColumn,
anatofuz
parents:
diff changeset
258 const llvm::Regex &CommentPragmasRegex) const override;
anatofuz
parents:
diff changeset
259 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
anatofuz
parents:
diff changeset
260 unsigned ContentIndent,
anatofuz
parents:
diff changeset
261 WhitespaceManager &Whitespaces) const override;
anatofuz
parents:
diff changeset
262 void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
anatofuz
parents:
diff changeset
263 WhitespaceManager &Whitespaces) const override {}
anatofuz
parents:
diff changeset
264 unsigned getLineCount() const override;
anatofuz
parents:
diff changeset
265 unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
anatofuz
parents:
diff changeset
266 StringRef::size_type Length,
anatofuz
parents:
diff changeset
267 unsigned StartColumn) const override;
anatofuz
parents:
diff changeset
268 unsigned getRemainingLength(unsigned LineIndex, unsigned Offset,
anatofuz
parents:
diff changeset
269 unsigned StartColumn) const override;
anatofuz
parents:
diff changeset
270 unsigned getContentStartColumn(unsigned LineIndex, bool Break) const override;
anatofuz
parents:
diff changeset
271
anatofuz
parents:
diff changeset
272 protected:
anatofuz
parents:
diff changeset
273 // The column in which the token starts.
anatofuz
parents:
diff changeset
274 unsigned StartColumn;
anatofuz
parents:
diff changeset
275 // The prefix a line needs after a break in the token.
anatofuz
parents:
diff changeset
276 StringRef Prefix;
anatofuz
parents:
diff changeset
277 // The postfix a line needs before introducing a break.
anatofuz
parents:
diff changeset
278 StringRef Postfix;
anatofuz
parents:
diff changeset
279 // The token text excluding the prefix and postfix.
anatofuz
parents:
diff changeset
280 StringRef Line;
anatofuz
parents:
diff changeset
281 // Length of the sequence of tokens after this string literal that cannot
anatofuz
parents:
diff changeset
282 // contain line breaks.
anatofuz
parents:
diff changeset
283 unsigned UnbreakableTailLength;
anatofuz
parents:
diff changeset
284 };
anatofuz
parents:
diff changeset
285
anatofuz
parents:
diff changeset
286 class BreakableComment : public BreakableToken {
anatofuz
parents:
diff changeset
287 protected:
anatofuz
parents:
diff changeset
288 /// Creates a breakable token for a comment.
anatofuz
parents:
diff changeset
289 ///
anatofuz
parents:
diff changeset
290 /// \p StartColumn specifies the column in which the comment will start after
anatofuz
parents:
diff changeset
291 /// formatting.
anatofuz
parents:
diff changeset
292 BreakableComment(const FormatToken &Token, unsigned StartColumn,
anatofuz
parents:
diff changeset
293 bool InPPDirective, encoding::Encoding Encoding,
anatofuz
parents:
diff changeset
294 const FormatStyle &Style);
anatofuz
parents:
diff changeset
295
anatofuz
parents:
diff changeset
296 public:
anatofuz
parents:
diff changeset
297 bool supportsReflow() const override { return true; }
anatofuz
parents:
diff changeset
298 unsigned getLineCount() const override;
anatofuz
parents:
diff changeset
299 Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
anatofuz
parents:
diff changeset
300 unsigned ContentStartColumn,
anatofuz
parents:
diff changeset
301 const llvm::Regex &CommentPragmasRegex) const override;
anatofuz
parents:
diff changeset
302 void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
anatofuz
parents:
diff changeset
303 WhitespaceManager &Whitespaces) const override;
anatofuz
parents:
diff changeset
304
anatofuz
parents:
diff changeset
305 protected:
anatofuz
parents:
diff changeset
306 // Returns the token containing the line at LineIndex.
anatofuz
parents:
diff changeset
307 const FormatToken &tokenAt(unsigned LineIndex) const;
anatofuz
parents:
diff changeset
308
anatofuz
parents:
diff changeset
309 // Checks if the content of line LineIndex may be reflown with the previous
anatofuz
parents:
diff changeset
310 // line.
anatofuz
parents:
diff changeset
311 virtual bool mayReflow(unsigned LineIndex,
anatofuz
parents:
diff changeset
312 const llvm::Regex &CommentPragmasRegex) const = 0;
anatofuz
parents:
diff changeset
313
anatofuz
parents:
diff changeset
314 // Contains the original text of the lines of the block comment.
anatofuz
parents:
diff changeset
315 //
anatofuz
parents:
diff changeset
316 // In case of a block comments, excludes the leading /* in the first line and
anatofuz
parents:
diff changeset
317 // trailing */ in the last line. In case of line comments, excludes the
anatofuz
parents:
diff changeset
318 // leading // and spaces.
anatofuz
parents:
diff changeset
319 SmallVector<StringRef, 16> Lines;
anatofuz
parents:
diff changeset
320
anatofuz
parents:
diff changeset
321 // Contains the text of the lines excluding all leading and trailing
anatofuz
parents:
diff changeset
322 // whitespace between the lines. Note that the decoration (if present) is also
anatofuz
parents:
diff changeset
323 // not considered part of the text.
anatofuz
parents:
diff changeset
324 SmallVector<StringRef, 16> Content;
anatofuz
parents:
diff changeset
325
anatofuz
parents:
diff changeset
326 // Tokens[i] contains a reference to the token containing Lines[i] if the
anatofuz
parents:
diff changeset
327 // whitespace range before that token is managed by this block.
anatofuz
parents:
diff changeset
328 // Otherwise, Tokens[i] is a null pointer.
anatofuz
parents:
diff changeset
329 SmallVector<FormatToken *, 16> Tokens;
anatofuz
parents:
diff changeset
330
anatofuz
parents:
diff changeset
331 // ContentColumn[i] is the target column at which Content[i] should be.
anatofuz
parents:
diff changeset
332 // Note that this excludes a leading "* " or "*" in case of block comments
anatofuz
parents:
diff changeset
333 // where all lines have a "*" prefix, or the leading "// " or "//" in case of
anatofuz
parents:
diff changeset
334 // line comments.
anatofuz
parents:
diff changeset
335 //
anatofuz
parents:
diff changeset
336 // In block comments, the first line's target column is always positive. The
anatofuz
parents:
diff changeset
337 // remaining lines' target columns are relative to the first line to allow
anatofuz
parents:
diff changeset
338 // correct indentation of comments in \c WhitespaceManager. Thus they can be
anatofuz
parents:
diff changeset
339 // negative as well (in case the first line needs to be unindented more than
anatofuz
parents:
diff changeset
340 // there's actual whitespace in another line).
anatofuz
parents:
diff changeset
341 SmallVector<int, 16> ContentColumn;
anatofuz
parents:
diff changeset
342
anatofuz
parents:
diff changeset
343 // The intended start column of the first line of text from this section.
anatofuz
parents:
diff changeset
344 unsigned StartColumn;
anatofuz
parents:
diff changeset
345
anatofuz
parents:
diff changeset
346 // The prefix to use in front a line that has been reflown up.
anatofuz
parents:
diff changeset
347 // For example, when reflowing the second line after the first here:
anatofuz
parents:
diff changeset
348 // // comment 1
anatofuz
parents:
diff changeset
349 // // comment 2
anatofuz
parents:
diff changeset
350 // we expect:
anatofuz
parents:
diff changeset
351 // // comment 1 comment 2
anatofuz
parents:
diff changeset
352 // and not:
anatofuz
parents:
diff changeset
353 // // comment 1comment 2
anatofuz
parents:
diff changeset
354 StringRef ReflowPrefix = " ";
anatofuz
parents:
diff changeset
355 };
anatofuz
parents:
diff changeset
356
anatofuz
parents:
diff changeset
357 class BreakableBlockComment : public BreakableComment {
anatofuz
parents:
diff changeset
358 public:
anatofuz
parents:
diff changeset
359 BreakableBlockComment(const FormatToken &Token, unsigned StartColumn,
anatofuz
parents:
diff changeset
360 unsigned OriginalStartColumn, bool FirstInLine,
anatofuz
parents:
diff changeset
361 bool InPPDirective, encoding::Encoding Encoding,
anatofuz
parents:
diff changeset
362 const FormatStyle &Style, bool UseCRLF);
anatofuz
parents:
diff changeset
363
anatofuz
parents:
diff changeset
364 Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
anatofuz
parents:
diff changeset
365 unsigned ContentStartColumn,
anatofuz
parents:
diff changeset
366 const llvm::Regex &CommentPragmasRegex) const override;
anatofuz
parents:
diff changeset
367 unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
anatofuz
parents:
diff changeset
368 StringRef::size_type Length,
anatofuz
parents:
diff changeset
369 unsigned StartColumn) const override;
anatofuz
parents:
diff changeset
370 unsigned getRemainingLength(unsigned LineIndex, unsigned Offset,
anatofuz
parents:
diff changeset
371 unsigned StartColumn) const override;
anatofuz
parents:
diff changeset
372 unsigned getContentStartColumn(unsigned LineIndex, bool Break) const override;
anatofuz
parents:
diff changeset
373 unsigned getContentIndent(unsigned LineIndex) const override;
anatofuz
parents:
diff changeset
374 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
anatofuz
parents:
diff changeset
375 unsigned ContentIndent,
anatofuz
parents:
diff changeset
376 WhitespaceManager &Whitespaces) const override;
anatofuz
parents:
diff changeset
377 Split getReflowSplit(unsigned LineIndex,
anatofuz
parents:
diff changeset
378 const llvm::Regex &CommentPragmasRegex) const override;
anatofuz
parents:
diff changeset
379 void reflow(unsigned LineIndex,
anatofuz
parents:
diff changeset
380 WhitespaceManager &Whitespaces) const override;
anatofuz
parents:
diff changeset
381 bool introducesBreakBeforeToken() const override;
anatofuz
parents:
diff changeset
382 void adaptStartOfLine(unsigned LineIndex,
anatofuz
parents:
diff changeset
383 WhitespaceManager &Whitespaces) const override;
anatofuz
parents:
diff changeset
384 Split getSplitAfterLastLine(unsigned TailOffset) const override;
anatofuz
parents:
diff changeset
385
anatofuz
parents:
diff changeset
386 bool mayReflow(unsigned LineIndex,
anatofuz
parents:
diff changeset
387 const llvm::Regex &CommentPragmasRegex) const override;
anatofuz
parents:
diff changeset
388
anatofuz
parents:
diff changeset
389 // Contains Javadoc annotations that require additional indent when continued
anatofuz
parents:
diff changeset
390 // on multiple lines.
anatofuz
parents:
diff changeset
391 static const llvm::StringSet<> ContentIndentingJavadocAnnotations;
anatofuz
parents:
diff changeset
392
anatofuz
parents:
diff changeset
393 private:
anatofuz
parents:
diff changeset
394 // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex].
anatofuz
parents:
diff changeset
395 //
anatofuz
parents:
diff changeset
396 // Updates Content[LineIndex-1] and Content[LineIndex] by stripping off
anatofuz
parents:
diff changeset
397 // leading and trailing whitespace.
anatofuz
parents:
diff changeset
398 //
anatofuz
parents:
diff changeset
399 // Sets ContentColumn to the intended column in which the text at
anatofuz
parents:
diff changeset
400 // Lines[LineIndex] starts (note that the decoration, if present, is not
anatofuz
parents:
diff changeset
401 // considered part of the text).
anatofuz
parents:
diff changeset
402 void adjustWhitespace(unsigned LineIndex, int IndentDelta);
anatofuz
parents:
diff changeset
403
anatofuz
parents:
diff changeset
404 // The column at which the text of a broken line should start.
anatofuz
parents:
diff changeset
405 // Note that an optional decoration would go before that column.
anatofuz
parents:
diff changeset
406 // IndentAtLineBreak is a uniform position for all lines in a block comment,
anatofuz
parents:
diff changeset
407 // regardless of their relative position.
anatofuz
parents:
diff changeset
408 // FIXME: Revisit the decision to do this; the main reason was to support
anatofuz
parents:
diff changeset
409 // patterns like
anatofuz
parents:
diff changeset
410 // /**************//**
anatofuz
parents:
diff changeset
411 // * Comment
anatofuz
parents:
diff changeset
412 // We could also support such patterns by special casing the first line
anatofuz
parents:
diff changeset
413 // instead.
anatofuz
parents:
diff changeset
414 unsigned IndentAtLineBreak;
anatofuz
parents:
diff changeset
415
anatofuz
parents:
diff changeset
416 // This is to distinguish between the case when the last line was empty and
anatofuz
parents:
diff changeset
417 // the case when it started with a decoration ("*" or "* ").
anatofuz
parents:
diff changeset
418 bool LastLineNeedsDecoration;
anatofuz
parents:
diff changeset
419
anatofuz
parents:
diff changeset
420 // Either "* " if all lines begin with a "*", or empty.
anatofuz
parents:
diff changeset
421 StringRef Decoration;
anatofuz
parents:
diff changeset
422
anatofuz
parents:
diff changeset
423 // If this block comment has decorations, this is the column of the start of
anatofuz
parents:
diff changeset
424 // the decorations.
anatofuz
parents:
diff changeset
425 unsigned DecorationColumn;
anatofuz
parents:
diff changeset
426
anatofuz
parents:
diff changeset
427 // If true, make sure that the opening '/**' and the closing '*/' ends on a
anatofuz
parents:
diff changeset
428 // line of itself. Styles like jsdoc require this for multiline comments.
anatofuz
parents:
diff changeset
429 bool DelimitersOnNewline;
anatofuz
parents:
diff changeset
430
anatofuz
parents:
diff changeset
431 // Length of the sequence of tokens after this string literal that cannot
anatofuz
parents:
diff changeset
432 // contain line breaks.
anatofuz
parents:
diff changeset
433 unsigned UnbreakableTailLength;
anatofuz
parents:
diff changeset
434 };
anatofuz
parents:
diff changeset
435
anatofuz
parents:
diff changeset
436 class BreakableLineCommentSection : public BreakableComment {
anatofuz
parents:
diff changeset
437 public:
anatofuz
parents:
diff changeset
438 BreakableLineCommentSection(const FormatToken &Token, unsigned StartColumn,
anatofuz
parents:
diff changeset
439 unsigned OriginalStartColumn, bool FirstInLine,
anatofuz
parents:
diff changeset
440 bool InPPDirective, encoding::Encoding Encoding,
anatofuz
parents:
diff changeset
441 const FormatStyle &Style);
anatofuz
parents:
diff changeset
442
anatofuz
parents:
diff changeset
443 unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
anatofuz
parents:
diff changeset
444 StringRef::size_type Length,
anatofuz
parents:
diff changeset
445 unsigned StartColumn) const override;
anatofuz
parents:
diff changeset
446 unsigned getContentStartColumn(unsigned LineIndex, bool Break) const override;
anatofuz
parents:
diff changeset
447 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
anatofuz
parents:
diff changeset
448 unsigned ContentIndent,
anatofuz
parents:
diff changeset
449 WhitespaceManager &Whitespaces) const override;
anatofuz
parents:
diff changeset
450 Split getReflowSplit(unsigned LineIndex,
anatofuz
parents:
diff changeset
451 const llvm::Regex &CommentPragmasRegex) const override;
anatofuz
parents:
diff changeset
452 void reflow(unsigned LineIndex,
anatofuz
parents:
diff changeset
453 WhitespaceManager &Whitespaces) const override;
anatofuz
parents:
diff changeset
454 void adaptStartOfLine(unsigned LineIndex,
anatofuz
parents:
diff changeset
455 WhitespaceManager &Whitespaces) const override;
anatofuz
parents:
diff changeset
456 void updateNextToken(LineState &State) const override;
anatofuz
parents:
diff changeset
457 bool mayReflow(unsigned LineIndex,
anatofuz
parents:
diff changeset
458 const llvm::Regex &CommentPragmasRegex) const override;
anatofuz
parents:
diff changeset
459
anatofuz
parents:
diff changeset
460 private:
anatofuz
parents:
diff changeset
461 // OriginalPrefix[i] contains the original prefix of line i, including
anatofuz
parents:
diff changeset
462 // trailing whitespace before the start of the content. The indentation
anatofuz
parents:
diff changeset
463 // preceding the prefix is not included.
anatofuz
parents:
diff changeset
464 // For example, if the line is:
anatofuz
parents:
diff changeset
465 // // content
anatofuz
parents:
diff changeset
466 // then the original prefix is "// ".
anatofuz
parents:
diff changeset
467 SmallVector<StringRef, 16> OriginalPrefix;
anatofuz
parents:
diff changeset
468
anatofuz
parents:
diff changeset
469 // Prefix[i] contains the intended leading "//" with trailing spaces to
anatofuz
parents:
diff changeset
470 // account for the indentation of content within the comment at line i after
anatofuz
parents:
diff changeset
471 // formatting. It can be different than the original prefix when the original
anatofuz
parents:
diff changeset
472 // line starts like this:
anatofuz
parents:
diff changeset
473 // //content
anatofuz
parents:
diff changeset
474 // Then the original prefix is "//", but the prefix is "// ".
anatofuz
parents:
diff changeset
475 SmallVector<StringRef, 16> Prefix;
anatofuz
parents:
diff changeset
476
anatofuz
parents:
diff changeset
477 SmallVector<unsigned, 16> OriginalContentColumn;
anatofuz
parents:
diff changeset
478
anatofuz
parents:
diff changeset
479 /// The token to which the last line of this breakable token belongs
anatofuz
parents:
diff changeset
480 /// to; nullptr if that token is the initial token.
anatofuz
parents:
diff changeset
481 ///
anatofuz
parents:
diff changeset
482 /// The distinction is because if the token of the last line of this breakable
anatofuz
parents:
diff changeset
483 /// token is distinct from the initial token, this breakable token owns the
anatofuz
parents:
diff changeset
484 /// whitespace before the token of the last line, and the whitespace manager
anatofuz
parents:
diff changeset
485 /// must be able to modify it.
anatofuz
parents:
diff changeset
486 FormatToken *LastLineTok = nullptr;
anatofuz
parents:
diff changeset
487 };
anatofuz
parents:
diff changeset
488 } // namespace format
anatofuz
parents:
diff changeset
489 } // namespace clang
anatofuz
parents:
diff changeset
490
anatofuz
parents:
diff changeset
491 #endif