blob: aca529809c3beb6e02d927bfca8cf006710dc184 [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,
Daniel Jaspere9ab42d2014-10-31 18:23:49 +000051 TT_LeadingJavaAnnotation,
Alexander Kornienko4b672072013-06-03 16:45:03 +000052 TT_LineComment,
Daniel Jasperc13ee342014-03-27 09:43:54 +000053 TT_ObjCBlockLBrace,
Manuel Klimek68b03042014-04-14 09:14:11 +000054 TT_ObjCBlockLParen,
Alexander Kornienko4b672072013-06-03 16:45:03 +000055 TT_ObjCDecl,
Alexander Kornienko4b672072013-06-03 16:45:03 +000056 TT_ObjCForIn,
57 TT_ObjCMethodExpr,
58 TT_ObjCMethodSpecifier,
59 TT_ObjCProperty,
Alexander Kornienko4b672072013-06-03 16:45:03 +000060 TT_OverloadedOperator,
61 TT_OverloadedOperatorLParen,
62 TT_PointerOrReference,
63 TT_PureVirtualSpecifier,
64 TT_RangeBasedForLoopColon,
Daniel Jasperf9ae3122014-05-08 07:01:45 +000065 TT_RegexLiteral,
Daniel Jasper17062ff2014-06-10 14:44:02 +000066 TT_SelectorName,
Alexander Kornienko4b672072013-06-03 16:45:03 +000067 TT_StartOfName,
68 TT_TemplateCloser,
69 TT_TemplateOpener,
Daniel Jasper43e6a282013-12-16 15:01:54 +000070 TT_TrailingAnnotation,
Daniel Jasper6cdec7c2013-07-09 14:36:48 +000071 TT_TrailingReturnArrow,
Alexander Kornienko4b672072013-06-03 16:45:03 +000072 TT_TrailingUnaryOperator,
73 TT_UnaryOperator,
74 TT_Unknown
75};
76
Daniel Jasperb1f74a82013-07-09 09:06:29 +000077// Represents what type of block a set of braces open.
78enum BraceBlockKind {
79 BK_Unknown,
80 BK_Block,
81 BK_BracedInit
82};
83
Daniel Jasperb10cbc42013-07-10 14:02:49 +000084// The packing kind of a function's parameters.
85enum ParameterPackingKind {
86 PPK_BinPacked,
87 PPK_OnePerLine,
88 PPK_Inconclusive
89};
90
Manuel Klimek71814b42013-10-11 21:25:45 +000091enum FormatDecision {
92 FD_Unformatted,
93 FD_Continue,
94 FD_Break
95};
96
Daniel Jasper8de9ed02013-08-22 15:00:41 +000097class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000098class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +000099
Alexander Kornienko4b672072013-06-03 16:45:03 +0000100/// \brief A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000101/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000102struct FormatToken {
103 FormatToken()
Alexander Kornienko632abb92013-09-02 13:58:14 +0000104 : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
Alexander Kornienko39856b72013-09-10 09:38:25 +0000105 ColumnWidth(0), LastLineColumnWidth(0), IsMultiline(false),
Alexander Kornienko632abb92013-09-02 13:58:14 +0000106 IsFirst(false), MustBreakBefore(false), IsUnterminatedLiteral(false),
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000107 BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
108 CanBreakBefore(false), ClosesTemplateDeclaration(false),
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000109 ParameterCount(0), BlockParameterCount(0),
110 PackingKind(PPK_Inconclusive), TotalLength(0), UnbreakableTailLength(0),
111 BindingStrength(0), NestingLevel(0), SplitPenalty(0),
112 LongestObjCSelectorName(0), FakeRParens(0),
Daniel Jasper562ecd42013-09-06 08:08:14 +0000113 StartsBinaryExpression(false), EndsBinaryExpression(false),
Daniel Jasper0e617842014-04-16 12:26:54 +0000114 OperatorIndex(0), LastOperator(false),
115 PartOfMultiVariableDeclStmt(false), IsForEachMacro(false),
Craig Topper2145bc02014-05-09 08:15:10 +0000116 MatchingParen(nullptr), Previous(nullptr), Next(nullptr),
Manuel Klimek71814b42013-10-11 21:25:45 +0000117 Decision(FD_Unformatted), Finalized(false) {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000118
119 /// \brief The \c Token.
120 Token Tok;
121
122 /// \brief The number of newlines immediately before the \c Token.
123 ///
124 /// This can be used to determine what the user wrote in the original code
125 /// and thereby e.g. leave an empty line between two function definitions.
126 unsigned NewlinesBefore;
127
128 /// \brief Whether there is at least one unescaped newline before the \c
129 /// Token.
130 bool HasUnescapedNewline;
131
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000132 /// \brief The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000133 SourceRange WhitespaceRange;
134
135 /// \brief The offset just past the last '\n' in this token's leading
136 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
137 unsigned LastNewlineOffset;
138
Alexander Kornienko39856b72013-09-10 09:38:25 +0000139 /// \brief The width of the non-whitespace parts of the token (or its first
140 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000141 /// We need this to correctly measure number of columns a token spans.
Alexander Kornienko39856b72013-09-10 09:38:25 +0000142 unsigned ColumnWidth;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000143
Alexander Kornienko39856b72013-09-10 09:38:25 +0000144 /// \brief Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000145 /// token.
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000146 unsigned LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000147
Alexander Kornienko39856b72013-09-10 09:38:25 +0000148 /// \brief Whether the token text contains newlines (escaped or not).
149 bool IsMultiline;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000150
Alexander Kornienko4b672072013-06-03 16:45:03 +0000151 /// \brief Indicates that this is the first token.
152 bool IsFirst;
153
154 /// \brief Whether there must be a line break before this token.
155 ///
156 /// This happens for example when a preprocessor directive ended directly
157 /// before the token.
158 bool MustBreakBefore;
159
160 /// \brief Returns actual token start location without leading escaped
161 /// newlines and whitespace.
162 ///
163 /// This can be different to Tok.getLocation(), which includes leading escaped
164 /// newlines.
165 SourceLocation getStartOfNonWhitespace() const {
166 return WhitespaceRange.getEnd();
167 }
168
169 /// \brief The raw text of the token.
170 ///
171 /// Contains the raw token text without leading whitespace and without leading
172 /// escaped newlines.
173 StringRef TokenText;
174
Daniel Jasper8369aa52013-07-16 20:28:33 +0000175 /// \brief Set to \c true if this token is an unterminated literal.
176 bool IsUnterminatedLiteral;
177
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000178 /// \brief Contains the kind of block if this token is a brace.
179 BraceBlockKind BlockKind;
180
Alexander Kornienko4b672072013-06-03 16:45:03 +0000181 TokenType Type;
182
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000183 /// \brief The number of spaces that should be inserted before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000184 unsigned SpacesRequiredBefore;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000185
186 /// \brief \c true if it is allowed to break before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000187 bool CanBreakBefore;
188
189 bool ClosesTemplateDeclaration;
190
191 /// \brief Number of parameters, if this is "(", "[" or "<".
192 ///
193 /// This is initialized to 1 as we don't need to distinguish functions with
194 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
195 /// the number of commas.
196 unsigned ParameterCount;
197
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000198 /// \brief Number of parameters that are nested blocks,
199 /// if this is "(", "[" or "<".
200 unsigned BlockParameterCount;
201
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000202 /// \brief A token can have a special role that can carry extra information
203 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000204 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000205
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000206 /// \brief If this is an opening parenthesis, how are the parameters packed?
207 ParameterPackingKind PackingKind;
208
Manuel Klimek31c85922013-08-29 15:21:40 +0000209 /// \brief The total length of the unwrapped line up to and including this
210 /// token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000211 unsigned TotalLength;
212
Alexander Kornienko39856b72013-09-10 09:38:25 +0000213 /// \brief The original 0-based column of this token, including expanded tabs.
214 /// The configured TabWidth is used as tab width.
Manuel Klimek31c85922013-08-29 15:21:40 +0000215 unsigned OriginalColumn;
216
Alexander Kornienko4b672072013-06-03 16:45:03 +0000217 /// \brief The length of following tokens until the next natural split point,
218 /// or the next token that can be broken.
219 unsigned UnbreakableTailLength;
220
221 // FIXME: Come up with a 'cleaner' concept.
222 /// \brief The binding strength of a token. This is a combined value of
223 /// operator precedence, parenthesis nesting, etc.
224 unsigned BindingStrength;
225
Daniel Jasper63af7c42013-12-09 14:40:19 +0000226 /// \brief The nesting level of this token, i.e. the number of surrounding (),
227 /// [], {} or <>.
228 unsigned NestingLevel;
229
Alexander Kornienko4b672072013-06-03 16:45:03 +0000230 /// \brief Penalty for inserting a line break before this token.
231 unsigned SplitPenalty;
232
233 /// \brief If this is the first ObjC selector name in an ObjC method
234 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000235 ///
236 /// This being set to 0 means that the selectors should not be colon-aligned,
237 /// e.g. because several of them are block-type.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000238 unsigned LongestObjCSelectorName;
239
240 /// \brief Stores the number of required fake parentheses and the
241 /// corresponding operator precedence.
242 ///
243 /// If multiple fake parentheses start at a token, this vector stores them in
244 /// reverse order, i.e. inner fake parenthesis first.
245 SmallVector<prec::Level, 4> FakeLParens;
246 /// \brief Insert this many fake ) after this token for correct indentation.
247 unsigned FakeRParens;
248
Daniel Jasper562ecd42013-09-06 08:08:14 +0000249 /// \brief \c true if this token starts a binary expression, i.e. has at least
250 /// one fake l_paren with a precedence greater than prec::Unknown.
251 bool StartsBinaryExpression;
252 /// \brief \c true if this token ends a binary expression.
253 bool EndsBinaryExpression;
254
Daniel Jasper0e617842014-04-16 12:26:54 +0000255 /// \brief Is this is an operator (or "."/"->") in a sequence of operators
256 /// with the same precedence, contains the 0-based operator index.
257 unsigned OperatorIndex;
258
259 /// \brief Is this the last operator (or "."/"->") in a sequence of operators
260 /// with the same precedence?
261 bool LastOperator;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000262
263 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
264 ///
265 /// Only set if \c Type == \c TT_StartOfName.
266 bool PartOfMultiVariableDeclStmt;
267
Daniel Jaspere1e43192014-04-01 12:55:11 +0000268 /// \brief Is this a foreach macro?
269 bool IsForEachMacro;
270
Alexander Kornienko4b672072013-06-03 16:45:03 +0000271 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
272
273 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const {
274 return is(K1) || is(K2);
275 }
276
277 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3) const {
278 return is(K1) || is(K2) || is(K3);
279 }
280
281 bool isOneOf(tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind K3,
282 tok::TokenKind K4, tok::TokenKind K5 = tok::NUM_TOKENS,
283 tok::TokenKind K6 = tok::NUM_TOKENS,
284 tok::TokenKind K7 = tok::NUM_TOKENS,
285 tok::TokenKind K8 = tok::NUM_TOKENS,
286 tok::TokenKind K9 = tok::NUM_TOKENS,
287 tok::TokenKind K10 = tok::NUM_TOKENS,
288 tok::TokenKind K11 = tok::NUM_TOKENS,
289 tok::TokenKind K12 = tok::NUM_TOKENS) const {
290 return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) ||
291 is(K8) || is(K9) || is(K10) || is(K11) || is(K12);
292 }
293
294 bool isNot(tok::TokenKind Kind) const { return Tok.isNot(Kind); }
Daniel Jasper04b6a082013-12-20 06:22:01 +0000295 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000296
297 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
298 return Tok.isObjCAtKeyword(Kind);
299 }
300
301 bool isAccessSpecifier(bool ColonRequired = true) const {
302 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
303 (!ColonRequired || (Next && Next->is(tok::colon)));
304 }
305
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000306 /// \brief Determine whether the token is a simple-type-specifier.
307 bool isSimpleTypeSpecifier() const;
308
Alexander Kornienko4b672072013-06-03 16:45:03 +0000309 bool isObjCAccessSpecifier() const {
310 return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
311 Next->isObjCAtKeyword(tok::objc_protected) ||
312 Next->isObjCAtKeyword(tok::objc_package) ||
313 Next->isObjCAtKeyword(tok::objc_private));
314 }
315
316 /// \brief Returns whether \p Tok is ([{ or a template opening <.
317 bool opensScope() const {
318 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square) ||
319 Type == TT_TemplateOpener;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000320 }
Nico Weber0f987a62013-06-25 19:25:12 +0000321 /// \brief Returns whether \p Tok is )]} or a template closing >.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000322 bool closesScope() const {
323 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square) ||
324 Type == TT_TemplateCloser;
325 }
326
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000327 /// \brief Returns \c true if this is a "." or "->" accessing a member.
328 bool isMemberAccess() const {
Daniel Jasper85bcadc2014-07-09 13:07:57 +0000329 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
Daniel Jasperda07a722014-10-17 14:37:40 +0000330 Type != TT_DesignatedInitializerPeriod &&
331 Type != TT_TrailingReturnArrow;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000332 }
333
Alexander Kornienko4b672072013-06-03 16:45:03 +0000334 bool isUnaryOperator() const {
335 switch (Tok.getKind()) {
336 case tok::plus:
337 case tok::plusplus:
338 case tok::minus:
339 case tok::minusminus:
340 case tok::exclaim:
341 case tok::tilde:
342 case tok::kw_sizeof:
343 case tok::kw_alignof:
344 return true;
345 default:
346 return false;
347 }
348 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000349
Alexander Kornienko4b672072013-06-03 16:45:03 +0000350 bool isBinaryOperator() const {
351 // Comma is a binary operator, but does not behave as such wrt. formatting.
352 return getPrecedence() > prec::Comma;
353 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000354
Alexander Kornienko4b672072013-06-03 16:45:03 +0000355 bool isTrailingComment() const {
Daniel Jasper610381f2014-08-26 09:37:52 +0000356 return is(tok::comment) &&
357 (Type == TT_LineComment || !Next || Next->NewlinesBefore > 0);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000358 }
359
Daniel Jasper78b194992014-08-06 14:15:41 +0000360 /// \brief Returns \c true if this is a keyword that can be used
361 /// like a function call (e.g. sizeof, typeid, ...).
362 bool isFunctionLikeKeyword() const {
363 switch (Tok.getKind()) {
364 case tok::kw_throw:
365 case tok::kw_typeid:
366 case tok::kw_return:
367 case tok::kw_sizeof:
368 case tok::kw_alignof:
369 case tok::kw_alignas:
370 case tok::kw_decltype:
371 case tok::kw_noexcept:
372 case tok::kw_static_assert:
373 case tok::kw___attribute:
374 return true;
375 default:
376 return false;
377 }
378 }
379
Alexander Kornienko4b672072013-06-03 16:45:03 +0000380 prec::Level getPrecedence() const {
381 return getBinOpPrecedence(Tok.getKind(), true, true);
382 }
383
384 /// \brief Returns the previous token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000385 FormatToken *getPreviousNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000386 FormatToken *Tok = Previous;
Craig Topper2145bc02014-05-09 08:15:10 +0000387 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000388 Tok = Tok->Previous;
389 return Tok;
390 }
391
392 /// \brief Returns the next token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000393 const FormatToken *getNextNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000394 const FormatToken *Tok = Next;
Craig Topper2145bc02014-05-09 08:15:10 +0000395 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000396 Tok = Tok->Next;
397 return Tok;
398 }
399
Daniel Jasperb8f61682013-10-22 15:45:58 +0000400 /// \brief Returns \c true if this tokens starts a block-type list, i.e. a
401 /// list that should be indented with a block indent.
402 bool opensBlockTypeList(const FormatStyle &Style) const {
403 return Type == TT_ArrayInitializerLSquare ||
404 (is(tok::l_brace) &&
Daniel Jasperb596fb22013-10-24 10:31:50 +0000405 (BlockKind == BK_Block || Type == TT_DictLiteral ||
Daniel Jasperb8f61682013-10-22 15:45:58 +0000406 !Style.Cpp11BracedListStyle));
407 }
408
409 /// \brief Same as opensBlockTypeList, but for the closing token.
Daniel Jasper1db6c382013-10-22 15:30:28 +0000410 bool closesBlockTypeList(const FormatStyle &Style) const {
Daniel Jasperb8f61682013-10-22 15:45:58 +0000411 return MatchingParen && MatchingParen->opensBlockTypeList(Style);
Daniel Jasper1db6c382013-10-22 15:30:28 +0000412 }
413
Alexander Kornienko4b672072013-06-03 16:45:03 +0000414 FormatToken *MatchingParen;
415
416 FormatToken *Previous;
417 FormatToken *Next;
418
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000419 SmallVector<AnnotatedLine *, 1> Children;
420
Manuel Klimek71814b42013-10-11 21:25:45 +0000421 /// \brief Stores the formatting decision for the token once it was made.
422 FormatDecision Decision;
423
424 /// \brief If \c true, this token has been fully formatted (indented and
425 /// potentially re-formatted inside), and we do not allow further formatting
426 /// changes.
427 bool Finalized;
428
Alexander Kornienko4b672072013-06-03 16:45:03 +0000429private:
430 // Disallow copying.
Craig Topper411294d2013-07-01 04:07:34 +0000431 FormatToken(const FormatToken &) LLVM_DELETED_FUNCTION;
432 void operator=(const FormatToken &) LLVM_DELETED_FUNCTION;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000433};
434
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000435class ContinuationIndenter;
436struct LineState;
437
438class TokenRole {
439public:
440 TokenRole(const FormatStyle &Style) : Style(Style) {}
441 virtual ~TokenRole();
442
443 /// \brief After the \c TokenAnnotator has finished annotating all the tokens,
444 /// this function precomputes required information for formatting.
445 virtual void precomputeFormattingInfos(const FormatToken *Token);
446
447 /// \brief Apply the special formatting that the given role demands.
448 ///
Daniel Jasper01603472014-01-09 13:42:56 +0000449 /// Assumes that the token having this role is already formatted.
450 ///
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000451 /// Continues formatting from \p State leaving indentation to \p Indenter and
452 /// returns the total penalty that this formatting incurs.
Daniel Jasper01603472014-01-09 13:42:56 +0000453 virtual unsigned formatFromToken(LineState &State,
454 ContinuationIndenter *Indenter,
455 bool DryRun) {
456 return 0;
457 }
458
459 /// \brief Same as \c formatFromToken, but assumes that the first token has
460 /// already been set thereby deciding on the first line break.
461 virtual unsigned formatAfterToken(LineState &State,
462 ContinuationIndenter *Indenter,
463 bool DryRun) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000464 return 0;
465 }
466
467 /// \brief Notifies the \c Role that a comma was found.
468 virtual void CommaFound(const FormatToken *Token) {}
469
470protected:
471 const FormatStyle &Style;
472};
473
474class CommaSeparatedList : public TokenRole {
475public:
Daniel Jasper01603472014-01-09 13:42:56 +0000476 CommaSeparatedList(const FormatStyle &Style)
477 : TokenRole(Style), HasNestedBracedList(false) {}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000478
Craig Topperfb6b25b2014-03-15 04:29:04 +0000479 void precomputeFormattingInfos(const FormatToken *Token) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000480
Craig Topperfb6b25b2014-03-15 04:29:04 +0000481 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
482 bool DryRun) override;
Daniel Jasper01603472014-01-09 13:42:56 +0000483
Craig Topperfb6b25b2014-03-15 04:29:04 +0000484 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
485 bool DryRun) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000486
487 /// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000488 void CommaFound(const FormatToken *Token) override {
489 Commas.push_back(Token);
490 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000491
492private:
493 /// \brief A struct that holds information on how to format a given list with
494 /// a specific number of columns.
495 struct ColumnFormat {
496 /// \brief The number of columns to use.
497 unsigned Columns;
498
499 /// \brief The total width in characters.
500 unsigned TotalWidth;
501
502 /// \brief The number of lines required for this format.
503 unsigned LineCount;
504
505 /// \brief The size of each column in characters.
506 SmallVector<unsigned, 8> ColumnSizes;
507 };
508
509 /// \brief Calculate which \c ColumnFormat fits best into
510 /// \p RemainingCharacters.
511 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
512
513 /// \brief The ordered \c FormatTokens making up the commas of this list.
514 SmallVector<const FormatToken *, 8> Commas;
515
516 /// \brief The length of each of the list's items in characters including the
517 /// trailing comma.
518 SmallVector<unsigned, 8> ItemLengths;
519
520 /// \brief Precomputed formats that can be used for this list.
521 SmallVector<ColumnFormat, 4> Formats;
Daniel Jasper01603472014-01-09 13:42:56 +0000522
523 bool HasNestedBracedList;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000524};
525
Alexander Kornienko4b672072013-06-03 16:45:03 +0000526} // namespace format
527} // namespace clang
528
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000529#endif