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