blob: 6a558875619ac0d1c1651361507c91a132939682 [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>
Martin Probst37a7f912017-07-04 15:30:21 +000024#include <unordered_set>
Alexander Kornienko4b672072013-06-03 16:45:03 +000025
26namespace clang {
27namespace format {
28
Manuel Klimek89628f62017-09-20 09:51:03 +000029#define LIST_TOKEN_TYPES \
30 TYPE(ArrayInitializerLSquare) \
31 TYPE(ArraySubscriptLSquare) \
32 TYPE(AttributeParen) \
33 TYPE(BinaryOperator) \
34 TYPE(BitFieldColon) \
35 TYPE(BlockComment) \
36 TYPE(CastRParen) \
37 TYPE(ConditionalExpr) \
38 TYPE(ConflictAlternative) \
39 TYPE(ConflictEnd) \
40 TYPE(ConflictStart) \
41 TYPE(CtorInitializerColon) \
42 TYPE(CtorInitializerComma) \
43 TYPE(DesignatedInitializerLSquare) \
44 TYPE(DesignatedInitializerPeriod) \
45 TYPE(DictLiteral) \
46 TYPE(ForEachMacro) \
47 TYPE(FunctionAnnotationRParen) \
48 TYPE(FunctionDeclarationName) \
49 TYPE(FunctionLBrace) \
50 TYPE(FunctionTypeLParen) \
51 TYPE(ImplicitStringLiteral) \
52 TYPE(InheritanceColon) \
53 TYPE(InheritanceComma) \
54 TYPE(InlineASMBrace) \
55 TYPE(InlineASMColon) \
56 TYPE(JavaAnnotation) \
57 TYPE(JsComputedPropertyName) \
58 TYPE(JsExponentiation) \
59 TYPE(JsExponentiationEqual) \
60 TYPE(JsFatArrow) \
61 TYPE(JsNonNullAssertion) \
62 TYPE(JsTypeColon) \
63 TYPE(JsTypeOperator) \
64 TYPE(JsTypeOptionalQuestion) \
65 TYPE(LambdaArrow) \
66 TYPE(LambdaLSquare) \
67 TYPE(LeadingJavaAnnotation) \
68 TYPE(LineComment) \
69 TYPE(MacroBlockBegin) \
70 TYPE(MacroBlockEnd) \
71 TYPE(ObjCBlockLBrace) \
72 TYPE(ObjCBlockLParen) \
73 TYPE(ObjCDecl) \
74 TYPE(ObjCForIn) \
75 TYPE(ObjCMethodExpr) \
76 TYPE(ObjCMethodSpecifier) \
77 TYPE(ObjCProperty) \
78 TYPE(ObjCStringLiteral) \
79 TYPE(OverloadedOperator) \
80 TYPE(OverloadedOperatorLParen) \
81 TYPE(PointerOrReference) \
82 TYPE(PureVirtualSpecifier) \
83 TYPE(RangeBasedForLoopColon) \
84 TYPE(RegexLiteral) \
85 TYPE(SelectorName) \
86 TYPE(StartOfName) \
87 TYPE(StructuredBindingLSquare) \
88 TYPE(TemplateCloser) \
89 TYPE(TemplateOpener) \
90 TYPE(TemplateString) \
Krasimir Georgiev4e2906482018-02-13 10:20:39 +000091 TYPE(ProtoExtensionLSquare) \
Manuel Klimek89628f62017-09-20 09:51:03 +000092 TYPE(TrailingAnnotation) \
93 TYPE(TrailingReturnArrow) \
94 TYPE(TrailingUnaryOperator) \
95 TYPE(UnaryOperator) \
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +000096 TYPE(Unknown)
97
Alexander Kornienko4b672072013-06-03 16:45:03 +000098enum TokenType {
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +000099#define TYPE(X) TT_##X,
Manuel Klimek89628f62017-09-20 09:51:03 +0000100 LIST_TOKEN_TYPES
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +0000101#undef TYPE
Manuel Klimek89628f62017-09-20 09:51:03 +0000102 NUM_TOKEN_TYPES
Alexander Kornienko4b672072013-06-03 16:45:03 +0000103};
104
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +0000105/// \brief Determines the name of a token type.
106const char *getTokenTypeName(TokenType Type);
107
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000108// Represents what type of block a set of braces open.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000109enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit };
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000110
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000111// The packing kind of a function's parameters.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000112enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive };
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000113
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000114enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break };
Manuel Klimek71814b42013-10-11 21:25:45 +0000115
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000116class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000117class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000118
Alexander Kornienko4b672072013-06-03 16:45:03 +0000119/// \brief A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000120/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000121struct FormatToken {
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000122 FormatToken() {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000123
124 /// \brief The \c Token.
125 Token Tok;
126
127 /// \brief The number of newlines immediately before the \c Token.
128 ///
129 /// This can be used to determine what the user wrote in the original code
130 /// and thereby e.g. leave an empty line between two function definitions.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000131 unsigned NewlinesBefore = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000132
133 /// \brief Whether there is at least one unescaped newline before the \c
134 /// Token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000135 bool HasUnescapedNewline = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000136
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000137 /// \brief The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000138 SourceRange WhitespaceRange;
139
140 /// \brief The offset just past the last '\n' in this token's leading
141 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000142 unsigned LastNewlineOffset = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000143
Alexander Kornienko39856b72013-09-10 09:38:25 +0000144 /// \brief The width of the non-whitespace parts of the token (or its first
145 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000146 /// We need this to correctly measure number of columns a token spans.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000147 unsigned ColumnWidth = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000148
Alexander Kornienko39856b72013-09-10 09:38:25 +0000149 /// \brief Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000150 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000151 unsigned LastLineColumnWidth = 0;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000152
Alexander Kornienko39856b72013-09-10 09:38:25 +0000153 /// \brief Whether the token text contains newlines (escaped or not).
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000154 bool IsMultiline = false;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000155
Martin Probst7ea9b6d2016-05-29 14:41:36 +0000156 /// \brief Indicates that this is the first token of the file.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000157 bool IsFirst = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000158
159 /// \brief Whether there must be a line break before this token.
160 ///
161 /// This happens for example when a preprocessor directive ended directly
162 /// before the token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000163 bool MustBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000164
165 /// \brief The raw text of the token.
166 ///
167 /// Contains the raw token text without leading whitespace and without leading
168 /// escaped newlines.
169 StringRef TokenText;
170
Daniel Jasper8369aa52013-07-16 20:28:33 +0000171 /// \brief Set to \c true if this token is an unterminated literal.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000172 bool IsUnterminatedLiteral = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +0000173
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000174 /// \brief Contains the kind of block if this token is a brace.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000175 BraceBlockKind BlockKind = BK_Unknown;
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000176
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000177 TokenType Type = TT_Unknown;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000178
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000179 /// \brief The number of spaces that should be inserted before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000180 unsigned SpacesRequiredBefore = 0;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000181
182 /// \brief \c true if it is allowed to break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000183 bool CanBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000184
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000185 /// \brief \c true if this is the ">" of "template<..>".
186 bool ClosesTemplateDeclaration = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000187
188 /// \brief Number of parameters, if this is "(", "[" or "<".
189 ///
190 /// This is initialized to 1 as we don't need to distinguish functions with
191 /// 0 parameters from functions with 1 parameter. Thus, we can simply count
192 /// the number of commas.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000193 unsigned ParameterCount = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000194
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000195 /// \brief Number of parameters that are nested blocks,
196 /// if this is "(", "[" or "<".
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000197 unsigned BlockParameterCount = 0;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000198
Daniel Jasperde7ca752015-05-04 07:39:00 +0000199 /// \brief If this is a bracket ("<", "(", "[" or "{"), contains the kind of
200 /// the surrounding bracket.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000201 tok::TokenKind ParentBracket = tok::unknown;
Daniel Jasperde7ca752015-05-04 07:39:00 +0000202
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000203 /// \brief A token can have a special role that can carry extra information
204 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000205 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000206
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000207 /// \brief If this is an opening parenthesis, how are the parameters packed?
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000208 ParameterPackingKind PackingKind = PPK_Inconclusive;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000209
Manuel Klimek31c85922013-08-29 15:21:40 +0000210 /// \brief The total length of the unwrapped line up to and including this
211 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000212 unsigned TotalLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000213
Alexander Kornienko39856b72013-09-10 09:38:25 +0000214 /// \brief The original 0-based column of this token, including expanded tabs.
215 /// The configured TabWidth is used as tab width.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000216 unsigned OriginalColumn = 0;
Manuel Klimek31c85922013-08-29 15:21:40 +0000217
Alexander Kornienko4b672072013-06-03 16:45:03 +0000218 /// \brief The length of following tokens until the next natural split point,
219 /// or the next token that can be broken.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000220 unsigned UnbreakableTailLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000221
222 // FIXME: Come up with a 'cleaner' concept.
223 /// \brief The binding strength of a token. This is a combined value of
224 /// operator precedence, parenthesis nesting, etc.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000225 unsigned BindingStrength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000226
Daniel Jasper63af7c42013-12-09 14:40:19 +0000227 /// \brief The nesting level of this token, i.e. the number of surrounding (),
228 /// [], {} or <>.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000229 unsigned NestingLevel = 0;
Daniel Jasper63af7c42013-12-09 14:40:19 +0000230
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000231 /// \brief The indent level of this token. Copied from the surrounding line.
232 unsigned IndentLevel = 0;
233
Alexander Kornienko4b672072013-06-03 16:45:03 +0000234 /// \brief Penalty for inserting a line break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000235 unsigned SplitPenalty = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000236
237 /// \brief If this is the first ObjC selector name in an ObjC method
238 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000239 ///
240 /// This being set to 0 means that the selectors should not be colon-aligned,
241 /// e.g. because several of them are block-type.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000242 unsigned LongestObjCSelectorName = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000243
Jacek Olesiakfb7f5c02018-02-07 10:35:08 +0000244 /// \brief How many parts ObjC selector have (i.e. how many parameters method
245 /// has).
246 unsigned ObjCSelectorNameParts = 0;
247
Alexander Kornienko4b672072013-06-03 16:45:03 +0000248 /// \brief Stores the number of required fake parentheses and the
249 /// corresponding operator precedence.
250 ///
251 /// If multiple fake parentheses start at a token, this vector stores them in
252 /// reverse order, i.e. inner fake parenthesis first.
253 SmallVector<prec::Level, 4> FakeLParens;
254 /// \brief Insert this many fake ) after this token for correct indentation.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000255 unsigned FakeRParens = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000256
Daniel Jasper562ecd42013-09-06 08:08:14 +0000257 /// \brief \c true if this token starts a binary expression, i.e. has at least
258 /// one fake l_paren with a precedence greater than prec::Unknown.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000259 bool StartsBinaryExpression = false;
Daniel Jasper562ecd42013-09-06 08:08:14 +0000260 /// \brief \c true if this token ends a binary expression.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000261 bool EndsBinaryExpression = false;
Daniel Jasper562ecd42013-09-06 08:08:14 +0000262
Daniel Jasper0e617842014-04-16 12:26:54 +0000263 /// \brief Is this is an operator (or "."/"->") in a sequence of operators
264 /// with the same precedence, contains the 0-based operator index.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000265 unsigned OperatorIndex = 0;
Daniel Jasper0e617842014-04-16 12:26:54 +0000266
Daniel Jasper00492f92016-01-05 13:03:50 +0000267 /// \brief If this is an operator (or "."/"->") in a sequence of operators
268 /// with the same precedence, points to the next operator.
269 FormatToken *NextOperator = nullptr;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000270
271 /// \brief Is this token part of a \c DeclStmt defining multiple variables?
272 ///
273 /// Only set if \c Type == \c TT_StartOfName.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000274 bool PartOfMultiVariableDeclStmt = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000275
Krasimir Georgievb6ccd382017-02-02 14:36:50 +0000276 /// \brief Does this line comment continue a line comment section?
277 ///
278 /// Only set to true if \c Type == \c TT_LineComment.
279 bool ContinuesLineCommentSection = false;
280
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000281 /// \brief If this is a bracket, this points to the matching one.
282 FormatToken *MatchingParen = nullptr;
283
284 /// \brief The previous token in the unwrapped line.
285 FormatToken *Previous = nullptr;
286
287 /// \brief The next token in the unwrapped line.
288 FormatToken *Next = nullptr;
289
290 /// \brief If this token starts a block, this contains all the unwrapped lines
291 /// in it.
292 SmallVector<AnnotatedLine *, 1> Children;
293
294 /// \brief Stores the formatting decision for the token once it was made.
295 FormatDecision Decision = FD_Unformatted;
296
297 /// \brief If \c true, this token has been fully formatted (indented and
298 /// potentially re-formatted inside), and we do not allow further formatting
299 /// changes.
300 bool Finalized = false;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000301
Alexander Kornienko4b672072013-06-03 16:45:03 +0000302 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
Daniel Jasper8354ea82014-11-21 12:14:12 +0000303 bool is(TokenType TT) const { return Type == TT; }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000304 bool is(const IdentifierInfo *II) const {
305 return II && II == Tok.getIdentifierInfo();
306 }
Daniel Jasperd7884572015-08-14 12:44:06 +0000307 bool is(tok::PPKeywordKind Kind) const {
308 return Tok.getIdentifierInfo() &&
309 Tok.getIdentifierInfo()->getPPKeywordID() == Kind;
310 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000311 template <typename A, typename B> bool isOneOf(A K1, B K2) const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000312 return is(K1) || is(K2);
313 }
Benjamin Kramerc5bc3cd2015-02-15 20:11:14 +0000314 template <typename A, typename B, typename... Ts>
315 bool isOneOf(A K1, B K2, Ts... Ks) const {
316 return is(K1) || isOneOf(K2, Ks...);
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000317 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000318 template <typename T> bool isNot(T Kind) const { return !is(Kind); }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000319
Martin Probst1244ecb2016-05-29 14:41:02 +0000320 /// \c true if this token starts a sequence with the given tokens in order,
321 /// following the ``Next`` pointers, ignoring comments.
322 template <typename A, typename... Ts>
323 bool startsSequence(A K1, Ts... Tokens) const {
324 return startsSequenceInternal(K1, Tokens...);
325 }
326
327 /// \c true if this token ends a sequence with the given tokens in order,
328 /// following the ``Previous`` pointers, ignoring comments.
329 template <typename A, typename... Ts>
330 bool endsSequence(A K1, Ts... Tokens) const {
331 return endsSequenceInternal(K1, Tokens...);
332 }
333
Daniel Jasper04b6a082013-12-20 06:22:01 +0000334 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000335
336 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
337 return Tok.isObjCAtKeyword(Kind);
338 }
339
340 bool isAccessSpecifier(bool ColonRequired = true) const {
341 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
342 (!ColonRequired || (Next && Next->is(tok::colon)));
343 }
344
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000345 /// \brief Determine whether the token is a simple-type-specifier.
346 bool isSimpleTypeSpecifier() const;
347
Alexander Kornienko4b672072013-06-03 16:45:03 +0000348 bool isObjCAccessSpecifier() const {
Manuel Klimek89628f62017-09-20 09:51:03 +0000349 return is(tok::at) && Next &&
350 (Next->isObjCAtKeyword(tok::objc_public) ||
351 Next->isObjCAtKeyword(tok::objc_protected) ||
352 Next->isObjCAtKeyword(tok::objc_package) ||
353 Next->isObjCAtKeyword(tok::objc_private));
Alexander Kornienko4b672072013-06-03 16:45:03 +0000354 }
355
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000356 /// \brief Returns whether \p Tok is ([{ or an opening < of a template or in
357 /// protos.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000358 bool opensScope() const {
Daniel Jasper24de6fb2017-01-31 13:03:07 +0000359 if (is(TT_TemplateString) && TokenText.endswith("${"))
360 return true;
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000361 if (is(TT_DictLiteral) && is(tok::less))
362 return true;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000363 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
364 TT_TemplateOpener);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000365 }
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000366 /// \brief Returns whether \p Tok is )]} or a closing > of a template or in
367 /// protos.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000368 bool closesScope() const {
Daniel Jasper24de6fb2017-01-31 13:03:07 +0000369 if (is(TT_TemplateString) && TokenText.startswith("}"))
370 return true;
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000371 if (is(TT_DictLiteral) && is(tok::greater))
372 return true;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000373 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
374 TT_TemplateCloser);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000375 }
376
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000377 /// \brief Returns \c true if this is a "." or "->" accessing a member.
378 bool isMemberAccess() const {
Daniel Jasper85bcadc2014-07-09 13:07:57 +0000379 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
Daniel Jasper05cd9292015-03-26 18:46:28 +0000380 !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
381 TT_LambdaArrow);
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000382 }
383
Alexander Kornienko4b672072013-06-03 16:45:03 +0000384 bool isUnaryOperator() const {
385 switch (Tok.getKind()) {
386 case tok::plus:
387 case tok::plusplus:
388 case tok::minus:
389 case tok::minusminus:
390 case tok::exclaim:
391 case tok::tilde:
392 case tok::kw_sizeof:
393 case tok::kw_alignof:
394 return true;
395 default:
396 return false;
397 }
398 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000399
Alexander Kornienko4b672072013-06-03 16:45:03 +0000400 bool isBinaryOperator() const {
401 // Comma is a binary operator, but does not behave as such wrt. formatting.
402 return getPrecedence() > prec::Comma;
403 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000404
Alexander Kornienko4b672072013-06-03 16:45:03 +0000405 bool isTrailingComment() const {
Daniel Jasper610381f2014-08-26 09:37:52 +0000406 return is(tok::comment) &&
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000407 (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000408 }
409
Daniel Jasper78b194992014-08-06 14:15:41 +0000410 /// \brief Returns \c true if this is a keyword that can be used
411 /// like a function call (e.g. sizeof, typeid, ...).
412 bool isFunctionLikeKeyword() const {
413 switch (Tok.getKind()) {
414 case tok::kw_throw:
415 case tok::kw_typeid:
416 case tok::kw_return:
417 case tok::kw_sizeof:
418 case tok::kw_alignof:
419 case tok::kw_alignas:
420 case tok::kw_decltype:
421 case tok::kw_noexcept:
422 case tok::kw_static_assert:
423 case tok::kw___attribute:
424 return true;
425 default:
426 return false;
427 }
428 }
429
Daniel Jasper7209bb92016-12-13 11:16:42 +0000430 /// \brief Returns \c true if this is a string literal that's like a label,
431 /// e.g. ends with "=" or ":".
432 bool isLabelString() const {
433 if (!is(tok::string_literal))
434 return false;
435 StringRef Content = TokenText;
436 if (Content.startswith("\"") || Content.startswith("'"))
437 Content = Content.drop_front(1);
438 if (Content.endswith("\"") || Content.endswith("'"))
439 Content = Content.drop_back(1);
440 Content = Content.trim();
441 return Content.size() > 1 &&
442 (Content.back() == ':' || Content.back() == '=');
443 }
444
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000445 /// \brief Returns actual token start location without leading escaped
446 /// newlines and whitespace.
447 ///
448 /// This can be different to Tok.getLocation(), which includes leading escaped
449 /// newlines.
450 SourceLocation getStartOfNonWhitespace() const {
451 return WhitespaceRange.getEnd();
452 }
453
Alexander Kornienko4b672072013-06-03 16:45:03 +0000454 prec::Level getPrecedence() const {
Nico Weber47867e32018-01-23 17:29:41 +0000455 return getBinOpPrecedence(Tok.getKind(), /*GreaterThanIsOperator=*/true,
456 /*CPlusPlus11=*/true);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000457 }
458
459 /// \brief Returns the previous token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000460 FormatToken *getPreviousNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000461 FormatToken *Tok = Previous;
Craig Topper2145bc02014-05-09 08:15:10 +0000462 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000463 Tok = Tok->Previous;
464 return Tok;
465 }
466
467 /// \brief Returns the next token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000468 const FormatToken *getNextNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000469 const FormatToken *Tok = Next;
Craig Topper2145bc02014-05-09 08:15:10 +0000470 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000471 Tok = Tok->Next;
472 return Tok;
473 }
474
Daniel Jasperb8f61682013-10-22 15:45:58 +0000475 /// \brief Returns \c true if this tokens starts a block-type list, i.e. a
476 /// list that should be indented with a block indent.
Daniel Jasperbd73bcf2015-10-27 13:42:08 +0000477 bool opensBlockOrBlockTypeList(const FormatStyle &Style) const {
Daniel Jasper98e0b122017-02-20 14:51:16 +0000478 if (is(TT_TemplateString) && opensScope())
479 return true;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000480 return is(TT_ArrayInitializerLSquare) ||
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000481 (is(tok::l_brace) &&
482 (BlockKind == BK_Block || is(TT_DictLiteral) ||
Krasimir Georgievff747be2017-06-27 13:43:07 +0000483 (!Style.Cpp11BracedListStyle && NestingLevel == 0))) ||
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000484 (is(tok::less) && (Style.Language == FormatStyle::LK_Proto ||
485 Style.Language == FormatStyle::LK_TextProto));
Daniel Jasperb8f61682013-10-22 15:45:58 +0000486 }
487
Manuel Klimeke411aa82017-09-20 09:29:37 +0000488 /// \brief Returns whether the token is the left square bracket of a C++
489 /// structured binding declaration.
490 bool isCppStructuredBinding(const FormatStyle &Style) const {
491 if (!Style.isCpp() || isNot(tok::l_square))
492 return false;
Manuel Klimek89628f62017-09-20 09:51:03 +0000493 const FormatToken *T = this;
Manuel Klimeke411aa82017-09-20 09:29:37 +0000494 do {
495 T = T->getPreviousNonComment();
496 } while (T && T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
497 tok::ampamp));
498 return T && T->is(tok::kw_auto);
499 }
500
Daniel Jasperbd73bcf2015-10-27 13:42:08 +0000501 /// \brief Same as opensBlockOrBlockTypeList, but for the closing token.
502 bool closesBlockOrBlockTypeList(const FormatStyle &Style) const {
Daniel Jasper98e0b122017-02-20 14:51:16 +0000503 if (is(TT_TemplateString) && closesScope())
504 return true;
Daniel Jasperbd73bcf2015-10-27 13:42:08 +0000505 return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style);
Daniel Jasper1db6c382013-10-22 15:30:28 +0000506 }
507
Francois Ferrandad722562017-06-30 20:25:55 +0000508 /// \brief Return the actual namespace token, if this token starts a namespace
509 /// block.
510 const FormatToken *getNamespaceToken() const {
511 const FormatToken *NamespaceTok = this;
512 if (is(tok::comment))
513 NamespaceTok = NamespaceTok->getNextNonComment();
514 // Detect "(inline)? namespace" in the beginning of a line.
515 if (NamespaceTok && NamespaceTok->is(tok::kw_inline))
516 NamespaceTok = NamespaceTok->getNextNonComment();
517 return NamespaceTok && NamespaceTok->is(tok::kw_namespace) ? NamespaceTok
518 : nullptr;
519 }
520
Alexander Kornienko4b672072013-06-03 16:45:03 +0000521private:
522 // Disallow copying.
Aaron Ballmanabc18922015-02-15 22:54:08 +0000523 FormatToken(const FormatToken &) = delete;
524 void operator=(const FormatToken &) = delete;
Martin Probst1244ecb2016-05-29 14:41:02 +0000525
526 template <typename A, typename... Ts>
527 bool startsSequenceInternal(A K1, Ts... Tokens) const {
528 if (is(tok::comment) && Next)
529 return Next->startsSequenceInternal(K1, Tokens...);
530 return is(K1) && Next && Next->startsSequenceInternal(Tokens...);
531 }
532
Manuel Klimek89628f62017-09-20 09:51:03 +0000533 template <typename A> bool startsSequenceInternal(A K1) const {
Martin Probst1244ecb2016-05-29 14:41:02 +0000534 if (is(tok::comment) && Next)
535 return Next->startsSequenceInternal(K1);
536 return is(K1);
537 }
538
Manuel Klimek89628f62017-09-20 09:51:03 +0000539 template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const {
Martin Probst1244ecb2016-05-29 14:41:02 +0000540 if (is(tok::comment) && Previous)
541 return Previous->endsSequenceInternal(K1);
542 return is(K1);
543 }
544
545 template <typename A, typename... Ts>
546 bool endsSequenceInternal(A K1, Ts... Tokens) const {
547 if (is(tok::comment) && Previous)
548 return Previous->endsSequenceInternal(K1, Tokens...);
549 return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...);
550 }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000551};
552
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000553class ContinuationIndenter;
554struct LineState;
555
556class TokenRole {
557public:
558 TokenRole(const FormatStyle &Style) : Style(Style) {}
559 virtual ~TokenRole();
560
561 /// \brief After the \c TokenAnnotator has finished annotating all the tokens,
562 /// this function precomputes required information for formatting.
563 virtual void precomputeFormattingInfos(const FormatToken *Token);
564
565 /// \brief Apply the special formatting that the given role demands.
566 ///
Daniel Jasper01603472014-01-09 13:42:56 +0000567 /// Assumes that the token having this role is already formatted.
568 ///
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000569 /// Continues formatting from \p State leaving indentation to \p Indenter and
570 /// returns the total penalty that this formatting incurs.
Daniel Jasper01603472014-01-09 13:42:56 +0000571 virtual unsigned formatFromToken(LineState &State,
572 ContinuationIndenter *Indenter,
573 bool DryRun) {
574 return 0;
575 }
576
577 /// \brief Same as \c formatFromToken, but assumes that the first token has
578 /// already been set thereby deciding on the first line break.
579 virtual unsigned formatAfterToken(LineState &State,
580 ContinuationIndenter *Indenter,
581 bool DryRun) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000582 return 0;
583 }
584
585 /// \brief Notifies the \c Role that a comma was found.
586 virtual void CommaFound(const FormatToken *Token) {}
587
588protected:
589 const FormatStyle &Style;
590};
591
592class CommaSeparatedList : public TokenRole {
593public:
Daniel Jasper01603472014-01-09 13:42:56 +0000594 CommaSeparatedList(const FormatStyle &Style)
595 : TokenRole(Style), HasNestedBracedList(false) {}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000596
Craig Topperfb6b25b2014-03-15 04:29:04 +0000597 void precomputeFormattingInfos(const FormatToken *Token) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000598
Craig Topperfb6b25b2014-03-15 04:29:04 +0000599 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
600 bool DryRun) override;
Daniel Jasper01603472014-01-09 13:42:56 +0000601
Craig Topperfb6b25b2014-03-15 04:29:04 +0000602 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
603 bool DryRun) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000604
605 /// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000606 void CommaFound(const FormatToken *Token) override {
607 Commas.push_back(Token);
608 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000609
610private:
611 /// \brief A struct that holds information on how to format a given list with
612 /// a specific number of columns.
613 struct ColumnFormat {
614 /// \brief The number of columns to use.
615 unsigned Columns;
616
617 /// \brief The total width in characters.
618 unsigned TotalWidth;
619
620 /// \brief The number of lines required for this format.
621 unsigned LineCount;
622
623 /// \brief The size of each column in characters.
624 SmallVector<unsigned, 8> ColumnSizes;
625 };
626
627 /// \brief Calculate which \c ColumnFormat fits best into
628 /// \p RemainingCharacters.
629 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
630
631 /// \brief The ordered \c FormatTokens making up the commas of this list.
632 SmallVector<const FormatToken *, 8> Commas;
633
634 /// \brief The length of each of the list's items in characters including the
635 /// trailing comma.
636 SmallVector<unsigned, 8> ItemLengths;
637
638 /// \brief Precomputed formats that can be used for this list.
639 SmallVector<ColumnFormat, 4> Formats;
Daniel Jasper01603472014-01-09 13:42:56 +0000640
641 bool HasNestedBracedList;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000642};
643
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000644/// \brief Encapsulates keywords that are context sensitive or for languages not
645/// properly supported by Clang's lexer.
646struct AdditionalKeywords {
647 AdditionalKeywords(IdentifierTable &IdentTable) {
Daniel Jasper5d7f06f2015-08-24 14:28:08 +0000648 kw_final = &IdentTable.get("final");
649 kw_override = &IdentTable.get("override");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000650 kw_in = &IdentTable.get("in");
Daniel Jasperb7fda112016-02-11 13:24:15 +0000651 kw_of = &IdentTable.get("of");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000652 kw_CF_ENUM = &IdentTable.get("CF_ENUM");
653 kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000654 kw_NS_ENUM = &IdentTable.get("NS_ENUM");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000655 kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000656
Martin Probstc4a0dd42016-05-20 11:24:24 +0000657 kw_as = &IdentTable.get("as");
Martin Probst5f8445b2016-04-24 22:05:09 +0000658 kw_async = &IdentTable.get("async");
659 kw_await = &IdentTable.get("await");
Martin Probst72fd75a2016-11-10 16:20:58 +0000660 kw_declare = &IdentTable.get("declare");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000661 kw_finally = &IdentTable.get("finally");
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +0000662 kw_from = &IdentTable.get("from");
Martin Probst5f8445b2016-04-24 22:05:09 +0000663 kw_function = &IdentTable.get("function");
Martin Probst19c7de02017-04-26 12:34:15 +0000664 kw_get = &IdentTable.get("get");
Daniel Jasper354aa512015-02-19 16:07:32 +0000665 kw_import = &IdentTable.get("import");
Daniel Jasper779c66f2015-12-30 08:00:58 +0000666 kw_is = &IdentTable.get("is");
Daniel Jasper9f642f72015-09-28 14:28:08 +0000667 kw_let = &IdentTable.get("let");
Martin Probst72fd75a2016-11-10 16:20:58 +0000668 kw_module = &IdentTable.get("module");
Martin Probsta81dd0b2017-07-07 13:17:10 +0000669 kw_readonly = &IdentTable.get("readonly");
Martin Probst19c7de02017-04-26 12:34:15 +0000670 kw_set = &IdentTable.get("set");
Martin Probst1b7f9802016-06-23 19:52:32 +0000671 kw_type = &IdentTable.get("type");
Martin Probst9926abb2017-08-01 17:42:16 +0000672 kw_typeof = &IdentTable.get("typeof");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000673 kw_var = &IdentTable.get("var");
Martin Probst5f8445b2016-04-24 22:05:09 +0000674 kw_yield = &IdentTable.get("yield");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000675
Daniel Jasper82c92752014-11-21 12:19:07 +0000676 kw_abstract = &IdentTable.get("abstract");
Nico Weber4f113492015-09-15 23:48:17 +0000677 kw_assert = &IdentTable.get("assert");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000678 kw_extends = &IdentTable.get("extends");
679 kw_implements = &IdentTable.get("implements");
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000680 kw_instanceof = &IdentTable.get("instanceof");
Nico Webera644d7f2014-11-10 16:30:02 +0000681 kw_interface = &IdentTable.get("interface");
Nico Webered501662015-01-13 22:32:50 +0000682 kw_native = &IdentTable.get("native");
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000683 kw_package = &IdentTable.get("package");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000684 kw_synchronized = &IdentTable.get("synchronized");
685 kw_throws = &IdentTable.get("throws");
Nico Weberfac23712015-02-04 15:26:27 +0000686 kw___except = &IdentTable.get("__except");
Nico Weber21088802017-02-10 19:36:52 +0000687 kw___has_include = &IdentTable.get("__has_include");
688 kw___has_include_next = &IdentTable.get("__has_include_next");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000689
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000690 kw_mark = &IdentTable.get("mark");
691
Daniel Jasperd8d98392015-11-20 14:32:54 +0000692 kw_extend = &IdentTable.get("extend");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000693 kw_option = &IdentTable.get("option");
694 kw_optional = &IdentTable.get("optional");
695 kw_repeated = &IdentTable.get("repeated");
696 kw_required = &IdentTable.get("required");
697 kw_returns = &IdentTable.get("returns");
Daniel Jasper53395402015-04-07 15:04:40 +0000698
699 kw_signals = &IdentTable.get("signals");
Daniel Jaspera00de632015-12-01 12:05:04 +0000700 kw_qsignals = &IdentTable.get("Q_SIGNALS");
Daniel Jasper53395402015-04-07 15:04:40 +0000701 kw_slots = &IdentTable.get("slots");
702 kw_qslots = &IdentTable.get("Q_SLOTS");
Krasimir Georgiev835ac9b2017-07-05 12:24:01 +0000703
704 // Keep this at the end of the constructor to make sure everything here is
705 // already initialized.
706 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
707 {kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
Martin Probsta81dd0b2017-07-07 13:17:10 +0000708 kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_readonly,
Martin Probst9926abb2017-08-01 17:42:16 +0000709 kw_set, kw_type, kw_typeof, kw_var, kw_yield,
Krasimir Georgiev835ac9b2017-07-05 12:24:01 +0000710 // Keywords from the Java section.
711 kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000712 }
713
Nico Weberfac23712015-02-04 15:26:27 +0000714 // Context sensitive keywords.
Daniel Jasper5d7f06f2015-08-24 14:28:08 +0000715 IdentifierInfo *kw_final;
716 IdentifierInfo *kw_override;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000717 IdentifierInfo *kw_in;
Daniel Jasperb7fda112016-02-11 13:24:15 +0000718 IdentifierInfo *kw_of;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000719 IdentifierInfo *kw_CF_ENUM;
720 IdentifierInfo *kw_CF_OPTIONS;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000721 IdentifierInfo *kw_NS_ENUM;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000722 IdentifierInfo *kw_NS_OPTIONS;
Nico Weberfac23712015-02-04 15:26:27 +0000723 IdentifierInfo *kw___except;
Nico Weber21088802017-02-10 19:36:52 +0000724 IdentifierInfo *kw___has_include;
725 IdentifierInfo *kw___has_include_next;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000726
727 // JavaScript keywords.
Martin Probstc4a0dd42016-05-20 11:24:24 +0000728 IdentifierInfo *kw_as;
Martin Probst5f8445b2016-04-24 22:05:09 +0000729 IdentifierInfo *kw_async;
730 IdentifierInfo *kw_await;
Martin Probst72fd75a2016-11-10 16:20:58 +0000731 IdentifierInfo *kw_declare;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000732 IdentifierInfo *kw_finally;
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +0000733 IdentifierInfo *kw_from;
Martin Probst5f8445b2016-04-24 22:05:09 +0000734 IdentifierInfo *kw_function;
Martin Probst19c7de02017-04-26 12:34:15 +0000735 IdentifierInfo *kw_get;
Daniel Jasper354aa512015-02-19 16:07:32 +0000736 IdentifierInfo *kw_import;
Daniel Jasper779c66f2015-12-30 08:00:58 +0000737 IdentifierInfo *kw_is;
Daniel Jasper9f642f72015-09-28 14:28:08 +0000738 IdentifierInfo *kw_let;
Martin Probst72fd75a2016-11-10 16:20:58 +0000739 IdentifierInfo *kw_module;
Martin Probsta81dd0b2017-07-07 13:17:10 +0000740 IdentifierInfo *kw_readonly;
Martin Probst19c7de02017-04-26 12:34:15 +0000741 IdentifierInfo *kw_set;
Martin Probst1b7f9802016-06-23 19:52:32 +0000742 IdentifierInfo *kw_type;
Martin Probst9926abb2017-08-01 17:42:16 +0000743 IdentifierInfo *kw_typeof;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000744 IdentifierInfo *kw_var;
Martin Probst5f8445b2016-04-24 22:05:09 +0000745 IdentifierInfo *kw_yield;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000746
747 // Java keywords.
Daniel Jasper82c92752014-11-21 12:19:07 +0000748 IdentifierInfo *kw_abstract;
Nico Weber4f113492015-09-15 23:48:17 +0000749 IdentifierInfo *kw_assert;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000750 IdentifierInfo *kw_extends;
751 IdentifierInfo *kw_implements;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000752 IdentifierInfo *kw_instanceof;
Nico Webera644d7f2014-11-10 16:30:02 +0000753 IdentifierInfo *kw_interface;
Nico Webered501662015-01-13 22:32:50 +0000754 IdentifierInfo *kw_native;
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000755 IdentifierInfo *kw_package;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000756 IdentifierInfo *kw_synchronized;
757 IdentifierInfo *kw_throws;
758
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000759 // Pragma keywords.
760 IdentifierInfo *kw_mark;
761
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000762 // Proto keywords.
Daniel Jasperd8d98392015-11-20 14:32:54 +0000763 IdentifierInfo *kw_extend;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000764 IdentifierInfo *kw_option;
765 IdentifierInfo *kw_optional;
766 IdentifierInfo *kw_repeated;
767 IdentifierInfo *kw_required;
768 IdentifierInfo *kw_returns;
Daniel Jasper53395402015-04-07 15:04:40 +0000769
770 // QT keywords.
771 IdentifierInfo *kw_signals;
Daniel Jaspera00de632015-12-01 12:05:04 +0000772 IdentifierInfo *kw_qsignals;
Daniel Jasper53395402015-04-07 15:04:40 +0000773 IdentifierInfo *kw_slots;
774 IdentifierInfo *kw_qslots;
Martin Probst37a7f912017-07-04 15:30:21 +0000775
776 /// \brief Returns \c true if \p Tok is a true JavaScript identifier, returns
777 /// \c false if it is a keyword or a pseudo keyword.
778 bool IsJavaScriptIdentifier(const FormatToken &Tok) const {
779 return Tok.is(tok::identifier) &&
780 JsExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
781 JsExtraKeywords.end();
782 }
783
784private:
785 /// \brief The JavaScript keywords beyond the C++ keyword set.
786 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000787};
788
Alexander Kornienko4b672072013-06-03 16:45:03 +0000789} // namespace format
790} // namespace clang
791
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000792#endif