blob: 10390c42911b87ddd8109c79e4e8d8bd9826ed1d [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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000011/// This file contains the declaration of the FormatToken, a wrapper
Alexander Kornienko4b672072013-06-03 16:45:03 +000012/// 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) \
Ben Hamiltonb060ad82018-03-12 15:42:38 +000032 TYPE(AttributeColon) \
Manuel Klimek89628f62017-09-20 09:51:03 +000033 TYPE(AttributeParen) \
Ben Hamiltonb060ad82018-03-12 15:42:38 +000034 TYPE(AttributeSquare) \
Manuel Klimek89628f62017-09-20 09:51:03 +000035 TYPE(BinaryOperator) \
36 TYPE(BitFieldColon) \
37 TYPE(BlockComment) \
38 TYPE(CastRParen) \
39 TYPE(ConditionalExpr) \
40 TYPE(ConflictAlternative) \
41 TYPE(ConflictEnd) \
42 TYPE(ConflictStart) \
43 TYPE(CtorInitializerColon) \
44 TYPE(CtorInitializerComma) \
45 TYPE(DesignatedInitializerLSquare) \
46 TYPE(DesignatedInitializerPeriod) \
47 TYPE(DictLiteral) \
48 TYPE(ForEachMacro) \
49 TYPE(FunctionAnnotationRParen) \
50 TYPE(FunctionDeclarationName) \
51 TYPE(FunctionLBrace) \
52 TYPE(FunctionTypeLParen) \
53 TYPE(ImplicitStringLiteral) \
54 TYPE(InheritanceColon) \
55 TYPE(InheritanceComma) \
56 TYPE(InlineASMBrace) \
57 TYPE(InlineASMColon) \
58 TYPE(JavaAnnotation) \
59 TYPE(JsComputedPropertyName) \
60 TYPE(JsExponentiation) \
61 TYPE(JsExponentiationEqual) \
62 TYPE(JsFatArrow) \
63 TYPE(JsNonNullAssertion) \
64 TYPE(JsTypeColon) \
65 TYPE(JsTypeOperator) \
66 TYPE(JsTypeOptionalQuestion) \
67 TYPE(LambdaArrow) \
68 TYPE(LambdaLSquare) \
69 TYPE(LeadingJavaAnnotation) \
70 TYPE(LineComment) \
71 TYPE(MacroBlockBegin) \
72 TYPE(MacroBlockEnd) \
73 TYPE(ObjCBlockLBrace) \
74 TYPE(ObjCBlockLParen) \
75 TYPE(ObjCDecl) \
76 TYPE(ObjCForIn) \
77 TYPE(ObjCMethodExpr) \
78 TYPE(ObjCMethodSpecifier) \
79 TYPE(ObjCProperty) \
80 TYPE(ObjCStringLiteral) \
81 TYPE(OverloadedOperator) \
82 TYPE(OverloadedOperatorLParen) \
83 TYPE(PointerOrReference) \
84 TYPE(PureVirtualSpecifier) \
85 TYPE(RangeBasedForLoopColon) \
86 TYPE(RegexLiteral) \
87 TYPE(SelectorName) \
88 TYPE(StartOfName) \
Francois Ferrand6f40e212018-10-02 16:37:51 +000089 TYPE(StatementMacro) \
Manuel Klimek89628f62017-09-20 09:51:03 +000090 TYPE(StructuredBindingLSquare) \
91 TYPE(TemplateCloser) \
92 TYPE(TemplateOpener) \
93 TYPE(TemplateString) \
Krasimir Georgiev4e2906482018-02-13 10:20:39 +000094 TYPE(ProtoExtensionLSquare) \
Manuel Klimek89628f62017-09-20 09:51:03 +000095 TYPE(TrailingAnnotation) \
96 TYPE(TrailingReturnArrow) \
97 TYPE(TrailingUnaryOperator) \
98 TYPE(UnaryOperator) \
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +000099 TYPE(Unknown)
100
Alexander Kornienko4b672072013-06-03 16:45:03 +0000101enum TokenType {
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +0000102#define TYPE(X) TT_##X,
Manuel Klimek89628f62017-09-20 09:51:03 +0000103 LIST_TOKEN_TYPES
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +0000104#undef TYPE
Manuel Klimek89628f62017-09-20 09:51:03 +0000105 NUM_TOKEN_TYPES
Alexander Kornienko4b672072013-06-03 16:45:03 +0000106};
107
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000108/// Determines the name of a token type.
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +0000109const char *getTokenTypeName(TokenType Type);
110
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000111// Represents what type of block a set of braces open.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000112enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit };
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000113
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000114// The packing kind of a function's parameters.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000115enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive };
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000116
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000117enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break };
Manuel Klimek71814b42013-10-11 21:25:45 +0000118
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000119class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000120class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000121
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000122/// A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000123/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000124struct FormatToken {
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000125 FormatToken() {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000126
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000127 /// The \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000128 Token Tok;
129
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000130 /// The number of newlines immediately before the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000131 ///
132 /// This can be used to determine what the user wrote in the original code
133 /// and thereby e.g. leave an empty line between two function definitions.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000134 unsigned NewlinesBefore = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000135
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000136 /// Whether there is at least one unescaped newline before the \c
Alexander Kornienko4b672072013-06-03 16:45:03 +0000137 /// Token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000138 bool HasUnescapedNewline = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000139
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000140 /// The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000141 SourceRange WhitespaceRange;
142
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000143 /// The offset just past the last '\n' in this token's leading
Alexander Kornienko4b672072013-06-03 16:45:03 +0000144 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000145 unsigned LastNewlineOffset = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000146
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000147 /// The width of the non-whitespace parts of the token (or its first
Alexander Kornienko39856b72013-09-10 09:38:25 +0000148 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000149 /// We need this to correctly measure number of columns a token spans.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000150 unsigned ColumnWidth = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000151
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000152 /// Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000153 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000154 unsigned LastLineColumnWidth = 0;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000155
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000156 /// Whether the token text contains newlines (escaped or not).
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000157 bool IsMultiline = false;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000158
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000159 /// Indicates that this is the first token of the file.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000160 bool IsFirst = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000161
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000162 /// Whether there must be a line break before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000163 ///
164 /// This happens for example when a preprocessor directive ended directly
165 /// before the token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000166 bool MustBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000167
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000168 /// The raw text of the token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000169 ///
170 /// Contains the raw token text without leading whitespace and without leading
171 /// escaped newlines.
172 StringRef TokenText;
173
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000174 /// Set to \c true if this token is an unterminated literal.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000175 bool IsUnterminatedLiteral = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +0000176
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000177 /// Contains the kind of block if this token is a brace.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000178 BraceBlockKind BlockKind = BK_Unknown;
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000179
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000180 TokenType Type = TT_Unknown;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000181
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000182 /// The number of spaces that should be inserted before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000183 unsigned SpacesRequiredBefore = 0;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000184
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000185 /// \c true if it is allowed to break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000186 bool CanBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000187
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000188 /// \c true if this is the ">" of "template<..>".
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000189 bool ClosesTemplateDeclaration = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000190
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000191 /// Number of parameters, if this is "(", "[" or "<".
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000192 unsigned ParameterCount = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000193
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000194 /// Number of parameters that are nested blocks,
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000195 /// if this is "(", "[" or "<".
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000196 unsigned BlockParameterCount = 0;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000197
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000198 /// If this is a bracket ("<", "(", "[" or "{"), contains the kind of
Daniel Jasperde7ca752015-05-04 07:39:00 +0000199 /// the surrounding bracket.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000200 tok::TokenKind ParentBracket = tok::unknown;
Daniel Jasperde7ca752015-05-04 07:39:00 +0000201
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000202 /// A token can have a special role that can carry extra information
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000203 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000204 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000205
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000206 /// If this is an opening parenthesis, how are the parameters packed?
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000207 ParameterPackingKind PackingKind = PPK_Inconclusive;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000208
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000209 /// The total length of the unwrapped line up to and including this
Manuel Klimek31c85922013-08-29 15:21:40 +0000210 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000211 unsigned TotalLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000212
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000213 /// The original 0-based column of this token, including expanded tabs.
Alexander Kornienko39856b72013-09-10 09:38:25 +0000214 /// The configured TabWidth is used as tab width.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000215 unsigned OriginalColumn = 0;
Manuel Klimek31c85922013-08-29 15:21:40 +0000216
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000217 /// The length of following tokens until the next natural split point,
Alexander Kornienko4b672072013-06-03 16:45:03 +0000218 /// or the next token that can be broken.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000219 unsigned UnbreakableTailLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000220
221 // FIXME: Come up with a 'cleaner' concept.
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000222 /// The binding strength of a token. This is a combined value of
Alexander Kornienko4b672072013-06-03 16:45:03 +0000223 /// operator precedence, parenthesis nesting, etc.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000224 unsigned BindingStrength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000225
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000226 /// The nesting level of this token, i.e. the number of surrounding (),
Daniel Jasper63af7c42013-12-09 14:40:19 +0000227 /// [], {} or <>.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000228 unsigned NestingLevel = 0;
Daniel Jasper63af7c42013-12-09 14:40:19 +0000229
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000230 /// The indent level of this token. Copied from the surrounding line.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000231 unsigned IndentLevel = 0;
232
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000233 /// Penalty for inserting a line break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000234 unsigned SplitPenalty = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000235
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000236 /// If this is the first ObjC selector name in an ObjC method
Alexander Kornienko4b672072013-06-03 16:45:03 +0000237 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000238 ///
239 /// This being set to 0 means that the selectors should not be colon-aligned,
240 /// e.g. because several of them are block-type.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000241 unsigned LongestObjCSelectorName = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000242
Jacek Olesiak27a55792018-07-09 05:58:51 +0000243 /// If this is the first ObjC selector name in an ObjC method
244 /// definition or call, this contains the number of parts that the whole
245 /// selector consist of.
Jacek Olesiakfb7f5c02018-02-07 10:35:08 +0000246 unsigned ObjCSelectorNameParts = 0;
247
Jacek Olesiak6b475b72018-07-09 06:54:52 +0000248 /// The 0-based index of the parameter/argument. For ObjC it is set
249 /// for the selector name token.
250 /// For now calculated only for ObjC.
251 unsigned ParameterIndex = 0;
252
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000253 /// Stores the number of required fake parentheses and the
Alexander Kornienko4b672072013-06-03 16:45:03 +0000254 /// corresponding operator precedence.
255 ///
256 /// If multiple fake parentheses start at a token, this vector stores them in
257 /// reverse order, i.e. inner fake parenthesis first.
258 SmallVector<prec::Level, 4> FakeLParens;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000259 /// Insert this many fake ) after this token for correct indentation.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000260 unsigned FakeRParens = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000261
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000262 /// \c true if this token starts a binary expression, i.e. has at least
Daniel Jasper562ecd42013-09-06 08:08:14 +0000263 /// one fake l_paren with a precedence greater than prec::Unknown.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000264 bool StartsBinaryExpression = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000265 /// \c true if this token ends a binary expression.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000266 bool EndsBinaryExpression = false;
Daniel Jasper562ecd42013-09-06 08:08:14 +0000267
Krasimir Georgiev54ff0df2018-10-01 18:18:00 +0000268 /// If this is an operator (or "."/"->") in a sequence of operators
Daniel Jasper0e617842014-04-16 12:26:54 +0000269 /// with the same precedence, contains the 0-based operator index.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000270 unsigned OperatorIndex = 0;
Daniel Jasper0e617842014-04-16 12:26:54 +0000271
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000272 /// If this is an operator (or "."/"->") in a sequence of operators
Daniel Jasper00492f92016-01-05 13:03:50 +0000273 /// with the same precedence, points to the next operator.
274 FormatToken *NextOperator = nullptr;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000275
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000276 /// Is this token part of a \c DeclStmt defining multiple variables?
Alexander Kornienko4b672072013-06-03 16:45:03 +0000277 ///
278 /// Only set if \c Type == \c TT_StartOfName.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000279 bool PartOfMultiVariableDeclStmt = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000280
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000281 /// Does this line comment continue a line comment section?
Krasimir Georgievb6ccd382017-02-02 14:36:50 +0000282 ///
283 /// Only set to true if \c Type == \c TT_LineComment.
284 bool ContinuesLineCommentSection = false;
285
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000286 /// If this is a bracket, this points to the matching one.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000287 FormatToken *MatchingParen = nullptr;
288
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000289 /// The previous token in the unwrapped line.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000290 FormatToken *Previous = nullptr;
291
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000292 /// The next token in the unwrapped line.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000293 FormatToken *Next = nullptr;
294
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000295 /// If this token starts a block, this contains all the unwrapped lines
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000296 /// in it.
297 SmallVector<AnnotatedLine *, 1> Children;
298
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000299 /// Stores the formatting decision for the token once it was made.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000300 FormatDecision Decision = FD_Unformatted;
301
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000302 /// If \c true, this token has been fully formatted (indented and
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000303 /// potentially re-formatted inside), and we do not allow further formatting
304 /// changes.
305 bool Finalized = false;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000306
Alexander Kornienko4b672072013-06-03 16:45:03 +0000307 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
Daniel Jasper8354ea82014-11-21 12:14:12 +0000308 bool is(TokenType TT) const { return Type == TT; }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000309 bool is(const IdentifierInfo *II) const {
310 return II && II == Tok.getIdentifierInfo();
311 }
Daniel Jasperd7884572015-08-14 12:44:06 +0000312 bool is(tok::PPKeywordKind Kind) const {
313 return Tok.getIdentifierInfo() &&
314 Tok.getIdentifierInfo()->getPPKeywordID() == Kind;
315 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000316 template <typename A, typename B> bool isOneOf(A K1, B K2) const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000317 return is(K1) || is(K2);
318 }
Benjamin Kramerc5bc3cd2015-02-15 20:11:14 +0000319 template <typename A, typename B, typename... Ts>
320 bool isOneOf(A K1, B K2, Ts... Ks) const {
321 return is(K1) || isOneOf(K2, Ks...);
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000322 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000323 template <typename T> bool isNot(T Kind) const { return !is(Kind); }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000324
Ilya Biryukov370eff82018-09-17 07:46:20 +0000325 bool closesScopeAfterBlock() const {
326 if (BlockKind == BK_Block)
327 return true;
328 if (closesScope())
329 return Previous->closesScopeAfterBlock();
330 return false;
331 }
332
Martin Probst1244ecb2016-05-29 14:41:02 +0000333 /// \c true if this token starts a sequence with the given tokens in order,
334 /// following the ``Next`` pointers, ignoring comments.
335 template <typename A, typename... Ts>
336 bool startsSequence(A K1, Ts... Tokens) const {
337 return startsSequenceInternal(K1, Tokens...);
338 }
339
340 /// \c true if this token ends a sequence with the given tokens in order,
341 /// following the ``Previous`` pointers, ignoring comments.
342 template <typename A, typename... Ts>
343 bool endsSequence(A K1, Ts... Tokens) const {
344 return endsSequenceInternal(K1, Tokens...);
345 }
346
Daniel Jasper04b6a082013-12-20 06:22:01 +0000347 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000348
349 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
350 return Tok.isObjCAtKeyword(Kind);
351 }
352
353 bool isAccessSpecifier(bool ColonRequired = true) const {
354 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
355 (!ColonRequired || (Next && Next->is(tok::colon)));
356 }
357
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000358 /// Determine whether the token is a simple-type-specifier.
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000359 bool isSimpleTypeSpecifier() const;
360
Alexander Kornienko4b672072013-06-03 16:45:03 +0000361 bool isObjCAccessSpecifier() const {
Manuel Klimek89628f62017-09-20 09:51:03 +0000362 return is(tok::at) && Next &&
363 (Next->isObjCAtKeyword(tok::objc_public) ||
364 Next->isObjCAtKeyword(tok::objc_protected) ||
365 Next->isObjCAtKeyword(tok::objc_package) ||
366 Next->isObjCAtKeyword(tok::objc_private));
Alexander Kornienko4b672072013-06-03 16:45:03 +0000367 }
368
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000369 /// Returns whether \p Tok is ([{ or an opening < of a template or in
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000370 /// protos.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000371 bool opensScope() const {
Daniel Jasper24de6fb2017-01-31 13:03:07 +0000372 if (is(TT_TemplateString) && TokenText.endswith("${"))
373 return true;
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000374 if (is(TT_DictLiteral) && is(tok::less))
375 return true;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000376 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
377 TT_TemplateOpener);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000378 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000379 /// Returns whether \p Tok is )]} or a closing > of a template or in
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000380 /// protos.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000381 bool closesScope() const {
Daniel Jasper24de6fb2017-01-31 13:03:07 +0000382 if (is(TT_TemplateString) && TokenText.startswith("}"))
383 return true;
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000384 if (is(TT_DictLiteral) && is(tok::greater))
385 return true;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000386 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
387 TT_TemplateCloser);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000388 }
389
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000390 /// Returns \c true if this is a "." or "->" accessing a member.
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000391 bool isMemberAccess() const {
Daniel Jasper85bcadc2014-07-09 13:07:57 +0000392 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
Daniel Jasper05cd9292015-03-26 18:46:28 +0000393 !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
394 TT_LambdaArrow);
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000395 }
396
Alexander Kornienko4b672072013-06-03 16:45:03 +0000397 bool isUnaryOperator() const {
398 switch (Tok.getKind()) {
399 case tok::plus:
400 case tok::plusplus:
401 case tok::minus:
402 case tok::minusminus:
403 case tok::exclaim:
404 case tok::tilde:
405 case tok::kw_sizeof:
406 case tok::kw_alignof:
407 return true;
408 default:
409 return false;
410 }
411 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000412
Alexander Kornienko4b672072013-06-03 16:45:03 +0000413 bool isBinaryOperator() const {
414 // Comma is a binary operator, but does not behave as such wrt. formatting.
415 return getPrecedence() > prec::Comma;
416 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000417
Alexander Kornienko4b672072013-06-03 16:45:03 +0000418 bool isTrailingComment() const {
Daniel Jasper610381f2014-08-26 09:37:52 +0000419 return is(tok::comment) &&
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000420 (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000421 }
422
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000423 /// Returns \c true if this is a keyword that can be used
Daniel Jasper78b194992014-08-06 14:15:41 +0000424 /// like a function call (e.g. sizeof, typeid, ...).
425 bool isFunctionLikeKeyword() const {
426 switch (Tok.getKind()) {
427 case tok::kw_throw:
428 case tok::kw_typeid:
429 case tok::kw_return:
430 case tok::kw_sizeof:
431 case tok::kw_alignof:
432 case tok::kw_alignas:
433 case tok::kw_decltype:
434 case tok::kw_noexcept:
435 case tok::kw_static_assert:
436 case tok::kw___attribute:
437 return true;
438 default:
439 return false;
440 }
441 }
442
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000443 /// Returns \c true if this is a string literal that's like a label,
Daniel Jasper7209bb92016-12-13 11:16:42 +0000444 /// e.g. ends with "=" or ":".
445 bool isLabelString() const {
446 if (!is(tok::string_literal))
447 return false;
448 StringRef Content = TokenText;
449 if (Content.startswith("\"") || Content.startswith("'"))
450 Content = Content.drop_front(1);
451 if (Content.endswith("\"") || Content.endswith("'"))
452 Content = Content.drop_back(1);
453 Content = Content.trim();
454 return Content.size() > 1 &&
455 (Content.back() == ':' || Content.back() == '=');
456 }
457
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000458 /// Returns actual token start location without leading escaped
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000459 /// newlines and whitespace.
460 ///
461 /// This can be different to Tok.getLocation(), which includes leading escaped
462 /// newlines.
463 SourceLocation getStartOfNonWhitespace() const {
464 return WhitespaceRange.getEnd();
465 }
466
Alexander Kornienko4b672072013-06-03 16:45:03 +0000467 prec::Level getPrecedence() const {
Nico Weber47867e32018-01-23 17:29:41 +0000468 return getBinOpPrecedence(Tok.getKind(), /*GreaterThanIsOperator=*/true,
469 /*CPlusPlus11=*/true);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000470 }
471
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000472 /// Returns the previous token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000473 FormatToken *getPreviousNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000474 FormatToken *Tok = Previous;
Craig Topper2145bc02014-05-09 08:15:10 +0000475 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000476 Tok = Tok->Previous;
477 return Tok;
478 }
479
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000480 /// Returns the next token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000481 const FormatToken *getNextNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000482 const FormatToken *Tok = Next;
Craig Topper2145bc02014-05-09 08:15:10 +0000483 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000484 Tok = Tok->Next;
485 return Tok;
486 }
487
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000488 /// Returns \c true if this tokens starts a block-type list, i.e. a
Daniel Jasperb8f61682013-10-22 15:45:58 +0000489 /// list that should be indented with a block indent.
Daniel Jasperbd73bcf2015-10-27 13:42:08 +0000490 bool opensBlockOrBlockTypeList(const FormatStyle &Style) const {
Daniel Jasper98e0b122017-02-20 14:51:16 +0000491 if (is(TT_TemplateString) && opensScope())
492 return true;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000493 return is(TT_ArrayInitializerLSquare) ||
Krasimir Georgiev9c5ac632018-02-19 16:00:21 +0000494 is(TT_ProtoExtensionLSquare) ||
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000495 (is(tok::l_brace) &&
496 (BlockKind == BK_Block || is(TT_DictLiteral) ||
Krasimir Georgievff747be2017-06-27 13:43:07 +0000497 (!Style.Cpp11BracedListStyle && NestingLevel == 0))) ||
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000498 (is(tok::less) && (Style.Language == FormatStyle::LK_Proto ||
499 Style.Language == FormatStyle::LK_TextProto));
Daniel Jasperb8f61682013-10-22 15:45:58 +0000500 }
501
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000502 /// Returns whether the token is the left square bracket of a C++
Manuel Klimeke411aa82017-09-20 09:29:37 +0000503 /// structured binding declaration.
504 bool isCppStructuredBinding(const FormatStyle &Style) const {
505 if (!Style.isCpp() || isNot(tok::l_square))
506 return false;
Manuel Klimek89628f62017-09-20 09:51:03 +0000507 const FormatToken *T = this;
Manuel Klimeke411aa82017-09-20 09:29:37 +0000508 do {
509 T = T->getPreviousNonComment();
510 } while (T && T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
511 tok::ampamp));
512 return T && T->is(tok::kw_auto);
513 }
514
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000515 /// Same as opensBlockOrBlockTypeList, but for the closing token.
Daniel Jasperbd73bcf2015-10-27 13:42:08 +0000516 bool closesBlockOrBlockTypeList(const FormatStyle &Style) const {
Daniel Jasper98e0b122017-02-20 14:51:16 +0000517 if (is(TT_TemplateString) && closesScope())
518 return true;
Daniel Jasperbd73bcf2015-10-27 13:42:08 +0000519 return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style);
Daniel Jasper1db6c382013-10-22 15:30:28 +0000520 }
521
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000522 /// Return the actual namespace token, if this token starts a namespace
Francois Ferrandad722562017-06-30 20:25:55 +0000523 /// block.
524 const FormatToken *getNamespaceToken() const {
525 const FormatToken *NamespaceTok = this;
526 if (is(tok::comment))
527 NamespaceTok = NamespaceTok->getNextNonComment();
Sam McCall6f3778c2018-09-05 07:44:02 +0000528 // Detect "(inline|export)? namespace" in the beginning of a line.
529 if (NamespaceTok && NamespaceTok->isOneOf(tok::kw_inline, tok::kw_export))
Francois Ferrandad722562017-06-30 20:25:55 +0000530 NamespaceTok = NamespaceTok->getNextNonComment();
531 return NamespaceTok && NamespaceTok->is(tok::kw_namespace) ? NamespaceTok
532 : nullptr;
533 }
534
Alexander Kornienko4b672072013-06-03 16:45:03 +0000535private:
536 // Disallow copying.
Aaron Ballmanabc18922015-02-15 22:54:08 +0000537 FormatToken(const FormatToken &) = delete;
538 void operator=(const FormatToken &) = delete;
Martin Probst1244ecb2016-05-29 14:41:02 +0000539
540 template <typename A, typename... Ts>
541 bool startsSequenceInternal(A K1, Ts... Tokens) const {
542 if (is(tok::comment) && Next)
543 return Next->startsSequenceInternal(K1, Tokens...);
544 return is(K1) && Next && Next->startsSequenceInternal(Tokens...);
545 }
546
Manuel Klimek89628f62017-09-20 09:51:03 +0000547 template <typename A> bool startsSequenceInternal(A K1) const {
Martin Probst1244ecb2016-05-29 14:41:02 +0000548 if (is(tok::comment) && Next)
549 return Next->startsSequenceInternal(K1);
550 return is(K1);
551 }
552
Manuel Klimek89628f62017-09-20 09:51:03 +0000553 template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const {
Martin Probst1244ecb2016-05-29 14:41:02 +0000554 if (is(tok::comment) && Previous)
555 return Previous->endsSequenceInternal(K1);
556 return is(K1);
557 }
558
559 template <typename A, typename... Ts>
560 bool endsSequenceInternal(A K1, Ts... Tokens) const {
561 if (is(tok::comment) && Previous)
562 return Previous->endsSequenceInternal(K1, Tokens...);
563 return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...);
564 }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000565};
566
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000567class ContinuationIndenter;
568struct LineState;
569
570class TokenRole {
571public:
572 TokenRole(const FormatStyle &Style) : Style(Style) {}
573 virtual ~TokenRole();
574
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000575 /// After the \c TokenAnnotator has finished annotating all the tokens,
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000576 /// this function precomputes required information for formatting.
577 virtual void precomputeFormattingInfos(const FormatToken *Token);
578
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000579 /// Apply the special formatting that the given role demands.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000580 ///
Daniel Jasper01603472014-01-09 13:42:56 +0000581 /// Assumes that the token having this role is already formatted.
582 ///
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000583 /// Continues formatting from \p State leaving indentation to \p Indenter and
584 /// returns the total penalty that this formatting incurs.
Daniel Jasper01603472014-01-09 13:42:56 +0000585 virtual unsigned formatFromToken(LineState &State,
586 ContinuationIndenter *Indenter,
587 bool DryRun) {
588 return 0;
589 }
590
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000591 /// Same as \c formatFromToken, but assumes that the first token has
Daniel Jasper01603472014-01-09 13:42:56 +0000592 /// already been set thereby deciding on the first line break.
593 virtual unsigned formatAfterToken(LineState &State,
594 ContinuationIndenter *Indenter,
595 bool DryRun) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000596 return 0;
597 }
598
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000599 /// Notifies the \c Role that a comma was found.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000600 virtual void CommaFound(const FormatToken *Token) {}
601
Krasimir Georgiev5528cac2018-10-31 17:56:57 +0000602 virtual const FormatToken *lastComma() { return nullptr; }
603
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000604protected:
605 const FormatStyle &Style;
606};
607
608class CommaSeparatedList : public TokenRole {
609public:
Daniel Jasper01603472014-01-09 13:42:56 +0000610 CommaSeparatedList(const FormatStyle &Style)
611 : TokenRole(Style), HasNestedBracedList(false) {}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000612
Craig Topperfb6b25b2014-03-15 04:29:04 +0000613 void precomputeFormattingInfos(const FormatToken *Token) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000614
Craig Topperfb6b25b2014-03-15 04:29:04 +0000615 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
616 bool DryRun) override;
Daniel Jasper01603472014-01-09 13:42:56 +0000617
Craig Topperfb6b25b2014-03-15 04:29:04 +0000618 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
619 bool DryRun) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000620
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000621 /// Adds \p Token as the next comma to the \c CommaSeparated list.
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000622 void CommaFound(const FormatToken *Token) override {
623 Commas.push_back(Token);
624 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000625
Krasimir Georgiev5528cac2018-10-31 17:56:57 +0000626 const FormatToken *lastComma() override {
627 if (Commas.empty())
628 return nullptr;
629 return Commas.back();
630 }
631
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000632private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000633 /// A struct that holds information on how to format a given list with
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000634 /// a specific number of columns.
635 struct ColumnFormat {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000636 /// The number of columns to use.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000637 unsigned Columns;
638
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000639 /// The total width in characters.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000640 unsigned TotalWidth;
641
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000642 /// The number of lines required for this format.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000643 unsigned LineCount;
644
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000645 /// The size of each column in characters.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000646 SmallVector<unsigned, 8> ColumnSizes;
647 };
648
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000649 /// Calculate which \c ColumnFormat fits best into
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000650 /// \p RemainingCharacters.
651 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
652
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000653 /// The ordered \c FormatTokens making up the commas of this list.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000654 SmallVector<const FormatToken *, 8> Commas;
655
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000656 /// The length of each of the list's items in characters including the
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000657 /// trailing comma.
658 SmallVector<unsigned, 8> ItemLengths;
659
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000660 /// Precomputed formats that can be used for this list.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000661 SmallVector<ColumnFormat, 4> Formats;
Daniel Jasper01603472014-01-09 13:42:56 +0000662
663 bool HasNestedBracedList;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000664};
665
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000666/// Encapsulates keywords that are context sensitive or for languages not
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000667/// properly supported by Clang's lexer.
668struct AdditionalKeywords {
669 AdditionalKeywords(IdentifierTable &IdentTable) {
Daniel Jasper5d7f06f2015-08-24 14:28:08 +0000670 kw_final = &IdentTable.get("final");
671 kw_override = &IdentTable.get("override");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000672 kw_in = &IdentTable.get("in");
Daniel Jasperb7fda112016-02-11 13:24:15 +0000673 kw_of = &IdentTable.get("of");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000674 kw_CF_ENUM = &IdentTable.get("CF_ENUM");
675 kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000676 kw_NS_ENUM = &IdentTable.get("NS_ENUM");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000677 kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000678
Martin Probstc4a0dd42016-05-20 11:24:24 +0000679 kw_as = &IdentTable.get("as");
Martin Probst5f8445b2016-04-24 22:05:09 +0000680 kw_async = &IdentTable.get("async");
681 kw_await = &IdentTable.get("await");
Martin Probst72fd75a2016-11-10 16:20:58 +0000682 kw_declare = &IdentTable.get("declare");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000683 kw_finally = &IdentTable.get("finally");
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +0000684 kw_from = &IdentTable.get("from");
Martin Probst5f8445b2016-04-24 22:05:09 +0000685 kw_function = &IdentTable.get("function");
Martin Probst19c7de02017-04-26 12:34:15 +0000686 kw_get = &IdentTable.get("get");
Daniel Jasper354aa512015-02-19 16:07:32 +0000687 kw_import = &IdentTable.get("import");
Martin Probst3315aed2018-09-27 06:48:13 +0000688 kw_infer = &IdentTable.get("infer");
Daniel Jasper779c66f2015-12-30 08:00:58 +0000689 kw_is = &IdentTable.get("is");
Daniel Jasper9f642f72015-09-28 14:28:08 +0000690 kw_let = &IdentTable.get("let");
Martin Probst72fd75a2016-11-10 16:20:58 +0000691 kw_module = &IdentTable.get("module");
Martin Probsta81dd0b2017-07-07 13:17:10 +0000692 kw_readonly = &IdentTable.get("readonly");
Martin Probst19c7de02017-04-26 12:34:15 +0000693 kw_set = &IdentTable.get("set");
Martin Probst1b7f9802016-06-23 19:52:32 +0000694 kw_type = &IdentTable.get("type");
Martin Probst9926abb2017-08-01 17:42:16 +0000695 kw_typeof = &IdentTable.get("typeof");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000696 kw_var = &IdentTable.get("var");
Martin Probst5f8445b2016-04-24 22:05:09 +0000697 kw_yield = &IdentTable.get("yield");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000698
Daniel Jasper82c92752014-11-21 12:19:07 +0000699 kw_abstract = &IdentTable.get("abstract");
Nico Weber4f113492015-09-15 23:48:17 +0000700 kw_assert = &IdentTable.get("assert");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000701 kw_extends = &IdentTable.get("extends");
702 kw_implements = &IdentTable.get("implements");
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000703 kw_instanceof = &IdentTable.get("instanceof");
Nico Webera644d7f2014-11-10 16:30:02 +0000704 kw_interface = &IdentTable.get("interface");
Nico Webered501662015-01-13 22:32:50 +0000705 kw_native = &IdentTable.get("native");
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000706 kw_package = &IdentTable.get("package");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000707 kw_synchronized = &IdentTable.get("synchronized");
708 kw_throws = &IdentTable.get("throws");
Nico Weberfac23712015-02-04 15:26:27 +0000709 kw___except = &IdentTable.get("__except");
Nico Weber21088802017-02-10 19:36:52 +0000710 kw___has_include = &IdentTable.get("__has_include");
711 kw___has_include_next = &IdentTable.get("__has_include_next");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000712
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000713 kw_mark = &IdentTable.get("mark");
714
Daniel Jasperd8d98392015-11-20 14:32:54 +0000715 kw_extend = &IdentTable.get("extend");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000716 kw_option = &IdentTable.get("option");
717 kw_optional = &IdentTable.get("optional");
718 kw_repeated = &IdentTable.get("repeated");
719 kw_required = &IdentTable.get("required");
720 kw_returns = &IdentTable.get("returns");
Daniel Jasper53395402015-04-07 15:04:40 +0000721
722 kw_signals = &IdentTable.get("signals");
Daniel Jaspera00de632015-12-01 12:05:04 +0000723 kw_qsignals = &IdentTable.get("Q_SIGNALS");
Daniel Jasper53395402015-04-07 15:04:40 +0000724 kw_slots = &IdentTable.get("slots");
725 kw_qslots = &IdentTable.get("Q_SLOTS");
Krasimir Georgiev835ac9b2017-07-05 12:24:01 +0000726
727 // Keep this at the end of the constructor to make sure everything here is
728 // already initialized.
729 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
730 {kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
Martin Probsta81dd0b2017-07-07 13:17:10 +0000731 kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_readonly,
Martin Probst9926abb2017-08-01 17:42:16 +0000732 kw_set, kw_type, kw_typeof, kw_var, kw_yield,
Krasimir Georgiev835ac9b2017-07-05 12:24:01 +0000733 // Keywords from the Java section.
734 kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000735 }
736
Nico Weberfac23712015-02-04 15:26:27 +0000737 // Context sensitive keywords.
Daniel Jasper5d7f06f2015-08-24 14:28:08 +0000738 IdentifierInfo *kw_final;
739 IdentifierInfo *kw_override;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000740 IdentifierInfo *kw_in;
Daniel Jasperb7fda112016-02-11 13:24:15 +0000741 IdentifierInfo *kw_of;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000742 IdentifierInfo *kw_CF_ENUM;
743 IdentifierInfo *kw_CF_OPTIONS;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000744 IdentifierInfo *kw_NS_ENUM;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000745 IdentifierInfo *kw_NS_OPTIONS;
Nico Weberfac23712015-02-04 15:26:27 +0000746 IdentifierInfo *kw___except;
Nico Weber21088802017-02-10 19:36:52 +0000747 IdentifierInfo *kw___has_include;
748 IdentifierInfo *kw___has_include_next;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000749
750 // JavaScript keywords.
Martin Probstc4a0dd42016-05-20 11:24:24 +0000751 IdentifierInfo *kw_as;
Martin Probst5f8445b2016-04-24 22:05:09 +0000752 IdentifierInfo *kw_async;
753 IdentifierInfo *kw_await;
Martin Probst72fd75a2016-11-10 16:20:58 +0000754 IdentifierInfo *kw_declare;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000755 IdentifierInfo *kw_finally;
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +0000756 IdentifierInfo *kw_from;
Martin Probst5f8445b2016-04-24 22:05:09 +0000757 IdentifierInfo *kw_function;
Martin Probst19c7de02017-04-26 12:34:15 +0000758 IdentifierInfo *kw_get;
Daniel Jasper354aa512015-02-19 16:07:32 +0000759 IdentifierInfo *kw_import;
Martin Probst3315aed2018-09-27 06:48:13 +0000760 IdentifierInfo *kw_infer;
Daniel Jasper779c66f2015-12-30 08:00:58 +0000761 IdentifierInfo *kw_is;
Daniel Jasper9f642f72015-09-28 14:28:08 +0000762 IdentifierInfo *kw_let;
Martin Probst72fd75a2016-11-10 16:20:58 +0000763 IdentifierInfo *kw_module;
Martin Probsta81dd0b2017-07-07 13:17:10 +0000764 IdentifierInfo *kw_readonly;
Martin Probst19c7de02017-04-26 12:34:15 +0000765 IdentifierInfo *kw_set;
Martin Probst1b7f9802016-06-23 19:52:32 +0000766 IdentifierInfo *kw_type;
Martin Probst9926abb2017-08-01 17:42:16 +0000767 IdentifierInfo *kw_typeof;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000768 IdentifierInfo *kw_var;
Martin Probst5f8445b2016-04-24 22:05:09 +0000769 IdentifierInfo *kw_yield;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000770
771 // Java keywords.
Daniel Jasper82c92752014-11-21 12:19:07 +0000772 IdentifierInfo *kw_abstract;
Nico Weber4f113492015-09-15 23:48:17 +0000773 IdentifierInfo *kw_assert;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000774 IdentifierInfo *kw_extends;
775 IdentifierInfo *kw_implements;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000776 IdentifierInfo *kw_instanceof;
Nico Webera644d7f2014-11-10 16:30:02 +0000777 IdentifierInfo *kw_interface;
Nico Webered501662015-01-13 22:32:50 +0000778 IdentifierInfo *kw_native;
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000779 IdentifierInfo *kw_package;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000780 IdentifierInfo *kw_synchronized;
781 IdentifierInfo *kw_throws;
782
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000783 // Pragma keywords.
784 IdentifierInfo *kw_mark;
785
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000786 // Proto keywords.
Daniel Jasperd8d98392015-11-20 14:32:54 +0000787 IdentifierInfo *kw_extend;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000788 IdentifierInfo *kw_option;
789 IdentifierInfo *kw_optional;
790 IdentifierInfo *kw_repeated;
791 IdentifierInfo *kw_required;
792 IdentifierInfo *kw_returns;
Daniel Jasper53395402015-04-07 15:04:40 +0000793
794 // QT keywords.
795 IdentifierInfo *kw_signals;
Daniel Jaspera00de632015-12-01 12:05:04 +0000796 IdentifierInfo *kw_qsignals;
Daniel Jasper53395402015-04-07 15:04:40 +0000797 IdentifierInfo *kw_slots;
798 IdentifierInfo *kw_qslots;
Martin Probst37a7f912017-07-04 15:30:21 +0000799
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000800 /// Returns \c true if \p Tok is a true JavaScript identifier, returns
Martin Probst37a7f912017-07-04 15:30:21 +0000801 /// \c false if it is a keyword or a pseudo keyword.
802 bool IsJavaScriptIdentifier(const FormatToken &Tok) const {
803 return Tok.is(tok::identifier) &&
804 JsExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
805 JsExtraKeywords.end();
806 }
807
808private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000809 /// The JavaScript keywords beyond the C++ keyword set.
Martin Probst37a7f912017-07-04 15:30:21 +0000810 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000811};
812
Alexander Kornienko4b672072013-06-03 16:45:03 +0000813} // namespace format
814} // namespace clang
815
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000816#endif