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