15#ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
16#define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
22#include <unordered_set>
30#define LIST_TOKEN_TYPES \
31 TYPE(ArrayInitializerLSquare) \
32 TYPE(ArraySubscriptLSquare) \
33 TYPE(AttributeColon) \
34 TYPE(AttributeLParen) \
35 TYPE(AttributeMacro) \
36 TYPE(AttributeRParen) \
37 TYPE(AttributeSquare) \
38 TYPE(BinaryOperator) \
41 TYPE(BracedListLBrace) \
43 TYPE(CaseLabelColon) \
48 TYPE(ConditionalExpr) \
50 TYPE(ConditionLParen) \
51 TYPE(ConflictAlternative) \
55 TYPE(ControlStatementLBrace) \
56 TYPE(ControlStatementRBrace) \
58 TYPE(CSharpGenericTypeConstraint) \
59 TYPE(CSharpGenericTypeConstraintColon) \
60 TYPE(CSharpGenericTypeConstraintComma) \
61 TYPE(CSharpNamedArgumentColon) \
62 TYPE(CSharpNullable) \
63 TYPE(CSharpNullConditionalLSquare) \
64 TYPE(CSharpStringLiteral) \
65 TYPE(CtorInitializerColon) \
66 TYPE(CtorInitializerComma) \
67 TYPE(CtorDtorDeclName) \
68 TYPE(DesignatedInitializerLSquare) \
69 TYPE(DesignatedInitializerPeriod) \
78 TYPE(FunctionAnnotationRParen) \
79 TYPE(FunctionDeclarationName) \
80 TYPE(FunctionLBrace) \
81 TYPE(FunctionLikeOrFreestandingMacro) \
82 TYPE(FunctionTypeLParen) \
84 TYPE(GenericSelectionColon) \
86 TYPE(GotoLabelColon) \
88 TYPE(ImplicitStringLiteral) \
89 TYPE(InheritanceColon) \
90 TYPE(InheritanceComma) \
91 TYPE(InlineASMBrace) \
92 TYPE(InlineASMColon) \
93 TYPE(InlineASMSymbolicNameLSquare) \
94 TYPE(JavaAnnotation) \
96 TYPE(JsComputedPropertyName) \
97 TYPE(JsExponentiation) \
98 TYPE(JsExponentiationEqual) \
99 TYPE(JsPipePipeEqual) \
100 TYPE(JsPrivateIdentifier) \
102 TYPE(JsTypeOperator) \
103 TYPE(JsTypeOptionalQuestion) \
105 TYPE(LambdaLSquare) \
106 TYPE(LeadingJavaAnnotation) \
108 TYPE(MacroBlockBegin) \
109 TYPE(MacroBlockEnd) \
110 TYPE(ModulePartitionColon) \
111 TYPE(NamespaceLBrace) \
112 TYPE(NamespaceMacro) \
113 TYPE(NamespaceRBrace) \
114 TYPE(NonNullAssertion) \
115 TYPE(NullCoalescingEqual) \
116 TYPE(NullCoalescingOperator) \
117 TYPE(NullPropagatingOperator) \
118 TYPE(ObjCBlockLBrace) \
119 TYPE(ObjCBlockLParen) \
122 TYPE(ObjCMethodExpr) \
123 TYPE(ObjCMethodSpecifier) \
125 TYPE(ObjCStringLiteral) \
126 TYPE(OverloadedOperator) \
127 TYPE(OverloadedOperatorLParen) \
128 TYPE(PointerOrReference) \
129 TYPE(ProtoExtensionLSquare) \
130 TYPE(PureVirtualSpecifier) \
131 TYPE(RangeBasedForLoopColon) \
135 TYPE(RequiresClause) \
136 TYPE(RequiresClauseInARequiresExpression) \
137 TYPE(RequiresExpression) \
138 TYPE(RequiresExpressionLBrace) \
139 TYPE(RequiresExpressionLParen) \
142 TYPE(StatementAttributeLikeMacro) \
143 TYPE(StatementMacro) \
148 TYPE(StringInConcatenation) \
151 TYPE(StructuredBindingLSquare) \
152 TYPE(TableGenBangOperator) \
153 TYPE(TableGenCondOperator) \
154 TYPE(TableGenCondOperatorColon) \
155 TYPE(TableGenCondOperatorComma) \
156 TYPE(TableGenDAGArgCloser) \
157 TYPE(TableGenDAGArgListColon) \
158 TYPE(TableGenDAGArgListComma) \
159 TYPE(TableGenDAGArgOpener) \
160 TYPE(TableGenListCloser) \
161 TYPE(TableGenListOpener) \
162 TYPE(TableGenMultiLineString) \
163 TYPE(TableGenTrailingPasteOperator) \
164 TYPE(TableGenValueSuffix) \
165 TYPE(TemplateCloser) \
166 TYPE(TemplateOpener) \
167 TYPE(TemplateString) \
168 TYPE(TrailingAnnotation) \
169 TYPE(TrailingReturnArrow) \
170 TYPE(TrailingUnaryOperator) \
171 TYPE(TypeDeclarationParen) \
173 TYPE(TypenameMacro) \
174 TYPE(UnaryOperator) \
177 TYPE(UntouchableMacroFunc) \
179 TYPE(VerilogAssignComma) \
181 TYPE(VerilogBlockLabelColon) \
185 TYPE(VerilogDimensionedTypeName) \
187 TYPE(VerilogInstancePortComma) \
188 TYPE(VerilogInstancePortLParen) \
191 TYPE(VerilogMultiLineListLParen) \
193 TYPE(VerilogNumberBase) \
195 TYPE(VerilogStrength) \
197 TYPE(VerilogTableItem) \
199 TYPE(VerilogTypeComma) \
205#define TYPE(X) TT_##X,
315 std::shared_ptr<TokenRole>
Role;
378 unsigned BlockKind : 2;
386 assert(
getBlockKind() == BBK &&
"BraceBlockKind overflow!");
391 unsigned Decision : 2;
399 assert(
getDecision() == D &&
"FormatDecision overflow!");
404 unsigned PackingKind : 2;
412 assert(
getPackingKind() == K &&
"ParameterPackingKind overflow!");
416 unsigned TypeIsFinalized : 1;
429 assert((!TypeIsFinalized || T ==
Type) &&
430 "Please use overwriteFixedType to change a fixed type.");
441 TypeIsFinalized =
true;
446 TypeIsFinalized =
false;
587 std::optional<MacroExpansion>
MacroCtx;
610 template <
typename A,
typename B>
bool isOneOf(A K1, B K2)
const {
613 template <
typename A,
typename B,
typename... Ts>
614 bool isOneOf(A K1, B K2, Ts... Ks)
const {
617 template <
typename T>
bool isNot(T Kind)
const {
return !
is(Kind); }
619 bool isIf(
bool AllowConstexprMacro =
true)
const {
620 return is(tok::kw_if) ||
endsSequence(tok::kw_constexpr, tok::kw_if) ||
621 (
endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro);
634 template <
typename A,
typename... Ts>
636 return startsSequenceInternal(K1, Tokens...);
645 template <
typename A,
typename... Ts>
647 return endsSequenceInternal(K1, Tokens...);
653 return isOneOf(tok::kw___attribute, tok::kw___declspec, TT_AttributeMacro);
661 if (!
isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
666 return NextNonComment && NextNonComment->is(tok::colon);
670 return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile,
671 tok::kw__Nonnull, tok::kw__Nullable,
672 tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64,
673 tok::kw___funcref) ||
685 return is(tok::at) &&
Next &&
695 if (
is(TT_TemplateString) &&
TokenText.ends_with(
"${"))
697 if (
is(TT_DictLiteral) &&
is(tok::less))
699 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
705 if (
is(TT_TemplateString) &&
TokenText.starts_with(
"}"))
707 if (
is(TT_DictLiteral) &&
is(tok::greater))
709 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
715 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
716 !
isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
717 TT_LeadingJavaAnnotation);
721 return isOneOf(tok::star, tok::amp, tok::ampamp);
729 case tok::minusminus:
733 case tok::kw_alignof:
746 return is(tok::comment) &&
756 return isOneOf(tok::kw_throw, tok::kw_typeid, tok::kw_return,
757 tok::kw_sizeof, tok::kw_alignof, tok::kw_alignas,
758 tok::kw_decltype, tok::kw_noexcept, tok::kw_static_assert,
761#include
"clang/Basic/TransformTypeTraits.def"
768 if (
isNot(tok::string_literal))
771 if (Content.starts_with(
"\"") || Content.starts_with(
"'"))
772 Content = Content.drop_front(1);
773 if (Content.ends_with(
"\"") || Content.ends_with(
"'"))
774 Content = Content.drop_back(1);
775 Content = Content.trim();
776 return Content.size() > 1 &&
777 (Content.back() ==
':' || Content.back() ==
'=');
813 while (
Tok &&
Tok->
is(tok::comment))
833 }
while (T && T->
isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
835 return T && T->
is(tok::kw_auto);
849 if (
is(tok::comment))
852 if (NamespaceTok && NamespaceTok->
isOneOf(tok::kw_inline, tok::kw_export))
854 return NamespaceTok &&
855 NamespaceTok->
isOneOf(tok::kw_namespace, TT_NamespaceMacro)
867 template <
typename A,
typename... Ts>
868 bool startsSequenceInternal(A K1, Ts... Tokens)
const {
869 if (
is(tok::comment) &&
Next)
870 return Next->startsSequenceInternal(K1, Tokens...);
871 return is(K1) &&
Next &&
Next->startsSequenceInternal(Tokens...);
874 template <
typename A>
bool startsSequenceInternal(A K1)
const {
875 if (
is(tok::comment) &&
Next)
876 return Next->startsSequenceInternal(K1);
880 template <
typename A,
typename... Ts>
bool endsSequenceInternal(A K1)
const {
882 return Previous->endsSequenceInternal(K1);
886 template <
typename A,
typename... Ts>
887 bool endsSequenceInternal(A K1, Ts... Tokens)
const {
889 return Previous->endsSequenceInternal(K1, Tokens...);
943 bool DryRun)
override;
946 bool DryRun)
override;
950 Commas.push_back(
Token);
956 return Commas.back();
962 struct ColumnFormat {
978 const ColumnFormat *getColumnFormat(
unsigned RemainingCharacters)
const;
981 SmallVector<const FormatToken *, 8> Commas;
985 SmallVector<unsigned, 8> ItemLengths;
988 SmallVector<ColumnFormat, 4> Formats;
990 bool HasNestedBracedList;
1060 &IdentTable.
get(
"__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__");
1248 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
1255 CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>(
1272 VerilogExtraKeywords = std::unordered_set<IdentifierInfo *>(
1338 TableGenExtraKeywords = std::unordered_set<IdentifierInfo *>({
1631 bool AcceptIdentifierName =
true)
const {
1639 case tok::kw_continue:
1641 case tok::kw_default:
1642 case tok::kw_delete:
1646 case tok::kw_export:
1650 case tok::kw_import:
1651 case tok::kw_module:
1653 case tok::kw_private:
1654 case tok::kw_protected:
1655 case tok::kw_public:
1656 case tok::kw_return:
1657 case tok::kw_static:
1658 case tok::kw_switch:
1663 case tok::kw_typeof:
1668 case tok::identifier: {
1671 bool IsPseudoKeyword =
1673 JsExtraKeywords.end();
1674 return AcceptIdentifierName || !IsPseudoKeyword;
1684#define KEYWORD(X, Y) case tok::kw_##X:
1685#include "clang/Basic/TokenKinds.def"
1698 switch (Tok.Tok.getKind()) {
1706 case tok::kw_continue:
1707 case tok::kw_default:
1709 case tok::kw_double:
1712 case tok::kw_explicit:
1713 case tok::kw_extern:
1721 case tok::kw_namespace:
1723 case tok::kw_operator:
1724 case tok::kw_private:
1725 case tok::kw_protected:
1726 case tok::kw_public:
1727 case tok::kw_return:
1729 case tok::kw_sizeof:
1730 case tok::kw_static:
1731 case tok::kw_struct:
1732 case tok::kw_switch:
1737 case tok::kw_typeof:
1739 case tok::kw_virtual:
1741 case tok::kw_volatile:
1745 return Tok.is(tok::identifier) &&
1746 CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
1747 CSharpExtraKeywords.end();
1761 case tok::kw_continue:
1762 case tok::kw_default:
1764 case tok::kw_extern:
1769 case tok::kw_restrict:
1770 case tok::kw_signed:
1771 case tok::kw_static:
1772 case tok::kw_struct:
1773 case tok::kw_typedef:
1775 case tok::kw_unsigned:
1776 case tok::kw_virtual:
1779 case tok::identifier:
1782 VerilogExtraKeywords.end();
1793 auto Info = Tok.Tok.getIdentifierInfo();
1796 switch (Info->getPPKeywordID()) {
1797 case tok::pp_define:
1801 case tok::pp_ifndef:
1802 case tok::pp_include:
1804 case tok::pp_pragma:
1856 return Tok.
is(TT_CaseLabelColon) ||
1857 (Tok.
is(tok::kw_default) &&
1871 case tok::kw_extern:
1872 case tok::kw_signed:
1873 case tok::kw_static:
1874 case tok::kw_unsigned:
1875 case tok::kw_virtual:
1877 case tok::identifier:
1905 return Tok.
is(tok::identifier) &&
1907 TableGenExtraKeywords.end();
1913 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
1916 std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
1919 std::unordered_set<IdentifierInfo *> VerilogExtraKeywords;
1922 std::unordered_set<IdentifierInfo *> TableGenExtraKeywords;
1926 return FormatTok.is(tok::comment) && !FormatTok.TokenText.starts_with(
"/*");
1934 const FormatToken *MinColumnToken) {
1937 unsigned MinContinueColumn =
1938 MinColumnToken->OriginalColumn + (
isLineComment(*MinColumnToken) ? 0 : 1);
1939 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
1941 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.