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