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