clang 23.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(EnumEqual) \
80 TYPE(EnumLBrace) \
81 TYPE(EnumRBrace) \
82 TYPE(EnumUnderlyingTypeColon) \
83 TYPE(FatArrow) \
84 TYPE(ForEachMacro) \
85 TYPE(FunctionAnnotationRParen) \
86 TYPE(FunctionDeclarationName) \
87 TYPE(FunctionDeclarationLParen) \
88 TYPE(FunctionLBrace) \
89 TYPE(FunctionLikeMacro) \
90 TYPE(FunctionLikeOrFreestandingMacro) \
91 TYPE(FunctionTypeLParen) \
92 /* The colons as part of a C11 _Generic selection */ \
93 TYPE(GenericSelectionColon) \
94 /* The colon at the end of a goto label. */ \
95 TYPE(GotoLabelColon) \
96 TYPE(IfMacro) \
97 TYPE(ImplicitStringLiteral) \
98 TYPE(InheritanceColon) \
99 TYPE(InheritanceComma) \
100 TYPE(InlineASMBrace) \
101 TYPE(InlineASMColon) \
102 TYPE(InlineASMSymbolicNameLSquare) \
103 TYPE(JavaAnnotation) \
104 TYPE(JsAndAndEqual) \
105 TYPE(JsComputedPropertyName) \
106 TYPE(JsExponentiation) \
107 TYPE(JsExponentiationEqual) \
108 TYPE(JsPipePipeEqual) \
109 TYPE(JsPrivateIdentifier) \
110 TYPE(JsTypeColon) \
111 TYPE(JsTypeOperator) \
112 TYPE(JsTypeOptionalQuestion) \
113 TYPE(LambdaArrow) \
114 TYPE(LambdaDefinitionLParen) \
115 TYPE(LambdaLBrace) \
116 TYPE(LambdaLSquare) \
117 TYPE(LeadingJavaAnnotation) \
118 TYPE(LineComment) \
119 TYPE(MacroBlockBegin) \
120 TYPE(MacroBlockEnd) \
121 TYPE(ModulePartitionColon) \
122 TYPE(NamespaceLBrace) \
123 TYPE(NamespaceMacro) \
124 TYPE(NamespaceRBrace) \
125 TYPE(NonNullAssertion) \
126 TYPE(NullCoalescingEqual) \
127 TYPE(NullCoalescingOperator) \
128 TYPE(NullPropagatingOperator) \
129 TYPE(ObjCBlockLBrace) \
130 TYPE(ObjCBlockLParen) \
131 TYPE(ObjCDecl) \
132 TYPE(ObjCForIn) \
133 /* The square brackets surrounding a method call, the colon separating the \
134 * method or parameter name and the argument inside the square brackets, and \
135 * the colon separating the method or parameter name and the type inside the \
136 * method declaration. */ \
137 TYPE(ObjCMethodExpr) \
138 /* The '+' or '-' at the start of the line. */ \
139 TYPE(ObjCMethodSpecifier) \
140 TYPE(ObjCProperty) \
141 /* The parentheses following '@selector' and the colon following the method \
142 * or parameter name inside the parentheses. */ \
143 TYPE(ObjCSelector) \
144 TYPE(ObjCStringLiteral) \
145 TYPE(OverloadedOperator) \
146 TYPE(OverloadedOperatorLParen) \
147 TYPE(PointerOrReference) \
148 TYPE(ProtoExtensionLSquare) \
149 TYPE(PureVirtualSpecifier) \
150 TYPE(QtProperty) \
151 TYPE(RangeBasedForLoopColon) \
152 TYPE(RecordLBrace) \
153 TYPE(RecordRBrace) \
154 TYPE(RegexLiteral) \
155 TYPE(RequiresClause) \
156 TYPE(RequiresClauseInARequiresExpression) \
157 TYPE(RequiresExpression) \
158 TYPE(RequiresExpressionLBrace) \
159 TYPE(RequiresExpressionLParen) \
160 /* The hash key in languages that have hash literals, not including the \
161 * field name in the C++ struct literal. Also the method or parameter name \
162 * in the Objective-C method declaration or call. */ \
163 TYPE(SelectorName) \
164 TYPE(StartOfName) \
165 TYPE(StatementAttributeLikeMacro) \
166 TYPE(StatementMacro) \
167 /* A string that is part of a string concatenation. For C#, JavaScript, and \
168 * Java, it is used for marking whether a string needs parentheses around it \
169 * if it is to be split into parts joined by `+`. For Verilog, whether \
170 * braces need to be added to split it. Not used for other languages. */ \
171 TYPE(StringInConcatenation) \
172 TYPE(StructLBrace) \
173 TYPE(StructRBrace) \
174 TYPE(StructuredBindingLSquare) \
175 TYPE(SwitchExpressionLabel) \
176 TYPE(SwitchExpressionLBrace) \
177 TYPE(TableGenBangOperator) \
178 TYPE(TableGenCondOperator) \
179 TYPE(TableGenCondOperatorColon) \
180 TYPE(TableGenCondOperatorComma) \
181 TYPE(TableGenDAGArgCloser) \
182 TYPE(TableGenDAGArgListColon) \
183 TYPE(TableGenDAGArgListColonToAlign) \
184 TYPE(TableGenDAGArgListComma) \
185 TYPE(TableGenDAGArgListCommaToBreak) \
186 TYPE(TableGenDAGArgOpener) \
187 TYPE(TableGenDAGArgOpenerToBreak) \
188 TYPE(TableGenDAGArgOperatorID) \
189 TYPE(TableGenDAGArgOperatorToBreak) \
190 TYPE(TableGenListCloser) \
191 TYPE(TableGenListOpener) \
192 TYPE(TableGenMultiLineString) \
193 TYPE(TableGenTrailingPasteOperator) \
194 TYPE(TableGenValueSuffix) \
195 TYPE(TemplateCloser) \
196 TYPE(TemplateOpener) \
197 TYPE(TemplateString) \
198 TYPE(TrailingAnnotation) \
199 TYPE(TrailingReturnArrow) \
200 TYPE(TrailingUnaryOperator) \
201 TYPE(TypeDeclarationParen) \
202 TYPE(TemplateName) \
203 TYPE(TypeName) \
204 TYPE(TypenameMacro) \
205 TYPE(UnaryOperator) \
206 TYPE(UnionLBrace) \
207 TYPE(UnionRBrace) \
208 TYPE(UntouchableMacroFunc) \
209 TYPE(VariableTemplate) \
210 /* Like in 'assign x = 0, y = 1;' . */ \
211 TYPE(VerilogAssignComma) \
212 /* like in begin : block */ \
213 TYPE(VerilogBlockLabelColon) \
214 /* The square bracket for the dimension part of the type name. \
215 * In 'logic [1:0] x[1:0]', only the first '['. This way we can have space \
216 * before the first bracket but not the second. */ \
217 TYPE(VerilogDimensionedTypeName) \
218 /* list of port connections or parameters in a module instantiation */ \
219 TYPE(VerilogInstancePortComma) \
220 TYPE(VerilogInstancePortLParen) \
221 /* A parenthesized list within which line breaks are inserted by the \
222 * formatter, for example the list of ports in a module header. */ \
223 TYPE(VerilogMultiLineListLParen) \
224 /* for the base in a number literal, not including the quote */ \
225 TYPE(VerilogNumberBase) \
226 /* The text that is in the opaque protected block. Like the text between \
227 * 'pragma protect data_block' and 'pragma protect end_protected'. */ \
228 TYPE(VerilogProtected) \
229 /* like `(strong1, pull0)` */ \
230 TYPE(VerilogStrength) \
231 /* Things inside the table in user-defined primitives. */ \
232 TYPE(VerilogTableItem) \
233 /* those that separate ports of different types */ \
234 TYPE(VerilogTypeComma) \
235 TYPE(Unknown)
236
237/// Determines the semantic type of a syntactic token, e.g. whether "<" is a
238/// template opener or binary operator.
239enum TokenType : uint8_t {
240#define TYPE(X) TT_##X,
242#undef TYPE
244};
246/// Determines the name of a token type.
247const char *getTokenTypeName(TokenType Type);
249// Represents what type of block a set of braces open.
252// The packing kind of a function's parameters.
256
257/// Roles a token can take in a configured macro expansion.
258enum MacroRole {
259 /// The token was expanded from a macro argument when formatting the expanded
260 /// token sequence.
262 /// The token is part of a macro argument that was previously formatted as
263 /// expansion when formatting the unexpanded macro call.
265 /// The token was expanded from a macro definition, and is not visible as part
266 /// of the macro call.
267 MR_Hidden,
268};
269
270struct FormatToken;
271
272/// Contains information on the token's role in a macro expansion.
273///
274/// Given the following definitions:
275/// A(X) = [ X ]
276/// B(X) = < X >
277/// C(X) = X
278///
279/// Consider the macro call:
280/// A({B(C(C(x)))}) -> [{<x>}]
281///
282/// In this case, the tokens of the unexpanded macro call will have the
283/// following relevant entries in their macro context (note that formatting
284/// the unexpanded macro call happens *after* formatting the expanded macro
285/// call):
286/// A( { B( C( C(x) ) ) } )
287/// Role: NN U NN NN NNUN N N U N (N=None, U=UnexpandedArg)
288///
289/// [ { < x > } ]
290/// Role: H E H E H E H (H=Hidden, E=ExpandedArg)
291/// ExpandedFrom[0]: A A A A A A A
292/// ExpandedFrom[1]: B B B
293/// ExpandedFrom[2]: C
294/// ExpandedFrom[3]: C
295/// StartOfExpansion: 1 0 1 2 0 0 0
296/// EndOfExpansion: 0 0 0 2 1 0 1
297struct MacroExpansion {
300 /// The token's role in the macro expansion.
301 /// When formatting an expanded macro, all tokens that are part of macro
302 /// arguments will be MR_ExpandedArg, while all tokens that are not visible in
303 /// the macro call will be MR_Hidden.
304 /// When formatting an unexpanded macro call, all tokens that are part of
305 /// macro arguments will be MR_UnexpandedArg.
307
308 /// The stack of macro call identifier tokens this token was expanded from.
310
311 /// The number of expansions of which this macro is the first entry.
312 unsigned StartOfExpansion = 0;
313
314 /// The number of currently open expansions in \c ExpandedFrom this macro is
315 /// the last token in.
316 unsigned EndOfExpansion = 0;
317};
318
319class TokenRole;
320class AnnotatedLine;
321
322/// A wrapper around a \c Token storing information about the
323/// whitespace characters preceding it.
324struct FormatToken {
333 BlockKind(BK_Unknown), Decision(FD_Unformatted),
334 PackingKind(PPK_Inconclusive), TypeIsFinalized(false),
335 Type(TT_Unknown) {}
336
337 /// The \c Token.
338 Token Tok;
339
340 /// The raw text of the token.
341 ///
342 /// Contains the raw token text without leading whitespace and without leading
343 /// escaped newlines.
344 StringRef TokenText;
345
346 /// A token can have a special role that can carry extra information
347 /// about the token's formatting.
348 /// FIXME: Make FormatToken for parsing and AnnotatedToken two different
349 /// classes and make this a unique_ptr in the AnnotatedToken class.
350 std::shared_ptr<TokenRole> Role;
351
352 /// The range of the whitespace immediately preceding the \c Token.
354
355 /// Whether there is at least one unescaped newline before the \c
356 /// Token.
357 unsigned HasUnescapedNewline : 1;
358
359 /// Whether the token text contains newlines (escaped or not).
360 unsigned IsMultiline : 1;
361
362 /// Indicates that this is the first token of the file.
363 unsigned IsFirst : 1;
364
365 /// Whether there must be a line break before this token.
366 ///
367 /// This happens for example when a preprocessor directive ended directly
368 /// before the token.
369 unsigned MustBreakBefore : 1;
370
371 /// Whether MustBreakBefore is finalized during parsing and must not
372 /// be reset between runs.
374
375 /// Set to \c true if this token is an unterminated literal.
376 unsigned IsUnterminatedLiteral : 1;
377
378 /// \c true if it is allowed to break before this token.
379 unsigned CanBreakBefore : 1;
381 /// \c true if this is the ">" of "template<..>".
382 unsigned ClosesTemplateDeclaration : 1;
383
384 /// \c true if this token starts a binary expression, i.e. has at least
385 /// one fake l_paren with a precedence greater than prec::Unknown.
386 unsigned StartsBinaryExpression : 1;
387 /// \c true if this token ends a binary expression.
388 unsigned EndsBinaryExpression : 1;
389
390 /// Is this token part of a \c DeclStmt defining multiple variables?
391 ///
392 /// Only set if \c Type == \c TT_StartOfName.
394
395 /// Does this line comment continue a line comment section?
396 ///
397 /// Only set to true if \c Type == \c TT_LineComment.
398 unsigned ContinuesLineCommentSection : 1;
399
400 /// If \c true, this token has been fully formatted (indented and
401 /// potentially re-formatted inside), and we do not allow further formatting
402 /// changes.
403 unsigned Finalized : 1;
404
405 /// \c true if this is the last token within requires clause.
407
408 /// \c true if this token ends a group of C++ attributes.
409 unsigned EndsCppAttributeGroup : 1;
410
411private:
412 /// Contains the kind of block if this token is a brace.
413 unsigned BlockKind : 2;
414
415public:
417 return static_cast<BraceBlockKind>(BlockKind);
418 }
420 BlockKind = BBK;
421 assert(getBlockKind() == BBK && "BraceBlockKind overflow!");
422 }
423
424private:
425 /// Stores the formatting decision for the token once it was made.
426 unsigned Decision : 2;
427
428public:
430 return static_cast<FormatDecision>(Decision);
431 }
433 Decision = D;
434 assert(getDecision() == D && "FormatDecision overflow!");
435 }
436
437private:
438 /// If this is an opening parenthesis, how are the parameters packed?
439 unsigned PackingKind : 2;
440
441public:
443 return static_cast<ParameterPackingKind>(PackingKind);
446 PackingKind = K;
447 assert(getPackingKind() == K && "ParameterPackingKind overflow!");
448 }
449
450private:
451 unsigned TypeIsFinalized : 1;
453
454public:
455 /// Returns the token's type, e.g. whether "<" is a template opener or
456 /// binary operator.
457 TokenType getType() const { return Type; }
458 void setType(TokenType T) {
459 // If this token is a macro argument while formatting an unexpanded macro
460 // call, we do not change its type any more - the type was deduced from
461 // formatting the expanded macro stream already.
462 if (MacroCtx && MacroCtx->Role == MR_UnexpandedArg)
463 return;
464 assert((!TypeIsFinalized || T == Type) &&
465 "Please use overwriteFixedType to change a fixed type.");
466 Type = T;
467 }
468 /// Sets the type and also the finalized flag. This prevents the type to be
469 /// reset in TokenAnnotator::resetTokenMetadata(). If the type needs to be set
470 /// to another one please use overwriteFixedType, or even better remove the
471 /// need to reassign the type.
473 if (MacroCtx && MacroCtx->Role == MR_UnexpandedArg)
474 return;
475 Type = T;
476 TypeIsFinalized = true;
477 }
479 if (MacroCtx && MacroCtx->Role == MR_UnexpandedArg)
480 return;
481 TypeIsFinalized = false;
482 setType(T);
483 }
484 bool isTypeFinalized() const { return TypeIsFinalized; }
485
486 /// Used to set an operator precedence explicitly.
488
489 /// The number of newlines immediately before the \c Token.
490 ///
491 /// This can be used to determine what the user wrote in the original code
492 /// and thereby e.g. leave an empty line between two function definitions.
493 unsigned NewlinesBefore = 0;
494
495 /// The number of newlines immediately before the \c Token after formatting.
496 ///
497 /// This is used to avoid overlapping whitespace replacements when \c Newlines
498 /// is recomputed for a finalized preprocessor branching directive.
499 int Newlines = -1;
500
501 /// The offset just past the last '\n' in this token's leading
502 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
503 unsigned LastNewlineOffset = 0;
504
505 /// The width of the non-whitespace parts of the token (or its first
506 /// line for multi-line tokens) in columns.
507 /// We need this to correctly measure number of columns a token spans.
508 unsigned ColumnWidth = 0;
510 /// Contains the width in columns of the last line of a multi-line
511 /// token.
512 unsigned LastLineColumnWidth = 0;
514 /// The number of spaces that should be inserted before this token.
515 unsigned SpacesRequiredBefore = 0;
516
517 /// Number of parameters, if this is "(", "[" or "<".
518 unsigned ParameterCount = 0;
519
520 /// Number of parameters that are nested blocks,
521 /// if this is "(", "[" or "<".
522 unsigned BlockParameterCount = 0;
523
524 /// If this is a bracket ("<", "(", "[" or "{"), contains the kind of
525 /// the surrounding bracket.
526 tok::TokenKind ParentBracket = tok::unknown;
527
528 /// The total length of the unwrapped line up to and including this
529 /// token.
530 unsigned TotalLength = 0;
531
532 /// The original 0-based column of this token, including expanded tabs.
533 /// The configured TabWidth is used as tab width.
534 unsigned OriginalColumn = 0;
535
536 /// The length of following tokens until the next natural split point,
537 /// or the next token that can be broken.
538 unsigned UnbreakableTailLength = 0;
539
540 // FIXME: Come up with a 'cleaner' concept.
541 /// The binding strength of a token. This is a combined value of
542 /// operator precedence, parenthesis nesting, etc.
543 unsigned BindingStrength = 0;
545 /// The nesting level of this token, i.e. the number of surrounding (),
546 /// [], {} or <>.
547 unsigned NestingLevel = 0;
548
549 /// The indent level of this token. Copied from the surrounding line.
550 unsigned IndentLevel = 0;
552 /// Block + continuation indent level, applied by the WhitespaceManager to
553 /// this token.
554 mutable unsigned AppliedIndentLevel = 0;
555
556 /// Penalty for inserting a line break before this token.
557 unsigned SplitPenalty = 0;
558
559 /// If this is the first ObjC selector name in an ObjC method
560 /// definition or call, this contains the length of the longest name.
561 ///
562 /// This being set to 0 means that the selectors should not be colon-aligned,
563 /// e.g. because several of them are block-type.
564 unsigned LongestObjCSelectorName = 0;
565
566 /// If this is the first ObjC selector name in an ObjC method
567 /// definition or call, this contains the number of parts that the whole
568 /// selector consist of.
569 unsigned ObjCSelectorNameParts = 0;
571 /// The 0-based index of the parameter/argument. For ObjC it is set
572 /// for the selector name token.
573 /// For now calculated only for ObjC.
574 unsigned ParameterIndex = 0;
575
576 /// Stores the number of required fake parentheses and the
577 /// corresponding operator precedence.
578 ///
579 /// If multiple fake parentheses start at a token, this vector stores them in
580 /// reverse order, i.e. inner fake parenthesis first.
582 /// Insert this many fake ) after this token for correct indentation.
583 unsigned FakeRParens = 0;
585 /// If this is an operator (or "."/"->") in a sequence of operators
586 /// with the same precedence, contains the 0-based operator index.
587 unsigned OperatorIndex = 0;
588
589 /// If this is an operator (or "."/"->") in a sequence of operators
590 /// with the same precedence, points to the next operator.
591 FormatToken *NextOperator = nullptr;
592
593 /// If this is a bracket, this points to the matching one.
594 FormatToken *MatchingParen = nullptr;
595
596 /// The previous token in the unwrapped line.
597 FormatToken *Previous = nullptr;
598
599 /// The next token in the unwrapped line.
600 FormatToken *Next = nullptr;
601
602 /// The first token in set of column elements.
603 bool StartsColumn = false;
604
605 /// This notes the start of the line of an array initializer.
606 bool ArrayInitializerLineStart = false;
607
608 /// This starts an array initializer.
609 bool IsArrayInitializer = false;
610
611 /// Is optional and can be removed.
612 bool Optional = false;
613
614 /// Might be function declaration open/closing paren.
615 bool MightBeFunctionDeclParen = false;
616
617 /// Has "\n\f\n" or "\n\f\r\n" before TokenText.
618 bool HasFormFeedBefore = false;
619
620 /// Is the first token after a preprocessor line.
621 bool FirstAfterPPLine = false;
623 /// Number of optional braces to be inserted after this token:
624 /// -1: a single left brace
625 /// 0: no braces
626 /// >0: number of right braces
627 int8_t BraceCount = 0;
628
629 /// If this token starts a block, this contains all the unwrapped lines
630 /// in it.
632
633 // Contains all attributes related to how this token takes part
634 // in a configured macro expansion.
635 std::optional<MacroExpansion> MacroCtx;
636
637 /// When macro expansion introduces nodes with children, those are marked as
638 /// \c MacroParent.
639 /// FIXME: The formatting code currently hard-codes the assumption that
640 /// child nodes are introduced by blocks following an opening brace.
641 /// This is deeply baked into the code and disentangling this will require
642 /// signficant refactorings. \c MacroParent allows us to special-case the
643 /// cases in which we treat parents as block-openers for now.
644 bool MacroParent = false;
646 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
647 bool is(tok::ObjCKeywordKind Kind) const {
648 return Tok.getObjCKeywordID() == Kind;
649 }
650 bool is(TokenType TT) const { return getType() == TT; }
651 bool is(const IdentifierInfo *II) const {
652 return II && II == Tok.getIdentifierInfo();
653 }
654 bool is(tok::PPKeywordKind Kind) const {
655 return Tok.getIdentifierInfo() &&
656 Tok.getIdentifierInfo()->getPPKeywordID() == Kind;
657 }
658 bool is(BraceBlockKind BBK) const { return getBlockKind() == BBK; }
659 bool is(ParameterPackingKind PPK) const { return getPackingKind() == PPK; }
661 template <typename A, typename B> bool isOneOf(A K1, B K2) const {
662 return is(K1) || is(K2);
663 }
664 template <typename A, typename B, typename... Ts>
665 bool isOneOf(A K1, B K2, Ts... Ks) const {
666 return is(K1) || isOneOf(K2, Ks...);
667 }
668 template <typename T> bool isNot(T Kind) const { return !is(Kind); }
669 template <typename... Ts> bool isNoneOf(Ts... Ks) const {
670 return !isOneOf(Ks...);
672
673 bool isIf(bool AllowConstexprMacro = true) const {
674 return is(tok::kw_if) || endsSequence(tok::kw_constexpr, tok::kw_if) ||
675 (endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro);
676 }
677
678 bool isLoop(const FormatStyle &Style) const {
679 return isOneOf(tok::kw_for, tok::kw_while) ||
680 (Style.isJavaScript() && isNot(tok::l_paren) && Previous &&
681 Previous->is(tok::kw_for));
682 }
683
684 bool closesScopeAfterBlock() const {
685 if (getBlockKind() == BK_Block)
686 return true;
687 if (closesScope())
688 return Previous->closesScopeAfterBlock();
689 return false;
690 }
691
692 /// \c true if this token starts a sequence with the given tokens in order,
693 /// following the ``Next`` pointers, ignoring comments.
694 template <typename A, typename... Ts>
695 bool startsSequence(A K1, Ts... Tokens) const {
696 return startsSequenceInternal(K1, Tokens...);
697 }
698
699 /// \c true if this token ends a sequence with the given tokens in order,
700 /// following the ``Previous`` pointers, ignoring comments.
701 /// For example, given tokens [T1, T2, T3], the function returns true if
702 /// 3 tokens ending at this (ignoring comments) are [T3, T2, T1]. In other
703 /// words, the tokens passed to this function need to the reverse of the
704 /// order the tokens appear in code.
705 template <typename A, typename... Ts>
706 bool endsSequence(A K1, Ts... Tokens) const {
707 return endsSequenceInternal(K1, Tokens...);
708 }
709
710 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
711
712 bool isAttribute() const {
713 return isOneOf(tok::kw___attribute, tok::kw___declspec, TT_AttributeMacro);
714 }
715
716 bool isAccessSpecifierKeyword() const {
717 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private);
718 }
719
720 bool isAccessSpecifier(bool ColonRequired = true) const {
721 if (!isAccessSpecifierKeyword())
722 return false;
723 if (!ColonRequired)
724 return true;
725 const auto *NextNonComment = getNextNonComment();
726 return NextNonComment && NextNonComment->is(tok::colon);
727 }
728
729 bool canBePointerOrReferenceQualifier() const {
730 return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile,
731 tok::kw__Nonnull, tok::kw__Nullable,
732 tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64,
733 tok::kw___funcref) ||
734 isAttribute();
735 }
736
737 [[nodiscard]] bool isQtProperty() const;
738 [[nodiscard]] bool isTypeName(const LangOptions &LangOpts) const;
739 [[nodiscard]] bool isTypeOrIdentifier(const LangOptions &LangOpts) const;
740
741 bool isObjCAccessSpecifier() const {
742 return is(tok::at) && Next &&
743 Next->isOneOf(tok::objc_public, tok::objc_protected,
744 tok::objc_package, tok::objc_private);
745 }
746
747 bool isObjCLifetimeQualifier(const FormatStyle &Style) const {
748 if (Style.Language != FormatStyle::LK_ObjC || isNot(tok::identifier) ||
749 !TokenText.starts_with("__")) {
750 return false;
751 }
752 const auto Qualifier = TokenText.substr(2);
753 return Qualifier == "autoreleasing" || Qualifier == "strong" ||
754 Qualifier == "weak" || Qualifier == "unsafe_unretained";
755 }
756
757 /// Returns whether \p Tok is ([{ or an opening < of a template or in
758 /// protos.
759 bool opensScope() const {
760 if (is(TT_TemplateString) && TokenText.ends_with("${"))
761 return true;
762 if (is(TT_DictLiteral) && is(tok::less))
763 return true;
764 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
765 TT_TemplateOpener);
766 }
767 /// Returns whether \p Tok is )]} or a closing > of a template or in
768 /// protos.
769 bool closesScope() const {
770 if (is(TT_TemplateString) && TokenText.starts_with("}"))
771 return true;
772 if (is(TT_DictLiteral) && is(tok::greater))
773 return true;
774 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
775 TT_TemplateCloser);
776 }
777
778 /// Returns \c true if this is a "." or "->" accessing a member.
779 bool isMemberAccess() const {
780 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
781 isNoneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
782 TT_LambdaArrow, TT_LeadingJavaAnnotation);
783 }
784
785 bool isPointerOrReference() const {
786 return isOneOf(tok::star, tok::amp, tok::ampamp);
787 }
788
789 bool isPlacementOperator() const {
790 return isOneOf(tok::kw_new, tok::kw_delete);
791 }
792
793 bool isUnaryOperator() const {
794 switch (Tok.getKind()) {
795 case tok::plus:
796 case tok::plusplus:
797 case tok::minus:
798 case tok::minusminus:
799 case tok::exclaim:
800 case tok::tilde:
801 case tok::kw_sizeof:
802 case tok::kw_alignof:
803 return true;
804 default:
805 return false;
806 }
807 }
808
809 bool isBinaryOperator() const {
810 // Comma is a binary operator, but does not behave as such wrt. formatting.
811 return getPrecedence() > prec::Comma;
812 }
813
814 bool isTrailingComment() const {
815 return is(tok::comment) &&
816 (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
817 }
818
819 /// Returns \c true if this is a keyword that can be used
820 /// like a function call (e.g. sizeof, typeid, ...).
821 bool isFunctionLikeKeyword() const {
822 if (isAttribute())
823 return true;
824
825 return isOneOf(tok::kw_throw, tok::kw_typeid, tok::kw_return,
826 tok::kw_sizeof, tok::kw_alignof, tok::kw_alignas,
827 tok::kw_decltype, tok::kw_noexcept, tok::kw_static_assert,
828 tok::kw__Atomic,
829#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) tok::kw___##Trait,
830#include "clang/Basic/TransformTypeTraits.def"
831 tok::kw_requires);
832 }
833
834 /// Returns \c true if this is a string literal that's like a label,
835 /// e.g. ends with "=" or ":".
836 bool isLabelString() const {
837 if (isNot(tok::string_literal))
838 return false;
839 StringRef Content = TokenText;
840 if (Content.starts_with("\"") || Content.starts_with("'"))
841 Content = Content.drop_front(1);
842 if (Content.ends_with("\"") || Content.ends_with("'"))
843 Content = Content.drop_back(1);
844 Content = Content.trim();
845 return Content.size() > 1 &&
846 (Content.back() == ':' || Content.back() == '=');
847 }
848
849 /// Returns actual token start location without leading escaped
850 /// newlines and whitespace.
851 ///
852 /// This can be different to Tok.getLocation(), which includes leading escaped
853 /// newlines.
854 SourceLocation getStartOfNonWhitespace() const {
855 return WhitespaceRange.getEnd();
856 }
857
858 /// Returns \c true if the range of whitespace immediately preceding the \c
859 /// Token is not empty.
860 bool hasWhitespaceBefore() const {
861 return WhitespaceRange.getBegin() != WhitespaceRange.getEnd();
862 }
863
864 prec::Level getPrecedence() const {
867 return getBinOpPrecedence(Tok.getKind(), /*GreaterThanIsOperator=*/true,
868 /*CPlusPlus11=*/true);
869 }
870
871 template <typename T> [[nodiscard]] FormatToken *getPrevious(T A1) const {
873 while (Tok && Tok->isNot(A1))
874 Tok = Tok->Previous;
875 return Tok;
876 }
877
878 template <typename... Ts>
879 [[nodiscard]] FormatToken *getPreviousOneOf(Ts... Ks) const {
881 while (Tok && (Tok->isNot(Ks) && ...))
882 Tok = Tok->Previous;
883 return Tok;
885
886 /// Returns the previous token ignoring comments.
887 [[nodiscard]] FormatToken *getPreviousNonComment() const {
889 while (Tok && Tok->is(tok::comment))
890 Tok = Tok->Previous;
891 return Tok;
892 }
894 /// Returns the next token ignoring comments.
895 [[nodiscard]] FormatToken *getNextNonComment() const {
897 while (Tok && Tok->is(tok::comment))
898 Tok = Tok->Next;
899 return Tok;
900 }
901
902 /// Returns \c true if this token likely names an object-like macro.
903 ///
904 /// If \p AllowFollowingColonColon is \c true, a following \c :: does not
905 /// disqualify the token from being considered macro-like.
906 bool isPossibleMacro(bool AllowFollowingColonColon = false) const {
907 if (isNot(tok::identifier))
908 return false;
909
910 assert(!TokenText.empty());
911
912 // T, K, U, V likely could be template arguments.
913 if (TokenText.size() == 1)
914 return false;
915
916 // It's unlikely that qualified names are object-like macros.
917 const auto *Prev = getPreviousNonComment();
918 if (Prev && Prev->is(tok::coloncolon))
919 return false;
920 if (!AllowFollowingColonColon) {
921 const auto *Next = getNextNonComment();
922 if (Next && Next->is(tok::coloncolon))
923 return false;
924 }
926 return TokenText == TokenText.upper();
928
929 /// Returns \c true if this token ends a block indented initializer list.
930 [[nodiscard]] bool isBlockIndentedInitRBrace(const FormatStyle &Style) const;
931
932 /// Returns \c true if this tokens starts a block-type list, i.e. a
933 /// list that should be indented with a block indent.
934 [[nodiscard]] bool opensBlockOrBlockTypeList(const FormatStyle &Style) const;
935
936 /// Returns whether the token is the left square bracket of a C++
937 /// structured binding declaration.
938 bool isCppStructuredBinding(bool IsCpp) const {
939 if (!IsCpp || isNot(tok::l_square))
940 return false;
941 const FormatToken *T = this;
942 do {
943 T = T->getPreviousNonComment();
944 } while (T && T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
945 tok::ampamp));
946 return T && T->is(tok::kw_auto);
947 }
948
949 /// Same as opensBlockOrBlockTypeList, but for the closing token.
950 bool closesBlockOrBlockTypeList(const FormatStyle &Style) const {
951 if (is(TT_TemplateString) && closesScope())
952 return true;
953 return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style);
954 }
955
956 /// Return the actual namespace token, if this token starts a namespace
957 /// block.
959 const FormatToken *NamespaceTok = this;
960 if (is(tok::comment))
961 NamespaceTok = NamespaceTok->getNextNonComment();
962 // Detect "(inline|export)? namespace" in the beginning of a line.
963 if (NamespaceTok && NamespaceTok->isOneOf(tok::kw_inline, tok::kw_export))
964 NamespaceTok = NamespaceTok->getNextNonComment();
965 return NamespaceTok &&
966 NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro)
967 ? NamespaceTok
968 : nullptr;
969 }
970
971 void copyFrom(const FormatToken &Tok) { *this = Tok; }
972
973private:
974 // Only allow copying via the explicit copyFrom method.
975 FormatToken(const FormatToken &) = delete;
976 FormatToken &operator=(const FormatToken &) = default;
977
978 template <typename A, typename... Ts>
979 bool startsSequenceInternal(A K1, Ts... Tokens) const {
980 if (is(tok::comment) && Next)
981 return Next->startsSequenceInternal(K1, Tokens...);
982 return is(K1) && Next && Next->startsSequenceInternal(Tokens...);
983 }
984
985 template <typename A> bool startsSequenceInternal(A K1) const {
986 if (is(tok::comment) && Next)
987 return Next->startsSequenceInternal(K1);
988 return is(K1);
989 }
990
991 template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const {
992 if (is(tok::comment) && Previous)
993 return Previous->endsSequenceInternal(K1);
994 return is(K1);
996
997 template <typename A, typename... Ts>
998 bool endsSequenceInternal(A K1, Ts... Tokens) const {
999 if (is(tok::comment) && Previous)
1000 return Previous->endsSequenceInternal(K1, Tokens...);
1001 return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...);
1002 }
1003};
1004
1005class ContinuationIndenter;
1006struct LineState;
1007
1008class TokenRole {
1009public:
1010 TokenRole(const FormatStyle &Style) : Style(Style) {}
1011 virtual ~TokenRole();
1013 /// After the \c TokenAnnotator has finished annotating all the tokens,
1014 /// this function precomputes required information for formatting.
1015 virtual void precomputeFormattingInfos(const FormatToken *Token);
1016
1017 /// Apply the special formatting that the given role demands.
1018 ///
1019 /// Assumes that the token having this role is already formatted.
1020 ///
1021 /// Continues formatting from \p State leaving indentation to \p Indenter and
1022 /// returns the total penalty that this formatting incurs.
1023 virtual unsigned formatFromToken(LineState &State,
1024 ContinuationIndenter *Indenter,
1025 bool DryRun) {
1026 return 0;
1028
1029 /// Same as \c formatFromToken, but assumes that the first token has
1030 /// already been set thereby deciding on the first line break.
1031 virtual unsigned formatAfterToken(LineState &State,
1032 ContinuationIndenter *Indenter,
1033 bool DryRun) {
1034 return 0;
1036
1037 /// Notifies the \c Role that a comma was found.
1038 virtual void CommaFound(const FormatToken *Token) {}
1039
1040 virtual const FormatToken *lastComma() { return nullptr; }
1041
1042protected:
1043 const FormatStyle &Style;
1044};
1045
1046class CommaSeparatedList : public TokenRole {
1047public:
1048 CommaSeparatedList(const FormatStyle &Style)
1049 : TokenRole(Style), HasNestedBracedList(false) {}
1050
1053 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
1054 bool DryRun) override;
1055
1056 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
1057 bool DryRun) override;
1058
1059 /// Adds \p Token as the next comma to the \c CommaSeparated list.
1060 void CommaFound(const FormatToken *Token) override {
1061 Commas.push_back(Token);
1062 }
1063
1064 const FormatToken *lastComma() override {
1065 if (Commas.empty())
1066 return nullptr;
1067 return Commas.back();
1069
1070private:
1071 /// A struct that holds information on how to format a given list with
1072 /// a specific number of columns.
1073 struct ColumnFormat {
1074 /// The number of columns to use.
1075 unsigned Columns;
1076
1077 /// The total width in characters.
1078 unsigned TotalWidth;
1079
1080 /// The number of lines required for this format.
1081 unsigned LineCount;
1082
1083 /// The size of each column in characters.
1085 };
1086
1087 /// Calculate which \c ColumnFormat fits best into
1088 /// \p RemainingCharacters.
1089 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
1090
1091 /// The ordered \c FormatTokens making up the commas of this list.
1094 /// The length of each of the list's items in characters including the
1095 /// trailing comma.
1096 SmallVector<unsigned, 8> ItemLengths;
1097
1098 /// Precomputed formats that can be used for this list.
1100
1101 bool HasNestedBracedList;
1102};
1104/// Encapsulates keywords that are context sensitive or for languages not
1105/// properly supported by Clang's lexer.
1106struct AdditionalKeywords {
1107 AdditionalKeywords(IdentifierTable &IdentTable) {
1108 kw_final = &IdentTable.get("final");
1109 kw_override = &IdentTable.get("override");
1110 kw_in = &IdentTable.get("in");
1111 kw_of = &IdentTable.get("of");
1112 kw_CF_CLOSED_ENUM = &IdentTable.get("CF_CLOSED_ENUM");
1113 kw_CF_ENUM = &IdentTable.get("CF_ENUM");
1114 kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
1115 kw_NS_CLOSED_ENUM = &IdentTable.get("NS_CLOSED_ENUM");
1116 kw_NS_ENUM = &IdentTable.get("NS_ENUM");
1117 kw_NS_ERROR_ENUM = &IdentTable.get("NS_ERROR_ENUM");
1118 kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
1119
1120 kw_as = &IdentTable.get("as");
1121 kw_async = &IdentTable.get("async");
1122 kw_await = &IdentTable.get("await");
1123 kw_declare = &IdentTable.get("declare");
1124 kw_finally = &IdentTable.get("finally");
1125 kw_from = &IdentTable.get("from");
1126 kw_function = &IdentTable.get("function");
1127 kw_get = &IdentTable.get("get");
1128 kw_import = &IdentTable.get("import");
1129 kw_infer = &IdentTable.get("infer");
1130 kw_is = &IdentTable.get("is");
1131 kw_let = &IdentTable.get("let");
1132 kw_module = &IdentTable.get("module");
1133 kw_readonly = &IdentTable.get("readonly");
1134 kw_set = &IdentTable.get("set");
1135 kw_type = &IdentTable.get("type");
1136 kw_typeof = &IdentTable.get("typeof");
1137 kw_var = &IdentTable.get("var");
1138 kw_yield = &IdentTable.get("yield");
1139
1140 kw_abstract = &IdentTable.get("abstract");
1141 kw_assert = &IdentTable.get("assert");
1142 kw_extends = &IdentTable.get("extends");
1143 kw_implements = &IdentTable.get("implements");
1144 kw_instanceof = &IdentTable.get("instanceof");
1145 kw_interface = &IdentTable.get("interface");
1146 kw_native = &IdentTable.get("native");
1147 kw_package = &IdentTable.get("package");
1148 kw_record = &IdentTable.get("record");
1149 kw_synchronized = &IdentTable.get("synchronized");
1150 kw_throws = &IdentTable.get("throws");
1151 kw___except = &IdentTable.get("__except");
1152 kw___has_include = &IdentTable.get("__has_include");
1153 kw___has_include_next = &IdentTable.get("__has_include_next");
1154
1155 kw_mark = &IdentTable.get("mark");
1156 kw_region = &IdentTable.get("region");
1157
1158 kw_extend = &IdentTable.get("extend");
1159 kw_option = &IdentTable.get("option");
1160 kw_optional = &IdentTable.get("optional");
1161 kw_repeated = &IdentTable.get("repeated");
1162 kw_required = &IdentTable.get("required");
1163 kw_returns = &IdentTable.get("returns");
1164
1165 kw_signals = &IdentTable.get("signals");
1166 kw_qsignals = &IdentTable.get("Q_SIGNALS");
1167 kw_slots = &IdentTable.get("slots");
1168 kw_qslots = &IdentTable.get("Q_SLOTS");
1170 // For internal clang-format use.
1171 kw_internal_ident_after_define =
1172 &IdentTable.get("__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__");
1173
1174 // C# keywords
1175 kw_dollar = &IdentTable.get("dollar");
1176 kw_base = &IdentTable.get("base");
1177 kw_byte = &IdentTable.get("byte");
1178 kw_checked = &IdentTable.get("checked");
1179 kw_decimal = &IdentTable.get("decimal");
1180 kw_delegate = &IdentTable.get("delegate");
1181 kw_event = &IdentTable.get("event");
1182 kw_fixed = &IdentTable.get("fixed");
1183 kw_foreach = &IdentTable.get("foreach");
1184 kw_init = &IdentTable.get("init");
1185 kw_implicit = &IdentTable.get("implicit");
1186 kw_internal = &IdentTable.get("internal");
1187 kw_lock = &IdentTable.get("lock");
1188 kw_null = &IdentTable.get("null");
1189 kw_object = &IdentTable.get("object");
1190 kw_out = &IdentTable.get("out");
1191 kw_params = &IdentTable.get("params");
1192 kw_ref = &IdentTable.get("ref");
1193 kw_string = &IdentTable.get("string");
1194 kw_stackalloc = &IdentTable.get("stackalloc");
1195 kw_sbyte = &IdentTable.get("sbyte");
1196 kw_sealed = &IdentTable.get("sealed");
1197 kw_uint = &IdentTable.get("uint");
1198 kw_ulong = &IdentTable.get("ulong");
1199 kw_unchecked = &IdentTable.get("unchecked");
1200 kw_unsafe = &IdentTable.get("unsafe");
1201 kw_ushort = &IdentTable.get("ushort");
1202 kw_when = &IdentTable.get("when");
1203 kw_where = &IdentTable.get("where");
1204
1205 // Verilog keywords
1206 kw_always = &IdentTable.get("always");
1207 kw_always_comb = &IdentTable.get("always_comb");
1208 kw_always_ff = &IdentTable.get("always_ff");
1209 kw_always_latch = &IdentTable.get("always_latch");
1210 kw_assign = &IdentTable.get("assign");
1211 kw_assume = &IdentTable.get("assume");
1212 kw_automatic = &IdentTable.get("automatic");
1213 kw_before = &IdentTable.get("before");
1214 kw_begin = &IdentTable.get("begin");
1215 kw_begin_keywords = &IdentTable.get("begin_keywords");
1216 kw_bins = &IdentTable.get("bins");
1217 kw_binsof = &IdentTable.get("binsof");
1218 kw_casex = &IdentTable.get("casex");
1219 kw_casez = &IdentTable.get("casez");
1220 kw_celldefine = &IdentTable.get("celldefine");
1221 kw_checker = &IdentTable.get("checker");
1222 kw_clocking = &IdentTable.get("clocking");
1223 kw_constraint = &IdentTable.get("constraint");
1224 kw_context = &IdentTable.get("context");
1225 kw_cover = &IdentTable.get("cover");
1226 kw_covergroup = &IdentTable.get("covergroup");
1227 kw_coverpoint = &IdentTable.get("coverpoint");
1228 kw_data_block = &IdentTable.get("data_block");
1229 kw_data_decrypt_key = &IdentTable.get("data_decrypt_key");
1230 kw_data_public_key = &IdentTable.get("data_public_key");
1231 kw_default_decay_time = &IdentTable.get("default_decay_time");
1232 kw_default_nettype = &IdentTable.get("default_nettype");
1233 kw_default_trireg_strength = &IdentTable.get("default_trireg_strength");
1234 kw_delay_mode_distributed = &IdentTable.get("delay_mode_distributed");
1235 kw_delay_mode_path = &IdentTable.get("delay_mode_path");
1236 kw_delay_mode_unit = &IdentTable.get("delay_mode_unit");
1237 kw_delay_mode_zero = &IdentTable.get("delay_mode_zero");
1238 kw_digest_block = &IdentTable.get("digest_block");
1239 kw_digest_decrypt_key = &IdentTable.get("digest_decrypt_key");
1240 kw_digest_public_key = &IdentTable.get("digest_public_key");
1241 kw_disable = &IdentTable.get("disable");
1242 kw_dist = &IdentTable.get("dist");
1243 kw_edge = &IdentTable.get("edge");
1244 kw_elsif = &IdentTable.get("elsif");
1245 kw_end = &IdentTable.get("end");
1246 kw_end_keywords = &IdentTable.get("end_keywords");
1247 kw_endcase = &IdentTable.get("endcase");
1248 kw_endcelldefine = &IdentTable.get("endcelldefine");
1249 kw_endchecker = &IdentTable.get("endchecker");
1250 kw_endclass = &IdentTable.get("endclass");
1251 kw_endclocking = &IdentTable.get("endclocking");
1252 kw_endfunction = &IdentTable.get("endfunction");
1253 kw_endgenerate = &IdentTable.get("endgenerate");
1254 kw_endgroup = &IdentTable.get("endgroup");
1255 kw_endinterface = &IdentTable.get("endinterface");
1256 kw_endmodule = &IdentTable.get("endmodule");
1257 kw_endpackage = &IdentTable.get("endpackage");
1258 kw_endprimitive = &IdentTable.get("endprimitive");
1259 kw_endprogram = &IdentTable.get("endprogram");
1260 kw_endproperty = &IdentTable.get("endproperty");
1261 kw_endsequence = &IdentTable.get("endsequence");
1262 kw_endspecify = &IdentTable.get("endspecify");
1263 kw_endtable = &IdentTable.get("endtable");
1264 kw_endtask = &IdentTable.get("endtask");
1265 kw_forever = &IdentTable.get("forever");
1266 kw_fork = &IdentTable.get("fork");
1267 kw_generate = &IdentTable.get("generate");
1268 kw_highz0 = &IdentTable.get("highz0");
1269 kw_highz1 = &IdentTable.get("highz1");
1270 kw_iff = &IdentTable.get("iff");
1271 kw_ifnone = &IdentTable.get("ifnone");
1272 kw_ignore_bins = &IdentTable.get("ignore_bins");
1273 kw_illegal_bins = &IdentTable.get("illegal_bins");
1274 kw_initial = &IdentTable.get("initial");
1275 kw_inout = &IdentTable.get("inout");
1276 kw_input = &IdentTable.get("input");
1277 kw_inside = &IdentTable.get("inside");
1278 kw_interconnect = &IdentTable.get("interconnect");
1279 kw_intersect = &IdentTable.get("intersect");
1280 kw_join = &IdentTable.get("join");
1281 kw_join_any = &IdentTable.get("join_any");
1282 kw_join_none = &IdentTable.get("join_none");
1283 kw_key_block = &IdentTable.get("key_block");
1284 kw_key_public_key = &IdentTable.get("key_public_key");
1285 kw_large = &IdentTable.get("large");
1286 kw_local = &IdentTable.get("local");
1287 kw_localparam = &IdentTable.get("localparam");
1288 kw_macromodule = &IdentTable.get("macromodule");
1289 kw_matches = &IdentTable.get("matches");
1290 kw_medium = &IdentTable.get("medium");
1291 kw_negedge = &IdentTable.get("negedge");
1292 kw_nounconnected_drive = &IdentTable.get("nounconnected_drive");
1293 kw_output = &IdentTable.get("output");
1294 kw_packed = &IdentTable.get("packed");
1295 kw_parameter = &IdentTable.get("parameter");
1296 kw_posedge = &IdentTable.get("posedge");
1297 kw_primitive = &IdentTable.get("primitive");
1298 kw_priority = &IdentTable.get("priority");
1299 kw_program = &IdentTable.get("program");
1300 kw_property = &IdentTable.get("property");
1301 kw_protect = &IdentTable.get("protect");
1302 kw_pull0 = &IdentTable.get("pull0");
1303 kw_pull1 = &IdentTable.get("pull1");
1304 kw_pure = &IdentTable.get("pure");
1305 kw_rand = &IdentTable.get("rand");
1306 kw_randc = &IdentTable.get("randc");
1307 kw_randcase = &IdentTable.get("randcase");
1308 kw_randsequence = &IdentTable.get("randsequence");
1309 kw_repeat = &IdentTable.get("repeat");
1310 kw_resetall = &IdentTable.get("resetall");
1311 kw_sample = &IdentTable.get("sample");
1312 kw_scalared = &IdentTable.get("scalared");
1313 kw_sequence = &IdentTable.get("sequence");
1314 kw_small = &IdentTable.get("small");
1315 kw_soft = &IdentTable.get("soft");
1316 kw_solve = &IdentTable.get("solve");
1317 kw_specify = &IdentTable.get("specify");
1318 kw_specparam = &IdentTable.get("specparam");
1319 kw_strong0 = &IdentTable.get("strong0");
1320 kw_strong1 = &IdentTable.get("strong1");
1321 kw_supply0 = &IdentTable.get("supply0");
1322 kw_supply1 = &IdentTable.get("supply1");
1323 kw_table = &IdentTable.get("table");
1324 kw_tagged = &IdentTable.get("tagged");
1325 kw_task = &IdentTable.get("task");
1326 kw_timescale = &IdentTable.get("timescale");
1327 kw_tri = &IdentTable.get("tri");
1328 kw_tri0 = &IdentTable.get("tri0");
1329 kw_tri1 = &IdentTable.get("tri1");
1330 kw_triand = &IdentTable.get("triand");
1331 kw_trior = &IdentTable.get("trior");
1332 kw_trireg = &IdentTable.get("trireg");
1333 kw_unconnected_drive = &IdentTable.get("unconnected_drive");
1334 kw_undefineall = &IdentTable.get("undefineall");
1335 kw_unique = &IdentTable.get("unique");
1336 kw_unique0 = &IdentTable.get("unique0");
1337 kw_uwire = &IdentTable.get("uwire");
1338 kw_vectored = &IdentTable.get("vectored");
1339 kw_wait = &IdentTable.get("wait");
1340 kw_wand = &IdentTable.get("wand");
1341 kw_weak0 = &IdentTable.get("weak0");
1342 kw_weak1 = &IdentTable.get("weak1");
1343 kw_wildcard = &IdentTable.get("wildcard");
1344 kw_wire = &IdentTable.get("wire");
1345 kw_with = &IdentTable.get("with");
1346 kw_wor = &IdentTable.get("wor");
1347
1348 // Symbols that are treated as keywords.
1349 kw_verilogHash = &IdentTable.get("#");
1350 kw_verilogHashHash = &IdentTable.get("##");
1351 kw_apostrophe = &IdentTable.get("\'");
1352
1353 // TableGen keywords.
1354 kw_bit = &IdentTable.get("bit");
1355 kw_bits = &IdentTable.get("bits");
1356 kw_code = &IdentTable.get("code");
1357 kw_dag = &IdentTable.get("dag");
1358 kw_def = &IdentTable.get("def");
1359 kw_defm = &IdentTable.get("defm");
1360 kw_defset = &IdentTable.get("defset");
1361 kw_defvar = &IdentTable.get("defvar");
1362 kw_dump = &IdentTable.get("dump");
1363 kw_include = &IdentTable.get("include");
1364 kw_list = &IdentTable.get("list");
1365 kw_multiclass = &IdentTable.get("multiclass");
1366 kw_then = &IdentTable.get("then");
1367
1368 // Keep this at the end of the constructor to make sure everything here is
1369 // already initialized.
1370 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
1371 {kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
1372 kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_override,
1373 kw_readonly, kw_set, kw_type, kw_typeof, kw_var, kw_yield,
1374 // Keywords from the Java section.
1375 kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
1376
1377 CSharpExtraKeywords = JsExtraKeywords;
1378 CSharpExtraKeywords.insert(
1379 {kw_base, kw_byte, kw_checked, kw_decimal, kw_delegate,
1380 kw_event, kw_fixed, kw_foreach, kw_implicit, kw_in,
1381 kw_init, kw_internal, kw_lock, kw_null, kw_object,
1382 kw_out, kw_params, kw_ref, kw_string, kw_stackalloc,
1383 kw_sbyte, kw_sealed, kw_uint, kw_ulong, kw_unchecked,
1384 kw_unsafe, kw_ushort, kw_when, kw_where});
1385
1386 // Some keywords are not included here because they don't need special
1387 // treatment like `showcancelled` or they should be treated as identifiers
1388 // like `int` and `logic`.
1389 VerilogExtraKeywords =
1390 std::unordered_set<IdentifierInfo *>{kw_always,
1391 kw_always_comb,
1392 kw_always_ff,
1393 kw_always_latch,
1394 kw_assert,
1395 kw_assign,
1396 kw_assume,
1397 kw_automatic,
1398 kw_before,
1399 kw_begin,
1400 kw_bins,
1401 kw_binsof,
1402 kw_casex,
1403 kw_casez,
1404 kw_celldefine,
1405 kw_checker,
1406 kw_clocking,
1407 kw_constraint,
1408 kw_context,
1409 kw_cover,
1410 kw_covergroup,
1411 kw_coverpoint,
1412 kw_disable,
1413 kw_dist,
1414 kw_edge,
1415 kw_end,
1416 kw_endcase,
1417 kw_endchecker,
1418 kw_endclass,
1419 kw_endclocking,
1420 kw_endfunction,
1421 kw_endgenerate,
1422 kw_endgroup,
1423 kw_endinterface,
1424 kw_endmodule,
1425 kw_endpackage,
1426 kw_endprimitive,
1427 kw_endprogram,
1428 kw_endproperty,
1429 kw_endsequence,
1430 kw_endspecify,
1431 kw_endtable,
1432 kw_endtask,
1433 kw_extends,
1434 kw_final,
1435 kw_foreach,
1436 kw_forever,
1437 kw_fork,
1438 kw_function,
1439 kw_generate,
1440 kw_highz0,
1441 kw_highz1,
1442 kw_iff,
1443 kw_ifnone,
1444 kw_ignore_bins,
1445 kw_illegal_bins,
1446 kw_implements,
1447 kw_import,
1448 kw_initial,
1449 kw_inout,
1450 kw_input,
1451 kw_inside,
1452 kw_interconnect,
1453 kw_interface,
1454 kw_intersect,
1455 kw_join,
1456 kw_join_any,
1457 kw_join_none,
1458 kw_large,
1459 kw_let,
1460 kw_local,
1461 kw_localparam,
1462 kw_macromodule,
1463 kw_matches,
1464 kw_medium,
1465 kw_module,
1466 kw_negedge,
1467 kw_output,
1468 kw_package,
1469 kw_packed,
1470 kw_parameter,
1471 kw_posedge,
1472 kw_primitive,
1473 kw_priority,
1474 kw_program,
1475 kw_property,
1476 kw_pull0,
1477 kw_pull1,
1478 kw_pure,
1479 kw_rand,
1480 kw_randc,
1481 kw_randcase,
1482 kw_randsequence,
1483 kw_ref,
1484 kw_repeat,
1485 kw_sample,
1486 kw_scalared,
1487 kw_sequence,
1488 kw_small,
1489 kw_soft,
1490 kw_solve,
1491 kw_specify,
1492 kw_specparam,
1493 kw_strong0,
1494 kw_strong1,
1495 kw_supply0,
1496 kw_supply1,
1497 kw_table,
1498 kw_tagged,
1499 kw_task,
1500 kw_tri,
1501 kw_tri0,
1502 kw_tri1,
1503 kw_triand,
1504 kw_trior,
1505 kw_trireg,
1506 kw_unique,
1507 kw_unique0,
1508 kw_uwire,
1509 kw_var,
1510 kw_vectored,
1511 kw_wait,
1512 kw_wand,
1513 kw_weak0,
1514 kw_weak1,
1515 kw_wildcard,
1522 TableGenExtraKeywords = std::unordered_set<IdentifierInfo *>{
1528 // Context sensitive keywords.
1544 // JavaScript keywords.
1564
1565 // Java keywords.
1577
1578 // Pragma keywords.
1582 // Proto keywords.
1590 // QT keywords.
1596 // For internal use by clang-format.
1599 // C# keywords
1619
1633 // Verilog keywords
1776 // Workaround for hashes and backticks in Verilog.
1780 // Symbols in Verilog that don't exist in C++.
1783 // TableGen keywords
1798 /// Returns \c true if \p Tok is a keyword or an identifier.
1799 bool isWordLike(const FormatToken &Tok, bool IsVerilog = true) const {
1800 // getIdentifierinfo returns non-null for keywords as well as identifiers.
1801 return Tok.Tok.getIdentifierInfo() &&
1802 (!IsVerilog || !isVerilogKeywordSymbol(Tok));
1803 }
1804
1805 /// Returns \c true if \p Tok is a true JavaScript identifier, returns
1806 /// \c false if it is a keyword or a pseudo keyword.
1807 /// If \c AcceptIdentifierName is true, returns true not only for keywords,
1808 // but also for IdentifierName tokens (aka pseudo-keywords), such as
1809 // ``yield``.
1810 bool isJavaScriptIdentifier(const FormatToken &Tok,
1811 bool AcceptIdentifierName = true) const {
1812 // Based on the list of JavaScript & TypeScript keywords here:
1813 // https://github.com/microsoft/TypeScript/blob/main/src/compiler/scanner.ts#L74
1814 if (Tok.isAccessSpecifierKeyword())
1815 return false;
1816 switch (Tok.Tok.getKind()) {
1817 case tok::kw_break:
1818 case tok::kw_case:
1819 case tok::kw_catch:
1820 case tok::kw_class:
1821 case tok::kw_continue:
1822 case tok::kw_const:
1823 case tok::kw_default:
1824 case tok::kw_delete:
1825 case tok::kw_do:
1826 case tok::kw_else:
1827 case tok::kw_enum:
1828 case tok::kw_export:
1829 case tok::kw_false:
1830 case tok::kw_for:
1831 case tok::kw_if:
1832 case tok::kw_import:
1833 case tok::kw_module:
1834 case tok::kw_new:
1835 case tok::kw_return:
1836 case tok::kw_static:
1837 case tok::kw_switch:
1838 case tok::kw_this:
1839 case tok::kw_throw:
1840 case tok::kw_true:
1841 case tok::kw_try:
1842 case tok::kw_typeof:
1843 case tok::kw_void:
1844 case tok::kw_while:
1845 // These are JS keywords that are lexed by LLVM/clang as keywords.
1846 return false;
1847 case tok::identifier: {
1848 // For identifiers, make sure they are true identifiers, excluding the
1849 // JavaScript pseudo-keywords (not lexed by LLVM/clang as keywords).
1850 bool IsPseudoKeyword =
1851 JsExtraKeywords.find(Tok.Tok.getIdentifierInfo()) !=
1852 JsExtraKeywords.end();
1853 return AcceptIdentifierName || !IsPseudoKeyword;
1854 }
1855 default:
1856 // Other keywords are handled in the switch below, to avoid problems due
1857 // to duplicate case labels when using the #include trick.
1858 break;
1859 }
1860
1861 switch (Tok.Tok.getKind()) {
1862 // Handle C++ keywords not included above: these are all JS identifiers.
1863#define KEYWORD(X, Y) case tok::kw_##X:
1864#include "clang/Basic/TokenKinds.def"
1865 // #undef KEYWORD is not needed -- it's #undef-ed at the end of
1866 // TokenKinds.def
1867 return true;
1868 default:
1869 // All other tokens (punctuation etc) are not JS identifiers.
1870 return false;
1871 }
1872 }
1873
1874 /// Returns \c true if \p Tok is a C# keyword, returns \c false if it is
1875 /// anything else.
1876 bool isCSharpKeyword(const FormatToken &Tok) const {
1877 if (Tok.isAccessSpecifierKeyword())
1878 return true;
1879 switch (Tok.Tok.getKind()) {
1880 case tok::kw_bool:
1881 case tok::kw_break:
1882 case tok::kw_case:
1883 case tok::kw_catch:
1884 case tok::kw_char:
1885 case tok::kw_class:
1886 case tok::kw_const:
1887 case tok::kw_continue:
1888 case tok::kw_default:
1889 case tok::kw_do:
1890 case tok::kw_double:
1891 case tok::kw_else:
1892 case tok::kw_enum:
1893 case tok::kw_explicit:
1894 case tok::kw_extern:
1895 case tok::kw_false:
1896 case tok::kw_float:
1897 case tok::kw_for:
1898 case tok::kw_goto:
1899 case tok::kw_if:
1900 case tok::kw_int:
1901 case tok::kw_long:
1902 case tok::kw_namespace:
1903 case tok::kw_new:
1904 case tok::kw_operator:
1905 case tok::kw_return:
1906 case tok::kw_short:
1907 case tok::kw_sizeof:
1908 case tok::kw_static:
1909 case tok::kw_struct:
1910 case tok::kw_switch:
1911 case tok::kw_this:
1912 case tok::kw_throw:
1913 case tok::kw_true:
1914 case tok::kw_try:
1915 case tok::kw_typeof:
1916 case tok::kw_using:
1917 case tok::kw_virtual:
1918 case tok::kw_void:
1919 case tok::kw_volatile:
1920 case tok::kw_while:
1921 return true;
1922 default:
1923 return Tok.is(tok::identifier) &&
1924 CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
1925 CSharpExtraKeywords.end();
1926 }
1927 }
1928
1929 bool isVerilogKeywordSymbol(const FormatToken &Tok) const {
1930 return Tok.isOneOf(kw_verilogHash, kw_verilogHashHash, kw_apostrophe);
1931 }
1932
1933 bool isVerilogWordOperator(const FormatToken &Tok) const {
1934 return Tok.isOneOf(kw_before, kw_intersect, kw_dist, kw_iff, kw_inside,
1935 kw_with);
1936 }
1937
1938 bool isVerilogIdentifier(const FormatToken &Tok) const {
1939 switch (Tok.Tok.getKind()) {
1940 case tok::kw_case:
1941 case tok::kw_class:
1942 case tok::kw_const:
1943 case tok::kw_continue:
1944 case tok::kw_default:
1945 case tok::kw_do:
1946 case tok::kw_else:
1947 case tok::kw_enum:
1948 case tok::kw_export:
1949 case tok::kw_extern:
1950 case tok::kw_for:
1951 case tok::kw_if:
1952 case tok::kw_import:
1953 case tok::kw_restrict:
1954 case tok::kw_signed:
1955 case tok::kw_static:
1956 case tok::kw_struct:
1957 case tok::kw_typedef:
1958 case tok::kw_union:
1959 case tok::kw_unsigned:
1960 case tok::kw_virtual:
1961 case tok::kw_while:
1962 return false;
1963 case tok::identifier:
1964 return isWordLike(Tok) &&
1965 VerilogExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
1966 VerilogExtraKeywords.end();
1967 default:
1968 // getIdentifierInfo returns non-null for both identifiers and keywords.
1969 return Tok.Tok.getIdentifierInfo();
1970 }
1971 }
1972
1973 /// Returns whether \p Tok is a Verilog preprocessor directive. This is
1974 /// needed because macro expansions start with a backtick as well and they
1975 /// need to be treated differently.
1976 bool isVerilogPPDirective(const FormatToken &Tok) const {
1977 auto Info = Tok.Tok.getIdentifierInfo();
1978 if (!Info)
1979 return false;
1980 switch (Info->getPPKeywordID()) {
1981 case tok::pp_define:
1982 case tok::pp_else:
1983 case tok::pp_endif:
1984 case tok::pp_ifdef:
1985 case tok::pp_ifndef:
1986 case tok::pp_include:
1987 case tok::pp_line:
1988 case tok::pp_pragma:
1989 case tok::pp_undef:
1999 }
2000 }
2002 /// Returns whether \p Tok is a Verilog keyword that opens a block.
2003 bool isVerilogBegin(const FormatToken &Tok) const {
2004 // `table` is not included since it needs to be treated specially.
2005 if (Tok.isOneOf(kw_begin, kw_generate, kw_specify))
2006 return true;
2007 if (Tok.isNot(kw_fork))
2008 return false;
2009 const auto *Prev = Tok.getPreviousNonComment();
2010 return !(Prev && Prev->isOneOf(kw_disable, kw_wait));
2011 }
2012
2013 /// Returns whether \p Tok is a Verilog keyword that closes a block.
2014 bool isVerilogEnd(const FormatToken &Tok) const {
2015 return !Tok.endsSequence(kw_join, kw_rand) &&
2016 Tok.isOneOf(TT_MacroBlockEnd, kw_end, kw_endcase, kw_endclass,
2023 }
2024
2025 /// Returns whether \p Tok is a Verilog keyword that opens a module, etc.
2026 bool isVerilogHierarchy(const FormatToken &Tok) const {
2027 if (Tok.endsSequence(kw_function, kw_with))
2028 return false;
2029 if (Tok.is(kw_property)) {
2030 const FormatToken *Prev = Tok.getPreviousNonComment();
2031 return !(Prev &&
2032 Prev->isOneOf(tok::kw_restrict, kw_assert, kw_assume, kw_cover));
2033 }
2034 return Tok.isOneOf(tok::kw_case, tok::kw_class, kw_function, kw_module,
2035 kw_interface, kw_package, kw_casex, kw_casez, kw_checker,
2036 kw_clocking, kw_covergroup, kw_macromodule, kw_primitive,
2037 kw_program, kw_property, kw_randcase, kw_randsequence,
2038 kw_task);
2040
2041 bool isVerilogEndOfLabel(const FormatToken &Tok) const {
2042 const FormatToken *Next = Tok.getNextNonComment();
2043 // In Verilog the colon in a default label is optional.
2044 return Tok.is(TT_CaseLabelColon) ||
2045 (Tok.is(tok::kw_default) &&
2046 !(Next && Next->isOneOf(tok::colon, tok::semi, kw_clocking, kw_iff,
2048 }
2049
2050 /// Returns whether \p Tok is a Verilog keyword that starts a
2051 /// structured procedure like 'always'.
2052 bool isVerilogStructuredProcedure(const FormatToken &Tok) const {
2053 return Tok.isOneOf(kw_always, kw_always_comb, kw_always_ff, kw_always_latch,
2054 kw_final, kw_forever, kw_initial);
2055 }
2056
2057 bool isVerilogQualifier(const FormatToken &Tok) const {
2058 switch (Tok.Tok.getKind()) {
2059 case tok::kw_extern:
2060 case tok::kw_signed:
2061 case tok::kw_static:
2062 case tok::kw_unsigned:
2063 case tok::kw_virtual:
2064 return true;
2073 default:
2074 return false;
2075 }
2076 }
2077
2078 bool isTableGenDefinition(const FormatToken &Tok) const {
2079 return Tok.isOneOf(kw_def, kw_defm, kw_defset, kw_defvar, kw_multiclass,
2080 kw_let, tok::kw_class);
2081 }
2082
2083 bool isTableGenKeyword(const FormatToken &Tok) const {
2084 switch (Tok.Tok.getKind()) {
2085 case tok::kw_class:
2086 case tok::kw_else:
2087 case tok::kw_false:
2088 case tok::kw_if:
2089 case tok::kw_int:
2090 case tok::kw_true:
2091 return true;
2092 default:
2093 return Tok.is(tok::identifier) &&
2094 TableGenExtraKeywords.find(Tok.Tok.getIdentifierInfo()) !=
2095 TableGenExtraKeywords.end();
2096 }
2097 }
2098
2099private:
2100 /// The JavaScript keywords beyond the C++ keyword set.
2101 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
2102
2103 /// The C# keywords beyond the C++ keyword set.
2104 std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
2105
2106 /// The Verilog keywords beyond the C++ keyword set.
2107 std::unordered_set<IdentifierInfo *> VerilogExtraKeywords;
2108
2109 /// The TableGen keywords beyond the C++ keyword set.
2110 std::unordered_set<IdentifierInfo *> TableGenExtraKeywords;
2111};
2112
2113inline bool isLineComment(const FormatToken &FormatTok) {
2114 return FormatTok.is(tok::comment) && !FormatTok.TokenText.starts_with("/*");
2115}
2116
2117// Checks if \p FormatTok is a line comment that continues the line comment
2118// \p Previous. The original column of \p MinColumnToken is used to determine
2119// whether \p FormatTok is indented enough to the right to continue \p Previous.
2120inline bool continuesLineComment(const FormatToken &FormatTok,
2121 const FormatToken *Previous,
2122 const FormatToken *MinColumnToken) {
2123 if (!Previous || !MinColumnToken)
2124 return false;
2125 unsigned MinContinueColumn =
2126 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
2127 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
2129 FormatTok.OriginalColumn >= MinContinueColumn;
2130}
2131
2132// Returns \c true if \c Current starts a new parameter.
2133bool startsNextParameter(const FormatToken &Current, const FormatStyle &Style);
2134
2135// Returns \c true if \c Tok is a function/storage specifier that may appear
2136// before a function return type (e.g. ``static``, ``inline``, ``constexpr``).
2137inline bool isReturnTypePrefixSpecifier(const FormatToken &Tok) {
2138 return Tok.isOneOf(tok::kw_static, tok::kw_extern, tok::kw_inline,
2139 tok::kw_virtual, tok::kw_constexpr, tok::kw_consteval,
2140 tok::kw_friend, tok::kw_export, tok::kw__Noreturn,
2141 tok::kw___forceinline);
2142}
2143
2144} // namespace format
2145} // namespace clang
2146
2147#endif
#define LIST_TOKEN_TYPES
Definition FormatToken.h:27
prec::Level ForcedPrecedence
Used to set an operator precedence explicitly.
StringRef TokenText
The raw text of the token.
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, _)
CommaSeparatedList(const FormatStyle &Style)
void precomputeFormattingInfos(const FormatToken *Token) override
After the TokenAnnotator has finished annotating all the tokens, this function precomputes required i...
One of these records is kept for each identifier that is lexed.
Encodes a location in the source.
A trivial tuple used to represent a source range.
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:1875
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)
FormatToken * getPreviousNonComment() const
Returns the previous token ignoring comments.
FormatToken * getPreviousOneOf(Ts... Ks) const
struct clang::format::FormatToken getPrevious(T A1) const
bool isPossibleMacro(bool AllowFollowingColonColon=false) const
Returns true if this token likely names an object-like macro.
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:93
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)
bool isReturnTypePrefixSpecifier(const FormatToken &Tok)
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_protect
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_digest_decrypt_key
IdentifierInfo * kw_multiclass
IdentifierInfo * kw_internal
IdentifierInfo * kw_soft
IdentifierInfo * kw_returns
IdentifierInfo * kw_supply0
IdentifierInfo * kw_when
IdentifierInfo * kw_large
IdentifierInfo * kw_endclocking
IdentifierInfo * kw_endfunction
IdentifierInfo * kw_endprimitive
IdentifierInfo * kw_implicit
IdentifierInfo * kw_where
IdentifierInfo * kw_randcase
IdentifierInfo * kw_rand
IdentifierInfo * kw_NS_OPTIONS
IdentifierInfo * kw_triand
IdentifierInfo * kw_inside
IdentifierInfo * kw_defm
IdentifierInfo * kw_native
IdentifierInfo * kw_get
IdentifierInfo * kw_clocking
IdentifierInfo * kw_typeof
IdentifierInfo * kw_join_none
IdentifierInfo * kw_wand
IdentifierInfo * kw_endcase
IdentifierInfo * kw_intersect
IdentifierInfo * kw_include
IdentifierInfo * kw_tri0
IdentifierInfo * kw_then
IdentifierInfo * kw_primitive
IdentifierInfo * kw_record
IdentifierInfo * kw_string
IdentifierInfo * kw_begin
IdentifierInfo * kw_throws
IdentifierInfo * kw_slots
IdentifierInfo * kw_elsif
IdentifierInfo * kw_NS_ENUM
IdentifierInfo * kw_end
IdentifierInfo * kw_set
IdentifierInfo * kw_key_block
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_digest_block
IdentifierInfo * kw_data_public_key
IdentifierInfo * kw_uint
IdentifierInfo * kw_internal_ident_after_define
IdentifierInfo * kw_randsequence
IdentifierInfo * kw_data_block
IdentifierInfo * kw_checked
IdentifierInfo * kw_key_public_key
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_context
IdentifierInfo * kw_endsequence
IdentifierInfo * kw_inout
IdentifierInfo * kw_defset
IdentifierInfo * kw_end_keywords
IdentifierInfo * kw_matches
IdentifierInfo * kw_uwire
IdentifierInfo * kw_endspecify
IdentifierInfo * kw_data_decrypt_key
IdentifierInfo * kw_CF_CLOSED_ENUM
IdentifierInfo * kw_digest_public_key
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.
unsigned AppliedIndentLevel
Block + continuation indent level, applied by the WhitespaceManager to this token.
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.