Thanks to visit codestin.com
Credit goes to clang.llvm.org

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