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(AfterPPDirective) \
29 TYPE(ArrayInitializerLSquare) \
30 TYPE(ArraySubscriptLSquare) \
31 TYPE(AttributeColon) \
32 TYPE(AttributeLParen) \
33 TYPE(AttributeMacro) \
34 TYPE(AttributeRParen) \
35 TYPE(AttributeSquare) \
36 TYPE(BinaryOperator) \
41 TYPE(BracedListLBrace) \
42 TYPE(CaseLabelArrow) \
44 TYPE(CaseLabelColon) \
50 TYPE(CompoundRequirementLBrace) \
52 TYPE(ConditionalExpr) \
54 TYPE(ConditionLParen) \
55 TYPE(ConflictAlternative) \
59 TYPE(ControlStatementLBrace) \
60 TYPE(ControlStatementRBrace) \
62 TYPE(CSharpGenericTypeConstraint) \
63 TYPE(CSharpGenericTypeConstraintColon) \
64 TYPE(CSharpGenericTypeConstraintComma) \
65 TYPE(CSharpNamedArgumentColon) \
66 TYPE(CSharpNullable) \
67 TYPE(CSharpNullConditionalLSquare) \
68 TYPE(CSharpStringLiteral) \
69 TYPE(CtorInitializerColon) \
70 TYPE(CtorInitializerComma) \
71 TYPE(CtorDtorDeclName) \
72 TYPE(DesignatedInitializerLSquare) \
73 TYPE(DesignatedInitializerPeriod) \
82 TYPE(FunctionAnnotationRParen) \
83 TYPE(FunctionDeclarationName) \
84 TYPE(FunctionDeclarationLParen) \
85 TYPE(FunctionLBrace) \
86 TYPE(FunctionLikeMacro) \
87 TYPE(FunctionLikeOrFreestandingMacro) \
88 TYPE(FunctionTypeLParen) \
90 TYPE(GenericSelectionColon) \
92 TYPE(GotoLabelColon) \
94 TYPE(ImplicitStringLiteral) \
95 TYPE(InheritanceColon) \
96 TYPE(InheritanceComma) \
97 TYPE(InlineASMBrace) \
98 TYPE(InlineASMColon) \
99 TYPE(InlineASMSymbolicNameLSquare) \
100 TYPE(JavaAnnotation) \
101 TYPE(JsAndAndEqual) \
102 TYPE(JsComputedPropertyName) \
103 TYPE(JsExponentiation) \
104 TYPE(JsExponentiationEqual) \
105 TYPE(JsPipePipeEqual) \
106 TYPE(JsPrivateIdentifier) \
108 TYPE(JsTypeOperator) \
109 TYPE(JsTypeOptionalQuestion) \
111 TYPE(LambdaDefinitionLParen) \
113 TYPE(LambdaLSquare) \
114 TYPE(LeadingJavaAnnotation) \
116 TYPE(MacroBlockBegin) \
117 TYPE(MacroBlockEnd) \
118 TYPE(ModulePartitionColon) \
119 TYPE(NamespaceLBrace) \
120 TYPE(NamespaceMacro) \
121 TYPE(NamespaceRBrace) \
122 TYPE(NonNullAssertion) \
123 TYPE(NullCoalescingEqual) \
124 TYPE(NullCoalescingOperator) \
125 TYPE(NullPropagatingOperator) \
126 TYPE(ObjCBlockLBrace) \
127 TYPE(ObjCBlockLParen) \
130 TYPE(ObjCMethodExpr) \
131 TYPE(ObjCMethodSpecifier) \
133 TYPE(ObjCStringLiteral) \
134 TYPE(OverloadedOperator) \
135 TYPE(OverloadedOperatorLParen) \
136 TYPE(PointerOrReference) \
137 TYPE(ProtoExtensionLSquare) \
138 TYPE(PureVirtualSpecifier) \
139 TYPE(RangeBasedForLoopColon) \
143 TYPE(RequiresClause) \
144 TYPE(RequiresClauseInARequiresExpression) \
145 TYPE(RequiresExpression) \
146 TYPE(RequiresExpressionLBrace) \
147 TYPE(RequiresExpressionLParen) \
150 TYPE(StatementAttributeLikeMacro) \
151 TYPE(StatementMacro) \
156 TYPE(StringInConcatenation) \
159 TYPE(StructuredBindingLSquare) \
160 TYPE(SwitchExpressionLabel) \
161 TYPE(SwitchExpressionLBrace) \
162 TYPE(TableGenBangOperator) \
163 TYPE(TableGenCondOperator) \
164 TYPE(TableGenCondOperatorColon) \
165 TYPE(TableGenCondOperatorComma) \
166 TYPE(TableGenDAGArgCloser) \
167 TYPE(TableGenDAGArgListColon) \
168 TYPE(TableGenDAGArgListColonToAlign) \
169 TYPE(TableGenDAGArgListComma) \
170 TYPE(TableGenDAGArgListCommaToBreak) \
171 TYPE(TableGenDAGArgOpener) \
172 TYPE(TableGenDAGArgOpenerToBreak) \
173 TYPE(TableGenDAGArgOperatorID) \
174 TYPE(TableGenDAGArgOperatorToBreak) \
175 TYPE(TableGenListCloser) \
176 TYPE(TableGenListOpener) \
177 TYPE(TableGenMultiLineString) \
178 TYPE(TableGenTrailingPasteOperator) \
179 TYPE(TableGenValueSuffix) \
180 TYPE(TemplateCloser) \
181 TYPE(TemplateOpener) \
182 TYPE(TemplateString) \
183 TYPE(TrailingAnnotation) \
184 TYPE(TrailingReturnArrow) \
185 TYPE(TrailingUnaryOperator) \
186 TYPE(TypeDeclarationParen) \
189 TYPE(TypenameMacro) \
190 TYPE(UnaryOperator) \
193 TYPE(UntouchableMacroFunc) \
194 TYPE(VariableTemplate) \
196 TYPE(VerilogAssignComma) \
198 TYPE(VerilogBlockLabelColon) \
202 TYPE(VerilogDimensionedTypeName) \
204 TYPE(VerilogInstancePortComma) \
205 TYPE(VerilogInstancePortLParen) \
208 TYPE(VerilogMultiLineListLParen) \
210 TYPE(VerilogNumberBase) \
212 TYPE(VerilogStrength) \
214 TYPE(VerilogTableItem) \
216 TYPE(VerilogTypeComma) \
222#define TYPE(X) TT_##X,
332 std::shared_ptr<TokenRole>
Role;
395 unsigned BlockKind : 2;
403 assert(
getBlockKind() == BBK &&
"BraceBlockKind overflow!");
408 unsigned Decision : 2;
416 assert(
getDecision() ==
D &&
"FormatDecision overflow!");
421 unsigned PackingKind : 2;
429 assert(
getPackingKind() == K &&
"ParameterPackingKind overflow!");
433 unsigned TypeIsFinalized : 1;
446 assert((!TypeIsFinalized ||
T ==
Type) &&
447 "Please use overwriteFixedType to change a fixed type.");
458 TypeIsFinalized =
true;
463 TypeIsFinalized =
false;
613 std::optional<MacroExpansion>
MacroCtx;
639 template <
typename A,
typename B>
bool isOneOf(A K1, B K2)
const {
642 template <
typename A,
typename B,
typename... Ts>
643 bool isOneOf(A K1, B K2, Ts... Ks)
const {
648 bool isIf(
bool AllowConstexprMacro =
true)
const {
649 return is(tok::kw_if) ||
endsSequence(tok::kw_constexpr, tok::kw_if) ||
650 (
endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro);
663 template <
typename A,
typename... Ts>
665 return startsSequenceInternal(K1, Tokens...);
674 template <
typename A,
typename... Ts>
676 return endsSequenceInternal(K1, Tokens...);
682 return isOneOf(tok::kw___attribute, tok::kw___declspec, TT_AttributeMacro);
686 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private);
695 return NextNonComment && NextNonComment->is(tok::colon);
699 return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile,
700 tok::kw__Nonnull, tok::kw__Nullable,
701 tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64,
702 tok::kw___funcref) ||
710 return is(tok::at) &&
Next &&
711 Next->
isOneOf(tok::objc_public, tok::objc_protected,
712 tok::objc_package, tok::objc_private);
720 const auto Qualifier =
TokenText.substr(2);
721 return Qualifier ==
"autoreleasing" || Qualifier ==
"strong" ||
722 Qualifier ==
"weak" || Qualifier ==
"unsafe_unretained";
728 if (
is(TT_TemplateString) &&
TokenText.ends_with(
"${"))
730 if (
is(TT_DictLiteral) &&
is(tok::less))
732 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
738 if (
is(TT_TemplateString) &&
TokenText.starts_with(
"}"))
740 if (
is(TT_DictLiteral) &&
is(tok::greater))
742 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
748 return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
749 !
isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
750 TT_LambdaArrow, TT_LeadingJavaAnnotation);
754 return isOneOf(tok::star, tok::amp, tok::ampamp);
758 return isOneOf(tok::kw_new, tok::kw_delete);
766 case tok::minusminus:
770 case tok::kw_alignof:
783 return is(tok::comment) &&
793 return isOneOf(tok::kw_throw, tok::kw_typeid, tok::kw_return,
794 tok::kw_sizeof, tok::kw_alignof, tok::kw_alignas,
795 tok::kw_decltype, tok::kw_noexcept, tok::kw_static_assert,
798#include
"clang/Basic/TransformTypeTraits.def"
805 if (
isNot(tok::string_literal))
808 if (Content.starts_with(
"\"") || Content.starts_with(
"'"))
809 Content = Content.drop_front(1);
810 if (Content.ends_with(
"\"") || Content.ends_with(
"'"))
811 Content = Content.drop_back(1);
812 Content = Content.trim();
813 return Content.size() > 1 &&
814 (Content.back() ==
':' || Content.back() ==
'=');
850 while (
Tok &&
Tok->
is(tok::comment))
865 if (!IsCpp ||
isNot(tok::l_square))
869 T =
T->getPreviousNonComment();
870 }
while (
T &&
T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
872 return T &&
T->is(tok::kw_auto);
886 if (
is(tok::comment))
889 if (NamespaceTok && NamespaceTok->
isOneOf(tok::kw_inline, tok::kw_export))
891 return NamespaceTok &&
892 NamespaceTok->
isOneOf(tok::kw_namespace, TT_NamespaceMacro)
904 template <
typename A,
typename... Ts>
905 bool startsSequenceInternal(A K1, Ts... Tokens)
const {
906 if (
is(tok::comment) &&
Next)
907 return Next->startsSequenceInternal(K1, Tokens...);
908 return is(K1) &&
Next &&
Next->startsSequenceInternal(Tokens...);
911 template <
typename A>
bool startsSequenceInternal(A K1)
const {
912 if (
is(tok::comment) &&
Next)
913 return Next->startsSequenceInternal(K1);
917 template <
typename A,
typename... Ts>
bool endsSequenceInternal(A K1)
const {
919 return Previous->endsSequenceInternal(K1);
923 template <
typename A,
typename... Ts>
924 bool endsSequenceInternal(A K1, Ts... Tokens)
const {
926 return Previous->endsSequenceInternal(K1, Tokens...);
980 bool DryRun)
override;
983 bool DryRun)
override;
987 Commas.push_back(
Token);
993 return Commas.back();
999 struct ColumnFormat {
1004 unsigned TotalWidth;
1015 const ColumnFormat *getColumnFormat(
unsigned RemainingCharacters)
const;
1018 SmallVector<const FormatToken *, 8> Commas;
1022 SmallVector<unsigned, 8> ItemLengths;
1025 SmallVector<ColumnFormat, 4> Formats;
1027 bool HasNestedBracedList;
1098 &IdentTable.
get(
"__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__");
1286 JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
1293 CSharpExtraKeywords = JsExtraKeywords;
1294 CSharpExtraKeywords.insert(
1305 VerilogExtraKeywords = std::unordered_set<IdentifierInfo *>(
1350 TableGenExtraKeywords = std::unordered_set<IdentifierInfo *>({
1645 bool AcceptIdentifierName =
true)
const {
1655 case tok::kw_continue:
1657 case tok::kw_default:
1658 case tok::kw_delete:
1662 case tok::kw_export:
1666 case tok::kw_import:
1667 case tok::kw_module:
1669 case tok::kw_return:
1670 case tok::kw_static:
1671 case tok::kw_switch:
1676 case tok::kw_typeof:
1681 case tok::identifier: {
1684 bool IsPseudoKeyword =
1686 JsExtraKeywords.end();
1687 return AcceptIdentifierName || !IsPseudoKeyword;
1697#define KEYWORD(X, Y) case tok::kw_##X:
1698#include "clang/Basic/TokenKinds.def"
1711 if (Tok.isAccessSpecifierKeyword())
1713 switch (Tok.Tok.getKind()) {
1721 case tok::kw_continue:
1722 case tok::kw_default:
1724 case tok::kw_double:
1727 case tok::kw_explicit:
1728 case tok::kw_extern:
1736 case tok::kw_namespace:
1738 case tok::kw_operator:
1739 case tok::kw_return:
1741 case tok::kw_sizeof:
1742 case tok::kw_static:
1743 case tok::kw_struct:
1744 case tok::kw_switch:
1749 case tok::kw_typeof:
1751 case tok::kw_virtual:
1753 case tok::kw_volatile:
1757 return Tok.is(tok::identifier) &&
1758 CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
1759 CSharpExtraKeywords.end();
1777 case tok::kw_continue:
1778 case tok::kw_default:
1780 case tok::kw_extern:
1785 case tok::kw_restrict:
1786 case tok::kw_signed:
1787 case tok::kw_static:
1788 case tok::kw_struct:
1789 case tok::kw_typedef:
1791 case tok::kw_unsigned:
1792 case tok::kw_virtual:
1795 case tok::identifier:
1798 VerilogExtraKeywords.end();
1809 auto Info = Tok.Tok.getIdentifierInfo();
1812 switch (Info->getPPKeywordID()) {
1813 case tok::pp_define:
1817 case tok::pp_ifndef:
1818 case tok::pp_include:
1820 case tok::pp_pragma:
1841 const auto *Prev = Tok.getPreviousNonComment();
1876 return Tok.
is(TT_CaseLabelColon) ||
1877 (Tok.
is(tok::kw_default) &&
1891 case tok::kw_extern:
1892 case tok::kw_signed:
1893 case tok::kw_static:
1894 case tok::kw_unsigned:
1895 case tok::kw_virtual:
1897 case tok::identifier:
1925 return Tok.
is(tok::identifier) &&
1927 TableGenExtraKeywords.end();
1933 std::unordered_set<IdentifierInfo *> JsExtraKeywords;
1936 std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
1939 std::unordered_set<IdentifierInfo *> VerilogExtraKeywords;
1942 std::unordered_set<IdentifierInfo *> TableGenExtraKeywords;
1946 return FormatTok.is(tok::comment) && !FormatTok.TokenText.starts_with(
"/*");
1954 const FormatToken *MinColumnToken) {
1957 unsigned MinContinueColumn =
1958 MinColumnToken->OriginalColumn + (
isLineComment(*MinColumnToken) ? 0 : 1);
1959 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
1961 FormatTok.OriginalColumn >= MinContinueColumn;
enum clang::sema::@1840::IndirectLocalPathEntry::EntryKind Kind
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
tok::ObjCKeywordKind getObjCKeywordID() const
Return the ObjC keyword kind.
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
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