blob: f335eda086c0b009a248efc8b9de18194c0f8094 [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
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +000028#define LIST_TOKEN_TYPES \
29 TYPE(ArrayInitializerLSquare) \
30 TYPE(ArraySubscriptLSquare) \
31 TYPE(AttributeParen) \
32 TYPE(BinaryOperator) \
33 TYPE(BitFieldColon) \
34 TYPE(BlockComment) \
35 TYPE(CastRParen) \
36 TYPE(ConditionalExpr) \
37 TYPE(ConflictAlternative) \
38 TYPE(ConflictEnd) \
39 TYPE(ConflictStart) \
40 TYPE(CtorInitializerColon) \
41 TYPE(CtorInitializerComma) \
42 TYPE(DesignatedInitializerPeriod) \
43 TYPE(DictLiteral) \
44 TYPE(ForEachMacro) \
45 TYPE(FunctionAnnotationRParen) \
46 TYPE(FunctionDeclarationName) \
47 TYPE(FunctionLBrace) \
48 TYPE(FunctionTypeLParen) \
49 TYPE(ImplicitStringLiteral) \
50 TYPE(InheritanceColon) \
51 TYPE(InlineASMBrace) \
52 TYPE(InlineASMColon) \
53 TYPE(JavaAnnotation) \
54 TYPE(JsComputedPropertyName) \
55 TYPE(JsFatArrow) \
56 TYPE(JsTypeColon) \
57 TYPE(JsTypeOptionalQuestion) \
58 TYPE(LambdaArrow) \
59 TYPE(LambdaLSquare) \
60 TYPE(LeadingJavaAnnotation) \
61 TYPE(LineComment) \
62 TYPE(MacroBlockBegin) \
63 TYPE(MacroBlockEnd) \
64 TYPE(ObjCBlockLBrace) \
65 TYPE(ObjCBlockLParen) \
66 TYPE(ObjCDecl) \
67 TYPE(ObjCForIn) \
68 TYPE(ObjCMethodExpr) \
69 TYPE(ObjCMethodSpecifier) \
70 TYPE(ObjCProperty) \
71 TYPE(ObjCStringLiteral) \
72 TYPE(OverloadedOperator) \
73 TYPE(OverloadedOperatorLParen) \
74 TYPE(PointerOrReference) \
75 TYPE(PureVirtualSpecifier) \
76 TYPE(RangeBasedForLoopColon) \
77 TYPE(RegexLiteral) \
78 TYPE(SelectorName) \
79 TYPE(StartOfName) \
80 TYPE(TemplateCloser) \
81 TYPE(TemplateOpener) \
82 TYPE(TemplateString) \
83 TYPE(TrailingAnnotation) \
84 TYPE(TrailingReturnArrow) \
85 TYPE(TrailingUnaryOperator) \
86 TYPE(UnaryOperator) \
87 TYPE(Unknown)
88
Alexander Kornienko4b672072013-06-03 16:45:03 +000089enum TokenType {
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +000090#define TYPE(X) TT_##X,
91LIST_TOKEN_TYPES
92#undef TYPE
93 NUM_TOKEN_TYPES
Alexander Kornienko4b672072013-06-03 16:45:03 +000094};
95
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +000096/// \brief Determines the name of a token type.
97const char *getTokenTypeName(TokenType Type);
98
Daniel Jasperb1f74a82013-07-09 09:06:29 +000099// Represents what type of block a set of braces open.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000100enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit };
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000101
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000102// The packing kind of a function's parameters.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000103enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive };
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000104
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000105enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break };
Manuel Klimek71814b42013-10-11 21:25:45 +0000106
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000107class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000108class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000109
Alexander Kornienko4b672072013-06-03 16:45:03 +0000110/// \brief A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000111/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000112struct FormatToken {
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000113 FormatToken() {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000114
115 /// \brief The \c Token.
116 Token Tok;
117
118 /// \brief The number of newlines immediately before the \c Token.
119 ///
120 /// This can be used to determine what the user wrote in the original code
121 /// and thereby e.g. leave an empty line between two function definitions.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000122 unsigned NewlinesBefore = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000123
124 /// \brief Whether there is at least one unescaped newline before the \c
125 /// Token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000126 bool HasUnescapedNewline = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000127
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000128 /// \brief The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000129 SourceRange WhitespaceRange;
130
131 /// \brief The offset just past the last '\n' in this token's leading
132 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000133 unsigned LastNewlineOffset = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000134
Alexander Kornienko39856b72013-09-10 09:38:25 +0000135 /// \brief The width of the non-whitespace parts of the token (or its first
136 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000137 /// We need this to correctly measure number of columns a token spans.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000138 unsigned ColumnWidth = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000139
Alexander Kornienko39856b72013-09-10 09:38:25 +0000140 /// \brief Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000141 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000142 unsigned LastLineColumnWidth = 0;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000143
Alexander Kornienko39856b72013-09-10 09:38:25 +0000144 /// \brief Whether the token text contains newlines (escaped or not).
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000145 bool IsMultiline = false;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000146
Alexander Kornienko4b672072013-06-03 16:45:03 +0000147 /// \brief Indicates that this is the first token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000148 bool IsFirst = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000149
150 /// \brief Whether there must be a line break before this token.
151 ///
152 /// This happens for example when a preprocessor directive ended directly
153 /// before the token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000154 bool MustBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000155
156 /// \brief The raw text of the token.
157 ///
158 /// Contains the raw token text without leading whitespace and without leading
159 /// escaped newlines.
160 StringRef TokenText;
161
Daniel Jasper8369aa52013-07-16 20:28:33 +0000162 /// \brief Set to \c true if this token is an unterminated literal.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000163 bool IsUnterminatedLiteral = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +0000164
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000165 /// \brief Contains the kind of block if this token is a brace.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000166 BraceBlockKind BlockKind = BK_Unknown;
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000167
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000168 TokenType Type = TT_Unknown;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000169
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000170 /// \brief The number of spaces that should be inserted before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000171 unsigned SpacesRequiredBefore = 0;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000172
173 /// \brief \c true if it is allowed to break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000174 bool CanBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000175
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000176 /// \brief \c true if this is the ">" of "template<..>".
177 bool ClosesTemplateDeclaration = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000178
179 /// \brief Number of parameters, if this is "(", "[" or "<".
180 ///
181 /// This is initialized to 1 as we don't need to distinguish functions with
182 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
183 /// the number of commas.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000184 unsigned ParameterCount = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000185
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000186 /// \brief Number of parameters that are nested blocks,
187 /// if this is "(", "[" or "<".
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000188 unsigned BlockParameterCount = 0;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000189
Daniel Jasperde7ca752015-05-04 07:39:00 +0000190 /// \brief If this is a bracket ("<", "(", "[" or "{"), contains the kind of
191 /// the surrounding bracket.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000192 tok::TokenKind ParentBracket = tok::unknown;
Daniel Jasperde7ca752015-05-04 07:39:00 +0000193
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000194 /// \brief A token can have a special role that can carry extra information
195 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000196 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000197
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000198 /// \brief If this is an opening parenthesis, how are the parameters packed?
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000199 ParameterPackingKind PackingKind = PPK_Inconclusive;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000200
Manuel Klimek31c85922013-08-29 15:21:40 +0000201 /// \brief The total length of the unwrapped line up to and including this
202 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000203 unsigned TotalLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000204
Alexander Kornienko39856b72013-09-10 09:38:25 +0000205 /// \brief The original 0-based column of this token, including expanded tabs.
206 /// The configured TabWidth is used as tab width.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000207 unsigned OriginalColumn = 0;
Manuel Klimek31c85922013-08-29 15:21:40 +0000208
Alexander Kornienko4b672072013-06-03 16:45:03 +0000209 /// \brief The length of following tokens until the next natural split point,
210 /// or the next token that can be broken.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000211 unsigned UnbreakableTailLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000212
213 // FIXME: Come up with a 'cleaner' concept.
214 /// \brief The binding strength of a token. This is a combined value of
215 /// operator precedence, parenthesis nesting, etc.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000216 unsigned BindingStrength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000217
Daniel Jasper63af7c42013-12-09 14:40:19 +0000218 /// \brief The nesting level of this token, i.e. the number of surrounding (),
219 /// [], {} or <>.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000220 unsigned NestingLevel = 0;
Daniel Jasper63af7c42013-12-09 14:40:19 +0000221
Alexander Kornienko4b672072013-06-03 16:45:03 +0000222 /// \brief Penalty for inserting a line break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000223 unsigned SplitPenalty = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000224
225 /// \brief If this is the first ObjC selector name in an ObjC method
226 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000227 ///
228 /// This being set to 0 means that the selectors should not be colon-aligned,
229 /// e.g. because several of them are block-type.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000230 unsigned LongestObjCSelectorName = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000231
232 /// \brief Stores the number of required fake parentheses and the
233 /// corresponding operator precedence.
234 ///
235 /// If multiple fake parentheses start at a token, this vector stores them in
236 /// reverse order, i.e. inner fake parenthesis first.
237 SmallVector<prec::Level, 4> FakeLParens;
238 /// \brief Insert this many fake ) after this token for correct indentation.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000239 unsigned FakeRParens = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000240
Daniel Jasper562ecd42013-09-06 08:08:14 +0000241 /// \brief \c true if this token starts a binary expression, i.e. has at least
242 /// one fake l_paren with a precedence greater than prec::Unknown.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000243 bool StartsBinaryExpression = false;
Daniel Jasper562ecd42013-09-06 08:08:14 +0000244 /// \brief \c true if this token ends a binary expression.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000245 bool EndsBinaryExpression = false;
Daniel Jasper562ecd42013-09-06 08:08:14 +0000246
Daniel Jasper0e617842014-04-16 12:26:54 +0000247 /// \brief Is this is an operator (or "."/"->") in a sequence of operators
248 /// with the same precedence, contains the 0-based operator index.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000249 unsigned OperatorIndex = 0;
Daniel Jasper0e617842014-04-16 12:26:54 +0000250
251 /// \brief Is this the last operator (or "."/"->") in a sequence of operators
252 /// with the same precedence?
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000253 bool LastOperator = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000254
255 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
256 ///
257 /// Only set if \c Type == \c TT_StartOfName.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000258 bool PartOfMultiVariableDeclStmt = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000259
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000260 /// \brief If this is a bracket, this points to the matching one.
261 FormatToken *MatchingParen = nullptr;
262
263 /// \brief The previous token in the unwrapped line.
264 FormatToken *Previous = nullptr;
265
266 /// \brief The next token in the unwrapped line.
267 FormatToken *Next = nullptr;
268
269 /// \brief If this token starts a block, this contains all the unwrapped lines
270 /// in it.
271 SmallVector<AnnotatedLine *, 1> Children;
272
273 /// \brief Stores the formatting decision for the token once it was made.
274 FormatDecision Decision = FD_Unformatted;
275
276 /// \brief If \c true, this token has been fully formatted (indented and
277 /// potentially re-formatted inside), and we do not allow further formatting
278 /// changes.
279 bool Finalized = false;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000280
Alexander Kornienko4b672072013-06-03 16:45:03 +0000281 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
Daniel Jasper8354ea82014-11-21 12:14:12 +0000282 bool is(TokenType TT) const { return Type == TT; }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000283 bool is(const IdentifierInfo *II) const {
284 return II && II == Tok.getIdentifierInfo();
285 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000286 template <typename A, typename B> bool isOneOf(A K1, B K2) const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000287 return is(K1) || is(K2);
288 }
Benjamin Kramerc5bc3cd2015-02-15 20:11:14 +0000289 template <typename A, typename B, typename... Ts>
290 bool isOneOf(A K1, B K2, Ts... Ks) const {
291 return is(K1) || isOneOf(K2, Ks...);
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000292 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000293 template <typename T> bool isNot(T Kind) const { return !is(Kind); }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000294
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 {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000318 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
319 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 {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000323 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
324 TT_TemplateCloser);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000325 }
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 Jasper05cd9292015-03-26 18:46:28 +0000330 !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
331 TT_LambdaArrow);
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) &&
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000357 (is(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
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000380 /// \brief Returns actual token start location without leading escaped
381 /// newlines and whitespace.
382 ///
383 /// This can be different to Tok.getLocation(), which includes leading escaped
384 /// newlines.
385 SourceLocation getStartOfNonWhitespace() const {
386 return WhitespaceRange.getEnd();
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 {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000412 return is(TT_ArrayInitializerLSquare) ||
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000413 (is(tok::l_brace) &&
414 (BlockKind == BK_Block || is(TT_DictLiteral) ||
415 (!Style.Cpp11BracedListStyle && NestingLevel == 0)));
Daniel Jasperb8f61682013-10-22 15:45:58 +0000416 }
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 +0000423private:
424 // Disallow copying.
Aaron Ballmanabc18922015-02-15 22:54:08 +0000425 FormatToken(const FormatToken &) = delete;
426 void operator=(const FormatToken &) = delete;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000427};
428
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000429class ContinuationIndenter;
430struct LineState;
431
432class TokenRole {
433public:
434 TokenRole(const FormatStyle &Style) : Style(Style) {}
435 virtual ~TokenRole();
436
437 /// \brief After the \c TokenAnnotator has finished annotating all the tokens,
438 /// this function precomputes required information for formatting.
439 virtual void precomputeFormattingInfos(const FormatToken *Token);
440
441 /// \brief Apply the special formatting that the given role demands.
442 ///
Daniel Jasper01603472014-01-09 13:42:56 +0000443 /// Assumes that the token having this role is already formatted.
444 ///
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000445 /// Continues formatting from \p State leaving indentation to \p Indenter and
446 /// returns the total penalty that this formatting incurs.
Daniel Jasper01603472014-01-09 13:42:56 +0000447 virtual unsigned formatFromToken(LineState &State,
448 ContinuationIndenter *Indenter,
449 bool DryRun) {
450 return 0;
451 }
452
453 /// \brief Same as \c formatFromToken, but assumes that the first token has
454 /// already been set thereby deciding on the first line break.
455 virtual unsigned formatAfterToken(LineState &State,
456 ContinuationIndenter *Indenter,
457 bool DryRun) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000458 return 0;
459 }
460
461 /// \brief Notifies the \c Role that a comma was found.
462 virtual void CommaFound(const FormatToken *Token) {}
463
464protected:
465 const FormatStyle &Style;
466};
467
468class CommaSeparatedList : public TokenRole {
469public:
Daniel Jasper01603472014-01-09 13:42:56 +0000470 CommaSeparatedList(const FormatStyle &Style)
471 : TokenRole(Style), HasNestedBracedList(false) {}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000472
Craig Topperfb6b25b2014-03-15 04:29:04 +0000473 void precomputeFormattingInfos(const FormatToken *Token) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000474
Craig Topperfb6b25b2014-03-15 04:29:04 +0000475 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
476 bool DryRun) override;
Daniel Jasper01603472014-01-09 13:42:56 +0000477
Craig Topperfb6b25b2014-03-15 04:29:04 +0000478 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
479 bool DryRun) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000480
481 /// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000482 void CommaFound(const FormatToken *Token) override {
483 Commas.push_back(Token);
484 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000485
486private:
487 /// \brief A struct that holds information on how to format a given list with
488 /// a specific number of columns.
489 struct ColumnFormat {
490 /// \brief The number of columns to use.
491 unsigned Columns;
492
493 /// \brief The total width in characters.
494 unsigned TotalWidth;
495
496 /// \brief The number of lines required for this format.
497 unsigned LineCount;
498
499 /// \brief The size of each column in characters.
500 SmallVector<unsigned, 8> ColumnSizes;
501 };
502
503 /// \brief Calculate which \c ColumnFormat fits best into
504 /// \p RemainingCharacters.
505 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
506
507 /// \brief The ordered \c FormatTokens making up the commas of this list.
508 SmallVector<const FormatToken *, 8> Commas;
509
510 /// \brief The length of each of the list's items in characters including the
511 /// trailing comma.
512 SmallVector<unsigned, 8> ItemLengths;
513
514 /// \brief Precomputed formats that can be used for this list.
515 SmallVector<ColumnFormat, 4> Formats;
Daniel Jasper01603472014-01-09 13:42:56 +0000516
517 bool HasNestedBracedList;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000518};
519
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000520/// \brief Encapsulates keywords that are context sensitive or for languages not
521/// properly supported by Clang's lexer.
522struct AdditionalKeywords {
523 AdditionalKeywords(IdentifierTable &IdentTable) {
524 kw_in = &IdentTable.get("in");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000525 kw_CF_ENUM = &IdentTable.get("CF_ENUM");
526 kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000527 kw_NS_ENUM = &IdentTable.get("NS_ENUM");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000528 kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000529
530 kw_finally = &IdentTable.get("finally");
531 kw_function = &IdentTable.get("function");
Daniel Jasper354aa512015-02-19 16:07:32 +0000532 kw_import = &IdentTable.get("import");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000533 kw_var = &IdentTable.get("var");
534
Daniel Jasper82c92752014-11-21 12:19:07 +0000535 kw_abstract = &IdentTable.get("abstract");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000536 kw_extends = &IdentTable.get("extends");
Daniel Jasper82c92752014-11-21 12:19:07 +0000537 kw_final = &IdentTable.get("final");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000538 kw_implements = &IdentTable.get("implements");
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000539 kw_instanceof = &IdentTable.get("instanceof");
Nico Webera644d7f2014-11-10 16:30:02 +0000540 kw_interface = &IdentTable.get("interface");
Nico Webered501662015-01-13 22:32:50 +0000541 kw_native = &IdentTable.get("native");
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000542 kw_package = &IdentTable.get("package");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000543 kw_synchronized = &IdentTable.get("synchronized");
544 kw_throws = &IdentTable.get("throws");
Nico Weberfac23712015-02-04 15:26:27 +0000545 kw___except = &IdentTable.get("__except");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000546
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000547 kw_mark = &IdentTable.get("mark");
548
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000549 kw_option = &IdentTable.get("option");
550 kw_optional = &IdentTable.get("optional");
551 kw_repeated = &IdentTable.get("repeated");
552 kw_required = &IdentTable.get("required");
553 kw_returns = &IdentTable.get("returns");
Daniel Jasper53395402015-04-07 15:04:40 +0000554
555 kw_signals = &IdentTable.get("signals");
556 kw_slots = &IdentTable.get("slots");
557 kw_qslots = &IdentTable.get("Q_SLOTS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000558 }
559
Nico Weberfac23712015-02-04 15:26:27 +0000560 // Context sensitive keywords.
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000561 IdentifierInfo *kw_in;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000562 IdentifierInfo *kw_CF_ENUM;
563 IdentifierInfo *kw_CF_OPTIONS;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000564 IdentifierInfo *kw_NS_ENUM;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000565 IdentifierInfo *kw_NS_OPTIONS;
Nico Weberfac23712015-02-04 15:26:27 +0000566 IdentifierInfo *kw___except;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000567
568 // JavaScript keywords.
569 IdentifierInfo *kw_finally;
570 IdentifierInfo *kw_function;
Daniel Jasper354aa512015-02-19 16:07:32 +0000571 IdentifierInfo *kw_import;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000572 IdentifierInfo *kw_var;
573
574 // Java keywords.
Daniel Jasper82c92752014-11-21 12:19:07 +0000575 IdentifierInfo *kw_abstract;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000576 IdentifierInfo *kw_extends;
Daniel Jasper82c92752014-11-21 12:19:07 +0000577 IdentifierInfo *kw_final;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000578 IdentifierInfo *kw_implements;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000579 IdentifierInfo *kw_instanceof;
Nico Webera644d7f2014-11-10 16:30:02 +0000580 IdentifierInfo *kw_interface;
Nico Webered501662015-01-13 22:32:50 +0000581 IdentifierInfo *kw_native;
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000582 IdentifierInfo *kw_package;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000583 IdentifierInfo *kw_synchronized;
584 IdentifierInfo *kw_throws;
585
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000586 // Pragma keywords.
587 IdentifierInfo *kw_mark;
588
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000589 // Proto keywords.
590 IdentifierInfo *kw_option;
591 IdentifierInfo *kw_optional;
592 IdentifierInfo *kw_repeated;
593 IdentifierInfo *kw_required;
594 IdentifierInfo *kw_returns;
Daniel Jasper53395402015-04-07 15:04:40 +0000595
596 // QT keywords.
597 IdentifierInfo *kw_signals;
598 IdentifierInfo *kw_slots;
599 IdentifierInfo *kw_qslots;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000600};
601
Alexander Kornienko4b672072013-06-03 16:45:03 +0000602} // namespace format
603} // namespace clang
604
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000605#endif