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