clang  5.0.0
FormatToken.h
Go to the documentation of this file.
1 //===--- FormatToken.h - Format C++ code ------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file contains the declaration of the FormatToken, a wrapper
12 /// around Token with additional information related to formatting.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
17 #define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
18 
21 #include "clang/Format/Format.h"
22 #include "clang/Lex/Lexer.h"
23 #include <memory>
24 #include <unordered_set>
25 
26 namespace clang {
27 namespace format {
28 
29 #define LIST_TOKEN_TYPES \
30  TYPE(ArrayInitializerLSquare) \
31  TYPE(ArraySubscriptLSquare) \
32  TYPE(AttributeParen) \
33  TYPE(BinaryOperator) \
34  TYPE(BitFieldColon) \
35  TYPE(BlockComment) \
36  TYPE(CastRParen) \
37  TYPE(ConditionalExpr) \
38  TYPE(ConflictAlternative) \
39  TYPE(ConflictEnd) \
40  TYPE(ConflictStart) \
41  TYPE(CtorInitializerColon) \
42  TYPE(CtorInitializerComma) \
43  TYPE(DesignatedInitializerLSquare) \
44  TYPE(DesignatedInitializerPeriod) \
45  TYPE(DictLiteral) \
46  TYPE(ForEachMacro) \
47  TYPE(FunctionAnnotationRParen) \
48  TYPE(FunctionDeclarationName) \
49  TYPE(FunctionLBrace) \
50  TYPE(FunctionTypeLParen) \
51  TYPE(ImplicitStringLiteral) \
52  TYPE(InheritanceColon) \
53  TYPE(InheritanceComma) \
54  TYPE(InlineASMBrace) \
55  TYPE(InlineASMColon) \
56  TYPE(JavaAnnotation) \
57  TYPE(JsComputedPropertyName) \
58  TYPE(JsExponentiation) \
59  TYPE(JsExponentiationEqual) \
60  TYPE(JsFatArrow) \
61  TYPE(JsNonNullAssertion) \
62  TYPE(JsTypeColon) \
63  TYPE(JsTypeOperator) \
64  TYPE(JsTypeOptionalQuestion) \
65  TYPE(LambdaArrow) \
66  TYPE(LambdaLSquare) \
67  TYPE(LeadingJavaAnnotation) \
68  TYPE(LineComment) \
69  TYPE(MacroBlockBegin) \
70  TYPE(MacroBlockEnd) \
71  TYPE(ObjCBlockLBrace) \
72  TYPE(ObjCBlockLParen) \
73  TYPE(ObjCDecl) \
74  TYPE(ObjCForIn) \
75  TYPE(ObjCMethodExpr) \
76  TYPE(ObjCMethodSpecifier) \
77  TYPE(ObjCProperty) \
78  TYPE(ObjCStringLiteral) \
79  TYPE(OverloadedOperator) \
80  TYPE(OverloadedOperatorLParen) \
81  TYPE(PointerOrReference) \
82  TYPE(PureVirtualSpecifier) \
83  TYPE(RangeBasedForLoopColon) \
84  TYPE(RegexLiteral) \
85  TYPE(SelectorName) \
86  TYPE(StartOfName) \
87  TYPE(TemplateCloser) \
88  TYPE(TemplateOpener) \
89  TYPE(TemplateString) \
90  TYPE(TrailingAnnotation) \
91  TYPE(TrailingReturnArrow) \
92  TYPE(TrailingUnaryOperator) \
93  TYPE(UnaryOperator) \
94  TYPE(Unknown)
95 
96 enum TokenType {
97 #define TYPE(X) TT_##X,
99 #undef TYPE
101 };
102 
103 /// \brief Determines the name of a token type.
104 const char *getTokenTypeName(TokenType Type);
105 
106 // Represents what type of block a set of braces open.
108 
109 // The packing kind of a function's parameters.
111 
113 
114 class TokenRole;
115 class AnnotatedLine;
116 
117 /// \brief A wrapper around a \c Token storing information about the
118 /// whitespace characters preceding it.
119 struct FormatToken {
121 
122  /// \brief The \c Token.
124 
125  /// \brief The number of newlines immediately before the \c Token.
126  ///
127  /// This can be used to determine what the user wrote in the original code
128  /// and thereby e.g. leave an empty line between two function definitions.
129  unsigned NewlinesBefore = 0;
130 
131  /// \brief Whether there is at least one unescaped newline before the \c
132  /// Token.
133  bool HasUnescapedNewline = false;
134 
135  /// \brief The range of the whitespace immediately preceding the \c Token.
137 
138  /// \brief The offset just past the last '\n' in this token's leading
139  /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
140  unsigned LastNewlineOffset = 0;
141 
142  /// \brief The width of the non-whitespace parts of the token (or its first
143  /// line for multi-line tokens) in columns.
144  /// We need this to correctly measure number of columns a token spans.
145  unsigned ColumnWidth = 0;
146 
147  /// \brief Contains the width in columns of the last line of a multi-line
148  /// token.
149  unsigned LastLineColumnWidth = 0;
150 
151  /// \brief Whether the token text contains newlines (escaped or not).
152  bool IsMultiline = false;
153 
154  /// \brief Indicates that this is the first token of the file.
155  bool IsFirst = false;
156 
157  /// \brief Whether there must be a line break before this token.
158  ///
159  /// This happens for example when a preprocessor directive ended directly
160  /// before the token.
161  bool MustBreakBefore = false;
162 
163  /// \brief The raw text of the token.
164  ///
165  /// Contains the raw token text without leading whitespace and without leading
166  /// escaped newlines.
167  StringRef TokenText;
168 
169  /// \brief Set to \c true if this token is an unterminated literal.
171 
172  /// \brief Contains the kind of block if this token is a brace.
174 
175  TokenType Type = TT_Unknown;
176 
177  /// \brief The number of spaces that should be inserted before this token.
178  unsigned SpacesRequiredBefore = 0;
179 
180  /// \brief \c true if it is allowed to break before this token.
181  bool CanBreakBefore = false;
182 
183  /// \brief \c true if this is the ">" of "template<..>".
185 
186  /// \brief Number of parameters, if this is "(", "[" or "<".
187  ///
188  /// This is initialized to 1 as we don't need to distinguish functions with
189  /// 0 parameters from functions with 1 parameter. Thus, we can simply count
190  /// the number of commas.
191  unsigned ParameterCount = 0;
192 
193  /// \brief Number of parameters that are nested blocks,
194  /// if this is "(", "[" or "<".
195  unsigned BlockParameterCount = 0;
196 
197  /// \brief If this is a bracket ("<", "(", "[" or "{"), contains the kind of
198  /// the surrounding bracket.
200 
201  /// \brief A token can have a special role that can carry extra information
202  /// about the token's formatting.
203  std::unique_ptr<TokenRole> Role;
204 
205  /// \brief If this is an opening parenthesis, how are the parameters packed?
207 
208  /// \brief The total length of the unwrapped line up to and including this
209  /// token.
210  unsigned TotalLength = 0;
211 
212  /// \brief The original 0-based column of this token, including expanded tabs.
213  /// The configured TabWidth is used as tab width.
214  unsigned OriginalColumn = 0;
215 
216  /// \brief The length of following tokens until the next natural split point,
217  /// or the next token that can be broken.
218  unsigned UnbreakableTailLength = 0;
219 
220  // FIXME: Come up with a 'cleaner' concept.
221  /// \brief The binding strength of a token. This is a combined value of
222  /// operator precedence, parenthesis nesting, etc.
223  unsigned BindingStrength = 0;
224 
225  /// \brief The nesting level of this token, i.e. the number of surrounding (),
226  /// [], {} or <>.
227  unsigned NestingLevel = 0;
228 
229  /// \brief The indent level of this token. Copied from the surrounding line.
230  unsigned IndentLevel = 0;
231 
232  /// \brief Penalty for inserting a line break before this token.
233  unsigned SplitPenalty = 0;
234 
235  /// \brief If this is the first ObjC selector name in an ObjC method
236  /// definition or call, this contains the length of the longest name.
237  ///
238  /// This being set to 0 means that the selectors should not be colon-aligned,
239  /// e.g. because several of them are block-type.
241 
242  /// \brief Stores the number of required fake parentheses and the
243  /// corresponding operator precedence.
244  ///
245  /// If multiple fake parentheses start at a token, this vector stores them in
246  /// reverse order, i.e. inner fake parenthesis first.
248  /// \brief Insert this many fake ) after this token for correct indentation.
249  unsigned FakeRParens = 0;
250 
251  /// \brief \c true if this token starts a binary expression, i.e. has at least
252  /// one fake l_paren with a precedence greater than prec::Unknown.
254  /// \brief \c true if this token ends a binary expression.
255  bool EndsBinaryExpression = false;
256 
257  /// \brief Is this is an operator (or "."/"->") in a sequence of operators
258  /// with the same precedence, contains the 0-based operator index.
259  unsigned OperatorIndex = 0;
260 
261  /// \brief If this is an operator (or "."/"->") in a sequence of operators
262  /// with the same precedence, points to the next operator.
264 
265  /// \brief Is this token part of a \c DeclStmt defining multiple variables?
266  ///
267  /// Only set if \c Type == \c TT_StartOfName.
269 
270  /// \brief Does this line comment continue a line comment section?
271  ///
272  /// Only set to true if \c Type == \c TT_LineComment.
274 
275  /// \brief If this is a bracket, this points to the matching one.
277 
278  /// \brief The previous token in the unwrapped line.
279  FormatToken *Previous = nullptr;
280 
281  /// \brief The next token in the unwrapped line.
282  FormatToken *Next = nullptr;
283 
284  /// \brief If this token starts a block, this contains all the unwrapped lines
285  /// in it.
287 
288  /// \brief Stores the formatting decision for the token once it was made.
290 
291  /// \brief If \c true, this token has been fully formatted (indented and
292  /// potentially re-formatted inside), and we do not allow further formatting
293  /// changes.
294  bool Finalized = false;
295 
296  bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
297  bool is(TokenType TT) const { return Type == TT; }
298  bool is(const IdentifierInfo *II) const {
299  return II && II == Tok.getIdentifierInfo();
300  }
301  bool is(tok::PPKeywordKind Kind) const {
302  return Tok.getIdentifierInfo() &&
304  }
305  template <typename A, typename B> bool isOneOf(A K1, B K2) const {
306  return is(K1) || is(K2);
307  }
308  template <typename A, typename B, typename... Ts>
309  bool isOneOf(A K1, B K2, Ts... Ks) const {
310  return is(K1) || isOneOf(K2, Ks...);
311  }
312  template <typename T> bool isNot(T Kind) const { return !is(Kind); }
313 
314  /// \c true if this token starts a sequence with the given tokens in order,
315  /// following the ``Next`` pointers, ignoring comments.
316  template <typename A, typename... Ts>
317  bool startsSequence(A K1, Ts... Tokens) const {
318  return startsSequenceInternal(K1, Tokens...);
319  }
320 
321  /// \c true if this token ends a sequence with the given tokens in order,
322  /// following the ``Previous`` pointers, ignoring comments.
323  template <typename A, typename... Ts>
324  bool endsSequence(A K1, Ts... Tokens) const {
325  return endsSequenceInternal(K1, Tokens...);
326  }
327 
328  bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
329 
331  return Tok.isObjCAtKeyword(Kind);
332  }
333 
334  bool isAccessSpecifier(bool ColonRequired = true) const {
335  return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
336  (!ColonRequired || (Next && Next->is(tok::colon)));
337  }
338 
339  /// \brief Determine whether the token is a simple-type-specifier.
340  bool isSimpleTypeSpecifier() const;
341 
342  bool isObjCAccessSpecifier() const {
343  return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
344  Next->isObjCAtKeyword(tok::objc_protected) ||
345  Next->isObjCAtKeyword(tok::objc_package) ||
346  Next->isObjCAtKeyword(tok::objc_private));
347  }
348 
349  /// \brief Returns whether \p Tok is ([{ or a template opening <.
350  bool opensScope() const {
351  if (is(TT_TemplateString) && TokenText.endswith("${"))
352  return true;
353  return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
354  TT_TemplateOpener);
355  }
356  /// \brief Returns whether \p Tok is )]} or a template closing >.
357  bool closesScope() const {
358  if (is(TT_TemplateString) && TokenText.startswith("}"))
359  return true;
360  return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
361  TT_TemplateCloser);
362  }
363 
364  /// \brief Returns \c true if this is a "." or "->" accessing a member.
365  bool isMemberAccess() const {
366  return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
367  !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
368  TT_LambdaArrow);
369  }
370 
371  bool isUnaryOperator() const {
372  switch (Tok.getKind()) {
373  case tok::plus:
374  case tok::plusplus:
375  case tok::minus:
376  case tok::minusminus:
377  case tok::exclaim:
378  case tok::tilde:
379  case tok::kw_sizeof:
380  case tok::kw_alignof:
381  return true;
382  default:
383  return false;
384  }
385  }
386 
387  bool isBinaryOperator() const {
388  // Comma is a binary operator, but does not behave as such wrt. formatting.
389  return getPrecedence() > prec::Comma;
390  }
391 
392  bool isTrailingComment() const {
393  return is(tok::comment) &&
394  (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
395  }
396 
397  /// \brief Returns \c true if this is a keyword that can be used
398  /// like a function call (e.g. sizeof, typeid, ...).
399  bool isFunctionLikeKeyword() const {
400  switch (Tok.getKind()) {
401  case tok::kw_throw:
402  case tok::kw_typeid:
403  case tok::kw_return:
404  case tok::kw_sizeof:
405  case tok::kw_alignof:
406  case tok::kw_alignas:
407  case tok::kw_decltype:
408  case tok::kw_noexcept:
409  case tok::kw_static_assert:
410  case tok::kw___attribute:
411  return true;
412  default:
413  return false;
414  }
415  }
416 
417  /// \brief Returns \c true if this is a string literal that's like a label,
418  /// e.g. ends with "=" or ":".
419  bool isLabelString() const {
420  if (!is(tok::string_literal))
421  return false;
422  StringRef Content = TokenText;
423  if (Content.startswith("\"") || Content.startswith("'"))
424  Content = Content.drop_front(1);
425  if (Content.endswith("\"") || Content.endswith("'"))
426  Content = Content.drop_back(1);
427  Content = Content.trim();
428  return Content.size() > 1 &&
429  (Content.back() == ':' || Content.back() == '=');
430  }
431 
432  /// \brief Returns actual token start location without leading escaped
433  /// newlines and whitespace.
434  ///
435  /// This can be different to Tok.getLocation(), which includes leading escaped
436  /// newlines.
438  return WhitespaceRange.getEnd();
439  }
440 
442  return getBinOpPrecedence(Tok.getKind(), true, true);
443  }
444 
445  /// \brief Returns the previous token ignoring comments.
448  while (Tok && Tok->is(tok::comment))
449  Tok = Tok->Previous;
450  return Tok;
451  }
452 
453  /// \brief Returns the next token ignoring comments.
455  const FormatToken *Tok = Next;
456  while (Tok && Tok->is(tok::comment))
457  Tok = Tok->Next;
458  return Tok;
459  }
460 
461  /// \brief Returns \c true if this tokens starts a block-type list, i.e. a
462  /// list that should be indented with a block indent.
464  if (is(TT_TemplateString) && opensScope())
465  return true;
466  return is(TT_ArrayInitializerLSquare) ||
467  (is(tok::l_brace) &&
468  (BlockKind == BK_Block || is(TT_DictLiteral) ||
469  (!Style.Cpp11BracedListStyle && NestingLevel == 0))) ||
470  (is(tok::less) && (Style.Language == FormatStyle::LK_Proto ||
472  }
473 
474  /// \brief Same as opensBlockOrBlockTypeList, but for the closing token.
476  if (is(TT_TemplateString) && closesScope())
477  return true;
479  }
480 
481  /// \brief Return the actual namespace token, if this token starts a namespace
482  /// block.
484  const FormatToken *NamespaceTok = this;
485  if (is(tok::comment))
486  NamespaceTok = NamespaceTok->getNextNonComment();
487  // Detect "(inline)? namespace" in the beginning of a line.
488  if (NamespaceTok && NamespaceTok->is(tok::kw_inline))
489  NamespaceTok = NamespaceTok->getNextNonComment();
490  return NamespaceTok && NamespaceTok->is(tok::kw_namespace) ? NamespaceTok
491  : nullptr;
492  }
493 
494 private:
495  // Disallow copying.
496  FormatToken(const FormatToken &) = delete;
497  void operator=(const FormatToken &) = delete;
498 
499  template <typename A, typename... Ts>
500  bool startsSequenceInternal(A K1, Ts... Tokens) const {
501  if (is(tok::comment) && Next)
502  return Next->startsSequenceInternal(K1, Tokens...);
503  return is(K1) && Next && Next->startsSequenceInternal(Tokens...);
504  }
505 
506  template <typename A>
507  bool startsSequenceInternal(A K1) const {
508  if (is(tok::comment) && Next)
509  return Next->startsSequenceInternal(K1);
510  return is(K1);
511  }
512 
513  template <typename A, typename... Ts>
514  bool endsSequenceInternal(A K1) const {
515  if (is(tok::comment) && Previous)
516  return Previous->endsSequenceInternal(K1);
517  return is(K1);
518  }
519 
520  template <typename A, typename... Ts>
521  bool endsSequenceInternal(A K1, Ts... Tokens) const {
522  if (is(tok::comment) && Previous)
523  return Previous->endsSequenceInternal(K1, Tokens...);
524  return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...);
525  }
526 };
527 
528 class ContinuationIndenter;
529 struct LineState;
530 
531 class TokenRole {
532 public:
533  TokenRole(const FormatStyle &Style) : Style(Style) {}
534  virtual ~TokenRole();
535 
536  /// \brief After the \c TokenAnnotator has finished annotating all the tokens,
537  /// this function precomputes required information for formatting.
538  virtual void precomputeFormattingInfos(const FormatToken *Token);
539 
540  /// \brief Apply the special formatting that the given role demands.
541  ///
542  /// Assumes that the token having this role is already formatted.
543  ///
544  /// Continues formatting from \p State leaving indentation to \p Indenter and
545  /// returns the total penalty that this formatting incurs.
546  virtual unsigned formatFromToken(LineState &State,
548  bool DryRun) {
549  return 0;
550  }
551 
552  /// \brief Same as \c formatFromToken, but assumes that the first token has
553  /// already been set thereby deciding on the first line break.
554  virtual unsigned formatAfterToken(LineState &State,
556  bool DryRun) {
557  return 0;
558  }
559 
560  /// \brief Notifies the \c Role that a comma was found.
561  virtual void CommaFound(const FormatToken *Token) {}
562 
563 protected:
565 };
566 
568 public:
570  : TokenRole(Style), HasNestedBracedList(false) {}
571 
572  void precomputeFormattingInfos(const FormatToken *Token) override;
573 
575  bool DryRun) override;
576 
578  bool DryRun) override;
579 
580  /// \brief Adds \p Token as the next comma to the \c CommaSeparated list.
581  void CommaFound(const FormatToken *Token) override {
582  Commas.push_back(Token);
583  }
584 
585 private:
586  /// \brief A struct that holds information on how to format a given list with
587  /// a specific number of columns.
588  struct ColumnFormat {
589  /// \brief The number of columns to use.
590  unsigned Columns;
591 
592  /// \brief The total width in characters.
593  unsigned TotalWidth;
594 
595  /// \brief The number of lines required for this format.
596  unsigned LineCount;
597 
598  /// \brief The size of each column in characters.
599  SmallVector<unsigned, 8> ColumnSizes;
600  };
601 
602  /// \brief Calculate which \c ColumnFormat fits best into
603  /// \p RemainingCharacters.
604  const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
605 
606  /// \brief The ordered \c FormatTokens making up the commas of this list.
608 
609  /// \brief The length of each of the list's items in characters including the
610  /// trailing comma.
611  SmallVector<unsigned, 8> ItemLengths;
612 
613  /// \brief Precomputed formats that can be used for this list.
615 
616  bool HasNestedBracedList;
617 };
618 
619 /// \brief Encapsulates keywords that are context sensitive or for languages not
620 /// properly supported by Clang's lexer.
623  kw_final = &IdentTable.get("final");
624  kw_override = &IdentTable.get("override");
625  kw_in = &IdentTable.get("in");
626  kw_of = &IdentTable.get("of");
627  kw_CF_ENUM = &IdentTable.get("CF_ENUM");
628  kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
629  kw_NS_ENUM = &IdentTable.get("NS_ENUM");
630  kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
631 
632  kw_as = &IdentTable.get("as");
633  kw_async = &IdentTable.get("async");
634  kw_await = &IdentTable.get("await");
635  kw_declare = &IdentTable.get("declare");
636  kw_finally = &IdentTable.get("finally");
637  kw_from = &IdentTable.get("from");
638  kw_function = &IdentTable.get("function");
639  kw_get = &IdentTable.get("get");
640  kw_import = &IdentTable.get("import");
641  kw_is = &IdentTable.get("is");
642  kw_let = &IdentTable.get("let");
643  kw_module = &IdentTable.get("module");
644  kw_readonly = &IdentTable.get("readonly");
645  kw_set = &IdentTable.get("set");
646  kw_type = &IdentTable.get("type");
647  kw_var = &IdentTable.get("var");
648  kw_yield = &IdentTable.get("yield");
649 
650  kw_abstract = &IdentTable.get("abstract");
651  kw_assert = &IdentTable.get("assert");
652  kw_extends = &IdentTable.get("extends");
653  kw_implements = &IdentTable.get("implements");
654  kw_instanceof = &IdentTable.get("instanceof");
655  kw_interface = &IdentTable.get("interface");
656  kw_native = &IdentTable.get("native");
657  kw_package = &IdentTable.get("package");
658  kw_synchronized = &IdentTable.get("synchronized");
659  kw_throws = &IdentTable.get("throws");
660  kw___except = &IdentTable.get("__except");
661  kw___has_include = &IdentTable.get("__has_include");
662  kw___has_include_next = &IdentTable.get("__has_include_next");
663 
664  kw_mark = &IdentTable.get("mark");
665 
666  kw_extend = &IdentTable.get("extend");
667  kw_option = &IdentTable.get("option");
668  kw_optional = &IdentTable.get("optional");
669  kw_repeated = &IdentTable.get("repeated");
670  kw_required = &IdentTable.get("required");
671  kw_returns = &IdentTable.get("returns");
672 
673  kw_signals = &IdentTable.get("signals");
674  kw_qsignals = &IdentTable.get("Q_SIGNALS");
675  kw_slots = &IdentTable.get("slots");
676  kw_qslots = &IdentTable.get("Q_SLOTS");
677 
678  // Keep this at the end of the constructor to make sure everything here is
679  // already initialized.
680  JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
684  // Keywords from the Java section.
686  }
687 
688  // Context sensitive keywords.
700 
701  // JavaScript keywords.
719 
720  // Java keywords.
731 
732  // Pragma keywords.
734 
735  // Proto keywords.
742 
743  // QT keywords.
748 
749  /// \brief Returns \c true if \p Tok is a true JavaScript identifier, returns
750  /// \c false if it is a keyword or a pseudo keyword.
751  bool IsJavaScriptIdentifier(const FormatToken &Tok) const {
752  return Tok.is(tok::identifier) &&
753  JsExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
754  JsExtraKeywords.end();
755  }
756 
757 private:
758  /// \brief The JavaScript keywords beyond the C++ keyword set.
759  std::unordered_set<IdentifierInfo *> JsExtraKeywords;
760 };
761 
762 } // namespace format
763 } // namespace clang
764 
765 #endif
SourceLocation getEnd() const
unsigned NestingLevel
The nesting level of this token, i.e.
Definition: FormatToken.h:227
bool isAccessSpecifier(bool ColonRequired=true) const
Definition: FormatToken.h:334
Token Tok
The Token.
Definition: FormatToken.h:123
Should be used for Protocol Buffers (https://developers.google.com/protocol-buffers/).
Definition: Format.h:1121
CommaSeparatedList(const FormatStyle &Style)
Definition: FormatToken.h:569
std::unique_ptr< TokenRole > Role
A token can have a special role that can carry extra information about the token's formatting...
Definition: FormatToken.h:203
unsigned OriginalColumn
The original 0-based column of this token, including expanded tabs.
Definition: FormatToken.h:214
bool isOneOf(A K1, B K2) const
Definition: FormatToken.h:305
FormatToken * getPreviousNonComment() const
Returns the previous token ignoring comments.
Definition: FormatToken.h:446
The base class of the type hierarchy.
Definition: Type.h:1303
IdentTable(getFormattingLangOpts(Style))
bool is(TokenType TT) const
Definition: FormatToken.h:297
bool IsMultiline
Whether the token text contains newlines (escaped or not).
Definition: FormatToken.h:152
bool IsFirst
Indicates that this is the first token of the file.
Definition: FormatToken.h:155
bool isStringLiteral(TokenKind K)
Return true if this is a C or C++ string-literal (or C++11 user-defined-string-literal) token...
Definition: TokenKinds.h:79
bool isNot(T Kind) const
Definition: FormatToken.h:312
bool EndsBinaryExpression
true if this token ends a binary expression.
Definition: FormatToken.h:255
unsigned TotalLength
The total length of the unwrapped line up to and including this token.
Definition: FormatToken.h:210
unsigned NewlinesBefore
The number of newlines immediately before the Token.
Definition: FormatToken.h:129
FormatToken * Next
The next token in the unwrapped line.
Definition: FormatToken.h:282
unsigned UnbreakableTailLength
The length of following tokens until the next natural split point, or the next token that can be brok...
Definition: FormatToken.h:218
unsigned SplitPenalty
Penalty for inserting a line break before this token.
Definition: FormatToken.h:233
One of these records is kept for each identifier that is lexed.
unsigned ParameterCount
Number of parameters, if this is "(", "[" or "<".
Definition: FormatToken.h:191
unsigned FakeRParens
Insert this many fake ) after this token for correct indentation.
Definition: FormatToken.h:249
LineState State
bool CanBreakBefore
true if it is allowed to break before this token.
Definition: FormatToken.h:181
FormatToken * Previous
The previous token in the unwrapped line.
Definition: FormatToken.h:279
AdditionalKeywords(IdentifierTable &IdentTable)
Definition: FormatToken.h:622
const FormatToken * getNextNonComment() const
Returns the next token ignoring comments.
Definition: FormatToken.h:454
bool StartsBinaryExpression
true if this token starts a binary expression, i.e.
Definition: FormatToken.h:253
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
unsigned LongestObjCSelectorName
If this is the first ObjC selector name in an ObjC method definition or call, this contains the lengt...
Definition: FormatToken.h:240
unsigned OperatorIndex
Is this is an operator (or "."/"->") in a sequence of operators with the same precedence, contains the 0-based operator index.
Definition: FormatToken.h:259
unsigned SpacesRequiredBefore
The number of spaces that should be inserted before this token.
Definition: FormatToken.h:178
bool isObjCAccessSpecifier() const
Definition: FormatToken.h:342
bool closesScope() const
Returns whether Tok is )]} or a template closing >.
Definition: FormatToken.h:357
unsigned BlockParameterCount
Number of parameters that are nested blocks, if this is "(", "[" or "<".
Definition: FormatToken.h:195
bool closesBlockOrBlockTypeList(const FormatStyle &Style) const
Same as opensBlockOrBlockTypeList, but for the closing token.
Definition: FormatToken.h:475
unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter, bool DryRun) override
Same as formatFromToken, but assumes that the first token has already been set thereby deciding on th...
Definition: FormatToken.cpp:74
void CommaFound(const FormatToken *Token) override
Adds Token as the next comma to the CommaSeparated list.
Definition: FormatToken.h:581
bool isStringLiteral() const
Definition: FormatToken.h:328
tok::TokenKind getKind() const
Definition: Token.h:90
virtual void precomputeFormattingInfos(const FormatToken *Token)
After the TokenAnnotator has finished annotating all the tokens, this function precomputes required i...
Definition: FormatToken.cpp:72
virtual void CommaFound(const FormatToken *Token)
Notifies the Role that a comma was found.
Definition: FormatToken.h:561
The current state when indenting a unwrapped line.
bool endsSequence(A K1, Ts...Tokens) const
true if this token ends a sequence with the given tokens in order, following the Previous pointers...
Definition: FormatToken.h:324
ContinuationIndenter * Indenter
Implements an efficient mapping from strings to IdentifierInfo nodes.
ParameterPackingKind PackingKind
If this is an opening parenthesis, how are the parameters packed?
Definition: FormatToken.h:206
PPKeywordKind
Provides a namespace for preprocessor keywords which start with a '#' at the beginning of the line...
Definition: TokenKinds.h:33
IdentifierInfo * kw___has_include_next
Definition: FormatToken.h:699
A wrapper around a Token storing information about the whitespace characters preceding it...
Definition: FormatToken.h:119
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Defines and computes precedence levels for binary/ternary operators.
TokenRole(const FormatStyle &Style)
Definition: FormatToken.h:533
ObjCKeywordKind
Provides a namespace for Objective-C keywords which start with an '@'.
Definition: TokenKinds.h:41
bool is(tok::PPKeywordKind Kind) const
Definition: FormatToken.h:301
bool startsSequence(A K1, Ts...Tokens) const
true if this token starts a sequence with the given tokens in order, following the Next pointers...
Definition: FormatToken.h:317
unsigned LastNewlineOffset
The offset just past the last ' ' in this token's leading whitespace (relative to WhiteSpaceStart)...
Definition: FormatToken.h:140
const char * getTokenTypeName(TokenType Type)
Determines the name of a token type.
Definition: FormatToken.cpp:25
bool isOneOf(A K1, B K2, Ts...Ks) const
Definition: FormatToken.h:309
#define false
Definition: stdbool.h:33
Kind
bool isTrailingComment() const
Definition: FormatToken.h:392
Encodes a location in the source.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
Various functions to configurably format source code.
bool is(const IdentifierInfo *II) const
Definition: FormatToken.h:298
bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const
Return true if we have an ObjC keyword identifier.
Definition: Lexer.cpp:46
Encapsulates keywords that are context sensitive or for languages not properly supported by Clang's l...
Definition: FormatToken.h:621
SourceRange WhitespaceRange
The range of the whitespace immediately preceding the Token.
Definition: FormatToken.h:136
ArrayRef< FormatToken * > Tokens
bool opensBlockOrBlockTypeList(const FormatStyle &Style) const
Returns true if this tokens starts a block-type list, i.e.
Definition: FormatToken.h:463
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
tok::TokenKind ParentBracket
If this is a bracket ("<", "(", "[" or "{"), contains the kind of the surrounding bracket...
Definition: FormatToken.h:199
bool IsUnterminatedLiteral
Set to true if this token is an unterminated literal.
Definition: FormatToken.h:170
StringRef TokenText
The raw text of the token.
Definition: FormatToken.h:167
SmallVector< prec::Level, 4 > FakeLParens
Stores the number of required fake parentheses and the corresponding operator precedence.
Definition: FormatToken.h:247
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {...
Definition: Token.h:95
bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const
Definition: FormatToken.h:330
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:46
unsigned IndentLevel
The indent level of this token. Copied from the surrounding line.
Definition: FormatToken.h:230
LanguageKind Language
Language, this format style is targeted at.
Definition: Format.h:1131
unsigned ColumnWidth
The width of the non-whitespace parts of the token (or its first line for multi-line tokens) in colum...
Definition: FormatToken.h:145
bool isLabelString() const
Returns true if this is a string literal that's like a label, e.g.
Definition: FormatToken.h:419
void precomputeFormattingInfos(const FormatToken *Token) override
After the TokenAnnotator has finished annotating all the tokens, this function precomputes required i...
bool Finalized
If true, this token has been fully formatted (indented and potentially re-formatted inside)...
Definition: FormatToken.h:294
bool Cpp11BracedListStyle
If true, format braced lists as best suited for C++11 braced lists.
Definition: Format.h:908
virtual unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter, bool DryRun)
Same as formatFromToken, but assumes that the first token has already been set thereby deciding on th...
Definition: FormatToken.h:554
unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter, bool DryRun) override
Apply the special formatting that the given role demands.
bool opensScope() const
Returns whether Tok is ([{ or a template opening <.
Definition: FormatToken.h:350
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:296
bool IsJavaScriptIdentifier(const FormatToken &Tok) const
Returns true if Tok is a true JavaScript identifier, returns false if it is a keyword or a pseudo key...
Definition: FormatToken.h:751
bool isUnaryOperator() const
Definition: FormatToken.h:371
FormatToken * NextOperator
If this is an operator (or "."/"->") in a sequence of operators with the same precedence, points to the next operator.
Definition: FormatToken.h:263
prec::Level getPrecedence() const
Definition: FormatToken.h:441
bool ClosesTemplateDeclaration
true if this is the ">" of "template<..>".
Definition: FormatToken.h:184
const FormatToken * getNamespaceToken() const
Return the actual namespace token, if this token starts a namespace block.
Definition: FormatToken.h:483
FormatToken * MatchingParen
If this is a bracket, this points to the matching one.
Definition: FormatToken.h:276
SmallVector< AnnotatedLine *, 1 > Children
If this token starts a block, this contains all the unwrapped lines in it.
Definition: FormatToken.h:286
bool isMemberAccess() const
Returns true if this is a "." or "->" accessing a member.
Definition: FormatToken.h:365
SourceLocation getStartOfNonWhitespace() const
Returns actual token start location without leading escaped newlines and whitespace.
Definition: FormatToken.h:437
const FormatStyle & Style
Definition: FormatToken.h:564
bool MustBreakBefore
Whether there must be a line break before this token.
Definition: FormatToken.h:161
virtual unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter, bool DryRun)
Apply the special formatting that the given role demands.
Definition: FormatToken.h:546
Should be used for Protocol Buffer messages in text format (https://developers.google.com/protocol-buffers/).
Definition: Format.h:1126
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.
A trivial tuple used to represent a source range.
unsigned BindingStrength
The binding strength of a token.
Definition: FormatToken.h:223
FormatDecision Decision
Stores the formatting decision for the token once it was made.
Definition: FormatToken.h:289
bool ContinuesLineCommentSection
Does this line comment continue a line comment section?
Definition: FormatToken.h:273
bool HasUnescapedNewline
Whether there is at least one unescaped newline before the Token.
Definition: FormatToken.h:133
BraceBlockKind BlockKind
Contains the kind of block if this token is a brace.
Definition: FormatToken.h:173
bool PartOfMultiVariableDeclStmt
Is this token part of a DeclStmt defining multiple variables?
Definition: FormatToken.h:268
bool isSimpleTypeSpecifier() const
Determine whether the token is a simple-type-specifier.
Definition: FormatToken.cpp:41
unsigned LastLineColumnWidth
Contains the width in columns of the last line of a multi-line token.
Definition: FormatToken.h:149
bool isBinaryOperator() const
Definition: FormatToken.h:387
#define LIST_TOKEN_TYPES
Definition: FormatToken.h:29
bool isFunctionLikeKeyword() const
Returns true if this is a keyword that can be used like a function call (e.g.
Definition: FormatToken.h:399
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:177
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.