blob: cf8105347a143df8ea46c464cbc849d4d06290d1 [file] [log] [blame]
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00001//===--- BreakableToken.h - 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
Krasimir Georgiev91834222017-01-25 13:58:58 +000011/// \brief Declares BreakableToken, BreakableStringLiteral, BreakableComment,
12/// BreakableBlockComment and BreakableLineCommentSection classes, that contain
13/// token type-specific logic to break long lines in tokens and reflow content
14/// between tokens.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000015///
16//===----------------------------------------------------------------------===//
17
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000018#ifndef LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H
19#define LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000020
Alexander Kornienkoffcc0102013-06-05 14:09:10 +000021#include "Encoding.h"
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000022#include "TokenAnnotator.h"
23#include "WhitespaceManager.h"
24#include <utility>
25
26namespace clang {
27namespace format {
28
Krasimir Georgiev91834222017-01-25 13:58:58 +000029/// \brief Checks if \p Token switches formatting, like /* clang-format off */.
30/// \p Token must be a comment.
31bool switchesFormatting(const FormatToken &Token);
32
Manuel Klimek9043c742013-05-27 15:23:34 +000033struct FormatStyle;
34
35/// \brief Base class for strategies on how to break tokens.
36///
Krasimir Georgiev91834222017-01-25 13:58:58 +000037/// This is organised around the concept of a \c Split, which is a whitespace
38/// range that signifies a position of the content of a token where a
39/// reformatting might be done. Operating with splits is divided into 3
40/// operations:
41/// - getSplit, for finding a split starting at a position,
42/// - getLineLengthAfterSplit, for calculating the size in columns of the rest
43/// of the content after a split has been used for breaking, and
44/// - insertBreak, for executing the split using a whitespace manager.
45///
46/// There is a pair of operations that are used to compress a long whitespace
47/// range with a single space if that will bring the line lenght under the
48/// column limit:
49/// - getLineLengthAfterCompression, for calculating the size in columns of the
50/// line after a whitespace range has been compressed, and
51/// - compressWhitespace, for executing the whitespace compression using a
52/// whitespace manager; note that the compressed whitespace may be in the
53/// middle of the original line and of the reformatted line.
54///
55/// For tokens where the whitespace before each line needs to be also
56/// reformatted, for example for tokens supporting reflow, there are analogous
57/// operations that might be executed before the main line breaking occurs:
58/// - getSplitBefore, for finding a split such that the content preceding it
59/// needs to be specially reflown,
60/// - getLineLengthAfterSplitBefore, for calculating the line length in columns
61/// of the remainder of the content after the beginning of the content has
62/// been reformatted, and
63/// - replaceWhitespaceBefore, for executing the reflow using a whitespace
64/// manager.
65///
Manuel Klimek9043c742013-05-27 15:23:34 +000066/// FIXME: The interface seems set in stone, so we might want to just pull the
67/// strategy into the class, instead of controlling it from the outside.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000068class BreakableToken {
69public:
Alexander Kornienkodd7ece52013-06-07 16:02:52 +000070 /// \brief Contains starting character index and length of split.
Manuel Klimek9043c742013-05-27 15:23:34 +000071 typedef std::pair<StringRef::size_type, unsigned> Split;
72
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000073 virtual ~BreakableToken() {}
Manuel Klimek9043c742013-05-27 15:23:34 +000074
75 /// \brief Returns the number of lines in this token in the original code.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000076 virtual unsigned getLineCount() const = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +000077
Alexander Kornienkodd7ece52013-06-07 16:02:52 +000078 /// \brief Returns the number of columns required to format the piece of line
Krasimir Georgiev91834222017-01-25 13:58:58 +000079 /// at \p LineIndex, from byte offset \p TailOffset with length \p Length.
Manuel Klimek9043c742013-05-27 15:23:34 +000080 ///
Krasimir Georgiev91834222017-01-25 13:58:58 +000081 /// Note that previous breaks are not taken into account. \p TailOffset is
82 /// always specified from the start of the (original) line.
Alexander Kornienkodd7ece52013-06-07 16:02:52 +000083 /// \p Length can be set to StringRef::npos, which means "to the end of line".
84 virtual unsigned
Krasimir Georgiev91834222017-01-25 13:58:58 +000085 getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +000086 StringRef::size_type Length) const = 0;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000087
Manuel Klimek9043c742013-05-27 15:23:34 +000088 /// \brief Returns a range (offset, length) at which to break the line at
89 /// \p LineIndex, if previously broken at \p TailOffset. If possible, do not
90 /// violate \p ColumnLimit.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000091 virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
Alexander Kornienko9e90b622013-04-17 17:34:05 +000092 unsigned ColumnLimit) const = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +000093
94 /// \brief Emits the previously retrieved \p Split via \p Whitespaces.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000095 virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000096 WhitespaceManager &Whitespaces) = 0;
Manuel Klimek9043c742013-05-27 15:23:34 +000097
Krasimir Georgiev91834222017-01-25 13:58:58 +000098 /// \brief Returns the number of columns required to format the piece of line
99 /// at \p LineIndex, from byte offset \p TailOffset after the whitespace range
100 /// \p Split has been compressed into a single space.
101 unsigned getLineLengthAfterCompression(unsigned RemainingTokenColumns,
102 Split Split) const;
103
Alexander Kornienko875395f2013-11-12 17:50:13 +0000104 /// \brief Replaces the whitespace range described by \p Split with a single
105 /// space.
Krasimir Georgiev91834222017-01-25 13:58:58 +0000106 virtual void compressWhitespace(unsigned LineIndex, unsigned TailOffset,
107 Split Split,
108 WhitespaceManager &Whitespaces) = 0;
109
110 /// \brief Returns a whitespace range (offset, length) of the content at
111 /// \p LineIndex such that the content preceding this range needs to be
112 /// reformatted before any breaks are made to this line.
113 ///
114 /// \p PreviousEndColumn is the end column of the previous line after
115 /// formatting.
116 ///
117 /// A result having offset == StringRef::npos means that no piece of the line
118 /// needs to be reformatted before any breaks are made.
119 virtual Split getSplitBefore(unsigned LineIndex,
120 unsigned PreviousEndColumn,
121 unsigned ColumnLimit) const {
122 return Split(StringRef::npos, 0);
123 }
124
125 /// \brief Returns the number of columns required to format the piece of line
126 /// at \p LineIndex after the content preceding the whitespace range specified
127 /// \p SplitBefore has been reformatted, but before any breaks are made to
128 /// this line.
129 virtual unsigned getLineLengthAfterSplitBefore(unsigned LineIndex,
130 unsigned TailOffset,
131 unsigned PreviousEndColumn,
132 unsigned ColumnLimit,
133 Split SplitBefore) const {
134 return getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
135 }
Alexander Kornienko875395f2013-11-12 17:50:13 +0000136
Manuel Klimek9043c742013-05-27 15:23:34 +0000137 /// \brief Replaces the whitespace between \p LineIndex-1 and \p LineIndex.
Krasimir Georgiev91834222017-01-25 13:58:58 +0000138 /// Performs a reformatting of the content at \p LineIndex preceding the
139 /// whitespace range \p SplitBefore.
Manuel Klimek9043c742013-05-27 15:23:34 +0000140 virtual void replaceWhitespaceBefore(unsigned LineIndex,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000141 unsigned PreviousEndColumn,
142 unsigned ColumnLimit,
143 Split SplitBefore,
Manuel Klimek9043c742013-05-27 15:23:34 +0000144 WhitespaceManager &Whitespaces) {}
145
Krasimir Georgiev91834222017-01-25 13:58:58 +0000146 /// \brief Updates the next token of \p State to the next token after this
147 /// one. This can be used when this token manages a set of underlying tokens
148 /// as a unit and is responsible for the formatting of the them.
149 virtual void updateNextToken(LineState &State) const {}
150
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000151protected:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000152 BreakableToken(const FormatToken &Tok, bool InPPDirective,
153 encoding::Encoding Encoding, const FormatStyle &Style)
154 : Tok(Tok), InPPDirective(InPPDirective), Encoding(Encoding),
155 Style(Style) {}
Manuel Klimek9043c742013-05-27 15:23:34 +0000156
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000157 const FormatToken &Tok;
Alexander Kornienkobe633902013-06-14 11:46:10 +0000158 const bool InPPDirective;
159 const encoding::Encoding Encoding;
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000160 const FormatStyle &Style;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000161};
162
Manuel Klimek9043c742013-05-27 15:23:34 +0000163/// \brief Base class for single line tokens that can be broken.
164///
165/// \c getSplit() needs to be implemented by child classes.
166class BreakableSingleLineToken : public BreakableToken {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000167public:
Craig Topperfb6b25b2014-03-15 04:29:04 +0000168 unsigned getLineCount() const override;
169 unsigned getLineLengthAfterSplit(unsigned LineIndex, unsigned TailOffset,
170 StringRef::size_type Length) const override;
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000171
172protected:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000173 BreakableSingleLineToken(const FormatToken &Tok, unsigned StartColumn,
174 StringRef Prefix, StringRef Postfix,
175 bool InPPDirective, encoding::Encoding Encoding,
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000176 const FormatStyle &Style);
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000177
Manuel Klimek9043c742013-05-27 15:23:34 +0000178 // The column in which the token starts.
179 unsigned StartColumn;
180 // The prefix a line needs after a break in the token.
181 StringRef Prefix;
182 // The postfix a line needs before introducing a break.
183 StringRef Postfix;
184 // The token text excluding the prefix and postfix.
185 StringRef Line;
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000186};
187
Manuel Klimek9043c742013-05-27 15:23:34 +0000188class BreakableStringLiteral : public BreakableSingleLineToken {
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000189public:
Manuel Klimek9043c742013-05-27 15:23:34 +0000190 /// \brief Creates a breakable token for a single line string literal.
191 ///
192 /// \p StartColumn specifies the column in which the token will start
193 /// after formatting.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000194 BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn,
195 StringRef Prefix, StringRef Postfix,
196 bool InPPDirective, encoding::Encoding Encoding,
197 const FormatStyle &Style);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000198
Craig Topperfb6b25b2014-03-15 04:29:04 +0000199 Split getSplit(unsigned LineIndex, unsigned TailOffset,
200 unsigned ColumnLimit) const override;
201 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
202 WhitespaceManager &Whitespaces) override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000203 void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
204 WhitespaceManager &Whitespaces) override {}
Manuel Klimek9043c742013-05-27 15:23:34 +0000205};
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000206
Krasimir Georgiev91834222017-01-25 13:58:58 +0000207class BreakableComment : public BreakableToken {
208protected:
209 /// \brief Creates a breakable token for a comment.
Manuel Klimek9043c742013-05-27 15:23:34 +0000210 ///
211 /// \p StartColumn specifies the column in which the comment will start
212 /// after formatting, while \p OriginalStartColumn specifies in which
213 /// column the comment started before formatting.
214 /// If the comment starts a line after formatting, set \p FirstInLine to true.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000215 BreakableComment(const FormatToken &Token, unsigned StartColumn,
216 unsigned OriginalStartColumn, bool FirstInLine,
217 bool InPPDirective, encoding::Encoding Encoding,
218 const FormatStyle &Style);
Krasimir Georgiev91834222017-01-25 13:58:58 +0000219
220public:
221 unsigned getLineCount() const override;
222 Split getSplit(unsigned LineIndex, unsigned TailOffset,
223 unsigned ColumnLimit) const override;
224 void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
225 WhitespaceManager &Whitespaces) override;
226
227protected:
228 virtual unsigned getContentStartColumn(unsigned LineIndex,
229 unsigned TailOffset) const = 0;
230
231 // Returns a split that divides Text into a left and right parts, such that
232 // the left part is suitable for reflowing after PreviousEndColumn.
233 Split getReflowSplit(StringRef Text, StringRef ReflowPrefix,
234 unsigned PreviousEndColumn, unsigned ColumnLimit) const;
235
236 // Returns the token containing the line at LineIndex.
237 const FormatToken &tokenAt(unsigned LineIndex) const;
238
239 // Checks if the content of line LineIndex may be reflown with the previous
240 // line.
241 bool mayReflow(unsigned LineIndex) const;
242
243 // Contains the original text of the lines of the block comment.
244 //
245 // In case of a block comments, excludes the leading /* in the first line and
246 // trailing */ in the last line. In case of line comments, excludes the
247 // leading // and spaces.
248 SmallVector<StringRef, 16> Lines;
249
250 // Contains the text of the lines excluding all leading and trailing
251 // whitespace between the lines. Note that the decoration (if present) is also
252 // not considered part of the text.
253 SmallVector<StringRef, 16> Content;
254
255 // Tokens[i] contains a reference to the token containing Lines[i] if the
256 // whitespace range before that token is managed by this block.
257 // Otherwise, Tokens[i] is a null pointer.
258 SmallVector<FormatToken *, 16> Tokens;
259
260 // ContentColumn[i] is the target column at which Content[i] should be.
261 // Note that this excludes a leading "* " or "*" in case of block comments
262 // where all lines have a "*" prefix, or the leading "// " or "//" in case of
263 // line comments.
264 //
265 // In block comments, the first line's target column is always positive. The
266 // remaining lines' target columns are relative to the first line to allow
267 // correct indentation of comments in \c WhitespaceManager. Thus they can be
268 // negative as well (in case the first line needs to be unindented more than
269 // there's actual whitespace in another line).
270 SmallVector<int, 16> ContentColumn;
271
272 // The intended start column of the first line of text from this section.
273 unsigned StartColumn;
274
275 // The original start column of the first line of text from this section.
276 unsigned OriginalStartColumn;
277
278 // Whether the first token of this section is the first token in its unwrapped
279 // line.
280 bool FirstInLine;
281
282 // In case of line comments, holds the original prefix, including trailing
283 // whitespace.
284 SmallVector<StringRef, 16> OriginalPrefix;
285
286 // The prefix to use in front a line that has been reflown up.
287 // For example, when reflowing the second line after the first here:
288 // // comment 1
289 // // comment 2
290 // we expect:
291 // // comment 1 comment 2
292 // and not:
293 // // comment 1comment 2
294 StringRef ReflowPrefix = " ";
295};
296
297class BreakableBlockComment : public BreakableComment {
298public:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000299 BreakableBlockComment(const FormatToken &Token, unsigned StartColumn,
300 unsigned OriginalStartColumn, bool FirstInLine,
301 bool InPPDirective, encoding::Encoding Encoding,
302 const FormatStyle &Style);
Manuel Klimek9043c742013-05-27 15:23:34 +0000303
Krasimir Georgiev91834222017-01-25 13:58:58 +0000304 unsigned getLineLengthAfterSplit(unsigned LineIndex,
305 unsigned TailOffset,
Craig Topperfb6b25b2014-03-15 04:29:04 +0000306 StringRef::size_type Length) const override;
Craig Topperfb6b25b2014-03-15 04:29:04 +0000307 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
308 WhitespaceManager &Whitespaces) override;
Krasimir Georgiev91834222017-01-25 13:58:58 +0000309 Split getSplitBefore(unsigned LineIndex, unsigned PreviousEndColumn,
310 unsigned ColumnLimit) const override;
311 unsigned getLineLengthAfterSplitBefore(unsigned LineIndex,
312 unsigned TailOffset,
313 unsigned PreviousEndColumn,
314 unsigned ColumnLimit,
315 Split SplitBefore) const override;
316 void replaceWhitespaceBefore(unsigned LineIndex, unsigned PreviousEndColumn,
317 unsigned ColumnLimit,
318 Split SplitBefore,
Craig Topperfb6b25b2014-03-15 04:29:04 +0000319 WhitespaceManager &Whitespaces) override;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000320
321private:
Krasimir Georgiev91834222017-01-25 13:58:58 +0000322 // Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex].
Manuel Klimek9043c742013-05-27 15:23:34 +0000323 //
Krasimir Georgiev91834222017-01-25 13:58:58 +0000324 // Updates Content[LineIndex-1] and Content[LineIndex] by stripping off
325 // leading and trailing whitespace.
326 //
327 // Sets ContentColumn to the intended column in which the text at
Manuel Klimek9043c742013-05-27 15:23:34 +0000328 // Lines[LineIndex] starts (note that the decoration, if present, is not
329 // considered part of the text).
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000330 void adjustWhitespace(unsigned LineIndex, int IndentDelta);
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000331
Krasimir Georgiev91834222017-01-25 13:58:58 +0000332 // Computes the end column if the full Content from LineIndex gets reflown
333 // after PreviousEndColumn.
334 unsigned getReflownColumn(StringRef Content,
335 unsigned LineIndex,
336 unsigned PreviousEndColumn) const;
Alexander Kornienko9e90b622013-04-17 17:34:05 +0000337
Krasimir Georgiev91834222017-01-25 13:58:58 +0000338 unsigned getContentStartColumn(unsigned LineIndex,
339 unsigned TailOffset) const override;
Manuel Klimek9043c742013-05-27 15:23:34 +0000340
341 // The column at which the text of a broken line should start.
342 // Note that an optional decoration would go before that column.
343 // IndentAtLineBreak is a uniform position for all lines in a block comment,
344 // regardless of their relative position.
345 // FIXME: Revisit the decision to do this; the main reason was to support
346 // patterns like
347 // /**************//**
348 // * Comment
349 // We could also support such patterns by special casing the first line
350 // instead.
351 unsigned IndentAtLineBreak;
352
Alexander Kornienko614d96a2013-07-08 14:12:07 +0000353 // This is to distinguish between the case when the last line was empty and
354 // the case when it started with a decoration ("*" or "* ").
355 bool LastLineNeedsDecoration;
356
Manuel Klimek9043c742013-05-27 15:23:34 +0000357 // Either "* " if all lines begin with a "*", or empty.
358 StringRef Decoration;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000359};
360
Krasimir Georgiev91834222017-01-25 13:58:58 +0000361class BreakableLineCommentSection : public BreakableComment {
362public:
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000363 BreakableLineCommentSection(const FormatToken &Token, unsigned StartColumn,
Krasimir Georgiev91834222017-01-25 13:58:58 +0000364 unsigned OriginalStartColumn, bool FirstInLine,
365 bool InPPDirective, encoding::Encoding Encoding,
366 const FormatStyle &Style);
367
368 unsigned getLineLengthAfterSplit(unsigned LineIndex,
369 unsigned TailOffset,
370 StringRef::size_type Length) const override;
371 void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
372 WhitespaceManager &Whitespaces) override;
373 Split getSplitBefore(unsigned LineIndex, unsigned PreviousEndColumn,
374 unsigned ColumnLimit) const override;
375 unsigned getLineLengthAfterSplitBefore(unsigned LineIndex, unsigned TailOffset,
376 unsigned PreviousEndColumn,
377 unsigned ColumnLimit,
378 Split SplitBefore) const override;
379 void replaceWhitespaceBefore(unsigned LineIndex, unsigned PreviousEndColumn,
380 unsigned ColumnLimit, Split SplitBefore,
381 WhitespaceManager &Whitespaces) override;
382 void updateNextToken(LineState& State) const override;
383
384private:
385 unsigned getContentStartColumn(unsigned LineIndex,
386 unsigned TailOffset) const override;
387
388 // Prefix[i] contains the intended leading "//" with trailing spaces to
389 // account for the indentation of content within the comment at line i after
390 // formatting. It can be different than the original prefix when the original
391 // line starts like this:
392 // //content
393 // Then the original prefix is "//", but the prefix is "// ".
394 SmallVector<StringRef, 16> Prefix;
395
396 SmallVector<unsigned, 16> OriginalContentColumn;
397
398 /// \brief The token to which the last line of this breakable token belongs
399 /// to; nullptr if that token is the initial token.
400 ///
401 /// The distinction is because if the token of the last line of this breakable
402 /// token is distinct from the initial token, this breakable token owns the
403 /// whitespace before the token of the last line, and the whitespace manager
404 /// must be able to modify it.
405 FormatToken *LastLineTok = nullptr;
406};
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000407} // namespace format
408} // namespace clang
409
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000410#endif