blob: 14f45a445541ab615b431096a0dfacec01c61e78 [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 Jasperb10bdff2015-02-18 17:09:53 +000051 TT_JsTypeColon,
Daniel Jasper8354ea82014-11-21 12:14:12 +000052 TT_LambdaArrow,
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000053 TT_LambdaLSquare,
Daniel Jaspere9ab42d2014-10-31 18:23:49 +000054 TT_LeadingJavaAnnotation,
Alexander Kornienko4b672072013-06-03 16:45:03 +000055 TT_LineComment,
Daniel Jasperc13ee342014-03-27 09:43:54 +000056 TT_ObjCBlockLBrace,
Manuel Klimek68b03042014-04-14 09:14:11 +000057 TT_ObjCBlockLParen,
Alexander Kornienko4b672072013-06-03 16:45:03 +000058 TT_ObjCDecl,
Alexander Kornienko4b672072013-06-03 16:45:03 +000059 TT_ObjCForIn,
60 TT_ObjCMethodExpr,
61 TT_ObjCMethodSpecifier,
62 TT_ObjCProperty,
Alexander Kornienko4b672072013-06-03 16:45:03 +000063 TT_OverloadedOperator,
64 TT_OverloadedOperatorLParen,
65 TT_PointerOrReference,
66 TT_PureVirtualSpecifier,
67 TT_RangeBasedForLoopColon,
Daniel Jasperf9ae3122014-05-08 07:01:45 +000068 TT_RegexLiteral,
Daniel Jasper17062ff2014-06-10 14:44:02 +000069 TT_SelectorName,
Alexander Kornienko4b672072013-06-03 16:45:03 +000070 TT_StartOfName,
71 TT_TemplateCloser,
72 TT_TemplateOpener,
Daniel Jaspera0ef4f32015-02-20 13:47:38 +000073 TT_TemplateString,
Daniel Jasper43e6a282013-12-16 15:01:54 +000074 TT_TrailingAnnotation,
Daniel Jasper6cdec7c2013-07-09 14:36:48 +000075 TT_TrailingReturnArrow,
Alexander Kornienko4b672072013-06-03 16:45:03 +000076 TT_TrailingUnaryOperator,
77 TT_UnaryOperator,
78 TT_Unknown
79};
80
Daniel Jasperb1f74a82013-07-09 09:06:29 +000081// Represents what type of block a set of braces open.
82enum BraceBlockKind {
83 BK_Unknown,
84 BK_Block,
85 BK_BracedInit
86};
87
Daniel Jasperb10cbc42013-07-10 14:02:49 +000088// The packing kind of a function's parameters.
89enum ParameterPackingKind {
90 PPK_BinPacked,
91 PPK_OnePerLine,
92 PPK_Inconclusive
93};
94
Manuel Klimek71814b42013-10-11 21:25:45 +000095enum FormatDecision {
96 FD_Unformatted,
97 FD_Continue,
98 FD_Break
99};
100
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000101class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000102class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000103
Alexander Kornienko4b672072013-06-03 16:45:03 +0000104/// \brief A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000105/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000106struct FormatToken {
107 FormatToken()
Alexander Kornienko632abb92013-09-02 13:58:14 +0000108 : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0),
Alexander Kornienko39856b72013-09-10 09:38:25 +0000109 ColumnWidth(0), LastLineColumnWidth(0), IsMultiline(false),
Alexander Kornienko632abb92013-09-02 13:58:14 +0000110 IsFirst(false), MustBreakBefore(false), IsUnterminatedLiteral(false),
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000111 BlockKind(BK_Unknown), Type(TT_Unknown), SpacesRequiredBefore(0),
112 CanBreakBefore(false), ClosesTemplateDeclaration(false),
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000113 ParameterCount(0), BlockParameterCount(0),
114 PackingKind(PPK_Inconclusive), TotalLength(0), UnbreakableTailLength(0),
115 BindingStrength(0), NestingLevel(0), SplitPenalty(0),
116 LongestObjCSelectorName(0), FakeRParens(0),
Daniel Jasper562ecd42013-09-06 08:08:14 +0000117 StartsBinaryExpression(false), EndsBinaryExpression(false),
Daniel Jasper0e617842014-04-16 12:26:54 +0000118 OperatorIndex(0), LastOperator(false),
119 PartOfMultiVariableDeclStmt(false), IsForEachMacro(false),
Craig Topper2145bc02014-05-09 08:15:10 +0000120 MatchingParen(nullptr), Previous(nullptr), Next(nullptr),
Manuel Klimek71814b42013-10-11 21:25:45 +0000121 Decision(FD_Unformatted), Finalized(false) {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000122
123 /// \brief The \c Token.
124 Token Tok;
125
126 /// \brief The number of newlines immediately before the \c Token.
127 ///
128 /// This can be used to determine what the user wrote in the original code
129 /// and thereby e.g. leave an empty line between two function definitions.
130 unsigned NewlinesBefore;
131
132 /// \brief Whether there is at least one unescaped newline before the \c
133 /// Token.
134 bool HasUnescapedNewline;
135
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000136 /// \brief The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000137 SourceRange WhitespaceRange;
138
139 /// \brief The offset just past the last '\n' in this token's leading
140 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
141 unsigned LastNewlineOffset;
142
Alexander Kornienko39856b72013-09-10 09:38:25 +0000143 /// \brief The width of the non-whitespace parts of the token (or its first
144 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000145 /// We need this to correctly measure number of columns a token spans.
Alexander Kornienko39856b72013-09-10 09:38:25 +0000146 unsigned ColumnWidth;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000147
Alexander Kornienko39856b72013-09-10 09:38:25 +0000148 /// \brief Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000149 /// token.
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000150 unsigned LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000151
Alexander Kornienko39856b72013-09-10 09:38:25 +0000152 /// \brief Whether the token text contains newlines (escaped or not).
153 bool IsMultiline;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000154
Alexander Kornienko4b672072013-06-03 16:45:03 +0000155 /// \brief Indicates that this is the first token.
156 bool IsFirst;
157
158 /// \brief Whether there must be a line break before this token.
159 ///
160 /// This happens for example when a preprocessor directive ended directly
161 /// before the token.
162 bool MustBreakBefore;
163
164 /// \brief Returns actual token start location without leading escaped
165 /// newlines and whitespace.
166 ///
167 /// This can be different to Tok.getLocation(), which includes leading escaped
168 /// newlines.
169 SourceLocation getStartOfNonWhitespace() const {
170 return WhitespaceRange.getEnd();
171 }
172
173 /// \brief The raw text of the token.
174 ///
175 /// Contains the raw token text without leading whitespace and without leading
176 /// escaped newlines.
177 StringRef TokenText;
178
Daniel Jasper8369aa52013-07-16 20:28:33 +0000179 /// \brief Set to \c true if this token is an unterminated literal.
180 bool IsUnterminatedLiteral;
181
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000182 /// \brief Contains the kind of block if this token is a brace.
183 BraceBlockKind BlockKind;
184
Alexander Kornienko4b672072013-06-03 16:45:03 +0000185 TokenType Type;
186
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000187 /// \brief The number of spaces that should be inserted before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000188 unsigned SpacesRequiredBefore;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000189
190 /// \brief \c true if it is allowed to break before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000191 bool CanBreakBefore;
192
193 bool ClosesTemplateDeclaration;
194
195 /// \brief Number of parameters, if this is "(", "[" or "<".
196 ///
197 /// This is initialized to 1 as we don't need to distinguish functions with
198 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
199 /// the number of commas.
200 unsigned ParameterCount;
201
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000202 /// \brief Number of parameters that are nested blocks,
203 /// if this is "(", "[" or "<".
204 unsigned BlockParameterCount;
205
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000206 /// \brief A token can have a special role that can carry extra information
207 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000208 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000209
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000210 /// \brief If this is an opening parenthesis, how are the parameters packed?
211 ParameterPackingKind PackingKind;
212
Manuel Klimek31c85922013-08-29 15:21:40 +0000213 /// \brief The total length of the unwrapped line up to and including this
214 /// token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000215 unsigned TotalLength;
216
Alexander Kornienko39856b72013-09-10 09:38:25 +0000217 /// \brief The original 0-based column of this token, including expanded tabs.
218 /// The configured TabWidth is used as tab width.
Manuel Klimek31c85922013-08-29 15:21:40 +0000219 unsigned OriginalColumn;
220
Alexander Kornienko4b672072013-06-03 16:45:03 +0000221 /// \brief The length of following tokens until the next natural split point,
222 /// or the next token that can be broken.
223 unsigned UnbreakableTailLength;
224
225 // FIXME: Come up with a 'cleaner' concept.
226 /// \brief The binding strength of a token. This is a combined value of
227 /// operator precedence, parenthesis nesting, etc.
228 unsigned BindingStrength;
229
Daniel Jasper63af7c42013-12-09 14:40:19 +0000230 /// \brief The nesting level of this token, i.e. the number of surrounding (),
231 /// [], {} or <>.
232 unsigned NestingLevel;
233
Alexander Kornienko4b672072013-06-03 16:45:03 +0000234 /// \brief Penalty for inserting a line break before this token.
235 unsigned SplitPenalty;
236
237 /// \brief If this is the first ObjC selector name in an ObjC method
238 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000239 ///
240 /// This being set to 0 means that the selectors should not be colon-aligned,
241 /// e.g. because several of them are block-type.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000242 unsigned LongestObjCSelectorName;
243
244 /// \brief Stores the number of required fake parentheses and the
245 /// corresponding operator precedence.
246 ///
247 /// If multiple fake parentheses start at a token, this vector stores them in
248 /// reverse order, i.e. inner fake parenthesis first.
249 SmallVector<prec::Level, 4> FakeLParens;
250 /// \brief Insert this many fake ) after this token for correct indentation.
251 unsigned FakeRParens;
252
Daniel Jasper562ecd42013-09-06 08:08:14 +0000253 /// \brief \c true if this token starts a binary expression, i.e. has at least
254 /// one fake l_paren with a precedence greater than prec::Unknown.
255 bool StartsBinaryExpression;
256 /// \brief \c true if this token ends a binary expression.
257 bool EndsBinaryExpression;
258
Daniel Jasper0e617842014-04-16 12:26:54 +0000259 /// \brief Is this is an operator (or "."/"->") in a sequence of operators
260 /// with the same precedence, contains the 0-based operator index.
261 unsigned OperatorIndex;
262
263 /// \brief Is this the last operator (or "."/"->") in a sequence of operators
264 /// with the same precedence?
265 bool LastOperator;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000266
267 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
268 ///
269 /// Only set if \c Type == \c TT_StartOfName.
270 bool PartOfMultiVariableDeclStmt;
271
Daniel Jaspere1e43192014-04-01 12:55:11 +0000272 /// \brief Is this a foreach macro?
273 bool IsForEachMacro;
274
Alexander Kornienko4b672072013-06-03 16:45:03 +0000275 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
Daniel Jasper8354ea82014-11-21 12:14:12 +0000276 bool is(TokenType TT) const { return Type == TT; }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000277 bool is(const IdentifierInfo *II) const {
278 return II && II == Tok.getIdentifierInfo();
279 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000280 template <typename A, typename B> bool isOneOf(A K1, B K2) const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000281 return is(K1) || is(K2);
282 }
Benjamin Kramerc5bc3cd2015-02-15 20:11:14 +0000283 template <typename A, typename B, typename... Ts>
284 bool isOneOf(A K1, B K2, Ts... Ks) const {
285 return is(K1) || isOneOf(K2, Ks...);
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000286 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000287 template <typename T> bool isNot(T Kind) const { return !is(Kind); }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000288
Daniel Jasper04b6a082013-12-20 06:22:01 +0000289 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000290
291 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
292 return Tok.isObjCAtKeyword(Kind);
293 }
294
295 bool isAccessSpecifier(bool ColonRequired = true) const {
296 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
297 (!ColonRequired || (Next && Next->is(tok::colon)));
298 }
299
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000300 /// \brief Determine whether the token is a simple-type-specifier.
301 bool isSimpleTypeSpecifier() const;
302
Alexander Kornienko4b672072013-06-03 16:45:03 +0000303 bool isObjCAccessSpecifier() const {
304 return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
305 Next->isObjCAtKeyword(tok::objc_protected) ||
306 Next->isObjCAtKeyword(tok::objc_package) ||
307 Next->isObjCAtKeyword(tok::objc_private));
308 }
309
310 /// \brief Returns whether \p Tok is ([{ or a template opening <.
311 bool opensScope() const {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000312 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
313 TT_TemplateOpener);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000314 }
Nico Weber0f987a62013-06-25 19:25:12 +0000315 /// \brief Returns whether \p Tok is )]} or a template closing >.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000316 bool closesScope() const {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000317 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
318 TT_TemplateCloser);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000319 }
320
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000321 /// \brief Returns \c true if this is a "." or "->" accessing a member.
322 bool isMemberAccess() const {
Daniel Jasper85bcadc2014-07-09 13:07:57 +0000323 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000324 !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow);
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000325 }
326
Alexander Kornienko4b672072013-06-03 16:45:03 +0000327 bool isUnaryOperator() const {
328 switch (Tok.getKind()) {
329 case tok::plus:
330 case tok::plusplus:
331 case tok::minus:
332 case tok::minusminus:
333 case tok::exclaim:
334 case tok::tilde:
335 case tok::kw_sizeof:
336 case tok::kw_alignof:
337 return true;
338 default:
339 return false;
340 }
341 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000342
Alexander Kornienko4b672072013-06-03 16:45:03 +0000343 bool isBinaryOperator() const {
344 // Comma is a binary operator, but does not behave as such wrt. formatting.
345 return getPrecedence() > prec::Comma;
346 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000347
Alexander Kornienko4b672072013-06-03 16:45:03 +0000348 bool isTrailingComment() const {
Daniel Jasper610381f2014-08-26 09:37:52 +0000349 return is(tok::comment) &&
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000350 (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000351 }
352
Daniel Jasper78b194992014-08-06 14:15:41 +0000353 /// \brief Returns \c true if this is a keyword that can be used
354 /// like a function call (e.g. sizeof, typeid, ...).
355 bool isFunctionLikeKeyword() const {
356 switch (Tok.getKind()) {
357 case tok::kw_throw:
358 case tok::kw_typeid:
359 case tok::kw_return:
360 case tok::kw_sizeof:
361 case tok::kw_alignof:
362 case tok::kw_alignas:
363 case tok::kw_decltype:
364 case tok::kw_noexcept:
365 case tok::kw_static_assert:
366 case tok::kw___attribute:
367 return true;
368 default:
369 return false;
370 }
371 }
372
Alexander Kornienko4b672072013-06-03 16:45:03 +0000373 prec::Level getPrecedence() const {
374 return getBinOpPrecedence(Tok.getKind(), true, true);
375 }
376
377 /// \brief Returns the previous token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000378 FormatToken *getPreviousNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000379 FormatToken *Tok = Previous;
Craig Topper2145bc02014-05-09 08:15:10 +0000380 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000381 Tok = Tok->Previous;
382 return Tok;
383 }
384
385 /// \brief Returns the next token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000386 const FormatToken *getNextNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000387 const FormatToken *Tok = Next;
Craig Topper2145bc02014-05-09 08:15:10 +0000388 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000389 Tok = Tok->Next;
390 return Tok;
391 }
392
Daniel Jasperb8f61682013-10-22 15:45:58 +0000393 /// \brief Returns \c true if this tokens starts a block-type list, i.e. a
394 /// list that should be indented with a block indent.
395 bool opensBlockTypeList(const FormatStyle &Style) const {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000396 return is(TT_ArrayInitializerLSquare) ||
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000397 (is(tok::l_brace) &&
398 (BlockKind == BK_Block || is(TT_DictLiteral) ||
399 (!Style.Cpp11BracedListStyle && NestingLevel == 0)));
Daniel Jasperb8f61682013-10-22 15:45:58 +0000400 }
401
402 /// \brief Same as opensBlockTypeList, but for the closing token.
Daniel Jasper1db6c382013-10-22 15:30:28 +0000403 bool closesBlockTypeList(const FormatStyle &Style) const {
Daniel Jasperb8f61682013-10-22 15:45:58 +0000404 return MatchingParen && MatchingParen->opensBlockTypeList(Style);
Daniel Jasper1db6c382013-10-22 15:30:28 +0000405 }
406
Alexander Kornienko4b672072013-06-03 16:45:03 +0000407 FormatToken *MatchingParen;
408
409 FormatToken *Previous;
410 FormatToken *Next;
411
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000412 SmallVector<AnnotatedLine *, 1> Children;
413
Manuel Klimek71814b42013-10-11 21:25:45 +0000414 /// \brief Stores the formatting decision for the token once it was made.
415 FormatDecision Decision;
416
417 /// \brief If \c true, this token has been fully formatted (indented and
418 /// potentially re-formatted inside), and we do not allow further formatting
419 /// changes.
420 bool Finalized;
421
Alexander Kornienko4b672072013-06-03 16:45:03 +0000422private:
423 // Disallow copying.
Aaron Ballmanabc18922015-02-15 22:54:08 +0000424 FormatToken(const FormatToken &) = delete;
425 void operator=(const FormatToken &) = delete;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000426};
427
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000428class ContinuationIndenter;
429struct LineState;
430
431class TokenRole {
432public:
433 TokenRole(const FormatStyle &Style) : Style(Style) {}
434 virtual ~TokenRole();
435
436 /// \brief After the \c TokenAnnotator has finished annotating all the tokens,
437 /// this function precomputes required information for formatting.
438 virtual void precomputeFormattingInfos(const FormatToken *Token);
439
440 /// \brief Apply the special formatting that the given role demands.
441 ///
Daniel Jasper01603472014-01-09 13:42:56 +0000442 /// Assumes that the token having this role is already formatted.
443 ///
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000444 /// Continues formatting from \p State leaving indentation to \p Indenter and
445 /// returns the total penalty that this formatting incurs.
Daniel Jasper01603472014-01-09 13:42:56 +0000446 virtual unsigned formatFromToken(LineState &State,
447 ContinuationIndenter *Indenter,
448 bool DryRun) {
449 return 0;
450 }
451
452 /// \brief Same as \c formatFromToken, but assumes that the first token has
453 /// already been set thereby deciding on the first line break.
454 virtual unsigned formatAfterToken(LineState &State,
455 ContinuationIndenter *Indenter,
456 bool DryRun) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000457 return 0;
458 }
459
460 /// \brief Notifies the \c Role that a comma was found.
461 virtual void CommaFound(const FormatToken *Token) {}
462
463protected:
464 const FormatStyle &Style;
465};
466
467class CommaSeparatedList : public TokenRole {
468public:
Daniel Jasper01603472014-01-09 13:42:56 +0000469 CommaSeparatedList(const FormatStyle &Style)
470 : TokenRole(Style), HasNestedBracedList(false) {}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000471
Craig Topperfb6b25b2014-03-15 04:29:04 +0000472 void precomputeFormattingInfos(const FormatToken *Token) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000473
Craig Topperfb6b25b2014-03-15 04:29:04 +0000474 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
475 bool DryRun) override;
Daniel Jasper01603472014-01-09 13:42:56 +0000476
Craig Topperfb6b25b2014-03-15 04:29:04 +0000477 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
478 bool DryRun) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000479
480 /// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000481 void CommaFound(const FormatToken *Token) override {
482 Commas.push_back(Token);
483 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000484
485private:
486 /// \brief A struct that holds information on how to format a given list with
487 /// a specific number of columns.
488 struct ColumnFormat {
489 /// \brief The number of columns to use.
490 unsigned Columns;
491
492 /// \brief The total width in characters.
493 unsigned TotalWidth;
494
495 /// \brief The number of lines required for this format.
496 unsigned LineCount;
497
498 /// \brief The size of each column in characters.
499 SmallVector<unsigned, 8> ColumnSizes;
500 };
501
502 /// \brief Calculate which \c ColumnFormat fits best into
503 /// \p RemainingCharacters.
504 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
505
506 /// \brief The ordered \c FormatTokens making up the commas of this list.
507 SmallVector<const FormatToken *, 8> Commas;
508
509 /// \brief The length of each of the list's items in characters including the
510 /// trailing comma.
511 SmallVector<unsigned, 8> ItemLengths;
512
513 /// \brief Precomputed formats that can be used for this list.
514 SmallVector<ColumnFormat, 4> Formats;
Daniel Jasper01603472014-01-09 13:42:56 +0000515
516 bool HasNestedBracedList;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000517};
518
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000519/// \brief Encapsulates keywords that are context sensitive or for languages not
520/// properly supported by Clang's lexer.
521struct AdditionalKeywords {
522 AdditionalKeywords(IdentifierTable &IdentTable) {
523 kw_in = &IdentTable.get("in");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000524 kw_CF_ENUM = &IdentTable.get("CF_ENUM");
525 kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000526 kw_NS_ENUM = &IdentTable.get("NS_ENUM");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000527 kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000528
529 kw_finally = &IdentTable.get("finally");
530 kw_function = &IdentTable.get("function");
Daniel Jasper354aa512015-02-19 16:07:32 +0000531 kw_import = &IdentTable.get("import");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000532 kw_var = &IdentTable.get("var");
533
Daniel Jasper82c92752014-11-21 12:19:07 +0000534 kw_abstract = &IdentTable.get("abstract");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000535 kw_extends = &IdentTable.get("extends");
Daniel Jasper82c92752014-11-21 12:19:07 +0000536 kw_final = &IdentTable.get("final");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000537 kw_implements = &IdentTable.get("implements");
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000538 kw_instanceof = &IdentTable.get("instanceof");
Nico Webera644d7f2014-11-10 16:30:02 +0000539 kw_interface = &IdentTable.get("interface");
Nico Webered501662015-01-13 22:32:50 +0000540 kw_native = &IdentTable.get("native");
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000541 kw_package = &IdentTable.get("package");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000542 kw_synchronized = &IdentTable.get("synchronized");
543 kw_throws = &IdentTable.get("throws");
Nico Weberfac23712015-02-04 15:26:27 +0000544 kw___except = &IdentTable.get("__except");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000545
546 kw_option = &IdentTable.get("option");
547 kw_optional = &IdentTable.get("optional");
548 kw_repeated = &IdentTable.get("repeated");
549 kw_required = &IdentTable.get("required");
550 kw_returns = &IdentTable.get("returns");
551 }
552
Nico Weberfac23712015-02-04 15:26:27 +0000553 // Context sensitive keywords.
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000554 IdentifierInfo *kw_in;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000555 IdentifierInfo *kw_CF_ENUM;
556 IdentifierInfo *kw_CF_OPTIONS;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000557 IdentifierInfo *kw_NS_ENUM;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000558 IdentifierInfo *kw_NS_OPTIONS;
Nico Weberfac23712015-02-04 15:26:27 +0000559 IdentifierInfo *kw___except;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000560
561 // JavaScript keywords.
562 IdentifierInfo *kw_finally;
563 IdentifierInfo *kw_function;
Daniel Jasper354aa512015-02-19 16:07:32 +0000564 IdentifierInfo *kw_import;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000565 IdentifierInfo *kw_var;
566
567 // Java keywords.
Daniel Jasper82c92752014-11-21 12:19:07 +0000568 IdentifierInfo *kw_abstract;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000569 IdentifierInfo *kw_extends;
Daniel Jasper82c92752014-11-21 12:19:07 +0000570 IdentifierInfo *kw_final;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000571 IdentifierInfo *kw_implements;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000572 IdentifierInfo *kw_instanceof;
Nico Webera644d7f2014-11-10 16:30:02 +0000573 IdentifierInfo *kw_interface;
Nico Webered501662015-01-13 22:32:50 +0000574 IdentifierInfo *kw_native;
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000575 IdentifierInfo *kw_package;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000576 IdentifierInfo *kw_synchronized;
577 IdentifierInfo *kw_throws;
578
579 // Proto keywords.
580 IdentifierInfo *kw_option;
581 IdentifierInfo *kw_optional;
582 IdentifierInfo *kw_repeated;
583 IdentifierInfo *kw_required;
584 IdentifierInfo *kw_returns;
585};
586
Alexander Kornienko4b672072013-06-03 16:45:03 +0000587} // namespace format
588} // namespace clang
589
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000590#endif