blob: 053a3b050e4bd2932df9887faed48a1f8a5eb896 [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
Daniel Jasperd0ec0d62014-11-04 12:41:02 +000019#include "clang/Basic/IdentifierTable.h"
Alexander Kornienko4b672072013-06-03 16:45:03 +000020#include "clang/Basic/OperatorPrecedence.h"
Daniel Jasper8de9ed02013-08-22 15:00:41 +000021#include "clang/Format/Format.h"
Alexander Kornienko4b672072013-06-03 16:45:03 +000022#include "clang/Lex/Lexer.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000023#include <memory>
Alexander Kornienko4b672072013-06-03 16:45:03 +000024
25namespace clang {
26namespace format {
27
28enum TokenType {
Daniel Jasper1db6c382013-10-22 15:30:28 +000029 TT_ArrayInitializerLSquare,
30 TT_ArraySubscriptLSquare,
Daniel Jasper559b63c2014-01-28 20:13:43 +000031 TT_AttributeParen,
Alexander Kornienko4b672072013-06-03 16:45:03 +000032 TT_BinaryOperator,
Alexander Kornienko60d1b042013-10-10 13:36:20 +000033 TT_BitFieldColon,
Alexander Kornienko4b672072013-06-03 16:45:03 +000034 TT_BlockComment,
35 TT_CastRParen,
36 TT_ConditionalExpr,
Manuel Klimek68b03042014-04-14 09:14:11 +000037 TT_ConflictAlternative,
38 TT_ConflictEnd,
39 TT_ConflictStart,
Alexander Kornienko4b672072013-06-03 16:45:03 +000040 TT_CtorInitializerColon,
Daniel Jaspere33d4af2013-07-26 16:56:36 +000041 TT_CtorInitializerComma,
Alexander Kornienko4b672072013-06-03 16:45:03 +000042 TT_DesignatedInitializerPeriod,
Daniel Jasperb596fb22013-10-24 10:31:50 +000043 TT_DictLiteral,
Daniel Jasper4355e7f2014-07-09 07:50:33 +000044 TT_FunctionDeclarationName,
Alexander Kornienko3cfa9732013-11-20 16:33:05 +000045 TT_FunctionLBrace,
Alexander Kornienko4b672072013-06-03 16:45:03 +000046 TT_FunctionTypeLParen,
Manuel Klimek68b03042014-04-14 09:14:11 +000047 TT_ImplicitStringLiteral,
48 TT_InheritanceColon,
49 TT_InlineASMColon,
Daniel Jasperfab69ff2014-10-21 08:24:18 +000050 TT_JavaAnnotation,
Daniel Jasper8354ea82014-11-21 12:14:12 +000051 TT_LambdaArrow,
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000052 TT_LambdaLSquare,
Daniel Jaspere9ab42d2014-10-31 18:23:49 +000053 TT_LeadingJavaAnnotation,
Alexander Kornienko4b672072013-06-03 16:45:03 +000054 TT_LineComment,
Daniel Jasperc13ee342014-03-27 09:43:54 +000055 TT_ObjCBlockLBrace,
Manuel Klimek68b03042014-04-14 09:14:11 +000056 TT_ObjCBlockLParen,
Alexander Kornienko4b672072013-06-03 16:45:03 +000057 TT_ObjCDecl,
Alexander Kornienko4b672072013-06-03 16:45:03 +000058 TT_ObjCForIn,
59 TT_ObjCMethodExpr,
60 TT_ObjCMethodSpecifier,
61 TT_ObjCProperty,
Alexander Kornienko4b672072013-06-03 16:45:03 +000062 TT_OverloadedOperator,
63 TT_OverloadedOperatorLParen,
64 TT_PointerOrReference,
65 TT_PureVirtualSpecifier,
66 TT_RangeBasedForLoopColon,
Daniel Jasperf9ae3122014-05-08 07:01:45 +000067 TT_RegexLiteral,
Daniel Jasper17062ff2014-06-10 14:44:02 +000068 TT_SelectorName,
Alexander Kornienko4b672072013-06-03 16:45:03 +000069 TT_StartOfName,
70 TT_TemplateCloser,
71 TT_TemplateOpener,
Daniel Jasper43e6a282013-12-16 15:01:54 +000072 TT_TrailingAnnotation,
Daniel Jasper6cdec7c2013-07-09 14:36:48 +000073 TT_TrailingReturnArrow,
Alexander Kornienko4b672072013-06-03 16:45:03 +000074 TT_TrailingUnaryOperator,
75 TT_UnaryOperator,
76 TT_Unknown
77};
78
Daniel Jasperb1f74a82013-07-09 09:06:29 +000079// Represents what type of block a set of braces open.
80enum BraceBlockKind {
81 BK_Unknown,
82 BK_Block,
83 BK_BracedInit
84};
85
Daniel Jasperb10cbc42013-07-10 14:02:49 +000086// The packing kind of a function's parameters.
87enum ParameterPackingKind {
88 PPK_BinPacked,
89 PPK_OnePerLine,
90 PPK_Inconclusive
91};
92
Manuel Klimek71814b42013-10-11 21:25:45 +000093enum FormatDecision {
94 FD_Unformatted,
95 FD_Continue,
96 FD_Break
97};
98
Daniel Jasper8de9ed02013-08-22 15:00:41 +000099class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000100class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000101
Alexander Kornienko4b672072013-06-03 16:45:03 +0000102/// \brief A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000103/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000104struct FormatToken {
105 FormatToken()
Alexander Kornienko632abb92013-09-02 13:58:14 +0000106 : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
Alexander Kornienko39856b72013-09-10 09:38:25 +0000107 ColumnWidth(0), LastLineColumnWidth(0), IsMultiline(false),
Alexander Kornienko632abb92013-09-02 13:58:14 +0000108 IsFirst(false), MustBreakBefore(false), IsUnterminatedLiteral(false),
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000109 BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
110 CanBreakBefore(false), ClosesTemplateDeclaration(false),
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000111 ParameterCount(0), BlockParameterCount(0),
112 PackingKind(PPK_Inconclusive), TotalLength(0), UnbreakableTailLength(0),
113 BindingStrength(0), NestingLevel(0), SplitPenalty(0),
114 LongestObjCSelectorName(0), FakeRParens(0),
Daniel Jasper562ecd42013-09-06 08:08:14 +0000115 StartsBinaryExpression(false), EndsBinaryExpression(false),
Daniel Jasper0e617842014-04-16 12:26:54 +0000116 OperatorIndex(0), LastOperator(false),
117 PartOfMultiVariableDeclStmt(false), IsForEachMacro(false),
Craig Topper2145bc02014-05-09 08:15:10 +0000118 MatchingParen(nullptr), Previous(nullptr), Next(nullptr),
Manuel Klimek71814b42013-10-11 21:25:45 +0000119 Decision(FD_Unformatted), Finalized(false) {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000120
121 /// \brief The \c Token.
122 Token Tok;
123
124 /// \brief The number of newlines immediately before the \c Token.
125 ///
126 /// This can be used to determine what the user wrote in the original code
127 /// and thereby e.g. leave an empty line between two function definitions.
128 unsigned NewlinesBefore;
129
130 /// \brief Whether there is at least one unescaped newline before the \c
131 /// Token.
132 bool HasUnescapedNewline;
133
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000134 /// \brief The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000135 SourceRange WhitespaceRange;
136
137 /// \brief The offset just past the last '\n' in this token's leading
138 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
139 unsigned LastNewlineOffset;
140
Alexander Kornienko39856b72013-09-10 09:38:25 +0000141 /// \brief The width of the non-whitespace parts of the token (or its first
142 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000143 /// We need this to correctly measure number of columns a token spans.
Alexander Kornienko39856b72013-09-10 09:38:25 +0000144 unsigned ColumnWidth;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000145
Alexander Kornienko39856b72013-09-10 09:38:25 +0000146 /// \brief Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000147 /// token.
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000148 unsigned LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000149
Alexander Kornienko39856b72013-09-10 09:38:25 +0000150 /// \brief Whether the token text contains newlines (escaped or not).
151 bool IsMultiline;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000152
Alexander Kornienko4b672072013-06-03 16:45:03 +0000153 /// \brief Indicates that this is the first token.
154 bool IsFirst;
155
156 /// \brief Whether there must be a line break before this token.
157 ///
158 /// This happens for example when a preprocessor directive ended directly
159 /// before the token.
160 bool MustBreakBefore;
161
162 /// \brief Returns actual token start location without leading escaped
163 /// newlines and whitespace.
164 ///
165 /// This can be different to Tok.getLocation(), which includes leading escaped
166 /// newlines.
167 SourceLocation getStartOfNonWhitespace() const {
168 return WhitespaceRange.getEnd();
169 }
170
171 /// \brief The raw text of the token.
172 ///
173 /// Contains the raw token text without leading whitespace and without leading
174 /// escaped newlines.
175 StringRef TokenText;
176
Daniel Jasper8369aa52013-07-16 20:28:33 +0000177 /// \brief Set to \c true if this token is an unterminated literal.
178 bool IsUnterminatedLiteral;
179
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000180 /// \brief Contains the kind of block if this token is a brace.
181 BraceBlockKind BlockKind;
182
Alexander Kornienko4b672072013-06-03 16:45:03 +0000183 TokenType Type;
184
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000185 /// \brief The number of spaces that should be inserted before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000186 unsigned SpacesRequiredBefore;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000187
188 /// \brief \c true if it is allowed to break before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000189 bool CanBreakBefore;
190
191 bool ClosesTemplateDeclaration;
192
193 /// \brief Number of parameters, if this is "(", "[" or "<".
194 ///
195 /// This is initialized to 1 as we don't need to distinguish functions with
196 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
197 /// the number of commas.
198 unsigned ParameterCount;
199
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000200 /// \brief Number of parameters that are nested blocks,
201 /// if this is "(", "[" or "<".
202 unsigned BlockParameterCount;
203
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000204 /// \brief A token can have a special role that can carry extra information
205 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000206 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000207
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000208 /// \brief If this is an opening parenthesis, how are the parameters packed?
209 ParameterPackingKind PackingKind;
210
Manuel Klimek31c85922013-08-29 15:21:40 +0000211 /// \brief The total length of the unwrapped line up to and including this
212 /// token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000213 unsigned TotalLength;
214
Alexander Kornienko39856b72013-09-10 09:38:25 +0000215 /// \brief The original 0-based column of this token, including expanded tabs.
216 /// The configured TabWidth is used as tab width.
Manuel Klimek31c85922013-08-29 15:21:40 +0000217 unsigned OriginalColumn;
218
Alexander Kornienko4b672072013-06-03 16:45:03 +0000219 /// \brief The length of following tokens until the next natural split point,
220 /// or the next token that can be broken.
221 unsigned UnbreakableTailLength;
222
223 // FIXME: Come up with a 'cleaner' concept.
224 /// \brief The binding strength of a token. This is a combined value of
225 /// operator precedence, parenthesis nesting, etc.
226 unsigned BindingStrength;
227
Daniel Jasper63af7c42013-12-09 14:40:19 +0000228 /// \brief The nesting level of this token, i.e. the number of surrounding (),
229 /// [], {} or <>.
230 unsigned NestingLevel;
231
Alexander Kornienko4b672072013-06-03 16:45:03 +0000232 /// \brief Penalty for inserting a line break before this token.
233 unsigned SplitPenalty;
234
235 /// \brief If this is the first ObjC selector name in an ObjC method
236 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000237 ///
238 /// This being set to 0 means that the selectors should not be colon-aligned,
239 /// e.g. because several of them are block-type.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000240 unsigned LongestObjCSelectorName;
241
242 /// \brief Stores the number of required fake parentheses and the
243 /// corresponding operator precedence.
244 ///
245 /// If multiple fake parentheses start at a token, this vector stores them in
246 /// reverse order, i.e. inner fake parenthesis first.
247 SmallVector<prec::Level, 4> FakeLParens;
248 /// \brief Insert this many fake ) after this token for correct indentation.
249 unsigned FakeRParens;
250
Daniel Jasper562ecd42013-09-06 08:08:14 +0000251 /// \brief \c true if this token starts a binary expression, i.e. has at least
252 /// one fake l_paren with a precedence greater than prec::Unknown.
253 bool StartsBinaryExpression;
254 /// \brief \c true if this token ends a binary expression.
255 bool EndsBinaryExpression;
256
Daniel Jasper0e617842014-04-16 12:26:54 +0000257 /// \brief Is this is an operator (or "."/"->") in a sequence of operators
258 /// with the same precedence, contains the 0-based operator index.
259 unsigned OperatorIndex;
260
261 /// \brief Is this the last operator (or "."/"->") in a sequence of operators
262 /// with the same precedence?
263 bool LastOperator;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000264
265 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
266 ///
267 /// Only set if \c Type == \c TT_StartOfName.
268 bool PartOfMultiVariableDeclStmt;
269
Daniel Jaspere1e43192014-04-01 12:55:11 +0000270 /// \brief Is this a foreach macro?
271 bool IsForEachMacro;
272
Alexander Kornienko4b672072013-06-03 16:45:03 +0000273 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000274
Daniel Jasper8354ea82014-11-21 12:14:12 +0000275 bool is(TokenType TT) const { return Type == TT; }
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000276
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000277 bool is(const IdentifierInfo *II) const {
278 return II && II == Tok.getIdentifierInfo();
279 }
280
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000281 template <typename T>
282 bool isOneOf(T K1, T K2) const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000283 return is(K1) || is(K2);
284 }
285
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000286 template <typename T>
287 bool isOneOf(T K1, T K2, T K3) const {
288 return is(K1) || is(K2) || is(K3);
289 }
290
291 template <typename T>
292 bool isOneOf(T K1, T K2, T K3, T K4, T K5 = tok::NUM_TOKENS,
293 T K6 = tok::NUM_TOKENS, T K7 = tok::NUM_TOKENS,
294 T K8 = tok::NUM_TOKENS, T K9 = tok::NUM_TOKENS,
295 T K10 = tok::NUM_TOKENS, T K11 = tok::NUM_TOKENS,
296 T K12 = tok::NUM_TOKENS) const {
297 return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) ||
298 is(K8) || is(K9) || is(K10) || is(K11) || is(K12);
299 }
300
301 template <typename T>
302 bool isNot(T Kind) const {
303 return Tok.isNot(Kind);
304 }
305 bool isNot(IdentifierInfo *II) const { return II != Tok.getIdentifierInfo(); }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000306
Daniel Jasper04b6a082013-12-20 06:22:01 +0000307 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000308
309 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
310 return Tok.isObjCAtKeyword(Kind);
311 }
312
313 bool isAccessSpecifier(bool ColonRequired = true) const {
314 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
315 (!ColonRequired || (Next && Next->is(tok::colon)));
316 }
317
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000318 /// \brief Determine whether the token is a simple-type-specifier.
319 bool isSimpleTypeSpecifier() const;
320
Alexander Kornienko4b672072013-06-03 16:45:03 +0000321 bool isObjCAccessSpecifier() const {
322 return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
323 Next->isObjCAtKeyword(tok::objc_protected) ||
324 Next->isObjCAtKeyword(tok::objc_package) ||
325 Next->isObjCAtKeyword(tok::objc_private));
326 }
327
328 /// \brief Returns whether \p Tok is ([{ or a template opening <.
329 bool opensScope() const {
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000330 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square) ||
331 Type == TT_TemplateOpener;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000332 }
Nico Weber0f987a62013-06-25 19:25:12 +0000333 /// \brief Returns whether \p Tok is )]} or a template closing >.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000334 bool closesScope() const {
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000335 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square) ||
336 Type == TT_TemplateCloser;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000337 }
338
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000339 /// \brief Returns \c true if this is a "." or "->" accessing a member.
340 bool isMemberAccess() const {
Daniel Jasper85bcadc2014-07-09 13:07:57 +0000341 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000342 Type != TT_DesignatedInitializerPeriod &&
343 Type != TT_TrailingReturnArrow;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000344 }
345
Alexander Kornienko4b672072013-06-03 16:45:03 +0000346 bool isUnaryOperator() const {
347 switch (Tok.getKind()) {
348 case tok::plus:
349 case tok::plusplus:
350 case tok::minus:
351 case tok::minusminus:
352 case tok::exclaim:
353 case tok::tilde:
354 case tok::kw_sizeof:
355 case tok::kw_alignof:
356 return true;
357 default:
358 return false;
359 }
360 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000361
Alexander Kornienko4b672072013-06-03 16:45:03 +0000362 bool isBinaryOperator() const {
363 // Comma is a binary operator, but does not behave as such wrt. formatting.
364 return getPrecedence() > prec::Comma;
365 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000366
Alexander Kornienko4b672072013-06-03 16:45:03 +0000367 bool isTrailingComment() const {
Daniel Jasper610381f2014-08-26 09:37:52 +0000368 return is(tok::comment) &&
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000369 (Type == TT_LineComment || !Next || Next->NewlinesBefore > 0);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000370 }
371
Daniel Jasper78b194992014-08-06 14:15:41 +0000372 /// \brief Returns \c true if this is a keyword that can be used
373 /// like a function call (e.g. sizeof, typeid, ...).
374 bool isFunctionLikeKeyword() const {
375 switch (Tok.getKind()) {
376 case tok::kw_throw:
377 case tok::kw_typeid:
378 case tok::kw_return:
379 case tok::kw_sizeof:
380 case tok::kw_alignof:
381 case tok::kw_alignas:
382 case tok::kw_decltype:
383 case tok::kw_noexcept:
384 case tok::kw_static_assert:
385 case tok::kw___attribute:
386 return true;
387 default:
388 return false;
389 }
390 }
391
Alexander Kornienko4b672072013-06-03 16:45:03 +0000392 prec::Level getPrecedence() const {
393 return getBinOpPrecedence(Tok.getKind(), true, true);
394 }
395
396 /// \brief Returns the previous token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000397 FormatToken *getPreviousNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000398 FormatToken *Tok = Previous;
Craig Topper2145bc02014-05-09 08:15:10 +0000399 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000400 Tok = Tok->Previous;
401 return Tok;
402 }
403
404 /// \brief Returns the next token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000405 const FormatToken *getNextNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000406 const FormatToken *Tok = Next;
Craig Topper2145bc02014-05-09 08:15:10 +0000407 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000408 Tok = Tok->Next;
409 return Tok;
410 }
411
Daniel Jasperb8f61682013-10-22 15:45:58 +0000412 /// \brief Returns \c true if this tokens starts a block-type list, i.e. a
413 /// list that should be indented with a block indent.
414 bool opensBlockTypeList(const FormatStyle &Style) const {
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000415 return Type == TT_ArrayInitializerLSquare ||
416 (is(tok::l_brace) &&
417 (BlockKind == BK_Block || Type == TT_DictLiteral ||
418 !Style.Cpp11BracedListStyle));
Daniel Jasperb8f61682013-10-22 15:45:58 +0000419 }
420
421 /// \brief Same as opensBlockTypeList, but for the closing token.
Daniel Jasper1db6c382013-10-22 15:30:28 +0000422 bool closesBlockTypeList(const FormatStyle &Style) const {
Daniel Jasperb8f61682013-10-22 15:45:58 +0000423 return MatchingParen && MatchingParen->opensBlockTypeList(Style);
Daniel Jasper1db6c382013-10-22 15:30:28 +0000424 }
425
Alexander Kornienko4b672072013-06-03 16:45:03 +0000426 FormatToken *MatchingParen;
427
428 FormatToken *Previous;
429 FormatToken *Next;
430
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000431 SmallVector<AnnotatedLine *, 1> Children;
432
Manuel Klimek71814b42013-10-11 21:25:45 +0000433 /// \brief Stores the formatting decision for the token once it was made.
434 FormatDecision Decision;
435
436 /// \brief If \c true, this token has been fully formatted (indented and
437 /// potentially re-formatted inside), and we do not allow further formatting
438 /// changes.
439 bool Finalized;
440
Alexander Kornienko4b672072013-06-03 16:45:03 +0000441private:
442 // Disallow copying.
Craig Topper411294d2013-07-01 04:07:34 +0000443 FormatToken(const FormatToken &) LLVM_DELETED_FUNCTION;
444 void operator=(const FormatToken &) LLVM_DELETED_FUNCTION;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000445};
446
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000447class ContinuationIndenter;
448struct LineState;
449
450class TokenRole {
451public:
452 TokenRole(const FormatStyle &Style) : Style(Style) {}
453 virtual ~TokenRole();
454
455 /// \brief After the \c TokenAnnotator has finished annotating all the tokens,
456 /// this function precomputes required information for formatting.
457 virtual void precomputeFormattingInfos(const FormatToken *Token);
458
459 /// \brief Apply the special formatting that the given role demands.
460 ///
Daniel Jasper01603472014-01-09 13:42:56 +0000461 /// Assumes that the token having this role is already formatted.
462 ///
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000463 /// Continues formatting from \p State leaving indentation to \p Indenter and
464 /// returns the total penalty that this formatting incurs.
Daniel Jasper01603472014-01-09 13:42:56 +0000465 virtual unsigned formatFromToken(LineState &State,
466 ContinuationIndenter *Indenter,
467 bool DryRun) {
468 return 0;
469 }
470
471 /// \brief Same as \c formatFromToken, but assumes that the first token has
472 /// already been set thereby deciding on the first line break.
473 virtual unsigned formatAfterToken(LineState &State,
474 ContinuationIndenter *Indenter,
475 bool DryRun) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000476 return 0;
477 }
478
479 /// \brief Notifies the \c Role that a comma was found.
480 virtual void CommaFound(const FormatToken *Token) {}
481
482protected:
483 const FormatStyle &Style;
484};
485
486class CommaSeparatedList : public TokenRole {
487public:
Daniel Jasper01603472014-01-09 13:42:56 +0000488 CommaSeparatedList(const FormatStyle &Style)
489 : TokenRole(Style), HasNestedBracedList(false) {}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000490
Craig Topperfb6b25b2014-03-15 04:29:04 +0000491 void precomputeFormattingInfos(const FormatToken *Token) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000492
Craig Topperfb6b25b2014-03-15 04:29:04 +0000493 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
494 bool DryRun) override;
Daniel Jasper01603472014-01-09 13:42:56 +0000495
Craig Topperfb6b25b2014-03-15 04:29:04 +0000496 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
497 bool DryRun) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000498
499 /// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000500 void CommaFound(const FormatToken *Token) override {
501 Commas.push_back(Token);
502 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000503
504private:
505 /// \brief A struct that holds information on how to format a given list with
506 /// a specific number of columns.
507 struct ColumnFormat {
508 /// \brief The number of columns to use.
509 unsigned Columns;
510
511 /// \brief The total width in characters.
512 unsigned TotalWidth;
513
514 /// \brief The number of lines required for this format.
515 unsigned LineCount;
516
517 /// \brief The size of each column in characters.
518 SmallVector<unsigned, 8> ColumnSizes;
519 };
520
521 /// \brief Calculate which \c ColumnFormat fits best into
522 /// \p RemainingCharacters.
523 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
524
525 /// \brief The ordered \c FormatTokens making up the commas of this list.
526 SmallVector<const FormatToken *, 8> Commas;
527
528 /// \brief The length of each of the list's items in characters including the
529 /// trailing comma.
530 SmallVector<unsigned, 8> ItemLengths;
531
532 /// \brief Precomputed formats that can be used for this list.
533 SmallVector<ColumnFormat, 4> Formats;
Daniel Jasper01603472014-01-09 13:42:56 +0000534
535 bool HasNestedBracedList;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000536};
537
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000538/// \brief Encapsulates keywords that are context sensitive or for languages not
539/// properly supported by Clang's lexer.
540struct AdditionalKeywords {
541 AdditionalKeywords(IdentifierTable &IdentTable) {
542 kw_in = &IdentTable.get("in");
543 kw_NS_ENUM = &IdentTable.get("NS_ENUM");
544
545 kw_finally = &IdentTable.get("finally");
546 kw_function = &IdentTable.get("function");
547 kw_var = &IdentTable.get("var");
548
Daniel Jasper82c92752014-11-21 12:19:07 +0000549 kw_abstract = &IdentTable.get("abstract");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000550 kw_extends = &IdentTable.get("extends");
Daniel Jasper82c92752014-11-21 12:19:07 +0000551 kw_final = &IdentTable.get("final");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000552 kw_implements = &IdentTable.get("implements");
Nico Webera644d7f2014-11-10 16:30:02 +0000553 kw_interface = &IdentTable.get("interface");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000554 kw_synchronized = &IdentTable.get("synchronized");
555 kw_throws = &IdentTable.get("throws");
556
557 kw_option = &IdentTable.get("option");
558 kw_optional = &IdentTable.get("optional");
559 kw_repeated = &IdentTable.get("repeated");
560 kw_required = &IdentTable.get("required");
561 kw_returns = &IdentTable.get("returns");
562 }
563
564 // ObjC context sensitive keywords.
565 IdentifierInfo *kw_in;
566 IdentifierInfo *kw_NS_ENUM;
567
568 // JavaScript keywords.
569 IdentifierInfo *kw_finally;
570 IdentifierInfo *kw_function;
571 IdentifierInfo *kw_var;
572
573 // Java keywords.
Daniel Jasper82c92752014-11-21 12:19:07 +0000574 IdentifierInfo *kw_abstract;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000575 IdentifierInfo *kw_extends;
Daniel Jasper82c92752014-11-21 12:19:07 +0000576 IdentifierInfo *kw_final;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000577 IdentifierInfo *kw_implements;
Nico Webera644d7f2014-11-10 16:30:02 +0000578 IdentifierInfo *kw_interface;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000579 IdentifierInfo *kw_synchronized;
580 IdentifierInfo *kw_throws;
581
582 // Proto keywords.
583 IdentifierInfo *kw_option;
584 IdentifierInfo *kw_optional;
585 IdentifierInfo *kw_repeated;
586 IdentifierInfo *kw_required;
587 IdentifierInfo *kw_returns;
588};
589
Alexander Kornienko4b672072013-06-03 16:45:03 +0000590} // namespace format
591} // namespace clang
592
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000593#endif