1 //===--- FormatToken.h - Format C++ code ------------------------*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file contains the declaration of the FormatToken, a wrapper
11 /// around Token with additional information related to formatting.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
16 #define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
17 
18 #include "clang/Basic/IdentifierTable.h"
19 #include "clang/Basic/OperatorPrecedence.h"
20 #include "clang/Format/Format.h"
21 #include "clang/Lex/Lexer.h"
22 #include <memory>
23 #include <unordered_set>
24 
25 namespace clang {
26 namespace format {
27 
28 #define LIST_TOKEN_TYPES                                                       \
29   TYPE(ArrayInitializerLSquare)                                                \
30   TYPE(ArraySubscriptLSquare)                                                  \
31   TYPE(AttributeColon)                                                         \
32   TYPE(AttributeMacro)                                                         \
33   TYPE(AttributeParen)                                                         \
34   TYPE(AttributeSquare)                                                        \
35   TYPE(BinaryOperator)                                                         \
36   TYPE(BitFieldColon)                                                          \
37   TYPE(BlockComment)                                                           \
38   TYPE(BracedListLBrace)                                                       \
39   TYPE(CastRParen)                                                             \
40   TYPE(ClassLBrace)                                                            \
41   TYPE(CompoundRequirementLBrace)                                              \
42   TYPE(ConditionalExpr)                                                        \
43   TYPE(ConflictAlternative)                                                    \
44   TYPE(ConflictEnd)                                                            \
45   TYPE(ConflictStart)                                                          \
46   /* l_brace of if/for/while */                                                \
47   TYPE(ControlStatementLBrace)                                                 \
48   TYPE(CppCastLParen)                                                          \
49   TYPE(CtorInitializerColon)                                                   \
50   TYPE(CtorInitializerComma)                                                   \
51   TYPE(DesignatedInitializerLSquare)                                           \
52   TYPE(DesignatedInitializerPeriod)                                            \
53   TYPE(DictLiteral)                                                            \
54   TYPE(ElseLBrace)                                                             \
55   TYPE(EnumLBrace)                                                             \
56   TYPE(FatArrow)                                                               \
57   TYPE(ForEachMacro)                                                           \
58   TYPE(FunctionAnnotationRParen)                                               \
59   TYPE(FunctionDeclarationName)                                                \
60   TYPE(FunctionLBrace)                                                         \
61   TYPE(FunctionLikeOrFreestandingMacro)                                        \
62   TYPE(FunctionTypeLParen)                                                     \
63   TYPE(IfMacro)                                                                \
64   TYPE(ImplicitStringLiteral)                                                  \
65   TYPE(InheritanceColon)                                                       \
66   TYPE(InheritanceComma)                                                       \
67   TYPE(InlineASMBrace)                                                         \
68   TYPE(InlineASMColon)                                                         \
69   TYPE(InlineASMSymbolicNameLSquare)                                           \
70   TYPE(JavaAnnotation)                                                         \
71   TYPE(JsComputedPropertyName)                                                 \
72   TYPE(JsExponentiation)                                                       \
73   TYPE(JsExponentiationEqual)                                                  \
74   TYPE(JsPipePipeEqual)                                                        \
75   TYPE(JsPrivateIdentifier)                                                    \
76   TYPE(JsTypeColon)                                                            \
77   TYPE(JsTypeOperator)                                                         \
78   TYPE(JsTypeOptionalQuestion)                                                 \
79   TYPE(JsAndAndEqual)                                                          \
80   TYPE(LambdaArrow)                                                            \
81   TYPE(LambdaLBrace)                                                           \
82   TYPE(LambdaLSquare)                                                          \
83   TYPE(LeadingJavaAnnotation)                                                  \
84   TYPE(LineComment)                                                            \
85   TYPE(MacroBlockBegin)                                                        \
86   TYPE(MacroBlockEnd)                                                          \
87   TYPE(ModulePartitionColon)                                                   \
88   TYPE(NamespaceMacro)                                                         \
89   TYPE(NonNullAssertion)                                                       \
90   TYPE(NullCoalescingEqual)                                                    \
91   TYPE(NullCoalescingOperator)                                                 \
92   TYPE(NullPropagatingOperator)                                                \
93   TYPE(ObjCBlockLBrace)                                                        \
94   TYPE(ObjCBlockLParen)                                                        \
95   TYPE(ObjCDecl)                                                               \
96   TYPE(ObjCForIn)                                                              \
97   TYPE(ObjCMethodExpr)                                                         \
98   TYPE(ObjCMethodSpecifier)                                                    \
99   TYPE(ObjCProperty)                                                           \
100   TYPE(ObjCStringLiteral)                                                      \
101   TYPE(OverloadedOperator)                                                     \
102   TYPE(OverloadedOperatorLParen)                                               \
103   TYPE(PointerOrReference)                                                     \
104   TYPE(PureVirtualSpecifier)                                                   \
105   TYPE(RangeBasedForLoopColon)                                                 \
106   TYPE(RecordLBrace)                                                           \
107   TYPE(RegexLiteral)                                                           \
108   TYPE(RequiresClause)                                                         \
109   TYPE(RequiresClauseInARequiresExpression)                                    \
110   TYPE(RequiresExpression)                                                     \
111   TYPE(RequiresExpressionLBrace)                                               \
112   TYPE(RequiresExpressionLParen)                                               \
113   TYPE(SelectorName)                                                           \
114   TYPE(StartOfName)                                                            \
115   TYPE(StatementAttributeLikeMacro)                                            \
116   TYPE(StatementMacro)                                                         \
117   TYPE(StructLBrace)                                                           \
118   TYPE(StructuredBindingLSquare)                                               \
119   TYPE(TemplateCloser)                                                         \
120   TYPE(TemplateOpener)                                                         \
121   TYPE(TemplateString)                                                         \
122   TYPE(ProtoExtensionLSquare)                                                  \
123   TYPE(TrailingAnnotation)                                                     \
124   TYPE(TrailingReturnArrow)                                                    \
125   TYPE(TrailingUnaryOperator)                                                  \
126   TYPE(TypeDeclarationParen)                                                   \
127   TYPE(TypenameMacro)                                                          \
128   TYPE(UnaryOperator)                                                          \
129   TYPE(UnionLBrace)                                                            \
130   TYPE(UntouchableMacroFunc)                                                   \
131   TYPE(CSharpStringLiteral)                                                    \
132   TYPE(CSharpNamedArgumentColon)                                               \
133   TYPE(CSharpNullable)                                                         \
134   TYPE(CSharpNullConditionalLSquare)                                           \
135   TYPE(CSharpGenericTypeConstraint)                                            \
136   TYPE(CSharpGenericTypeConstraintColon)                                       \
137   TYPE(CSharpGenericTypeConstraintComma)                                       \
138   TYPE(Unknown)
139 
140 /// Determines the semantic type of a syntactic token, e.g. whether "<" is a
141 /// template opener or binary operator.
142 enum TokenType : uint8_t {
143 #define TYPE(X) TT_##X,
144   LIST_TOKEN_TYPES
145 #undef TYPE
146       NUM_TOKEN_TYPES
147 };
148 
149 /// Determines the name of a token type.
150 const char *getTokenTypeName(TokenType Type);
151 
152 // Represents what type of block a set of braces open.
153 enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit };
154 
155 // The packing kind of a function's parameters.
156 enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive };
157 
158 enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break };
159 
160 /// Roles a token can take in a configured macro expansion.
161 enum MacroRole {
162   /// The token was expanded from a macro argument when formatting the expanded
163   /// token sequence.
164   MR_ExpandedArg,
165   /// The token is part of a macro argument that was previously formatted as
166   /// expansion when formatting the unexpanded macro call.
167   MR_UnexpandedArg,
168   /// The token was expanded from a macro definition, and is not visible as part
169   /// of the macro call.
170   MR_Hidden,
171 };
172 
173 struct FormatToken;
174 
175 /// Contains information on the token's role in a macro expansion.
176 ///
177 /// Given the following definitions:
178 /// A(X) = [ X ]
179 /// B(X) = < X >
180 /// C(X) = X
181 ///
182 /// Consider the macro call:
183 /// A({B(C(C(x)))}) -> [{<x>}]
184 ///
185 /// In this case, the tokens of the unexpanded macro call will have the
186 /// following relevant entries in their macro context (note that formatting
187 /// the unexpanded macro call happens *after* formatting the expanded macro
188 /// call):
189 ///                   A( { B( C( C(x) ) ) } )
190 /// Role:             NN U NN NN NNUN N N U N  (N=None, U=UnexpandedArg)
191 ///
192 ///                   [  { <       x    > } ]
193 /// Role:             H  E H       E    H E H  (H=Hidden, E=ExpandedArg)
194 /// ExpandedFrom[0]:  A  A A       A    A A A
195 /// ExpandedFrom[1]:       B       B    B
196 /// ExpandedFrom[2]:               C
197 /// ExpandedFrom[3]:               C
198 /// StartOfExpansion: 1  0 1       2    0 0 0
199 /// EndOfExpansion:   0  0 0       2    1 0 1
200 struct MacroExpansion {
201   MacroExpansion(MacroRole Role) : Role(Role) {}
202 
203   /// The token's role in the macro expansion.
204   /// When formatting an expanded macro, all tokens that are part of macro
205   /// arguments will be MR_ExpandedArg, while all tokens that are not visible in
206   /// the macro call will be MR_Hidden.
207   /// When formatting an unexpanded macro call, all tokens that are part of
208   /// macro arguments will be MR_UnexpandedArg.
209   MacroRole Role;
210 
211   /// The stack of macro call identifier tokens this token was expanded from.
212   llvm::SmallVector<FormatToken *, 1> ExpandedFrom;
213 
214   /// The number of expansions of which this macro is the first entry.
215   unsigned StartOfExpansion = 0;
216 
217   /// The number of currently open expansions in \c ExpandedFrom this macro is
218   /// the last token in.
219   unsigned EndOfExpansion = 0;
220 };
221 
222 class TokenRole;
223 class AnnotatedLine;
224 
225 /// A wrapper around a \c Token storing information about the
226 /// whitespace characters preceding it.
227 struct FormatToken {
228   FormatToken()
229       : HasUnescapedNewline(false), IsMultiline(false), IsFirst(false),
230         MustBreakBefore(false), IsUnterminatedLiteral(false),
231         CanBreakBefore(false), ClosesTemplateDeclaration(false),
232         StartsBinaryExpression(false), EndsBinaryExpression(false),
233         PartOfMultiVariableDeclStmt(false), ContinuesLineCommentSection(false),
234         Finalized(false), ClosesRequiresClause(false), BlockKind(BK_Unknown),
235         Decision(FD_Unformatted), PackingKind(PPK_Inconclusive),
236         TypeIsFinalized(false), Type(TT_Unknown) {}
237 
238   /// The \c Token.
239   Token Tok;
240 
241   /// The raw text of the token.
242   ///
243   /// Contains the raw token text without leading whitespace and without leading
244   /// escaped newlines.
245   StringRef TokenText;
246 
247   /// A token can have a special role that can carry extra information
248   /// about the token's formatting.
249   /// FIXME: Make FormatToken for parsing and AnnotatedToken two different
250   /// classes and make this a unique_ptr in the AnnotatedToken class.
251   std::shared_ptr<TokenRole> Role;
252 
253   /// The range of the whitespace immediately preceding the \c Token.
254   SourceRange WhitespaceRange;
255 
256   /// Whether there is at least one unescaped newline before the \c
257   /// Token.
258   unsigned HasUnescapedNewline : 1;
259 
260   /// Whether the token text contains newlines (escaped or not).
261   unsigned IsMultiline : 1;
262 
263   /// Indicates that this is the first token of the file.
264   unsigned IsFirst : 1;
265 
266   /// Whether there must be a line break before this token.
267   ///
268   /// This happens for example when a preprocessor directive ended directly
269   /// before the token.
270   unsigned MustBreakBefore : 1;
271 
272   /// Set to \c true if this token is an unterminated literal.
273   unsigned IsUnterminatedLiteral : 1;
274 
275   /// \c true if it is allowed to break before this token.
276   unsigned CanBreakBefore : 1;
277 
278   /// \c true if this is the ">" of "template<..>".
279   unsigned ClosesTemplateDeclaration : 1;
280 
281   /// \c true if this token starts a binary expression, i.e. has at least
282   /// one fake l_paren with a precedence greater than prec::Unknown.
283   unsigned StartsBinaryExpression : 1;
284   /// \c true if this token ends a binary expression.
285   unsigned EndsBinaryExpression : 1;
286 
287   /// Is this token part of a \c DeclStmt defining multiple variables?
288   ///
289   /// Only set if \c Type == \c TT_StartOfName.
290   unsigned PartOfMultiVariableDeclStmt : 1;
291 
292   /// Does this line comment continue a line comment section?
293   ///
294   /// Only set to true if \c Type == \c TT_LineComment.
295   unsigned ContinuesLineCommentSection : 1;
296 
297   /// If \c true, this token has been fully formatted (indented and
298   /// potentially re-formatted inside), and we do not allow further formatting
299   /// changes.
300   unsigned Finalized : 1;
301 
302   /// \c true if this is the last token within requires clause.
303   unsigned ClosesRequiresClause : 1;
304 
305 private:
306   /// Contains the kind of block if this token is a brace.
307   unsigned BlockKind : 2;
308 
309 public:
310   BraceBlockKind getBlockKind() const {
311     return static_cast<BraceBlockKind>(BlockKind);
312   }
313   void setBlockKind(BraceBlockKind BBK) {
314     BlockKind = BBK;
315     assert(getBlockKind() == BBK && "BraceBlockKind overflow!");
316   }
317 
318 private:
319   /// Stores the formatting decision for the token once it was made.
320   unsigned Decision : 2;
321 
322 public:
323   FormatDecision getDecision() const {
324     return static_cast<FormatDecision>(Decision);
325   }
326   void setDecision(FormatDecision D) {
327     Decision = D;
328     assert(getDecision() == D && "FormatDecision overflow!");
329   }
330 
331 private:
332   /// If this is an opening parenthesis, how are the parameters packed?
333   unsigned PackingKind : 2;
334 
335 public:
336   ParameterPackingKind getPackingKind() const {
337     return static_cast<ParameterPackingKind>(PackingKind);
338   }
339   void setPackingKind(ParameterPackingKind K) {
340     PackingKind = K;
341     assert(getPackingKind() == K && "ParameterPackingKind overflow!");
342   }
343 
344 private:
345   unsigned TypeIsFinalized : 1;
346   TokenType Type;
347 
348 public:
349   /// Returns the token's type, e.g. whether "<" is a template opener or
350   /// binary operator.
351   TokenType getType() const { return Type; }
352   void setType(TokenType T) {
353     assert((!TypeIsFinalized || T == Type) &&
354            "Please use overwriteFixedType to change a fixed type.");
355     Type = T;
356   }
357   /// Sets the type and also the finalized flag. This prevents the type to be
358   /// reset in TokenAnnotator::resetTokenMetadata(). If the type needs to be set
359   /// to another one please use overwriteFixedType, or even better remove the
360   /// need to reassign the type.
361   void setFinalizedType(TokenType T) {
362     Type = T;
363     TypeIsFinalized = true;
364   }
365   void overwriteFixedType(TokenType T) {
366     TypeIsFinalized = false;
367     setType(T);
368   }
369   bool isTypeFinalized() const { return TypeIsFinalized; }
370 
371   /// The number of newlines immediately before the \c Token.
372   ///
373   /// This can be used to determine what the user wrote in the original code
374   /// and thereby e.g. leave an empty line between two function definitions.
375   unsigned NewlinesBefore = 0;
376 
377   /// The offset just past the last '\n' in this token's leading
378   /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
379   unsigned LastNewlineOffset = 0;
380 
381   /// The width of the non-whitespace parts of the token (or its first
382   /// line for multi-line tokens) in columns.
383   /// We need this to correctly measure number of columns a token spans.
384   unsigned ColumnWidth = 0;
385 
386   /// Contains the width in columns of the last line of a multi-line
387   /// token.
388   unsigned LastLineColumnWidth = 0;
389 
390   /// The number of spaces that should be inserted before this token.
391   unsigned SpacesRequiredBefore = 0;
392 
393   /// Number of parameters, if this is "(", "[" or "<".
394   unsigned ParameterCount = 0;
395 
396   /// Number of parameters that are nested blocks,
397   /// if this is "(", "[" or "<".
398   unsigned BlockParameterCount = 0;
399 
400   /// If this is a bracket ("<", "(", "[" or "{"), contains the kind of
401   /// the surrounding bracket.
402   tok::TokenKind ParentBracket = tok::unknown;
403 
404   /// The total length of the unwrapped line up to and including this
405   /// token.
406   unsigned TotalLength = 0;
407 
408   /// The original 0-based column of this token, including expanded tabs.
409   /// The configured TabWidth is used as tab width.
410   unsigned OriginalColumn = 0;
411 
412   /// The length of following tokens until the next natural split point,
413   /// or the next token that can be broken.
414   unsigned UnbreakableTailLength = 0;
415 
416   // FIXME: Come up with a 'cleaner' concept.
417   /// The binding strength of a token. This is a combined value of
418   /// operator precedence, parenthesis nesting, etc.
419   unsigned BindingStrength = 0;
420 
421   /// The nesting level of this token, i.e. the number of surrounding (),
422   /// [], {} or <>.
423   unsigned NestingLevel = 0;
424 
425   /// The indent level of this token. Copied from the surrounding line.
426   unsigned IndentLevel = 0;
427 
428   /// Penalty for inserting a line break before this token.
429   unsigned SplitPenalty = 0;
430 
431   /// If this is the first ObjC selector name in an ObjC method
432   /// definition or call, this contains the length of the longest name.
433   ///
434   /// This being set to 0 means that the selectors should not be colon-aligned,
435   /// e.g. because several of them are block-type.
436   unsigned LongestObjCSelectorName = 0;
437 
438   /// If this is the first ObjC selector name in an ObjC method
439   /// definition or call, this contains the number of parts that the whole
440   /// selector consist of.
441   unsigned ObjCSelectorNameParts = 0;
442 
443   /// The 0-based index of the parameter/argument. For ObjC it is set
444   /// for the selector name token.
445   /// For now calculated only for ObjC.
446   unsigned ParameterIndex = 0;
447 
448   /// Stores the number of required fake parentheses and the
449   /// corresponding operator precedence.
450   ///
451   /// If multiple fake parentheses start at a token, this vector stores them in
452   /// reverse order, i.e. inner fake parenthesis first.
453   SmallVector<prec::Level, 4> FakeLParens;
454   /// Insert this many fake ) after this token for correct indentation.
455   unsigned FakeRParens = 0;
456 
457   /// If this is an operator (or "."/"->") in a sequence of operators
458   /// with the same precedence, contains the 0-based operator index.
459   unsigned OperatorIndex = 0;
460 
461   /// If this is an operator (or "."/"->") in a sequence of operators
462   /// with the same precedence, points to the next operator.
463   FormatToken *NextOperator = nullptr;
464 
465   /// If this is a bracket, this points to the matching one.
466   FormatToken *MatchingParen = nullptr;
467 
468   /// The previous token in the unwrapped line.
469   FormatToken *Previous = nullptr;
470 
471   /// The next token in the unwrapped line.
472   FormatToken *Next = nullptr;
473 
474   /// The first token in set of column elements.
475   bool StartsColumn = false;
476 
477   /// This notes the start of the line of an array initializer.
478   bool ArrayInitializerLineStart = false;
479 
480   /// This starts an array initializer.
481   bool IsArrayInitializer = false;
482 
483   /// Is optional and can be removed.
484   bool Optional = false;
485 
486   /// Number of optional braces to be inserted after this token:
487   ///   -1: a single left brace
488   ///    0: no braces
489   ///   >0: number of right braces
490   int8_t BraceCount = 0;
491 
492   /// If this token starts a block, this contains all the unwrapped lines
493   /// in it.
494   SmallVector<AnnotatedLine *, 1> Children;
495 
496   // Contains all attributes related to how this token takes part
497   // in a configured macro expansion.
498   llvm::Optional<MacroExpansion> MacroCtx;
499 
500   bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
501   bool is(TokenType TT) const { return getType() == TT; }
502   bool is(const IdentifierInfo *II) const {
503     return II && II == Tok.getIdentifierInfo();
504   }
505   bool is(tok::PPKeywordKind Kind) const {
506     return Tok.getIdentifierInfo() &&
507            Tok.getIdentifierInfo()->getPPKeywordID() == Kind;
508   }
509   bool is(BraceBlockKind BBK) const { return getBlockKind() == BBK; }
510   bool is(ParameterPackingKind PPK) const { return getPackingKind() == PPK; }
511 
512   template <typename A, typename B> bool isOneOf(A K1, B K2) const {
513     return is(K1) || is(K2);
514   }
515   template <typename A, typename B, typename... Ts>
516   bool isOneOf(A K1, B K2, Ts... Ks) const {
517     return is(K1) || isOneOf(K2, Ks...);
518   }
519   template <typename T> bool isNot(T Kind) const { return !is(Kind); }
520 
521   bool isIf(bool AllowConstexprMacro = true) const {
522     return is(tok::kw_if) || endsSequence(tok::kw_constexpr, tok::kw_if) ||
523            (endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro);
524   }
525 
526   bool closesScopeAfterBlock() const {
527     if (getBlockKind() == BK_Block)
528       return true;
529     if (closesScope())
530       return Previous->closesScopeAfterBlock();
531     return false;
532   }
533 
534   /// \c true if this token starts a sequence with the given tokens in order,
535   /// following the ``Next`` pointers, ignoring comments.
536   template <typename A, typename... Ts>
537   bool startsSequence(A K1, Ts... Tokens) const {
538     return startsSequenceInternal(K1, Tokens...);
539   }
540 
541   /// \c true if this token ends a sequence with the given tokens in order,
542   /// following the ``Previous`` pointers, ignoring comments.
543   /// For example, given tokens [T1, T2, T3], the function returns true if
544   /// 3 tokens ending at this (ignoring comments) are [T3, T2, T1]. In other
545   /// words, the tokens passed to this function need to the reverse of the
546   /// order the tokens appear in code.
547   template <typename A, typename... Ts>
548   bool endsSequence(A K1, Ts... Tokens) const {
549     return endsSequenceInternal(K1, Tokens...);
550   }
551 
552   bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
553 
554   bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
555     return Tok.isObjCAtKeyword(Kind);
556   }
557 
558   bool isAccessSpecifier(bool ColonRequired = true) const {
559     return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
560            (!ColonRequired || (Next && Next->is(tok::colon)));
561   }
562 
563   bool canBePointerOrReferenceQualifier() const {
564     return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile,
565                    tok::kw___attribute, tok::kw__Nonnull, tok::kw__Nullable,
566                    tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64,
567                    TT_AttributeMacro);
568   }
569 
570   /// Determine whether the token is a simple-type-specifier.
571   LLVM_NODISCARD bool isSimpleTypeSpecifier() const;
572 
573   LLVM_NODISCARD bool isTypeOrIdentifier() const;
574 
575   bool isObjCAccessSpecifier() const {
576     return is(tok::at) && Next &&
577            (Next->isObjCAtKeyword(tok::objc_public) ||
578             Next->isObjCAtKeyword(tok::objc_protected) ||
579             Next->isObjCAtKeyword(tok::objc_package) ||
580             Next->isObjCAtKeyword(tok::objc_private));
581   }
582 
583   /// Returns whether \p Tok is ([{ or an opening < of a template or in
584   /// protos.
585   bool opensScope() const {
586     if (is(TT_TemplateString) && TokenText.endswith("${"))
587       return true;
588     if (is(TT_DictLiteral) && is(tok::less))
589       return true;
590     return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
591                    TT_TemplateOpener);
592   }
593   /// Returns whether \p Tok is )]} or a closing > of a template or in
594   /// protos.
595   bool closesScope() const {
596     if (is(TT_TemplateString) && TokenText.startswith("}"))
597       return true;
598     if (is(TT_DictLiteral) && is(tok::greater))
599       return true;
600     return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
601                    TT_TemplateCloser);
602   }
603 
604   /// Returns \c true if this is a "." or "->" accessing a member.
605   bool isMemberAccess() const {
606     return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
607            !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
608                     TT_LambdaArrow, TT_LeadingJavaAnnotation);
609   }
610 
611   bool isUnaryOperator() const {
612     switch (Tok.getKind()) {
613     case tok::plus:
614     case tok::plusplus:
615     case tok::minus:
616     case tok::minusminus:
617     case tok::exclaim:
618     case tok::tilde:
619     case tok::kw_sizeof:
620     case tok::kw_alignof:
621       return true;
622     default:
623       return false;
624     }
625   }
626 
627   bool isBinaryOperator() const {
628     // Comma is a binary operator, but does not behave as such wrt. formatting.
629     return getPrecedence() > prec::Comma;
630   }
631 
632   bool isTrailingComment() const {
633     return is(tok::comment) &&
634            (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
635   }
636 
637   /// Returns \c true if this is a keyword that can be used
638   /// like a function call (e.g. sizeof, typeid, ...).
639   bool isFunctionLikeKeyword() const {
640     switch (Tok.getKind()) {
641     case tok::kw_throw:
642     case tok::kw_typeid:
643     case tok::kw_return:
644     case tok::kw_sizeof:
645     case tok::kw_alignof:
646     case tok::kw_alignas:
647     case tok::kw_decltype:
648     case tok::kw_noexcept:
649     case tok::kw_static_assert:
650     case tok::kw__Atomic:
651     case tok::kw___attribute:
652     case tok::kw___underlying_type:
653     case tok::kw_requires:
654       return true;
655     default:
656       return false;
657     }
658   }
659 
660   /// Returns \c true if this is a string literal that's like a label,
661   /// e.g. ends with "=" or ":".
662   bool isLabelString() const {
663     if (!is(tok::string_literal))
664       return false;
665     StringRef Content = TokenText;
666     if (Content.startswith("\"") || Content.startswith("'"))
667       Content = Content.drop_front(1);
668     if (Content.endswith("\"") || Content.endswith("'"))
669       Content = Content.drop_back(1);
670     Content = Content.trim();
671     return Content.size() > 1 &&
672            (Content.back() == ':' || Content.back() == '=');
673   }
674 
675   /// Returns actual token start location without leading escaped
676   /// newlines and whitespace.
677   ///
678   /// This can be different to Tok.getLocation(), which includes leading escaped
679   /// newlines.
680   SourceLocation getStartOfNonWhitespace() const {
681     return WhitespaceRange.getEnd();
682   }
683 
684   /// Returns \c true if the range of whitespace immediately preceding the \c
685   /// Token is not empty.
686   bool hasWhitespaceBefore() const {
687     return WhitespaceRange.getBegin() != WhitespaceRange.getEnd();
688   }
689 
690   prec::Level getPrecedence() const {
691     return getBinOpPrecedence(Tok.getKind(), /*GreaterThanIsOperator=*/true,
692                               /*CPlusPlus11=*/true);
693   }
694 
695   /// Returns the previous token ignoring comments.
696   LLVM_NODISCARD FormatToken *getPreviousNonComment() const {
697     FormatToken *Tok = Previous;
698     while (Tok && Tok->is(tok::comment))
699       Tok = Tok->Previous;
700     return Tok;
701   }
702 
703   /// Returns the next token ignoring comments.
704   LLVM_NODISCARD const FormatToken *getNextNonComment() const {
705     const FormatToken *Tok = Next;
706     while (Tok && Tok->is(tok::comment))
707       Tok = Tok->Next;
708     return Tok;
709   }
710 
711   /// Returns \c true if this tokens starts a block-type list, i.e. a
712   /// list that should be indented with a block indent.
713   LLVM_NODISCARD bool opensBlockOrBlockTypeList(const FormatStyle &Style) const;
714 
715   /// Returns whether the token is the left square bracket of a C++
716   /// structured binding declaration.
717   bool isCppStructuredBinding(const FormatStyle &Style) const {
718     if (!Style.isCpp() || isNot(tok::l_square))
719       return false;
720     const FormatToken *T = this;
721     do {
722       T = T->getPreviousNonComment();
723     } while (T && T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
724                              tok::ampamp));
725     return T && T->is(tok::kw_auto);
726   }
727 
728   /// Same as opensBlockOrBlockTypeList, but for the closing token.
729   bool closesBlockOrBlockTypeList(const FormatStyle &Style) const {
730     if (is(TT_TemplateString) && closesScope())
731       return true;
732     return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style);
733   }
734 
735   /// Return the actual namespace token, if this token starts a namespace
736   /// block.
737   const FormatToken *getNamespaceToken() const {
738     const FormatToken *NamespaceTok = this;
739     if (is(tok::comment))
740       NamespaceTok = NamespaceTok->getNextNonComment();
741     // Detect "(inline|export)? namespace" in the beginning of a line.
742     if (NamespaceTok && NamespaceTok->isOneOf(tok::kw_inline, tok::kw_export))
743       NamespaceTok = NamespaceTok->getNextNonComment();
744     return NamespaceTok &&
745                    NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro)
746                ? NamespaceTok
747                : nullptr;
748   }
749 
750   void copyFrom(const FormatToken &Tok) { *this = Tok; }
751 
752 private:
753   // Only allow copying via the explicit copyFrom method.
754   FormatToken(const FormatToken &) = delete;
755   FormatToken &operator=(const FormatToken &) = default;
756 
757   template <typename A, typename... Ts>
758   bool startsSequenceInternal(A K1, Ts... Tokens) const {
759     if (is(tok::comment) && Next)
760       return Next->startsSequenceInternal(K1, Tokens...);
761     return is(K1) && Next && Next->startsSequenceInternal(Tokens...);
762   }
763 
764   template <typename A> bool startsSequenceInternal(A K1) const {
765     if (is(tok::comment) && Next)
766       return Next->startsSequenceInternal(K1);
767     return is(K1);
768   }
769 
770   template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const {
771     if (is(tok::comment) && Previous)
772       return Previous->endsSequenceInternal(K1);
773     return is(K1);
774   }
775 
776   template <typename A, typename... Ts>
777   bool endsSequenceInternal(A K1, Ts... Tokens) const {
778     if (is(tok::comment) && Previous)
779       return Previous->endsSequenceInternal(K1, Tokens...);
780     return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...);
781   }
782 };
783 
784 class ContinuationIndenter;
785 struct LineState;
786 
787 class TokenRole {
788 public:
789   TokenRole(const FormatStyle &Style) : Style(Style) {}
790   virtual ~TokenRole();
791 
792   /// After the \c TokenAnnotator has finished annotating all the tokens,
793   /// this function precomputes required information for formatting.
794   virtual void precomputeFormattingInfos(const FormatToken *Token);
795 
796   /// Apply the special formatting that the given role demands.
797   ///
798   /// Assumes that the token having this role is already formatted.
799   ///
800   /// Continues formatting from \p State leaving indentation to \p Indenter and
801   /// returns the total penalty that this formatting incurs.
802   virtual unsigned formatFromToken(LineState &State,
803                                    ContinuationIndenter *Indenter,
804                                    bool DryRun) {
805     return 0;
806   }
807 
808   /// Same as \c formatFromToken, but assumes that the first token has
809   /// already been set thereby deciding on the first line break.
810   virtual unsigned formatAfterToken(LineState &State,
811                                     ContinuationIndenter *Indenter,
812                                     bool DryRun) {
813     return 0;
814   }
815 
816   /// Notifies the \c Role that a comma was found.
817   virtual void CommaFound(const FormatToken *Token) {}
818 
819   virtual const FormatToken *lastComma() { return nullptr; }
820 
821 protected:
822   const FormatStyle &Style;
823 };
824 
825 class CommaSeparatedList : public TokenRole {
826 public:
827   CommaSeparatedList(const FormatStyle &Style)
828       : TokenRole(Style), HasNestedBracedList(false) {}
829 
830   void precomputeFormattingInfos(const FormatToken *Token) override;
831 
832   unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
833                             bool DryRun) override;
834 
835   unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
836                            bool DryRun) override;
837 
838   /// Adds \p Token as the next comma to the \c CommaSeparated list.
839   void CommaFound(const FormatToken *Token) override {
840     Commas.push_back(Token);
841   }
842 
843   const FormatToken *lastComma() override {
844     if (Commas.empty())
845       return nullptr;
846     return Commas.back();
847   }
848 
849 private:
850   /// A struct that holds information on how to format a given list with
851   /// a specific number of columns.
852   struct ColumnFormat {
853     /// The number of columns to use.
854     unsigned Columns;
855 
856     /// The total width in characters.
857     unsigned TotalWidth;
858 
859     /// The number of lines required for this format.
860     unsigned LineCount;
861 
862     /// The size of each column in characters.
863     SmallVector<unsigned, 8> ColumnSizes;
864   };
865 
866   /// Calculate which \c ColumnFormat fits best into
867   /// \p RemainingCharacters.
868   const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
869 
870   /// The ordered \c FormatTokens making up the commas of this list.
871   SmallVector<const FormatToken *, 8> Commas;
872 
873   /// The length of each of the list's items in characters including the
874   /// trailing comma.
875   SmallVector<unsigned, 8> ItemLengths;
876 
877   /// Precomputed formats that can be used for this list.
878   SmallVector<ColumnFormat, 4> Formats;
879 
880   bool HasNestedBracedList;
881 };
882 
883 /// Encapsulates keywords that are context sensitive or for languages not
884 /// properly supported by Clang's lexer.
885 struct AdditionalKeywords {
886   AdditionalKeywords(IdentifierTable &IdentTable) {
887     kw_final = &IdentTable.get("final");
888     kw_override = &IdentTable.get("override");
889     kw_in = &IdentTable.get("in");
890     kw_of = &IdentTable.get("of");
891     kw_CF_CLOSED_ENUM = &IdentTable.get("CF_CLOSED_ENUM");
892     kw_CF_ENUM = &IdentTable.get("CF_ENUM");
893     kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
894     kw_NS_CLOSED_ENUM = &IdentTable.get("NS_CLOSED_ENUM");
895     kw_NS_ENUM = &IdentTable.get("NS_ENUM");
896     kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
897 
898     kw_as = &IdentTable.get("as");
899     kw_async = &IdentTable.get("async");
900     kw_await = &IdentTable.get("await");
901     kw_declare = &IdentTable.get("declare");
902     kw_finally = &IdentTable.get("finally");
903     kw_from = &IdentTable.get("from");
904     kw_function = &IdentTable.get("function");
905     kw_get = &IdentTable.get("get");
906     kw_import = &IdentTable.get("import");
907     kw_infer = &IdentTable.get("infer");
908     kw_is = &IdentTable.get("is");
909     kw_let = &IdentTable.get("let");
910     kw_module = &IdentTable.get("module");
911     kw_readonly = &IdentTable.get("readonly");
912     kw_set = &IdentTable.get("set");
913     kw_type = &IdentTable.get("type");
914     kw_typeof = &IdentTable.get("typeof");
915     kw_var = &IdentTable.get("var");
916     kw_yield = &IdentTable.get("yield");
917 
918     kw_abstract = &IdentTable.get("abstract");
919     kw_assert = &IdentTable.get("assert");
920     kw_extends = &IdentTable.get("extends");
921     kw_implements = &IdentTable.get("implements");
922     kw_instanceof = &IdentTable.get("instanceof");
923     kw_interface = &IdentTable.get("interface");
924     kw_native = &IdentTable.get("native");
925     kw_package = &IdentTable.get("package");
926     kw_synchronized = &IdentTable.get("synchronized");
927     kw_throws = &IdentTable.get("throws");
928     kw___except = &IdentTable.get("__except");
929     kw___has_include = &IdentTable.get("__has_include");
930     kw___has_include_next = &IdentTable.get("__has_include_next");
931 
932     kw_mark = &IdentTable.get("mark");
933 
934     kw_extend = &IdentTable.get("extend");
935     kw_option = &IdentTable.get("option");
936     kw_optional = &IdentTable.get("optional");
937     kw_repeated = &IdentTable.get("repeated");
938     kw_required = &IdentTable.get("required");
939     kw_returns = &IdentTable.get("returns");
940 
941     kw_signals = &IdentTable.get("signals");
942     kw_qsignals = &IdentTable.get("Q_SIGNALS");
943     kw_slots = &IdentTable.get("slots");
944     kw_qslots = &IdentTable.get("Q_SLOTS");
945 
946     // For internal clang-format use.
947     kw_internal_ident_after_define =
948         &IdentTable.get("__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__");
949 
950     // C# keywords
951     kw_dollar = &IdentTable.get("dollar");
952     kw_base = &IdentTable.get("base");
953     kw_byte = &IdentTable.get("byte");
954     kw_checked = &IdentTable.get("checked");
955     kw_decimal = &IdentTable.get("decimal");
956     kw_delegate = &IdentTable.get("delegate");
957     kw_event = &IdentTable.get("event");
958     kw_fixed = &IdentTable.get("fixed");
959     kw_foreach = &IdentTable.get("foreach");
960     kw_init = &IdentTable.get("init");
961     kw_implicit = &IdentTable.get("implicit");
962     kw_internal = &IdentTable.get("internal");
963     kw_lock = &IdentTable.get("lock");
964     kw_null = &IdentTable.get("null");
965     kw_object = &IdentTable.get("object");
966     kw_out = &IdentTable.get("out");
967     kw_params = &IdentTable.get("params");
968     kw_ref = &IdentTable.get("ref");
969     kw_string = &IdentTable.get("string");
970     kw_stackalloc = &IdentTable.get("stackalloc");
971     kw_sbyte = &IdentTable.get("sbyte");
972     kw_sealed = &IdentTable.get("sealed");
973     kw_uint = &IdentTable.get("uint");
974     kw_ulong = &IdentTable.get("ulong");
975     kw_unchecked = &IdentTable.get("unchecked");
976     kw_unsafe = &IdentTable.get("unsafe");
977     kw_ushort = &IdentTable.get("ushort");
978     kw_when = &IdentTable.get("when");
979     kw_where = &IdentTable.get("where");
980 
981     // Keep this at the end of the constructor to make sure everything here
982     // is
983     // already initialized.
984     JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
985         {kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
986          kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_override,
987          kw_readonly, kw_set, kw_type, kw_typeof, kw_var, kw_yield,
988          // Keywords from the Java section.
989          kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
990 
991     CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>(
992         {kw_base, kw_byte, kw_checked, kw_decimal, kw_delegate, kw_event,
993          kw_fixed, kw_foreach, kw_implicit, kw_in, kw_init, kw_interface,
994          kw_internal, kw_is, kw_lock, kw_null, kw_object, kw_out, kw_override,
995          kw_params, kw_readonly, kw_ref, kw_string, kw_stackalloc, kw_sbyte,
996          kw_sealed, kw_uint, kw_ulong, kw_unchecked, kw_unsafe, kw_ushort,
997          kw_when, kw_where,
998          // Keywords from the JavaScript section.
999          kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
1000          kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_readonly,
1001          kw_set, kw_type, kw_typeof, kw_var, kw_yield,
1002          // Keywords from the Java section.
1003          kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
1004   }
1005 
1006   // Context sensitive keywords.
1007   IdentifierInfo *kw_final;
1008   IdentifierInfo *kw_override;
1009   IdentifierInfo *kw_in;
1010   IdentifierInfo *kw_of;
1011   IdentifierInfo *kw_CF_CLOSED_ENUM;
1012   IdentifierInfo *kw_CF_ENUM;
1013   IdentifierInfo *kw_CF_OPTIONS;
1014   IdentifierInfo *kw_NS_CLOSED_ENUM;
1015   IdentifierInfo *kw_NS_ENUM;
1016   IdentifierInfo *kw_NS_OPTIONS;
1017   IdentifierInfo *kw___except;
1018   IdentifierInfo *kw___has_include;
1019   IdentifierInfo *kw___has_include_next;
1020 
1021   // JavaScript keywords.
1022   IdentifierInfo *kw_as;
1023   IdentifierInfo *kw_async;
1024   IdentifierInfo *kw_await;
1025   IdentifierInfo *kw_declare;
1026   IdentifierInfo *kw_finally;
1027   IdentifierInfo *kw_from;
1028   IdentifierInfo *kw_function;
1029   IdentifierInfo *kw_get;
1030   IdentifierInfo *kw_import;
1031   IdentifierInfo *kw_infer;
1032   IdentifierInfo *kw_is;
1033   IdentifierInfo *kw_let;
1034   IdentifierInfo *kw_module;
1035   IdentifierInfo *kw_readonly;
1036   IdentifierInfo *kw_set;
1037   IdentifierInfo *kw_type;
1038   IdentifierInfo *kw_typeof;
1039   IdentifierInfo *kw_var;
1040   IdentifierInfo *kw_yield;
1041 
1042   // Java keywords.
1043   IdentifierInfo *kw_abstract;
1044   IdentifierInfo *kw_assert;
1045   IdentifierInfo *kw_extends;
1046   IdentifierInfo *kw_implements;
1047   IdentifierInfo *kw_instanceof;
1048   IdentifierInfo *kw_interface;
1049   IdentifierInfo *kw_native;
1050   IdentifierInfo *kw_package;
1051   IdentifierInfo *kw_synchronized;
1052   IdentifierInfo *kw_throws;
1053 
1054   // Pragma keywords.
1055   IdentifierInfo *kw_mark;
1056 
1057   // Proto keywords.
1058   IdentifierInfo *kw_extend;
1059   IdentifierInfo *kw_option;
1060   IdentifierInfo *kw_optional;
1061   IdentifierInfo *kw_repeated;
1062   IdentifierInfo *kw_required;
1063   IdentifierInfo *kw_returns;
1064 
1065   // QT keywords.
1066   IdentifierInfo *kw_signals;
1067   IdentifierInfo *kw_qsignals;
1068   IdentifierInfo *kw_slots;
1069   IdentifierInfo *kw_qslots;
1070 
1071   // For internal use by clang-format.
1072   IdentifierInfo *kw_internal_ident_after_define;
1073 
1074   // C# keywords
1075   IdentifierInfo *kw_dollar;
1076   IdentifierInfo *kw_base;
1077   IdentifierInfo *kw_byte;
1078   IdentifierInfo *kw_checked;
1079   IdentifierInfo *kw_decimal;
1080   IdentifierInfo *kw_delegate;
1081   IdentifierInfo *kw_event;
1082   IdentifierInfo *kw_fixed;
1083   IdentifierInfo *kw_foreach;
1084   IdentifierInfo *kw_implicit;
1085   IdentifierInfo *kw_init;
1086   IdentifierInfo *kw_internal;
1087 
1088   IdentifierInfo *kw_lock;
1089   IdentifierInfo *kw_null;
1090   IdentifierInfo *kw_object;
1091   IdentifierInfo *kw_out;
1092 
1093   IdentifierInfo *kw_params;
1094 
1095   IdentifierInfo *kw_ref;
1096   IdentifierInfo *kw_string;
1097   IdentifierInfo *kw_stackalloc;
1098   IdentifierInfo *kw_sbyte;
1099   IdentifierInfo *kw_sealed;
1100   IdentifierInfo *kw_uint;
1101   IdentifierInfo *kw_ulong;
1102   IdentifierInfo *kw_unchecked;
1103   IdentifierInfo *kw_unsafe;
1104   IdentifierInfo *kw_ushort;
1105   IdentifierInfo *kw_when;
1106   IdentifierInfo *kw_where;
1107 
1108   /// Returns \c true if \p Tok is a true JavaScript identifier, returns
1109   /// \c false if it is a keyword or a pseudo keyword.
1110   /// If \c AcceptIdentifierName is true, returns true not only for keywords,
1111   // but also for IdentifierName tokens (aka pseudo-keywords), such as
1112   // ``yield``.
1113   bool IsJavaScriptIdentifier(const FormatToken &Tok,
1114                               bool AcceptIdentifierName = true) const {
1115     // Based on the list of JavaScript & TypeScript keywords here:
1116     // https://github.com/microsoft/TypeScript/blob/main/src/compiler/scanner.ts#L74
1117     switch (Tok.Tok.getKind()) {
1118     case tok::kw_break:
1119     case tok::kw_case:
1120     case tok::kw_catch:
1121     case tok::kw_class:
1122     case tok::kw_continue:
1123     case tok::kw_const:
1124     case tok::kw_default:
1125     case tok::kw_delete:
1126     case tok::kw_do:
1127     case tok::kw_else:
1128     case tok::kw_enum:
1129     case tok::kw_export:
1130     case tok::kw_false:
1131     case tok::kw_for:
1132     case tok::kw_if:
1133     case tok::kw_import:
1134     case tok::kw_module:
1135     case tok::kw_new:
1136     case tok::kw_private:
1137     case tok::kw_protected:
1138     case tok::kw_public:
1139     case tok::kw_return:
1140     case tok::kw_static:
1141     case tok::kw_switch:
1142     case tok::kw_this:
1143     case tok::kw_throw:
1144     case tok::kw_true:
1145     case tok::kw_try:
1146     case tok::kw_typeof:
1147     case tok::kw_void:
1148     case tok::kw_while:
1149       // These are JS keywords that are lexed by LLVM/clang as keywords.
1150       return false;
1151     case tok::identifier: {
1152       // For identifiers, make sure they are true identifiers, excluding the
1153       // JavaScript pseudo-keywords (not lexed by LLVM/clang as keywords).
1154       bool IsPseudoKeyword =
1155           JsExtraKeywords.find(Tok.Tok.getIdentifierInfo()) !=
1156           JsExtraKeywords.end();
1157       return AcceptIdentifierName || !IsPseudoKeyword;
1158     }
1159     default:
1160       // Other keywords are handled in the switch below, to avoid problems due
1161       // to duplicate case labels when using the #include trick.
1162       break;
1163     }
1164 
1165     switch (Tok.Tok.getKind()) {
1166       // Handle C++ keywords not included above: these are all JS identifiers.
1167 #define KEYWORD(X, Y) case tok::kw_##X:
1168 #include "clang/Basic/TokenKinds.def"
1169       // #undef KEYWORD is not needed -- it's #undef-ed at the end of
1170       // TokenKinds.def
1171       return true;
1172     default:
1173       // All other tokens (punctuation etc) are not JS identifiers.
1174       return false;
1175     }
1176   }
1177 
1178   /// Returns \c true if \p Tok is a C# keyword, returns
1179   /// \c false if it is a anything else.
1180   bool isCSharpKeyword(const FormatToken &Tok) const {
1181     switch (Tok.Tok.getKind()) {
1182     case tok::kw_bool:
1183     case tok::kw_break:
1184     case tok::kw_case:
1185     case tok::kw_catch:
1186     case tok::kw_char:
1187     case tok::kw_class:
1188     case tok::kw_const:
1189     case tok::kw_continue:
1190     case tok::kw_default:
1191     case tok::kw_do:
1192     case tok::kw_double:
1193     case tok::kw_else:
1194     case tok::kw_enum:
1195     case tok::kw_explicit:
1196     case tok::kw_extern:
1197     case tok::kw_false:
1198     case tok::kw_float:
1199     case tok::kw_for:
1200     case tok::kw_goto:
1201     case tok::kw_if:
1202     case tok::kw_int:
1203     case tok::kw_long:
1204     case tok::kw_namespace:
1205     case tok::kw_new:
1206     case tok::kw_operator:
1207     case tok::kw_private:
1208     case tok::kw_protected:
1209     case tok::kw_public:
1210     case tok::kw_return:
1211     case tok::kw_short:
1212     case tok::kw_sizeof:
1213     case tok::kw_static:
1214     case tok::kw_struct:
1215     case tok::kw_switch:
1216     case tok::kw_this:
1217     case tok::kw_throw:
1218     case tok::kw_true:
1219     case tok::kw_try:
1220     case tok::kw_typeof:
1221     case tok::kw_using:
1222     case tok::kw_virtual:
1223     case tok::kw_void:
1224     case tok::kw_volatile:
1225     case tok::kw_while:
1226       return true;
1227     default:
1228       return Tok.is(tok::identifier) &&
1229              CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
1230                  CSharpExtraKeywords.end();
1231     }
1232   }
1233 
1234 private:
1235   /// The JavaScript keywords beyond the C++ keyword set.
1236   std::unordered_set<IdentifierInfo *> JsExtraKeywords;
1237 
1238   /// The C# keywords beyond the C++ keyword set
1239   std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
1240 };
1241 
1242 } // namespace format
1243 } // namespace clang
1244 
1245 #endif
1246