blob: f54ffe9d54cabf50088c5b4c70c560502bb08623 [file] [log] [blame]
Alexander Kornienko4b672072013-06-03 16:45:03 +00001//===--- FormatToken.h - Format C++ code ------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexander Kornienko4b672072013-06-03 16:45:03 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file contains the declaration of the FormatToken, a wrapper
Alexander Kornienko4b672072013-06-03 16:45:03 +000011/// around Token with additional information related to formatting.
12///
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000015#ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
16#define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
Alexander Kornienko4b672072013-06-03 16:45:03 +000017
Daniel Jasperd0ec0d62014-11-04 12:41:02 +000018#include "clang/Basic/IdentifierTable.h"
Alexander Kornienko4b672072013-06-03 16:45:03 +000019#include "clang/Basic/OperatorPrecedence.h"
Daniel Jasper8de9ed02013-08-22 15:00:41 +000020#include "clang/Format/Format.h"
Alexander Kornienko4b672072013-06-03 16:45:03 +000021#include "clang/Lex/Lexer.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000022#include <memory>
Martin Probst37a7f912017-07-04 15:30:21 +000023#include <unordered_set>
Alexander Kornienko4b672072013-06-03 16:45:03 +000024
25namespace clang {
26namespace format {
27
Manuel Klimek89628f62017-09-20 09:51:03 +000028#define LIST_TOKEN_TYPES \
29 TYPE(ArrayInitializerLSquare) \
30 TYPE(ArraySubscriptLSquare) \
Ben Hamiltonb060ad82018-03-12 15:42:38 +000031 TYPE(AttributeColon) \
Manuel Klimek89628f62017-09-20 09:51:03 +000032 TYPE(AttributeParen) \
Ben Hamiltonb060ad82018-03-12 15:42:38 +000033 TYPE(AttributeSquare) \
Manuel Klimek89628f62017-09-20 09:51:03 +000034 TYPE(BinaryOperator) \
35 TYPE(BitFieldColon) \
36 TYPE(BlockComment) \
37 TYPE(CastRParen) \
38 TYPE(ConditionalExpr) \
39 TYPE(ConflictAlternative) \
40 TYPE(ConflictEnd) \
41 TYPE(ConflictStart) \
42 TYPE(CtorInitializerColon) \
43 TYPE(CtorInitializerComma) \
44 TYPE(DesignatedInitializerLSquare) \
45 TYPE(DesignatedInitializerPeriod) \
46 TYPE(DictLiteral) \
47 TYPE(ForEachMacro) \
48 TYPE(FunctionAnnotationRParen) \
49 TYPE(FunctionDeclarationName) \
50 TYPE(FunctionLBrace) \
51 TYPE(FunctionTypeLParen) \
52 TYPE(ImplicitStringLiteral) \
53 TYPE(InheritanceColon) \
54 TYPE(InheritanceComma) \
55 TYPE(InlineASMBrace) \
56 TYPE(InlineASMColon) \
57 TYPE(JavaAnnotation) \
58 TYPE(JsComputedPropertyName) \
59 TYPE(JsExponentiation) \
60 TYPE(JsExponentiationEqual) \
61 TYPE(JsFatArrow) \
62 TYPE(JsNonNullAssertion) \
Martin Probst26a484f42019-03-19 12:28:41 +000063 TYPE(JsPrivateIdentifier) \
Manuel Klimek89628f62017-09-20 09:51:03 +000064 TYPE(JsTypeColon) \
65 TYPE(JsTypeOperator) \
66 TYPE(JsTypeOptionalQuestion) \
67 TYPE(LambdaArrow) \
Ronald Wamplera83e2db2019-03-26 20:18:14 +000068 TYPE(LambdaLBrace) \
Manuel Klimek89628f62017-09-20 09:51:03 +000069 TYPE(LambdaLSquare) \
70 TYPE(LeadingJavaAnnotation) \
71 TYPE(LineComment) \
72 TYPE(MacroBlockBegin) \
73 TYPE(MacroBlockEnd) \
74 TYPE(ObjCBlockLBrace) \
75 TYPE(ObjCBlockLParen) \
76 TYPE(ObjCDecl) \
77 TYPE(ObjCForIn) \
78 TYPE(ObjCMethodExpr) \
79 TYPE(ObjCMethodSpecifier) \
80 TYPE(ObjCProperty) \
81 TYPE(ObjCStringLiteral) \
82 TYPE(OverloadedOperator) \
83 TYPE(OverloadedOperatorLParen) \
84 TYPE(PointerOrReference) \
85 TYPE(PureVirtualSpecifier) \
86 TYPE(RangeBasedForLoopColon) \
87 TYPE(RegexLiteral) \
88 TYPE(SelectorName) \
89 TYPE(StartOfName) \
Francois Ferrand6f40e212018-10-02 16:37:51 +000090 TYPE(StatementMacro) \
Manuel Klimek89628f62017-09-20 09:51:03 +000091 TYPE(StructuredBindingLSquare) \
92 TYPE(TemplateCloser) \
93 TYPE(TemplateOpener) \
94 TYPE(TemplateString) \
Krasimir Georgiev4e2906482018-02-13 10:20:39 +000095 TYPE(ProtoExtensionLSquare) \
Manuel Klimek89628f62017-09-20 09:51:03 +000096 TYPE(TrailingAnnotation) \
97 TYPE(TrailingReturnArrow) \
98 TYPE(TrailingUnaryOperator) \
99 TYPE(UnaryOperator) \
Paul Hoadcbb726d2019-03-21 13:09:22 +0000100 TYPE(CSharpStringLiteral) \
101 TYPE(CSharpNullCoalescing) \
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +0000102 TYPE(Unknown)
103
Alexander Kornienko4b672072013-06-03 16:45:03 +0000104enum TokenType {
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +0000105#define TYPE(X) TT_##X,
Manuel Klimek89628f62017-09-20 09:51:03 +0000106 LIST_TOKEN_TYPES
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +0000107#undef TYPE
Manuel Klimek89628f62017-09-20 09:51:03 +0000108 NUM_TOKEN_TYPES
Alexander Kornienko4b672072013-06-03 16:45:03 +0000109};
110
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000111/// Determines the name of a token type.
Birunthan Mohanathas67d81c82015-07-13 16:19:34 +0000112const char *getTokenTypeName(TokenType Type);
113
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000114// Represents what type of block a set of braces open.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000115enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit };
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000116
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000117// The packing kind of a function's parameters.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000118enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive };
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000119
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000120enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break };
Manuel Klimek71814b42013-10-11 21:25:45 +0000121
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000122class TokenRole;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000123class AnnotatedLine;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000124
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000125/// A wrapper around a \c Token storing information about the
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000126/// whitespace characters preceding it.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000127struct FormatToken {
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000128 FormatToken() {}
Alexander Kornienko4b672072013-06-03 16:45:03 +0000129
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000130 /// The \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000131 Token Tok;
132
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000133 /// The number of newlines immediately before the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000134 ///
135 /// This can be used to determine what the user wrote in the original code
136 /// and thereby e.g. leave an empty line between two function definitions.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000137 unsigned NewlinesBefore = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000138
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000139 /// Whether there is at least one unescaped newline before the \c
Alexander Kornienko4b672072013-06-03 16:45:03 +0000140 /// Token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000141 bool HasUnescapedNewline = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000142
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000143 /// The range of the whitespace immediately preceding the \c Token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000144 SourceRange WhitespaceRange;
145
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000146 /// The offset just past the last '\n' in this token's leading
Alexander Kornienko4b672072013-06-03 16:45:03 +0000147 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000148 unsigned LastNewlineOffset = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000149
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000150 /// The width of the non-whitespace parts of the token (or its first
Alexander Kornienko39856b72013-09-10 09:38:25 +0000151 /// line for multi-line tokens) in columns.
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000152 /// We need this to correctly measure number of columns a token spans.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000153 unsigned ColumnWidth = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000154
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000155 /// Contains the width in columns of the last line of a multi-line
Alexander Kornienko632abb92013-09-02 13:58:14 +0000156 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000157 unsigned LastLineColumnWidth = 0;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000158
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000159 /// Whether the token text contains newlines (escaped or not).
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000160 bool IsMultiline = false;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000161
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000162 /// Indicates that this is the first token of the file.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000163 bool IsFirst = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000164
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000165 /// Whether there must be a line break before this token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000166 ///
167 /// This happens for example when a preprocessor directive ended directly
168 /// before the token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000169 bool MustBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000170
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000171 /// The raw text of the token.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000172 ///
173 /// Contains the raw token text without leading whitespace and without leading
174 /// escaped newlines.
175 StringRef TokenText;
176
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000177 /// Set to \c true if this token is an unterminated literal.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000178 bool IsUnterminatedLiteral = 0;
Daniel Jasper8369aa52013-07-16 20:28:33 +0000179
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000180 /// Contains the kind of block if this token is a brace.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000181 BraceBlockKind BlockKind = BK_Unknown;
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000182
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000183 TokenType Type = TT_Unknown;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000184
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000185 /// The number of spaces that should be inserted before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000186 unsigned SpacesRequiredBefore = 0;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000187
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000188 /// \c true if it is allowed to break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000189 bool CanBreakBefore = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000190
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000191 /// \c true if this is the ">" of "template<..>".
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000192 bool ClosesTemplateDeclaration = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000193
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000194 /// Number of parameters, if this is "(", "[" or "<".
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000195 unsigned ParameterCount = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000196
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000197 /// Number of parameters that are nested blocks,
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000198 /// if this is "(", "[" or "<".
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000199 unsigned BlockParameterCount = 0;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000200
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000201 /// If this is a bracket ("<", "(", "[" or "{"), contains the kind of
Daniel Jasperde7ca752015-05-04 07:39:00 +0000202 /// the surrounding bracket.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000203 tok::TokenKind ParentBracket = tok::unknown;
Daniel Jasperde7ca752015-05-04 07:39:00 +0000204
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000205 /// A token can have a special role that can carry extra information
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000206 /// about the token's formatting.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000207 std::unique_ptr<TokenRole> Role;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000208
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000209 /// If this is an opening parenthesis, how are the parameters packed?
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000210 ParameterPackingKind PackingKind = PPK_Inconclusive;
Daniel Jasperb10cbc42013-07-10 14:02:49 +0000211
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000212 /// The total length of the unwrapped line up to and including this
Manuel Klimek31c85922013-08-29 15:21:40 +0000213 /// token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000214 unsigned TotalLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000215
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000216 /// The original 0-based column of this token, including expanded tabs.
Alexander Kornienko39856b72013-09-10 09:38:25 +0000217 /// The configured TabWidth is used as tab width.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000218 unsigned OriginalColumn = 0;
Manuel Klimek31c85922013-08-29 15:21:40 +0000219
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000220 /// The length of following tokens until the next natural split point,
Alexander Kornienko4b672072013-06-03 16:45:03 +0000221 /// or the next token that can be broken.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000222 unsigned UnbreakableTailLength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000223
224 // FIXME: Come up with a 'cleaner' concept.
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000225 /// The binding strength of a token. This is a combined value of
Alexander Kornienko4b672072013-06-03 16:45:03 +0000226 /// operator precedence, parenthesis nesting, etc.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000227 unsigned BindingStrength = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000228
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000229 /// The nesting level of this token, i.e. the number of surrounding (),
Daniel Jasper63af7c42013-12-09 14:40:19 +0000230 /// [], {} or <>.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000231 unsigned NestingLevel = 0;
Daniel Jasper63af7c42013-12-09 14:40:19 +0000232
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000233 /// The indent level of this token. Copied from the surrounding line.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000234 unsigned IndentLevel = 0;
235
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000236 /// Penalty for inserting a line break before this token.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000237 unsigned SplitPenalty = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000238
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000239 /// If this is the first ObjC selector name in an ObjC method
Alexander Kornienko4b672072013-06-03 16:45:03 +0000240 /// definition or call, this contains the length of the longest name.
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000241 ///
242 /// This being set to 0 means that the selectors should not be colon-aligned,
243 /// e.g. because several of them are block-type.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000244 unsigned LongestObjCSelectorName = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000245
Jacek Olesiak27a55792018-07-09 05:58:51 +0000246 /// If this is the first ObjC selector name in an ObjC method
247 /// definition or call, this contains the number of parts that the whole
248 /// selector consist of.
Jacek Olesiakfb7f5c02018-02-07 10:35:08 +0000249 unsigned ObjCSelectorNameParts = 0;
250
Jacek Olesiak6b475b72018-07-09 06:54:52 +0000251 /// The 0-based index of the parameter/argument. For ObjC it is set
252 /// for the selector name token.
253 /// For now calculated only for ObjC.
254 unsigned ParameterIndex = 0;
255
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000256 /// Stores the number of required fake parentheses and the
Alexander Kornienko4b672072013-06-03 16:45:03 +0000257 /// corresponding operator precedence.
258 ///
259 /// If multiple fake parentheses start at a token, this vector stores them in
260 /// reverse order, i.e. inner fake parenthesis first.
261 SmallVector<prec::Level, 4> FakeLParens;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000262 /// Insert this many fake ) after this token for correct indentation.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000263 unsigned FakeRParens = 0;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000264
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000265 /// \c true if this token starts a binary expression, i.e. has at least
Daniel Jasper562ecd42013-09-06 08:08:14 +0000266 /// one fake l_paren with a precedence greater than prec::Unknown.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000267 bool StartsBinaryExpression = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000268 /// \c true if this token ends a binary expression.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000269 bool EndsBinaryExpression = false;
Daniel Jasper562ecd42013-09-06 08:08:14 +0000270
Krasimir Georgiev54ff0df2018-10-01 18:18:00 +0000271 /// If this is an operator (or "."/"->") in a sequence of operators
Daniel Jasper0e617842014-04-16 12:26:54 +0000272 /// with the same precedence, contains the 0-based operator index.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000273 unsigned OperatorIndex = 0;
Daniel Jasper0e617842014-04-16 12:26:54 +0000274
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000275 /// If this is an operator (or "."/"->") in a sequence of operators
Daniel Jasper00492f92016-01-05 13:03:50 +0000276 /// with the same precedence, points to the next operator.
277 FormatToken *NextOperator = nullptr;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000278
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000279 /// Is this token part of a \c DeclStmt defining multiple variables?
Alexander Kornienko4b672072013-06-03 16:45:03 +0000280 ///
281 /// Only set if \c Type == \c TT_StartOfName.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000282 bool PartOfMultiVariableDeclStmt = false;
Alexander Kornienko4b672072013-06-03 16:45:03 +0000283
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000284 /// Does this line comment continue a line comment section?
Krasimir Georgievb6ccd382017-02-02 14:36:50 +0000285 ///
286 /// Only set to true if \c Type == \c TT_LineComment.
287 bool ContinuesLineCommentSection = false;
288
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000289 /// If this is a bracket, this points to the matching one.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000290 FormatToken *MatchingParen = nullptr;
291
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000292 /// The previous token in the unwrapped line.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000293 FormatToken *Previous = nullptr;
294
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000295 /// The next token in the unwrapped line.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000296 FormatToken *Next = nullptr;
297
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000298 /// If this token starts a block, this contains all the unwrapped lines
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000299 /// in it.
300 SmallVector<AnnotatedLine *, 1> Children;
301
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000302 /// Stores the formatting decision for the token once it was made.
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000303 FormatDecision Decision = FD_Unformatted;
304
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000305 /// If \c true, this token has been fully formatted (indented and
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000306 /// potentially re-formatted inside), and we do not allow further formatting
307 /// changes.
308 bool Finalized = false;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000309
Alexander Kornienko4b672072013-06-03 16:45:03 +0000310 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
Daniel Jasper8354ea82014-11-21 12:14:12 +0000311 bool is(TokenType TT) const { return Type == TT; }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000312 bool is(const IdentifierInfo *II) const {
313 return II && II == Tok.getIdentifierInfo();
314 }
Daniel Jasperd7884572015-08-14 12:44:06 +0000315 bool is(tok::PPKeywordKind Kind) const {
316 return Tok.getIdentifierInfo() &&
317 Tok.getIdentifierInfo()->getPPKeywordID() == Kind;
318 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000319 template <typename A, typename B> bool isOneOf(A K1, B K2) const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000320 return is(K1) || is(K2);
321 }
Benjamin Kramerc5bc3cd2015-02-15 20:11:14 +0000322 template <typename A, typename B, typename... Ts>
323 bool isOneOf(A K1, B K2, Ts... Ks) const {
324 return is(K1) || isOneOf(K2, Ks...);
Aaron Ballman484ee9b2014-11-24 15:42:34 +0000325 }
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000326 template <typename T> bool isNot(T Kind) const { return !is(Kind); }
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000327
Ilya Biryukov370eff82018-09-17 07:46:20 +0000328 bool closesScopeAfterBlock() const {
329 if (BlockKind == BK_Block)
330 return true;
331 if (closesScope())
332 return Previous->closesScopeAfterBlock();
333 return false;
334 }
335
Martin Probst1244ecb2016-05-29 14:41:02 +0000336 /// \c true if this token starts a sequence with the given tokens in order,
337 /// following the ``Next`` pointers, ignoring comments.
338 template <typename A, typename... Ts>
339 bool startsSequence(A K1, Ts... Tokens) const {
340 return startsSequenceInternal(K1, Tokens...);
341 }
342
343 /// \c true if this token ends a sequence with the given tokens in order,
344 /// following the ``Previous`` pointers, ignoring comments.
345 template <typename A, typename... Ts>
346 bool endsSequence(A K1, Ts... Tokens) const {
347 return endsSequenceInternal(K1, Tokens...);
348 }
349
Daniel Jasper04b6a082013-12-20 06:22:01 +0000350 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000351
352 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
353 return Tok.isObjCAtKeyword(Kind);
354 }
355
356 bool isAccessSpecifier(bool ColonRequired = true) const {
357 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
358 (!ColonRequired || (Next && Next->is(tok::colon)));
359 }
360
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000361 /// Determine whether the token is a simple-type-specifier.
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000362 bool isSimpleTypeSpecifier() const;
363
Alexander Kornienko4b672072013-06-03 16:45:03 +0000364 bool isObjCAccessSpecifier() const {
Manuel Klimek89628f62017-09-20 09:51:03 +0000365 return is(tok::at) && Next &&
366 (Next->isObjCAtKeyword(tok::objc_public) ||
367 Next->isObjCAtKeyword(tok::objc_protected) ||
368 Next->isObjCAtKeyword(tok::objc_package) ||
369 Next->isObjCAtKeyword(tok::objc_private));
Alexander Kornienko4b672072013-06-03 16:45:03 +0000370 }
371
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000372 /// Returns whether \p Tok is ([{ or an opening < of a template or in
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000373 /// protos.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000374 bool opensScope() const {
Daniel Jasper24de6fb2017-01-31 13:03:07 +0000375 if (is(TT_TemplateString) && TokenText.endswith("${"))
376 return true;
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000377 if (is(TT_DictLiteral) && is(tok::less))
378 return true;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000379 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
380 TT_TemplateOpener);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000381 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000382 /// Returns whether \p Tok is )]} or a closing > of a template or in
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000383 /// protos.
Alexander Kornienko4b672072013-06-03 16:45:03 +0000384 bool closesScope() const {
Daniel Jasper24de6fb2017-01-31 13:03:07 +0000385 if (is(TT_TemplateString) && TokenText.startswith("}"))
386 return true;
Krasimir Georgieva79d62d2018-02-06 11:34:34 +0000387 if (is(TT_DictLiteral) && is(tok::greater))
388 return true;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000389 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
390 TT_TemplateCloser);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000391 }
392
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000393 /// Returns \c true if this is a "." or "->" accessing a member.
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000394 bool isMemberAccess() const {
Daniel Jasper85bcadc2014-07-09 13:07:57 +0000395 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
Daniel Jasper05cd9292015-03-26 18:46:28 +0000396 !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
397 TT_LambdaArrow);
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000398 }
399
Alexander Kornienko4b672072013-06-03 16:45:03 +0000400 bool isUnaryOperator() const {
401 switch (Tok.getKind()) {
402 case tok::plus:
403 case tok::plusplus:
404 case tok::minus:
405 case tok::minusminus:
406 case tok::exclaim:
407 case tok::tilde:
408 case tok::kw_sizeof:
409 case tok::kw_alignof:
410 return true;
411 default:
412 return false;
413 }
414 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000415
Alexander Kornienko4b672072013-06-03 16:45:03 +0000416 bool isBinaryOperator() const {
417 // Comma is a binary operator, but does not behave as such wrt. formatting.
418 return getPrecedence() > prec::Comma;
419 }
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000420
Alexander Kornienko4b672072013-06-03 16:45:03 +0000421 bool isTrailingComment() const {
Daniel Jasper610381f2014-08-26 09:37:52 +0000422 return is(tok::comment) &&
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000423 (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000424 }
425
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000426 /// Returns \c true if this is a keyword that can be used
Daniel Jasper78b194992014-08-06 14:15:41 +0000427 /// like a function call (e.g. sizeof, typeid, ...).
428 bool isFunctionLikeKeyword() const {
429 switch (Tok.getKind()) {
430 case tok::kw_throw:
431 case tok::kw_typeid:
432 case tok::kw_return:
433 case tok::kw_sizeof:
434 case tok::kw_alignof:
435 case tok::kw_alignas:
436 case tok::kw_decltype:
437 case tok::kw_noexcept:
438 case tok::kw_static_assert:
439 case tok::kw___attribute:
440 return true;
441 default:
442 return false;
443 }
444 }
445
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000446 /// Returns \c true if this is a string literal that's like a label,
Daniel Jasper7209bb92016-12-13 11:16:42 +0000447 /// e.g. ends with "=" or ":".
448 bool isLabelString() const {
449 if (!is(tok::string_literal))
450 return false;
451 StringRef Content = TokenText;
452 if (Content.startswith("\"") || Content.startswith("'"))
453 Content = Content.drop_front(1);
454 if (Content.endswith("\"") || Content.endswith("'"))
455 Content = Content.drop_back(1);
456 Content = Content.trim();
457 return Content.size() > 1 &&
458 (Content.back() == ':' || Content.back() == '=');
459 }
460
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000461 /// Returns actual token start location without leading escaped
Daniel Jasper3cea45d2015-05-04 08:51:40 +0000462 /// newlines and whitespace.
463 ///
464 /// This can be different to Tok.getLocation(), which includes leading escaped
465 /// newlines.
466 SourceLocation getStartOfNonWhitespace() const {
467 return WhitespaceRange.getEnd();
468 }
469
Alexander Kornienko4b672072013-06-03 16:45:03 +0000470 prec::Level getPrecedence() const {
Nico Weber47867e32018-01-23 17:29:41 +0000471 return getBinOpPrecedence(Tok.getKind(), /*GreaterThanIsOperator=*/true,
472 /*CPlusPlus11=*/true);
Alexander Kornienko4b672072013-06-03 16:45:03 +0000473 }
474
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000475 /// Returns the previous token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000476 FormatToken *getPreviousNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000477 FormatToken *Tok = Previous;
Craig Topper2145bc02014-05-09 08:15:10 +0000478 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000479 Tok = Tok->Previous;
480 return Tok;
481 }
482
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000483 /// Returns the next token ignoring comments.
Alexander Kornienko1efe0a02013-07-04 14:47:51 +0000484 const FormatToken *getNextNonComment() const {
Alexander Kornienko4b672072013-06-03 16:45:03 +0000485 const FormatToken *Tok = Next;
Craig Topper2145bc02014-05-09 08:15:10 +0000486 while (Tok && Tok->is(tok::comment))
Alexander Kornienko4b672072013-06-03 16:45:03 +0000487 Tok = Tok->Next;
488 return Tok;
489 }
490
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000491 /// Returns \c true if this tokens starts a block-type list, i.e. a
Daniel Jasperb8f61682013-10-22 15:45:58 +0000492 /// list that should be indented with a block indent.
Daniel Jasperbd73bcf2015-10-27 13:42:08 +0000493 bool opensBlockOrBlockTypeList(const FormatStyle &Style) const {
Daniel Jasper98e0b122017-02-20 14:51:16 +0000494 if (is(TT_TemplateString) && opensScope())
495 return true;
Paul Hoad5bcf99b2019-03-01 09:09:54 +0000496 return is(TT_ArrayInitializerLSquare) || is(TT_ProtoExtensionLSquare) ||
Daniel Jasper11a0ac62014-12-12 09:40:58 +0000497 (is(tok::l_brace) &&
498 (BlockKind == BK_Block || is(TT_DictLiteral) ||
Krasimir Georgievff747be2017-06-27 13:43:07 +0000499 (!Style.Cpp11BracedListStyle && NestingLevel == 0))) ||
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000500 (is(tok::less) && (Style.Language == FormatStyle::LK_Proto ||
501 Style.Language == FormatStyle::LK_TextProto));
Daniel Jasperb8f61682013-10-22 15:45:58 +0000502 }
503
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000504 /// Returns whether the token is the left square bracket of a C++
Manuel Klimeke411aa82017-09-20 09:29:37 +0000505 /// structured binding declaration.
506 bool isCppStructuredBinding(const FormatStyle &Style) const {
507 if (!Style.isCpp() || isNot(tok::l_square))
508 return false;
Manuel Klimek89628f62017-09-20 09:51:03 +0000509 const FormatToken *T = this;
Manuel Klimeke411aa82017-09-20 09:29:37 +0000510 do {
511 T = T->getPreviousNonComment();
512 } while (T && T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
513 tok::ampamp));
514 return T && T->is(tok::kw_auto);
515 }
516
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000517 /// Same as opensBlockOrBlockTypeList, but for the closing token.
Daniel Jasperbd73bcf2015-10-27 13:42:08 +0000518 bool closesBlockOrBlockTypeList(const FormatStyle &Style) const {
Daniel Jasper98e0b122017-02-20 14:51:16 +0000519 if (is(TT_TemplateString) && closesScope())
520 return true;
Daniel Jasperbd73bcf2015-10-27 13:42:08 +0000521 return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style);
Daniel Jasper1db6c382013-10-22 15:30:28 +0000522 }
523
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000524 /// Return the actual namespace token, if this token starts a namespace
Francois Ferrandad722562017-06-30 20:25:55 +0000525 /// block.
526 const FormatToken *getNamespaceToken() const {
527 const FormatToken *NamespaceTok = this;
528 if (is(tok::comment))
529 NamespaceTok = NamespaceTok->getNextNonComment();
Sam McCall6f3778c2018-09-05 07:44:02 +0000530 // Detect "(inline|export)? namespace" in the beginning of a line.
531 if (NamespaceTok && NamespaceTok->isOneOf(tok::kw_inline, tok::kw_export))
Francois Ferrandad722562017-06-30 20:25:55 +0000532 NamespaceTok = NamespaceTok->getNextNonComment();
533 return NamespaceTok && NamespaceTok->is(tok::kw_namespace) ? NamespaceTok
534 : nullptr;
535 }
536
Alexander Kornienko4b672072013-06-03 16:45:03 +0000537private:
538 // Disallow copying.
Aaron Ballmanabc18922015-02-15 22:54:08 +0000539 FormatToken(const FormatToken &) = delete;
540 void operator=(const FormatToken &) = delete;
Martin Probst1244ecb2016-05-29 14:41:02 +0000541
542 template <typename A, typename... Ts>
543 bool startsSequenceInternal(A K1, Ts... Tokens) const {
544 if (is(tok::comment) && Next)
545 return Next->startsSequenceInternal(K1, Tokens...);
546 return is(K1) && Next && Next->startsSequenceInternal(Tokens...);
547 }
548
Manuel Klimek89628f62017-09-20 09:51:03 +0000549 template <typename A> bool startsSequenceInternal(A K1) const {
Martin Probst1244ecb2016-05-29 14:41:02 +0000550 if (is(tok::comment) && Next)
551 return Next->startsSequenceInternal(K1);
552 return is(K1);
553 }
554
Manuel Klimek89628f62017-09-20 09:51:03 +0000555 template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const {
Martin Probst1244ecb2016-05-29 14:41:02 +0000556 if (is(tok::comment) && Previous)
557 return Previous->endsSequenceInternal(K1);
558 return is(K1);
559 }
560
561 template <typename A, typename... Ts>
562 bool endsSequenceInternal(A K1, Ts... Tokens) const {
563 if (is(tok::comment) && Previous)
564 return Previous->endsSequenceInternal(K1, Tokens...);
565 return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...);
566 }
Alexander Kornienko4b672072013-06-03 16:45:03 +0000567};
568
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000569class ContinuationIndenter;
570struct LineState;
571
572class TokenRole {
573public:
574 TokenRole(const FormatStyle &Style) : Style(Style) {}
575 virtual ~TokenRole();
576
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000577 /// After the \c TokenAnnotator has finished annotating all the tokens,
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000578 /// this function precomputes required information for formatting.
579 virtual void precomputeFormattingInfos(const FormatToken *Token);
580
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000581 /// Apply the special formatting that the given role demands.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000582 ///
Daniel Jasper01603472014-01-09 13:42:56 +0000583 /// Assumes that the token having this role is already formatted.
584 ///
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000585 /// Continues formatting from \p State leaving indentation to \p Indenter and
586 /// returns the total penalty that this formatting incurs.
Daniel Jasper01603472014-01-09 13:42:56 +0000587 virtual unsigned formatFromToken(LineState &State,
588 ContinuationIndenter *Indenter,
589 bool DryRun) {
590 return 0;
591 }
592
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000593 /// Same as \c formatFromToken, but assumes that the first token has
Daniel Jasper01603472014-01-09 13:42:56 +0000594 /// already been set thereby deciding on the first line break.
595 virtual unsigned formatAfterToken(LineState &State,
596 ContinuationIndenter *Indenter,
597 bool DryRun) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000598 return 0;
599 }
600
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000601 /// Notifies the \c Role that a comma was found.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000602 virtual void CommaFound(const FormatToken *Token) {}
603
Krasimir Georgiev5528cac2018-10-31 17:56:57 +0000604 virtual const FormatToken *lastComma() { return nullptr; }
605
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000606protected:
607 const FormatStyle &Style;
608};
609
610class CommaSeparatedList : public TokenRole {
611public:
Daniel Jasper01603472014-01-09 13:42:56 +0000612 CommaSeparatedList(const FormatStyle &Style)
613 : TokenRole(Style), HasNestedBracedList(false) {}
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000614
Craig Topperfb6b25b2014-03-15 04:29:04 +0000615 void precomputeFormattingInfos(const FormatToken *Token) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000616
Craig Topperfb6b25b2014-03-15 04:29:04 +0000617 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
618 bool DryRun) override;
Daniel Jasper01603472014-01-09 13:42:56 +0000619
Craig Topperfb6b25b2014-03-15 04:29:04 +0000620 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
621 bool DryRun) override;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000622
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000623 /// Adds \p Token as the next comma to the \c CommaSeparated list.
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000624 void CommaFound(const FormatToken *Token) override {
625 Commas.push_back(Token);
626 }
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000627
Krasimir Georgiev5528cac2018-10-31 17:56:57 +0000628 const FormatToken *lastComma() override {
629 if (Commas.empty())
630 return nullptr;
631 return Commas.back();
632 }
633
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000634private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000635 /// A struct that holds information on how to format a given list with
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000636 /// a specific number of columns.
637 struct ColumnFormat {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000638 /// The number of columns to use.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000639 unsigned Columns;
640
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000641 /// The total width in characters.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000642 unsigned TotalWidth;
643
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000644 /// The number of lines required for this format.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000645 unsigned LineCount;
646
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000647 /// The size of each column in characters.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000648 SmallVector<unsigned, 8> ColumnSizes;
649 };
650
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000651 /// Calculate which \c ColumnFormat fits best into
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000652 /// \p RemainingCharacters.
653 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
654
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000655 /// The ordered \c FormatTokens making up the commas of this list.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000656 SmallVector<const FormatToken *, 8> Commas;
657
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000658 /// The length of each of the list's items in characters including the
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000659 /// trailing comma.
660 SmallVector<unsigned, 8> ItemLengths;
661
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000662 /// Precomputed formats that can be used for this list.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000663 SmallVector<ColumnFormat, 4> Formats;
Daniel Jasper01603472014-01-09 13:42:56 +0000664
665 bool HasNestedBracedList;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000666};
667
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000668/// Encapsulates keywords that are context sensitive or for languages not
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000669/// properly supported by Clang's lexer.
670struct AdditionalKeywords {
671 AdditionalKeywords(IdentifierTable &IdentTable) {
Daniel Jasper5d7f06f2015-08-24 14:28:08 +0000672 kw_final = &IdentTable.get("final");
673 kw_override = &IdentTable.get("override");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000674 kw_in = &IdentTable.get("in");
Daniel Jasperb7fda112016-02-11 13:24:15 +0000675 kw_of = &IdentTable.get("of");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000676 kw_CF_ENUM = &IdentTable.get("CF_ENUM");
677 kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000678 kw_NS_ENUM = &IdentTable.get("NS_ENUM");
Daniel Jasper31f6c542014-12-05 10:42:21 +0000679 kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000680
Martin Probstc4a0dd42016-05-20 11:24:24 +0000681 kw_as = &IdentTable.get("as");
Martin Probst5f8445b2016-04-24 22:05:09 +0000682 kw_async = &IdentTable.get("async");
683 kw_await = &IdentTable.get("await");
Martin Probst72fd75a2016-11-10 16:20:58 +0000684 kw_declare = &IdentTable.get("declare");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000685 kw_finally = &IdentTable.get("finally");
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +0000686 kw_from = &IdentTable.get("from");
Martin Probst5f8445b2016-04-24 22:05:09 +0000687 kw_function = &IdentTable.get("function");
Martin Probst19c7de02017-04-26 12:34:15 +0000688 kw_get = &IdentTable.get("get");
Daniel Jasper354aa512015-02-19 16:07:32 +0000689 kw_import = &IdentTable.get("import");
Martin Probst3315aed2018-09-27 06:48:13 +0000690 kw_infer = &IdentTable.get("infer");
Daniel Jasper779c66f2015-12-30 08:00:58 +0000691 kw_is = &IdentTable.get("is");
Daniel Jasper9f642f72015-09-28 14:28:08 +0000692 kw_let = &IdentTable.get("let");
Martin Probst72fd75a2016-11-10 16:20:58 +0000693 kw_module = &IdentTable.get("module");
Martin Probsta81dd0b2017-07-07 13:17:10 +0000694 kw_readonly = &IdentTable.get("readonly");
Martin Probst19c7de02017-04-26 12:34:15 +0000695 kw_set = &IdentTable.get("set");
Martin Probst1b7f9802016-06-23 19:52:32 +0000696 kw_type = &IdentTable.get("type");
Martin Probst9926abb2017-08-01 17:42:16 +0000697 kw_typeof = &IdentTable.get("typeof");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000698 kw_var = &IdentTable.get("var");
Martin Probst5f8445b2016-04-24 22:05:09 +0000699 kw_yield = &IdentTable.get("yield");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000700
Daniel Jasper82c92752014-11-21 12:19:07 +0000701 kw_abstract = &IdentTable.get("abstract");
Nico Weber4f113492015-09-15 23:48:17 +0000702 kw_assert = &IdentTable.get("assert");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000703 kw_extends = &IdentTable.get("extends");
704 kw_implements = &IdentTable.get("implements");
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000705 kw_instanceof = &IdentTable.get("instanceof");
Nico Webera644d7f2014-11-10 16:30:02 +0000706 kw_interface = &IdentTable.get("interface");
Nico Webered501662015-01-13 22:32:50 +0000707 kw_native = &IdentTable.get("native");
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000708 kw_package = &IdentTable.get("package");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000709 kw_synchronized = &IdentTable.get("synchronized");
710 kw_throws = &IdentTable.get("throws");
Nico Weberfac23712015-02-04 15:26:27 +0000711 kw___except = &IdentTable.get("__except");
Nico Weber21088802017-02-10 19:36:52 +0000712 kw___has_include = &IdentTable.get("__has_include");
713 kw___has_include_next = &IdentTable.get("__has_include_next");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000714
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000715 kw_mark = &IdentTable.get("mark");
716
Daniel Jasperd8d98392015-11-20 14:32:54 +0000717 kw_extend = &IdentTable.get("extend");
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000718 kw_option = &IdentTable.get("option");
719 kw_optional = &IdentTable.get("optional");
720 kw_repeated = &IdentTable.get("repeated");
721 kw_required = &IdentTable.get("required");
722 kw_returns = &IdentTable.get("returns");
Daniel Jasper53395402015-04-07 15:04:40 +0000723
724 kw_signals = &IdentTable.get("signals");
Daniel Jaspera00de632015-12-01 12:05:04 +0000725 kw_qsignals = &IdentTable.get("Q_SIGNALS");
Daniel Jasper53395402015-04-07 15:04:40 +0000726 kw_slots = &IdentTable.get("slots");
727 kw_qslots = &IdentTable.get("Q_SLOTS");
Krasimir Georgiev835ac9b2017-07-05 12:24:01 +0000728
Paul Hoadcbb726d2019-03-21 13:09:22 +0000729 // C# keywords
730 kw_dollar = &IdentTable.get("dollar");
731 kw_base = &IdentTable.get("base");
732 kw_byte = &IdentTable.get("byte");
733 kw_checked = &IdentTable.get("checked");
734 kw_decimal = &IdentTable.get("decimal");
735 kw_delegate = &IdentTable.get("delegate");
736 kw_event = &IdentTable.get("event");
737 kw_fixed = &IdentTable.get("fixed");
738 kw_foreach = &IdentTable.get("foreach");
739 kw_implicit = &IdentTable.get("implicit");
740 kw_internal = &IdentTable.get("internal");
741 kw_lock = &IdentTable.get("lock");
742 kw_null = &IdentTable.get("null");
743 kw_object = &IdentTable.get("object");
744 kw_out = &IdentTable.get("out");
745 kw_params = &IdentTable.get("params");
746 kw_ref = &IdentTable.get("ref");
747 kw_string = &IdentTable.get("string");
748 kw_stackalloc = &IdentTable.get("stackalloc");
749 kw_sbyte = &IdentTable.get("sbyte");
750 kw_sealed = &IdentTable.get("sealed");
751 kw_uint = &IdentTable.get("uint");
752 kw_ulong = &IdentTable.get("ulong");
753 kw_unchecked = &IdentTable.get("unchecked");
754 kw_unsafe = &IdentTable.get("unsafe");
755 kw_ushort = &IdentTable.get("ushort");
756
757 // Keep this at the end of the constructor to make sure everything here
758 // is
Krasimir Georgiev835ac9b2017-07-05 12:24:01 +0000759 // already initialized.
760 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
761 {kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
Martin Probsta81dd0b2017-07-07 13:17:10 +0000762 kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_readonly,
Martin Probst9926abb2017-08-01 17:42:16 +0000763 kw_set, kw_type, kw_typeof, kw_var, kw_yield,
Krasimir Georgiev835ac9b2017-07-05 12:24:01 +0000764 // Keywords from the Java section.
765 kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
Paul Hoadcbb726d2019-03-21 13:09:22 +0000766
767 CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>(
768 {kw_base, kw_byte, kw_checked, kw_decimal, kw_delegate, kw_event,
769 kw_fixed, kw_foreach, kw_implicit, kw_in, kw_interface, kw_internal,
770 kw_is, kw_lock, kw_null, kw_object, kw_out, kw_override, kw_params,
771 kw_readonly, kw_ref, kw_string, kw_stackalloc, kw_sbyte, kw_sealed,
772 kw_uint, kw_ulong, kw_unchecked, kw_unsafe, kw_ushort,
773 // Keywords from the JavaScript section.
774 kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
775 kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_readonly,
776 kw_set, kw_type, kw_typeof, kw_var, kw_yield,
777 // Keywords from the Java section.
778 kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000779 }
780
Nico Weberfac23712015-02-04 15:26:27 +0000781 // Context sensitive keywords.
Daniel Jasper5d7f06f2015-08-24 14:28:08 +0000782 IdentifierInfo *kw_final;
783 IdentifierInfo *kw_override;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000784 IdentifierInfo *kw_in;
Daniel Jasperb7fda112016-02-11 13:24:15 +0000785 IdentifierInfo *kw_of;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000786 IdentifierInfo *kw_CF_ENUM;
787 IdentifierInfo *kw_CF_OPTIONS;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000788 IdentifierInfo *kw_NS_ENUM;
Daniel Jasper31f6c542014-12-05 10:42:21 +0000789 IdentifierInfo *kw_NS_OPTIONS;
Nico Weberfac23712015-02-04 15:26:27 +0000790 IdentifierInfo *kw___except;
Nico Weber21088802017-02-10 19:36:52 +0000791 IdentifierInfo *kw___has_include;
792 IdentifierInfo *kw___has_include_next;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000793
794 // JavaScript keywords.
Martin Probstc4a0dd42016-05-20 11:24:24 +0000795 IdentifierInfo *kw_as;
Martin Probst5f8445b2016-04-24 22:05:09 +0000796 IdentifierInfo *kw_async;
797 IdentifierInfo *kw_await;
Martin Probst72fd75a2016-11-10 16:20:58 +0000798 IdentifierInfo *kw_declare;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000799 IdentifierInfo *kw_finally;
Daniel Jasper8fc7a1e2016-03-22 14:32:20 +0000800 IdentifierInfo *kw_from;
Martin Probst5f8445b2016-04-24 22:05:09 +0000801 IdentifierInfo *kw_function;
Martin Probst19c7de02017-04-26 12:34:15 +0000802 IdentifierInfo *kw_get;
Daniel Jasper354aa512015-02-19 16:07:32 +0000803 IdentifierInfo *kw_import;
Martin Probst3315aed2018-09-27 06:48:13 +0000804 IdentifierInfo *kw_infer;
Daniel Jasper779c66f2015-12-30 08:00:58 +0000805 IdentifierInfo *kw_is;
Daniel Jasper9f642f72015-09-28 14:28:08 +0000806 IdentifierInfo *kw_let;
Martin Probst72fd75a2016-11-10 16:20:58 +0000807 IdentifierInfo *kw_module;
Martin Probsta81dd0b2017-07-07 13:17:10 +0000808 IdentifierInfo *kw_readonly;
Martin Probst19c7de02017-04-26 12:34:15 +0000809 IdentifierInfo *kw_set;
Martin Probst1b7f9802016-06-23 19:52:32 +0000810 IdentifierInfo *kw_type;
Martin Probst9926abb2017-08-01 17:42:16 +0000811 IdentifierInfo *kw_typeof;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000812 IdentifierInfo *kw_var;
Martin Probst5f8445b2016-04-24 22:05:09 +0000813 IdentifierInfo *kw_yield;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000814
815 // Java keywords.
Daniel Jasper82c92752014-11-21 12:19:07 +0000816 IdentifierInfo *kw_abstract;
Nico Weber4f113492015-09-15 23:48:17 +0000817 IdentifierInfo *kw_assert;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000818 IdentifierInfo *kw_extends;
819 IdentifierInfo *kw_implements;
Daniel Jaspera98b7b02014-11-25 10:05:17 +0000820 IdentifierInfo *kw_instanceof;
Nico Webera644d7f2014-11-10 16:30:02 +0000821 IdentifierInfo *kw_interface;
Nico Webered501662015-01-13 22:32:50 +0000822 IdentifierInfo *kw_native;
Daniel Jasper9b9e0762014-11-26 18:03:42 +0000823 IdentifierInfo *kw_package;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000824 IdentifierInfo *kw_synchronized;
825 IdentifierInfo *kw_throws;
826
Daniel Jasperee4a8a12015-04-22 09:45:42 +0000827 // Pragma keywords.
828 IdentifierInfo *kw_mark;
829
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000830 // Proto keywords.
Daniel Jasperd8d98392015-11-20 14:32:54 +0000831 IdentifierInfo *kw_extend;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000832 IdentifierInfo *kw_option;
833 IdentifierInfo *kw_optional;
834 IdentifierInfo *kw_repeated;
835 IdentifierInfo *kw_required;
836 IdentifierInfo *kw_returns;
Daniel Jasper53395402015-04-07 15:04:40 +0000837
838 // QT keywords.
839 IdentifierInfo *kw_signals;
Daniel Jaspera00de632015-12-01 12:05:04 +0000840 IdentifierInfo *kw_qsignals;
Daniel Jasper53395402015-04-07 15:04:40 +0000841 IdentifierInfo *kw_slots;
842 IdentifierInfo *kw_qslots;
Martin Probst37a7f912017-07-04 15:30:21 +0000843
Paul Hoadcbb726d2019-03-21 13:09:22 +0000844 // C# keywords
845 IdentifierInfo *kw_dollar;
846 IdentifierInfo *kw_base;
847 IdentifierInfo *kw_byte;
848 IdentifierInfo *kw_checked;
849 IdentifierInfo *kw_decimal;
850 IdentifierInfo *kw_delegate;
851 IdentifierInfo *kw_event;
852 IdentifierInfo *kw_fixed;
853 IdentifierInfo *kw_foreach;
854 IdentifierInfo *kw_implicit;
855 IdentifierInfo *kw_internal;
856
857 IdentifierInfo *kw_lock;
858 IdentifierInfo *kw_null;
859 IdentifierInfo *kw_object;
860 IdentifierInfo *kw_out;
861
862 IdentifierInfo *kw_params;
863
864 IdentifierInfo *kw_ref;
865 IdentifierInfo *kw_string;
866 IdentifierInfo *kw_stackalloc;
867 IdentifierInfo *kw_sbyte;
868 IdentifierInfo *kw_sealed;
869 IdentifierInfo *kw_uint;
870 IdentifierInfo *kw_ulong;
871 IdentifierInfo *kw_unchecked;
872 IdentifierInfo *kw_unsafe;
873 IdentifierInfo *kw_ushort;
874
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000875 /// Returns \c true if \p Tok is a true JavaScript identifier, returns
Martin Probst37a7f912017-07-04 15:30:21 +0000876 /// \c false if it is a keyword or a pseudo keyword.
877 bool IsJavaScriptIdentifier(const FormatToken &Tok) const {
878 return Tok.is(tok::identifier) &&
879 JsExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
880 JsExtraKeywords.end();
881 }
882
Paul Hoadcbb726d2019-03-21 13:09:22 +0000883 /// Returns \c true if \p Tok is a C# keyword, returns
884 /// \c false if it is a anything else.
885 bool isCSharpKeyword(const FormatToken &Tok) const {
886 switch (Tok.Tok.getKind()) {
887 case tok::kw_bool:
888 case tok::kw_break:
889 case tok::kw_case:
890 case tok::kw_catch:
891 case tok::kw_char:
892 case tok::kw_class:
893 case tok::kw_const:
894 case tok::kw_continue:
895 case tok::kw_default:
896 case tok::kw_do:
897 case tok::kw_double:
898 case tok::kw_else:
899 case tok::kw_enum:
900 case tok::kw_explicit:
901 case tok::kw_extern:
902 case tok::kw_false:
903 case tok::kw_float:
904 case tok::kw_for:
905 case tok::kw_goto:
906 case tok::kw_if:
907 case tok::kw_int:
908 case tok::kw_long:
909 case tok::kw_namespace:
910 case tok::kw_new:
911 case tok::kw_operator:
912 case tok::kw_private:
913 case tok::kw_protected:
914 case tok::kw_public:
915 case tok::kw_return:
916 case tok::kw_short:
917 case tok::kw_sizeof:
918 case tok::kw_static:
919 case tok::kw_struct:
920 case tok::kw_switch:
921 case tok::kw_this:
922 case tok::kw_throw:
923 case tok::kw_true:
924 case tok::kw_try:
925 case tok::kw_typeof:
926 case tok::kw_using:
927 case tok::kw_virtual:
928 case tok::kw_void:
929 case tok::kw_volatile:
930 case tok::kw_while:
931 return true;
932 default:
933 return Tok.is(tok::identifier) &&
934 CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
935 CSharpExtraKeywords.end();
936 }
937 }
938
Martin Probst37a7f912017-07-04 15:30:21 +0000939private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000940 /// The JavaScript keywords beyond the C++ keyword set.
Martin Probst37a7f912017-07-04 15:30:21 +0000941 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
Paul Hoadcbb726d2019-03-21 13:09:22 +0000942
943 /// The C# keywords beyond the C++ keyword set
944 std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000945};
946
Alexander Kornienko4b672072013-06-03 16:45:03 +0000947} // namespace format
948} // namespace clang
949
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000950#endif