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) \
41 TYPE(CaseLabelArrow) \
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(SwitchExpressionLabel) \
153 TYPE(SwitchExpressionLBrace) \
154 TYPE(TableGenBangOperator) \
155 TYPE(TableGenCondOperator) \
156 TYPE(TableGenCondOperatorColon) \
157 TYPE(TableGenCondOperatorComma) \
158 TYPE(TableGenDAGArgCloser) \
159 TYPE(TableGenDAGArgListColon) \
160 TYPE(TableGenDAGArgListColonToAlign) \
161 TYPE(TableGenDAGArgListComma) \
162 TYPE(TableGenDAGArgListCommaToBreak) \
163 TYPE(TableGenDAGArgOpener) \
164 TYPE(TableGenDAGArgOpenerToBreak) \
165 TYPE(TableGenDAGArgOperatorID) \
166 TYPE(TableGenDAGArgOperatorToBreak) \
167 TYPE(TableGenListCloser) \
168 TYPE(TableGenListOpener) \
169 TYPE(TableGenMultiLineString) \
170 TYPE(TableGenTrailingPasteOperator) \
171 TYPE(TableGenValueSuffix) \
172 TYPE(TemplateCloser) \
173 TYPE(TemplateOpener) \
174 TYPE(TemplateString) \
175 TYPE(TrailingAnnotation) \
176 TYPE(TrailingReturnArrow) \
177 TYPE(TrailingUnaryOperator) \
178 TYPE(TypeDeclarationParen) \
180 TYPE(TypenameMacro) \
181 TYPE(UnaryOperator) \
184 TYPE(UntouchableMacroFunc) \
186 TYPE(VerilogAssignComma) \
188 TYPE(VerilogBlockLabelColon) \
192 TYPE(VerilogDimensionedTypeName) \
194 TYPE(VerilogInstancePortComma) \
195 TYPE(VerilogInstancePortLParen) \
198 TYPE(VerilogMultiLineListLParen) \
200 TYPE(VerilogNumberBase) \
202 TYPE(VerilogStrength) \
204 TYPE(VerilogTableItem) \
206 TYPE(VerilogTypeComma) \
212#define TYPE(X) TT_##X,
322 std::shared_ptr<TokenRole>
Role;
385 unsigned BlockKind : 2;
393 assert(
getBlockKind() == BBK &&
"BraceBlockKind overflow!");
398 unsigned Decision : 2;
406 assert(
getDecision() == D &&
"FormatDecision overflow!");
411 unsigned PackingKind : 2;
419 assert(
getPackingKind() == K &&
"ParameterPackingKind overflow!");
423 unsigned TypeIsFinalized : 1;
436 assert((!TypeIsFinalized ||
T ==
Type) &&
437 "Please use overwriteFixedType to change a fixed type.");
448 TypeIsFinalized =
true;
453 TypeIsFinalized =
false;
597 std::optional<MacroExpansion>
MacroCtx;
620 template <
typename A,
typename B>
bool isOneOf(A K1, B K2)
const {
623 template <
typename A,
typename B,
typename... Ts>
624 bool isOneOf(A K1, B K2, Ts... Ks)
const {
627 template <
typename T>
bool isNot(
T Kind)
const {
return !
is(Kind); }
629 bool isIf(
bool AllowConstexprMacro =
true)
const {
630 return is(tok::kw_if) ||
endsSequence(tok::kw_constexpr, tok::kw_if) ||
631 (
endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro);
644 template <
typename A,
typename... Ts>
646 return startsSequenceInternal(K1, Tokens...);
655 template <
typename A,
typename... Ts>
657 return endsSequenceInternal(K1, Tokens...);
663 return isOneOf(tok::kw___attribute, tok::kw___declspec, TT_AttributeMacro);
671 if (!
isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
676 return NextNonComment && NextNonComment->is(tok::colon);
680 return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile,
681 tok::kw__Nonnull, tok::kw__Nullable,
682 tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64,
683 tok::kw___funcref) ||
691 return is(tok::at) &&
Next &&
701 if (
is(TT_TemplateString) &&
TokenText.ends_with(
"${"))
703 if (
is(TT_DictLiteral) &&
is(tok::less))
705 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
711 if (
is(TT_TemplateString) &&
TokenText.starts_with(
"}"))
713 if (
is(TT_DictLiteral) &&
is(tok::greater))
715 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
721 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
722 !
isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
723 TT_LeadingJavaAnnotation);
727 return isOneOf(tok::star, tok::amp, tok::ampamp);
735 case tok::minusminus:
739 case tok::kw_alignof:
752 return is(tok::comment) &&
762 return isOneOf(tok::kw_throw, tok::kw_typeid, tok::kw_return,
763 tok::kw_sizeof, tok::kw_alignof, tok::kw_alignas,
764 tok::kw_decltype, tok::kw_noexcept, tok::kw_static_assert,
767#include
"clang/Basic/TransformTypeTraits.def"
774 if (
isNot(tok::string_literal))
777 if (Content.starts_with(
"\"") || Content.starts_with(
"'"))
778 Content = Content.drop_front(1);
779 if (Content.ends_with(
"\"") || Content.ends_with(
"'"))
780 Content = Content.drop_back(1);
781 Content = Content.trim();
782 return Content.size() > 1 &&
783 (Content.back() ==
':' || Content.back() ==
'=');
819 while (
Tok &&
Tok->
is(tok::comment))
834 if (!IsCpp ||
isNot(tok::l_square))
838 T =
T->getPreviousNonComment();
839 }
while (
T &&
T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
841 return T &&
T->is(tok::kw_auto);
855 if (
is(tok::comment))
858 if (NamespaceTok && NamespaceTok->
isOneOf(tok::kw_inline, tok::kw_export))
860 return NamespaceTok &&
861 NamespaceTok->
isOneOf(tok::kw_namespace, TT_NamespaceMacro)
873 template <
typename A,
typename... Ts>
874 bool startsSequenceInternal(A K1, Ts... Tokens)
const {
875 if (
is(tok::comment) &&
Next)
876 return Next->startsSequenceInternal(K1, Tokens...);
877 return is(K1) &&
Next &&
Next->startsSequenceInternal(Tokens...);
880 template <
typename A>
bool startsSequenceInternal(A K1)
const {
881 if (
is(tok::comment) &&
Next)
882 return Next->startsSequenceInternal(K1);
886 template <
typename A,
typename... Ts>
bool endsSequenceInternal(A K1)
const {
888 return Previous->endsSequenceInternal(K1);
892 template <
typename A,
typename... Ts>
893 bool endsSequenceInternal(A K1, Ts... Tokens)
const {
895 return Previous->endsSequenceInternal(K1, Tokens...);
949 bool DryRun)
override;
952 bool DryRun)
override;
956 Commas.push_back(
Token);
962 return Commas.back();
968 struct ColumnFormat {
984 const ColumnFormat *getColumnFormat(
unsigned RemainingCharacters)
const;
987 SmallVector<const FormatToken *, 8> Commas;
991 SmallVector<unsigned, 8> ItemLengths;
994 SmallVector<ColumnFormat, 4> Formats;
996 bool HasNestedBracedList;
1066 &IdentTable.
get(
"__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__");
1254 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
1261 CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>(
1278 VerilogExtraKeywords = std::unordered_set<IdentifierInfo *>(
1344 TableGenExtraKeywords = std::unordered_set<IdentifierInfo *>({
1637 bool AcceptIdentifierName =
true)
const {
1645 case tok::kw_continue:
1647 case tok::kw_default:
1648 case tok::kw_delete:
1652 case tok::kw_export:
1656 case tok::kw_import:
1657 case tok::kw_module:
1659 case tok::kw_private:
1660 case tok::kw_protected:
1661 case tok::kw_public:
1662 case tok::kw_return:
1663 case tok::kw_static:
1664 case tok::kw_switch:
1669 case tok::kw_typeof:
1674 case tok::identifier: {
1677 bool IsPseudoKeyword =
1679 JsExtraKeywords.end();
1680 return AcceptIdentifierName || !IsPseudoKeyword;
1690#define KEYWORD(X, Y) case tok::kw_##X:
1691#include "clang/Basic/TokenKinds.def"
1704 switch (Tok.Tok.getKind()) {
1712 case tok::kw_continue:
1713 case tok::kw_default:
1715 case tok::kw_double:
1718 case tok::kw_explicit:
1719 case tok::kw_extern:
1727 case tok::kw_namespace:
1729 case tok::kw_operator:
1730 case tok::kw_private:
1731 case tok::kw_protected:
1732 case tok::kw_public:
1733 case tok::kw_return:
1735 case tok::kw_sizeof:
1736 case tok::kw_static:
1737 case tok::kw_struct:
1738 case tok::kw_switch:
1743 case tok::kw_typeof:
1745 case tok::kw_virtual:
1747 case tok::kw_volatile:
1751 return Tok.is(tok::identifier) &&
1752 CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
1753 CSharpExtraKeywords.end();
1771 case tok::kw_continue:
1772 case tok::kw_default:
1774 case tok::kw_extern:
1779 case tok::kw_restrict:
1780 case tok::kw_signed:
1781 case tok::kw_static:
1782 case tok::kw_struct:
1783 case tok::kw_typedef:
1785 case tok::kw_unsigned:
1786 case tok::kw_virtual:
1789 case tok::identifier:
1792 VerilogExtraKeywords.end();
1803 auto Info = Tok.Tok.getIdentifierInfo();
1806 switch (Info->getPPKeywordID()) {
1807 case tok::pp_define:
1811 case tok::pp_ifndef:
1812 case tok::pp_include:
1814 case tok::pp_pragma:
1866 return Tok.
is(TT_CaseLabelColon) ||
1867 (Tok.
is(tok::kw_default) &&
1881 case tok::kw_extern:
1882 case tok::kw_signed:
1883 case tok::kw_static:
1884 case tok::kw_unsigned:
1885 case tok::kw_virtual:
1887 case tok::identifier:
1915 return Tok.
is(tok::identifier) &&
1917 TableGenExtraKeywords.end();
1923 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
1926 std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
1929 std::unordered_set<IdentifierInfo *> VerilogExtraKeywords;
1932 std::unordered_set<IdentifierInfo *> TableGenExtraKeywords;
1936 return FormatTok.is(tok::comment) && !FormatTok.TokenText.starts_with(
"/*");
1944 const FormatToken *MinColumnToken) {
1947 unsigned MinContinueColumn =
1948 MinColumnToken->OriginalColumn + (
isLineComment(*MinColumnToken) ? 0 : 1);
1949 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
1951 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.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
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