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(AttributeLParen) \
34 TYPE(AttributeMacro) \
35 TYPE(AttributeRParen) \
36 TYPE(AttributeSquare) \
37 TYPE(BinaryOperator) \
40 TYPE(BracedListLBrace) \
42 TYPE(CaseLabelColon) \
47 TYPE(ConditionalExpr) \
49 TYPE(ConditionLParen) \
50 TYPE(ConflictAlternative) \
54 TYPE(ControlStatementLBrace) \
55 TYPE(ControlStatementRBrace) \
57 TYPE(CSharpGenericTypeConstraint) \
58 TYPE(CSharpGenericTypeConstraintColon) \
59 TYPE(CSharpGenericTypeConstraintComma) \
60 TYPE(CSharpNamedArgumentColon) \
61 TYPE(CSharpNullable) \
62 TYPE(CSharpNullConditionalLSquare) \
63 TYPE(CSharpStringLiteral) \
64 TYPE(CtorInitializerColon) \
65 TYPE(CtorInitializerComma) \
66 TYPE(CtorDtorDeclName) \
67 TYPE(DesignatedInitializerLSquare) \
68 TYPE(DesignatedInitializerPeriod) \
77 TYPE(FunctionAnnotationRParen) \
78 TYPE(FunctionDeclarationName) \
79 TYPE(FunctionLBrace) \
80 TYPE(FunctionLikeOrFreestandingMacro) \
81 TYPE(FunctionTypeLParen) \
83 TYPE(GenericSelectionColon) \
85 TYPE(GotoLabelColon) \
87 TYPE(ImplicitStringLiteral) \
88 TYPE(InheritanceColon) \
89 TYPE(InheritanceComma) \
90 TYPE(InlineASMBrace) \
91 TYPE(InlineASMColon) \
92 TYPE(InlineASMSymbolicNameLSquare) \
93 TYPE(JavaAnnotation) \
95 TYPE(JsComputedPropertyName) \
96 TYPE(JsExponentiation) \
97 TYPE(JsExponentiationEqual) \
98 TYPE(JsPipePipeEqual) \
99 TYPE(JsPrivateIdentifier) \
101 TYPE(JsTypeOperator) \
102 TYPE(JsTypeOptionalQuestion) \
104 TYPE(LambdaLSquare) \
105 TYPE(LeadingJavaAnnotation) \
107 TYPE(MacroBlockBegin) \
108 TYPE(MacroBlockEnd) \
109 TYPE(ModulePartitionColon) \
110 TYPE(NamespaceLBrace) \
111 TYPE(NamespaceMacro) \
112 TYPE(NamespaceRBrace) \
113 TYPE(NonNullAssertion) \
114 TYPE(NullCoalescingEqual) \
115 TYPE(NullCoalescingOperator) \
116 TYPE(NullPropagatingOperator) \
117 TYPE(ObjCBlockLBrace) \
118 TYPE(ObjCBlockLParen) \
121 TYPE(ObjCMethodExpr) \
122 TYPE(ObjCMethodSpecifier) \
124 TYPE(ObjCStringLiteral) \
125 TYPE(OverloadedOperator) \
126 TYPE(OverloadedOperatorLParen) \
127 TYPE(PointerOrReference) \
128 TYPE(ProtoExtensionLSquare) \
129 TYPE(PureVirtualSpecifier) \
130 TYPE(RangeBasedForLoopColon) \
134 TYPE(RequiresClause) \
135 TYPE(RequiresClauseInARequiresExpression) \
136 TYPE(RequiresExpression) \
137 TYPE(RequiresExpressionLBrace) \
138 TYPE(RequiresExpressionLParen) \
141 TYPE(StatementAttributeLikeMacro) \
142 TYPE(StatementMacro) \
147 TYPE(StringInConcatenation) \
150 TYPE(StructuredBindingLSquare) \
151 TYPE(TemplateCloser) \
152 TYPE(TemplateOpener) \
153 TYPE(TemplateString) \
154 TYPE(TrailingAnnotation) \
155 TYPE(TrailingReturnArrow) \
156 TYPE(TrailingUnaryOperator) \
157 TYPE(TypeDeclarationParen) \
159 TYPE(TypenameMacro) \
160 TYPE(UnaryOperator) \
163 TYPE(UntouchableMacroFunc) \
165 TYPE(VerilogAssignComma) \
167 TYPE(VerilogBlockLabelColon) \
171 TYPE(VerilogDimensionedTypeName) \
173 TYPE(VerilogInstancePortComma) \
174 TYPE(VerilogInstancePortLParen) \
177 TYPE(VerilogMultiLineListLParen) \
179 TYPE(VerilogNumberBase) \
181 TYPE(VerilogStrength) \
183 TYPE(VerilogTableItem) \
185 TYPE(VerilogTypeComma) \
191#define TYPE(X) TT_##X,
285 TypeIsFinalized(
false),
Type(TT_Unknown) {}
300 std::shared_ptr<TokenRole>
Role;
359 unsigned BlockKind : 2;
367 assert(
getBlockKind() == BBK &&
"BraceBlockKind overflow!");
372 unsigned Decision : 2;
380 assert(
getDecision() == D &&
"FormatDecision overflow!");
385 unsigned PackingKind : 2;
393 assert(
getPackingKind() == K &&
"ParameterPackingKind overflow!");
397 unsigned TypeIsFinalized : 1;
410 assert((!TypeIsFinalized || T ==
Type) &&
411 "Please use overwriteFixedType to change a fixed type.");
420 TypeIsFinalized =
true;
423 TypeIsFinalized =
false;
564 std::optional<MacroExpansion>
MacroCtx;
587 template <
typename A,
typename B>
bool isOneOf(A K1, B K2)
const {
590 template <
typename A,
typename B,
typename... Ts>
591 bool isOneOf(A K1, B K2, Ts... Ks)
const {
594 template <
typename T>
bool isNot(T Kind)
const {
return !
is(Kind); }
596 bool isIf(
bool AllowConstexprMacro =
true)
const {
597 return is(tok::kw_if) ||
endsSequence(tok::kw_constexpr, tok::kw_if) ||
598 (
endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro);
611 template <
typename A,
typename... Ts>
613 return startsSequenceInternal(K1, Tokens...);
622 template <
typename A,
typename... Ts>
624 return endsSequenceInternal(K1, Tokens...);
630 return isOneOf(tok::kw___attribute, tok::kw___declspec, TT_AttributeMacro);
638 if (!
isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
643 return NextNonComment && NextNonComment->is(tok::colon);
647 return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile,
648 tok::kw__Nonnull, tok::kw__Nullable,
649 tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64,
650 tok::kw___funcref) ||
660 return is(tok::at) &&
Next &&
670 if (
is(TT_TemplateString) &&
TokenText.endswith(
"${"))
672 if (
is(TT_DictLiteral) &&
is(tok::less))
674 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
680 if (
is(TT_TemplateString) &&
TokenText.startswith(
"}"))
682 if (
is(TT_DictLiteral) &&
is(tok::greater))
684 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
690 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
691 !
isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
692 TT_LeadingJavaAnnotation);
696 return isOneOf(tok::star, tok::amp, tok::ampamp);
704 case tok::minusminus:
708 case tok::kw_alignof:
721 return is(tok::comment) &&
731 return isOneOf(tok::kw_throw, tok::kw_typeid, tok::kw_return,
732 tok::kw_sizeof, tok::kw_alignof, tok::kw_alignas,
733 tok::kw_decltype, tok::kw_noexcept, tok::kw_static_assert,
736#include
"clang/Basic/TransformTypeTraits.def"
743 if (
isNot(tok::string_literal))
746 if (Content.startswith(
"\"") || Content.startswith(
"'"))
747 Content = Content.drop_front(1);
748 if (Content.endswith(
"\"") || Content.endswith(
"'"))
749 Content = Content.drop_back(1);
750 Content = Content.trim();
751 return Content.size() > 1 &&
752 (Content.back() ==
':' || Content.back() ==
'=');
788 while (
Tok &&
Tok->
is(tok::comment))
803 if (!Style.isCpp() ||
isNot(tok::l_square))
808 }
while (T && T->
isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
810 return T && T->
is(tok::kw_auto);
824 if (
is(tok::comment))
827 if (NamespaceTok && NamespaceTok->
isOneOf(tok::kw_inline, tok::kw_export))
829 return NamespaceTok &&
830 NamespaceTok->
isOneOf(tok::kw_namespace, TT_NamespaceMacro)
842 template <
typename A,
typename... Ts>
843 bool startsSequenceInternal(A K1, Ts... Tokens)
const {
844 if (
is(tok::comment) &&
Next)
845 return Next->startsSequenceInternal(K1, Tokens...);
846 return is(K1) &&
Next &&
Next->startsSequenceInternal(Tokens...);
849 template <
typename A>
bool startsSequenceInternal(A K1)
const {
850 if (
is(tok::comment) &&
Next)
851 return Next->startsSequenceInternal(K1);
855 template <
typename A,
typename... Ts>
bool endsSequenceInternal(A K1)
const {
857 return Previous->endsSequenceInternal(K1);
861 template <
typename A,
typename... Ts>
862 bool endsSequenceInternal(A K1, Ts... Tokens)
const {
864 return Previous->endsSequenceInternal(K1, Tokens...);
918 bool DryRun)
override;
921 bool DryRun)
override;
925 Commas.push_back(
Token);
931 return Commas.back();
937 struct ColumnFormat {
953 const ColumnFormat *getColumnFormat(
unsigned RemainingCharacters)
const;
956 SmallVector<const FormatToken *, 8> Commas;
960 SmallVector<unsigned, 8> ItemLengths;
963 SmallVector<ColumnFormat, 4> Formats;
965 bool HasNestedBracedList;
1035 &IdentTable.
get(
"__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__");
1208 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
1215 CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>(
1232 VerilogExtraKeywords = std::unordered_set<IdentifierInfo *>(
1555 bool AcceptIdentifierName =
true)
const {
1563 case tok::kw_continue:
1565 case tok::kw_default:
1566 case tok::kw_delete:
1570 case tok::kw_export:
1574 case tok::kw_import:
1575 case tok::kw_module:
1577 case tok::kw_private:
1578 case tok::kw_protected:
1579 case tok::kw_public:
1580 case tok::kw_return:
1581 case tok::kw_static:
1582 case tok::kw_switch:
1587 case tok::kw_typeof:
1592 case tok::identifier: {
1595 bool IsPseudoKeyword =
1597 JsExtraKeywords.end();
1598 return AcceptIdentifierName || !IsPseudoKeyword;
1608#define KEYWORD(X, Y) case tok::kw_##X:
1609#include "clang/Basic/TokenKinds.def"
1622 switch (Tok.Tok.getKind()) {
1630 case tok::kw_continue:
1631 case tok::kw_default:
1633 case tok::kw_double:
1636 case tok::kw_explicit:
1637 case tok::kw_extern:
1645 case tok::kw_namespace:
1647 case tok::kw_operator:
1648 case tok::kw_private:
1649 case tok::kw_protected:
1650 case tok::kw_public:
1651 case tok::kw_return:
1653 case tok::kw_sizeof:
1654 case tok::kw_static:
1655 case tok::kw_struct:
1656 case tok::kw_switch:
1661 case tok::kw_typeof:
1663 case tok::kw_virtual:
1665 case tok::kw_volatile:
1669 return Tok.is(tok::identifier) &&
1670 CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
1671 CSharpExtraKeywords.end();
1685 case tok::kw_continue:
1686 case tok::kw_default:
1688 case tok::kw_extern:
1693 case tok::kw_restrict:
1694 case tok::kw_signed:
1695 case tok::kw_static:
1696 case tok::kw_struct:
1697 case tok::kw_typedef:
1699 case tok::kw_unsigned:
1700 case tok::kw_virtual:
1703 case tok::identifier:
1706 VerilogExtraKeywords.end();
1717 auto Info = Tok.Tok.getIdentifierInfo();
1720 switch (Info->getPPKeywordID()) {
1721 case tok::pp_define:
1725 case tok::pp_ifndef:
1726 case tok::pp_include:
1728 case tok::pp_pragma:
1780 return Tok.
is(TT_CaseLabelColon) ||
1781 (Tok.
is(tok::kw_default) &&
1795 case tok::kw_extern:
1796 case tok::kw_signed:
1797 case tok::kw_static:
1798 case tok::kw_unsigned:
1799 case tok::kw_virtual:
1801 case tok::identifier:
1816 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
1819 std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
1822 std::unordered_set<IdentifierInfo *> VerilogExtraKeywords;
1826 return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith(
"/*");
1834 const FormatToken *MinColumnToken) {
1837 unsigned MinContinueColumn =
1838 MinColumnToken->OriginalColumn + (
isLineComment(*MinColumnToken) ? 0 : 1);
1839 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
1841 FormatTok.OriginalColumn >= MinContinueColumn;
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, _)
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.