15#ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
16#define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
22#include <unordered_set>
27#define LIST_TOKEN_TYPES \
28 TYPE(ArrayInitializerLSquare) \
29 TYPE(ArraySubscriptLSquare) \
30 TYPE(AttributeColon) \
31 TYPE(AttributeLParen) \
32 TYPE(AttributeMacro) \
33 TYPE(AttributeRParen) \
34 TYPE(AttributeSquare) \
35 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(TableGenBangOperator) \
152 TYPE(TableGenCondOperator) \
153 TYPE(TableGenCondOperatorColon) \
154 TYPE(TableGenCondOperatorComma) \
155 TYPE(TableGenDAGArgCloser) \
156 TYPE(TableGenDAGArgListColon) \
157 TYPE(TableGenDAGArgListColonToAlign) \
158 TYPE(TableGenDAGArgListComma) \
159 TYPE(TableGenDAGArgListCommaToBreak) \
160 TYPE(TableGenDAGArgOpener) \
161 TYPE(TableGenDAGArgOpenerToBreak) \
162 TYPE(TableGenDAGArgOperatorID) \
163 TYPE(TableGenDAGArgOperatorToBreak) \
164 TYPE(TableGenListCloser) \
165 TYPE(TableGenListOpener) \
166 TYPE(TableGenMultiLineString) \
167 TYPE(TableGenTrailingPasteOperator) \
168 TYPE(TableGenValueSuffix) \
169 TYPE(TemplateCloser) \
170 TYPE(TemplateOpener) \
171 TYPE(TemplateString) \
172 TYPE(TrailingAnnotation) \
173 TYPE(TrailingReturnArrow) \
174 TYPE(TrailingUnaryOperator) \
175 TYPE(TypeDeclarationParen) \
177 TYPE(TypenameMacro) \
178 TYPE(UnaryOperator) \
181 TYPE(UntouchableMacroFunc) \
183 TYPE(VerilogAssignComma) \
185 TYPE(VerilogBlockLabelColon) \
189 TYPE(VerilogDimensionedTypeName) \
191 TYPE(VerilogInstancePortComma) \
192 TYPE(VerilogInstancePortLParen) \
195 TYPE(VerilogMultiLineListLParen) \
197 TYPE(VerilogNumberBase) \
199 TYPE(VerilogStrength) \
201 TYPE(VerilogTableItem) \
203 TYPE(VerilogTypeComma) \
209#define TYPE(X) TT_##X,
319 std::shared_ptr<TokenRole>
Role;
382 unsigned BlockKind : 2;
390 assert(
getBlockKind() == BBK &&
"BraceBlockKind overflow!");
395 unsigned Decision : 2;
403 assert(
getDecision() == D &&
"FormatDecision overflow!");
408 unsigned PackingKind : 2;
416 assert(
getPackingKind() == K &&
"ParameterPackingKind overflow!");
420 unsigned TypeIsFinalized : 1;
433 assert((!TypeIsFinalized ||
T ==
Type) &&
434 "Please use overwriteFixedType to change a fixed type.");
445 TypeIsFinalized =
true;
450 TypeIsFinalized =
false;
594 std::optional<MacroExpansion>
MacroCtx;
617 template <
typename A,
typename B>
bool isOneOf(A K1, B K2)
const {
620 template <
typename A,
typename B,
typename... Ts>
621 bool isOneOf(A K1, B K2, Ts... Ks)
const {
624 template <
typename T>
bool isNot(
T Kind)
const {
return !
is(Kind); }
626 bool isIf(
bool AllowConstexprMacro =
true)
const {
627 return is(tok::kw_if) ||
endsSequence(tok::kw_constexpr, tok::kw_if) ||
628 (
endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro);
641 template <
typename A,
typename... Ts>
643 return startsSequenceInternal(K1, Tokens...);
652 template <
typename A,
typename... Ts>
654 return endsSequenceInternal(K1, Tokens...);
660 return isOneOf(tok::kw___attribute, tok::kw___declspec, TT_AttributeMacro);
668 if (!
isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
673 return NextNonComment && NextNonComment->is(tok::colon);
677 return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile,
678 tok::kw__Nonnull, tok::kw__Nullable,
679 tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64,
680 tok::kw___funcref) ||
687 [[nodiscard]]
bool isTypeName(
bool IsCpp)
const;
692 return is(tok::at) &&
Next &&
702 if (
is(TT_TemplateString) &&
TokenText.ends_with(
"${"))
704 if (
is(TT_DictLiteral) &&
is(tok::less))
706 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
712 if (
is(TT_TemplateString) &&
TokenText.starts_with(
"}"))
714 if (
is(TT_DictLiteral) &&
is(tok::greater))
716 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
722 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
723 !
isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
724 TT_LeadingJavaAnnotation);
728 return isOneOf(tok::star, tok::amp, tok::ampamp);
736 case tok::minusminus:
740 case tok::kw_alignof:
753 return is(tok::comment) &&
763 return isOneOf(tok::kw_throw, tok::kw_typeid, tok::kw_return,
764 tok::kw_sizeof, tok::kw_alignof, tok::kw_alignas,
765 tok::kw_decltype, tok::kw_noexcept, tok::kw_static_assert,
768#include
"clang/Basic/TransformTypeTraits.def"
775 if (
isNot(tok::string_literal))
778 if (Content.starts_with(
"\"") || Content.starts_with(
"'"))
779 Content = Content.drop_front(1);
780 if (Content.ends_with(
"\"") || Content.ends_with(
"'"))
781 Content = Content.drop_back(1);
782 Content = Content.trim();
783 return Content.size() > 1 &&
784 (Content.back() ==
':' || Content.back() ==
'=');
820 while (
Tok &&
Tok->
is(tok::comment))
835 if (!IsCpp ||
isNot(tok::l_square))
839 T =
T->getPreviousNonComment();
840 }
while (
T &&
T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
842 return T &&
T->is(tok::kw_auto);
856 if (
is(tok::comment))
859 if (NamespaceTok && NamespaceTok->
isOneOf(tok::kw_inline, tok::kw_export))
861 return NamespaceTok &&
862 NamespaceTok->
isOneOf(tok::kw_namespace, TT_NamespaceMacro)
874 template <
typename A,
typename... Ts>
875 bool startsSequenceInternal(A K1, Ts... Tokens)
const {
876 if (
is(tok::comment) &&
Next)
877 return Next->startsSequenceInternal(K1, Tokens...);
878 return is(K1) &&
Next &&
Next->startsSequenceInternal(Tokens...);
881 template <
typename A>
bool startsSequenceInternal(A K1)
const {
882 if (
is(tok::comment) &&
Next)
883 return Next->startsSequenceInternal(K1);
887 template <
typename A,
typename... Ts>
bool endsSequenceInternal(A K1)
const {
889 return Previous->endsSequenceInternal(K1);
893 template <
typename A,
typename... Ts>
894 bool endsSequenceInternal(A K1, Ts... Tokens)
const {
896 return Previous->endsSequenceInternal(K1, Tokens...);
950 bool DryRun)
override;
953 bool DryRun)
override;
957 Commas.push_back(
Token);
963 return Commas.back();
969 struct ColumnFormat {
985 const ColumnFormat *getColumnFormat(
unsigned RemainingCharacters)
const;
988 SmallVector<const FormatToken *, 8> Commas;
992 SmallVector<unsigned, 8> ItemLengths;
995 SmallVector<ColumnFormat, 4> Formats;
997 bool HasNestedBracedList;
1067 &IdentTable.
get(
"__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__");
1255 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
1262 CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>(
1279 VerilogExtraKeywords = std::unordered_set<IdentifierInfo *>(
1345 TableGenExtraKeywords = std::unordered_set<IdentifierInfo *>({
1638 bool AcceptIdentifierName =
true)
const {
1646 case tok::kw_continue:
1648 case tok::kw_default:
1649 case tok::kw_delete:
1653 case tok::kw_export:
1657 case tok::kw_import:
1658 case tok::kw_module:
1660 case tok::kw_private:
1661 case tok::kw_protected:
1662 case tok::kw_public:
1663 case tok::kw_return:
1664 case tok::kw_static:
1665 case tok::kw_switch:
1670 case tok::kw_typeof:
1675 case tok::identifier: {
1678 bool IsPseudoKeyword =
1680 JsExtraKeywords.end();
1681 return AcceptIdentifierName || !IsPseudoKeyword;
1691#define KEYWORD(X, Y) case tok::kw_##X:
1692#include "clang/Basic/TokenKinds.def"
1705 switch (Tok.Tok.getKind()) {
1713 case tok::kw_continue:
1714 case tok::kw_default:
1716 case tok::kw_double:
1719 case tok::kw_explicit:
1720 case tok::kw_extern:
1728 case tok::kw_namespace:
1730 case tok::kw_operator:
1731 case tok::kw_private:
1732 case tok::kw_protected:
1733 case tok::kw_public:
1734 case tok::kw_return:
1736 case tok::kw_sizeof:
1737 case tok::kw_static:
1738 case tok::kw_struct:
1739 case tok::kw_switch:
1744 case tok::kw_typeof:
1746 case tok::kw_virtual:
1748 case tok::kw_volatile:
1752 return Tok.is(tok::identifier) &&
1753 CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
1754 CSharpExtraKeywords.end();
1768 case tok::kw_continue:
1769 case tok::kw_default:
1771 case tok::kw_extern:
1776 case tok::kw_restrict:
1777 case tok::kw_signed:
1778 case tok::kw_static:
1779 case tok::kw_struct:
1780 case tok::kw_typedef:
1782 case tok::kw_unsigned:
1783 case tok::kw_virtual:
1786 case tok::identifier:
1789 VerilogExtraKeywords.end();
1800 auto Info = Tok.Tok.getIdentifierInfo();
1803 switch (Info->getPPKeywordID()) {
1804 case tok::pp_define:
1808 case tok::pp_ifndef:
1809 case tok::pp_include:
1811 case tok::pp_pragma:
1863 return Tok.
is(TT_CaseLabelColon) ||
1864 (Tok.
is(tok::kw_default) &&
1878 case tok::kw_extern:
1879 case tok::kw_signed:
1880 case tok::kw_static:
1881 case tok::kw_unsigned:
1882 case tok::kw_virtual:
1884 case tok::identifier:
1912 return Tok.
is(tok::identifier) &&
1914 TableGenExtraKeywords.end();
1920 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
1923 std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
1926 std::unordered_set<IdentifierInfo *> VerilogExtraKeywords;
1929 std::unordered_set<IdentifierInfo *> TableGenExtraKeywords;
1933 return FormatTok.is(tok::comment) && !FormatTok.TokenText.starts_with(
"/*");
1941 const FormatToken *MinColumnToken) {
1944 unsigned MinContinueColumn =
1945 MinColumnToken->OriginalColumn + (
isLineComment(*MinColumnToken) ? 0 : 1);
1946 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
1948 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.
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