FormatToken.h 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  1. //===--- FormatToken.h - Format C++ code ------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file contains the declaration of the FormatToken, a wrapper
  11. /// around Token with additional information related to formatting.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
  15. #define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
  16. #include "clang/Basic/IdentifierTable.h"
  17. #include "clang/Basic/OperatorPrecedence.h"
  18. #include "clang/Format/Format.h"
  19. #include "clang/Lex/Lexer.h"
  20. #include <memory>
  21. #include <optional>
  22. #include <unordered_set>
  23. namespace clang {
  24. namespace format {
  25. #define LIST_TOKEN_TYPES \
  26. TYPE(ArrayInitializerLSquare) \
  27. TYPE(ArraySubscriptLSquare) \
  28. TYPE(AttributeColon) \
  29. TYPE(AttributeMacro) \
  30. TYPE(AttributeParen) \
  31. TYPE(AttributeSquare) \
  32. TYPE(BinaryOperator) \
  33. TYPE(BitFieldColon) \
  34. TYPE(BlockComment) \
  35. TYPE(BracedListLBrace) \
  36. TYPE(CastRParen) \
  37. TYPE(ClassLBrace) \
  38. TYPE(CompoundRequirementLBrace) \
  39. /* ternary ?: expression */ \
  40. TYPE(ConditionalExpr) \
  41. /* the condition in an if statement */ \
  42. TYPE(ConditionLParen) \
  43. TYPE(ConflictAlternative) \
  44. TYPE(ConflictEnd) \
  45. TYPE(ConflictStart) \
  46. /* l_brace of if/for/while */ \
  47. TYPE(ControlStatementLBrace) \
  48. TYPE(CppCastLParen) \
  49. TYPE(CSharpGenericTypeConstraint) \
  50. TYPE(CSharpGenericTypeConstraintColon) \
  51. TYPE(CSharpGenericTypeConstraintComma) \
  52. TYPE(CSharpNamedArgumentColon) \
  53. TYPE(CSharpNullable) \
  54. TYPE(CSharpNullConditionalLSquare) \
  55. TYPE(CSharpStringLiteral) \
  56. TYPE(CtorInitializerColon) \
  57. TYPE(CtorInitializerComma) \
  58. TYPE(DesignatedInitializerLSquare) \
  59. TYPE(DesignatedInitializerPeriod) \
  60. TYPE(DictLiteral) \
  61. TYPE(ElseLBrace) \
  62. TYPE(EnumLBrace) \
  63. TYPE(FatArrow) \
  64. TYPE(ForEachMacro) \
  65. TYPE(FunctionAnnotationRParen) \
  66. TYPE(FunctionDeclarationName) \
  67. TYPE(FunctionLBrace) \
  68. TYPE(FunctionLikeOrFreestandingMacro) \
  69. TYPE(FunctionTypeLParen) \
  70. /* The colons as part of a C11 _Generic selection */ \
  71. TYPE(GenericSelectionColon) \
  72. /* The colon at the end of a goto label or a case label. Currently only used \
  73. * for Verilog. */ \
  74. TYPE(GotoLabelColon) \
  75. TYPE(IfMacro) \
  76. TYPE(ImplicitStringLiteral) \
  77. TYPE(InheritanceColon) \
  78. TYPE(InheritanceComma) \
  79. TYPE(InlineASMBrace) \
  80. TYPE(InlineASMColon) \
  81. TYPE(InlineASMSymbolicNameLSquare) \
  82. TYPE(JavaAnnotation) \
  83. TYPE(JsAndAndEqual) \
  84. TYPE(JsComputedPropertyName) \
  85. TYPE(JsExponentiation) \
  86. TYPE(JsExponentiationEqual) \
  87. TYPE(JsPipePipeEqual) \
  88. TYPE(JsPrivateIdentifier) \
  89. TYPE(JsTypeColon) \
  90. TYPE(JsTypeOperator) \
  91. TYPE(JsTypeOptionalQuestion) \
  92. TYPE(LambdaArrow) \
  93. TYPE(LambdaLBrace) \
  94. TYPE(LambdaLSquare) \
  95. TYPE(LeadingJavaAnnotation) \
  96. TYPE(LineComment) \
  97. TYPE(MacroBlockBegin) \
  98. TYPE(MacroBlockEnd) \
  99. TYPE(ModulePartitionColon) \
  100. TYPE(NamespaceMacro) \
  101. TYPE(NonNullAssertion) \
  102. TYPE(NullCoalescingEqual) \
  103. TYPE(NullCoalescingOperator) \
  104. TYPE(NullPropagatingOperator) \
  105. TYPE(ObjCBlockLBrace) \
  106. TYPE(ObjCBlockLParen) \
  107. TYPE(ObjCDecl) \
  108. TYPE(ObjCForIn) \
  109. TYPE(ObjCMethodExpr) \
  110. TYPE(ObjCMethodSpecifier) \
  111. TYPE(ObjCProperty) \
  112. TYPE(ObjCStringLiteral) \
  113. TYPE(OverloadedOperator) \
  114. TYPE(OverloadedOperatorLParen) \
  115. TYPE(PointerOrReference) \
  116. TYPE(ProtoExtensionLSquare) \
  117. TYPE(PureVirtualSpecifier) \
  118. TYPE(RangeBasedForLoopColon) \
  119. TYPE(RecordLBrace) \
  120. TYPE(RegexLiteral) \
  121. TYPE(RequiresClause) \
  122. TYPE(RequiresClauseInARequiresExpression) \
  123. TYPE(RequiresExpression) \
  124. TYPE(RequiresExpressionLBrace) \
  125. TYPE(RequiresExpressionLParen) \
  126. TYPE(SelectorName) \
  127. TYPE(StartOfName) \
  128. TYPE(StatementAttributeLikeMacro) \
  129. TYPE(StatementMacro) \
  130. TYPE(StructLBrace) \
  131. TYPE(StructuredBindingLSquare) \
  132. TYPE(TemplateCloser) \
  133. TYPE(TemplateOpener) \
  134. TYPE(TemplateString) \
  135. TYPE(TrailingAnnotation) \
  136. TYPE(TrailingReturnArrow) \
  137. TYPE(TrailingUnaryOperator) \
  138. TYPE(TypeDeclarationParen) \
  139. TYPE(TypenameMacro) \
  140. TYPE(UnaryOperator) \
  141. TYPE(UnionLBrace) \
  142. TYPE(UntouchableMacroFunc) \
  143. /* like in begin : block */ \
  144. TYPE(VerilogBlockLabelColon) \
  145. /* The square bracket for the dimension part of the type name. \
  146. * In 'logic [1:0] x[1:0]', only the first '['. This way we can have space \
  147. * before the first bracket but not the second. */ \
  148. TYPE(VerilogDimensionedTypeName) \
  149. /* for the base in a number literal, not including the quote */ \
  150. TYPE(VerilogNumberBase) \
  151. /* Things inside the table in user-defined primitives. */ \
  152. TYPE(VerilogTableItem) \
  153. TYPE(Unknown)
  154. /// Determines the semantic type of a syntactic token, e.g. whether "<" is a
  155. /// template opener or binary operator.
  156. enum TokenType : uint8_t {
  157. #define TYPE(X) TT_##X,
  158. LIST_TOKEN_TYPES
  159. #undef TYPE
  160. NUM_TOKEN_TYPES
  161. };
  162. /// Determines the name of a token type.
  163. const char *getTokenTypeName(TokenType Type);
  164. // Represents what type of block a set of braces open.
  165. enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit };
  166. // The packing kind of a function's parameters.
  167. enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive };
  168. enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break };
  169. /// Roles a token can take in a configured macro expansion.
  170. enum MacroRole {
  171. /// The token was expanded from a macro argument when formatting the expanded
  172. /// token sequence.
  173. MR_ExpandedArg,
  174. /// The token is part of a macro argument that was previously formatted as
  175. /// expansion when formatting the unexpanded macro call.
  176. MR_UnexpandedArg,
  177. /// The token was expanded from a macro definition, and is not visible as part
  178. /// of the macro call.
  179. MR_Hidden,
  180. };
  181. struct FormatToken;
  182. /// Contains information on the token's role in a macro expansion.
  183. ///
  184. /// Given the following definitions:
  185. /// A(X) = [ X ]
  186. /// B(X) = < X >
  187. /// C(X) = X
  188. ///
  189. /// Consider the macro call:
  190. /// A({B(C(C(x)))}) -> [{<x>}]
  191. ///
  192. /// In this case, the tokens of the unexpanded macro call will have the
  193. /// following relevant entries in their macro context (note that formatting
  194. /// the unexpanded macro call happens *after* formatting the expanded macro
  195. /// call):
  196. /// A( { B( C( C(x) ) ) } )
  197. /// Role: NN U NN NN NNUN N N U N (N=None, U=UnexpandedArg)
  198. ///
  199. /// [ { < x > } ]
  200. /// Role: H E H E H E H (H=Hidden, E=ExpandedArg)
  201. /// ExpandedFrom[0]: A A A A A A A
  202. /// ExpandedFrom[1]: B B B
  203. /// ExpandedFrom[2]: C
  204. /// ExpandedFrom[3]: C
  205. /// StartOfExpansion: 1 0 1 2 0 0 0
  206. /// EndOfExpansion: 0 0 0 2 1 0 1
  207. struct MacroExpansion {
  208. MacroExpansion(MacroRole Role) : Role(Role) {}
  209. /// The token's role in the macro expansion.
  210. /// When formatting an expanded macro, all tokens that are part of macro
  211. /// arguments will be MR_ExpandedArg, while all tokens that are not visible in
  212. /// the macro call will be MR_Hidden.
  213. /// When formatting an unexpanded macro call, all tokens that are part of
  214. /// macro arguments will be MR_UnexpandedArg.
  215. MacroRole Role;
  216. /// The stack of macro call identifier tokens this token was expanded from.
  217. llvm::SmallVector<FormatToken *, 1> ExpandedFrom;
  218. /// The number of expansions of which this macro is the first entry.
  219. unsigned StartOfExpansion = 0;
  220. /// The number of currently open expansions in \c ExpandedFrom this macro is
  221. /// the last token in.
  222. unsigned EndOfExpansion = 0;
  223. };
  224. class TokenRole;
  225. class AnnotatedLine;
  226. /// A wrapper around a \c Token storing information about the
  227. /// whitespace characters preceding it.
  228. struct FormatToken {
  229. FormatToken()
  230. : HasUnescapedNewline(false), IsMultiline(false), IsFirst(false),
  231. MustBreakBefore(false), IsUnterminatedLiteral(false),
  232. CanBreakBefore(false), ClosesTemplateDeclaration(false),
  233. StartsBinaryExpression(false), EndsBinaryExpression(false),
  234. PartOfMultiVariableDeclStmt(false), ContinuesLineCommentSection(false),
  235. Finalized(false), ClosesRequiresClause(false),
  236. EndsCppAttributeGroup(false), BlockKind(BK_Unknown),
  237. Decision(FD_Unformatted), PackingKind(PPK_Inconclusive),
  238. TypeIsFinalized(false), Type(TT_Unknown) {}
  239. /// The \c Token.
  240. Token Tok;
  241. /// The raw text of the token.
  242. ///
  243. /// Contains the raw token text without leading whitespace and without leading
  244. /// escaped newlines.
  245. StringRef TokenText;
  246. /// A token can have a special role that can carry extra information
  247. /// about the token's formatting.
  248. /// FIXME: Make FormatToken for parsing and AnnotatedToken two different
  249. /// classes and make this a unique_ptr in the AnnotatedToken class.
  250. std::shared_ptr<TokenRole> Role;
  251. /// The range of the whitespace immediately preceding the \c Token.
  252. SourceRange WhitespaceRange;
  253. /// Whether there is at least one unescaped newline before the \c
  254. /// Token.
  255. unsigned HasUnescapedNewline : 1;
  256. /// Whether the token text contains newlines (escaped or not).
  257. unsigned IsMultiline : 1;
  258. /// Indicates that this is the first token of the file.
  259. unsigned IsFirst : 1;
  260. /// Whether there must be a line break before this token.
  261. ///
  262. /// This happens for example when a preprocessor directive ended directly
  263. /// before the token.
  264. unsigned MustBreakBefore : 1;
  265. /// Set to \c true if this token is an unterminated literal.
  266. unsigned IsUnterminatedLiteral : 1;
  267. /// \c true if it is allowed to break before this token.
  268. unsigned CanBreakBefore : 1;
  269. /// \c true if this is the ">" of "template<..>".
  270. unsigned ClosesTemplateDeclaration : 1;
  271. /// \c true if this token starts a binary expression, i.e. has at least
  272. /// one fake l_paren with a precedence greater than prec::Unknown.
  273. unsigned StartsBinaryExpression : 1;
  274. /// \c true if this token ends a binary expression.
  275. unsigned EndsBinaryExpression : 1;
  276. /// Is this token part of a \c DeclStmt defining multiple variables?
  277. ///
  278. /// Only set if \c Type == \c TT_StartOfName.
  279. unsigned PartOfMultiVariableDeclStmt : 1;
  280. /// Does this line comment continue a line comment section?
  281. ///
  282. /// Only set to true if \c Type == \c TT_LineComment.
  283. unsigned ContinuesLineCommentSection : 1;
  284. /// If \c true, this token has been fully formatted (indented and
  285. /// potentially re-formatted inside), and we do not allow further formatting
  286. /// changes.
  287. unsigned Finalized : 1;
  288. /// \c true if this is the last token within requires clause.
  289. unsigned ClosesRequiresClause : 1;
  290. /// \c true if this token ends a group of C++ attributes.
  291. unsigned EndsCppAttributeGroup : 1;
  292. private:
  293. /// Contains the kind of block if this token is a brace.
  294. unsigned BlockKind : 2;
  295. public:
  296. BraceBlockKind getBlockKind() const {
  297. return static_cast<BraceBlockKind>(BlockKind);
  298. }
  299. void setBlockKind(BraceBlockKind BBK) {
  300. BlockKind = BBK;
  301. assert(getBlockKind() == BBK && "BraceBlockKind overflow!");
  302. }
  303. private:
  304. /// Stores the formatting decision for the token once it was made.
  305. unsigned Decision : 2;
  306. public:
  307. FormatDecision getDecision() const {
  308. return static_cast<FormatDecision>(Decision);
  309. }
  310. void setDecision(FormatDecision D) {
  311. Decision = D;
  312. assert(getDecision() == D && "FormatDecision overflow!");
  313. }
  314. private:
  315. /// If this is an opening parenthesis, how are the parameters packed?
  316. unsigned PackingKind : 2;
  317. public:
  318. ParameterPackingKind getPackingKind() const {
  319. return static_cast<ParameterPackingKind>(PackingKind);
  320. }
  321. void setPackingKind(ParameterPackingKind K) {
  322. PackingKind = K;
  323. assert(getPackingKind() == K && "ParameterPackingKind overflow!");
  324. }
  325. private:
  326. unsigned TypeIsFinalized : 1;
  327. TokenType Type;
  328. public:
  329. /// Returns the token's type, e.g. whether "<" is a template opener or
  330. /// binary operator.
  331. TokenType getType() const { return Type; }
  332. void setType(TokenType T) {
  333. assert((!TypeIsFinalized || T == Type) &&
  334. "Please use overwriteFixedType to change a fixed type.");
  335. Type = T;
  336. }
  337. /// Sets the type and also the finalized flag. This prevents the type to be
  338. /// reset in TokenAnnotator::resetTokenMetadata(). If the type needs to be set
  339. /// to another one please use overwriteFixedType, or even better remove the
  340. /// need to reassign the type.
  341. void setFinalizedType(TokenType T) {
  342. Type = T;
  343. TypeIsFinalized = true;
  344. }
  345. void overwriteFixedType(TokenType T) {
  346. TypeIsFinalized = false;
  347. setType(T);
  348. }
  349. bool isTypeFinalized() const { return TypeIsFinalized; }
  350. /// Used to set an operator precedence explicitly.
  351. prec::Level ForcedPrecedence = prec::Unknown;
  352. /// The number of newlines immediately before the \c Token.
  353. ///
  354. /// This can be used to determine what the user wrote in the original code
  355. /// and thereby e.g. leave an empty line between two function definitions.
  356. unsigned NewlinesBefore = 0;
  357. /// The offset just past the last '\n' in this token's leading
  358. /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'.
  359. unsigned LastNewlineOffset = 0;
  360. /// The width of the non-whitespace parts of the token (or its first
  361. /// line for multi-line tokens) in columns.
  362. /// We need this to correctly measure number of columns a token spans.
  363. unsigned ColumnWidth = 0;
  364. /// Contains the width in columns of the last line of a multi-line
  365. /// token.
  366. unsigned LastLineColumnWidth = 0;
  367. /// The number of spaces that should be inserted before this token.
  368. unsigned SpacesRequiredBefore = 0;
  369. /// Number of parameters, if this is "(", "[" or "<".
  370. unsigned ParameterCount = 0;
  371. /// Number of parameters that are nested blocks,
  372. /// if this is "(", "[" or "<".
  373. unsigned BlockParameterCount = 0;
  374. /// If this is a bracket ("<", "(", "[" or "{"), contains the kind of
  375. /// the surrounding bracket.
  376. tok::TokenKind ParentBracket = tok::unknown;
  377. /// The total length of the unwrapped line up to and including this
  378. /// token.
  379. unsigned TotalLength = 0;
  380. /// The original 0-based column of this token, including expanded tabs.
  381. /// The configured TabWidth is used as tab width.
  382. unsigned OriginalColumn = 0;
  383. /// The length of following tokens until the next natural split point,
  384. /// or the next token that can be broken.
  385. unsigned UnbreakableTailLength = 0;
  386. // FIXME: Come up with a 'cleaner' concept.
  387. /// The binding strength of a token. This is a combined value of
  388. /// operator precedence, parenthesis nesting, etc.
  389. unsigned BindingStrength = 0;
  390. /// The nesting level of this token, i.e. the number of surrounding (),
  391. /// [], {} or <>.
  392. unsigned NestingLevel = 0;
  393. /// The indent level of this token. Copied from the surrounding line.
  394. unsigned IndentLevel = 0;
  395. /// Penalty for inserting a line break before this token.
  396. unsigned SplitPenalty = 0;
  397. /// If this is the first ObjC selector name in an ObjC method
  398. /// definition or call, this contains the length of the longest name.
  399. ///
  400. /// This being set to 0 means that the selectors should not be colon-aligned,
  401. /// e.g. because several of them are block-type.
  402. unsigned LongestObjCSelectorName = 0;
  403. /// If this is the first ObjC selector name in an ObjC method
  404. /// definition or call, this contains the number of parts that the whole
  405. /// selector consist of.
  406. unsigned ObjCSelectorNameParts = 0;
  407. /// The 0-based index of the parameter/argument. For ObjC it is set
  408. /// for the selector name token.
  409. /// For now calculated only for ObjC.
  410. unsigned ParameterIndex = 0;
  411. /// Stores the number of required fake parentheses and the
  412. /// corresponding operator precedence.
  413. ///
  414. /// If multiple fake parentheses start at a token, this vector stores them in
  415. /// reverse order, i.e. inner fake parenthesis first.
  416. SmallVector<prec::Level, 4> FakeLParens;
  417. /// Insert this many fake ) after this token for correct indentation.
  418. unsigned FakeRParens = 0;
  419. /// If this is an operator (or "."/"->") in a sequence of operators
  420. /// with the same precedence, contains the 0-based operator index.
  421. unsigned OperatorIndex = 0;
  422. /// If this is an operator (or "."/"->") in a sequence of operators
  423. /// with the same precedence, points to the next operator.
  424. FormatToken *NextOperator = nullptr;
  425. /// If this is a bracket, this points to the matching one.
  426. FormatToken *MatchingParen = nullptr;
  427. /// The previous token in the unwrapped line.
  428. FormatToken *Previous = nullptr;
  429. /// The next token in the unwrapped line.
  430. FormatToken *Next = nullptr;
  431. /// The first token in set of column elements.
  432. bool StartsColumn = false;
  433. /// This notes the start of the line of an array initializer.
  434. bool ArrayInitializerLineStart = false;
  435. /// This starts an array initializer.
  436. bool IsArrayInitializer = false;
  437. /// Is optional and can be removed.
  438. bool Optional = false;
  439. /// Number of optional braces to be inserted after this token:
  440. /// -1: a single left brace
  441. /// 0: no braces
  442. /// >0: number of right braces
  443. int8_t BraceCount = 0;
  444. /// If this token starts a block, this contains all the unwrapped lines
  445. /// in it.
  446. SmallVector<AnnotatedLine *, 1> Children;
  447. // Contains all attributes related to how this token takes part
  448. // in a configured macro expansion.
  449. std::optional<MacroExpansion> MacroCtx;
  450. /// When macro expansion introduces nodes with children, those are marked as
  451. /// \c MacroParent.
  452. /// FIXME: The formatting code currently hard-codes the assumption that
  453. /// child nodes are introduced by blocks following an opening brace.
  454. /// This is deeply baked into the code and disentangling this will require
  455. /// signficant refactorings. \c MacroParent allows us to special-case the
  456. /// cases in which we treat parents as block-openers for now.
  457. bool MacroParent = false;
  458. bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
  459. bool is(TokenType TT) const { return getType() == TT; }
  460. bool is(const IdentifierInfo *II) const {
  461. return II && II == Tok.getIdentifierInfo();
  462. }
  463. bool is(tok::PPKeywordKind Kind) const {
  464. return Tok.getIdentifierInfo() &&
  465. Tok.getIdentifierInfo()->getPPKeywordID() == Kind;
  466. }
  467. bool is(BraceBlockKind BBK) const { return getBlockKind() == BBK; }
  468. bool is(ParameterPackingKind PPK) const { return getPackingKind() == PPK; }
  469. template <typename A, typename B> bool isOneOf(A K1, B K2) const {
  470. return is(K1) || is(K2);
  471. }
  472. template <typename A, typename B, typename... Ts>
  473. bool isOneOf(A K1, B K2, Ts... Ks) const {
  474. return is(K1) || isOneOf(K2, Ks...);
  475. }
  476. template <typename T> bool isNot(T Kind) const { return !is(Kind); }
  477. bool isIf(bool AllowConstexprMacro = true) const {
  478. return is(tok::kw_if) || endsSequence(tok::kw_constexpr, tok::kw_if) ||
  479. (endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro);
  480. }
  481. bool closesScopeAfterBlock() const {
  482. if (getBlockKind() == BK_Block)
  483. return true;
  484. if (closesScope())
  485. return Previous->closesScopeAfterBlock();
  486. return false;
  487. }
  488. /// \c true if this token starts a sequence with the given tokens in order,
  489. /// following the ``Next`` pointers, ignoring comments.
  490. template <typename A, typename... Ts>
  491. bool startsSequence(A K1, Ts... Tokens) const {
  492. return startsSequenceInternal(K1, Tokens...);
  493. }
  494. /// \c true if this token ends a sequence with the given tokens in order,
  495. /// following the ``Previous`` pointers, ignoring comments.
  496. /// For example, given tokens [T1, T2, T3], the function returns true if
  497. /// 3 tokens ending at this (ignoring comments) are [T3, T2, T1]. In other
  498. /// words, the tokens passed to this function need to the reverse of the
  499. /// order the tokens appear in code.
  500. template <typename A, typename... Ts>
  501. bool endsSequence(A K1, Ts... Tokens) const {
  502. return endsSequenceInternal(K1, Tokens...);
  503. }
  504. bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
  505. bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
  506. return Tok.isObjCAtKeyword(Kind);
  507. }
  508. bool isAccessSpecifier(bool ColonRequired = true) const {
  509. if (!isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private))
  510. return false;
  511. if (!ColonRequired)
  512. return true;
  513. const auto NextNonComment = getNextNonComment();
  514. return NextNonComment && NextNonComment->is(tok::colon);
  515. }
  516. bool canBePointerOrReferenceQualifier() const {
  517. return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile,
  518. tok::kw___attribute, tok::kw__Nonnull, tok::kw__Nullable,
  519. tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64,
  520. TT_AttributeMacro);
  521. }
  522. /// Determine whether the token is a simple-type-specifier.
  523. [[nodiscard]] bool isSimpleTypeSpecifier() const;
  524. [[nodiscard]] bool isTypeOrIdentifier() const;
  525. bool isObjCAccessSpecifier() const {
  526. return is(tok::at) && Next &&
  527. (Next->isObjCAtKeyword(tok::objc_public) ||
  528. Next->isObjCAtKeyword(tok::objc_protected) ||
  529. Next->isObjCAtKeyword(tok::objc_package) ||
  530. Next->isObjCAtKeyword(tok::objc_private));
  531. }
  532. /// Returns whether \p Tok is ([{ or an opening < of a template or in
  533. /// protos.
  534. bool opensScope() const {
  535. if (is(TT_TemplateString) && TokenText.endswith("${"))
  536. return true;
  537. if (is(TT_DictLiteral) && is(tok::less))
  538. return true;
  539. return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
  540. TT_TemplateOpener);
  541. }
  542. /// Returns whether \p Tok is )]} or a closing > of a template or in
  543. /// protos.
  544. bool closesScope() const {
  545. if (is(TT_TemplateString) && TokenText.startswith("}"))
  546. return true;
  547. if (is(TT_DictLiteral) && is(tok::greater))
  548. return true;
  549. return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
  550. TT_TemplateCloser);
  551. }
  552. /// Returns \c true if this is a "." or "->" accessing a member.
  553. bool isMemberAccess() const {
  554. return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
  555. !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
  556. TT_LambdaArrow, TT_LeadingJavaAnnotation);
  557. }
  558. bool isUnaryOperator() const {
  559. switch (Tok.getKind()) {
  560. case tok::plus:
  561. case tok::plusplus:
  562. case tok::minus:
  563. case tok::minusminus:
  564. case tok::exclaim:
  565. case tok::tilde:
  566. case tok::kw_sizeof:
  567. case tok::kw_alignof:
  568. return true;
  569. default:
  570. return false;
  571. }
  572. }
  573. bool isBinaryOperator() const {
  574. // Comma is a binary operator, but does not behave as such wrt. formatting.
  575. return getPrecedence() > prec::Comma;
  576. }
  577. bool isTrailingComment() const {
  578. return is(tok::comment) &&
  579. (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
  580. }
  581. /// Returns \c true if this is a keyword that can be used
  582. /// like a function call (e.g. sizeof, typeid, ...).
  583. bool isFunctionLikeKeyword() const {
  584. switch (Tok.getKind()) {
  585. case tok::kw_throw:
  586. case tok::kw_typeid:
  587. case tok::kw_return:
  588. case tok::kw_sizeof:
  589. case tok::kw_alignof:
  590. case tok::kw_alignas:
  591. case tok::kw_decltype:
  592. case tok::kw_noexcept:
  593. case tok::kw_static_assert:
  594. case tok::kw__Atomic:
  595. case tok::kw___attribute:
  596. #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
  597. #include "clang/Basic/TransformTypeTraits.def"
  598. case tok::kw_requires:
  599. return true;
  600. default:
  601. return false;
  602. }
  603. }
  604. /// Returns \c true if this is a string literal that's like a label,
  605. /// e.g. ends with "=" or ":".
  606. bool isLabelString() const {
  607. if (!is(tok::string_literal))
  608. return false;
  609. StringRef Content = TokenText;
  610. if (Content.startswith("\"") || Content.startswith("'"))
  611. Content = Content.drop_front(1);
  612. if (Content.endswith("\"") || Content.endswith("'"))
  613. Content = Content.drop_back(1);
  614. Content = Content.trim();
  615. return Content.size() > 1 &&
  616. (Content.back() == ':' || Content.back() == '=');
  617. }
  618. /// Returns actual token start location without leading escaped
  619. /// newlines and whitespace.
  620. ///
  621. /// This can be different to Tok.getLocation(), which includes leading escaped
  622. /// newlines.
  623. SourceLocation getStartOfNonWhitespace() const {
  624. return WhitespaceRange.getEnd();
  625. }
  626. /// Returns \c true if the range of whitespace immediately preceding the \c
  627. /// Token is not empty.
  628. bool hasWhitespaceBefore() const {
  629. return WhitespaceRange.getBegin() != WhitespaceRange.getEnd();
  630. }
  631. prec::Level getPrecedence() const {
  632. if (ForcedPrecedence != prec::Unknown)
  633. return ForcedPrecedence;
  634. return getBinOpPrecedence(Tok.getKind(), /*GreaterThanIsOperator=*/true,
  635. /*CPlusPlus11=*/true);
  636. }
  637. /// Returns the previous token ignoring comments.
  638. [[nodiscard]] FormatToken *getPreviousNonComment() const {
  639. FormatToken *Tok = Previous;
  640. while (Tok && Tok->is(tok::comment))
  641. Tok = Tok->Previous;
  642. return Tok;
  643. }
  644. /// Returns the next token ignoring comments.
  645. [[nodiscard]] const FormatToken *getNextNonComment() const {
  646. const FormatToken *Tok = Next;
  647. while (Tok && Tok->is(tok::comment))
  648. Tok = Tok->Next;
  649. return Tok;
  650. }
  651. /// Returns \c true if this tokens starts a block-type list, i.e. a
  652. /// list that should be indented with a block indent.
  653. [[nodiscard]] bool opensBlockOrBlockTypeList(const FormatStyle &Style) const;
  654. /// Returns whether the token is the left square bracket of a C++
  655. /// structured binding declaration.
  656. bool isCppStructuredBinding(const FormatStyle &Style) const {
  657. if (!Style.isCpp() || isNot(tok::l_square))
  658. return false;
  659. const FormatToken *T = this;
  660. do {
  661. T = T->getPreviousNonComment();
  662. } while (T && T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp,
  663. tok::ampamp));
  664. return T && T->is(tok::kw_auto);
  665. }
  666. /// Same as opensBlockOrBlockTypeList, but for the closing token.
  667. bool closesBlockOrBlockTypeList(const FormatStyle &Style) const {
  668. if (is(TT_TemplateString) && closesScope())
  669. return true;
  670. return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style);
  671. }
  672. /// Return the actual namespace token, if this token starts a namespace
  673. /// block.
  674. const FormatToken *getNamespaceToken() const {
  675. const FormatToken *NamespaceTok = this;
  676. if (is(tok::comment))
  677. NamespaceTok = NamespaceTok->getNextNonComment();
  678. // Detect "(inline|export)? namespace" in the beginning of a line.
  679. if (NamespaceTok && NamespaceTok->isOneOf(tok::kw_inline, tok::kw_export))
  680. NamespaceTok = NamespaceTok->getNextNonComment();
  681. return NamespaceTok &&
  682. NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro)
  683. ? NamespaceTok
  684. : nullptr;
  685. }
  686. void copyFrom(const FormatToken &Tok) { *this = Tok; }
  687. private:
  688. // Only allow copying via the explicit copyFrom method.
  689. FormatToken(const FormatToken &) = delete;
  690. FormatToken &operator=(const FormatToken &) = default;
  691. template <typename A, typename... Ts>
  692. bool startsSequenceInternal(A K1, Ts... Tokens) const {
  693. if (is(tok::comment) && Next)
  694. return Next->startsSequenceInternal(K1, Tokens...);
  695. return is(K1) && Next && Next->startsSequenceInternal(Tokens...);
  696. }
  697. template <typename A> bool startsSequenceInternal(A K1) const {
  698. if (is(tok::comment) && Next)
  699. return Next->startsSequenceInternal(K1);
  700. return is(K1);
  701. }
  702. template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const {
  703. if (is(tok::comment) && Previous)
  704. return Previous->endsSequenceInternal(K1);
  705. return is(K1);
  706. }
  707. template <typename A, typename... Ts>
  708. bool endsSequenceInternal(A K1, Ts... Tokens) const {
  709. if (is(tok::comment) && Previous)
  710. return Previous->endsSequenceInternal(K1, Tokens...);
  711. return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...);
  712. }
  713. };
  714. class ContinuationIndenter;
  715. struct LineState;
  716. class TokenRole {
  717. public:
  718. TokenRole(const FormatStyle &Style) : Style(Style) {}
  719. virtual ~TokenRole();
  720. /// After the \c TokenAnnotator has finished annotating all the tokens,
  721. /// this function precomputes required information for formatting.
  722. virtual void precomputeFormattingInfos(const FormatToken *Token);
  723. /// Apply the special formatting that the given role demands.
  724. ///
  725. /// Assumes that the token having this role is already formatted.
  726. ///
  727. /// Continues formatting from \p State leaving indentation to \p Indenter and
  728. /// returns the total penalty that this formatting incurs.
  729. virtual unsigned formatFromToken(LineState &State,
  730. ContinuationIndenter *Indenter,
  731. bool DryRun) {
  732. return 0;
  733. }
  734. /// Same as \c formatFromToken, but assumes that the first token has
  735. /// already been set thereby deciding on the first line break.
  736. virtual unsigned formatAfterToken(LineState &State,
  737. ContinuationIndenter *Indenter,
  738. bool DryRun) {
  739. return 0;
  740. }
  741. /// Notifies the \c Role that a comma was found.
  742. virtual void CommaFound(const FormatToken *Token) {}
  743. virtual const FormatToken *lastComma() { return nullptr; }
  744. protected:
  745. const FormatStyle &Style;
  746. };
  747. class CommaSeparatedList : public TokenRole {
  748. public:
  749. CommaSeparatedList(const FormatStyle &Style)
  750. : TokenRole(Style), HasNestedBracedList(false) {}
  751. void precomputeFormattingInfos(const FormatToken *Token) override;
  752. unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
  753. bool DryRun) override;
  754. unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
  755. bool DryRun) override;
  756. /// Adds \p Token as the next comma to the \c CommaSeparated list.
  757. void CommaFound(const FormatToken *Token) override {
  758. Commas.push_back(Token);
  759. }
  760. const FormatToken *lastComma() override {
  761. if (Commas.empty())
  762. return nullptr;
  763. return Commas.back();
  764. }
  765. private:
  766. /// A struct that holds information on how to format a given list with
  767. /// a specific number of columns.
  768. struct ColumnFormat {
  769. /// The number of columns to use.
  770. unsigned Columns;
  771. /// The total width in characters.
  772. unsigned TotalWidth;
  773. /// The number of lines required for this format.
  774. unsigned LineCount;
  775. /// The size of each column in characters.
  776. SmallVector<unsigned, 8> ColumnSizes;
  777. };
  778. /// Calculate which \c ColumnFormat fits best into
  779. /// \p RemainingCharacters.
  780. const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
  781. /// The ordered \c FormatTokens making up the commas of this list.
  782. SmallVector<const FormatToken *, 8> Commas;
  783. /// The length of each of the list's items in characters including the
  784. /// trailing comma.
  785. SmallVector<unsigned, 8> ItemLengths;
  786. /// Precomputed formats that can be used for this list.
  787. SmallVector<ColumnFormat, 4> Formats;
  788. bool HasNestedBracedList;
  789. };
  790. /// Encapsulates keywords that are context sensitive or for languages not
  791. /// properly supported by Clang's lexer.
  792. struct AdditionalKeywords {
  793. AdditionalKeywords(IdentifierTable &IdentTable) {
  794. kw_final = &IdentTable.get("final");
  795. kw_override = &IdentTable.get("override");
  796. kw_in = &IdentTable.get("in");
  797. kw_of = &IdentTable.get("of");
  798. kw_CF_CLOSED_ENUM = &IdentTable.get("CF_CLOSED_ENUM");
  799. kw_CF_ENUM = &IdentTable.get("CF_ENUM");
  800. kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
  801. kw_NS_CLOSED_ENUM = &IdentTable.get("NS_CLOSED_ENUM");
  802. kw_NS_ENUM = &IdentTable.get("NS_ENUM");
  803. kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
  804. kw_as = &IdentTable.get("as");
  805. kw_async = &IdentTable.get("async");
  806. kw_await = &IdentTable.get("await");
  807. kw_declare = &IdentTable.get("declare");
  808. kw_finally = &IdentTable.get("finally");
  809. kw_from = &IdentTable.get("from");
  810. kw_function = &IdentTable.get("function");
  811. kw_get = &IdentTable.get("get");
  812. kw_import = &IdentTable.get("import");
  813. kw_infer = &IdentTable.get("infer");
  814. kw_is = &IdentTable.get("is");
  815. kw_let = &IdentTable.get("let");
  816. kw_module = &IdentTable.get("module");
  817. kw_readonly = &IdentTable.get("readonly");
  818. kw_set = &IdentTable.get("set");
  819. kw_type = &IdentTable.get("type");
  820. kw_typeof = &IdentTable.get("typeof");
  821. kw_var = &IdentTable.get("var");
  822. kw_yield = &IdentTable.get("yield");
  823. kw_abstract = &IdentTable.get("abstract");
  824. kw_assert = &IdentTable.get("assert");
  825. kw_extends = &IdentTable.get("extends");
  826. kw_implements = &IdentTable.get("implements");
  827. kw_instanceof = &IdentTable.get("instanceof");
  828. kw_interface = &IdentTable.get("interface");
  829. kw_native = &IdentTable.get("native");
  830. kw_package = &IdentTable.get("package");
  831. kw_synchronized = &IdentTable.get("synchronized");
  832. kw_throws = &IdentTable.get("throws");
  833. kw___except = &IdentTable.get("__except");
  834. kw___has_include = &IdentTable.get("__has_include");
  835. kw___has_include_next = &IdentTable.get("__has_include_next");
  836. kw_mark = &IdentTable.get("mark");
  837. kw_region = &IdentTable.get("region");
  838. kw_extend = &IdentTable.get("extend");
  839. kw_option = &IdentTable.get("option");
  840. kw_optional = &IdentTable.get("optional");
  841. kw_repeated = &IdentTable.get("repeated");
  842. kw_required = &IdentTable.get("required");
  843. kw_returns = &IdentTable.get("returns");
  844. kw_signals = &IdentTable.get("signals");
  845. kw_qsignals = &IdentTable.get("Q_SIGNALS");
  846. kw_slots = &IdentTable.get("slots");
  847. kw_qslots = &IdentTable.get("Q_SLOTS");
  848. // For internal clang-format use.
  849. kw_internal_ident_after_define =
  850. &IdentTable.get("__CLANG_FORMAT_INTERNAL_IDENT_AFTER_DEFINE__");
  851. // C# keywords
  852. kw_dollar = &IdentTable.get("dollar");
  853. kw_base = &IdentTable.get("base");
  854. kw_byte = &IdentTable.get("byte");
  855. kw_checked = &IdentTable.get("checked");
  856. kw_decimal = &IdentTable.get("decimal");
  857. kw_delegate = &IdentTable.get("delegate");
  858. kw_event = &IdentTable.get("event");
  859. kw_fixed = &IdentTable.get("fixed");
  860. kw_foreach = &IdentTable.get("foreach");
  861. kw_init = &IdentTable.get("init");
  862. kw_implicit = &IdentTable.get("implicit");
  863. kw_internal = &IdentTable.get("internal");
  864. kw_lock = &IdentTable.get("lock");
  865. kw_null = &IdentTable.get("null");
  866. kw_object = &IdentTable.get("object");
  867. kw_out = &IdentTable.get("out");
  868. kw_params = &IdentTable.get("params");
  869. kw_ref = &IdentTable.get("ref");
  870. kw_string = &IdentTable.get("string");
  871. kw_stackalloc = &IdentTable.get("stackalloc");
  872. kw_sbyte = &IdentTable.get("sbyte");
  873. kw_sealed = &IdentTable.get("sealed");
  874. kw_uint = &IdentTable.get("uint");
  875. kw_ulong = &IdentTable.get("ulong");
  876. kw_unchecked = &IdentTable.get("unchecked");
  877. kw_unsafe = &IdentTable.get("unsafe");
  878. kw_ushort = &IdentTable.get("ushort");
  879. kw_when = &IdentTable.get("when");
  880. kw_where = &IdentTable.get("where");
  881. // Verilog keywords
  882. kw_always = &IdentTable.get("always");
  883. kw_always_comb = &IdentTable.get("always_comb");
  884. kw_always_ff = &IdentTable.get("always_ff");
  885. kw_always_latch = &IdentTable.get("always_latch");
  886. kw_assign = &IdentTable.get("assign");
  887. kw_assume = &IdentTable.get("assume");
  888. kw_automatic = &IdentTable.get("automatic");
  889. kw_before = &IdentTable.get("before");
  890. kw_begin = &IdentTable.get("begin");
  891. kw_begin_keywords = &IdentTable.get("begin_keywords");
  892. kw_bins = &IdentTable.get("bins");
  893. kw_binsof = &IdentTable.get("binsof");
  894. kw_casex = &IdentTable.get("casex");
  895. kw_casez = &IdentTable.get("casez");
  896. kw_celldefine = &IdentTable.get("celldefine");
  897. kw_checker = &IdentTable.get("checker");
  898. kw_clocking = &IdentTable.get("clocking");
  899. kw_constraint = &IdentTable.get("constraint");
  900. kw_cover = &IdentTable.get("cover");
  901. kw_covergroup = &IdentTable.get("covergroup");
  902. kw_coverpoint = &IdentTable.get("coverpoint");
  903. kw_default_decay_time = &IdentTable.get("default_decay_time");
  904. kw_default_nettype = &IdentTable.get("default_nettype");
  905. kw_default_trireg_strength = &IdentTable.get("default_trireg_strength");
  906. kw_delay_mode_distributed = &IdentTable.get("delay_mode_distributed");
  907. kw_delay_mode_path = &IdentTable.get("delay_mode_path");
  908. kw_delay_mode_unit = &IdentTable.get("delay_mode_unit");
  909. kw_delay_mode_zero = &IdentTable.get("delay_mode_zero");
  910. kw_disable = &IdentTable.get("disable");
  911. kw_dist = &IdentTable.get("dist");
  912. kw_elsif = &IdentTable.get("elsif");
  913. kw_end = &IdentTable.get("end");
  914. kw_end_keywords = &IdentTable.get("end_keywords");
  915. kw_endcase = &IdentTable.get("endcase");
  916. kw_endcelldefine = &IdentTable.get("endcelldefine");
  917. kw_endchecker = &IdentTable.get("endchecker");
  918. kw_endclass = &IdentTable.get("endclass");
  919. kw_endclocking = &IdentTable.get("endclocking");
  920. kw_endfunction = &IdentTable.get("endfunction");
  921. kw_endgenerate = &IdentTable.get("endgenerate");
  922. kw_endgroup = &IdentTable.get("endgroup");
  923. kw_endinterface = &IdentTable.get("endinterface");
  924. kw_endmodule = &IdentTable.get("endmodule");
  925. kw_endpackage = &IdentTable.get("endpackage");
  926. kw_endprimitive = &IdentTable.get("endprimitive");
  927. kw_endprogram = &IdentTable.get("endprogram");
  928. kw_endproperty = &IdentTable.get("endproperty");
  929. kw_endsequence = &IdentTable.get("endsequence");
  930. kw_endspecify = &IdentTable.get("endspecify");
  931. kw_endtable = &IdentTable.get("endtable");
  932. kw_endtask = &IdentTable.get("endtask");
  933. kw_forever = &IdentTable.get("forever");
  934. kw_fork = &IdentTable.get("fork");
  935. kw_generate = &IdentTable.get("generate");
  936. kw_highz0 = &IdentTable.get("highz0");
  937. kw_highz1 = &IdentTable.get("highz1");
  938. kw_iff = &IdentTable.get("iff");
  939. kw_ifnone = &IdentTable.get("ifnone");
  940. kw_ignore_bins = &IdentTable.get("ignore_bins");
  941. kw_illegal_bins = &IdentTable.get("illegal_bins");
  942. kw_initial = &IdentTable.get("initial");
  943. kw_inout = &IdentTable.get("inout");
  944. kw_input = &IdentTable.get("input");
  945. kw_inside = &IdentTable.get("inside");
  946. kw_interconnect = &IdentTable.get("interconnect");
  947. kw_intersect = &IdentTable.get("intersect");
  948. kw_join = &IdentTable.get("join");
  949. kw_join_any = &IdentTable.get("join_any");
  950. kw_join_none = &IdentTable.get("join_none");
  951. kw_large = &IdentTable.get("large");
  952. kw_local = &IdentTable.get("local");
  953. kw_localparam = &IdentTable.get("localparam");
  954. kw_macromodule = &IdentTable.get("macromodule");
  955. kw_matches = &IdentTable.get("matches");
  956. kw_medium = &IdentTable.get("medium");
  957. kw_nounconnected_drive = &IdentTable.get("nounconnected_drive");
  958. kw_output = &IdentTable.get("output");
  959. kw_packed = &IdentTable.get("packed");
  960. kw_parameter = &IdentTable.get("parameter");
  961. kw_primitive = &IdentTable.get("primitive");
  962. kw_priority = &IdentTable.get("priority");
  963. kw_program = &IdentTable.get("program");
  964. kw_property = &IdentTable.get("property");
  965. kw_pull0 = &IdentTable.get("pull0");
  966. kw_pull1 = &IdentTable.get("pull1");
  967. kw_pure = &IdentTable.get("pure");
  968. kw_rand = &IdentTable.get("rand");
  969. kw_randc = &IdentTable.get("randc");
  970. kw_randcase = &IdentTable.get("randcase");
  971. kw_randsequence = &IdentTable.get("randsequence");
  972. kw_repeat = &IdentTable.get("repeat");
  973. kw_resetall = &IdentTable.get("resetall");
  974. kw_sample = &IdentTable.get("sample");
  975. kw_scalared = &IdentTable.get("scalared");
  976. kw_sequence = &IdentTable.get("sequence");
  977. kw_small = &IdentTable.get("small");
  978. kw_soft = &IdentTable.get("soft");
  979. kw_solve = &IdentTable.get("solve");
  980. kw_specify = &IdentTable.get("specify");
  981. kw_specparam = &IdentTable.get("specparam");
  982. kw_strong0 = &IdentTable.get("strong0");
  983. kw_strong1 = &IdentTable.get("strong1");
  984. kw_supply0 = &IdentTable.get("supply0");
  985. kw_supply1 = &IdentTable.get("supply1");
  986. kw_table = &IdentTable.get("table");
  987. kw_tagged = &IdentTable.get("tagged");
  988. kw_task = &IdentTable.get("task");
  989. kw_timescale = &IdentTable.get("timescale");
  990. kw_tri = &IdentTable.get("tri");
  991. kw_tri0 = &IdentTable.get("tri0");
  992. kw_tri1 = &IdentTable.get("tri1");
  993. kw_triand = &IdentTable.get("triand");
  994. kw_trior = &IdentTable.get("trior");
  995. kw_trireg = &IdentTable.get("trireg");
  996. kw_unconnected_drive = &IdentTable.get("unconnected_drive");
  997. kw_undefineall = &IdentTable.get("undefineall");
  998. kw_unique = &IdentTable.get("unique");
  999. kw_unique0 = &IdentTable.get("unique0");
  1000. kw_uwire = &IdentTable.get("uwire");
  1001. kw_vectored = &IdentTable.get("vectored");
  1002. kw_wand = &IdentTable.get("wand");
  1003. kw_weak0 = &IdentTable.get("weak0");
  1004. kw_weak1 = &IdentTable.get("weak1");
  1005. kw_wildcard = &IdentTable.get("wildcard");
  1006. kw_wire = &IdentTable.get("wire");
  1007. kw_with = &IdentTable.get("with");
  1008. kw_wor = &IdentTable.get("wor");
  1009. // Symbols that are treated as keywords.
  1010. kw_verilogHash = &IdentTable.get("#");
  1011. kw_verilogHashHash = &IdentTable.get("##");
  1012. kw_apostrophe = &IdentTable.get("\'");
  1013. // Keep this at the end of the constructor to make sure everything here
  1014. // is
  1015. // already initialized.
  1016. JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
  1017. {kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
  1018. kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_override,
  1019. kw_readonly, kw_set, kw_type, kw_typeof, kw_var, kw_yield,
  1020. // Keywords from the Java section.
  1021. kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
  1022. CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>(
  1023. {kw_base, kw_byte, kw_checked, kw_decimal, kw_delegate, kw_event,
  1024. kw_fixed, kw_foreach, kw_implicit, kw_in, kw_init, kw_interface,
  1025. kw_internal, kw_is, kw_lock, kw_null, kw_object, kw_out, kw_override,
  1026. kw_params, kw_readonly, kw_ref, kw_string, kw_stackalloc, kw_sbyte,
  1027. kw_sealed, kw_uint, kw_ulong, kw_unchecked, kw_unsafe, kw_ushort,
  1028. kw_when, kw_where,
  1029. // Keywords from the JavaScript section.
  1030. kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
  1031. kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_readonly,
  1032. kw_set, kw_type, kw_typeof, kw_var, kw_yield,
  1033. // Keywords from the Java section.
  1034. kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});
  1035. // Some keywords are not included here because they don't need special
  1036. // treatment like `showcancelled` or they should be treated as identifiers
  1037. // like `int` and `logic`.
  1038. VerilogExtraKeywords =
  1039. std::unordered_set<IdentifierInfo *>({kw_always,
  1040. kw_always_comb,
  1041. kw_always_ff,
  1042. kw_always_latch,
  1043. kw_assert,
  1044. kw_assign,
  1045. kw_assume,
  1046. kw_automatic,
  1047. kw_before,
  1048. kw_begin,
  1049. kw_bins,
  1050. kw_binsof,
  1051. kw_casex,
  1052. kw_casez,
  1053. kw_celldefine,
  1054. kw_checker,
  1055. kw_clocking,
  1056. kw_constraint,
  1057. kw_cover,
  1058. kw_covergroup,
  1059. kw_coverpoint,
  1060. kw_disable,
  1061. kw_dist,
  1062. kw_end,
  1063. kw_endcase,
  1064. kw_endchecker,
  1065. kw_endclass,
  1066. kw_endclocking,
  1067. kw_endfunction,
  1068. kw_endgenerate,
  1069. kw_endgroup,
  1070. kw_endinterface,
  1071. kw_endmodule,
  1072. kw_endpackage,
  1073. kw_endprimitive,
  1074. kw_endprogram,
  1075. kw_endproperty,
  1076. kw_endsequence,
  1077. kw_endspecify,
  1078. kw_endtable,
  1079. kw_endtask,
  1080. kw_extends,
  1081. kw_final,
  1082. kw_foreach,
  1083. kw_forever,
  1084. kw_fork,
  1085. kw_function,
  1086. kw_generate,
  1087. kw_highz0,
  1088. kw_highz1,
  1089. kw_iff,
  1090. kw_ifnone,
  1091. kw_ignore_bins,
  1092. kw_illegal_bins,
  1093. kw_implements,
  1094. kw_import,
  1095. kw_initial,
  1096. kw_inout,
  1097. kw_input,
  1098. kw_inside,
  1099. kw_interconnect,
  1100. kw_interface,
  1101. kw_intersect,
  1102. kw_join,
  1103. kw_join_any,
  1104. kw_join_none,
  1105. kw_large,
  1106. kw_let,
  1107. kw_local,
  1108. kw_localparam,
  1109. kw_macromodule,
  1110. kw_matches,
  1111. kw_medium,
  1112. kw_output,
  1113. kw_package,
  1114. kw_packed,
  1115. kw_parameter,
  1116. kw_primitive,
  1117. kw_priority,
  1118. kw_program,
  1119. kw_property,
  1120. kw_pull0,
  1121. kw_pull1,
  1122. kw_pure,
  1123. kw_rand,
  1124. kw_randc,
  1125. kw_randcase,
  1126. kw_randsequence,
  1127. kw_ref,
  1128. kw_repeat,
  1129. kw_sample,
  1130. kw_scalared,
  1131. kw_sequence,
  1132. kw_small,
  1133. kw_soft,
  1134. kw_solve,
  1135. kw_specify,
  1136. kw_specparam,
  1137. kw_strong0,
  1138. kw_strong1,
  1139. kw_supply0,
  1140. kw_supply1,
  1141. kw_table,
  1142. kw_tagged,
  1143. kw_task,
  1144. kw_tri,
  1145. kw_tri0,
  1146. kw_tri1,
  1147. kw_triand,
  1148. kw_trior,
  1149. kw_trireg,
  1150. kw_unique,
  1151. kw_unique0,
  1152. kw_uwire,
  1153. kw_var,
  1154. kw_vectored,
  1155. kw_wand,
  1156. kw_weak0,
  1157. kw_weak1,
  1158. kw_wildcard,
  1159. kw_wire,
  1160. kw_with,
  1161. kw_wor,
  1162. kw_verilogHash,
  1163. kw_verilogHashHash});
  1164. }
  1165. // Context sensitive keywords.
  1166. IdentifierInfo *kw_final;
  1167. IdentifierInfo *kw_override;
  1168. IdentifierInfo *kw_in;
  1169. IdentifierInfo *kw_of;
  1170. IdentifierInfo *kw_CF_CLOSED_ENUM;
  1171. IdentifierInfo *kw_CF_ENUM;
  1172. IdentifierInfo *kw_CF_OPTIONS;
  1173. IdentifierInfo *kw_NS_CLOSED_ENUM;
  1174. IdentifierInfo *kw_NS_ENUM;
  1175. IdentifierInfo *kw_NS_OPTIONS;
  1176. IdentifierInfo *kw___except;
  1177. IdentifierInfo *kw___has_include;
  1178. IdentifierInfo *kw___has_include_next;
  1179. // JavaScript keywords.
  1180. IdentifierInfo *kw_as;
  1181. IdentifierInfo *kw_async;
  1182. IdentifierInfo *kw_await;
  1183. IdentifierInfo *kw_declare;
  1184. IdentifierInfo *kw_finally;
  1185. IdentifierInfo *kw_from;
  1186. IdentifierInfo *kw_function;
  1187. IdentifierInfo *kw_get;
  1188. IdentifierInfo *kw_import;
  1189. IdentifierInfo *kw_infer;
  1190. IdentifierInfo *kw_is;
  1191. IdentifierInfo *kw_let;
  1192. IdentifierInfo *kw_module;
  1193. IdentifierInfo *kw_readonly;
  1194. IdentifierInfo *kw_set;
  1195. IdentifierInfo *kw_type;
  1196. IdentifierInfo *kw_typeof;
  1197. IdentifierInfo *kw_var;
  1198. IdentifierInfo *kw_yield;
  1199. // Java keywords.
  1200. IdentifierInfo *kw_abstract;
  1201. IdentifierInfo *kw_assert;
  1202. IdentifierInfo *kw_extends;
  1203. IdentifierInfo *kw_implements;
  1204. IdentifierInfo *kw_instanceof;
  1205. IdentifierInfo *kw_interface;
  1206. IdentifierInfo *kw_native;
  1207. IdentifierInfo *kw_package;
  1208. IdentifierInfo *kw_synchronized;
  1209. IdentifierInfo *kw_throws;
  1210. // Pragma keywords.
  1211. IdentifierInfo *kw_mark;
  1212. IdentifierInfo *kw_region;
  1213. // Proto keywords.
  1214. IdentifierInfo *kw_extend;
  1215. IdentifierInfo *kw_option;
  1216. IdentifierInfo *kw_optional;
  1217. IdentifierInfo *kw_repeated;
  1218. IdentifierInfo *kw_required;
  1219. IdentifierInfo *kw_returns;
  1220. // QT keywords.
  1221. IdentifierInfo *kw_signals;
  1222. IdentifierInfo *kw_qsignals;
  1223. IdentifierInfo *kw_slots;
  1224. IdentifierInfo *kw_qslots;
  1225. // For internal use by clang-format.
  1226. IdentifierInfo *kw_internal_ident_after_define;
  1227. // C# keywords
  1228. IdentifierInfo *kw_dollar;
  1229. IdentifierInfo *kw_base;
  1230. IdentifierInfo *kw_byte;
  1231. IdentifierInfo *kw_checked;
  1232. IdentifierInfo *kw_decimal;
  1233. IdentifierInfo *kw_delegate;
  1234. IdentifierInfo *kw_event;
  1235. IdentifierInfo *kw_fixed;
  1236. IdentifierInfo *kw_foreach;
  1237. IdentifierInfo *kw_implicit;
  1238. IdentifierInfo *kw_init;
  1239. IdentifierInfo *kw_internal;
  1240. IdentifierInfo *kw_lock;
  1241. IdentifierInfo *kw_null;
  1242. IdentifierInfo *kw_object;
  1243. IdentifierInfo *kw_out;
  1244. IdentifierInfo *kw_params;
  1245. IdentifierInfo *kw_ref;
  1246. IdentifierInfo *kw_string;
  1247. IdentifierInfo *kw_stackalloc;
  1248. IdentifierInfo *kw_sbyte;
  1249. IdentifierInfo *kw_sealed;
  1250. IdentifierInfo *kw_uint;
  1251. IdentifierInfo *kw_ulong;
  1252. IdentifierInfo *kw_unchecked;
  1253. IdentifierInfo *kw_unsafe;
  1254. IdentifierInfo *kw_ushort;
  1255. IdentifierInfo *kw_when;
  1256. IdentifierInfo *kw_where;
  1257. // Verilog keywords
  1258. IdentifierInfo *kw_always;
  1259. IdentifierInfo *kw_always_comb;
  1260. IdentifierInfo *kw_always_ff;
  1261. IdentifierInfo *kw_always_latch;
  1262. IdentifierInfo *kw_assign;
  1263. IdentifierInfo *kw_assume;
  1264. IdentifierInfo *kw_automatic;
  1265. IdentifierInfo *kw_before;
  1266. IdentifierInfo *kw_begin;
  1267. IdentifierInfo *kw_begin_keywords;
  1268. IdentifierInfo *kw_bins;
  1269. IdentifierInfo *kw_binsof;
  1270. IdentifierInfo *kw_casex;
  1271. IdentifierInfo *kw_casez;
  1272. IdentifierInfo *kw_celldefine;
  1273. IdentifierInfo *kw_checker;
  1274. IdentifierInfo *kw_clocking;
  1275. IdentifierInfo *kw_constraint;
  1276. IdentifierInfo *kw_cover;
  1277. IdentifierInfo *kw_covergroup;
  1278. IdentifierInfo *kw_coverpoint;
  1279. IdentifierInfo *kw_default_decay_time;
  1280. IdentifierInfo *kw_default_nettype;
  1281. IdentifierInfo *kw_default_trireg_strength;
  1282. IdentifierInfo *kw_delay_mode_distributed;
  1283. IdentifierInfo *kw_delay_mode_path;
  1284. IdentifierInfo *kw_delay_mode_unit;
  1285. IdentifierInfo *kw_delay_mode_zero;
  1286. IdentifierInfo *kw_disable;
  1287. IdentifierInfo *kw_dist;
  1288. IdentifierInfo *kw_elsif;
  1289. IdentifierInfo *kw_end;
  1290. IdentifierInfo *kw_end_keywords;
  1291. IdentifierInfo *kw_endcase;
  1292. IdentifierInfo *kw_endcelldefine;
  1293. IdentifierInfo *kw_endchecker;
  1294. IdentifierInfo *kw_endclass;
  1295. IdentifierInfo *kw_endclocking;
  1296. IdentifierInfo *kw_endfunction;
  1297. IdentifierInfo *kw_endgenerate;
  1298. IdentifierInfo *kw_endgroup;
  1299. IdentifierInfo *kw_endinterface;
  1300. IdentifierInfo *kw_endmodule;
  1301. IdentifierInfo *kw_endpackage;
  1302. IdentifierInfo *kw_endprimitive;
  1303. IdentifierInfo *kw_endprogram;
  1304. IdentifierInfo *kw_endproperty;
  1305. IdentifierInfo *kw_endsequence;
  1306. IdentifierInfo *kw_endspecify;
  1307. IdentifierInfo *kw_endtable;
  1308. IdentifierInfo *kw_endtask;
  1309. IdentifierInfo *kw_forever;
  1310. IdentifierInfo *kw_fork;
  1311. IdentifierInfo *kw_generate;
  1312. IdentifierInfo *kw_highz0;
  1313. IdentifierInfo *kw_highz1;
  1314. IdentifierInfo *kw_iff;
  1315. IdentifierInfo *kw_ifnone;
  1316. IdentifierInfo *kw_ignore_bins;
  1317. IdentifierInfo *kw_illegal_bins;
  1318. IdentifierInfo *kw_initial;
  1319. IdentifierInfo *kw_inout;
  1320. IdentifierInfo *kw_input;
  1321. IdentifierInfo *kw_inside;
  1322. IdentifierInfo *kw_interconnect;
  1323. IdentifierInfo *kw_intersect;
  1324. IdentifierInfo *kw_join;
  1325. IdentifierInfo *kw_join_any;
  1326. IdentifierInfo *kw_join_none;
  1327. IdentifierInfo *kw_large;
  1328. IdentifierInfo *kw_local;
  1329. IdentifierInfo *kw_localparam;
  1330. IdentifierInfo *kw_macromodule;
  1331. IdentifierInfo *kw_matches;
  1332. IdentifierInfo *kw_medium;
  1333. IdentifierInfo *kw_nounconnected_drive;
  1334. IdentifierInfo *kw_output;
  1335. IdentifierInfo *kw_packed;
  1336. IdentifierInfo *kw_parameter;
  1337. IdentifierInfo *kw_primitive;
  1338. IdentifierInfo *kw_priority;
  1339. IdentifierInfo *kw_program;
  1340. IdentifierInfo *kw_property;
  1341. IdentifierInfo *kw_pull0;
  1342. IdentifierInfo *kw_pull1;
  1343. IdentifierInfo *kw_pure;
  1344. IdentifierInfo *kw_rand;
  1345. IdentifierInfo *kw_randc;
  1346. IdentifierInfo *kw_randcase;
  1347. IdentifierInfo *kw_randsequence;
  1348. IdentifierInfo *kw_repeat;
  1349. IdentifierInfo *kw_resetall;
  1350. IdentifierInfo *kw_sample;
  1351. IdentifierInfo *kw_scalared;
  1352. IdentifierInfo *kw_sequence;
  1353. IdentifierInfo *kw_small;
  1354. IdentifierInfo *kw_soft;
  1355. IdentifierInfo *kw_solve;
  1356. IdentifierInfo *kw_specify;
  1357. IdentifierInfo *kw_specparam;
  1358. IdentifierInfo *kw_strong0;
  1359. IdentifierInfo *kw_strong1;
  1360. IdentifierInfo *kw_supply0;
  1361. IdentifierInfo *kw_supply1;
  1362. IdentifierInfo *kw_table;
  1363. IdentifierInfo *kw_tagged;
  1364. IdentifierInfo *kw_task;
  1365. IdentifierInfo *kw_timescale;
  1366. IdentifierInfo *kw_tri0;
  1367. IdentifierInfo *kw_tri1;
  1368. IdentifierInfo *kw_tri;
  1369. IdentifierInfo *kw_triand;
  1370. IdentifierInfo *kw_trior;
  1371. IdentifierInfo *kw_trireg;
  1372. IdentifierInfo *kw_unconnected_drive;
  1373. IdentifierInfo *kw_undefineall;
  1374. IdentifierInfo *kw_unique;
  1375. IdentifierInfo *kw_unique0;
  1376. IdentifierInfo *kw_uwire;
  1377. IdentifierInfo *kw_vectored;
  1378. IdentifierInfo *kw_wand;
  1379. IdentifierInfo *kw_weak0;
  1380. IdentifierInfo *kw_weak1;
  1381. IdentifierInfo *kw_wildcard;
  1382. IdentifierInfo *kw_wire;
  1383. IdentifierInfo *kw_with;
  1384. IdentifierInfo *kw_wor;
  1385. // Workaround for hashes and backticks in Verilog.
  1386. IdentifierInfo *kw_verilogHash;
  1387. IdentifierInfo *kw_verilogHashHash;
  1388. // Symbols in Verilog that don't exist in C++.
  1389. IdentifierInfo *kw_apostrophe;
  1390. /// Returns \c true if \p Tok is a keyword or an identifier.
  1391. bool isWordLike(const FormatToken &Tok) const {
  1392. // getIdentifierinfo returns non-null for keywords as well as identifiers.
  1393. return Tok.Tok.getIdentifierInfo() != nullptr &&
  1394. !Tok.isOneOf(kw_verilogHash, kw_verilogHashHash, kw_apostrophe);
  1395. }
  1396. /// Returns \c true if \p Tok is a true JavaScript identifier, returns
  1397. /// \c false if it is a keyword or a pseudo keyword.
  1398. /// If \c AcceptIdentifierName is true, returns true not only for keywords,
  1399. // but also for IdentifierName tokens (aka pseudo-keywords), such as
  1400. // ``yield``.
  1401. bool IsJavaScriptIdentifier(const FormatToken &Tok,
  1402. bool AcceptIdentifierName = true) const {
  1403. // Based on the list of JavaScript & TypeScript keywords here:
  1404. // https://github.com/microsoft/TypeScript/blob/main/src/compiler/scanner.ts#L74
  1405. switch (Tok.Tok.getKind()) {
  1406. case tok::kw_break:
  1407. case tok::kw_case:
  1408. case tok::kw_catch:
  1409. case tok::kw_class:
  1410. case tok::kw_continue:
  1411. case tok::kw_const:
  1412. case tok::kw_default:
  1413. case tok::kw_delete:
  1414. case tok::kw_do:
  1415. case tok::kw_else:
  1416. case tok::kw_enum:
  1417. case tok::kw_export:
  1418. case tok::kw_false:
  1419. case tok::kw_for:
  1420. case tok::kw_if:
  1421. case tok::kw_import:
  1422. case tok::kw_module:
  1423. case tok::kw_new:
  1424. case tok::kw_private:
  1425. case tok::kw_protected:
  1426. case tok::kw_public:
  1427. case tok::kw_return:
  1428. case tok::kw_static:
  1429. case tok::kw_switch:
  1430. case tok::kw_this:
  1431. case tok::kw_throw:
  1432. case tok::kw_true:
  1433. case tok::kw_try:
  1434. case tok::kw_typeof:
  1435. case tok::kw_void:
  1436. case tok::kw_while:
  1437. // These are JS keywords that are lexed by LLVM/clang as keywords.
  1438. return false;
  1439. case tok::identifier: {
  1440. // For identifiers, make sure they are true identifiers, excluding the
  1441. // JavaScript pseudo-keywords (not lexed by LLVM/clang as keywords).
  1442. bool IsPseudoKeyword =
  1443. JsExtraKeywords.find(Tok.Tok.getIdentifierInfo()) !=
  1444. JsExtraKeywords.end();
  1445. return AcceptIdentifierName || !IsPseudoKeyword;
  1446. }
  1447. default:
  1448. // Other keywords are handled in the switch below, to avoid problems due
  1449. // to duplicate case labels when using the #include trick.
  1450. break;
  1451. }
  1452. switch (Tok.Tok.getKind()) {
  1453. // Handle C++ keywords not included above: these are all JS identifiers.
  1454. #define KEYWORD(X, Y) case tok::kw_##X:
  1455. #include "clang/Basic/TokenKinds.def"
  1456. // #undef KEYWORD is not needed -- it's #undef-ed at the end of
  1457. // TokenKinds.def
  1458. return true;
  1459. default:
  1460. // All other tokens (punctuation etc) are not JS identifiers.
  1461. return false;
  1462. }
  1463. }
  1464. /// Returns \c true if \p Tok is a C# keyword, returns
  1465. /// \c false if it is a anything else.
  1466. bool isCSharpKeyword(const FormatToken &Tok) const {
  1467. switch (Tok.Tok.getKind()) {
  1468. case tok::kw_bool:
  1469. case tok::kw_break:
  1470. case tok::kw_case:
  1471. case tok::kw_catch:
  1472. case tok::kw_char:
  1473. case tok::kw_class:
  1474. case tok::kw_const:
  1475. case tok::kw_continue:
  1476. case tok::kw_default:
  1477. case tok::kw_do:
  1478. case tok::kw_double:
  1479. case tok::kw_else:
  1480. case tok::kw_enum:
  1481. case tok::kw_explicit:
  1482. case tok::kw_extern:
  1483. case tok::kw_false:
  1484. case tok::kw_float:
  1485. case tok::kw_for:
  1486. case tok::kw_goto:
  1487. case tok::kw_if:
  1488. case tok::kw_int:
  1489. case tok::kw_long:
  1490. case tok::kw_namespace:
  1491. case tok::kw_new:
  1492. case tok::kw_operator:
  1493. case tok::kw_private:
  1494. case tok::kw_protected:
  1495. case tok::kw_public:
  1496. case tok::kw_return:
  1497. case tok::kw_short:
  1498. case tok::kw_sizeof:
  1499. case tok::kw_static:
  1500. case tok::kw_struct:
  1501. case tok::kw_switch:
  1502. case tok::kw_this:
  1503. case tok::kw_throw:
  1504. case tok::kw_true:
  1505. case tok::kw_try:
  1506. case tok::kw_typeof:
  1507. case tok::kw_using:
  1508. case tok::kw_virtual:
  1509. case tok::kw_void:
  1510. case tok::kw_volatile:
  1511. case tok::kw_while:
  1512. return true;
  1513. default:
  1514. return Tok.is(tok::identifier) &&
  1515. CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
  1516. CSharpExtraKeywords.end();
  1517. }
  1518. }
  1519. bool isVerilogWordOperator(const FormatToken &Tok) const {
  1520. return Tok.isOneOf(kw_before, kw_intersect, kw_dist, kw_iff, kw_inside,
  1521. kw_with);
  1522. }
  1523. bool isVerilogIdentifier(const FormatToken &Tok) const {
  1524. switch (Tok.Tok.getKind()) {
  1525. case tok::kw_case:
  1526. case tok::kw_class:
  1527. case tok::kw_const:
  1528. case tok::kw_continue:
  1529. case tok::kw_default:
  1530. case tok::kw_do:
  1531. case tok::kw_extern:
  1532. case tok::kw_else:
  1533. case tok::kw_enum:
  1534. case tok::kw_for:
  1535. case tok::kw_if:
  1536. case tok::kw_restrict:
  1537. case tok::kw_signed:
  1538. case tok::kw_static:
  1539. case tok::kw_struct:
  1540. case tok::kw_typedef:
  1541. case tok::kw_union:
  1542. case tok::kw_unsigned:
  1543. case tok::kw_virtual:
  1544. case tok::kw_while:
  1545. return false;
  1546. case tok::identifier:
  1547. return VerilogExtraKeywords.find(Tok.Tok.getIdentifierInfo()) ==
  1548. VerilogExtraKeywords.end();
  1549. default:
  1550. // getIdentifierInfo returns non-null for both identifiers and keywords.
  1551. return Tok.Tok.getIdentifierInfo() != nullptr;
  1552. }
  1553. }
  1554. /// Returns whether \p Tok is a Verilog preprocessor directive. This is
  1555. /// needed because macro expansions start with a backtick as well and they
  1556. /// need to be treated differently.
  1557. bool isVerilogPPDirective(const FormatToken &Tok) const {
  1558. auto Info = Tok.Tok.getIdentifierInfo();
  1559. if (!Info)
  1560. return false;
  1561. switch (Info->getPPKeywordID()) {
  1562. case tok::pp_define:
  1563. case tok::pp_else:
  1564. case tok::pp_endif:
  1565. case tok::pp_ifdef:
  1566. case tok::pp_ifndef:
  1567. case tok::pp_include:
  1568. case tok::pp_line:
  1569. case tok::pp_pragma:
  1570. case tok::pp_undef:
  1571. return true;
  1572. default:
  1573. return Tok.isOneOf(kw_begin_keywords, kw_celldefine,
  1574. kw_default_decay_time, kw_default_nettype,
  1575. kw_default_trireg_strength, kw_delay_mode_distributed,
  1576. kw_delay_mode_path, kw_delay_mode_unit,
  1577. kw_delay_mode_zero, kw_elsif, kw_end_keywords,
  1578. kw_endcelldefine, kw_nounconnected_drive, kw_resetall,
  1579. kw_timescale, kw_unconnected_drive, kw_undefineall);
  1580. }
  1581. }
  1582. /// Returns whether \p Tok is a Verilog keyword that opens a block.
  1583. bool isVerilogBegin(const FormatToken &Tok) const {
  1584. // `table` is not included since it needs to be treated specially.
  1585. return !Tok.endsSequence(kw_fork, kw_disable) &&
  1586. Tok.isOneOf(kw_begin, kw_fork, kw_generate, kw_specify);
  1587. }
  1588. /// Returns whether \p Tok is a Verilog keyword that closes a block.
  1589. bool isVerilogEnd(const FormatToken &Tok) const {
  1590. return !Tok.endsSequence(kw_join, kw_rand) &&
  1591. Tok.isOneOf(TT_MacroBlockEnd, kw_end, kw_endcase, kw_endclass,
  1592. kw_endclocking, kw_endchecker, kw_endfunction,
  1593. kw_endgenerate, kw_endgroup, kw_endinterface,
  1594. kw_endmodule, kw_endpackage, kw_endprimitive,
  1595. kw_endprogram, kw_endproperty, kw_endsequence,
  1596. kw_endspecify, kw_endtable, kw_endtask, kw_join,
  1597. kw_join_any, kw_join_none);
  1598. }
  1599. /// Returns whether \p Tok is a Verilog keyword that opens a module, etc.
  1600. bool isVerilogHierarchy(const FormatToken &Tok) const {
  1601. if (Tok.endsSequence(kw_function, kw_with))
  1602. return false;
  1603. if (Tok.is(kw_property)) {
  1604. const FormatToken *Prev = Tok.getPreviousNonComment();
  1605. return !(Prev &&
  1606. Prev->isOneOf(tok::kw_restrict, kw_assert, kw_assume, kw_cover));
  1607. }
  1608. return Tok.isOneOf(tok::kw_case, tok::kw_class, kw_function, kw_module,
  1609. kw_interface, kw_package, kw_casex, kw_casez, kw_checker,
  1610. kw_clocking, kw_covergroup, kw_macromodule, kw_primitive,
  1611. kw_program, kw_property, kw_randcase, kw_randsequence,
  1612. kw_task);
  1613. }
  1614. bool isVerilogEndOfLabel(const FormatToken &Tok) const {
  1615. const FormatToken *Next = Tok.getNextNonComment();
  1616. // In Verilog the colon in a default label is optional.
  1617. return Tok.is(TT_GotoLabelColon) ||
  1618. (Tok.is(tok::kw_default) &&
  1619. !(Next && Next->isOneOf(tok::colon, tok::semi, kw_clocking, kw_iff,
  1620. kw_input, kw_output, kw_sequence)));
  1621. }
  1622. private:
  1623. /// The JavaScript keywords beyond the C++ keyword set.
  1624. std::unordered_set<IdentifierInfo *> JsExtraKeywords;
  1625. /// The C# keywords beyond the C++ keyword set
  1626. std::unordered_set<IdentifierInfo *> CSharpExtraKeywords;
  1627. /// The Verilog keywords beyond the C++ keyword set.
  1628. std::unordered_set<IdentifierInfo *> VerilogExtraKeywords;
  1629. };
  1630. } // namespace format
  1631. } // namespace clang
  1632. #endif