blob: d80bd0af5f006363152eee5c63b27910f9abfafd [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) \
Daniel Jasper91b1d1a2016-03-21 17:57:31 +000057 TYPE(JsTypeOperator) \
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +000058 TYPE(JsTypeOptionalQuestion) \
59 TYPE(LambdaArrow) \
60 TYPE(LambdaLSquare) \
61 TYPE(LeadingJavaAnnotation) \
62 TYPE(LineComment) \
63 TYPE(MacroBlockBegin) \
64 TYPE(MacroBlockEnd) \
65 TYPE(ObjCBlockLBrace) \
66 TYPE(ObjCBlockLParen) \
67 TYPE(ObjCDecl) \
68 TYPE(ObjCForIn) \
69 TYPE(ObjCMethodExpr) \
70 TYPE(ObjCMethodSpecifier) \
71 TYPE(ObjCProperty) \
72 TYPE(ObjCStringLiteral) \
73 TYPE(OverloadedOperator) \
74 TYPE(OverloadedOperatorLParen) \
75 TYPE(PointerOrReference) \
76 TYPE(PureVirtualSpecifier) \
77 TYPE(RangeBasedForLoopColon) \
78 TYPE(RegexLiteral) \
79 TYPE(SelectorName) \
80 TYPE(StartOfName) \
81 TYPE(TemplateCloser) \
82 TYPE(TemplateOpener) \
83 TYPE(TemplateString) \
84 TYPE(TrailingAnnotation) \
85 TYPE(TrailingReturnArrow) \
86 TYPE(TrailingUnaryOperator) \
87 TYPE(UnaryOperator) \
88 TYPE(Unknown)
89
Alexander Kornienko4b672072013-06-03 16:45:03 +000090enum TokenType {
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +000091#define TYPE(X) TT_##X,
92LIST_TOKEN_TYPES
93#undef TYPE
94 NUM_TOKEN_TYPES
Alexander Kornienko4b672072013-06-03 16:45:03 +000095};
96
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +000097/// \brief Determines the name of a token type.
98const char *getTokenTypeName(TokenType Type);
99
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000100// Represents what type of block a set of braces open.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000101enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit };
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000102
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000103// The packing kind of a function's parameters.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000104enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive };
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000105
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000106enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break };
Manuel Klimek71814b42013-10-11 21:25:45 +0000107
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000108class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000109class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000110
Alexander Kornienko4b672072013-06-03 16:45:03 +0000111/// \brief A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000112/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000113struct FormatToken {
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000114 FormatToken() {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000115
116 /// \brief The \c Token.
117 Token Tok;
118
119 /// \brief The number of newlines immediately before the \c Token.
120 ///
121 /// This can be used to determine what the user wrote in the original code
122 /// and thereby e.g. leave an empty line between two function definitions.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000123 unsigned NewlinesBefore = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000124
125 /// \brief Whether there is at least one unescaped newline before the \c
126 /// Token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000127 bool HasUnescapedNewline = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000128
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000129 /// \brief The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000130 SourceRange WhitespaceRange;
131
132 /// \brief The offset just past the last '\n' in this token's leading
133 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000134 unsigned LastNewlineOffset = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000135
Alexander Kornienko39856b72013-09-10 09:38:25 +0000136 /// \brief The width of the non-whitespace parts of the token (or its first
137 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000138 /// We need this to correctly measure number of columns a token spans.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000139 unsigned ColumnWidth = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000140
Alexander Kornienko39856b72013-09-10 09:38:25 +0000141 /// \brief Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000142 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000143 unsigned LastLineColumnWidth = 0;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000144
Alexander Kornienko39856b72013-09-10 09:38:25 +0000145 /// \brief Whether the token text contains newlines (escaped or not).
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000146 bool IsMultiline = false;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000147
Alexander Kornienko4b672072013-06-03 16:45:03 +0000148 /// \brief Indicates that this is the first token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000149 bool IsFirst = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000150
151 /// \brief Whether there must be a line break before this token.
152 ///
153 /// This happens for example when a preprocessor directive ended directly
154 /// before the token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000155 bool MustBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000156
157 /// \brief The raw text of the token.
158 ///
159 /// Contains the raw token text without leading whitespace and without leading
160 /// escaped newlines.
161 StringRef TokenText;
162
Daniel Jasper8369aa52013-07-16 20:28:33 +0000163 /// \brief Set to \c true if this token is an unterminated literal.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000164 bool IsUnterminatedLiteral = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +0000165
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000166 /// \brief Contains the kind of block if this token is a brace.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000167 BraceBlockKind BlockKind = BK_Unknown;
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000168
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000169 TokenType Type = TT_Unknown;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000170
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000171 /// \brief The number of spaces that should be inserted before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000172 unsigned SpacesRequiredBefore = 0;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000173
174 /// \brief \c true if it is allowed to break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000175 bool CanBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000176
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000177 /// \brief \c true if this is the ">" of "template<..>".
178 bool ClosesTemplateDeclaration = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000179
180 /// \brief Number of parameters, if this is "(", "[" or "<".
181 ///
182 /// This is initialized to 1 as we don't need to distinguish functions with
183 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
184 /// the number of commas.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000185 unsigned ParameterCount = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000186
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000187 /// \brief Number of parameters that are nested blocks,
188 /// if this is "(", "[" or "<".
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000189 unsigned BlockParameterCount = 0;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000190
Daniel Jasperde7ca752015-05-04 07:39:00 +0000191 /// \brief If this is a bracket ("<", "(", "[" or "{"), contains the kind of
192 /// the surrounding bracket.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000193 tok::TokenKind ParentBracket = tok::unknown;
Daniel Jasperde7ca752015-05-04 07:39:00 +0000194
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000195 /// \brief A token can have a special role that can carry extra information
196 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000197 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000198
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000199 /// \brief If this is an opening parenthesis, how are the parameters packed?
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000200 ParameterPackingKind PackingKind = PPK_Inconclusive;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000201
Manuel Klimek31c85922013-08-29 15:21:40 +0000202 /// \brief The total length of the unwrapped line up to and including this
203 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000204 unsigned TotalLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000205
Alexander Kornienko39856b72013-09-10 09:38:25 +0000206 /// \brief The original 0-based column of this token, including expanded tabs.
207 /// The configured TabWidth is used as tab width.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000208 unsigned OriginalColumn = 0;
Manuel Klimek31c85922013-08-29 15:21:40 +0000209
Alexander Kornienko4b672072013-06-03 16:45:03 +0000210 /// \brief The length of following tokens until the next natural split point,
211 /// or the next token that can be broken.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000212 unsigned UnbreakableTailLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000213
214 // FIXME: Come up with a 'cleaner' concept.
215 /// \brief The binding strength of a token. This is a combined value of
216 /// operator precedence, parenthesis nesting, etc.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000217 unsigned BindingStrength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000218
Daniel Jasper63af7c42013-12-09 14:40:19 +0000219 /// \brief The nesting level of this token, i.e. the number of surrounding (),
220 /// [], {} or <>.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000221 unsigned NestingLevel = 0;
Daniel Jasper63af7c42013-12-09 14:40:19 +0000222
Alexander Kornienko4b672072013-06-03 16:45:03 +0000223 /// \brief Penalty for inserting a line break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000224 unsigned SplitPenalty = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000225
226 /// \brief If this is the first ObjC selector name in an ObjC method
227 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000228 ///
229 /// This being set to 0 means that the selectors should not be colon-aligned,
230 /// e.g. because several of them are block-type.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000231 unsigned LongestObjCSelectorName = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000232
233 /// \brief Stores the number of required fake parentheses and the
234 /// corresponding operator precedence.
235 ///
236 /// If multiple fake parentheses start at a token, this vector stores them in
237 /// reverse order, i.e. inner fake parenthesis first.
238 SmallVector<prec::Level, 4> FakeLParens;
239 /// \brief Insert this many fake ) after this token for correct indentation.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000240 unsigned FakeRParens = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000241
Daniel Jasper562ecd42013-09-06 08:08:14 +0000242 /// \brief \c true if this token starts a binary expression, i.e. has at least
243 /// one fake l_paren with a precedence greater than prec::Unknown.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000244 bool StartsBinaryExpression = false;
Daniel Jasper562ecd42013-09-06 08:08:14 +0000245 /// \brief \c true if this token ends a binary expression.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000246 bool EndsBinaryExpression = false;
Daniel Jasper562ecd42013-09-06 08:08:14 +0000247
Daniel Jasper0e617842014-04-16 12:26:54 +0000248 /// \brief Is this is an operator (or "."/"->") in a sequence of operators
249 /// with the same precedence, contains the 0-based operator index.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000250 unsigned OperatorIndex = 0;
Daniel Jasper0e617842014-04-16 12:26:54 +0000251
Daniel Jasper00492f92016-01-05 13:03:50 +0000252 /// \brief If this is an operator (or "."/"->") in a sequence of operators
253 /// with the same precedence, points to the next operator.
254 FormatToken *NextOperator = nullptr;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000255
256 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
257 ///
258 /// Only set if \c Type == \c TT_StartOfName.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000259 bool PartOfMultiVariableDeclStmt = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000260
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000261 /// \brief If this is a bracket, this points to the matching one.
262 FormatToken *MatchingParen = nullptr;
263
264 /// \brief The previous token in the unwrapped line.
265 FormatToken *Previous = nullptr;
266
267 /// \brief The next token in the unwrapped line.
268 FormatToken *Next = nullptr;
269
270 /// \brief If this token starts a block, this contains all the unwrapped lines
271 /// in it.
272 SmallVector<AnnotatedLine *, 1> Children;
273
274 /// \brief Stores the formatting decision for the token once it was made.
275 FormatDecision Decision = FD_Unformatted;
276
277 /// \brief If \c true, this token has been fully formatted (indented and
278 /// potentially re-formatted inside), and we do not allow further formatting
279 /// changes.
280 bool Finalized = false;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000281
Alexander Kornienko4b672072013-06-03 16:45:03 +0000282 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
Daniel Jasper8354ea82014-11-21 12:14:12 +0000283 bool is(TokenType TT) const { return Type == TT; }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000284 bool is(const IdentifierInfo *II) const {
285 return II && II == Tok.getIdentifierInfo();
286 }
Daniel Jasperd7884572015-08-14 12:44:06 +0000287 bool is(tok::PPKeywordKind Kind) const {
288 return Tok.getIdentifierInfo() &&
289 Tok.getIdentifierInfo()->getPPKeywordID() == Kind;
290 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000291 template <typename A, typename B> bool isOneOf(A K1, B K2) const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000292 return is(K1) || is(K2);
293 }
Benjamin Kramerc5bc3cd2015-02-15 20:11:14 +0000294 template <typename A, typename B, typename... Ts>
295 bool isOneOf(A K1, B K2, Ts... Ks) const {
296 return is(K1) || isOneOf(K2, Ks...);
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000297 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000298 template <typename T> bool isNot(T Kind) const { return !is(Kind); }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000299
Daniel Jasper04b6a082013-12-20 06:22:01 +0000300 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000301
302 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
303 return Tok.isObjCAtKeyword(Kind);
304 }
305
306 bool isAccessSpecifier(bool ColonRequired = true) const {
307 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
308 (!ColonRequired || (Next && Next->is(tok::colon)));
309 }
310
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000311 /// \brief Determine whether the token is a simple-type-specifier.
312 bool isSimpleTypeSpecifier() const;
313
Alexander Kornienko4b672072013-06-03 16:45:03 +0000314 bool isObjCAccessSpecifier() const {
315 return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
316 Next->isObjCAtKeyword(tok::objc_protected) ||
317 Next->isObjCAtKeyword(tok::objc_package) ||
318 Next->isObjCAtKeyword(tok::objc_private));
319 }
320
321 /// \brief Returns whether \p Tok is ([{ or a template opening <.
322 bool opensScope() const {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000323 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
324 TT_TemplateOpener);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000325 }
Nico Weber0f987a62013-06-25 19:25:12 +0000326 /// \brief Returns whether \p Tok is )]} or a template closing >.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000327 bool closesScope() const {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000328 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
329 TT_TemplateCloser);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000330 }
331
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000332 /// \brief Returns \c true if this is a "." or "->" accessing a member.
333 bool isMemberAccess() const {
Daniel Jasper85bcadc2014-07-09 13:07:57 +0000334 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
Daniel Jasper05cd9292015-03-26 18:46:28 +0000335 !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
336 TT_LambdaArrow);
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000337 }
338
Alexander Kornienko4b672072013-06-03 16:45:03 +0000339 bool isUnaryOperator() const {
340 switch (Tok.getKind()) {
341 case tok::plus:
342 case tok::plusplus:
343 case tok::minus:
344 case tok::minusminus:
345 case tok::exclaim:
346 case tok::tilde:
347 case tok::kw_sizeof:
348 case tok::kw_alignof:
349 return true;
350 default:
351 return false;
352 }
353 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000354
Alexander Kornienko4b672072013-06-03 16:45:03 +0000355 bool isBinaryOperator() const {
356 // Comma is a binary operator, but does not behave as such wrt. formatting.
357 return getPrecedence() > prec::Comma;
358 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000359
Alexander Kornienko4b672072013-06-03 16:45:03 +0000360 bool isTrailingComment() const {
Daniel Jasper610381f2014-08-26 09:37:52 +0000361 return is(tok::comment) &&
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000362 (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000363 }
364
Daniel Jasper78b194992014-08-06 14:15:41 +0000365 /// \brief Returns \c true if this is a keyword that can be used
366 /// like a function call (e.g. sizeof, typeid, ...).
367 bool isFunctionLikeKeyword() const {
368 switch (Tok.getKind()) {
369 case tok::kw_throw:
370 case tok::kw_typeid:
371 case tok::kw_return:
372 case tok::kw_sizeof:
373 case tok::kw_alignof:
374 case tok::kw_alignas:
375 case tok::kw_decltype:
376 case tok::kw_noexcept:
377 case tok::kw_static_assert:
378 case tok::kw___attribute:
379 return true;
380 default:
381 return false;
382 }
383 }
384
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000385 /// \brief Returns actual token start location without leading escaped
386 /// newlines and whitespace.
387 ///
388 /// This can be different to Tok.getLocation(), which includes leading escaped
389 /// newlines.
390 SourceLocation getStartOfNonWhitespace() const {
391 return WhitespaceRange.getEnd();
392 }
393
Alexander Kornienko4b672072013-06-03 16:45:03 +0000394 prec::Level getPrecedence() const {
395 return getBinOpPrecedence(Tok.getKind(), true, true);
396 }
397
398 /// \brief Returns the previous token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000399 FormatToken *getPreviousNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000400 FormatToken *Tok = Previous;
Craig Topper2145bc02014-05-09 08:15:10 +0000401 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000402 Tok = Tok->Previous;
403 return Tok;
404 }
405
406 /// \brief Returns the next token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000407 const FormatToken *getNextNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000408 const FormatToken *Tok = Next;
Craig Topper2145bc02014-05-09 08:15:10 +0000409 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000410 Tok = Tok->Next;
411 return Tok;
412 }
413
Daniel Jasperb8f61682013-10-22 15:45:58 +0000414 /// \brief Returns \c true if this tokens starts a block-type list, i.e. a
415 /// list that should be indented with a block indent.
Daniel Jasperbd73bcf2015-10-27 13:42:08 +0000416 bool opensBlockOrBlockTypeList(const FormatStyle &Style) const {
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000417 return is(TT_ArrayInitializerLSquare) ||
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000418 (is(tok::l_brace) &&
419 (BlockKind == BK_Block || is(TT_DictLiteral) ||
420 (!Style.Cpp11BracedListStyle && NestingLevel == 0)));
Daniel Jasperb8f61682013-10-22 15:45:58 +0000421 }
422
Daniel Jasperbd73bcf2015-10-27 13:42:08 +0000423 /// \brief Same as opensBlockOrBlockTypeList, but for the closing token.
424 bool closesBlockOrBlockTypeList(const FormatStyle &Style) const {
425 return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style);
Daniel Jasper1db6c382013-10-22 15:30:28 +0000426 }
427
Alexander Kornienko4b672072013-06-03 16:45:03 +0000428private:
429 // Disallow copying.
Aaron Ballmanabc18922015-02-15 22:54:08 +0000430 FormatToken(const FormatToken &) = delete;
431 void operator=(const FormatToken &) = delete;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000432};
433
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000434class ContinuationIndenter;
435struct LineState;
436
437class TokenRole {
438public:
439 TokenRole(const FormatStyle &Style) : Style(Style) {}
440 virtual ~TokenRole();
441
442 /// \brief After the \c TokenAnnotator has finished annotating all the tokens,
443 /// this function precomputes required information for formatting.
444 virtual void precomputeFormattingInfos(const FormatToken *Token);
445
446 /// \brief Apply the special formatting that the given role demands.
447 ///
Daniel Jasper01603472014-01-09 13:42:56 +0000448 /// Assumes that the token having this role is already formatted.
449 ///
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000450 /// Continues formatting from \p State leaving indentation to \p Indenter and
451 /// returns the total penalty that this formatting incurs.
Daniel Jasper01603472014-01-09 13:42:56 +0000452 virtual unsigned formatFromToken(LineState &State,
453 ContinuationIndenter *Indenter,
454 bool DryRun) {
455 return 0;
456 }
457
458 /// \brief Same as \c formatFromToken, but assumes that the first token has
459 /// already been set thereby deciding on the first line break.
460 virtual unsigned formatAfterToken(LineState &State,
461 ContinuationIndenter *Indenter,
462 bool DryRun) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000463 return 0;
464 }
465
466 /// \brief Notifies the \c Role that a comma was found.
467 virtual void CommaFound(const FormatToken *Token) {}
468
469protected:
470 const FormatStyle &Style;
471};
472
473class CommaSeparatedList : public TokenRole {
474public:
Daniel Jasper01603472014-01-09 13:42:56 +0000475 CommaSeparatedList(const FormatStyle &Style)
476 : TokenRole(Style), HasNestedBracedList(false) {}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000477
Craig Topperfb6b25b2014-03-15 04:29:04 +0000478 void precomputeFormattingInfos(const FormatToken *Token) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000479
Craig Topperfb6b25b2014-03-15 04:29:04 +0000480 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
481 bool DryRun) override;
Daniel Jasper01603472014-01-09 13:42:56 +0000482
Craig Topperfb6b25b2014-03-15 04:29:04 +0000483 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
484 bool DryRun) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000485
486 /// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000487 void CommaFound(const FormatToken *Token) override {
488 Commas.push_back(Token);
489 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000490
491private:
492 /// \brief A struct that holds information on how to format a given list with
493 /// a specific number of columns.
494 struct ColumnFormat {
495 /// \brief The number of columns to use.
496 unsigned Columns;
497
498 /// \brief The total width in characters.
499 unsigned TotalWidth;
500
501 /// \brief The number of lines required for this format.
502 unsigned LineCount;
503
504 /// \brief The size of each column in characters.
505 SmallVector<unsigned, 8> ColumnSizes;
506 };
507
508 /// \brief Calculate which \c ColumnFormat fits best into
509 /// \p RemainingCharacters.
510 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
511
512 /// \brief The ordered \c FormatTokens making up the commas of this list.
513 SmallVector<const FormatToken *, 8> Commas;
514
515 /// \brief The length of each of the list's items in characters including the
516 /// trailing comma.
517 SmallVector<unsigned, 8> ItemLengths;
518
519 /// \brief Precomputed formats that can be used for this list.
520 SmallVector<ColumnFormat, 4> Formats;
Daniel Jasper01603472014-01-09 13:42:56 +0000521
522 bool HasNestedBracedList;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000523};
524
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000525/// \brief Encapsulates keywords that are context sensitive or for languages not
526/// properly supported by Clang's lexer.
527struct AdditionalKeywords {
528 AdditionalKeywords(IdentifierTable &IdentTable) {
Daniel Jasper5d7f06f2015-08-24 14:28:08 +0000529 kw_final = &IdentTable.get("final");
530 kw_override = &IdentTable.get("override");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000531 kw_in = &IdentTable.get("in");
Daniel Jasperb7fda112016-02-11 13:24:15 +0000532 kw_of = &IdentTable.get("of");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000533 kw_CF_ENUM = &IdentTable.get("CF_ENUM");
534 kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000535 kw_NS_ENUM = &IdentTable.get("NS_ENUM");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000536 kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000537
Martin Probst5f8445b2016-04-24 22:05:09 +0000538 kw_async = &IdentTable.get("async");
539 kw_await = &IdentTable.get("await");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000540 kw_finally = &IdentTable.get("finally");
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +0000541 kw_from = &IdentTable.get("from");
Martin Probst5f8445b2016-04-24 22:05:09 +0000542 kw_function = &IdentTable.get("function");
Daniel Jasper354aa512015-02-19 16:07:32 +0000543 kw_import = &IdentTable.get("import");
Daniel Jasper779c66f2015-12-30 08:00:58 +0000544 kw_is = &IdentTable.get("is");
Daniel Jasper9f642f72015-09-28 14:28:08 +0000545 kw_let = &IdentTable.get("let");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000546 kw_var = &IdentTable.get("var");
Martin Probst5f8445b2016-04-24 22:05:09 +0000547 kw_yield = &IdentTable.get("yield");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000548
Daniel Jasper82c92752014-11-21 12:19:07 +0000549 kw_abstract = &IdentTable.get("abstract");
Nico Weber4f113492015-09-15 23:48:17 +0000550 kw_assert = &IdentTable.get("assert");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000551 kw_extends = &IdentTable.get("extends");
552 kw_implements = &IdentTable.get("implements");
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000553 kw_instanceof = &IdentTable.get("instanceof");
Nico Webera644d7f2014-11-10 16:30:02 +0000554 kw_interface = &IdentTable.get("interface");
Nico Webered501662015-01-13 22:32:50 +0000555 kw_native = &IdentTable.get("native");
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000556 kw_package = &IdentTable.get("package");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000557 kw_synchronized = &IdentTable.get("synchronized");
558 kw_throws = &IdentTable.get("throws");
Nico Weberfac23712015-02-04 15:26:27 +0000559 kw___except = &IdentTable.get("__except");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000560
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000561 kw_mark = &IdentTable.get("mark");
562
Daniel Jasperd8d98392015-11-20 14:32:54 +0000563 kw_extend = &IdentTable.get("extend");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000564 kw_option = &IdentTable.get("option");
565 kw_optional = &IdentTable.get("optional");
566 kw_repeated = &IdentTable.get("repeated");
567 kw_required = &IdentTable.get("required");
568 kw_returns = &IdentTable.get("returns");
Daniel Jasper53395402015-04-07 15:04:40 +0000569
570 kw_signals = &IdentTable.get("signals");
Daniel Jaspera00de632015-12-01 12:05:04 +0000571 kw_qsignals = &IdentTable.get("Q_SIGNALS");
Daniel Jasper53395402015-04-07 15:04:40 +0000572 kw_slots = &IdentTable.get("slots");
573 kw_qslots = &IdentTable.get("Q_SLOTS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000574 }
575
Nico Weberfac23712015-02-04 15:26:27 +0000576 // Context sensitive keywords.
Daniel Jasper5d7f06f2015-08-24 14:28:08 +0000577 IdentifierInfo *kw_final;
578 IdentifierInfo *kw_override;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000579 IdentifierInfo *kw_in;
Daniel Jasperb7fda112016-02-11 13:24:15 +0000580 IdentifierInfo *kw_of;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000581 IdentifierInfo *kw_CF_ENUM;
582 IdentifierInfo *kw_CF_OPTIONS;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000583 IdentifierInfo *kw_NS_ENUM;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000584 IdentifierInfo *kw_NS_OPTIONS;
Nico Weberfac23712015-02-04 15:26:27 +0000585 IdentifierInfo *kw___except;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000586
587 // JavaScript keywords.
Martin Probst5f8445b2016-04-24 22:05:09 +0000588 IdentifierInfo *kw_async;
589 IdentifierInfo *kw_await;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000590 IdentifierInfo *kw_finally;
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +0000591 IdentifierInfo *kw_from;
Martin Probst5f8445b2016-04-24 22:05:09 +0000592 IdentifierInfo *kw_function;
Daniel Jasper354aa512015-02-19 16:07:32 +0000593 IdentifierInfo *kw_import;
Daniel Jasper779c66f2015-12-30 08:00:58 +0000594 IdentifierInfo *kw_is;
Daniel Jasper9f642f72015-09-28 14:28:08 +0000595 IdentifierInfo *kw_let;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000596 IdentifierInfo *kw_var;
Martin Probst5f8445b2016-04-24 22:05:09 +0000597 IdentifierInfo *kw_yield;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000598
599 // Java keywords.
Daniel Jasper82c92752014-11-21 12:19:07 +0000600 IdentifierInfo *kw_abstract;
Nico Weber4f113492015-09-15 23:48:17 +0000601 IdentifierInfo *kw_assert;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000602 IdentifierInfo *kw_extends;
603 IdentifierInfo *kw_implements;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000604 IdentifierInfo *kw_instanceof;
Nico Webera644d7f2014-11-10 16:30:02 +0000605 IdentifierInfo *kw_interface;
Nico Webered501662015-01-13 22:32:50 +0000606 IdentifierInfo *kw_native;
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000607 IdentifierInfo *kw_package;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000608 IdentifierInfo *kw_synchronized;
609 IdentifierInfo *kw_throws;
610
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000611 // Pragma keywords.
612 IdentifierInfo *kw_mark;
613
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000614 // Proto keywords.
Daniel Jasperd8d98392015-11-20 14:32:54 +0000615 IdentifierInfo *kw_extend;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000616 IdentifierInfo *kw_option;
617 IdentifierInfo *kw_optional;
618 IdentifierInfo *kw_repeated;
619 IdentifierInfo *kw_required;
620 IdentifierInfo *kw_returns;
Daniel Jasper53395402015-04-07 15:04:40 +0000621
622 // QT keywords.
623 IdentifierInfo *kw_signals;
Daniel Jaspera00de632015-12-01 12:05:04 +0000624 IdentifierInfo *kw_qsignals;
Daniel Jasper53395402015-04-07 15:04:40 +0000625 IdentifierInfo *kw_slots;
626 IdentifierInfo *kw_qslots;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000627};
628
Alexander Kornienko4b672072013-06-03 16:45:03 +0000629} // namespace format
630} // namespace clang
631
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000632#endif