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