blob: 70e23623518af19c1e7421e969ddc6edaad9e6f4 [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 Jaspera74f5072015-04-13 15:01:40 +000052 TT_JsTypeOptionalQuestion,
Daniel Jasper8354ea82014-11-21 12:14:12 +000053 TT_LambdaArrow,
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000054 TT_LambdaLSquare,
Daniel Jaspere9ab42d2014-10-31 18:23:49 +000055 TT_LeadingJavaAnnotation,
Alexander Kornienko4b672072013-06-03 16:45:03 +000056 TT_LineComment,
Daniel Jasperc13ee342014-03-27 09:43:54 +000057 TT_ObjCBlockLBrace,
Manuel Klimek68b03042014-04-14 09:14:11 +000058 TT_ObjCBlockLParen,
Alexander Kornienko4b672072013-06-03 16:45:03 +000059 TT_ObjCDecl,
Alexander Kornienko4b672072013-06-03 16:45:03 +000060 TT_ObjCForIn,
61 TT_ObjCMethodExpr,
62 TT_ObjCMethodSpecifier,
63 TT_ObjCProperty,
Alexander Kornienko4b672072013-06-03 16:45:03 +000064 TT_OverloadedOperator,
65 TT_OverloadedOperatorLParen,
66 TT_PointerOrReference,
67 TT_PureVirtualSpecifier,
68 TT_RangeBasedForLoopColon,
Daniel Jasperf9ae3122014-05-08 07:01:45 +000069 TT_RegexLiteral,
Daniel Jasper17062ff2014-06-10 14:44:02 +000070 TT_SelectorName,
Alexander Kornienko4b672072013-06-03 16:45:03 +000071 TT_StartOfName,
72 TT_TemplateCloser,
73 TT_TemplateOpener,
Daniel Jaspera0ef4f32015-02-20 13:47:38 +000074 TT_TemplateString,
Daniel Jasper43e6a282013-12-16 15:01:54 +000075 TT_TrailingAnnotation,
Daniel Jasper6cdec7c2013-07-09 14:36:48 +000076 TT_TrailingReturnArrow,
Alexander Kornienko4b672072013-06-03 16:45:03 +000077 TT_TrailingUnaryOperator,
78 TT_UnaryOperator,
79 TT_Unknown
80};
81
Daniel Jasperb1f74a82013-07-09 09:06:29 +000082// Represents what type of block a set of braces open.
83enum BraceBlockKind {
84 BK_Unknown,
85 BK_Block,
86 BK_BracedInit
87};
88
Daniel Jasperb10cbc42013-07-10 14:02:49 +000089// The packing kind of a function's parameters.
90enum ParameterPackingKind {
91 PPK_BinPacked,
92 PPK_OnePerLine,
93 PPK_Inconclusive
94};
95
Manuel Klimek71814b42013-10-11 21:25:45 +000096enum FormatDecision {
97 FD_Unformatted,
98 FD_Continue,
99 FD_Break
100};
101
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000102class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000103class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000104
Alexander Kornienko4b672072013-06-03 16:45:03 +0000105/// \brief A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000106/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000107struct FormatToken {
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000108 FormatToken() {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000109
110 /// \brief The \c Token.
111 Token Tok;
112
113 /// \brief The number of newlines immediately before the \c Token.
114 ///
115 /// This can be used to determine what the user wrote in the original code
116 /// and thereby e.g. leave an empty line between two function definitions.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000117 unsigned NewlinesBefore = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000118
119 /// \brief Whether there is at least one unescaped newline before the \c
120 /// Token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000121 bool HasUnescapedNewline = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000122
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000123 /// \brief The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000124 SourceRange WhitespaceRange;
125
126 /// \brief The offset just past the last '\n' in this token's leading
127 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000128 unsigned LastNewlineOffset = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000129
Alexander Kornienko39856b72013-09-10 09:38:25 +0000130 /// \brief The width of the non-whitespace parts of the token (or its first
131 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000132 /// We need this to correctly measure number of columns a token spans.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000133 unsigned ColumnWidth = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000134
Alexander Kornienko39856b72013-09-10 09:38:25 +0000135 /// \brief Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000136 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000137 unsigned LastLineColumnWidth = 0;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000138
Alexander Kornienko39856b72013-09-10 09:38:25 +0000139 /// \brief Whether the token text contains newlines (escaped or not).
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000140 bool IsMultiline = false;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000141
Alexander Kornienko4b672072013-06-03 16:45:03 +0000142 /// \brief Indicates that this is the first token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000143 bool IsFirst = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000144
145 /// \brief Whether there must be a line break before this token.
146 ///
147 /// This happens for example when a preprocessor directive ended directly
148 /// before the token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000149 bool MustBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000150
151 /// \brief The raw text of the token.
152 ///
153 /// Contains the raw token text without leading whitespace and without leading
154 /// escaped newlines.
155 StringRef TokenText;
156
Daniel Jasper8369aa52013-07-16 20:28:33 +0000157 /// \brief Set to \c true if this token is an unterminated literal.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000158 bool IsUnterminatedLiteral = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +0000159
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000160 /// \brief Contains the kind of block if this token is a brace.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000161 BraceBlockKind BlockKind = BK_Unknown;
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000162
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000163 TokenType Type = TT_Unknown;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000164
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000165 /// \brief The number of spaces that should be inserted before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000166 unsigned SpacesRequiredBefore = 0;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000167
168 /// \brief \c true if it is allowed to break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000169 bool CanBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000170
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000171 /// \brief \c true if this is the ">" of "template<..>".
172 bool ClosesTemplateDeclaration = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000173
174 /// \brief Number of parameters, if this is "(", "[" or "<".
175 ///
176 /// This is initialized to 1 as we don't need to distinguish functions with
177 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
178 /// the number of commas.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000179 unsigned ParameterCount = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000180
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000181 /// \brief Number of parameters that are nested blocks,
182 /// if this is "(", "[" or "<".
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000183 unsigned BlockParameterCount = 0;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000184
Daniel Jasperde7ca752015-05-04 07:39:00 +0000185 /// \brief If this is a bracket ("<", "(", "[" or "{"), contains the kind of
186 /// the surrounding bracket.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000187 tok::TokenKind ParentBracket = tok::unknown;
Daniel Jasperde7ca752015-05-04 07:39:00 +0000188
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000189 /// \brief A token can have a special role that can carry extra information
190 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000191 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000192
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000193 /// \brief If this is an opening parenthesis, how are the parameters packed?
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000194 ParameterPackingKind PackingKind = PPK_Inconclusive;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000195
Manuel Klimek31c85922013-08-29 15:21:40 +0000196 /// \brief The total length of the unwrapped line up to and including this
197 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000198 unsigned TotalLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000199
Alexander Kornienko39856b72013-09-10 09:38:25 +0000200 /// \brief The original 0-based column of this token, including expanded tabs.
201 /// The configured TabWidth is used as tab width.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000202 unsigned OriginalColumn = 0;
Manuel Klimek31c85922013-08-29 15:21:40 +0000203
Alexander Kornienko4b672072013-06-03 16:45:03 +0000204 /// \brief The length of following tokens until the next natural split point,
205 /// or the next token that can be broken.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000206 unsigned UnbreakableTailLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000207
208 // FIXME: Come up with a 'cleaner' concept.
209 /// \brief The binding strength of a token. This is a combined value of
210 /// operator precedence, parenthesis nesting, etc.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000211 unsigned BindingStrength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000212
Daniel Jasper63af7c42013-12-09 14:40:19 +0000213 /// \brief The nesting level of this token, i.e. the number of surrounding (),
214 /// [], {} or <>.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000215 unsigned NestingLevel = 0;
Daniel Jasper63af7c42013-12-09 14:40:19 +0000216
Alexander Kornienko4b672072013-06-03 16:45:03 +0000217 /// \brief Penalty for inserting a line break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000218 unsigned SplitPenalty = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000219
220 /// \brief If this is the first ObjC selector name in an ObjC method
221 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000222 ///
223 /// This being set to 0 means that the selectors should not be colon-aligned,
224 /// e.g. because several of them are block-type.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000225 unsigned LongestObjCSelectorName = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000226
227 /// \brief Stores the number of required fake parentheses and the
228 /// corresponding operator precedence.
229 ///
230 /// If multiple fake parentheses start at a token, this vector stores them in
231 /// reverse order, i.e. inner fake parenthesis first.
232 SmallVector<prec::Level, 4> FakeLParens;
233 /// \brief Insert this many fake ) after this token for correct indentation.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000234 unsigned FakeRParens = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000235
Daniel Jasper562ecd42013-09-06 08:08:14 +0000236 /// \brief \c true if this token starts a binary expression, i.e. has at least
237 /// one fake l_paren with a precedence greater than prec::Unknown.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000238 bool StartsBinaryExpression = false;
Daniel Jasper562ecd42013-09-06 08:08:14 +0000239 /// \brief \c true if this token ends a binary expression.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000240 bool EndsBinaryExpression = false;
Daniel Jasper562ecd42013-09-06 08:08:14 +0000241
Daniel Jasper0e617842014-04-16 12:26:54 +0000242 /// \brief Is this is an operator (or "."/"->") in a sequence of operators
243 /// with the same precedence, contains the 0-based operator index.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000244 unsigned OperatorIndex = 0;
Daniel Jasper0e617842014-04-16 12:26:54 +0000245
246 /// \brief Is this the last operator (or "."/"->") in a sequence of operators
247 /// with the same precedence?
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000248 bool LastOperator = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000249
250 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
251 ///
252 /// Only set if \c Type == \c TT_StartOfName.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000253 bool PartOfMultiVariableDeclStmt = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000254
Daniel Jaspere1e43192014-04-01 12:55:11 +0000255 /// \brief Is this a foreach macro?
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000256 bool IsForEachMacro = false;
257
258
259 /// \brief If this is a bracket, this points to the matching one.
260 FormatToken *MatchingParen = nullptr;
261
262 /// \brief The previous token in the unwrapped line.
263 FormatToken *Previous = nullptr;
264
265 /// \brief The next token in the unwrapped line.
266 FormatToken *Next = nullptr;
267
268 /// \brief If this token starts a block, this contains all the unwrapped lines
269 /// in it.
270 SmallVector<AnnotatedLine *, 1> Children;
271
272 /// \brief Stores the formatting decision for the token once it was made.
273 FormatDecision Decision = FD_Unformatted;
274
275 /// \brief If \c true, this token has been fully formatted (indented and
276 /// potentially re-formatted inside), and we do not allow further formatting
277 /// changes.
278 bool Finalized = false;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000279
Alexander Kornienko4b672072013-06-03 16:45:03 +0000280 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
Daniel Jasper8354ea82014-11-21 12:14:12 +0000281 bool is(TokenType TT) const { return Type == TT; }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000282 bool is(const IdentifierInfo *II) const {
283 return II && II == Tok.getIdentifierInfo();
284 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000285 template <typename A, typename B> bool isOneOf(A K1, B K2) const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000286 return is(K1) || is(K2);
287 }
Benjamin Kramerc5bc3cd2015-02-15 20:11:14 +0000288 template <typename A, typename B, typename... Ts>
289 bool isOneOf(A K1, B K2, Ts... Ks) const {
290 return is(K1) || isOneOf(K2, Ks...);
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000291 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000292 template <typename T> bool isNot(T Kind) const { return !is(Kind); }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000293
Daniel Jasper04b6a082013-12-20 06:22:01 +0000294 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000295
296 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
297 return Tok.isObjCAtKeyword(Kind);
298 }
299
300 bool isAccessSpecifier(bool ColonRequired = true) const {
301 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
302 (!ColonRequired || (Next && Next->is(tok::colon)));
303 }
304
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000305 /// \brief Determine whether the token is a simple-type-specifier.
306 bool isSimpleTypeSpecifier() const;
307
Alexander Kornienko4b672072013-06-03 16:45:03 +0000308 bool isObjCAccessSpecifier() const {
309 return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
310 Next->isObjCAtKeyword(tok::objc_protected) ||
311 Next->isObjCAtKeyword(tok::objc_package) ||
312 Next->isObjCAtKeyword(tok::objc_private));
313 }
314
315 /// \brief Returns whether \p Tok is ([{ or a template opening <.
316 bool opensScope() const {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000317 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
318 TT_TemplateOpener);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000319 }
Nico Weber0f987a62013-06-25 19:25:12 +0000320 /// \brief Returns whether \p Tok is )]} or a template closing >.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000321 bool closesScope() const {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000322 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
323 TT_TemplateCloser);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000324 }
325
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000326 /// \brief Returns \c true if this is a "." or "->" accessing a member.
327 bool isMemberAccess() const {
Daniel Jasper85bcadc2014-07-09 13:07:57 +0000328 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
Daniel Jasper05cd9292015-03-26 18:46:28 +0000329 !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
330 TT_LambdaArrow);
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000331 }
332
Alexander Kornienko4b672072013-06-03 16:45:03 +0000333 bool isUnaryOperator() const {
334 switch (Tok.getKind()) {
335 case tok::plus:
336 case tok::plusplus:
337 case tok::minus:
338 case tok::minusminus:
339 case tok::exclaim:
340 case tok::tilde:
341 case tok::kw_sizeof:
342 case tok::kw_alignof:
343 return true;
344 default:
345 return false;
346 }
347 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000348
Alexander Kornienko4b672072013-06-03 16:45:03 +0000349 bool isBinaryOperator() const {
350 // Comma is a binary operator, but does not behave as such wrt. formatting.
351 return getPrecedence() > prec::Comma;
352 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000353
Alexander Kornienko4b672072013-06-03 16:45:03 +0000354 bool isTrailingComment() const {
Daniel Jasper610381f2014-08-26 09:37:52 +0000355 return is(tok::comment) &&
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000356 (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000357 }
358
Daniel Jasper78b194992014-08-06 14:15:41 +0000359 /// \brief Returns \c true if this is a keyword that can be used
360 /// like a function call (e.g. sizeof, typeid, ...).
361 bool isFunctionLikeKeyword() const {
362 switch (Tok.getKind()) {
363 case tok::kw_throw:
364 case tok::kw_typeid:
365 case tok::kw_return:
366 case tok::kw_sizeof:
367 case tok::kw_alignof:
368 case tok::kw_alignas:
369 case tok::kw_decltype:
370 case tok::kw_noexcept:
371 case tok::kw_static_assert:
372 case tok::kw___attribute:
373 return true;
374 default:
375 return false;
376 }
377 }
378
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000379 /// \brief Returns actual token start location without leading escaped
380 /// newlines and whitespace.
381 ///
382 /// This can be different to Tok.getLocation(), which includes leading escaped
383 /// newlines.
384 SourceLocation getStartOfNonWhitespace() const {
385 return WhitespaceRange.getEnd();
386 }
387
Alexander Kornienko4b672072013-06-03 16:45:03 +0000388 prec::Level getPrecedence() const {
389 return getBinOpPrecedence(Tok.getKind(), true, true);
390 }
391
392 /// \brief Returns the previous token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000393 FormatToken *getPreviousNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000394 FormatToken *Tok = Previous;
Craig Topper2145bc02014-05-09 08:15:10 +0000395 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000396 Tok = Tok->Previous;
397 return Tok;
398 }
399
400 /// \brief Returns the next token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000401 const FormatToken *getNextNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000402 const FormatToken *Tok = Next;
Craig Topper2145bc02014-05-09 08:15:10 +0000403 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000404 Tok = Tok->Next;
405 return Tok;
406 }
407
Daniel Jasperb8f61682013-10-22 15:45:58 +0000408 /// \brief Returns \c true if this tokens starts a block-type list, i.e. a
409 /// list that should be indented with a block indent.
410 bool opensBlockTypeList(const FormatStyle &Style) const {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000411 return is(TT_ArrayInitializerLSquare) ||
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000412 (is(tok::l_brace) &&
413 (BlockKind == BK_Block || is(TT_DictLiteral) ||
414 (!Style.Cpp11BracedListStyle && NestingLevel == 0)));
Daniel Jasperb8f61682013-10-22 15:45:58 +0000415 }
416
417 /// \brief Same as opensBlockTypeList, but for the closing token.
Daniel Jasper1db6c382013-10-22 15:30:28 +0000418 bool closesBlockTypeList(const FormatStyle &Style) const {
Daniel Jasperb8f61682013-10-22 15:45:58 +0000419 return MatchingParen && MatchingParen->opensBlockTypeList(Style);
Daniel Jasper1db6c382013-10-22 15:30:28 +0000420 }
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
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000546 kw_mark = &IdentTable.get("mark");
547
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000548 kw_option = &IdentTable.get("option");
549 kw_optional = &IdentTable.get("optional");
550 kw_repeated = &IdentTable.get("repeated");
551 kw_required = &IdentTable.get("required");
552 kw_returns = &IdentTable.get("returns");
Daniel Jasper53395402015-04-07 15:04:40 +0000553
554 kw_signals = &IdentTable.get("signals");
555 kw_slots = &IdentTable.get("slots");
556 kw_qslots = &IdentTable.get("Q_SLOTS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000557 }
558
Nico Weberfac23712015-02-04 15:26:27 +0000559 // Context sensitive keywords.
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000560 IdentifierInfo *kw_in;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000561 IdentifierInfo *kw_CF_ENUM;
562 IdentifierInfo *kw_CF_OPTIONS;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000563 IdentifierInfo *kw_NS_ENUM;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000564 IdentifierInfo *kw_NS_OPTIONS;
Nico Weberfac23712015-02-04 15:26:27 +0000565 IdentifierInfo *kw___except;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000566
567 // JavaScript keywords.
568 IdentifierInfo *kw_finally;
569 IdentifierInfo *kw_function;
Daniel Jasper354aa512015-02-19 16:07:32 +0000570 IdentifierInfo *kw_import;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000571 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;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000578 IdentifierInfo *kw_instanceof;
Nico Webera644d7f2014-11-10 16:30:02 +0000579 IdentifierInfo *kw_interface;
Nico Webered501662015-01-13 22:32:50 +0000580 IdentifierInfo *kw_native;
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000581 IdentifierInfo *kw_package;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000582 IdentifierInfo *kw_synchronized;
583 IdentifierInfo *kw_throws;
584
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000585 // Pragma keywords.
586 IdentifierInfo *kw_mark;
587
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000588 // Proto keywords.
589 IdentifierInfo *kw_option;
590 IdentifierInfo *kw_optional;
591 IdentifierInfo *kw_repeated;
592 IdentifierInfo *kw_required;
593 IdentifierInfo *kw_returns;
Daniel Jasper53395402015-04-07 15:04:40 +0000594
595 // QT keywords.
596 IdentifierInfo *kw_signals;
597 IdentifierInfo *kw_slots;
598 IdentifierInfo *kw_qslots;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000599};
600
Alexander Kornienko4b672072013-06-03 16:45:03 +0000601} // namespace format
602} // namespace clang
603
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000604#endif