Go to the documentation of this file.
15 #ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
16 #define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
24 #include <unordered_set>
29 #define LIST_TOKEN_TYPES \
30 TYPE(ArrayInitializerLSquare) \
31 TYPE(ArraySubscriptLSquare) \
32 TYPE(AttributeColon) \
33 TYPE(AttributeMacro) \
34 TYPE(AttributeParen) \
35 TYPE(AttributeSquare) \
36 TYPE(BinaryOperator) \
39 TYPE(BracedListLBrace) \
42 TYPE(CompoundRequirementLBrace) \
44 TYPE(ConditionalExpr) \
46 TYPE(ConditionLParen) \
47 TYPE(ConflictAlternative) \
51 TYPE(ControlStatementLBrace) \
53 TYPE(CSharpGenericTypeConstraint) \
54 TYPE(CSharpGenericTypeConstraintColon) \
55 TYPE(CSharpGenericTypeConstraintComma) \
56 TYPE(CSharpNamedArgumentColon) \
57 TYPE(CSharpNullable) \
58 TYPE(CSharpNullConditionalLSquare) \
59 TYPE(CSharpStringLiteral) \
60 TYPE(CtorInitializerColon) \
61 TYPE(CtorInitializerComma) \
62 TYPE(DesignatedInitializerLSquare) \
63 TYPE(DesignatedInitializerPeriod) \
69 TYPE(FunctionAnnotationRParen) \
70 TYPE(FunctionDeclarationName) \
71 TYPE(FunctionLBrace) \
72 TYPE(FunctionLikeOrFreestandingMacro) \
73 TYPE(FunctionTypeLParen) \
75 TYPE(GenericSelectionColon) \
78 TYPE(GotoLabelColon) \
80 TYPE(ImplicitStringLiteral) \
81 TYPE(InheritanceColon) \
82 TYPE(InheritanceComma) \
83 TYPE(InlineASMBrace) \
84 TYPE(InlineASMColon) \
85 TYPE(InlineASMSymbolicNameLSquare) \
86 TYPE(JavaAnnotation) \
88 TYPE(JsComputedPropertyName) \
89 TYPE(JsExponentiation) \
90 TYPE(JsExponentiationEqual) \
91 TYPE(JsPipePipeEqual) \
92 TYPE(JsPrivateIdentifier) \
94 TYPE(JsTypeOperator) \
95 TYPE(JsTypeOptionalQuestion) \
99 TYPE(LeadingJavaAnnotation) \
101 TYPE(MacroBlockBegin) \
102 TYPE(MacroBlockEnd) \
103 TYPE(ModulePartitionColon) \
104 TYPE(NamespaceMacro) \
105 TYPE(NonNullAssertion) \
106 TYPE(NullCoalescingEqual) \
107 TYPE(NullCoalescingOperator) \
108 TYPE(NullPropagatingOperator) \
109 TYPE(ObjCBlockLBrace) \
110 TYPE(ObjCBlockLParen) \
113 TYPE(ObjCMethodExpr) \
114 TYPE(ObjCMethodSpecifier) \
116 TYPE(ObjCStringLiteral) \
117 TYPE(OverloadedOperator) \
118 TYPE(OverloadedOperatorLParen) \
119 TYPE(PointerOrReference) \
120 TYPE(ProtoExtensionLSquare) \
121 TYPE(PureVirtualSpecifier) \
122 TYPE(RangeBasedForLoopColon) \
125 TYPE(RequiresClause) \
126 TYPE(RequiresClauseInARequiresExpression) \
127 TYPE(RequiresExpression) \
128 TYPE(RequiresExpressionLBrace) \
129 TYPE(RequiresExpressionLParen) \
132 TYPE(StatementAttributeLikeMacro) \
133 TYPE(StatementMacro) \
135 TYPE(StructuredBindingLSquare) \
136 TYPE(TemplateCloser) \
137 TYPE(TemplateOpener) \
138 TYPE(TemplateString) \
139 TYPE(TrailingAnnotation) \
140 TYPE(TrailingReturnArrow) \
141 TYPE(TrailingUnaryOperator) \
142 TYPE(TypeDeclarationParen) \
143 TYPE(TypenameMacro) \
144 TYPE(UnaryOperator) \
146 TYPE(UntouchableMacroFunc) \
148 TYPE(VerilogBlockLabelColon) \
152 TYPE(VerilogDimensionedTypeName) \
154 TYPE(VerilogNumberBase) \
156 TYPE(VerilogTableItem) \
162 #define TYPE(X) TT_##X,
256 TypeIsFinalized(
false),
Type(TT_Unknown) {}
271 std::shared_ptr<TokenRole>
Role;
330 unsigned BlockKind : 2;
338 assert(
getBlockKind() == BBK &&
"BraceBlockKind overflow!");
343 unsigned Decision : 2;
351 assert(
getDecision() == D &&
"FormatDecision overflow!");
356 unsigned PackingKind : 2;
364 assert(
getPackingKind() == K &&
"ParameterPackingKind overflow!");
368 unsigned TypeIsFinalized : 1;
376 assert((!TypeIsFinalized || T ==
Type) &&
377 "Please use overwriteFixedType to change a fixed type.");
386 TypeIsFinalized =
true;
389 TypeIsFinalized =
false;
524 std::optional<MacroExpansion>
MacroCtx;
547 template <
typename A,
typename B>
bool isOneOf(A K1, B K2)
const {
550 template <
typename A,
typename B,
typename... Ts>
556 bool isIf(
bool AllowConstexprMacro =
true)
const {
557 return is(tok::kw_if) ||
endsSequence(tok::kw_constexpr, tok::kw_if) ||
571 template <
typename A,
typename... Ts>
573 return startsSequenceInternal(K1, Tokens...);
582 template <
typename A,
typename... Ts>
584 return endsSequenceInternal(K1, Tokens...);
594 if (!
isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
599 return NextNonComment && NextNonComment->is(tok::colon);
603 return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile,
604 tok::kw___attribute, tok::kw__Nonnull, tok::kw__Nullable,
605 tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64,
615 return is(tok::at) &&
Next &&
625 if (
is(TT_TemplateString) &&
TokenText.endswith(
"${"))
627 if (
is(TT_DictLiteral) &&
is(tok::less))
629 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
635 if (
is(TT_TemplateString) &&
TokenText.startswith(
"}"))
637 if (
is(TT_DictLiteral) &&
is(tok::greater))
639 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
645 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
646 !
isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
647 TT_LambdaArrow, TT_LeadingJavaAnnotation);
655 case tok::minusminus:
659 case tok::kw_alignof:
672 return is(tok::comment) &&
684 case tok::kw_alignof:
685 case tok::kw_alignas:
686 case tok::kw_decltype:
687 case tok::kw_noexcept:
688 case tok::kw_static_assert:
689 case tok::kw__Atomic:
690 case tok::kw___attribute:
691 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
692 #include "clang/Basic/TransformTypeTraits.def"
693 case tok::kw_requires:
703 if (!
is(tok::string_literal))
706 if (Content.startswith(
"\"") || Content.startswith(
"'"))
707 Content = Content.drop_front(1);
708 if (Content.endswith(
"\"") || Content.endswith(
"'"))
709 Content = Content.drop_back(1);
710 Content = Content.trim();
711 return Content.size() > 1 &&
712 (Content.back() ==
':' || Content.back() ==
'=');
740 while (
Tok &&
Tok->
is(tok::comment))
748 while (
Tok &&
Tok->
is(tok::comment))
760 if (!Style.isCpp() ||
isNot(tok::l_square))
765 }
while (T && T->
isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
767 return T && T->
is(tok::kw_auto);
781 if (
is(tok::comment))
784 if (NamespaceTok && NamespaceTok->
isOneOf(tok::kw_inline, tok::kw_export))
786 return NamespaceTok &&
787 NamespaceTok->
isOneOf(tok::kw_namespace, TT_NamespaceMacro)
799 template <
typename A,
typename... Ts>
800 bool startsSequenceInternal(A K1, Ts... Tokens)
const {
801 if (
is(tok::comment) &&
Next)
802 return Next->startsSequenceInternal(K1, Tokens...);
803 return is(K1) &&
Next &&
Next->startsSequenceInternal(Tokens...);
806 template <
typename A>
bool startsSequenceInternal(A K1)
const {
807 if (
is(tok::comment) &&
Next)
808 return Next->startsSequenceInternal(K1);
812 template <
typename A,
typename... Ts>
bool endsSequenceInternal(A K1)
const {
814 return Previous->endsSequenceInternal(K1);
818 template <
typename A,
typename... Ts>
819 bool endsSequenceInternal(A K1, Ts... Tokens)
const {
821 return Previous->endsSequenceInternal(K1, Tokens...);
875 bool DryRun)
override;
878 bool DryRun)
override;
888 return Commas.back();
894 struct ColumnFormat {
910 const ColumnFormat *getColumnFormat(
unsigned RemainingCharacters)
const;
913 SmallVector<const FormatToken *, 8> Commas;
917 SmallVector<unsigned, 8> ItemLengths;
920 SmallVector<ColumnFormat, 4> Formats;
922 bool HasNestedBracedList;
991 &IdentTable.
get(
"__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__");
1161 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
1168 CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>(
1185 VerilogExtraKeywords =
1186 std::unordered_set<IdentifierInfo *>({
kw_always,
1565 bool AcceptIdentifierName =
true)
const {
1573 case tok::kw_continue:
1575 case tok::kw_default:
1576 case tok::kw_delete:
1580 case tok::kw_export:
1584 case tok::kw_import:
1585 case tok::kw_module:
1587 case tok::kw_private:
1588 case tok::kw_protected:
1589 case tok::kw_public:
1590 case tok::kw_return:
1591 case tok::kw_static:
1592 case tok::kw_switch:
1597 case tok::kw_typeof:
1602 case tok::identifier: {
1605 bool IsPseudoKeyword =
1607 JsExtraKeywords.end();
1608 return AcceptIdentifierName || !IsPseudoKeyword;
1618 #define KEYWORD(X, Y) case tok::kw_##X:
1619 #include "clang/Basic/TokenKinds.def"
1640 case tok::kw_continue:
1641 case tok::kw_default:
1643 case tok::kw_double:
1646 case tok::kw_explicit:
1647 case tok::kw_extern:
1655 case tok::kw_namespace:
1657 case tok::kw_operator:
1658 case tok::kw_private:
1659 case tok::kw_protected:
1660 case tok::kw_public:
1661 case tok::kw_return:
1663 case tok::kw_sizeof:
1664 case tok::kw_static:
1665 case tok::kw_struct:
1666 case tok::kw_switch:
1671 case tok::kw_typeof:
1673 case tok::kw_virtual:
1675 case tok::kw_volatile:
1679 return Tok.
is(tok::identifier) &&
1681 CSharpExtraKeywords.end();
1695 case tok::kw_continue:
1696 case tok::kw_default:
1698 case tok::kw_extern:
1703 case tok::kw_restrict:
1704 case tok::kw_signed:
1705 case tok::kw_static:
1706 case tok::kw_struct:
1707 case tok::kw_typedef:
1709 case tok::kw_unsigned:
1710 case tok::kw_virtual:
1713 case tok::identifier:
1715 VerilogExtraKeywords.end();
1729 switch (Info->getPPKeywordID()) {
1737 case tok::pp_pragma:
1789 return Tok.
is(TT_GotoLabelColon) ||
1790 (Tok.
is(tok::kw_default) &&
1797 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
1800 std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
1803 std::unordered_set<IdentifierInfo *> VerilogExtraKeywords;
bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const
Return true if we have an ObjC keyword identifier.
A trivial tuple used to represent a source range.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
Encodes a location in the source.
SourceLocation getBegin() const
IdentifierInfo * getIdentifierInfo() const
Token - This structure provides full information about a lexed token.
bool isStringLiteral(TokenKind K)
Return true if this is a C or C++ string-literal (or C++11 user-defined-string-literal) token.
The base class of the type hierarchy.
std::optional< T > Optional
SourceLocation getEnd() const
tok::TokenKind getKind() const
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)) {....
ObjCKeywordKind
Provides a namespace for Objective-C keywords which start with an '@'.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
PPKeywordKind
Provides a namespace for preprocessor keywords which start with a '#' at the beginning of the line.
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.
One of these records is kept for each identifier that is lexed.
Implements an efficient mapping from strings to IdentifierInfo nodes.
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.