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