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