blob: 056d4bdd1bbd04bc54a6bbad9f0082861c9f8e51 [file] [log] [blame]
Alexander Kornienko4b672072013-06-03 16:45:03 +00001//===--- FormatToken.h - Format C++ code ------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file contains the declaration of the FormatToken, a wrapper
12/// around Token with additional information related to formatting.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_FORMAT_FORMAT_TOKEN_H
17#define LLVM_CLANG_FORMAT_FORMAT_TOKEN_H
18
19#include "clang/Basic/OperatorPrecedence.h"
Daniel Jasper8de9ed02013-08-22 15:00:41 +000020#include "clang/Format/Format.h"
Alexander Kornienko4b672072013-06-03 16:45:03 +000021#include "clang/Lex/Lexer.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000022#include <memory>
Alexander Kornienko4b672072013-06-03 16:45:03 +000023
24namespace clang {
25namespace format {
26
27enum TokenType {
Daniel Jasper1db6c382013-10-22 15:30:28 +000028 TT_ArrayInitializerLSquare,
29 TT_ArraySubscriptLSquare,
Daniel Jasper559b63c2014-01-28 20:13:43 +000030 TT_AttributeParen,
Alexander Kornienko4b672072013-06-03 16:45:03 +000031 TT_BinaryOperator,
Alexander Kornienko60d1b042013-10-10 13:36:20 +000032 TT_BitFieldColon,
Alexander Kornienko4b672072013-06-03 16:45:03 +000033 TT_BlockComment,
34 TT_CastRParen,
35 TT_ConditionalExpr,
Manuel Klimek68b03042014-04-14 09:14:11 +000036 TT_ConflictAlternative,
37 TT_ConflictEnd,
38 TT_ConflictStart,
Alexander Kornienko4b672072013-06-03 16:45:03 +000039 TT_CtorInitializerColon,
Daniel Jaspere33d4af2013-07-26 16:56:36 +000040 TT_CtorInitializerComma,
Alexander Kornienko4b672072013-06-03 16:45:03 +000041 TT_DesignatedInitializerPeriod,
Daniel Jasperb596fb22013-10-24 10:31:50 +000042 TT_DictLiteral,
Alexander Kornienko3cfa9732013-11-20 16:33:05 +000043 TT_FunctionLBrace,
Alexander Kornienko4b672072013-06-03 16:45:03 +000044 TT_FunctionTypeLParen,
Manuel Klimek68b03042014-04-14 09:14:11 +000045 TT_ImplicitStringLiteral,
46 TT_InheritanceColon,
47 TT_InlineASMColon,
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000048 TT_LambdaLSquare,
Alexander Kornienko4b672072013-06-03 16:45:03 +000049 TT_LineComment,
Daniel Jasperc13ee342014-03-27 09:43:54 +000050 TT_ObjCBlockLBrace,
Manuel Klimek68b03042014-04-14 09:14:11 +000051 TT_ObjCBlockLParen,
Alexander Kornienko4b672072013-06-03 16:45:03 +000052 TT_ObjCDecl,
Alexander Kornienko4b672072013-06-03 16:45:03 +000053 TT_ObjCForIn,
54 TT_ObjCMethodExpr,
55 TT_ObjCMethodSpecifier,
56 TT_ObjCProperty,
57 TT_ObjCSelectorName,
58 TT_OverloadedOperator,
59 TT_OverloadedOperatorLParen,
60 TT_PointerOrReference,
61 TT_PureVirtualSpecifier,
62 TT_RangeBasedForLoopColon,
Daniel Jasperf9ae3122014-05-08 07:01:45 +000063 TT_RegexLiteral,
Alexander Kornienko4b672072013-06-03 16:45:03 +000064 TT_StartOfName,
65 TT_TemplateCloser,
66 TT_TemplateOpener,
Daniel Jasper43e6a282013-12-16 15:01:54 +000067 TT_TrailingAnnotation,
Daniel Jasper6cdec7c2013-07-09 14:36:48 +000068 TT_TrailingReturnArrow,
Alexander Kornienko4b672072013-06-03 16:45:03 +000069 TT_TrailingUnaryOperator,
70 TT_UnaryOperator,
71 TT_Unknown
72};
73
Daniel Jasperb1f74a82013-07-09 09:06:29 +000074// Represents what type of block a set of braces open.
75enum BraceBlockKind {
76 BK_Unknown,
77 BK_Block,
78 BK_BracedInit
79};
80
Daniel Jasperb10cbc42013-07-10 14:02:49 +000081// The packing kind of a function's parameters.
82enum ParameterPackingKind {
83 PPK_BinPacked,
84 PPK_OnePerLine,
85 PPK_Inconclusive
86};
87
Manuel Klimek71814b42013-10-11 21:25:45 +000088enum FormatDecision {
89 FD_Unformatted,
90 FD_Continue,
91 FD_Break
92};
93
Daniel Jasper8de9ed02013-08-22 15:00:41 +000094class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000095class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +000096
Alexander Kornienko4b672072013-06-03 16:45:03 +000097/// \brief A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +000098/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +000099struct FormatToken {
100 FormatToken()
Alexander Kornienko632abb92013-09-02 13:58:14 +0000101 : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
Alexander Kornienko39856b72013-09-10 09:38:25 +0000102 ColumnWidth(0), LastLineColumnWidth(0), IsMultiline(false),
Alexander Kornienko632abb92013-09-02 13:58:14 +0000103 IsFirst(false), MustBreakBefore(false), IsUnterminatedLiteral(false),
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000104 BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
105 CanBreakBefore(false), ClosesTemplateDeclaration(false),
106 ParameterCount(0), PackingKind(PPK_Inconclusive), TotalLength(0),
Daniel Jasper63af7c42013-12-09 14:40:19 +0000107 UnbreakableTailLength(0), BindingStrength(0), NestingLevel(0),
108 SplitPenalty(0), LongestObjCSelectorName(0), FakeRParens(0),
Daniel Jasper562ecd42013-09-06 08:08:14 +0000109 StartsBinaryExpression(false), EndsBinaryExpression(false),
Daniel Jasper0e617842014-04-16 12:26:54 +0000110 OperatorIndex(0), LastOperator(false),
111 PartOfMultiVariableDeclStmt(false), IsForEachMacro(false),
112 MatchingParen(NULL), Previous(NULL), Next(NULL),
Manuel Klimek71814b42013-10-11 21:25:45 +0000113 Decision(FD_Unformatted), Finalized(false) {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000114
115 /// \brief The \c Token.
116 Token Tok;
117
118 /// \brief The number of newlines immediately before the \c Token.
119 ///
120 /// This can be used to determine what the user wrote in the original code
121 /// and thereby e.g. leave an empty line between two function definitions.
122 unsigned NewlinesBefore;
123
124 /// \brief Whether there is at least one unescaped newline before the \c
125 /// Token.
126 bool HasUnescapedNewline;
127
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000128 /// \brief The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000129 SourceRange WhitespaceRange;
130
131 /// \brief The offset just past the last '\n' in this token's leading
132 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
133 unsigned LastNewlineOffset;
134
Alexander Kornienko39856b72013-09-10 09:38:25 +0000135 /// \brief The width of the non-whitespace parts of the token (or its first
136 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000137 /// We need this to correctly measure number of columns a token spans.
Alexander Kornienko39856b72013-09-10 09:38:25 +0000138 unsigned ColumnWidth;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000139
Alexander Kornienko39856b72013-09-10 09:38:25 +0000140 /// \brief Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000141 /// token.
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000142 unsigned LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000143
Alexander Kornienko39856b72013-09-10 09:38:25 +0000144 /// \brief Whether the token text contains newlines (escaped or not).
145 bool IsMultiline;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000146
Alexander Kornienko4b672072013-06-03 16:45:03 +0000147 /// \brief Indicates that this is the first token.
148 bool IsFirst;
149
150 /// \brief Whether there must be a line break before this token.
151 ///
152 /// This happens for example when a preprocessor directive ended directly
153 /// before the token.
154 bool MustBreakBefore;
155
156 /// \brief Returns actual token start location without leading escaped
157 /// newlines and whitespace.
158 ///
159 /// This can be different to Tok.getLocation(), which includes leading escaped
160 /// newlines.
161 SourceLocation getStartOfNonWhitespace() const {
162 return WhitespaceRange.getEnd();
163 }
164
165 /// \brief The raw text of the token.
166 ///
167 /// Contains the raw token text without leading whitespace and without leading
168 /// escaped newlines.
169 StringRef TokenText;
170
Daniel Jasper8369aa52013-07-16 20:28:33 +0000171 /// \brief Set to \c true if this token is an unterminated literal.
172 bool IsUnterminatedLiteral;
173
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000174 /// \brief Contains the kind of block if this token is a brace.
175 BraceBlockKind BlockKind;
176
Alexander Kornienko4b672072013-06-03 16:45:03 +0000177 TokenType Type;
178
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000179 /// \brief The number of spaces that should be inserted before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000180 unsigned SpacesRequiredBefore;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000181
182 /// \brief \c true if it is allowed to break before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000183 bool CanBreakBefore;
184
185 bool ClosesTemplateDeclaration;
186
187 /// \brief Number of parameters, if this is "(", "[" or "<".
188 ///
189 /// This is initialized to 1 as we don't need to distinguish functions with
190 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
191 /// the number of commas.
192 unsigned ParameterCount;
193
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000194 /// \brief A token can have a special role that can carry extra information
195 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000196 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000197
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000198 /// \brief If this is an opening parenthesis, how are the parameters packed?
199 ParameterPackingKind PackingKind;
200
Manuel Klimek31c85922013-08-29 15:21:40 +0000201 /// \brief The total length of the unwrapped line up to and including this
202 /// token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000203 unsigned TotalLength;
204
Alexander Kornienko39856b72013-09-10 09:38:25 +0000205 /// \brief The original 0-based column of this token, including expanded tabs.
206 /// The configured TabWidth is used as tab width.
Manuel Klimek31c85922013-08-29 15:21:40 +0000207 unsigned OriginalColumn;
208
Alexander Kornienko4b672072013-06-03 16:45:03 +0000209 /// \brief The length of following tokens until the next natural split point,
210 /// or the next token that can be broken.
211 unsigned UnbreakableTailLength;
212
213 // FIXME: Come up with a 'cleaner' concept.
214 /// \brief The binding strength of a token. This is a combined value of
215 /// operator precedence, parenthesis nesting, etc.
216 unsigned BindingStrength;
217
Daniel Jasper63af7c42013-12-09 14:40:19 +0000218 /// \brief The nesting level of this token, i.e. the number of surrounding (),
219 /// [], {} or <>.
220 unsigned NestingLevel;
221
Alexander Kornienko4b672072013-06-03 16:45:03 +0000222 /// \brief Penalty for inserting a line break before this token.
223 unsigned SplitPenalty;
224
225 /// \brief If this is the first ObjC selector name in an ObjC method
226 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000227 ///
228 /// This being set to 0 means that the selectors should not be colon-aligned,
229 /// e.g. because several of them are block-type.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000230 unsigned LongestObjCSelectorName;
231
232 /// \brief Stores the number of required fake parentheses and the
233 /// corresponding operator precedence.
234 ///
235 /// If multiple fake parentheses start at a token, this vector stores them in
236 /// reverse order, i.e. inner fake parenthesis first.
237 SmallVector<prec::Level, 4> FakeLParens;
238 /// \brief Insert this many fake ) after this token for correct indentation.
239 unsigned FakeRParens;
240
Daniel Jasper562ecd42013-09-06 08:08:14 +0000241 /// \brief \c true if this token starts a binary expression, i.e. has at least
242 /// one fake l_paren with a precedence greater than prec::Unknown.
243 bool StartsBinaryExpression;
244 /// \brief \c true if this token ends a binary expression.
245 bool EndsBinaryExpression;
246
Daniel Jasper0e617842014-04-16 12:26:54 +0000247 /// \brief Is this is an operator (or "."/"->") in a sequence of operators
248 /// with the same precedence, contains the 0-based operator index.
249 unsigned OperatorIndex;
250
251 /// \brief Is this the last operator (or "."/"->") in a sequence of operators
252 /// with the same precedence?
253 bool LastOperator;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000254
255 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
256 ///
257 /// Only set if \c Type == \c TT_StartOfName.
258 bool PartOfMultiVariableDeclStmt;
259
Daniel Jaspere1e43192014-04-01 12:55:11 +0000260 /// \brief Is this a foreach macro?
261 bool IsForEachMacro;
262
Alexander Kornienko4b672072013-06-03 16:45:03 +0000263 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
264
265 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
266 return is(K1) || is(K2);
267 }
268
269 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const {
270 return is(K1) || is(K2) || is(K3);
271 }
272
273 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3,
274 tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS,
275 tok::TokenKind K6 = tok::NUM_TOKENS,
276 tok::TokenKind K7 = tok::NUM_TOKENS,
277 tok::TokenKind K8 = tok::NUM_TOKENS,
278 tok::TokenKind K9 = tok::NUM_TOKENS,
279 tok::TokenKind K10 = tok::NUM_TOKENS,
280 tok::TokenKind K11 = tok::NUM_TOKENS,
281 tok::TokenKind K12 = tok::NUM_TOKENS) const {
282 return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) ||
283 is(K8) || is(K9) || is(K10) || is(K11) || is(K12);
284 }
285
286 bool isNot(tok::TokenKind Kind) const { return Tok.isNot(Kind); }
Daniel Jasper04b6a082013-12-20 06:22:01 +0000287 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000288
289 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
290 return Tok.isObjCAtKeyword(Kind);
291 }
292
293 bool isAccessSpecifier(bool ColonRequired = true) const {
294 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
295 (!ColonRequired || (Next && Next->is(tok::colon)));
296 }
297
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000298 /// \brief Determine whether the token is a simple-type-specifier.
299 bool isSimpleTypeSpecifier() const;
300
Alexander Kornienko4b672072013-06-03 16:45:03 +0000301 bool isObjCAccessSpecifier() const {
302 return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
303 Next->isObjCAtKeyword(tok::objc_protected) ||
304 Next->isObjCAtKeyword(tok::objc_package) ||
305 Next->isObjCAtKeyword(tok::objc_private));
306 }
307
308 /// \brief Returns whether \p Tok is ([{ or a template opening <.
309 bool opensScope() const {
310 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square) ||
311 Type == TT_TemplateOpener;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000312 }
Nico Weber0f987a62013-06-25 19:25:12 +0000313 /// \brief Returns whether \p Tok is )]} or a template closing >.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000314 bool closesScope() const {
315 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square) ||
316 Type == TT_TemplateCloser;
317 }
318
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000319 /// \brief Returns \c true if this is a "." or "->" accessing a member.
320 bool isMemberAccess() const {
321 return isOneOf(tok::arrow, tok::period) &&
322 Type != TT_DesignatedInitializerPeriod;
323 }
324
Alexander Kornienko4b672072013-06-03 16:45:03 +0000325 bool isUnaryOperator() const {
326 switch (Tok.getKind()) {
327 case tok::plus:
328 case tok::plusplus:
329 case tok::minus:
330 case tok::minusminus:
331 case tok::exclaim:
332 case tok::tilde:
333 case tok::kw_sizeof:
334 case tok::kw_alignof:
335 return true;
336 default:
337 return false;
338 }
339 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000340
Alexander Kornienko4b672072013-06-03 16:45:03 +0000341 bool isBinaryOperator() const {
342 // Comma is a binary operator, but does not behave as such wrt. formatting.
343 return getPrecedence() > prec::Comma;
344 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000345
Alexander Kornienko4b672072013-06-03 16:45:03 +0000346 bool isTrailingComment() const {
347 return is(tok::comment) && (!Next || Next->NewlinesBefore > 0);
348 }
349
350 prec::Level getPrecedence() const {
351 return getBinOpPrecedence(Tok.getKind(), true, true);
352 }
353
354 /// \brief Returns the previous token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000355 FormatToken *getPreviousNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000356 FormatToken *Tok = Previous;
357 while (Tok != NULL && Tok->is(tok::comment))
358 Tok = Tok->Previous;
359 return Tok;
360 }
361
362 /// \brief Returns the next token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000363 const FormatToken *getNextNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000364 const FormatToken *Tok = Next;
365 while (Tok != NULL && Tok->is(tok::comment))
366 Tok = Tok->Next;
367 return Tok;
368 }
369
Daniel Jasperb8f61682013-10-22 15:45:58 +0000370 /// \brief Returns \c true if this tokens starts a block-type list, i.e. a
371 /// list that should be indented with a block indent.
372 bool opensBlockTypeList(const FormatStyle &Style) const {
373 return Type == TT_ArrayInitializerLSquare ||
374 (is(tok::l_brace) &&
Daniel Jasperb596fb22013-10-24 10:31:50 +0000375 (BlockKind == BK_Block || Type == TT_DictLiteral ||
Daniel Jasperb8f61682013-10-22 15:45:58 +0000376 !Style.Cpp11BracedListStyle));
377 }
378
379 /// \brief Same as opensBlockTypeList, but for the closing token.
Daniel Jasper1db6c382013-10-22 15:30:28 +0000380 bool closesBlockTypeList(const FormatStyle &Style) const {
Daniel Jasperb8f61682013-10-22 15:45:58 +0000381 return MatchingParen && MatchingParen->opensBlockTypeList(Style);
Daniel Jasper1db6c382013-10-22 15:30:28 +0000382 }
383
Alexander Kornienko4b672072013-06-03 16:45:03 +0000384 FormatToken *MatchingParen;
385
386 FormatToken *Previous;
387 FormatToken *Next;
388
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000389 SmallVector<AnnotatedLine *, 1> Children;
390
Manuel Klimek71814b42013-10-11 21:25:45 +0000391 /// \brief Stores the formatting decision for the token once it was made.
392 FormatDecision Decision;
393
394 /// \brief If \c true, this token has been fully formatted (indented and
395 /// potentially re-formatted inside), and we do not allow further formatting
396 /// changes.
397 bool Finalized;
398
Alexander Kornienko4b672072013-06-03 16:45:03 +0000399private:
400 // Disallow copying.
Craig Topper411294d2013-07-01 04:07:34 +0000401 FormatToken(const FormatToken &) LLVM_DELETED_FUNCTION;
402 void operator=(const FormatToken &) LLVM_DELETED_FUNCTION;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000403};
404
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000405class ContinuationIndenter;
406struct LineState;
407
408class TokenRole {
409public:
410 TokenRole(const FormatStyle &Style) : Style(Style) {}
411 virtual ~TokenRole();
412
413 /// \brief After the \c TokenAnnotator has finished annotating all the tokens,
414 /// this function precomputes required information for formatting.
415 virtual void precomputeFormattingInfos(const FormatToken *Token);
416
417 /// \brief Apply the special formatting that the given role demands.
418 ///
Daniel Jasper01603472014-01-09 13:42:56 +0000419 /// Assumes that the token having this role is already formatted.
420 ///
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000421 /// Continues formatting from \p State leaving indentation to \p Indenter and
422 /// returns the total penalty that this formatting incurs.
Daniel Jasper01603472014-01-09 13:42:56 +0000423 virtual unsigned formatFromToken(LineState &State,
424 ContinuationIndenter *Indenter,
425 bool DryRun) {
426 return 0;
427 }
428
429 /// \brief Same as \c formatFromToken, but assumes that the first token has
430 /// already been set thereby deciding on the first line break.
431 virtual unsigned formatAfterToken(LineState &State,
432 ContinuationIndenter *Indenter,
433 bool DryRun) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000434 return 0;
435 }
436
437 /// \brief Notifies the \c Role that a comma was found.
438 virtual void CommaFound(const FormatToken *Token) {}
439
440protected:
441 const FormatStyle &Style;
442};
443
444class CommaSeparatedList : public TokenRole {
445public:
Daniel Jasper01603472014-01-09 13:42:56 +0000446 CommaSeparatedList(const FormatStyle &Style)
447 : TokenRole(Style), HasNestedBracedList(false) {}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000448
Craig Topperfb6b25b2014-03-15 04:29:04 +0000449 void precomputeFormattingInfos(const FormatToken *Token) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000450
Craig Topperfb6b25b2014-03-15 04:29:04 +0000451 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
452 bool DryRun) override;
Daniel Jasper01603472014-01-09 13:42:56 +0000453
Craig Topperfb6b25b2014-03-15 04:29:04 +0000454 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
455 bool DryRun) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000456
457 /// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
Craig Topperfb6b25b2014-03-15 04:29:04 +0000458 void CommaFound(const FormatToken *Token) override { Commas.push_back(Token);}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000459
460private:
461 /// \brief A struct that holds information on how to format a given list with
462 /// a specific number of columns.
463 struct ColumnFormat {
464 /// \brief The number of columns to use.
465 unsigned Columns;
466
467 /// \brief The total width in characters.
468 unsigned TotalWidth;
469
470 /// \brief The number of lines required for this format.
471 unsigned LineCount;
472
473 /// \brief The size of each column in characters.
474 SmallVector<unsigned, 8> ColumnSizes;
475 };
476
477 /// \brief Calculate which \c ColumnFormat fits best into
478 /// \p RemainingCharacters.
479 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
480
481 /// \brief The ordered \c FormatTokens making up the commas of this list.
482 SmallVector<const FormatToken *, 8> Commas;
483
484 /// \brief The length of each of the list's items in characters including the
485 /// trailing comma.
486 SmallVector<unsigned, 8> ItemLengths;
487
488 /// \brief Precomputed formats that can be used for this list.
489 SmallVector<ColumnFormat, 4> Formats;
Daniel Jasper01603472014-01-09 13:42:56 +0000490
491 bool HasNestedBracedList;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000492};
493
Alexander Kornienko4b672072013-06-03 16:45:03 +0000494} // namespace format
495} // namespace clang
496
497#endif // LLVM_CLANG_FORMAT_FORMAT_TOKEN_H