blob: 425a7c836483f3d54bf466cc4f0ece16725ce9c7 [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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000016#ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
17#define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
Alexander Kornienko4b672072013-06-03 16:45:03 +000018
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,
Daniel Jasper4355e7f2014-07-09 07:50:33 +000043 TT_FunctionDeclarationName,
Alexander Kornienko3cfa9732013-11-20 16:33:05 +000044 TT_FunctionLBrace,
Alexander Kornienko4b672072013-06-03 16:45:03 +000045 TT_FunctionTypeLParen,
Manuel Klimek68b03042014-04-14 09:14:11 +000046 TT_ImplicitStringLiteral,
47 TT_InheritanceColon,
48 TT_InlineASMColon,
Daniel Jasperfab69ff2014-10-21 08:24:18 +000049 TT_JavaAnnotation,
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000050 TT_LambdaLSquare,
Alexander Kornienko4b672072013-06-03 16:45:03 +000051 TT_LineComment,
Daniel Jasperc13ee342014-03-27 09:43:54 +000052 TT_ObjCBlockLBrace,
Manuel Klimek68b03042014-04-14 09:14:11 +000053 TT_ObjCBlockLParen,
Alexander Kornienko4b672072013-06-03 16:45:03 +000054 TT_ObjCDecl,
Alexander Kornienko4b672072013-06-03 16:45:03 +000055 TT_ObjCForIn,
56 TT_ObjCMethodExpr,
57 TT_ObjCMethodSpecifier,
58 TT_ObjCProperty,
Alexander Kornienko4b672072013-06-03 16:45:03 +000059 TT_OverloadedOperator,
60 TT_OverloadedOperatorLParen,
61 TT_PointerOrReference,
62 TT_PureVirtualSpecifier,
63 TT_RangeBasedForLoopColon,
Daniel Jasperf9ae3122014-05-08 07:01:45 +000064 TT_RegexLiteral,
Daniel Jasper17062ff2014-06-10 14:44:02 +000065 TT_SelectorName,
Alexander Kornienko4b672072013-06-03 16:45:03 +000066 TT_StartOfName,
67 TT_TemplateCloser,
68 TT_TemplateOpener,
Daniel Jasper43e6a282013-12-16 15:01:54 +000069 TT_TrailingAnnotation,
Daniel Jasper6cdec7c2013-07-09 14:36:48 +000070 TT_TrailingReturnArrow,
Alexander Kornienko4b672072013-06-03 16:45:03 +000071 TT_TrailingUnaryOperator,
72 TT_UnaryOperator,
73 TT_Unknown
74};
75
Daniel Jasperb1f74a82013-07-09 09:06:29 +000076// Represents what type of block a set of braces open.
77enum BraceBlockKind {
78 BK_Unknown,
79 BK_Block,
80 BK_BracedInit
81};
82
Daniel Jasperb10cbc42013-07-10 14:02:49 +000083// The packing kind of a function's parameters.
84enum ParameterPackingKind {
85 PPK_BinPacked,
86 PPK_OnePerLine,
87 PPK_Inconclusive
88};
89
Manuel Klimek71814b42013-10-11 21:25:45 +000090enum FormatDecision {
91 FD_Unformatted,
92 FD_Continue,
93 FD_Break
94};
95
Daniel Jasper8de9ed02013-08-22 15:00:41 +000096class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000097class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +000098
Alexander Kornienko4b672072013-06-03 16:45:03 +000099/// \brief A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000100/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000101struct FormatToken {
102 FormatToken()
Alexander Kornienko632abb92013-09-02 13:58:14 +0000103 : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
Alexander Kornienko39856b72013-09-10 09:38:25 +0000104 ColumnWidth(0), LastLineColumnWidth(0), IsMultiline(false),
Alexander Kornienko632abb92013-09-02 13:58:14 +0000105 IsFirst(false), MustBreakBefore(false), IsUnterminatedLiteral(false),
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000106 BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
107 CanBreakBefore(false), ClosesTemplateDeclaration(false),
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000108 ParameterCount(0), BlockParameterCount(0),
109 PackingKind(PPK_Inconclusive), TotalLength(0), UnbreakableTailLength(0),
110 BindingStrength(0), NestingLevel(0), SplitPenalty(0),
111 LongestObjCSelectorName(0), FakeRParens(0),
Daniel Jasper562ecd42013-09-06 08:08:14 +0000112 StartsBinaryExpression(false), EndsBinaryExpression(false),
Daniel Jasper0e617842014-04-16 12:26:54 +0000113 OperatorIndex(0), LastOperator(false),
114 PartOfMultiVariableDeclStmt(false), IsForEachMacro(false),
Craig Topper2145bc02014-05-09 08:15:10 +0000115 MatchingParen(nullptr), Previous(nullptr), Next(nullptr),
Manuel Klimek71814b42013-10-11 21:25:45 +0000116 Decision(FD_Unformatted), Finalized(false) {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000117
118 /// \brief The \c Token.
119 Token Tok;
120
121 /// \brief The number of newlines immediately before the \c Token.
122 ///
123 /// This can be used to determine what the user wrote in the original code
124 /// and thereby e.g. leave an empty line between two function definitions.
125 unsigned NewlinesBefore;
126
127 /// \brief Whether there is at least one unescaped newline before the \c
128 /// Token.
129 bool HasUnescapedNewline;
130
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000131 /// \brief The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000132 SourceRange WhitespaceRange;
133
134 /// \brief The offset just past the last '\n' in this token's leading
135 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
136 unsigned LastNewlineOffset;
137
Alexander Kornienko39856b72013-09-10 09:38:25 +0000138 /// \brief The width of the non-whitespace parts of the token (or its first
139 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000140 /// We need this to correctly measure number of columns a token spans.
Alexander Kornienko39856b72013-09-10 09:38:25 +0000141 unsigned ColumnWidth;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000142
Alexander Kornienko39856b72013-09-10 09:38:25 +0000143 /// \brief Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000144 /// token.
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000145 unsigned LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000146
Alexander Kornienko39856b72013-09-10 09:38:25 +0000147 /// \brief Whether the token text contains newlines (escaped or not).
148 bool IsMultiline;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000149
Alexander Kornienko4b672072013-06-03 16:45:03 +0000150 /// \brief Indicates that this is the first token.
151 bool IsFirst;
152
153 /// \brief Whether there must be a line break before this token.
154 ///
155 /// This happens for example when a preprocessor directive ended directly
156 /// before the token.
157 bool MustBreakBefore;
158
159 /// \brief Returns actual token start location without leading escaped
160 /// newlines and whitespace.
161 ///
162 /// This can be different to Tok.getLocation(), which includes leading escaped
163 /// newlines.
164 SourceLocation getStartOfNonWhitespace() const {
165 return WhitespaceRange.getEnd();
166 }
167
168 /// \brief The raw text of the token.
169 ///
170 /// Contains the raw token text without leading whitespace and without leading
171 /// escaped newlines.
172 StringRef TokenText;
173
Daniel Jasper8369aa52013-07-16 20:28:33 +0000174 /// \brief Set to \c true if this token is an unterminated literal.
175 bool IsUnterminatedLiteral;
176
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000177 /// \brief Contains the kind of block if this token is a brace.
178 BraceBlockKind BlockKind;
179
Alexander Kornienko4b672072013-06-03 16:45:03 +0000180 TokenType Type;
181
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000182 /// \brief The number of spaces that should be inserted before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000183 unsigned SpacesRequiredBefore;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000184
185 /// \brief \c true if it is allowed to break before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000186 bool CanBreakBefore;
187
188 bool ClosesTemplateDeclaration;
189
190 /// \brief Number of parameters, if this is "(", "[" or "<".
191 ///
192 /// This is initialized to 1 as we don't need to distinguish functions with
193 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
194 /// the number of commas.
195 unsigned ParameterCount;
196
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000197 /// \brief Number of parameters that are nested blocks,
198 /// if this is "(", "[" or "<".
199 unsigned BlockParameterCount;
200
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000201 /// \brief A token can have a special role that can carry extra information
202 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000203 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000204
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000205 /// \brief If this is an opening parenthesis, how are the parameters packed?
206 ParameterPackingKind PackingKind;
207
Manuel Klimek31c85922013-08-29 15:21:40 +0000208 /// \brief The total length of the unwrapped line up to and including this
209 /// token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000210 unsigned TotalLength;
211
Alexander Kornienko39856b72013-09-10 09:38:25 +0000212 /// \brief The original 0-based column of this token, including expanded tabs.
213 /// The configured TabWidth is used as tab width.
Manuel Klimek31c85922013-08-29 15:21:40 +0000214 unsigned OriginalColumn;
215
Alexander Kornienko4b672072013-06-03 16:45:03 +0000216 /// \brief The length of following tokens until the next natural split point,
217 /// or the next token that can be broken.
218 unsigned UnbreakableTailLength;
219
220 // FIXME: Come up with a 'cleaner' concept.
221 /// \brief The binding strength of a token. This is a combined value of
222 /// operator precedence, parenthesis nesting, etc.
223 unsigned BindingStrength;
224
Daniel Jasper63af7c42013-12-09 14:40:19 +0000225 /// \brief The nesting level of this token, i.e. the number of surrounding (),
226 /// [], {} or <>.
227 unsigned NestingLevel;
228
Alexander Kornienko4b672072013-06-03 16:45:03 +0000229 /// \brief Penalty for inserting a line break before this token.
230 unsigned SplitPenalty;
231
232 /// \brief If this is the first ObjC selector name in an ObjC method
233 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000234 ///
235 /// This being set to 0 means that the selectors should not be colon-aligned,
236 /// e.g. because several of them are block-type.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000237 unsigned LongestObjCSelectorName;
238
239 /// \brief Stores the number of required fake parentheses and the
240 /// corresponding operator precedence.
241 ///
242 /// If multiple fake parentheses start at a token, this vector stores them in
243 /// reverse order, i.e. inner fake parenthesis first.
244 SmallVector<prec::Level, 4> FakeLParens;
245 /// \brief Insert this many fake ) after this token for correct indentation.
246 unsigned FakeRParens;
247
Daniel Jasper562ecd42013-09-06 08:08:14 +0000248 /// \brief \c true if this token starts a binary expression, i.e. has at least
249 /// one fake l_paren with a precedence greater than prec::Unknown.
250 bool StartsBinaryExpression;
251 /// \brief \c true if this token ends a binary expression.
252 bool EndsBinaryExpression;
253
Daniel Jasper0e617842014-04-16 12:26:54 +0000254 /// \brief Is this is an operator (or "."/"->") in a sequence of operators
255 /// with the same precedence, contains the 0-based operator index.
256 unsigned OperatorIndex;
257
258 /// \brief Is this the last operator (or "."/"->") in a sequence of operators
259 /// with the same precedence?
260 bool LastOperator;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000261
262 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
263 ///
264 /// Only set if \c Type == \c TT_StartOfName.
265 bool PartOfMultiVariableDeclStmt;
266
Daniel Jaspere1e43192014-04-01 12:55:11 +0000267 /// \brief Is this a foreach macro?
268 bool IsForEachMacro;
269
Alexander Kornienko4b672072013-06-03 16:45:03 +0000270 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
271
272 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
273 return is(K1) || is(K2);
274 }
275
276 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const {
277 return is(K1) || is(K2) || is(K3);
278 }
279
280 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3,
281 tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS,
282 tok::TokenKind K6 = tok::NUM_TOKENS,
283 tok::TokenKind K7 = tok::NUM_TOKENS,
284 tok::TokenKind K8 = tok::NUM_TOKENS,
285 tok::TokenKind K9 = tok::NUM_TOKENS,
286 tok::TokenKind K10 = tok::NUM_TOKENS,
287 tok::TokenKind K11 = tok::NUM_TOKENS,
288 tok::TokenKind K12 = tok::NUM_TOKENS) const {
289 return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) ||
290 is(K8) || is(K9) || is(K10) || is(K11) || is(K12);
291 }
292
293 bool isNot(tok::TokenKind Kind) const { return Tok.isNot(Kind); }
Daniel Jasper04b6a082013-12-20 06:22:01 +0000294 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000295
296 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
297 return Tok.isObjCAtKeyword(Kind);
298 }
299
300 bool isAccessSpecifier(bool ColonRequired = true) const {
301 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
302 (!ColonRequired || (Next && Next->is(tok::colon)));
303 }
304
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000305 /// \brief Determine whether the token is a simple-type-specifier.
306 bool isSimpleTypeSpecifier() const;
307
Alexander Kornienko4b672072013-06-03 16:45:03 +0000308 bool isObjCAccessSpecifier() const {
309 return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
310 Next->isObjCAtKeyword(tok::objc_protected) ||
311 Next->isObjCAtKeyword(tok::objc_package) ||
312 Next->isObjCAtKeyword(tok::objc_private));
313 }
314
315 /// \brief Returns whether \p Tok is ([{ or a template opening <.
316 bool opensScope() const {
317 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square) ||
318 Type == TT_TemplateOpener;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000319 }
Nico Weber0f987a62013-06-25 19:25:12 +0000320 /// \brief Returns whether \p Tok is )]} or a template closing >.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000321 bool closesScope() const {
322 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square) ||
323 Type == TT_TemplateCloser;
324 }
325
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000326 /// \brief Returns \c true if this is a "." or "->" accessing a member.
327 bool isMemberAccess() const {
Daniel Jasper85bcadc2014-07-09 13:07:57 +0000328 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
Daniel Jasperda07a722014-10-17 14:37:40 +0000329 Type != TT_DesignatedInitializerPeriod &&
330 Type != TT_TrailingReturnArrow;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000331 }
332
Alexander Kornienko4b672072013-06-03 16:45:03 +0000333 bool isUnaryOperator() const {
334 switch (Tok.getKind()) {
335 case tok::plus:
336 case tok::plusplus:
337 case tok::minus:
338 case tok::minusminus:
339 case tok::exclaim:
340 case tok::tilde:
341 case tok::kw_sizeof:
342 case tok::kw_alignof:
343 return true;
344 default:
345 return false;
346 }
347 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000348
Alexander Kornienko4b672072013-06-03 16:45:03 +0000349 bool isBinaryOperator() const {
350 // Comma is a binary operator, but does not behave as such wrt. formatting.
351 return getPrecedence() > prec::Comma;
352 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000353
Alexander Kornienko4b672072013-06-03 16:45:03 +0000354 bool isTrailingComment() const {
Daniel Jasper610381f2014-08-26 09:37:52 +0000355 return is(tok::comment) &&
356 (Type == TT_LineComment || !Next || Next->NewlinesBefore > 0);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000357 }
358
Daniel Jasper78b194992014-08-06 14:15:41 +0000359 /// \brief Returns \c true if this is a keyword that can be used
360 /// like a function call (e.g. sizeof, typeid, ...).
361 bool isFunctionLikeKeyword() const {
362 switch (Tok.getKind()) {
363 case tok::kw_throw:
364 case tok::kw_typeid:
365 case tok::kw_return:
366 case tok::kw_sizeof:
367 case tok::kw_alignof:
368 case tok::kw_alignas:
369 case tok::kw_decltype:
370 case tok::kw_noexcept:
371 case tok::kw_static_assert:
372 case tok::kw___attribute:
373 return true;
374 default:
375 return false;
376 }
377 }
378
Alexander Kornienko4b672072013-06-03 16:45:03 +0000379 prec::Level getPrecedence() const {
380 return getBinOpPrecedence(Tok.getKind(), true, true);
381 }
382
383 /// \brief Returns the previous token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000384 FormatToken *getPreviousNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000385 FormatToken *Tok = Previous;
Craig Topper2145bc02014-05-09 08:15:10 +0000386 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000387 Tok = Tok->Previous;
388 return Tok;
389 }
390
391 /// \brief Returns the next token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000392 const FormatToken *getNextNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000393 const FormatToken *Tok = Next;
Craig Topper2145bc02014-05-09 08:15:10 +0000394 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000395 Tok = Tok->Next;
396 return Tok;
397 }
398
Daniel Jasperb8f61682013-10-22 15:45:58 +0000399 /// \brief Returns \c true if this tokens starts a block-type list, i.e. a
400 /// list that should be indented with a block indent.
401 bool opensBlockTypeList(const FormatStyle &Style) const {
402 return Type == TT_ArrayInitializerLSquare ||
403 (is(tok::l_brace) &&
Daniel Jasperb596fb22013-10-24 10:31:50 +0000404 (BlockKind == BK_Block || Type == TT_DictLiteral ||
Daniel Jasperb8f61682013-10-22 15:45:58 +0000405 !Style.Cpp11BracedListStyle));
406 }
407
408 /// \brief Same as opensBlockTypeList, but for the closing token.
Daniel Jasper1db6c382013-10-22 15:30:28 +0000409 bool closesBlockTypeList(const FormatStyle &Style) const {
Daniel Jasperb8f61682013-10-22 15:45:58 +0000410 return MatchingParen && MatchingParen->opensBlockTypeList(Style);
Daniel Jasper1db6c382013-10-22 15:30:28 +0000411 }
412
Alexander Kornienko4b672072013-06-03 16:45:03 +0000413 FormatToken *MatchingParen;
414
415 FormatToken *Previous;
416 FormatToken *Next;
417
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000418 SmallVector<AnnotatedLine *, 1> Children;
419
Manuel Klimek71814b42013-10-11 21:25:45 +0000420 /// \brief Stores the formatting decision for the token once it was made.
421 FormatDecision Decision;
422
423 /// \brief If \c true, this token has been fully formatted (indented and
424 /// potentially re-formatted inside), and we do not allow further formatting
425 /// changes.
426 bool Finalized;
427
Alexander Kornienko4b672072013-06-03 16:45:03 +0000428private:
429 // Disallow copying.
Craig Topper411294d2013-07-01 04:07:34 +0000430 FormatToken(const FormatToken &) LLVM_DELETED_FUNCTION;
431 void operator=(const FormatToken &) LLVM_DELETED_FUNCTION;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000432};
433
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000434class ContinuationIndenter;
435struct LineState;
436
437class TokenRole {
438public:
439 TokenRole(const FormatStyle &Style) : Style(Style) {}
440 virtual ~TokenRole();
441
442 /// \brief After the \c TokenAnnotator has finished annotating all the tokens,
443 /// this function precomputes required information for formatting.
444 virtual void precomputeFormattingInfos(const FormatToken *Token);
445
446 /// \brief Apply the special formatting that the given role demands.
447 ///
Daniel Jasper01603472014-01-09 13:42:56 +0000448 /// Assumes that the token having this role is already formatted.
449 ///
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000450 /// Continues formatting from \p State leaving indentation to \p Indenter and
451 /// returns the total penalty that this formatting incurs.
Daniel Jasper01603472014-01-09 13:42:56 +0000452 virtual unsigned formatFromToken(LineState &State,
453 ContinuationIndenter *Indenter,
454 bool DryRun) {
455 return 0;
456 }
457
458 /// \brief Same as \c formatFromToken, but assumes that the first token has
459 /// already been set thereby deciding on the first line break.
460 virtual unsigned formatAfterToken(LineState &State,
461 ContinuationIndenter *Indenter,
462 bool DryRun) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000463 return 0;
464 }
465
466 /// \brief Notifies the \c Role that a comma was found.
467 virtual void CommaFound(const FormatToken *Token) {}
468
469protected:
470 const FormatStyle &Style;
471};
472
473class CommaSeparatedList : public TokenRole {
474public:
Daniel Jasper01603472014-01-09 13:42:56 +0000475 CommaSeparatedList(const FormatStyle &Style)
476 : TokenRole(Style), HasNestedBracedList(false) {}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000477
Craig Topperfb6b25b2014-03-15 04:29:04 +0000478 void precomputeFormattingInfos(const FormatToken *Token) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000479
Craig Topperfb6b25b2014-03-15 04:29:04 +0000480 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
481 bool DryRun) override;
Daniel Jasper01603472014-01-09 13:42:56 +0000482
Craig Topperfb6b25b2014-03-15 04:29:04 +0000483 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
484 bool DryRun) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000485
486 /// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000487 void CommaFound(const FormatToken *Token) override {
488 Commas.push_back(Token);
489 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000490
491private:
492 /// \brief A struct that holds information on how to format a given list with
493 /// a specific number of columns.
494 struct ColumnFormat {
495 /// \brief The number of columns to use.
496 unsigned Columns;
497
498 /// \brief The total width in characters.
499 unsigned TotalWidth;
500
501 /// \brief The number of lines required for this format.
502 unsigned LineCount;
503
504 /// \brief The size of each column in characters.
505 SmallVector<unsigned, 8> ColumnSizes;
506 };
507
508 /// \brief Calculate which \c ColumnFormat fits best into
509 /// \p RemainingCharacters.
510 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
511
512 /// \brief The ordered \c FormatTokens making up the commas of this list.
513 SmallVector<const FormatToken *, 8> Commas;
514
515 /// \brief The length of each of the list's items in characters including the
516 /// trailing comma.
517 SmallVector<unsigned, 8> ItemLengths;
518
519 /// \brief Precomputed formats that can be used for this list.
520 SmallVector<ColumnFormat, 4> Formats;
Daniel Jasper01603472014-01-09 13:42:56 +0000521
522 bool HasNestedBracedList;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000523};
524
Alexander Kornienko4b672072013-06-03 16:45:03 +0000525} // namespace format
526} // namespace clang
527
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000528#endif