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(AttributeParen)                                                         \
33   TYPE(AttributeSquare)                                                        \
34   TYPE(BinaryOperator)                                                         \
35   TYPE(BitFieldColon)                                                          \
36   TYPE(BlockComment)                                                           \
37   TYPE(CastRParen)                                                             \
38   TYPE(ConditionalExpr)                                                        \
39   TYPE(ConflictAlternative)                                                    \
40   TYPE(ConflictEnd)                                                            \
41   TYPE(ConflictStart)                                                          \
42   TYPE(CtorInitializerColon)                                                   \
43   TYPE(CtorInitializerComma)                                                   \
44   TYPE(DesignatedInitializerLSquare)                                           \
45   TYPE(DesignatedInitializerPeriod)                                            \
46   TYPE(DictLiteral)                                                            \
47   TYPE(ForEachMacro)                                                           \
48   TYPE(FunctionAnnotationRParen)                                               \
49   TYPE(FunctionDeclarationName)                                                \
50   TYPE(FunctionLBrace)                                                         \
51   TYPE(FunctionTypeLParen)                                                     \
52   TYPE(ImplicitStringLiteral)                                                  \
53   TYPE(InheritanceColon)                                                       \
54   TYPE(InheritanceComma)                                                       \
55   TYPE(InlineASMBrace)                                                         \
56   TYPE(InlineASMColon)                                                         \
57   TYPE(InlineASMSymbolicNameLSquare)                                           \
58   TYPE(JavaAnnotation)                                                         \
59   TYPE(JsComputedPropertyName)                                                 \
60   TYPE(JsExponentiation)                                                       \
61   TYPE(JsExponentiationEqual)                                                  \
62   TYPE(JsFatArrow)                                                             \
63   TYPE(JsNonNullAssertion)                                                     \
64   TYPE(JsNullishCoalescingOperator)                                            \
65   TYPE(JsNullPropagatingOperator)                                              \
66   TYPE(JsPrivateIdentifier)                                                    \
67   TYPE(JsTypeColon)                                                            \
68   TYPE(JsTypeOperator)                                                         \
69   TYPE(JsTypeOptionalQuestion)                                                 \
70   TYPE(LambdaArrow)                                                            \
71   TYPE(LambdaLBrace)                                                           \
72   TYPE(LambdaLSquare)                                                          \
73   TYPE(LeadingJavaAnnotation)                                                  \
74   TYPE(LineComment)                                                            \
75   TYPE(MacroBlockBegin)                                                        \
76   TYPE(MacroBlockEnd)                                                          \
77   TYPE(NamespaceMacro)                                                         \
78   TYPE(ObjCBlockLBrace)                                                        \
79   TYPE(ObjCBlockLParen)                                                        \
80   TYPE(ObjCDecl)                                                               \
81   TYPE(ObjCForIn)                                                              \
82   TYPE(ObjCMethodExpr)                                                         \
83   TYPE(ObjCMethodSpecifier)                                                    \
84   TYPE(ObjCProperty)                                                           \
85   TYPE(ObjCStringLiteral)                                                      \
86   TYPE(OverloadedOperator)                                                     \
87   TYPE(OverloadedOperatorLParen)                                               \
88   TYPE(PointerOrReference)                                                     \
89   TYPE(PureVirtualSpecifier)                                                   \
90   TYPE(RangeBasedForLoopColon)                                                 \
91   TYPE(RegexLiteral)                                                           \
92   TYPE(SelectorName)                                                           \
93   TYPE(StartOfName)                                                            \
94   TYPE(StatementMacro)                                                         \
95   TYPE(StructuredBindingLSquare)                                               \
96   TYPE(TemplateCloser)                                                         \
97   TYPE(TemplateOpener)                                                         \
98   TYPE(TemplateString)                                                         \
99   TYPE(ProtoExtensionLSquare)                                                  \
100   TYPE(TrailingAnnotation)                                                     \
101   TYPE(TrailingReturnArrow)                                                    \
102   TYPE(TrailingUnaryOperator)                                                  \
103   TYPE(TypenameMacro)                                                          \
104   TYPE(UnaryOperator)                                                          \
105   TYPE(CSharpStringLiteral)                                                    \
106   TYPE(CSharpNamedArgumentColon)                                               \
107   TYPE(CSharpNullable)                                                         \
108   TYPE(CSharpNullCoalescing)                                                   \
109   TYPE(CSharpNullConditional)                                                  \
110   TYPE(CSharpNullConditionalLSquare)                                           \
111   TYPE(Unknown)
112 
113 enum TokenType {
114 #define TYPE(X) TT_##X,
115   LIST_TOKEN_TYPES
116 #undef TYPE
117       NUM_TOKEN_TYPES
118 };
119 
120 /// Determines the name of a token type.
121 const char *getTokenTypeName(TokenType Type);
122 
123 // Represents what type of block a set of braces open.
124 enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit };
125 
126 // The packing kind of a function's parameters.
127 enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive };
128 
129 enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break };
130 
131 class TokenRole;
132 class AnnotatedLine;
133 
134 /// A wrapper around a \c Token storing information about the
135 /// whitespace characters preceding it.
136 struct FormatToken {
137   FormatToken() {}
138 
139   /// The \c Token.
140   Token Tok;
141 
142   /// The number of newlines immediately before the \c Token.
143   ///
144   /// This can be used to determine what the user wrote in the original code
145   /// and thereby e.g. leave an empty line between two function definitions.
146   unsigned NewlinesBefore = 0;
147 
148   /// Whether there is at least one unescaped newline before the \c
149   /// Token.
150   bool HasUnescapedNewline = false;
151 
152   /// The range of the whitespace immediately preceding the \c Token.
153   SourceRange WhitespaceRange;
154 
155   /// The offset just past the last '\n' in this token's leading
156   /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
157   unsigned LastNewlineOffset = 0;
158 
159   /// The width of the non-whitespace parts of the token (or its first
160   /// line for multi-line tokens) in columns.
161   /// We need this to correctly measure number of columns a token spans.
162   unsigned ColumnWidth = 0;
163 
164   /// Contains the width in columns of the last line of a multi-line
165   /// token.
166   unsigned LastLineColumnWidth = 0;
167 
168   /// Whether the token text contains newlines (escaped or not).
169   bool IsMultiline = false;
170 
171   /// Indicates that this is the first token of the file.
172   bool IsFirst = false;
173 
174   /// Whether there must be a line break before this token.
175   ///
176   /// This happens for example when a preprocessor directive ended directly
177   /// before the token.
178   bool MustBreakBefore = false;
179 
180   /// The raw text of the token.
181   ///
182   /// Contains the raw token text without leading whitespace and without leading
183   /// escaped newlines.
184   StringRef TokenText;
185 
186   /// Set to \c true if this token is an unterminated literal.
187   bool IsUnterminatedLiteral = 0;
188 
189   /// Contains the kind of block if this token is a brace.
190   BraceBlockKind BlockKind = BK_Unknown;
191 
192   TokenType Type = TT_Unknown;
193 
194   /// The number of spaces that should be inserted before this token.
195   unsigned SpacesRequiredBefore = 0;
196 
197   /// \c true if it is allowed to break before this token.
198   bool CanBreakBefore = false;
199 
200   /// \c true if this is the ">" of "template<..>".
201   bool ClosesTemplateDeclaration = false;
202 
203   /// Number of parameters, if this is "(", "[" or "<".
204   unsigned ParameterCount = 0;
205 
206   /// Number of parameters that are nested blocks,
207   /// if this is "(", "[" or "<".
208   unsigned BlockParameterCount = 0;
209 
210   /// If this is a bracket ("<", "(", "[" or "{"), contains the kind of
211   /// the surrounding bracket.
212   tok::TokenKind ParentBracket = tok::unknown;
213 
214   /// A token can have a special role that can carry extra information
215   /// about the token's formatting.
216   std::unique_ptr<TokenRole> Role;
217 
218   /// If this is an opening parenthesis, how are the parameters packed?
219   ParameterPackingKind PackingKind = PPK_Inconclusive;
220 
221   /// The total length of the unwrapped line up to and including this
222   /// token.
223   unsigned TotalLength = 0;
224 
225   /// The original 0-based column of this token, including expanded tabs.
226   /// The configured TabWidth is used as tab width.
227   unsigned OriginalColumn = 0;
228 
229   /// The length of following tokens until the next natural split point,
230   /// or the next token that can be broken.
231   unsigned UnbreakableTailLength = 0;
232 
233   // FIXME: Come up with a 'cleaner' concept.
234   /// The binding strength of a token. This is a combined value of
235   /// operator precedence, parenthesis nesting, etc.
236   unsigned BindingStrength = 0;
237 
238   /// The nesting level of this token, i.e. the number of surrounding (),
239   /// [], {} or <>.
240   unsigned NestingLevel = 0;
241 
242   /// The indent level of this token. Copied from the surrounding line.
243   unsigned IndentLevel = 0;
244 
245   /// Penalty for inserting a line break before this token.
246   unsigned SplitPenalty = 0;
247 
248   /// If this is the first ObjC selector name in an ObjC method
249   /// definition or call, this contains the length of the longest name.
250   ///
251   /// This being set to 0 means that the selectors should not be colon-aligned,
252   /// e.g. because several of them are block-type.
253   unsigned LongestObjCSelectorName = 0;
254 
255   /// If this is the first ObjC selector name in an ObjC method
256   /// definition or call, this contains the number of parts that the whole
257   /// selector consist of.
258   unsigned ObjCSelectorNameParts = 0;
259 
260   /// The 0-based index of the parameter/argument. For ObjC it is set
261   /// for the selector name token.
262   /// For now calculated only for ObjC.
263   unsigned ParameterIndex = 0;
264 
265   /// Stores the number of required fake parentheses and the
266   /// corresponding operator precedence.
267   ///
268   /// If multiple fake parentheses start at a token, this vector stores them in
269   /// reverse order, i.e. inner fake parenthesis first.
270   SmallVector<prec::Level, 4> FakeLParens;
271   /// Insert this many fake ) after this token for correct indentation.
272   unsigned FakeRParens = 0;
273 
274   /// \c true if this token starts a binary expression, i.e. has at least
275   /// one fake l_paren with a precedence greater than prec::Unknown.
276   bool StartsBinaryExpression = false;
277   /// \c true if this token ends a binary expression.
278   bool EndsBinaryExpression = false;
279 
280   /// If this is an operator (or "."/"->") in a sequence of operators
281   /// with the same precedence, contains the 0-based operator index.
282   unsigned OperatorIndex = 0;
283 
284   /// If this is an operator (or "."/"->") in a sequence of operators
285   /// with the same precedence, points to the next operator.
286   FormatToken *NextOperator = nullptr;
287 
288   /// Is this token part of a \c DeclStmt defining multiple variables?
289   ///
290   /// Only set if \c Type == \c TT_StartOfName.
291   bool PartOfMultiVariableDeclStmt = false;
292 
293   /// Does this line comment continue a line comment section?
294   ///
295   /// Only set to true if \c Type == \c TT_LineComment.
296   bool ContinuesLineCommentSection = false;
297 
298   /// If this is a bracket, this points to the matching one.
299   FormatToken *MatchingParen = nullptr;
300 
301   /// The previous token in the unwrapped line.
302   FormatToken *Previous = nullptr;
303 
304   /// The next token in the unwrapped line.
305   FormatToken *Next = nullptr;
306 
307   /// If this token starts a block, this contains all the unwrapped lines
308   /// in it.
309   SmallVector<AnnotatedLine *, 1> Children;
310 
311   /// Stores the formatting decision for the token once it was made.
312   FormatDecision Decision = FD_Unformatted;
313 
314   /// If \c true, this token has been fully formatted (indented and
315   /// potentially re-formatted inside), and we do not allow further formatting
316   /// changes.
317   bool Finalized = false;
318 
319   bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
320   bool is(TokenType TT) const { return Type == TT; }
321   bool is(const IdentifierInfo *II) const {
322     return II && II == Tok.getIdentifierInfo();
323   }
324   bool is(tok::PPKeywordKind Kind) const {
325     return Tok.getIdentifierInfo() &&
326            Tok.getIdentifierInfo()->getPPKeywordID() == Kind;
327   }
328   template <typename A, typename B> bool isOneOf(A K1, B K2) const {
329     return is(K1) || is(K2);
330   }
331   template <typename A, typename B, typename... Ts>
332   bool isOneOf(A K1, B K2, Ts... Ks) const {
333     return is(K1) || isOneOf(K2, Ks...);
334   }
335   template <typename T> bool isNot(T Kind) const { return !is(Kind); }
336 
337   bool isIf(bool AllowConstexprMacro = true) const {
338     return is(tok::kw_if) || endsSequence(tok::kw_constexpr, tok::kw_if) ||
339            (endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro);
340   }
341 
342   bool closesScopeAfterBlock() const {
343     if (BlockKind == BK_Block)
344       return true;
345     if (closesScope())
346       return Previous->closesScopeAfterBlock();
347     return false;
348   }
349 
350   /// \c true if this token starts a sequence with the given tokens in order,
351   /// following the ``Next`` pointers, ignoring comments.
352   template <typename A, typename... Ts>
353   bool startsSequence(A K1, Ts... Tokens) const {
354     return startsSequenceInternal(K1, Tokens...);
355   }
356 
357   /// \c true if this token ends a sequence with the given tokens in order,
358   /// following the ``Previous`` pointers, ignoring comments.
359   /// For example, given tokens [T1, T2, T3], the function returns true if
360   /// 3 tokens ending at this (ignoring comments) are [T3, T2, T1]. In other
361   /// words, the tokens passed to this function need to the reverse of the
362   /// order the tokens appear in code.
363   template <typename A, typename... Ts>
364   bool endsSequence(A K1, Ts... Tokens) const {
365     return endsSequenceInternal(K1, Tokens...);
366   }
367 
368   bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
369 
370   bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
371     return Tok.isObjCAtKeyword(Kind);
372   }
373 
374   bool isAccessSpecifier(bool ColonRequired = true) const {
375     return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
376            (!ColonRequired || (Next && Next->is(tok::colon)));
377   }
378 
379   /// Determine whether the token is a simple-type-specifier.
380   bool isSimpleTypeSpecifier() const;
381 
382   bool isObjCAccessSpecifier() const {
383     return is(tok::at) && Next &&
384            (Next->isObjCAtKeyword(tok::objc_public) ||
385             Next->isObjCAtKeyword(tok::objc_protected) ||
386             Next->isObjCAtKeyword(tok::objc_package) ||
387             Next->isObjCAtKeyword(tok::objc_private));
388   }
389 
390   /// Returns whether \p Tok is ([{ or an opening < of a template or in
391   /// protos.
392   bool opensScope() const {
393     if (is(TT_TemplateString) && TokenText.endswith("${"))
394       return true;
395     if (is(TT_DictLiteral) && is(tok::less))
396       return true;
397     return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
398                    TT_TemplateOpener);
399   }
400   /// Returns whether \p Tok is )]} or a closing > of a template or in
401   /// protos.
402   bool closesScope() const {
403     if (is(TT_TemplateString) && TokenText.startswith("}"))
404       return true;
405     if (is(TT_DictLiteral) && is(tok::greater))
406       return true;
407     return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
408                    TT_TemplateCloser);
409   }
410 
411   /// Returns \c true if this is a "." or "->" accessing a member.
412   bool isMemberAccess() const {
413     return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
414            !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
415                     TT_LambdaArrow, TT_LeadingJavaAnnotation);
416   }
417 
418   bool isUnaryOperator() const {
419     switch (Tok.getKind()) {
420     case tok::plus:
421     case tok::plusplus:
422     case tok::minus:
423     case tok::minusminus:
424     case tok::exclaim:
425     case tok::tilde:
426     case tok::kw_sizeof:
427     case tok::kw_alignof:
428       return true;
429     default:
430       return false;
431     }
432   }
433 
434   bool isBinaryOperator() const {
435     // Comma is a binary operator, but does not behave as such wrt. formatting.
436     return getPrecedence() > prec::Comma;
437   }
438 
439   bool isTrailingComment() const {
440     return is(tok::comment) &&
441            (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
442   }
443 
444   /// Returns \c true if this is a keyword that can be used
445   /// like a function call (e.g. sizeof, typeid, ...).
446   bool isFunctionLikeKeyword() const {
447     switch (Tok.getKind()) {
448     case tok::kw_throw:
449     case tok::kw_typeid:
450     case tok::kw_return:
451     case tok::kw_sizeof:
452     case tok::kw_alignof:
453     case tok::kw_alignas:
454     case tok::kw_decltype:
455     case tok::kw_noexcept:
456     case tok::kw_static_assert:
457     case tok::kw___attribute:
458       return true;
459     default:
460       return false;
461     }
462   }
463 
464   /// Returns \c true if this is a string literal that's like a label,
465   /// e.g. ends with "=" or ":".
466   bool isLabelString() const {
467     if (!is(tok::string_literal))
468       return false;
469     StringRef Content = TokenText;
470     if (Content.startswith("\"") || Content.startswith("'"))
471       Content = Content.drop_front(1);
472     if (Content.endswith("\"") || Content.endswith("'"))
473       Content = Content.drop_back(1);
474     Content = Content.trim();
475     return Content.size() > 1 &&
476            (Content.back() == ':' || Content.back() == '=');
477   }
478 
479   /// Returns actual token start location without leading escaped
480   /// newlines and whitespace.
481   ///
482   /// This can be different to Tok.getLocation(), which includes leading escaped
483   /// newlines.
484   SourceLocation getStartOfNonWhitespace() const {
485     return WhitespaceRange.getEnd();
486   }
487 
488   prec::Level getPrecedence() const {
489     return getBinOpPrecedence(Tok.getKind(), /*GreaterThanIsOperator=*/true,
490                               /*CPlusPlus11=*/true);
491   }
492 
493   /// Returns the previous token ignoring comments.
494   FormatToken *getPreviousNonComment() const {
495     FormatToken *Tok = Previous;
496     while (Tok && Tok->is(tok::comment))
497       Tok = Tok->Previous;
498     return Tok;
499   }
500 
501   /// Returns the next token ignoring comments.
502   const FormatToken *getNextNonComment() const {
503     const FormatToken *Tok = Next;
504     while (Tok && Tok->is(tok::comment))
505       Tok = Tok->Next;
506     return Tok;
507   }
508 
509   /// Returns \c true if this tokens starts a block-type list, i.e. a
510   /// list that should be indented with a block indent.
511   bool opensBlockOrBlockTypeList(const FormatStyle &Style) const {
512     // C# Does not indent object initialisers as continuations.
513     if (is(tok::l_brace) && BlockKind == BK_BracedInit && Style.isCSharp())
514       return true;
515     if (is(TT_TemplateString) && opensScope())
516       return true;
517     return is(TT_ArrayInitializerLSquare) || is(TT_ProtoExtensionLSquare) ||
518            (is(tok::l_brace) &&
519             (BlockKind == BK_Block || is(TT_DictLiteral) ||
520              (!Style.Cpp11BracedListStyle && NestingLevel == 0))) ||
521            (is(tok::less) && (Style.Language == FormatStyle::LK_Proto ||
522                               Style.Language == FormatStyle::LK_TextProto));
523   }
524 
525   /// Returns whether the token is the left square bracket of a C++
526   /// structured binding declaration.
527   bool isCppStructuredBinding(const FormatStyle &Style) const {
528     if (!Style.isCpp() || isNot(tok::l_square))
529       return false;
530     const FormatToken *T = this;
531     do {
532       T = T->getPreviousNonComment();
533     } while (T && T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
534                              tok::ampamp));
535     return T && T->is(tok::kw_auto);
536   }
537 
538   /// Same as opensBlockOrBlockTypeList, but for the closing token.
539   bool closesBlockOrBlockTypeList(const FormatStyle &Style) const {
540     if (is(TT_TemplateString) && closesScope())
541       return true;
542     return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style);
543   }
544 
545   /// Return the actual namespace token, if this token starts a namespace
546   /// block.
547   const FormatToken *getNamespaceToken() const {
548     const FormatToken *NamespaceTok = this;
549     if (is(tok::comment))
550       NamespaceTok = NamespaceTok->getNextNonComment();
551     // Detect "(inline|export)? namespace" in the beginning of a line.
552     if (NamespaceTok && NamespaceTok->isOneOf(tok::kw_inline, tok::kw_export))
553       NamespaceTok = NamespaceTok->getNextNonComment();
554     return NamespaceTok &&
555                    NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro)
556                ? NamespaceTok
557                : nullptr;
558   }
559 
560 private:
561   // Disallow copying.
562   FormatToken(const FormatToken &) = delete;
563   void operator=(const FormatToken &) = delete;
564 
565   template <typename A, typename... Ts>
566   bool startsSequenceInternal(A K1, Ts... Tokens) const {
567     if (is(tok::comment) && Next)
568       return Next->startsSequenceInternal(K1, Tokens...);
569     return is(K1) && Next && Next->startsSequenceInternal(Tokens...);
570   }
571 
572   template <typename A> bool startsSequenceInternal(A K1) const {
573     if (is(tok::comment) && Next)
574       return Next->startsSequenceInternal(K1);
575     return is(K1);
576   }
577 
578   template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const {
579     if (is(tok::comment) && Previous)
580       return Previous->endsSequenceInternal(K1);
581     return is(K1);
582   }
583 
584   template <typename A, typename... Ts>
585   bool endsSequenceInternal(A K1, Ts... Tokens) const {
586     if (is(tok::comment) && Previous)
587       return Previous->endsSequenceInternal(K1, Tokens...);
588     return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...);
589   }
590 };
591 
592 class ContinuationIndenter;
593 struct LineState;
594 
595 class TokenRole {
596 public:
597   TokenRole(const FormatStyle &Style) : Style(Style) {}
598   virtual ~TokenRole();
599 
600   /// After the \c TokenAnnotator has finished annotating all the tokens,
601   /// this function precomputes required information for formatting.
602   virtual void precomputeFormattingInfos(const FormatToken *Token);
603 
604   /// Apply the special formatting that the given role demands.
605   ///
606   /// Assumes that the token having this role is already formatted.
607   ///
608   /// Continues formatting from \p State leaving indentation to \p Indenter and
609   /// returns the total penalty that this formatting incurs.
610   virtual unsigned formatFromToken(LineState &State,
611                                    ContinuationIndenter *Indenter,
612                                    bool DryRun) {
613     return 0;
614   }
615 
616   /// Same as \c formatFromToken, but assumes that the first token has
617   /// already been set thereby deciding on the first line break.
618   virtual unsigned formatAfterToken(LineState &State,
619                                     ContinuationIndenter *Indenter,
620                                     bool DryRun) {
621     return 0;
622   }
623 
624   /// Notifies the \c Role that a comma was found.
625   virtual void CommaFound(const FormatToken *Token) {}
626 
627   virtual const FormatToken *lastComma() { return nullptr; }
628 
629 protected:
630   const FormatStyle &Style;
631 };
632 
633 class CommaSeparatedList : public TokenRole {
634 public:
635   CommaSeparatedList(const FormatStyle &Style)
636       : TokenRole(Style), HasNestedBracedList(false) {}
637 
638   void precomputeFormattingInfos(const FormatToken *Token) override;
639 
640   unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
641                             bool DryRun) override;
642 
643   unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
644                            bool DryRun) override;
645 
646   /// Adds \p Token as the next comma to the \c CommaSeparated list.
647   void CommaFound(const FormatToken *Token) override {
648     Commas.push_back(Token);
649   }
650 
651   const FormatToken *lastComma() override {
652     if (Commas.empty())
653       return nullptr;
654     return Commas.back();
655   }
656 
657 private:
658   /// A struct that holds information on how to format a given list with
659   /// a specific number of columns.
660   struct ColumnFormat {
661     /// The number of columns to use.
662     unsigned Columns;
663 
664     /// The total width in characters.
665     unsigned TotalWidth;
666 
667     /// The number of lines required for this format.
668     unsigned LineCount;
669 
670     /// The size of each column in characters.
671     SmallVector<unsigned, 8> ColumnSizes;
672   };
673 
674   /// Calculate which \c ColumnFormat fits best into
675   /// \p RemainingCharacters.
676   const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
677 
678   /// The ordered \c FormatTokens making up the commas of this list.
679   SmallVector<const FormatToken *, 8> Commas;
680 
681   /// The length of each of the list's items in characters including the
682   /// trailing comma.
683   SmallVector<unsigned, 8> ItemLengths;
684 
685   /// Precomputed formats that can be used for this list.
686   SmallVector<ColumnFormat, 4> Formats;
687 
688   bool HasNestedBracedList;
689 };
690 
691 /// Encapsulates keywords that are context sensitive or for languages not
692 /// properly supported by Clang's lexer.
693 struct AdditionalKeywords {
694   AdditionalKeywords(IdentifierTable &IdentTable) {
695     kw_final = &IdentTable.get("final");
696     kw_override = &IdentTable.get("override");
697     kw_in = &IdentTable.get("in");
698     kw_of = &IdentTable.get("of");
699     kw_CF_CLOSED_ENUM = &IdentTable.get("CF_CLOSED_ENUM");
700     kw_CF_ENUM = &IdentTable.get("CF_ENUM");
701     kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
702     kw_NS_CLOSED_ENUM = &IdentTable.get("NS_CLOSED_ENUM");
703     kw_NS_ENUM = &IdentTable.get("NS_ENUM");
704     kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
705 
706     kw_as = &IdentTable.get("as");
707     kw_async = &IdentTable.get("async");
708     kw_await = &IdentTable.get("await");
709     kw_declare = &IdentTable.get("declare");
710     kw_finally = &IdentTable.get("finally");
711     kw_from = &IdentTable.get("from");
712     kw_function = &IdentTable.get("function");
713     kw_get = &IdentTable.get("get");
714     kw_import = &IdentTable.get("import");
715     kw_infer = &IdentTable.get("infer");
716     kw_is = &IdentTable.get("is");
717     kw_let = &IdentTable.get("let");
718     kw_module = &IdentTable.get("module");
719     kw_readonly = &IdentTable.get("readonly");
720     kw_set = &IdentTable.get("set");
721     kw_type = &IdentTable.get("type");
722     kw_typeof = &IdentTable.get("typeof");
723     kw_var = &IdentTable.get("var");
724     kw_yield = &IdentTable.get("yield");
725 
726     kw_abstract = &IdentTable.get("abstract");
727     kw_assert = &IdentTable.get("assert");
728     kw_extends = &IdentTable.get("extends");
729     kw_implements = &IdentTable.get("implements");
730     kw_instanceof = &IdentTable.get("instanceof");
731     kw_interface = &IdentTable.get("interface");
732     kw_native = &IdentTable.get("native");
733     kw_package = &IdentTable.get("package");
734     kw_synchronized = &IdentTable.get("synchronized");
735     kw_throws = &IdentTable.get("throws");
736     kw___except = &IdentTable.get("__except");
737     kw___has_include = &IdentTable.get("__has_include");
738     kw___has_include_next = &IdentTable.get("__has_include_next");
739 
740     kw_mark = &IdentTable.get("mark");
741 
742     kw_extend = &IdentTable.get("extend");
743     kw_option = &IdentTable.get("option");
744     kw_optional = &IdentTable.get("optional");
745     kw_repeated = &IdentTable.get("repeated");
746     kw_required = &IdentTable.get("required");
747     kw_returns = &IdentTable.get("returns");
748 
749     kw_signals = &IdentTable.get("signals");
750     kw_qsignals = &IdentTable.get("Q_SIGNALS");
751     kw_slots = &IdentTable.get("slots");
752     kw_qslots = &IdentTable.get("Q_SLOTS");
753 
754     // C# keywords
755     kw_dollar = &IdentTable.get("dollar");
756     kw_base = &IdentTable.get("base");
757     kw_byte = &IdentTable.get("byte");
758     kw_checked = &IdentTable.get("checked");
759     kw_decimal = &IdentTable.get("decimal");
760     kw_delegate = &IdentTable.get("delegate");
761     kw_event = &IdentTable.get("event");
762     kw_fixed = &IdentTable.get("fixed");
763     kw_foreach = &IdentTable.get("foreach");
764     kw_implicit = &IdentTable.get("implicit");
765     kw_internal = &IdentTable.get("internal");
766     kw_lock = &IdentTable.get("lock");
767     kw_null = &IdentTable.get("null");
768     kw_object = &IdentTable.get("object");
769     kw_out = &IdentTable.get("out");
770     kw_params = &IdentTable.get("params");
771     kw_ref = &IdentTable.get("ref");
772     kw_string = &IdentTable.get("string");
773     kw_stackalloc = &IdentTable.get("stackalloc");
774     kw_sbyte = &IdentTable.get("sbyte");
775     kw_sealed = &IdentTable.get("sealed");
776     kw_uint = &IdentTable.get("uint");
777     kw_ulong = &IdentTable.get("ulong");
778     kw_unchecked = &IdentTable.get("unchecked");
779     kw_unsafe = &IdentTable.get("unsafe");
780     kw_ushort = &IdentTable.get("ushort");
781     kw_when = &IdentTable.get("when");
782 
783     // Keep this at the end of the constructor to make sure everything here
784     // is
785     // already initialized.
786     JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
787         {kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
788          kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_readonly,
789          kw_set, kw_type, kw_typeof, kw_var, kw_yield,
790          // Keywords from the Java section.
791          kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
792 
793     CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>(
794         {kw_base, kw_byte, kw_checked, kw_decimal, kw_delegate, kw_event,
795          kw_fixed, kw_foreach, kw_implicit, kw_in, kw_interface, kw_internal,
796          kw_is, kw_lock, kw_null, kw_object, kw_out, kw_override, kw_params,
797          kw_readonly, kw_ref, kw_string, kw_stackalloc, kw_sbyte, kw_sealed,
798          kw_uint, kw_ulong, kw_unchecked, kw_unsafe, kw_ushort, kw_when,
799          // Keywords from the JavaScript section.
800          kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
801          kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_readonly,
802          kw_set, kw_type, kw_typeof, kw_var, kw_yield,
803          // Keywords from the Java section.
804          kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
805   }
806 
807   // Context sensitive keywords.
808   IdentifierInfo *kw_final;
809   IdentifierInfo *kw_override;
810   IdentifierInfo *kw_in;
811   IdentifierInfo *kw_of;
812   IdentifierInfo *kw_CF_CLOSED_ENUM;
813   IdentifierInfo *kw_CF_ENUM;
814   IdentifierInfo *kw_CF_OPTIONS;
815   IdentifierInfo *kw_NS_CLOSED_ENUM;
816   IdentifierInfo *kw_NS_ENUM;
817   IdentifierInfo *kw_NS_OPTIONS;
818   IdentifierInfo *kw___except;
819   IdentifierInfo *kw___has_include;
820   IdentifierInfo *kw___has_include_next;
821 
822   // JavaScript keywords.
823   IdentifierInfo *kw_as;
824   IdentifierInfo *kw_async;
825   IdentifierInfo *kw_await;
826   IdentifierInfo *kw_declare;
827   IdentifierInfo *kw_finally;
828   IdentifierInfo *kw_from;
829   IdentifierInfo *kw_function;
830   IdentifierInfo *kw_get;
831   IdentifierInfo *kw_import;
832   IdentifierInfo *kw_infer;
833   IdentifierInfo *kw_is;
834   IdentifierInfo *kw_let;
835   IdentifierInfo *kw_module;
836   IdentifierInfo *kw_readonly;
837   IdentifierInfo *kw_set;
838   IdentifierInfo *kw_type;
839   IdentifierInfo *kw_typeof;
840   IdentifierInfo *kw_var;
841   IdentifierInfo *kw_yield;
842 
843   // Java keywords.
844   IdentifierInfo *kw_abstract;
845   IdentifierInfo *kw_assert;
846   IdentifierInfo *kw_extends;
847   IdentifierInfo *kw_implements;
848   IdentifierInfo *kw_instanceof;
849   IdentifierInfo *kw_interface;
850   IdentifierInfo *kw_native;
851   IdentifierInfo *kw_package;
852   IdentifierInfo *kw_synchronized;
853   IdentifierInfo *kw_throws;
854 
855   // Pragma keywords.
856   IdentifierInfo *kw_mark;
857 
858   // Proto keywords.
859   IdentifierInfo *kw_extend;
860   IdentifierInfo *kw_option;
861   IdentifierInfo *kw_optional;
862   IdentifierInfo *kw_repeated;
863   IdentifierInfo *kw_required;
864   IdentifierInfo *kw_returns;
865 
866   // QT keywords.
867   IdentifierInfo *kw_signals;
868   IdentifierInfo *kw_qsignals;
869   IdentifierInfo *kw_slots;
870   IdentifierInfo *kw_qslots;
871 
872   // C# keywords
873   IdentifierInfo *kw_dollar;
874   IdentifierInfo *kw_base;
875   IdentifierInfo *kw_byte;
876   IdentifierInfo *kw_checked;
877   IdentifierInfo *kw_decimal;
878   IdentifierInfo *kw_delegate;
879   IdentifierInfo *kw_event;
880   IdentifierInfo *kw_fixed;
881   IdentifierInfo *kw_foreach;
882   IdentifierInfo *kw_implicit;
883   IdentifierInfo *kw_internal;
884 
885   IdentifierInfo *kw_lock;
886   IdentifierInfo *kw_null;
887   IdentifierInfo *kw_object;
888   IdentifierInfo *kw_out;
889 
890   IdentifierInfo *kw_params;
891 
892   IdentifierInfo *kw_ref;
893   IdentifierInfo *kw_string;
894   IdentifierInfo *kw_stackalloc;
895   IdentifierInfo *kw_sbyte;
896   IdentifierInfo *kw_sealed;
897   IdentifierInfo *kw_uint;
898   IdentifierInfo *kw_ulong;
899   IdentifierInfo *kw_unchecked;
900   IdentifierInfo *kw_unsafe;
901   IdentifierInfo *kw_ushort;
902   IdentifierInfo *kw_when;
903 
904   /// Returns \c true if \p Tok is a true JavaScript identifier, returns
905   /// \c false if it is a keyword or a pseudo keyword.
906   bool IsJavaScriptIdentifier(const FormatToken &Tok) const {
907     return Tok.is(tok::identifier) &&
908            JsExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
909                JsExtraKeywords.end();
910   }
911 
912   /// Returns \c true if \p Tok is a C# keyword, returns
913   /// \c false if it is a anything else.
914   bool isCSharpKeyword(const FormatToken &Tok) const {
915     switch (Tok.Tok.getKind()) {
916     case tok::kw_bool:
917     case tok::kw_break:
918     case tok::kw_case:
919     case tok::kw_catch:
920     case tok::kw_char:
921     case tok::kw_class:
922     case tok::kw_const:
923     case tok::kw_continue:
924     case tok::kw_default:
925     case tok::kw_do:
926     case tok::kw_double:
927     case tok::kw_else:
928     case tok::kw_enum:
929     case tok::kw_explicit:
930     case tok::kw_extern:
931     case tok::kw_false:
932     case tok::kw_float:
933     case tok::kw_for:
934     case tok::kw_goto:
935     case tok::kw_if:
936     case tok::kw_int:
937     case tok::kw_long:
938     case tok::kw_namespace:
939     case tok::kw_new:
940     case tok::kw_operator:
941     case tok::kw_private:
942     case tok::kw_protected:
943     case tok::kw_public:
944     case tok::kw_return:
945     case tok::kw_short:
946     case tok::kw_sizeof:
947     case tok::kw_static:
948     case tok::kw_struct:
949     case tok::kw_switch:
950     case tok::kw_this:
951     case tok::kw_throw:
952     case tok::kw_true:
953     case tok::kw_try:
954     case tok::kw_typeof:
955     case tok::kw_using:
956     case tok::kw_virtual:
957     case tok::kw_void:
958     case tok::kw_volatile:
959     case tok::kw_while:
960       return true;
961     default:
962       return Tok.is(tok::identifier) &&
963              CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
964                  CSharpExtraKeywords.end();
965     }
966   }
967 
968 private:
969   /// The JavaScript keywords beyond the C++ keyword set.
970   std::unordered_set<IdentifierInfo *> JsExtraKeywords;
971 
972   /// The C# keywords beyond the C++ keyword set
973   std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
974 };
975 
976 } // namespace format
977 } // namespace clang
978 
979 #endif
980