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