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) \
41 TYPE(CaseLabelColon) \
44 TYPE(CompoundRequirementLBrace) \
46 TYPE(ConditionalExpr) \
48 TYPE(ConditionLParen) \
49 TYPE(ConflictAlternative) \
53 TYPE(ControlStatementLBrace) \
55 TYPE(CSharpGenericTypeConstraint) \
56 TYPE(CSharpGenericTypeConstraintColon) \
57 TYPE(CSharpGenericTypeConstraintComma) \
58 TYPE(CSharpNamedArgumentColon) \
59 TYPE(CSharpNullable) \
60 TYPE(CSharpNullConditionalLSquare) \
61 TYPE(CSharpStringLiteral) \
62 TYPE(CtorInitializerColon) \
63 TYPE(CtorInitializerComma) \
64 TYPE(DesignatedInitializerLSquare) \
65 TYPE(DesignatedInitializerPeriod) \
71 TYPE(FunctionAnnotationRParen) \
72 TYPE(FunctionDeclarationName) \
73 TYPE(FunctionLBrace) \
74 TYPE(FunctionLikeOrFreestandingMacro) \
75 TYPE(FunctionTypeLParen) \
77 TYPE(GenericSelectionColon) \
79 TYPE(GotoLabelColon) \
81 TYPE(ImplicitStringLiteral) \
82 TYPE(InheritanceColon) \
83 TYPE(InheritanceComma) \
84 TYPE(InlineASMBrace) \
85 TYPE(InlineASMColon) \
86 TYPE(InlineASMSymbolicNameLSquare) \
87 TYPE(JavaAnnotation) \
89 TYPE(JsComputedPropertyName) \
90 TYPE(JsExponentiation) \
91 TYPE(JsExponentiationEqual) \
92 TYPE(JsPipePipeEqual) \
93 TYPE(JsPrivateIdentifier) \
95 TYPE(JsTypeOperator) \
96 TYPE(JsTypeOptionalQuestion) \
100 TYPE(LeadingJavaAnnotation) \
102 TYPE(MacroBlockBegin) \
103 TYPE(MacroBlockEnd) \
104 TYPE(ModulePartitionColon) \
105 TYPE(NamespaceMacro) \
106 TYPE(NonNullAssertion) \
107 TYPE(NullCoalescingEqual) \
108 TYPE(NullCoalescingOperator) \
109 TYPE(NullPropagatingOperator) \
110 TYPE(ObjCBlockLBrace) \
111 TYPE(ObjCBlockLParen) \
114 TYPE(ObjCMethodExpr) \
115 TYPE(ObjCMethodSpecifier) \
117 TYPE(ObjCStringLiteral) \
118 TYPE(OverloadedOperator) \
119 TYPE(OverloadedOperatorLParen) \
120 TYPE(PointerOrReference) \
121 TYPE(ProtoExtensionLSquare) \
122 TYPE(PureVirtualSpecifier) \
123 TYPE(RangeBasedForLoopColon) \
126 TYPE(RequiresClause) \
127 TYPE(RequiresClauseInARequiresExpression) \
128 TYPE(RequiresExpression) \
129 TYPE(RequiresExpressionLBrace) \
130 TYPE(RequiresExpressionLParen) \
133 TYPE(StatementAttributeLikeMacro) \
134 TYPE(StatementMacro) \
136 TYPE(StructuredBindingLSquare) \
137 TYPE(TemplateCloser) \
138 TYPE(TemplateOpener) \
139 TYPE(TemplateString) \
140 TYPE(TrailingAnnotation) \
141 TYPE(TrailingReturnArrow) \
142 TYPE(TrailingUnaryOperator) \
143 TYPE(TypeDeclarationParen) \
144 TYPE(TypenameMacro) \
145 TYPE(UnaryOperator) \
147 TYPE(UntouchableMacroFunc) \
149 TYPE(VerilogAssignComma) \
151 TYPE(VerilogBlockLabelColon) \
155 TYPE(VerilogDimensionedTypeName) \
157 TYPE(VerilogInstancePortComma) \
158 TYPE(VerilogInstancePortLParen) \
161 TYPE(VerilogMultiLineListLParen) \
163 TYPE(VerilogNumberBase) \
165 TYPE(VerilogStrength) \
167 TYPE(VerilogTableItem) \
169 TYPE(VerilogTypeComma) \
175#define TYPE(X) TT_##X,
269 TypeIsFinalized(
false),
Type(TT_Unknown) {}
284 std::shared_ptr<TokenRole>
Role;
343 unsigned BlockKind : 2;
351 assert(
getBlockKind() == BBK &&
"BraceBlockKind overflow!");
356 unsigned Decision : 2;
364 assert(
getDecision() == D &&
"FormatDecision overflow!");
369 unsigned PackingKind : 2;
377 assert(
getPackingKind() == K &&
"ParameterPackingKind overflow!");
381 unsigned TypeIsFinalized : 1;
394 assert((!TypeIsFinalized || T ==
Type) &&
395 "Please use overwriteFixedType to change a fixed type.");
404 TypeIsFinalized =
true;
407 TypeIsFinalized =
false;
542 std::optional<MacroExpansion>
MacroCtx;
565 template <
typename A,
typename B>
bool isOneOf(A K1, B K2)
const {
568 template <
typename A,
typename B,
typename... Ts>
572 template <
typename T>
bool isNot(T Kind)
const {
return !
is(Kind); }
574 bool isIf(
bool AllowConstexprMacro =
true)
const {
575 return is(tok::kw_if) ||
endsSequence(tok::kw_constexpr, tok::kw_if) ||
589 template <
typename A,
typename... Ts>
591 return startsSequenceInternal(K1, Tokens...);
600 template <
typename A,
typename... Ts>
602 return endsSequenceInternal(K1, Tokens...);
612 if (!
isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
617 return NextNonComment && NextNonComment->is(tok::colon);
621 return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile,
622 tok::kw___attribute, tok::kw__Nonnull, tok::kw__Nullable,
623 tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64,
624 tok::kw___funcref, TT_AttributeMacro);
633 return is(tok::at) &&
Next &&
643 if (
is(TT_TemplateString) &&
TokenText.endswith(
"${"))
645 if (
is(TT_DictLiteral) &&
is(tok::less))
647 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
653 if (
is(TT_TemplateString) &&
TokenText.startswith(
"}"))
655 if (
is(TT_DictLiteral) &&
is(tok::greater))
657 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
663 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
664 !
isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
665 TT_LambdaArrow, TT_LeadingJavaAnnotation);
673 case tok::minusminus:
677 case tok::kw_alignof:
690 return is(tok::comment) &&
702 case tok::kw_alignof:
703 case tok::kw_alignas:
704 case tok::kw_decltype:
705 case tok::kw_noexcept:
706 case tok::kw_static_assert:
707 case tok::kw__Atomic:
708 case tok::kw___attribute:
709#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
710#include "clang/Basic/TransformTypeTraits.def"
711 case tok::kw_requires:
721 if (!
is(tok::string_literal))
724 if (Content.startswith(
"\"") || Content.startswith(
"'"))
725 Content = Content.drop_front(1);
726 if (Content.endswith(
"\"") || Content.endswith(
"'"))
727 Content = Content.drop_back(1);
728 Content = Content.trim();
729 return Content.size() > 1 &&
730 (Content.back() ==
':' || Content.back() ==
'=');
758 while (
Tok &&
Tok->
is(tok::comment))
766 while (
Tok &&
Tok->
is(tok::comment))
778 if (!Style.isCpp() ||
isNot(tok::l_square))
783 }
while (T && T->
isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
785 return T && T->
is(tok::kw_auto);
799 if (
is(tok::comment))
802 if (NamespaceTok && NamespaceTok->
isOneOf(tok::kw_inline, tok::kw_export))
804 return NamespaceTok &&
805 NamespaceTok->
isOneOf(tok::kw_namespace, TT_NamespaceMacro)
817 template <
typename A,
typename... Ts>
818 bool startsSequenceInternal(A K1, Ts... Tokens)
const {
819 if (
is(tok::comment) &&
Next)
820 return Next->startsSequenceInternal(K1, Tokens...);
821 return is(K1) &&
Next &&
Next->startsSequenceInternal(Tokens...);
824 template <
typename A>
bool startsSequenceInternal(A K1)
const {
825 if (
is(tok::comment) &&
Next)
826 return Next->startsSequenceInternal(K1);
830 template <
typename A,
typename... Ts>
bool endsSequenceInternal(A K1)
const {
832 return Previous->endsSequenceInternal(K1);
836 template <
typename A,
typename... Ts>
837 bool endsSequenceInternal(A K1, Ts... Tokens)
const {
839 return Previous->endsSequenceInternal(K1, Tokens...);
893 bool DryRun)
override;
896 bool DryRun)
override;
906 return Commas.back();
912 struct ColumnFormat {
928 const ColumnFormat *getColumnFormat(
unsigned RemainingCharacters)
const;
931 SmallVector<const FormatToken *, 8> Commas;
935 SmallVector<unsigned, 8> ItemLengths;
938 SmallVector<ColumnFormat, 4> Formats;
940 bool HasNestedBracedList;
1010 &IdentTable.
get(
"__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__");
1183 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
1190 CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>(
1207 VerilogExtraKeywords = std::unordered_set<IdentifierInfo *>(
1530 bool AcceptIdentifierName =
true)
const {
1538 case tok::kw_continue:
1540 case tok::kw_default:
1541 case tok::kw_delete:
1545 case tok::kw_export:
1549 case tok::kw_import:
1550 case tok::kw_module:
1552 case tok::kw_private:
1553 case tok::kw_protected:
1554 case tok::kw_public:
1555 case tok::kw_return:
1556 case tok::kw_static:
1557 case tok::kw_switch:
1562 case tok::kw_typeof:
1567 case tok::identifier: {
1570 bool IsPseudoKeyword =
1572 JsExtraKeywords.end();
1573 return AcceptIdentifierName || !IsPseudoKeyword;
1583#define KEYWORD(X, Y) case tok::kw_##X:
1584#include "clang/Basic/TokenKinds.def"
1605 case tok::kw_continue:
1606 case tok::kw_default:
1608 case tok::kw_double:
1611 case tok::kw_explicit:
1612 case tok::kw_extern:
1620 case tok::kw_namespace:
1622 case tok::kw_operator:
1623 case tok::kw_private:
1624 case tok::kw_protected:
1625 case tok::kw_public:
1626 case tok::kw_return:
1628 case tok::kw_sizeof:
1629 case tok::kw_static:
1630 case tok::kw_struct:
1631 case tok::kw_switch:
1636 case tok::kw_typeof:
1638 case tok::kw_virtual:
1640 case tok::kw_volatile:
1644 return Tok.
is(tok::identifier) &&
1646 CSharpExtraKeywords.end();
1660 case tok::kw_continue:
1661 case tok::kw_default:
1663 case tok::kw_extern:
1668 case tok::kw_restrict:
1669 case tok::kw_signed:
1670 case tok::kw_static:
1671 case tok::kw_struct:
1672 case tok::kw_typedef:
1674 case tok::kw_unsigned:
1675 case tok::kw_virtual:
1678 case tok::identifier:
1681 VerilogExtraKeywords.end();
1695 switch (Info->getPPKeywordID()) {
1696 case tok::pp_define:
1700 case tok::pp_ifndef:
1701 case tok::pp_include:
1703 case tok::pp_pragma:
1755 return Tok.
is(TT_CaseLabelColon) ||
1756 (Tok.
is(tok::kw_default) &&
1770 case tok::kw_extern:
1771 case tok::kw_signed:
1772 case tok::kw_static:
1773 case tok::kw_unsigned:
1774 case tok::kw_virtual:
1776 case tok::identifier:
1791 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
1794 std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
1797 std::unordered_set<IdentifierInfo *> VerilogExtraKeywords;
1801 return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith(
"/*");
1812 unsigned MinContinueColumn =
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Defines and computes precedence levels for binary/ternary operators.
static constexpr bool isOneOf()
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.
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.
IdentifierInfo * getIdentifierInfo() 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)) {....
tok::TokenKind getKind() const
bool isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const
Return true if we have an ObjC keyword identifier.
The base class of the type hierarchy.
bool isStringLiteral(TokenKind K)
Return true if this is a C or C++ string-literal (or C++11 user-defined-string-literal) token.
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.
prec::Level getBinOpPrecedence(tok::TokenKind Kind, bool GreaterThanIsOperator, bool CPlusPlus11)
Return the precedence of the specified binary operator token.