clang 17.0.0git
FormatToken.h
Go to the documentation of this file.
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
20#include "clang/Format/Format.h"
21#include "clang/Lex/Lexer.h"
22#include <memory>
23#include <optional>
24#include <unordered_set>
25
26namespace clang {
27namespace format {
28
29#define LIST_TOKEN_TYPES \
30 TYPE(ArrayInitializerLSquare) \
31 TYPE(ArraySubscriptLSquare) \
32 TYPE(AttributeColon) \
33 TYPE(AttributeMacro) \
34 TYPE(AttributeParen) \
35 TYPE(AttributeSquare) \
36 TYPE(BinaryOperator) \
37 TYPE(BitFieldColon) \
38 TYPE(BlockComment) \
39 TYPE(BracedListLBrace) \
40 /* The colon at the end of a case label. */ \
41 TYPE(CaseLabelColon) \
42 TYPE(CastRParen) \
43 TYPE(ClassLBrace) \
44 TYPE(CompoundRequirementLBrace) \
45 /* ternary ?: expression */ \
46 TYPE(ConditionalExpr) \
47 /* the condition in an if statement */ \
48 TYPE(ConditionLParen) \
49 TYPE(ConflictAlternative) \
50 TYPE(ConflictEnd) \
51 TYPE(ConflictStart) \
52 /* l_brace of if/for/while */ \
53 TYPE(ControlStatementLBrace) \
54 TYPE(CppCastLParen) \
55 TYPE(CSharpGenericTypeConstraint) \
56 TYPE(CSharpGenericTypeConstraintColon) \
57 TYPE(CSharpGenericTypeConstraintComma) \
58 TYPE(CSharpNamedArgumentColon) \
59 TYPE(CSharpNullable) \
60 TYPE(CSharpNullConditionalLSquare) \
61 TYPE(CSharpStringLiteral) \
62 TYPE(CtorInitializerColon) \
63 TYPE(CtorInitializerComma) \
64 TYPE(DesignatedInitializerLSquare) \
65 TYPE(DesignatedInitializerPeriod) \
66 TYPE(DictLiteral) \
67 TYPE(ElseLBrace) \
68 TYPE(EnumLBrace) \
69 TYPE(FatArrow) \
70 TYPE(ForEachMacro) \
71 TYPE(FunctionAnnotationRParen) \
72 TYPE(FunctionDeclarationName) \
73 TYPE(FunctionLBrace) \
74 TYPE(FunctionLikeOrFreestandingMacro) \
75 TYPE(FunctionTypeLParen) \
76 /* The colons as part of a C11 _Generic selection */ \
77 TYPE(GenericSelectionColon) \
78 /* The colon at the end of a goto label. */ \
79 TYPE(GotoLabelColon) \
80 TYPE(IfMacro) \
81 TYPE(ImplicitStringLiteral) \
82 TYPE(InheritanceColon) \
83 TYPE(InheritanceComma) \
84 TYPE(InlineASMBrace) \
85 TYPE(InlineASMColon) \
86 TYPE(InlineASMSymbolicNameLSquare) \
87 TYPE(JavaAnnotation) \
88 TYPE(JsAndAndEqual) \
89 TYPE(JsComputedPropertyName) \
90 TYPE(JsExponentiation) \
91 TYPE(JsExponentiationEqual) \
92 TYPE(JsPipePipeEqual) \
93 TYPE(JsPrivateIdentifier) \
94 TYPE(JsTypeColon) \
95 TYPE(JsTypeOperator) \
96 TYPE(JsTypeOptionalQuestion) \
97 TYPE(LambdaArrow) \
98 TYPE(LambdaLBrace) \
99 TYPE(LambdaLSquare) \
100 TYPE(LeadingJavaAnnotation) \
101 TYPE(LineComment) \
102 TYPE(MacroBlockBegin) \
103 TYPE(MacroBlockEnd) \
104 TYPE(ModulePartitionColon) \
105 TYPE(NamespaceMacro) \
106 TYPE(NonNullAssertion) \
107 TYPE(NullCoalescingEqual) \
108 TYPE(NullCoalescingOperator) \
109 TYPE(NullPropagatingOperator) \
110 TYPE(ObjCBlockLBrace) \
111 TYPE(ObjCBlockLParen) \
112 TYPE(ObjCDecl) \
113 TYPE(ObjCForIn) \
114 TYPE(ObjCMethodExpr) \
115 TYPE(ObjCMethodSpecifier) \
116 TYPE(ObjCProperty) \
117 TYPE(ObjCStringLiteral) \
118 TYPE(OverloadedOperator) \
119 TYPE(OverloadedOperatorLParen) \
120 TYPE(PointerOrReference) \
121 TYPE(ProtoExtensionLSquare) \
122 TYPE(PureVirtualSpecifier) \
123 TYPE(RangeBasedForLoopColon) \
124 TYPE(RecordLBrace) \
125 TYPE(RegexLiteral) \
126 TYPE(RequiresClause) \
127 TYPE(RequiresClauseInARequiresExpression) \
128 TYPE(RequiresExpression) \
129 TYPE(RequiresExpressionLBrace) \
130 TYPE(RequiresExpressionLParen) \
131 TYPE(SelectorName) \
132 TYPE(StartOfName) \
133 TYPE(StatementAttributeLikeMacro) \
134 TYPE(StatementMacro) \
135 TYPE(StructLBrace) \
136 TYPE(StructuredBindingLSquare) \
137 TYPE(TemplateCloser) \
138 TYPE(TemplateOpener) \
139 TYPE(TemplateString) \
140 TYPE(TrailingAnnotation) \
141 TYPE(TrailingReturnArrow) \
142 TYPE(TrailingUnaryOperator) \
143 TYPE(TypeDeclarationParen) \
144 TYPE(TypenameMacro) \
145 TYPE(UnaryOperator) \
146 TYPE(UnionLBrace) \
147 TYPE(UntouchableMacroFunc) \
148 /* Like in 'assign x = 0, y = 1;' . */ \
149 TYPE(VerilogAssignComma) \
150 /* like in begin : block */ \
151 TYPE(VerilogBlockLabelColon) \
152 /* The square bracket for the dimension part of the type name. \
153 * In 'logic [1:0] x[1:0]', only the first '['. This way we can have space \
154 * before the first bracket but not the second. */ \
155 TYPE(VerilogDimensionedTypeName) \
156 /* list of port connections or parameters in a module instantiation */ \
157 TYPE(VerilogInstancePortComma) \
158 TYPE(VerilogInstancePortLParen) \
159 /* A parenthesized list within which line breaks are inserted by the \
160 * formatter, for example the list of ports in a module header. */ \
161 TYPE(VerilogMultiLineListLParen) \
162 /* for the base in a number literal, not including the quote */ \
163 TYPE(VerilogNumberBase) \
164 /* like `(strong1, pull0)` */ \
165 TYPE(VerilogStrength) \
166 /* Things inside the table in user-defined primitives. */ \
167 TYPE(VerilogTableItem) \
168 /* those that separate ports of different types */ \
169 TYPE(VerilogTypeComma) \
170 TYPE(Unknown)
172/// Determines the semantic type of a syntactic token, e.g. whether "<" is a
173/// template opener or binary operator.
174enum TokenType : uint8_t {
175#define TYPE(X) TT_##X,
177#undef TYPE
179};
180
181/// Determines the name of a token type.
183
184// Represents what type of block a set of braces open.
186
187// The packing kind of a function's parameters.
189
191
192/// Roles a token can take in a configured macro expansion.
194 /// The token was expanded from a macro argument when formatting the expanded
195 /// token sequence.
197 /// The token is part of a macro argument that was previously formatted as
198 /// expansion when formatting the unexpanded macro call.
200 /// The token was expanded from a macro definition, and is not visible as part
201 /// of the macro call.
202 MR_Hidden,
203};
204
205struct FormatToken;
206
207/// Contains information on the token's role in a macro expansion.
208///
209/// Given the following definitions:
210/// A(X) = [ X ]
211/// B(X) = < X >
212/// C(X) = X
213///
214/// Consider the macro call:
215/// A({B(C(C(x)))}) -> [{<x>}]
216///
217/// In this case, the tokens of the unexpanded macro call will have the
218/// following relevant entries in their macro context (note that formatting
219/// the unexpanded macro call happens *after* formatting the expanded macro
220/// call):
221/// A( { B( C( C(x) ) ) } )
222/// Role: NN U NN NN NNUN N N U N (N=None, U=UnexpandedArg)
223///
224/// [ { < x > } ]
225/// Role: H E H E H E H (H=Hidden, E=ExpandedArg)
226/// ExpandedFrom[0]: A A A A A A A
227/// ExpandedFrom[1]: B B B
228/// ExpandedFrom[2]: C
229/// ExpandedFrom[3]: C
230/// StartOfExpansion: 1 0 1 2 0 0 0
231/// EndOfExpansion: 0 0 0 2 1 0 1
232struct MacroExpansion {
234
235 /// The token's role in the macro expansion.
236 /// When formatting an expanded macro, all tokens that are part of macro
237 /// arguments will be MR_ExpandedArg, while all tokens that are not visible in
238 /// the macro call will be MR_Hidden.
239 /// When formatting an unexpanded macro call, all tokens that are part of
240 /// macro arguments will be MR_UnexpandedArg.
242
243 /// The stack of macro call identifier tokens this token was expanded from.
245
246 /// The number of expansions of which this macro is the first entry.
247 unsigned StartOfExpansion = 0;
249 /// The number of currently open expansions in \c ExpandedFrom this macro is
250 /// the last token in.
251 unsigned EndOfExpansion = 0;
252};
253
254class TokenRole;
255class AnnotatedLine;
257/// A wrapper around a \c Token storing information about the
258/// whitespace characters preceding it.
259struct FormatToken {
261 : HasUnescapedNewline(false), IsMultiline(false), IsFirst(false),
266 Finalized(false), ClosesRequiresClause(false),
267 EndsCppAttributeGroup(false), BlockKind(BK_Unknown),
268 Decision(FD_Unformatted), PackingKind(PPK_Inconclusive),
269 TypeIsFinalized(false), Type(TT_Unknown) {}
270
271 /// The \c Token.
272 Token Tok;
273
274 /// The raw text of the token.
275 ///
276 /// Contains the raw token text without leading whitespace and without leading
277 /// escaped newlines.
278 StringRef TokenText;
279
280 /// A token can have a special role that can carry extra information
281 /// about the token's formatting.
282 /// FIXME: Make FormatToken for parsing and AnnotatedToken two different
283 /// classes and make this a unique_ptr in the AnnotatedToken class.
284 std::shared_ptr<TokenRole> Role;
285
286 /// The range of the whitespace immediately preceding the \c Token.
289 /// Whether there is at least one unescaped newline before the \c
290 /// Token.
292
293 /// Whether the token text contains newlines (escaped or not).
294 unsigned IsMultiline : 1;
295
296 /// Indicates that this is the first token of the file.
297 unsigned IsFirst : 1;
298
299 /// Whether there must be a line break before this token.
300 ///
301 /// This happens for example when a preprocessor directive ended directly
302 /// before the token.
303 unsigned MustBreakBefore : 1;
304
305 /// Set to \c true if this token is an unterminated literal.
307
308 /// \c true if it is allowed to break before this token.
309 unsigned CanBreakBefore : 1;
310
311 /// \c true if this is the ">" of "template<..>".
312 unsigned ClosesTemplateDeclaration : 1;
314 /// \c true if this token starts a binary expression, i.e. has at least
315 /// one fake l_paren with a precedence greater than prec::Unknown.
316 unsigned StartsBinaryExpression : 1;
317 /// \c true if this token ends a binary expression.
318 unsigned EndsBinaryExpression : 1;
319
320 /// Is this token part of a \c DeclStmt defining multiple variables?
321 ///
322 /// Only set if \c Type == \c TT_StartOfName.
323 unsigned PartOfMultiVariableDeclStmt : 1;
324
325 /// Does this line comment continue a line comment section?
326 ///
327 /// Only set to true if \c Type == \c TT_LineComment.
328 unsigned ContinuesLineCommentSection : 1;
329
330 /// If \c true, this token has been fully formatted (indented and
331 /// potentially re-formatted inside), and we do not allow further formatting
332 /// changes.
333 unsigned Finalized : 1;
334
335 /// \c true if this is the last token within requires clause.
337
338 /// \c true if this token ends a group of C++ attributes.
339 unsigned EndsCppAttributeGroup : 1;
340
341private:
342 /// Contains the kind of block if this token is a brace.
343 unsigned BlockKind : 2;
344
345public:
347 return static_cast<BraceBlockKind>(BlockKind);
348 }
349 void setBlockKind(BraceBlockKind BBK) {
350 BlockKind = BBK;
351 assert(getBlockKind() == BBK && "BraceBlockKind overflow!");
352 }
353
354private:
355 /// Stores the formatting decision for the token once it was made.
356 unsigned Decision : 2;
357
358public:
360 return static_cast<FormatDecision>(Decision);
361 }
363 Decision = D;
364 assert(getDecision() == D && "FormatDecision overflow!");
365 }
366
367private:
368 /// If this is an opening parenthesis, how are the parameters packed?
369 unsigned PackingKind : 2;
370
371public:
373 return static_cast<ParameterPackingKind>(PackingKind);
374 }
376 PackingKind = K;
377 assert(getPackingKind() == K && "ParameterPackingKind overflow!");
378 }
379
380private:
381 unsigned TypeIsFinalized : 1;
383
384public:
385 /// Returns the token's type, e.g. whether "<" is a template opener or
386 /// binary operator.
387 TokenType getType() const { return Type; }
388 void setType(TokenType T) {
389 // If this token is a macro argument while formatting an unexpanded macro
390 // call, we do not change its type any more - the type was deduced from
391 // formatting the expanded macro stream already.
392 if (MacroCtx && MacroCtx->Role == MR_UnexpandedArg)
393 return;
394 assert((!TypeIsFinalized || T == Type) &&
395 "Please use overwriteFixedType to change a fixed type.");
396 Type = T;
397 }
398 /// Sets the type and also the finalized flag. This prevents the type to be
399 /// reset in TokenAnnotator::resetTokenMetadata(). If the type needs to be set
400 /// to another one please use overwriteFixedType, or even better remove the
401 /// need to reassign the type.
403 Type = T;
404 TypeIsFinalized = true;
405 }
407 TypeIsFinalized = false;
408 setType(T);
409 }
410 bool isTypeFinalized() const { return TypeIsFinalized; }
411
412 /// Used to set an operator precedence explicitly.
414
415 /// The number of newlines immediately before the \c Token.
416 ///
417 /// This can be used to determine what the user wrote in the original code
418 /// and thereby e.g. leave an empty line between two function definitions.
419 unsigned NewlinesBefore = 0;
421 /// The offset just past the last '\n' in this token's leading
422 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
423 unsigned LastNewlineOffset = 0;
424
425 /// The width of the non-whitespace parts of the token (or its first
426 /// line for multi-line tokens) in columns.
427 /// We need this to correctly measure number of columns a token spans.
428 unsigned ColumnWidth = 0;
430 /// Contains the width in columns of the last line of a multi-line
431 /// token.
433
434 /// The number of spaces that should be inserted before this token.
436
437 /// Number of parameters, if this is "(", "[" or "<".
438 unsigned ParameterCount = 0;
440 /// Number of parameters that are nested blocks,
441 /// if this is "(", "[" or "<".
442 unsigned BlockParameterCount = 0;
444 /// If this is a bracket ("<", "(", "[" or "{"), contains the kind of
445 /// the surrounding bracket.
446 tok::TokenKind ParentBracket = tok::unknown;
448 /// The total length of the unwrapped line up to and including this
449 /// token.
450 unsigned TotalLength = 0;
452 /// The original 0-based column of this token, including expanded tabs.
453 /// The configured TabWidth is used as tab width.
454 unsigned OriginalColumn = 0;
456 /// The length of following tokens until the next natural split point,
457 /// or the next token that can be broken.
458 unsigned UnbreakableTailLength = 0;
459
460 // FIXME: Come up with a 'cleaner' concept.
461 /// The binding strength of a token. This is a combined value of
462 /// operator precedence, parenthesis nesting, etc.
463 unsigned BindingStrength = 0;
465 /// The nesting level of this token, i.e. the number of surrounding (),
466 /// [], {} or <>.
467 unsigned NestingLevel = 0;
468
469 /// The indent level of this token. Copied from the surrounding line.
470 unsigned IndentLevel = 0;
471
472 /// Penalty for inserting a line break before this token.
473 unsigned SplitPenalty = 0;
474
475 /// If this is the first ObjC selector name in an ObjC method
476 /// definition or call, this contains the length of the longest name.
477 ///
478 /// This being set to 0 means that the selectors should not be colon-aligned,
479 /// e.g. because several of them are block-type.
480 unsigned LongestObjCSelectorName = 0;
481
482 /// If this is the first ObjC selector name in an ObjC method
483 /// definition or call, this contains the number of parts that the whole
484 /// selector consist of.
485 unsigned ObjCSelectorNameParts = 0;
486
487 /// The 0-based index of the parameter/argument. For ObjC it is set
488 /// for the selector name token.
489 /// For now calculated only for ObjC.
490 unsigned ParameterIndex = 0;
491
492 /// Stores the number of required fake parentheses and the
493 /// corresponding operator precedence.
494 ///
495 /// If multiple fake parentheses start at a token, this vector stores them in
496 /// reverse order, i.e. inner fake parenthesis first.
498 /// Insert this many fake ) after this token for correct indentation.
499 unsigned FakeRParens = 0;
501 /// If this is an operator (or "."/"->") in a sequence of operators
502 /// with the same precedence, contains the 0-based operator index.
503 unsigned OperatorIndex = 0;
505 /// If this is an operator (or "."/"->") in a sequence of operators
506 /// with the same precedence, points to the next operator.
508
509 /// If this is a bracket, this points to the matching one.
511
512 /// The previous token in the unwrapped line.
514
515 /// The next token in the unwrapped line.
516 FormatToken *Next = nullptr;
517
518 /// The first token in set of column elements.
519 bool StartsColumn = false;
520
521 /// This notes the start of the line of an array initializer.
523
524 /// This starts an array initializer.
525 bool IsArrayInitializer = false;
526
527 /// Is optional and can be removed.
528 bool Optional = false;
529
530 /// Number of optional braces to be inserted after this token:
531 /// -1: a single left brace
532 /// 0: no braces
533 /// >0: number of right braces
534 int8_t BraceCount = 0;
536 /// If this token starts a block, this contains all the unwrapped lines
537 /// in it.
540 // Contains all attributes related to how this token takes part
541 // in a configured macro expansion.
542 std::optional<MacroExpansion> MacroCtx;
543
544 /// When macro expansion introduces nodes with children, those are marked as
545 /// \c MacroParent.
546 /// FIXME: The formatting code currently hard-codes the assumption that
547 /// child nodes are introduced by blocks following an opening brace.
548 /// This is deeply baked into the code and disentangling this will require
549 /// signficant refactorings. \c MacroParent allows us to special-case the
550 /// cases in which we treat parents as block-openers for now.
551 bool MacroParent = false;
553 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
554 bool is(TokenType TT) const { return getType() == TT; }
555 bool is(const IdentifierInfo *II) const {
556 return II && II == Tok.getIdentifierInfo();
557 }
558 bool is(tok::PPKeywordKind Kind) const {
561 }
562 bool is(BraceBlockKind BBK) const { return getBlockKind() == BBK; }
563 bool is(ParameterPackingKind PPK) const { return getPackingKind() == PPK; }
564
565 template <typename A, typename B> bool isOneOf(A K1, B K2) const {
566 return is(K1) || is(K2);
567 }
568 template <typename A, typename B, typename... Ts>
569 bool isOneOf(A K1, B K2, Ts... Ks) const {
570 return is(K1) || isOneOf(K2, Ks...);
572 template <typename T> bool isNot(T Kind) const { return !is(Kind); }
573
574 bool isIf(bool AllowConstexprMacro = true) const {
575 return is(tok::kw_if) || endsSequence(tok::kw_constexpr, tok::kw_if) ||
576 (endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro);
577 }
578
579 bool closesScopeAfterBlock() const {
580 if (getBlockKind() == BK_Block)
581 return true;
582 if (closesScope())
584 return false;
585 }
586
587 /// \c true if this token starts a sequence with the given tokens in order,
588 /// following the ``Next`` pointers, ignoring comments.
589 template <typename A, typename... Ts>
590 bool startsSequence(A K1, Ts... Tokens) const {
591 return startsSequenceInternal(K1, Tokens...);
592 }
593
594 /// \c true if this token ends a sequence with the given tokens in order,
595 /// following the ``Previous`` pointers, ignoring comments.
596 /// For example, given tokens [T1, T2, T3], the function returns true if
597 /// 3 tokens ending at this (ignoring comments) are [T3, T2, T1]. In other
598 /// words, the tokens passed to this function need to the reverse of the
599 /// order the tokens appear in code.
600 template <typename A, typename... Ts>
601 bool endsSequence(A K1, Ts... Tokens) const {
602 return endsSequenceInternal(K1, Tokens...);
603 }
605 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
606
607 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
608 return Tok.isObjCAtKeyword(Kind);
609 }
610
611 bool isAccessSpecifier(bool ColonRequired = true) const {
612 if (!isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
613 return false;
614 if (!ColonRequired)
615 return true;
616 const auto NextNonComment = getNextNonComment();
617 return NextNonComment && NextNonComment->is(tok::colon);
618 }
619
621 return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile,
622 tok::kw___attribute, tok::kw__Nonnull, tok::kw__Nullable,
623 tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64,
624 tok::kw___funcref, TT_AttributeMacro);
625 }
626
627 /// Determine whether the token is a simple-type-specifier.
628 [[nodiscard]] bool isSimpleTypeSpecifier() const;
630 [[nodiscard]] bool isTypeOrIdentifier() const;
631
632 bool isObjCAccessSpecifier() const {
633 return is(tok::at) && Next &&
634 (Next->isObjCAtKeyword(tok::objc_public) ||
635 Next->isObjCAtKeyword(tok::objc_protected) ||
636 Next->isObjCAtKeyword(tok::objc_package) ||
637 Next->isObjCAtKeyword(tok::objc_private));
638 }
640 /// Returns whether \p Tok is ([{ or an opening < of a template or in
641 /// protos.
642 bool opensScope() const {
643 if (is(TT_TemplateString) && TokenText.endswith("${"))
644 return true;
645 if (is(TT_DictLiteral) && is(tok::less))
646 return true;
647 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
648 TT_TemplateOpener);
650 /// Returns whether \p Tok is )]} or a closing > of a template or in
651 /// protos.
652 bool closesScope() const {
653 if (is(TT_TemplateString) && TokenText.startswith("}"))
654 return true;
655 if (is(TT_DictLiteral) && is(tok::greater))
656 return true;
657 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
658 TT_TemplateCloser);
660
661 /// Returns \c true if this is a "." or "->" accessing a member.
662 bool isMemberAccess() const {
663 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
664 !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
665 TT_LambdaArrow, TT_LeadingJavaAnnotation);
666 }
667
668 bool isUnaryOperator() const {
669 switch (Tok.getKind()) {
670 case tok::plus:
671 case tok::plusplus:
672 case tok::minus:
673 case tok::minusminus:
674 case tok::exclaim:
675 case tok::tilde:
676 case tok::kw_sizeof:
677 case tok::kw_alignof:
678 return true;
679 default:
680 return false;
682 }
683
684 bool isBinaryOperator() const {
685 // Comma is a binary operator, but does not behave as such wrt. formatting.
687 }
688
689 bool isTrailingComment() const {
690 return is(tok::comment) &&
691 (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
692 }
694 /// Returns \c true if this is a keyword that can be used
695 /// like a function call (e.g. sizeof, typeid, ...).
696 bool isFunctionLikeKeyword() const {
697 switch (Tok.getKind()) {
698 case tok::kw_throw:
699 case tok::kw_typeid:
700 case tok::kw_return:
701 case tok::kw_sizeof:
702 case tok::kw_alignof:
703 case tok::kw_alignas:
704 case tok::kw_decltype:
705 case tok::kw_noexcept:
706 case tok::kw_static_assert:
707 case tok::kw__Atomic:
708 case tok::kw___attribute:
709#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
710#include "clang/Basic/TransformTypeTraits.def"
711 case tok::kw_requires:
712 return true;
713 default:
714 return false;
715 }
716 }
718 /// Returns \c true if this is a string literal that's like a label,
719 /// e.g. ends with "=" or ":".
720 bool isLabelString() const {
721 if (!is(tok::string_literal))
722 return false;
723 StringRef Content = TokenText;
724 if (Content.startswith("\"") || Content.startswith("'"))
725 Content = Content.drop_front(1);
726 if (Content.endswith("\"") || Content.endswith("'"))
727 Content = Content.drop_back(1);
728 Content = Content.trim();
729 return Content.size() > 1 &&
730 (Content.back() == ':' || Content.back() == '=');
731 }
732
733 /// Returns actual token start location without leading escaped
734 /// newlines and whitespace.
735 ///
736 /// This can be different to Tok.getLocation(), which includes leading escaped
737 /// newlines.
739 return WhitespaceRange.getEnd();
740 }
742 /// Returns \c true if the range of whitespace immediately preceding the \c
743 /// Token is not empty.
744 bool hasWhitespaceBefore() const {
746 }
747
748 prec::Level getPrecedence() const {
750 return ForcedPrecedence;
751 return getBinOpPrecedence(Tok.getKind(), /*GreaterThanIsOperator=*/true,
752 /*CPlusPlus11=*/true);
754
755 /// Returns the previous token ignoring comments.
756 [[nodiscard]] FormatToken *getPreviousNonComment() const {
758 while (Tok && Tok->is(tok::comment))
759 Tok = Tok->Previous;
760 return Tok;
762
763 /// Returns the next token ignoring comments.
764 [[nodiscard]] FormatToken *getNextNonComment() const {
766 while (Tok && Tok->is(tok::comment))
767 Tok = Tok->Next;
768 return Tok;
769 }
770
771 /// Returns \c true if this tokens starts a block-type list, i.e. a
772 /// list that should be indented with a block indent.
773 [[nodiscard]] bool opensBlockOrBlockTypeList(const FormatStyle &Style) const;
775 /// Returns whether the token is the left square bracket of a C++
776 /// structured binding declaration.
777 bool isCppStructuredBinding(const FormatStyle &Style) const {
778 if (!Style.isCpp() || isNot(tok::l_square))
779 return false;
780 const FormatToken *T = this;
781 do {
782 T = T->getPreviousNonComment();
783 } while (T && T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
784 tok::ampamp));
785 return T && T->is(tok::kw_auto);
787
788 /// Same as opensBlockOrBlockTypeList, but for the closing token.
789 bool closesBlockOrBlockTypeList(const FormatStyle &Style) const {
790 if (is(TT_TemplateString) && closesScope())
791 return true;
793 }
795 /// Return the actual namespace token, if this token starts a namespace
796 /// block.
797 const FormatToken *getNamespaceToken() const {
798 const FormatToken *NamespaceTok = this;
799 if (is(tok::comment))
800 NamespaceTok = NamespaceTok->getNextNonComment();
801 // Detect "(inline|export)? namespace" in the beginning of a line.
802 if (NamespaceTok && NamespaceTok->isOneOf(tok::kw_inline, tok::kw_export))
803 NamespaceTok = NamespaceTok->getNextNonComment();
804 return NamespaceTok &&
805 NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro)
806 ? NamespaceTok
807 : nullptr;
808 }
809
810 void copyFrom(const FormatToken &Tok) { *this = Tok; }
811
812private:
813 // Only allow copying via the explicit copyFrom method.
814 FormatToken(const FormatToken &) = delete;
815 FormatToken &operator=(const FormatToken &) = default;
816
817 template <typename A, typename... Ts>
818 bool startsSequenceInternal(A K1, Ts... Tokens) const {
819 if (is(tok::comment) && Next)
820 return Next->startsSequenceInternal(K1, Tokens...);
821 return is(K1) && Next && Next->startsSequenceInternal(Tokens...);
822 }
823
824 template <typename A> bool startsSequenceInternal(A K1) const {
825 if (is(tok::comment) && Next)
826 return Next->startsSequenceInternal(K1);
827 return is(K1);
828 }
829
830 template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const {
831 if (is(tok::comment) && Previous)
832 return Previous->endsSequenceInternal(K1);
833 return is(K1);
834 }
835
836 template <typename A, typename... Ts>
837 bool endsSequenceInternal(A K1, Ts... Tokens) const {
838 if (is(tok::comment) && Previous)
839 return Previous->endsSequenceInternal(K1, Tokens...);
840 return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...);
841 }
842};
843
845struct LineState;
847class TokenRole {
848public:
850 virtual ~TokenRole();
851
852 /// After the \c TokenAnnotator has finished annotating all the tokens,
853 /// this function precomputes required information for formatting.
854 virtual void precomputeFormattingInfos(const FormatToken *Token);
855
856 /// Apply the special formatting that the given role demands.
857 ///
858 /// Assumes that the token having this role is already formatted.
859 ///
860 /// Continues formatting from \p State leaving indentation to \p Indenter and
861 /// returns the total penalty that this formatting incurs.
862 virtual unsigned formatFromToken(LineState &State,
864 bool DryRun) {
865 return 0;
866 }
868 /// Same as \c formatFromToken, but assumes that the first token has
869 /// already been set thereby deciding on the first line break.
870 virtual unsigned formatAfterToken(LineState &State,
872 bool DryRun) {
873 return 0;
875
876 /// Notifies the \c Role that a comma was found.
877 virtual void CommaFound(const FormatToken *Token) {}
878
879 virtual const FormatToken *lastComma() { return nullptr; }
880
881protected:
883};
885class CommaSeparatedList : public TokenRole {
886public:
888 : TokenRole(Style), HasNestedBracedList(false) {}
889
890 void precomputeFormattingInfos(const FormatToken *Token) override;
891
893 bool DryRun) override;
894
896 bool DryRun) override;
897
898 /// Adds \p Token as the next comma to the \c CommaSeparated list.
899 void CommaFound(const FormatToken *Token) override {
900 Commas.push_back(Token);
901 }
902
903 const FormatToken *lastComma() override {
904 if (Commas.empty())
905 return nullptr;
906 return Commas.back();
907 }
908
909private:
910 /// A struct that holds information on how to format a given list with
911 /// a specific number of columns.
912 struct ColumnFormat {
913 /// The number of columns to use.
914 unsigned Columns;
915
916 /// The total width in characters.
917 unsigned TotalWidth;
918
919 /// The number of lines required for this format.
920 unsigned LineCount;
921
922 /// The size of each column in characters.
923 SmallVector<unsigned, 8> ColumnSizes;
924 };
925
926 /// Calculate which \c ColumnFormat fits best into
927 /// \p RemainingCharacters.
928 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
929
930 /// The ordered \c FormatTokens making up the commas of this list.
931 SmallVector<const FormatToken *, 8> Commas;
932
933 /// The length of each of the list's items in characters including the
934 /// trailing comma.
935 SmallVector<unsigned, 8> ItemLengths;
936
937 /// Precomputed formats that can be used for this list.
938 SmallVector<ColumnFormat, 4> Formats;
939
940 bool HasNestedBracedList;
941};
943/// Encapsulates keywords that are context sensitive or for languages not
944/// properly supported by Clang's lexer.
945struct AdditionalKeywords {
947 kw_final = &IdentTable.get("final");
948 kw_override = &IdentTable.get("override");
949 kw_in = &IdentTable.get("in");
950 kw_of = &IdentTable.get("of");
951 kw_CF_CLOSED_ENUM = &IdentTable.get("CF_CLOSED_ENUM");
952 kw_CF_ENUM = &IdentTable.get("CF_ENUM");
953 kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
954 kw_NS_CLOSED_ENUM = &IdentTable.get("NS_CLOSED_ENUM");
955 kw_NS_ENUM = &IdentTable.get("NS_ENUM");
956 kw_NS_ERROR_ENUM = &IdentTable.get("NS_ERROR_ENUM");
957 kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
958
959 kw_as = &IdentTable.get("as");
960 kw_async = &IdentTable.get("async");
961 kw_await = &IdentTable.get("await");
962 kw_declare = &IdentTable.get("declare");
963 kw_finally = &IdentTable.get("finally");
964 kw_from = &IdentTable.get("from");
965 kw_function = &IdentTable.get("function");
966 kw_get = &IdentTable.get("get");
967 kw_import = &IdentTable.get("import");
968 kw_infer = &IdentTable.get("infer");
969 kw_is = &IdentTable.get("is");
970 kw_let = &IdentTable.get("let");
971 kw_module = &IdentTable.get("module");
972 kw_readonly = &IdentTable.get("readonly");
973 kw_set = &IdentTable.get("set");
974 kw_type = &IdentTable.get("type");
975 kw_typeof = &IdentTable.get("typeof");
976 kw_var = &IdentTable.get("var");
977 kw_yield = &IdentTable.get("yield");
978
979 kw_abstract = &IdentTable.get("abstract");
980 kw_assert = &IdentTable.get("assert");
981 kw_extends = &IdentTable.get("extends");
982 kw_implements = &IdentTable.get("implements");
983 kw_instanceof = &IdentTable.get("instanceof");
984 kw_interface = &IdentTable.get("interface");
985 kw_native = &IdentTable.get("native");
986 kw_package = &IdentTable.get("package");
987 kw_synchronized = &IdentTable.get("synchronized");
988 kw_throws = &IdentTable.get("throws");
989 kw___except = &IdentTable.get("__except");
990 kw___has_include = &IdentTable.get("__has_include");
991 kw___has_include_next = &IdentTable.get("__has_include_next");
992
993 kw_mark = &IdentTable.get("mark");
994 kw_region = &IdentTable.get("region");
995
996 kw_extend = &IdentTable.get("extend");
997 kw_option = &IdentTable.get("option");
998 kw_optional = &IdentTable.get("optional");
999 kw_repeated = &IdentTable.get("repeated");
1000 kw_required = &IdentTable.get("required");
1001 kw_returns = &IdentTable.get("returns");
1002
1003 kw_signals = &IdentTable.get("signals");
1004 kw_qsignals = &IdentTable.get("Q_SIGNALS");
1005 kw_slots = &IdentTable.get("slots");
1006 kw_qslots = &IdentTable.get("Q_SLOTS");
1007
1008 // For internal clang-format use.
1010 &IdentTable.get("__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__");
1011
1012 // C# keywords
1013 kw_dollar = &IdentTable.get("dollar");
1014 kw_base = &IdentTable.get("base");
1015 kw_byte = &IdentTable.get("byte");
1016 kw_checked = &IdentTable.get("checked");
1017 kw_decimal = &IdentTable.get("decimal");
1018 kw_delegate = &IdentTable.get("delegate");
1019 kw_event = &IdentTable.get("event");
1020 kw_fixed = &IdentTable.get("fixed");
1021 kw_foreach = &IdentTable.get("foreach");
1022 kw_init = &IdentTable.get("init");
1023 kw_implicit = &IdentTable.get("implicit");
1024 kw_internal = &IdentTable.get("internal");
1025 kw_lock = &IdentTable.get("lock");
1026 kw_null = &IdentTable.get("null");
1027 kw_object = &IdentTable.get("object");
1028 kw_out = &IdentTable.get("out");
1029 kw_params = &IdentTable.get("params");
1030 kw_ref = &IdentTable.get("ref");
1031 kw_string = &IdentTable.get("string");
1032 kw_stackalloc = &IdentTable.get("stackalloc");
1033 kw_sbyte = &IdentTable.get("sbyte");
1034 kw_sealed = &IdentTable.get("sealed");
1035 kw_uint = &IdentTable.get("uint");
1036 kw_ulong = &IdentTable.get("ulong");
1037 kw_unchecked = &IdentTable.get("unchecked");
1038 kw_unsafe = &IdentTable.get("unsafe");
1039 kw_ushort = &IdentTable.get("ushort");
1040 kw_when = &IdentTable.get("when");
1041 kw_where = &IdentTable.get("where");
1042
1043 // Verilog keywords
1044 kw_always = &IdentTable.get("always");
1045 kw_always_comb = &IdentTable.get("always_comb");
1046 kw_always_ff = &IdentTable.get("always_ff");
1047 kw_always_latch = &IdentTable.get("always_latch");
1048 kw_assign = &IdentTable.get("assign");
1049 kw_assume = &IdentTable.get("assume");
1050 kw_automatic = &IdentTable.get("automatic");
1051 kw_before = &IdentTable.get("before");
1052 kw_begin = &IdentTable.get("begin");
1053 kw_begin_keywords = &IdentTable.get("begin_keywords");
1054 kw_bins = &IdentTable.get("bins");
1055 kw_binsof = &IdentTable.get("binsof");
1056 kw_casex = &IdentTable.get("casex");
1057 kw_casez = &IdentTable.get("casez");
1058 kw_celldefine = &IdentTable.get("celldefine");
1059 kw_checker = &IdentTable.get("checker");
1060 kw_clocking = &IdentTable.get("clocking");
1061 kw_constraint = &IdentTable.get("constraint");
1062 kw_cover = &IdentTable.get("cover");
1063 kw_covergroup = &IdentTable.get("covergroup");
1064 kw_coverpoint = &IdentTable.get("coverpoint");
1065 kw_default_decay_time = &IdentTable.get("default_decay_time");
1066 kw_default_nettype = &IdentTable.get("default_nettype");
1067 kw_default_trireg_strength = &IdentTable.get("default_trireg_strength");
1068 kw_delay_mode_distributed = &IdentTable.get("delay_mode_distributed");
1069 kw_delay_mode_path = &IdentTable.get("delay_mode_path");
1070 kw_delay_mode_unit = &IdentTable.get("delay_mode_unit");
1071 kw_delay_mode_zero = &IdentTable.get("delay_mode_zero");
1072 kw_disable = &IdentTable.get("disable");
1073 kw_dist = &IdentTable.get("dist");
1074 kw_edge = &IdentTable.get("edge");
1075 kw_elsif = &IdentTable.get("elsif");
1076 kw_end = &IdentTable.get("end");
1077 kw_end_keywords = &IdentTable.get("end_keywords");
1078 kw_endcase = &IdentTable.get("endcase");
1079 kw_endcelldefine = &IdentTable.get("endcelldefine");
1080 kw_endchecker = &IdentTable.get("endchecker");
1081 kw_endclass = &IdentTable.get("endclass");
1082 kw_endclocking = &IdentTable.get("endclocking");
1083 kw_endfunction = &IdentTable.get("endfunction");
1084 kw_endgenerate = &IdentTable.get("endgenerate");
1085 kw_endgroup = &IdentTable.get("endgroup");
1086 kw_endinterface = &IdentTable.get("endinterface");
1087 kw_endmodule = &IdentTable.get("endmodule");
1088 kw_endpackage = &IdentTable.get("endpackage");
1089 kw_endprimitive = &IdentTable.get("endprimitive");
1090 kw_endprogram = &IdentTable.get("endprogram");
1091 kw_endproperty = &IdentTable.get("endproperty");
1092 kw_endsequence = &IdentTable.get("endsequence");
1093 kw_endspecify = &IdentTable.get("endspecify");
1094 kw_endtable = &IdentTable.get("endtable");
1095 kw_endtask = &IdentTable.get("endtask");
1096 kw_forever = &IdentTable.get("forever");
1097 kw_fork = &IdentTable.get("fork");
1098 kw_generate = &IdentTable.get("generate");
1099 kw_highz0 = &IdentTable.get("highz0");
1100 kw_highz1 = &IdentTable.get("highz1");
1101 kw_iff = &IdentTable.get("iff");
1102 kw_ifnone = &IdentTable.get("ifnone");
1103 kw_ignore_bins = &IdentTable.get("ignore_bins");
1104 kw_illegal_bins = &IdentTable.get("illegal_bins");
1105 kw_initial = &IdentTable.get("initial");
1106 kw_inout = &IdentTable.get("inout");
1107 kw_input = &IdentTable.get("input");
1108 kw_inside = &IdentTable.get("inside");
1109 kw_interconnect = &IdentTable.get("interconnect");
1110 kw_intersect = &IdentTable.get("intersect");
1111 kw_join = &IdentTable.get("join");
1112 kw_join_any = &IdentTable.get("join_any");
1113 kw_join_none = &IdentTable.get("join_none");
1114 kw_large = &IdentTable.get("large");
1115 kw_local = &IdentTable.get("local");
1116 kw_localparam = &IdentTable.get("localparam");
1117 kw_macromodule = &IdentTable.get("macromodule");
1118 kw_matches = &IdentTable.get("matches");
1119 kw_medium = &IdentTable.get("medium");
1120 kw_negedge = &IdentTable.get("negedge");
1121 kw_nounconnected_drive = &IdentTable.get("nounconnected_drive");
1122 kw_output = &IdentTable.get("output");
1123 kw_packed = &IdentTable.get("packed");
1124 kw_parameter = &IdentTable.get("parameter");
1125 kw_posedge = &IdentTable.get("posedge");
1126 kw_primitive = &IdentTable.get("primitive");
1127 kw_priority = &IdentTable.get("priority");
1128 kw_program = &IdentTable.get("program");
1129 kw_property = &IdentTable.get("property");
1130 kw_pull0 = &IdentTable.get("pull0");
1131 kw_pull1 = &IdentTable.get("pull1");
1132 kw_pure = &IdentTable.get("pure");
1133 kw_rand = &IdentTable.get("rand");
1134 kw_randc = &IdentTable.get("randc");
1135 kw_randcase = &IdentTable.get("randcase");
1136 kw_randsequence = &IdentTable.get("randsequence");
1137 kw_repeat = &IdentTable.get("repeat");
1138 kw_resetall = &IdentTable.get("resetall");
1139 kw_sample = &IdentTable.get("sample");
1140 kw_scalared = &IdentTable.get("scalared");
1141 kw_sequence = &IdentTable.get("sequence");
1142 kw_small = &IdentTable.get("small");
1143 kw_soft = &IdentTable.get("soft");
1144 kw_solve = &IdentTable.get("solve");
1145 kw_specify = &IdentTable.get("specify");
1146 kw_specparam = &IdentTable.get("specparam");
1147 kw_strong0 = &IdentTable.get("strong0");
1148 kw_strong1 = &IdentTable.get("strong1");
1149 kw_supply0 = &IdentTable.get("supply0");
1150 kw_supply1 = &IdentTable.get("supply1");
1151 kw_table = &IdentTable.get("table");
1152 kw_tagged = &IdentTable.get("tagged");
1153 kw_task = &IdentTable.get("task");
1154 kw_timescale = &IdentTable.get("timescale");
1155 kw_tri = &IdentTable.get("tri");
1156 kw_tri0 = &IdentTable.get("tri0");
1157 kw_tri1 = &IdentTable.get("tri1");
1158 kw_triand = &IdentTable.get("triand");
1159 kw_trior = &IdentTable.get("trior");
1160 kw_trireg = &IdentTable.get("trireg");
1161 kw_unconnected_drive = &IdentTable.get("unconnected_drive");
1162 kw_undefineall = &IdentTable.get("undefineall");
1163 kw_unique = &IdentTable.get("unique");
1164 kw_unique0 = &IdentTable.get("unique0");
1165 kw_uwire = &IdentTable.get("uwire");
1166 kw_vectored = &IdentTable.get("vectored");
1167 kw_wand = &IdentTable.get("wand");
1168 kw_weak0 = &IdentTable.get("weak0");
1169 kw_weak1 = &IdentTable.get("weak1");
1170 kw_wildcard = &IdentTable.get("wildcard");
1171 kw_wire = &IdentTable.get("wire");
1172 kw_with = &IdentTable.get("with");
1173 kw_wor = &IdentTable.get("wor");
1174
1175 // Symbols that are treated as keywords.
1176 kw_verilogHash = &IdentTable.get("#");
1177 kw_verilogHashHash = &IdentTable.get("##");
1178 kw_apostrophe = &IdentTable.get("\'");
1179
1180 // Keep this at the end of the constructor to make sure everything here
1181 // is
1182 // already initialized.
1183 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
1187 // Keywords from the Java section.
1189
1190 CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>(
1197 // Keywords from the JavaScript section.
1201 // Keywords from the Java section.
1203
1204 // Some keywords are not included here because they don't need special
1205 // treatment like `showcancelled` or they should be treated as identifiers
1206 // like `int` and `logic`.
1207 VerilogExtraKeywords = std::unordered_set<IdentifierInfo *>(
1262 kw_tri, kw_tri0,
1270 kw_with, kw_wor,
1274 // Context sensitive keywords.
1290 // JavaScript keywords.
1311 // Java keywords.
1323 // Pragma keywords.
1327 // Proto keywords.
1335 // QT keywords.
1340
1341 // For internal use by clang-format.
1344 // C# keywords
1378 // Verilog keywords
1510 // Workaround for hashes and backticks in Verilog.
1513
1514 // Symbols in Verilog that don't exist in C++.
1516
1517 /// Returns \c true if \p Tok is a keyword or an identifier.
1518 bool isWordLike(const FormatToken &Tok) const {
1519 // getIdentifierinfo returns non-null for keywords as well as identifiers.
1520 return Tok.Tok.getIdentifierInfo() &&
1522 }
1523
1524 /// Returns \c true if \p Tok is a true JavaScript identifier, returns
1525 /// \c false if it is a keyword or a pseudo keyword.
1526 /// If \c AcceptIdentifierName is true, returns true not only for keywords,
1527 // but also for IdentifierName tokens (aka pseudo-keywords), such as
1528 // ``yield``.
1529 bool IsJavaScriptIdentifier(const FormatToken &Tok,
1530 bool AcceptIdentifierName = true) const {
1531 // Based on the list of JavaScript & TypeScript keywords here:
1532 // https://github.com/microsoft/TypeScript/blob/main/src/compiler/scanner.ts#L74
1533 switch (Tok.Tok.getKind()) {
1534 case tok::kw_break:
1535 case tok::kw_case:
1536 case tok::kw_catch:
1537 case tok::kw_class:
1538 case tok::kw_continue:
1539 case tok::kw_const:
1540 case tok::kw_default:
1541 case tok::kw_delete:
1542 case tok::kw_do:
1543 case tok::kw_else:
1544 case tok::kw_enum:
1545 case tok::kw_export:
1546 case tok::kw_false:
1547 case tok::kw_for:
1548 case tok::kw_if:
1549 case tok::kw_import:
1550 case tok::kw_module:
1551 case tok::kw_new:
1552 case tok::kw_private:
1553 case tok::kw_protected:
1554 case tok::kw_public:
1555 case tok::kw_return:
1556 case tok::kw_static:
1557 case tok::kw_switch:
1558 case tok::kw_this:
1559 case tok::kw_throw:
1560 case tok::kw_true:
1561 case tok::kw_try:
1562 case tok::kw_typeof:
1563 case tok::kw_void:
1564 case tok::kw_while:
1565 // These are JS keywords that are lexed by LLVM/clang as keywords.
1566 return false;
1567 case tok::identifier: {
1568 // For identifiers, make sure they are true identifiers, excluding the
1569 // JavaScript pseudo-keywords (not lexed by LLVM/clang as keywords).
1570 bool IsPseudoKeyword =
1571 JsExtraKeywords.find(Tok.Tok.getIdentifierInfo()) !=
1572 JsExtraKeywords.end();
1573 return AcceptIdentifierName || !IsPseudoKeyword;
1574 }
1575 default:
1576 // Other keywords are handled in the switch below, to avoid problems due
1577 // to duplicate case labels when using the #include trick.
1578 break;
1579 }
1580
1581 switch (Tok.Tok.getKind()) {
1582 // Handle C++ keywords not included above: these are all JS identifiers.
1583#define KEYWORD(X, Y) case tok::kw_##X:
1584#include "clang/Basic/TokenKinds.def"
1585 // #undef KEYWORD is not needed -- it's #undef-ed at the end of
1586 // TokenKinds.def
1587 return true;
1588 default:
1589 // All other tokens (punctuation etc) are not JS identifiers.
1590 return false;
1591 }
1592 }
1594 /// Returns \c true if \p Tok is a C# keyword, returns
1595 /// \c false if it is a anything else.
1596 bool isCSharpKeyword(const FormatToken &Tok) const {
1597 switch (Tok.Tok.getKind()) {
1598 case tok::kw_bool:
1599 case tok::kw_break:
1600 case tok::kw_case:
1601 case tok::kw_catch:
1602 case tok::kw_char:
1603 case tok::kw_class:
1604 case tok::kw_const:
1605 case tok::kw_continue:
1606 case tok::kw_default:
1607 case tok::kw_do:
1608 case tok::kw_double:
1609 case tok::kw_else:
1610 case tok::kw_enum:
1611 case tok::kw_explicit:
1612 case tok::kw_extern:
1613 case tok::kw_false:
1614 case tok::kw_float:
1615 case tok::kw_for:
1616 case tok::kw_goto:
1617 case tok::kw_if:
1618 case tok::kw_int:
1619 case tok::kw_long:
1620 case tok::kw_namespace:
1621 case tok::kw_new:
1622 case tok::kw_operator:
1623 case tok::kw_private:
1624 case tok::kw_protected:
1625 case tok::kw_public:
1626 case tok::kw_return:
1627 case tok::kw_short:
1628 case tok::kw_sizeof:
1629 case tok::kw_static:
1630 case tok::kw_struct:
1631 case tok::kw_switch:
1632 case tok::kw_this:
1633 case tok::kw_throw:
1634 case tok::kw_true:
1635 case tok::kw_try:
1636 case tok::kw_typeof:
1637 case tok::kw_using:
1638 case tok::kw_virtual:
1639 case tok::kw_void:
1640 case tok::kw_volatile:
1641 case tok::kw_while:
1642 return true;
1643 default:
1644 return Tok.is(tok::identifier) &&
1645 CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
1646 CSharpExtraKeywords.end();
1648 }
1649
1650 bool isVerilogWordOperator(const FormatToken &Tok) const {
1651 return Tok.isOneOf(kw_before, kw_intersect, kw_dist, kw_iff, kw_inside,
1653 }
1654
1655 bool isVerilogIdentifier(const FormatToken &Tok) const {
1656 switch (Tok.Tok.getKind()) {
1657 case tok::kw_case:
1658 case tok::kw_class:
1659 case tok::kw_const:
1660 case tok::kw_continue:
1661 case tok::kw_default:
1662 case tok::kw_do:
1663 case tok::kw_extern:
1664 case tok::kw_else:
1665 case tok::kw_enum:
1666 case tok::kw_for:
1667 case tok::kw_if:
1668 case tok::kw_restrict:
1669 case tok::kw_signed:
1670 case tok::kw_static:
1671 case tok::kw_struct:
1672 case tok::kw_typedef:
1673 case tok::kw_union:
1674 case tok::kw_unsigned:
1675 case tok::kw_virtual:
1676 case tok::kw_while:
1677 return false;
1678 case tok::identifier:
1679 return isWordLike(Tok) &&
1680 VerilogExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
1681 VerilogExtraKeywords.end();
1682 default:
1683 // getIdentifierInfo returns non-null for both identifiers and keywords.
1684 return Tok.Tok.getIdentifierInfo();
1685 }
1686 }
1687
1688 /// Returns whether \p Tok is a Verilog preprocessor directive. This is
1689 /// needed because macro expansions start with a backtick as well and they
1690 /// need to be treated differently.
1691 bool isVerilogPPDirective(const FormatToken &Tok) const {
1692 auto Info = Tok.Tok.getIdentifierInfo();
1693 if (!Info)
1694 return false;
1695 switch (Info->getPPKeywordID()) {
1696 case tok::pp_define:
1697 case tok::pp_else:
1698 case tok::pp_endif:
1699 case tok::pp_ifdef:
1700 case tok::pp_ifndef:
1701 case tok::pp_include:
1702 case tok::pp_line:
1703 case tok::pp_pragma:
1704 case tok::pp_undef:
1705 return true;
1706 default:
1714 }
1716
1717 /// Returns whether \p Tok is a Verilog keyword that opens a block.
1718 bool isVerilogBegin(const FormatToken &Tok) const {
1719 // `table` is not included since it needs to be treated specially.
1720 return !Tok.endsSequence(kw_fork, kw_disable) &&
1723
1724 /// Returns whether \p Tok is a Verilog keyword that closes a block.
1725 bool isVerilogEnd(const FormatToken &Tok) const {
1726 return !Tok.endsSequence(kw_join, kw_rand) &&
1727 Tok.isOneOf(TT_MacroBlockEnd, kw_end, kw_endcase, kw_endclass,
1735
1736 /// Returns whether \p Tok is a Verilog keyword that opens a module, etc.
1737 bool isVerilogHierarchy(const FormatToken &Tok) const {
1739 return false;
1740 if (Tok.is(kw_property)) {
1741 const FormatToken *Prev = Tok.getPreviousNonComment();
1742 return !(Prev &&
1743 Prev->isOneOf(tok::kw_restrict, kw_assert, kw_assume, kw_cover));
1744 }
1745 return Tok.isOneOf(tok::kw_case, tok::kw_class, kw_function, kw_module,
1750 }
1751
1752 bool isVerilogEndOfLabel(const FormatToken &Tok) const {
1753 const FormatToken *Next = Tok.getNextNonComment();
1754 // In Verilog the colon in a default label is optional.
1755 return Tok.is(TT_CaseLabelColon) ||
1756 (Tok.is(tok::kw_default) &&
1757 !(Next && Next->isOneOf(tok::colon, tok::semi, kw_clocking, kw_iff,
1759 }
1761 /// Returns whether \p Tok is a Verilog keyword that starts a
1762 /// structured procedure like 'always'.
1763 bool isVerilogStructuredProcedure(const FormatToken &Tok) const {
1766 }
1767
1768 bool isVerilogQualifier(const FormatToken &Tok) const {
1769 switch (Tok.Tok.getKind()) {
1770 case tok::kw_extern:
1771 case tok::kw_signed:
1772 case tok::kw_static:
1773 case tok::kw_unsigned:
1774 case tok::kw_virtual:
1775 return true;
1776 case tok::identifier:
1777 return Tok.isOneOf(
1784 default:
1785 return false;
1786 }
1787 }
1788
1789private:
1790 /// The JavaScript keywords beyond the C++ keyword set.
1791 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
1792
1793 /// The C# keywords beyond the C++ keyword set
1794 std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
1795
1796 /// The Verilog keywords beyond the C++ keyword set.
1797 std::unordered_set<IdentifierInfo *> VerilogExtraKeywords;
1798};
1799
1800inline bool isLineComment(const FormatToken &FormatTok) {
1801 return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith("/*");
1802}
1803
1804// Checks if \p FormatTok is a line comment that continues the line comment
1805// \p Previous. The original column of \p MinColumnToken is used to determine
1806// whether \p FormatTok is indented enough to the right to continue \p Previous.
1807inline bool continuesLineComment(const FormatToken &FormatTok,
1808 const FormatToken *Previous,
1809 const FormatToken *MinColumnToken) {
1810 if (!Previous || !MinColumnToken)
1811 return false;
1812 unsigned MinContinueColumn =
1813 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
1814 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
1816 FormatTok.OriginalColumn >= MinContinueColumn;
1817}
1818
1819} // namespace format
1820} // namespace clang
1821
1822#endif
#define LIST_TOKEN_TYPES
Definition: FormatToken.h:29
Various functions to configurably format source code.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Defines and computes precedence levels for binary/ternary operators.
static constexpr bool isOneOf()
StateNode * Previous
ContinuationIndenter * Indenter
One of these records is kept for each identifier that is lexed.
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.
Implements an efficient mapping from strings to IdentifierInfo nodes.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
Encodes a location in the source.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:186
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:98
tok::TokenKind getKind() const
Definition: Token.h:93
bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const
Return true if we have an ObjC keyword identifier.
Definition: Lexer.cpp:57
The base class of the type hierarchy.
Definition: Type.h:1568
unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter, bool DryRun) override
Apply the special formatting that the given role demands.
void CommaFound(const FormatToken *Token) override
Adds Token as the next comma to the CommaSeparated list.
Definition: FormatToken.h:896
const FormatToken * lastComma() override
Definition: FormatToken.h:900
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:96
void precomputeFormattingInfos(const FormatToken *Token) override
After the TokenAnnotator has finished annotating all the tokens, this function precomputes required i...
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:867
virtual void precomputeFormattingInfos(const FormatToken *Token)
After the TokenAnnotator has finished annotating all the tokens, this function precomputes required i...
Definition: FormatToken.cpp:94
const FormatStyle & Style
Definition: FormatToken.h:879
virtual void CommaFound(const FormatToken *Token)
Notifies the Role that a comma was found.
Definition: FormatToken.h:874
virtual const FormatToken * lastComma()
Definition: FormatToken.h:876
virtual unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter, bool DryRun)
Apply the special formatting that the given role demands.
Definition: FormatToken.h:859
const char * getTokenTypeName(TokenType Type)
Determines the name of a token type.
Definition: FormatToken.cpp:24
MacroRole
Roles a token can take in a configured macro expansion.
Definition: FormatToken.h:190
@ MR_UnexpandedArg
The token is part of a macro argument that was previously formatted as expansion when formatting the ...
Definition: FormatToken.h:196
@ MR_Hidden
The token was expanded from a macro definition, and is not visible as part of the macro call.
Definition: FormatToken.h:199
@ MR_ExpandedArg
The token was expanded from a macro argument when formatting the expanded token sequence.
Definition: FormatToken.h:193
bool continuesLineComment(const FormatToken &FormatTok, const FormatToken *Previous, const FormatToken *MinColumnToken)
Definition: FormatToken.h:1804
bool isLineComment(const FormatToken &FormatTok)
Definition: FormatToken.h:1797
TokenType
Determines the semantic type of a syntactic token, e.g.
Definition: FormatToken.h:171
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:81
ObjCKeywordKind
Provides a namespace for Objective-C keywords which start with an '@'.
Definition: TokenKinds.h:41
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
PPKeywordKind
Provides a namespace for preprocessor keywords which start with a '#' at the beginning of the line.
Definition: TokenKinds.h:33
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.
Encapsulates keywords that are context sensitive or for languages not properly supported by Clang's l...
Definition: FormatToken.h:942
bool isVerilogEnd(const FormatToken &Tok) const
Returns whether Tok is a Verilog keyword that closes a block.
Definition: FormatToken.h:1722
IdentifierInfo * kw_nounconnected_drive
Definition: FormatToken.h:1453
bool isVerilogBegin(const FormatToken &Tok) const
Returns whether Tok is a Verilog keyword that opens a block.
Definition: FormatToken.h:1715
bool isVerilogQualifier(const FormatToken &Tok) const
Definition: FormatToken.h:1765
bool isWordLike(const FormatToken &Tok) const
Returns true if Tok is a keyword or an identifier.
Definition: FormatToken.h:1515
IdentifierInfo * kw_default_trireg_strength
Definition: FormatToken.h:1399
bool isVerilogStructuredProcedure(const FormatToken &Tok) const
Returns whether Tok is a Verilog keyword that starts a structured procedure like 'always'.
Definition: FormatToken.h:1760
bool isVerilogEndOfLabel(const FormatToken &Tok) const
Definition: FormatToken.h:1749
bool isVerilogWordOperator(const FormatToken &Tok) const
Definition: FormatToken.h:1647
IdentifierInfo * kw_delay_mode_distributed
Definition: FormatToken.h:1400
bool IsJavaScriptIdentifier(const FormatToken &Tok, bool AcceptIdentifierName=true) const
Returns true if Tok is a true JavaScript identifier, returns false if it is a keyword or a pseudo key...
Definition: FormatToken.h:1526
IdentifierInfo * kw_default_decay_time
Definition: FormatToken.h:1397
bool isCSharpKeyword(const FormatToken &Tok) const
Returns true if Tok is a C# keyword, returns false if it is a anything else.
Definition: FormatToken.h:1593
IdentifierInfo * kw___has_include_next
Definition: FormatToken.h:1285
bool isVerilogHierarchy(const FormatToken &Tok) const
Returns whether Tok is a Verilog keyword that opens a module, etc.
Definition: FormatToken.h:1734
bool isVerilogPPDirective(const FormatToken &Tok) const
Returns whether Tok is a Verilog preprocessor directive.
Definition: FormatToken.h:1688
IdentifierInfo * kw_unconnected_drive
Definition: FormatToken.h:1493
IdentifierInfo * kw_internal_ident_after_define
Definition: FormatToken.h:1339
bool isVerilogIdentifier(const FormatToken &Tok) const
Definition: FormatToken.h:1652
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:55
A wrapper around a Token storing information about the whitespace characters preceding it.
Definition: FormatToken.h:256
unsigned NestingLevel
The nesting level of this token, i.e.
Definition: FormatToken.h:464
bool isMemberAccess() const
Returns true if this is a "." or "->" accessing a member.
Definition: FormatToken.h:659
unsigned FakeRParens
Insert this many fake ) after this token for correct indentation.
Definition: FormatToken.h:496
bool ArrayInitializerLineStart
This notes the start of the line of an array initializer.
Definition: FormatToken.h:519
bool isTypeFinalized() const
Definition: FormatToken.h:407
bool Optional
Is optional and can be removed.
Definition: FormatToken.h:525
bool MacroParent
When macro expansion introduces nodes with children, those are marked as MacroParent.
Definition: FormatToken.h:548
SmallVector< AnnotatedLine *, 1 > Children
If this token starts a block, this contains all the unwrapped lines in it.
Definition: FormatToken.h:535
bool closesScopeAfterBlock() const
Definition: FormatToken.h:576
bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const
Definition: FormatToken.h:604
prec::Level ForcedPrecedence
Used to set an operator precedence explicitly.
Definition: FormatToken.h:410
bool closesBlockOrBlockTypeList(const FormatStyle &Style) const
Same as opensBlockOrBlockTypeList, but for the closing token.
Definition: FormatToken.h:786
bool is(tok::PPKeywordKind Kind) const
Definition: FormatToken.h:555
unsigned BindingStrength
The binding strength of a token.
Definition: FormatToken.h:460
bool isObjCAccessSpecifier() const
Definition: FormatToken.h:629
const FormatToken * getNamespaceToken() const
Return the actual namespace token, if this token starts a namespace block.
Definition: FormatToken.h:794
unsigned StartsBinaryExpression
true if this token starts a binary expression, i.e.
Definition: FormatToken.h:313
void setPackingKind(ParameterPackingKind K)
Definition: FormatToken.h:372
unsigned ClosesTemplateDeclaration
true if this is the ">" of "template<..>".
Definition: FormatToken.h:309
unsigned OperatorIndex
If this is an operator (or "."/"->") in a sequence of operators with the same precedence,...
Definition: FormatToken.h:500
unsigned OriginalColumn
The original 0-based column of this token, including expanded tabs.
Definition: FormatToken.h:451
unsigned ContinuesLineCommentSection
Does this line comment continue a line comment section?
Definition: FormatToken.h:325
unsigned CanBreakBefore
true if it is allowed to break before this token.
Definition: FormatToken.h:306
bool isNot(T Kind) const
Definition: FormatToken.h:569
StringRef TokenText
The raw text of the token.
Definition: FormatToken.h:275
unsigned LongestObjCSelectorName
If this is the first ObjC selector name in an ObjC method definition or call, this contains the lengt...
Definition: FormatToken.h:477
ParameterPackingKind getPackingKind() const
Definition: FormatToken.h:369
SourceLocation getStartOfNonWhitespace() const
Returns actual token start location without leading escaped newlines and whitespace.
Definition: FormatToken.h:735
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:587
tok::TokenKind ParentBracket
If this is a bracket ("<", "(", "[" or "{"), contains the kind of the surrounding bracket.
Definition: FormatToken.h:443
unsigned LastNewlineOffset
The offset just past the last ' ' in this token's leading whitespace (relative to WhiteSpaceStart).
Definition: FormatToken.h:420
unsigned SplitPenalty
Penalty for inserting a line break before this token.
Definition: FormatToken.h:470
bool opensScope() const
Returns whether Tok is ([{ or an opening < of a template or in protos.
Definition: FormatToken.h:639
FormatToken * getNextNonComment() const
Returns the next token ignoring comments.
Definition: FormatToken.h:761
void setDecision(FormatDecision D)
Definition: FormatToken.h:359
FormatToken * getPreviousNonComment() const
Returns the previous token ignoring comments.
Definition: FormatToken.h:753
bool isSimpleTypeSpecifier() const
Determine whether the token is a simple-type-specifier.
Definition: FormatToken.cpp:39
SmallVector< prec::Level, 4 > FakeLParens
Stores the number of required fake parentheses and the corresponding operator precedence.
Definition: FormatToken.h:494
unsigned Finalized
If true, this token has been fully formatted (indented and potentially re-formatted inside),...
Definition: FormatToken.h:330
std::optional< MacroExpansion > MacroCtx
Definition: FormatToken.h:539
BraceBlockKind getBlockKind() const
Definition: FormatToken.h:343
FormatToken * Next
The next token in the unwrapped line.
Definition: FormatToken.h:513
unsigned IsMultiline
Whether the token text contains newlines (escaped or not).
Definition: FormatToken.h:291
unsigned EndsCppAttributeGroup
true if this token ends a group of C++ attributes.
Definition: FormatToken.h:336
unsigned NewlinesBefore
The number of newlines immediately before the Token.
Definition: FormatToken.h:416
void setBlockKind(BraceBlockKind BBK)
Definition: FormatToken.h:346
bool isIf(bool AllowConstexprMacro=true) const
Definition: FormatToken.h:571
unsigned SpacesRequiredBefore
The number of spaces that should be inserted before this token.
Definition: FormatToken.h:432
std::shared_ptr< TokenRole > Role
A token can have a special role that can carry extra information about the token's formatting.
Definition: FormatToken.h:281
unsigned MustBreakBefore
Whether there must be a line break before this token.
Definition: FormatToken.h:300
unsigned HasUnescapedNewline
Whether there is at least one unescaped newline before the Token.
Definition: FormatToken.h:288
bool isTypeOrIdentifier() const
Definition: FormatToken.cpp:74
unsigned PartOfMultiVariableDeclStmt
Is this token part of a DeclStmt defining multiple variables?
Definition: FormatToken.h:320
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:425
unsigned ObjCSelectorNameParts
If this is the first ObjC selector name in an ObjC method definition or call, this contains the numbe...
Definition: FormatToken.h:482
void setType(TokenType T)
Definition: FormatToken.h:385
bool isFunctionLikeKeyword() const
Returns true if this is a keyword that can be used like a function call (e.g.
Definition: FormatToken.h:693
bool isStringLiteral() const
Definition: FormatToken.h:602
bool IsArrayInitializer
This starts an array initializer.
Definition: FormatToken.h:522
bool closesScope() const
Returns whether Tok is )]} or a closing > of a template or in protos.
Definition: FormatToken.h:649
unsigned EndsBinaryExpression
true if this token ends a binary expression.
Definition: FormatToken.h:315
bool isBinaryOperator() const
Definition: FormatToken.h:681
unsigned UnbreakableTailLength
The length of following tokens until the next natural split point, or the next token that can be brok...
Definition: FormatToken.h:455
prec::Level getPrecedence() const
Definition: FormatToken.h:745
FormatDecision getDecision() const
Definition: FormatToken.h:356
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:550
unsigned LastLineColumnWidth
Contains the width in columns of the last line of a multi-line token.
Definition: FormatToken.h:429
unsigned IndentLevel
The indent level of this token. Copied from the surrounding line.
Definition: FormatToken.h:467
bool opensBlockOrBlockTypeList(const FormatStyle &Style) const
Returns true if this tokens starts a block-type list, i.e.
Definition: FormatToken.cpp:78
unsigned BlockParameterCount
Number of parameters that are nested blocks, if this is "(", "[" or "<".
Definition: FormatToken.h:439
unsigned TotalLength
The total length of the unwrapped line up to and including this token.
Definition: FormatToken.h:447
bool hasWhitespaceBefore() const
Returns true if the range of whitespace immediately preceding the Token is not empty.
Definition: FormatToken.h:741
bool isOneOf(A K1, B K2) const
Definition: FormatToken.h:562
TokenType getType() const
Returns the token's type, e.g.
Definition: FormatToken.h:384
unsigned IsFirst
Indicates that this is the first token of the file.
Definition: FormatToken.h:294
bool isUnaryOperator() const
Definition: FormatToken.h:665
unsigned IsUnterminatedLiteral
Set to true if this token is an unterminated literal.
Definition: FormatToken.h:303
bool isTrailingComment() const
Definition: FormatToken.h:686
unsigned ClosesRequiresClause
true if this is the last token within requires clause.
Definition: FormatToken.h:333
unsigned ParameterIndex
The 0-based index of the parameter/argument.
Definition: FormatToken.h:487
bool isCppStructuredBinding(const FormatStyle &Style) const
Returns whether the token is the left square bracket of a C++ structured binding declaration.
Definition: FormatToken.h:774
int8_t BraceCount
Number of optional braces to be inserted after this token: -1: a single left brace 0: no braces >0: n...
Definition: FormatToken.h:531
bool canBePointerOrReferenceQualifier() const
Definition: FormatToken.h:617
unsigned ParameterCount
Number of parameters, if this is "(", "[" or "<".
Definition: FormatToken.h:435
bool isAccessSpecifier(bool ColonRequired=true) const
Definition: FormatToken.h:608
bool StartsColumn
The first token in set of column elements.
Definition: FormatToken.h:516
SourceRange WhitespaceRange
The range of the whitespace immediately preceding the Token.
Definition: FormatToken.h:284
FormatToken * NextOperator
If this is an operator (or "."/"->") in a sequence of operators with the same precedence,...
Definition: FormatToken.h:504
FormatToken * MatchingParen
If this is a bracket, this points to the matching one.
Definition: FormatToken.h:507
void copyFrom(const FormatToken &Tok)
Definition: FormatToken.h:807
void overwriteFixedType(TokenType T)
Definition: FormatToken.h:403
FormatToken * Previous
The previous token in the unwrapped line.
Definition: FormatToken.h:510
bool isLabelString() const
Returns true if this is a string literal that's like a label, e.g.
Definition: FormatToken.h:717
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:598
void setFinalizedType(TokenType T)
Sets the type and also the finalized flag.
Definition: FormatToken.h:399
The current state when indenting a unwrapped line.
Contains information on the token's role in a macro expansion.
Definition: FormatToken.h:229
llvm::SmallVector< FormatToken *, 1 > ExpandedFrom
The stack of macro call identifier tokens this token was expanded from.
Definition: FormatToken.h:241
unsigned StartOfExpansion
The number of expansions of which this macro is the first entry.
Definition: FormatToken.h:244
MacroRole Role
The token's role in the macro expansion.
Definition: FormatToken.h:238
unsigned EndOfExpansion
The number of currently open expansions in ExpandedFrom this macro is the last token in.
Definition: FormatToken.h:248