blob: 0ef563b73a7fdc6058248f40fbd04ebdf0fac410 [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 Jasper9fe0e8d2013-09-05 09:29:45 +000051 TT_LambdaLSquare,
Daniel Jaspere9ab42d2014-10-31 18:23:49 +000052 TT_LeadingJavaAnnotation,
Alexander Kornienko4b672072013-06-03 16:45:03 +000053 TT_LineComment,
Daniel Jasperc13ee342014-03-27 09:43:54 +000054 TT_ObjCBlockLBrace,
Manuel Klimek68b03042014-04-14 09:14:11 +000055 TT_ObjCBlockLParen,
Alexander Kornienko4b672072013-06-03 16:45:03 +000056 TT_ObjCDecl,
Alexander Kornienko4b672072013-06-03 16:45:03 +000057 TT_ObjCForIn,
58 TT_ObjCMethodExpr,
59 TT_ObjCMethodSpecifier,
60 TT_ObjCProperty,
Alexander Kornienko4b672072013-06-03 16:45:03 +000061 TT_OverloadedOperator,
62 TT_OverloadedOperatorLParen,
63 TT_PointerOrReference,
64 TT_PureVirtualSpecifier,
65 TT_RangeBasedForLoopColon,
Daniel Jasperf9ae3122014-05-08 07:01:45 +000066 TT_RegexLiteral,
Daniel Jasper17062ff2014-06-10 14:44:02 +000067 TT_SelectorName,
Alexander Kornienko4b672072013-06-03 16:45:03 +000068 TT_StartOfName,
69 TT_TemplateCloser,
70 TT_TemplateOpener,
Daniel Jasper43e6a282013-12-16 15:01:54 +000071 TT_TrailingAnnotation,
Daniel Jasper6cdec7c2013-07-09 14:36:48 +000072 TT_TrailingReturnArrow,
Alexander Kornienko4b672072013-06-03 16:45:03 +000073 TT_TrailingUnaryOperator,
74 TT_UnaryOperator,
75 TT_Unknown
76};
77
Daniel Jasperb1f74a82013-07-09 09:06:29 +000078// Represents what type of block a set of braces open.
79enum BraceBlockKind {
80 BK_Unknown,
81 BK_Block,
82 BK_BracedInit
83};
84
Daniel Jasperb10cbc42013-07-10 14:02:49 +000085// The packing kind of a function's parameters.
86enum ParameterPackingKind {
87 PPK_BinPacked,
88 PPK_OnePerLine,
89 PPK_Inconclusive
90};
91
Manuel Klimek71814b42013-10-11 21:25:45 +000092enum FormatDecision {
93 FD_Unformatted,
94 FD_Continue,
95 FD_Break
96};
97
Daniel Jasper8de9ed02013-08-22 15:00:41 +000098class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000099class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000100
Alexander Kornienko4b672072013-06-03 16:45:03 +0000101/// \brief A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000102/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000103struct FormatToken {
104 FormatToken()
Alexander Kornienko632abb92013-09-02 13:58:14 +0000105 : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
Alexander Kornienko39856b72013-09-10 09:38:25 +0000106 ColumnWidth(0), LastLineColumnWidth(0), IsMultiline(false),
Alexander Kornienko632abb92013-09-02 13:58:14 +0000107 IsFirst(false), MustBreakBefore(false), IsUnterminatedLiteral(false),
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000108 BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
109 CanBreakBefore(false), ClosesTemplateDeclaration(false),
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000110 ParameterCount(0), BlockParameterCount(0),
111 PackingKind(PPK_Inconclusive), TotalLength(0), UnbreakableTailLength(0),
112 BindingStrength(0), NestingLevel(0), SplitPenalty(0),
113 LongestObjCSelectorName(0), FakeRParens(0),
Daniel Jasper562ecd42013-09-06 08:08:14 +0000114 StartsBinaryExpression(false), EndsBinaryExpression(false),
Daniel Jasper0e617842014-04-16 12:26:54 +0000115 OperatorIndex(0), LastOperator(false),
116 PartOfMultiVariableDeclStmt(false), IsForEachMacro(false),
Craig Topper2145bc02014-05-09 08:15:10 +0000117 MatchingParen(nullptr), Previous(nullptr), Next(nullptr),
Manuel Klimek71814b42013-10-11 21:25:45 +0000118 Decision(FD_Unformatted), Finalized(false) {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000119
120 /// \brief The \c Token.
121 Token Tok;
122
123 /// \brief The number of newlines immediately before the \c Token.
124 ///
125 /// This can be used to determine what the user wrote in the original code
126 /// and thereby e.g. leave an empty line between two function definitions.
127 unsigned NewlinesBefore;
128
129 /// \brief Whether there is at least one unescaped newline before the \c
130 /// Token.
131 bool HasUnescapedNewline;
132
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000133 /// \brief The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000134 SourceRange WhitespaceRange;
135
136 /// \brief The offset just past the last '\n' in this token's leading
137 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
138 unsigned LastNewlineOffset;
139
Alexander Kornienko39856b72013-09-10 09:38:25 +0000140 /// \brief The width of the non-whitespace parts of the token (or its first
141 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000142 /// We need this to correctly measure number of columns a token spans.
Alexander Kornienko39856b72013-09-10 09:38:25 +0000143 unsigned ColumnWidth;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000144
Alexander Kornienko39856b72013-09-10 09:38:25 +0000145 /// \brief Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000146 /// token.
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000147 unsigned LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000148
Alexander Kornienko39856b72013-09-10 09:38:25 +0000149 /// \brief Whether the token text contains newlines (escaped or not).
150 bool IsMultiline;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000151
Alexander Kornienko4b672072013-06-03 16:45:03 +0000152 /// \brief Indicates that this is the first token.
153 bool IsFirst;
154
155 /// \brief Whether there must be a line break before this token.
156 ///
157 /// This happens for example when a preprocessor directive ended directly
158 /// before the token.
159 bool MustBreakBefore;
160
161 /// \brief Returns actual token start location without leading escaped
162 /// newlines and whitespace.
163 ///
164 /// This can be different to Tok.getLocation(), which includes leading escaped
165 /// newlines.
166 SourceLocation getStartOfNonWhitespace() const {
167 return WhitespaceRange.getEnd();
168 }
169
170 /// \brief The raw text of the token.
171 ///
172 /// Contains the raw token text without leading whitespace and without leading
173 /// escaped newlines.
174 StringRef TokenText;
175
Daniel Jasper8369aa52013-07-16 20:28:33 +0000176 /// \brief Set to \c true if this token is an unterminated literal.
177 bool IsUnterminatedLiteral;
178
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000179 /// \brief Contains the kind of block if this token is a brace.
180 BraceBlockKind BlockKind;
181
Alexander Kornienko4b672072013-06-03 16:45:03 +0000182 TokenType Type;
183
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000184 /// \brief The number of spaces that should be inserted before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000185 unsigned SpacesRequiredBefore;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000186
187 /// \brief \c true if it is allowed to break before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000188 bool CanBreakBefore;
189
190 bool ClosesTemplateDeclaration;
191
192 /// \brief Number of parameters, if this is "(", "[" or "<".
193 ///
194 /// This is initialized to 1 as we don't need to distinguish functions with
195 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
196 /// the number of commas.
197 unsigned ParameterCount;
198
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000199 /// \brief Number of parameters that are nested blocks,
200 /// if this is "(", "[" or "<".
201 unsigned BlockParameterCount;
202
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000203 /// \brief A token can have a special role that can carry extra information
204 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000205 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000206
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000207 /// \brief If this is an opening parenthesis, how are the parameters packed?
208 ParameterPackingKind PackingKind;
209
Manuel Klimek31c85922013-08-29 15:21:40 +0000210 /// \brief The total length of the unwrapped line up to and including this
211 /// token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000212 unsigned TotalLength;
213
Alexander Kornienko39856b72013-09-10 09:38:25 +0000214 /// \brief The original 0-based column of this token, including expanded tabs.
215 /// The configured TabWidth is used as tab width.
Manuel Klimek31c85922013-08-29 15:21:40 +0000216 unsigned OriginalColumn;
217
Alexander Kornienko4b672072013-06-03 16:45:03 +0000218 /// \brief The length of following tokens until the next natural split point,
219 /// or the next token that can be broken.
220 unsigned UnbreakableTailLength;
221
222 // FIXME: Come up with a 'cleaner' concept.
223 /// \brief The binding strength of a token. This is a combined value of
224 /// operator precedence, parenthesis nesting, etc.
225 unsigned BindingStrength;
226
Daniel Jasper63af7c42013-12-09 14:40:19 +0000227 /// \brief The nesting level of this token, i.e. the number of surrounding (),
228 /// [], {} or <>.
229 unsigned NestingLevel;
230
Alexander Kornienko4b672072013-06-03 16:45:03 +0000231 /// \brief Penalty for inserting a line break before this token.
232 unsigned SplitPenalty;
233
234 /// \brief If this is the first ObjC selector name in an ObjC method
235 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000236 ///
237 /// This being set to 0 means that the selectors should not be colon-aligned,
238 /// e.g. because several of them are block-type.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000239 unsigned LongestObjCSelectorName;
240
241 /// \brief Stores the number of required fake parentheses and the
242 /// corresponding operator precedence.
243 ///
244 /// If multiple fake parentheses start at a token, this vector stores them in
245 /// reverse order, i.e. inner fake parenthesis first.
246 SmallVector<prec::Level, 4> FakeLParens;
247 /// \brief Insert this many fake ) after this token for correct indentation.
248 unsigned FakeRParens;
249
Daniel Jasper562ecd42013-09-06 08:08:14 +0000250 /// \brief \c true if this token starts a binary expression, i.e. has at least
251 /// one fake l_paren with a precedence greater than prec::Unknown.
252 bool StartsBinaryExpression;
253 /// \brief \c true if this token ends a binary expression.
254 bool EndsBinaryExpression;
255
Daniel Jasper0e617842014-04-16 12:26:54 +0000256 /// \brief Is this is an operator (or "."/"->") in a sequence of operators
257 /// with the same precedence, contains the 0-based operator index.
258 unsigned OperatorIndex;
259
260 /// \brief Is this the last operator (or "."/"->") in a sequence of operators
261 /// with the same precedence?
262 bool LastOperator;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000263
264 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
265 ///
266 /// Only set if \c Type == \c TT_StartOfName.
267 bool PartOfMultiVariableDeclStmt;
268
Daniel Jaspere1e43192014-04-01 12:55:11 +0000269 /// \brief Is this a foreach macro?
270 bool IsForEachMacro;
271
Alexander Kornienko4b672072013-06-03 16:45:03 +0000272 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
273
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000274 bool is(const IdentifierInfo *II) const {
275 return II && II == Tok.getIdentifierInfo();
276 }
277
278 template <typename T>
279 bool isOneOf(T K1, T K2) const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000280 return is(K1) || is(K2);
281 }
282
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000283 template <typename T>
284 bool isOneOf(T K1, T K2, T K3) const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000285 return is(K1) || is(K2) || is(K3);
286 }
287
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000288 template <typename T>
289 bool isOneOf(T K1, T K2, T K3, T K4, T K5 = tok::NUM_TOKENS,
290 T K6 = tok::NUM_TOKENS, T K7 = tok::NUM_TOKENS,
291 T K8 = tok::NUM_TOKENS, T K9 = tok::NUM_TOKENS,
292 T K10 = tok::NUM_TOKENS, T K11 = tok::NUM_TOKENS,
293 T K12 = tok::NUM_TOKENS) const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000294 return is(K1) || is(K2) || is(K3) || is(K4) || is(K5) || is(K6) || is(K7) ||
295 is(K8) || is(K9) || is(K10) || is(K11) || is(K12);
296 }
297
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000298 template <typename T>
299 bool isNot(T Kind) const {
300 return Tok.isNot(Kind);
301 }
Nico Webera644d7f2014-11-10 16:30:02 +0000302 bool isNot(IdentifierInfo *II) const { return II != Tok.getIdentifierInfo(); }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000303
Daniel Jasper04b6a082013-12-20 06:22:01 +0000304 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000305
306 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
307 return Tok.isObjCAtKeyword(Kind);
308 }
309
310 bool isAccessSpecifier(bool ColonRequired = true) const {
311 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
312 (!ColonRequired || (Next && Next->is(tok::colon)));
313 }
314
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000315 /// \brief Determine whether the token is a simple-type-specifier.
316 bool isSimpleTypeSpecifier() const;
317
Alexander Kornienko4b672072013-06-03 16:45:03 +0000318 bool isObjCAccessSpecifier() const {
319 return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
320 Next->isObjCAtKeyword(tok::objc_protected) ||
321 Next->isObjCAtKeyword(tok::objc_package) ||
322 Next->isObjCAtKeyword(tok::objc_private));
323 }
324
325 /// \brief Returns whether \p Tok is ([{ or a template opening <.
326 bool opensScope() const {
327 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square) ||
328 Type == TT_TemplateOpener;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000329 }
Nico Weber0f987a62013-06-25 19:25:12 +0000330 /// \brief Returns whether \p Tok is )]} or a template closing >.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000331 bool closesScope() const {
332 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square) ||
333 Type == TT_TemplateCloser;
334 }
335
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000336 /// \brief Returns \c true if this is a "." or "->" accessing a member.
337 bool isMemberAccess() const {
Daniel Jasper85bcadc2014-07-09 13:07:57 +0000338 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
Daniel Jasperda07a722014-10-17 14:37:40 +0000339 Type != TT_DesignatedInitializerPeriod &&
340 Type != TT_TrailingReturnArrow;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000341 }
342
Alexander Kornienko4b672072013-06-03 16:45:03 +0000343 bool isUnaryOperator() const {
344 switch (Tok.getKind()) {
345 case tok::plus:
346 case tok::plusplus:
347 case tok::minus:
348 case tok::minusminus:
349 case tok::exclaim:
350 case tok::tilde:
351 case tok::kw_sizeof:
352 case tok::kw_alignof:
353 return true;
354 default:
355 return false;
356 }
357 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000358
Alexander Kornienko4b672072013-06-03 16:45:03 +0000359 bool isBinaryOperator() const {
360 // Comma is a binary operator, but does not behave as such wrt. formatting.
361 return getPrecedence() > prec::Comma;
362 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000363
Alexander Kornienko4b672072013-06-03 16:45:03 +0000364 bool isTrailingComment() const {
Daniel Jasper610381f2014-08-26 09:37:52 +0000365 return is(tok::comment) &&
366 (Type == TT_LineComment || !Next || Next->NewlinesBefore > 0);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000367 }
368
Daniel Jasper78b194992014-08-06 14:15:41 +0000369 /// \brief Returns \c true if this is a keyword that can be used
370 /// like a function call (e.g. sizeof, typeid, ...).
371 bool isFunctionLikeKeyword() const {
372 switch (Tok.getKind()) {
373 case tok::kw_throw:
374 case tok::kw_typeid:
375 case tok::kw_return:
376 case tok::kw_sizeof:
377 case tok::kw_alignof:
378 case tok::kw_alignas:
379 case tok::kw_decltype:
380 case tok::kw_noexcept:
381 case tok::kw_static_assert:
382 case tok::kw___attribute:
383 return true;
384 default:
385 return false;
386 }
387 }
388
Alexander Kornienko4b672072013-06-03 16:45:03 +0000389 prec::Level getPrecedence() const {
390 return getBinOpPrecedence(Tok.getKind(), true, true);
391 }
392
393 /// \brief Returns the previous token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000394 FormatToken *getPreviousNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000395 FormatToken *Tok = Previous;
Craig Topper2145bc02014-05-09 08:15:10 +0000396 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000397 Tok = Tok->Previous;
398 return Tok;
399 }
400
401 /// \brief Returns the next token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000402 const FormatToken *getNextNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000403 const FormatToken *Tok = Next;
Craig Topper2145bc02014-05-09 08:15:10 +0000404 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000405 Tok = Tok->Next;
406 return Tok;
407 }
408
Daniel Jasperb8f61682013-10-22 15:45:58 +0000409 /// \brief Returns \c true if this tokens starts a block-type list, i.e. a
410 /// list that should be indented with a block indent.
411 bool opensBlockTypeList(const FormatStyle &Style) const {
412 return Type == TT_ArrayInitializerLSquare ||
413 (is(tok::l_brace) &&
Daniel Jasperb596fb22013-10-24 10:31:50 +0000414 (BlockKind == BK_Block || Type == TT_DictLiteral ||
Daniel Jasperb8f61682013-10-22 15:45:58 +0000415 !Style.Cpp11BracedListStyle));
416 }
417
418 /// \brief Same as opensBlockTypeList, but for the closing token.
Daniel Jasper1db6c382013-10-22 15:30:28 +0000419 bool closesBlockTypeList(const FormatStyle &Style) const {
Daniel Jasperb8f61682013-10-22 15:45:58 +0000420 return MatchingParen && MatchingParen->opensBlockTypeList(Style);
Daniel Jasper1db6c382013-10-22 15:30:28 +0000421 }
422
Alexander Kornienko4b672072013-06-03 16:45:03 +0000423 FormatToken *MatchingParen;
424
425 FormatToken *Previous;
426 FormatToken *Next;
427
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000428 SmallVector<AnnotatedLine *, 1> Children;
429
Manuel Klimek71814b42013-10-11 21:25:45 +0000430 /// \brief Stores the formatting decision for the token once it was made.
431 FormatDecision Decision;
432
433 /// \brief If \c true, this token has been fully formatted (indented and
434 /// potentially re-formatted inside), and we do not allow further formatting
435 /// changes.
436 bool Finalized;
437
Alexander Kornienko4b672072013-06-03 16:45:03 +0000438private:
439 // Disallow copying.
Craig Topper411294d2013-07-01 04:07:34 +0000440 FormatToken(const FormatToken &) LLVM_DELETED_FUNCTION;
441 void operator=(const FormatToken &) LLVM_DELETED_FUNCTION;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000442};
443
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000444class ContinuationIndenter;
445struct LineState;
446
447class TokenRole {
448public:
449 TokenRole(const FormatStyle &Style) : Style(Style) {}
450 virtual ~TokenRole();
451
452 /// \brief After the \c TokenAnnotator has finished annotating all the tokens,
453 /// this function precomputes required information for formatting.
454 virtual void precomputeFormattingInfos(const FormatToken *Token);
455
456 /// \brief Apply the special formatting that the given role demands.
457 ///
Daniel Jasper01603472014-01-09 13:42:56 +0000458 /// Assumes that the token having this role is already formatted.
459 ///
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000460 /// Continues formatting from \p State leaving indentation to \p Indenter and
461 /// returns the total penalty that this formatting incurs.
Daniel Jasper01603472014-01-09 13:42:56 +0000462 virtual unsigned formatFromToken(LineState &State,
463 ContinuationIndenter *Indenter,
464 bool DryRun) {
465 return 0;
466 }
467
468 /// \brief Same as \c formatFromToken, but assumes that the first token has
469 /// already been set thereby deciding on the first line break.
470 virtual unsigned formatAfterToken(LineState &State,
471 ContinuationIndenter *Indenter,
472 bool DryRun) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000473 return 0;
474 }
475
476 /// \brief Notifies the \c Role that a comma was found.
477 virtual void CommaFound(const FormatToken *Token) {}
478
479protected:
480 const FormatStyle &Style;
481};
482
483class CommaSeparatedList : public TokenRole {
484public:
Daniel Jasper01603472014-01-09 13:42:56 +0000485 CommaSeparatedList(const FormatStyle &Style)
486 : TokenRole(Style), HasNestedBracedList(false) {}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000487
Craig Topperfb6b25b2014-03-15 04:29:04 +0000488 void precomputeFormattingInfos(const FormatToken *Token) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000489
Craig Topperfb6b25b2014-03-15 04:29:04 +0000490 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
491 bool DryRun) override;
Daniel Jasper01603472014-01-09 13:42:56 +0000492
Craig Topperfb6b25b2014-03-15 04:29:04 +0000493 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
494 bool DryRun) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000495
496 /// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000497 void CommaFound(const FormatToken *Token) override {
498 Commas.push_back(Token);
499 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000500
501private:
502 /// \brief A struct that holds information on how to format a given list with
503 /// a specific number of columns.
504 struct ColumnFormat {
505 /// \brief The number of columns to use.
506 unsigned Columns;
507
508 /// \brief The total width in characters.
509 unsigned TotalWidth;
510
511 /// \brief The number of lines required for this format.
512 unsigned LineCount;
513
514 /// \brief The size of each column in characters.
515 SmallVector<unsigned, 8> ColumnSizes;
516 };
517
518 /// \brief Calculate which \c ColumnFormat fits best into
519 /// \p RemainingCharacters.
520 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
521
522 /// \brief The ordered \c FormatTokens making up the commas of this list.
523 SmallVector<const FormatToken *, 8> Commas;
524
525 /// \brief The length of each of the list's items in characters including the
526 /// trailing comma.
527 SmallVector<unsigned, 8> ItemLengths;
528
529 /// \brief Precomputed formats that can be used for this list.
530 SmallVector<ColumnFormat, 4> Formats;
Daniel Jasper01603472014-01-09 13:42:56 +0000531
532 bool HasNestedBracedList;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000533};
534
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000535/// \brief Encapsulates keywords that are context sensitive or for languages not
536/// properly supported by Clang's lexer.
537struct AdditionalKeywords {
538 AdditionalKeywords(IdentifierTable &IdentTable) {
539 kw_in = &IdentTable.get("in");
540 kw_NS_ENUM = &IdentTable.get("NS_ENUM");
541
542 kw_finally = &IdentTable.get("finally");
543 kw_function = &IdentTable.get("function");
544 kw_var = &IdentTable.get("var");
545
546 kw_extends = &IdentTable.get("extends");
547 kw_implements = &IdentTable.get("implements");
Nico Webera644d7f2014-11-10 16:30:02 +0000548 kw_interface = &IdentTable.get("interface");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000549 kw_synchronized = &IdentTable.get("synchronized");
550 kw_throws = &IdentTable.get("throws");
551
552 kw_option = &IdentTable.get("option");
553 kw_optional = &IdentTable.get("optional");
554 kw_repeated = &IdentTable.get("repeated");
555 kw_required = &IdentTable.get("required");
556 kw_returns = &IdentTable.get("returns");
557 }
558
559 // ObjC context sensitive keywords.
560 IdentifierInfo *kw_in;
561 IdentifierInfo *kw_NS_ENUM;
562
563 // JavaScript keywords.
564 IdentifierInfo *kw_finally;
565 IdentifierInfo *kw_function;
566 IdentifierInfo *kw_var;
567
568 // Java keywords.
569 IdentifierInfo *kw_extends;
570 IdentifierInfo *kw_implements;
Nico Webera644d7f2014-11-10 16:30:02 +0000571 IdentifierInfo *kw_interface;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000572 IdentifierInfo *kw_synchronized;
573 IdentifierInfo *kw_throws;
574
575 // Proto keywords.
576 IdentifierInfo *kw_option;
577 IdentifierInfo *kw_optional;
578 IdentifierInfo *kw_repeated;
579 IdentifierInfo *kw_required;
580 IdentifierInfo *kw_returns;
581};
582
Alexander Kornienko4b672072013-06-03 16:45:03 +0000583} // namespace format
584} // namespace clang
585
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000586#endif