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 <unordered_set> 24 25 namespace clang { 26 namespace format { 27 28 #define LIST_TOKEN_TYPES \ 29 TYPE(ArrayInitializerLSquare) \ 30 TYPE(ArraySubscriptLSquare) \ 31 TYPE(AttributeColon) \ 32 TYPE(AttributeMacro) \ 33 TYPE(AttributeParen) \ 34 TYPE(AttributeSquare) \ 35 TYPE(BinaryOperator) \ 36 TYPE(BitFieldColon) \ 37 TYPE(BlockComment) \ 38 TYPE(CastRParen) \ 39 TYPE(ConditionalExpr) \ 40 TYPE(ConflictAlternative) \ 41 TYPE(ConflictEnd) \ 42 TYPE(ConflictStart) \ 43 TYPE(ConstraintJunctions) \ 44 TYPE(CtorInitializerColon) \ 45 TYPE(CtorInitializerComma) \ 46 TYPE(DesignatedInitializerLSquare) \ 47 TYPE(DesignatedInitializerPeriod) \ 48 TYPE(DictLiteral) \ 49 TYPE(FatArrow) \ 50 TYPE(ForEachMacro) \ 51 TYPE(FunctionAnnotationRParen) \ 52 TYPE(FunctionDeclarationName) \ 53 TYPE(FunctionLBrace) \ 54 TYPE(FunctionLikeOrFreestandingMacro) \ 55 TYPE(FunctionTypeLParen) \ 56 TYPE(IfMacro) \ 57 TYPE(ImplicitStringLiteral) \ 58 TYPE(InheritanceColon) \ 59 TYPE(InheritanceComma) \ 60 TYPE(InlineASMBrace) \ 61 TYPE(InlineASMColon) \ 62 TYPE(InlineASMSymbolicNameLSquare) \ 63 TYPE(JavaAnnotation) \ 64 TYPE(JsComputedPropertyName) \ 65 TYPE(JsExponentiation) \ 66 TYPE(JsExponentiationEqual) \ 67 TYPE(JsPipePipeEqual) \ 68 TYPE(JsPrivateIdentifier) \ 69 TYPE(JsTypeColon) \ 70 TYPE(JsTypeOperator) \ 71 TYPE(JsTypeOptionalQuestion) \ 72 TYPE(JsAndAndEqual) \ 73 TYPE(LambdaArrow) \ 74 TYPE(LambdaLBrace) \ 75 TYPE(LambdaLSquare) \ 76 TYPE(LeadingJavaAnnotation) \ 77 TYPE(LineComment) \ 78 TYPE(MacroBlockBegin) \ 79 TYPE(MacroBlockEnd) \ 80 TYPE(ModulePartitionColon) \ 81 TYPE(NamespaceMacro) \ 82 TYPE(NonNullAssertion) \ 83 TYPE(NullCoalescingEqual) \ 84 TYPE(NullCoalescingOperator) \ 85 TYPE(NullPropagatingOperator) \ 86 TYPE(ObjCBlockLBrace) \ 87 TYPE(ObjCBlockLParen) \ 88 TYPE(ObjCDecl) \ 89 TYPE(ObjCForIn) \ 90 TYPE(ObjCMethodExpr) \ 91 TYPE(ObjCMethodSpecifier) \ 92 TYPE(ObjCProperty) \ 93 TYPE(ObjCStringLiteral) \ 94 TYPE(OverloadedOperator) \ 95 TYPE(OverloadedOperatorLParen) \ 96 TYPE(PointerOrReference) \ 97 TYPE(PureVirtualSpecifier) \ 98 TYPE(RangeBasedForLoopColon) \ 99 TYPE(RecordLBrace) \ 100 TYPE(RegexLiteral) \ 101 TYPE(SelectorName) \ 102 TYPE(StartOfName) \ 103 TYPE(StatementAttributeLikeMacro) \ 104 TYPE(StatementMacro) \ 105 TYPE(StructuredBindingLSquare) \ 106 TYPE(TemplateCloser) \ 107 TYPE(TemplateOpener) \ 108 TYPE(TemplateString) \ 109 TYPE(ProtoExtensionLSquare) \ 110 TYPE(TrailingAnnotation) \ 111 TYPE(TrailingReturnArrow) \ 112 TYPE(TrailingUnaryOperator) \ 113 TYPE(TypeDeclarationParen) \ 114 TYPE(TypenameMacro) \ 115 TYPE(UnaryOperator) \ 116 TYPE(UntouchableMacroFunc) \ 117 TYPE(CSharpStringLiteral) \ 118 TYPE(CSharpNamedArgumentColon) \ 119 TYPE(CSharpNullable) \ 120 TYPE(CSharpNullConditionalLSquare) \ 121 TYPE(CSharpGenericTypeConstraint) \ 122 TYPE(CSharpGenericTypeConstraintColon) \ 123 TYPE(CSharpGenericTypeConstraintComma) \ 124 TYPE(Unknown) 125 126 /// Sorted operators that can follow a C variable. 127 static const std::vector<clang::tok::TokenKind> COperatorsFollowingVar = [] { 128 std::vector<clang::tok::TokenKind> ReturnVal = { 129 tok::l_square, tok::r_square, 130 tok::l_paren, tok::r_paren, 131 tok::r_brace, tok::period, 132 tok::ellipsis, tok::ampamp, 133 tok::ampequal, tok::star, 134 tok::starequal, tok::plus, 135 tok::plusplus, tok::plusequal, 136 tok::minus, tok::arrow, 137 tok::minusminus, tok::minusequal, 138 tok::exclaim, tok::exclaimequal, 139 tok::slash, tok::slashequal, 140 tok::percent, tok::percentequal, 141 tok::less, tok::lessless, 142 tok::lessequal, tok::lesslessequal, 143 tok::greater, tok::greatergreater, 144 tok::greaterequal, tok::greatergreaterequal, 145 tok::caret, tok::caretequal, 146 tok::pipe, tok::pipepipe, 147 tok::pipeequal, tok::question, 148 tok::semi, tok::equal, 149 tok::equalequal, tok::comma}; 150 assert(std::is_sorted(ReturnVal.begin(), ReturnVal.end())); 151 return ReturnVal; 152 }(); 153 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 163 /// Determines the name of a token type. 164 const char *getTokenTypeName(TokenType Type); 165 166 // Represents what type of block a set of braces open. 167 enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit }; 168 169 // The packing kind of a function's parameters. 170 enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive }; 171 172 enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break }; 173 174 /// Roles a token can take in a configured macro expansion. 175 enum MacroRole { 176 /// The token was expanded from a macro argument when formatting the expanded 177 /// token sequence. 178 MR_ExpandedArg, 179 /// The token is part of a macro argument that was previously formatted as 180 /// expansion when formatting the unexpanded macro call. 181 MR_UnexpandedArg, 182 /// The token was expanded from a macro definition, and is not visible as part 183 /// of the macro call. 184 MR_Hidden, 185 }; 186 187 struct FormatToken; 188 189 /// Contains information on the token's role in a macro expansion. 190 /// 191 /// Given the following definitions: 192 /// A(X) = [ X ] 193 /// B(X) = < X > 194 /// C(X) = X 195 /// 196 /// Consider the macro call: 197 /// A({B(C(C(x)))}) -> [{<x>}] 198 /// 199 /// In this case, the tokens of the unexpanded macro call will have the 200 /// following relevant entries in their macro context (note that formatting 201 /// the unexpanded macro call happens *after* formatting the expanded macro 202 /// call): 203 /// A( { B( C( C(x) ) ) } ) 204 /// Role: NN U NN NN NNUN N N U N (N=None, U=UnexpandedArg) 205 /// 206 /// [ { < x > } ] 207 /// Role: H E H E H E H (H=Hidden, E=ExpandedArg) 208 /// ExpandedFrom[0]: A A A A A A A 209 /// ExpandedFrom[1]: B B B 210 /// ExpandedFrom[2]: C 211 /// ExpandedFrom[3]: C 212 /// StartOfExpansion: 1 0 1 2 0 0 0 213 /// EndOfExpansion: 0 0 0 2 1 0 1 214 struct MacroExpansion { 215 MacroExpansion(MacroRole Role) : Role(Role) {} 216 217 /// The token's role in the macro expansion. 218 /// When formatting an expanded macro, all tokens that are part of macro 219 /// arguments will be MR_ExpandedArg, while all tokens that are not visible in 220 /// the macro call will be MR_Hidden. 221 /// When formatting an unexpanded macro call, all tokens that are part of 222 /// macro arguments will be MR_UnexpandedArg. 223 MacroRole Role; 224 225 /// The stack of macro call identifier tokens this token was expanded from. 226 llvm::SmallVector<FormatToken *, 1> ExpandedFrom; 227 228 /// The number of expansions of which this macro is the first entry. 229 unsigned StartOfExpansion = 0; 230 231 /// The number of currently open expansions in \c ExpandedFrom this macro is 232 /// the last token in. 233 unsigned EndOfExpansion = 0; 234 }; 235 236 class TokenRole; 237 class AnnotatedLine; 238 239 /// A wrapper around a \c Token storing information about the 240 /// whitespace characters preceding it. 241 struct FormatToken { 242 FormatToken() 243 : HasUnescapedNewline(false), IsMultiline(false), IsFirst(false), 244 MustBreakBefore(false), IsUnterminatedLiteral(false), 245 CanBreakBefore(false), ClosesTemplateDeclaration(false), 246 StartsBinaryExpression(false), EndsBinaryExpression(false), 247 PartOfMultiVariableDeclStmt(false), ContinuesLineCommentSection(false), 248 Finalized(false), BlockKind(BK_Unknown), Decision(FD_Unformatted), 249 PackingKind(PPK_Inconclusive), Type(TT_Unknown) {} 250 251 /// The \c Token. 252 Token Tok; 253 254 /// The raw text of the token. 255 /// 256 /// Contains the raw token text without leading whitespace and without leading 257 /// escaped newlines. 258 StringRef TokenText; 259 260 /// A token can have a special role that can carry extra information 261 /// about the token's formatting. 262 /// FIXME: Make FormatToken for parsing and AnnotatedToken two different 263 /// classes and make this a unique_ptr in the AnnotatedToken class. 264 std::shared_ptr<TokenRole> Role; 265 266 /// The range of the whitespace immediately preceding the \c Token. 267 SourceRange WhitespaceRange; 268 269 /// Whether there is at least one unescaped newline before the \c 270 /// Token. 271 unsigned HasUnescapedNewline : 1; 272 273 /// Whether the token text contains newlines (escaped or not). 274 unsigned IsMultiline : 1; 275 276 /// Indicates that this is the first token of the file. 277 unsigned IsFirst : 1; 278 279 /// Whether there must be a line break before this token. 280 /// 281 /// This happens for example when a preprocessor directive ended directly 282 /// before the token. 283 unsigned MustBreakBefore : 1; 284 285 /// Set to \c true if this token is an unterminated literal. 286 unsigned IsUnterminatedLiteral : 1; 287 288 /// \c true if it is allowed to break before this token. 289 unsigned CanBreakBefore : 1; 290 291 /// \c true if this is the ">" of "template<..>". 292 unsigned ClosesTemplateDeclaration : 1; 293 294 /// \c true if this token starts a binary expression, i.e. has at least 295 /// one fake l_paren with a precedence greater than prec::Unknown. 296 unsigned StartsBinaryExpression : 1; 297 /// \c true if this token ends a binary expression. 298 unsigned EndsBinaryExpression : 1; 299 300 /// Is this token part of a \c DeclStmt defining multiple variables? 301 /// 302 /// Only set if \c Type == \c TT_StartOfName. 303 unsigned PartOfMultiVariableDeclStmt : 1; 304 305 /// Does this line comment continue a line comment section? 306 /// 307 /// Only set to true if \c Type == \c TT_LineComment. 308 unsigned ContinuesLineCommentSection : 1; 309 310 /// If \c true, this token has been fully formatted (indented and 311 /// potentially re-formatted inside), and we do not allow further formatting 312 /// changes. 313 unsigned Finalized : 1; 314 315 private: 316 /// Contains the kind of block if this token is a brace. 317 unsigned BlockKind : 2; 318 319 public: 320 BraceBlockKind getBlockKind() const { 321 return static_cast<BraceBlockKind>(BlockKind); 322 } 323 void setBlockKind(BraceBlockKind BBK) { 324 BlockKind = BBK; 325 assert(getBlockKind() == BBK && "BraceBlockKind overflow!"); 326 } 327 328 private: 329 /// Stores the formatting decision for the token once it was made. 330 unsigned Decision : 2; 331 332 public: 333 FormatDecision getDecision() const { 334 return static_cast<FormatDecision>(Decision); 335 } 336 void setDecision(FormatDecision D) { 337 Decision = D; 338 assert(getDecision() == D && "FormatDecision overflow!"); 339 } 340 341 private: 342 /// If this is an opening parenthesis, how are the parameters packed? 343 unsigned PackingKind : 2; 344 345 public: 346 ParameterPackingKind getPackingKind() const { 347 return static_cast<ParameterPackingKind>(PackingKind); 348 } 349 void setPackingKind(ParameterPackingKind K) { 350 PackingKind = K; 351 assert(getPackingKind() == K && "ParameterPackingKind overflow!"); 352 } 353 354 private: 355 TokenType Type; 356 357 public: 358 /// Returns the token's type, e.g. whether "<" is a template opener or 359 /// binary operator. 360 TokenType getType() const { return Type; } 361 void setType(TokenType T) { Type = T; } 362 363 /// The number of newlines immediately before the \c Token. 364 /// 365 /// This can be used to determine what the user wrote in the original code 366 /// and thereby e.g. leave an empty line between two function definitions. 367 unsigned NewlinesBefore = 0; 368 369 /// The offset just past the last '\n' in this token's leading 370 /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'. 371 unsigned LastNewlineOffset = 0; 372 373 /// The width of the non-whitespace parts of the token (or its first 374 /// line for multi-line tokens) in columns. 375 /// We need this to correctly measure number of columns a token spans. 376 unsigned ColumnWidth = 0; 377 378 /// Contains the width in columns of the last line of a multi-line 379 /// token. 380 unsigned LastLineColumnWidth = 0; 381 382 /// The number of spaces that should be inserted before this token. 383 unsigned SpacesRequiredBefore = 0; 384 385 /// Number of parameters, if this is "(", "[" or "<". 386 unsigned ParameterCount = 0; 387 388 /// Number of parameters that are nested blocks, 389 /// if this is "(", "[" or "<". 390 unsigned BlockParameterCount = 0; 391 392 /// If this is a bracket ("<", "(", "[" or "{"), contains the kind of 393 /// the surrounding bracket. 394 tok::TokenKind ParentBracket = tok::unknown; 395 396 /// The total length of the unwrapped line up to and including this 397 /// token. 398 unsigned TotalLength = 0; 399 400 /// The original 0-based column of this token, including expanded tabs. 401 /// The configured TabWidth is used as tab width. 402 unsigned OriginalColumn = 0; 403 404 /// The length of following tokens until the next natural split point, 405 /// or the next token that can be broken. 406 unsigned UnbreakableTailLength = 0; 407 408 // FIXME: Come up with a 'cleaner' concept. 409 /// The binding strength of a token. This is a combined value of 410 /// operator precedence, parenthesis nesting, etc. 411 unsigned BindingStrength = 0; 412 413 /// The nesting level of this token, i.e. the number of surrounding (), 414 /// [], {} or <>. 415 unsigned NestingLevel = 0; 416 417 /// The indent level of this token. Copied from the surrounding line. 418 unsigned IndentLevel = 0; 419 420 /// Penalty for inserting a line break before this token. 421 unsigned SplitPenalty = 0; 422 423 /// If this is the first ObjC selector name in an ObjC method 424 /// definition or call, this contains the length of the longest name. 425 /// 426 /// This being set to 0 means that the selectors should not be colon-aligned, 427 /// e.g. because several of them are block-type. 428 unsigned LongestObjCSelectorName = 0; 429 430 /// If this is the first ObjC selector name in an ObjC method 431 /// definition or call, this contains the number of parts that the whole 432 /// selector consist of. 433 unsigned ObjCSelectorNameParts = 0; 434 435 /// The 0-based index of the parameter/argument. For ObjC it is set 436 /// for the selector name token. 437 /// For now calculated only for ObjC. 438 unsigned ParameterIndex = 0; 439 440 /// Stores the number of required fake parentheses and the 441 /// corresponding operator precedence. 442 /// 443 /// If multiple fake parentheses start at a token, this vector stores them in 444 /// reverse order, i.e. inner fake parenthesis first. 445 SmallVector<prec::Level, 4> FakeLParens; 446 /// Insert this many fake ) after this token for correct indentation. 447 unsigned FakeRParens = 0; 448 449 /// If this is an operator (or "."/"->") in a sequence of operators 450 /// with the same precedence, contains the 0-based operator index. 451 unsigned OperatorIndex = 0; 452 453 /// If this is an operator (or "."/"->") in a sequence of operators 454 /// with the same precedence, points to the next operator. 455 FormatToken *NextOperator = nullptr; 456 457 /// If this is a bracket, this points to the matching one. 458 FormatToken *MatchingParen = nullptr; 459 460 /// The previous token in the unwrapped line. 461 FormatToken *Previous = nullptr; 462 463 /// The next token in the unwrapped line. 464 FormatToken *Next = nullptr; 465 466 /// The first token in set of column elements. 467 bool StartsColumn = false; 468 469 /// This notes the start of the line of an array initializer. 470 bool ArrayInitializerLineStart = false; 471 472 /// This starts an array initializer. 473 bool IsArrayInitializer = false; 474 475 /// Is optional and can be removed. 476 bool Optional = false; 477 478 /// If this token starts a block, this contains all the unwrapped lines 479 /// in it. 480 SmallVector<AnnotatedLine *, 1> Children; 481 482 // Contains all attributes related to how this token takes part 483 // in a configured macro expansion. 484 llvm::Optional<MacroExpansion> MacroCtx; 485 486 bool is(tok::TokenKind Kind) const { return Tok.is(Kind); } 487 bool is(TokenType TT) const { return getType() == TT; } 488 bool is(const IdentifierInfo *II) const { 489 return II && II == Tok.getIdentifierInfo(); 490 } 491 bool is(tok::PPKeywordKind Kind) const { 492 return Tok.getIdentifierInfo() && 493 Tok.getIdentifierInfo()->getPPKeywordID() == Kind; 494 } 495 bool is(BraceBlockKind BBK) const { return getBlockKind() == BBK; } 496 bool is(ParameterPackingKind PPK) const { return getPackingKind() == PPK; } 497 498 template <typename A, typename B> bool isOneOf(A K1, B K2) const { 499 return is(K1) || is(K2); 500 } 501 template <typename A, typename B, typename... Ts> 502 bool isOneOf(A K1, B K2, Ts... Ks) const { 503 return is(K1) || isOneOf(K2, Ks...); 504 } 505 template <typename T> bool isNot(T Kind) const { return !is(Kind); } 506 507 bool isIf(bool AllowConstexprMacro = true) const { 508 return is(tok::kw_if) || endsSequence(tok::kw_constexpr, tok::kw_if) || 509 (endsSequence(tok::identifier, tok::kw_if) && AllowConstexprMacro); 510 } 511 512 bool closesScopeAfterBlock() const { 513 if (getBlockKind() == BK_Block) 514 return true; 515 if (closesScope()) 516 return Previous->closesScopeAfterBlock(); 517 return false; 518 } 519 520 /// \c true if this token starts a sequence with the given tokens in order, 521 /// following the ``Next`` pointers, ignoring comments. 522 template <typename A, typename... Ts> 523 bool startsSequence(A K1, Ts... Tokens) const { 524 return startsSequenceInternal(K1, Tokens...); 525 } 526 527 /// \c true if this token ends a sequence with the given tokens in order, 528 /// following the ``Previous`` pointers, ignoring comments. 529 /// For example, given tokens [T1, T2, T3], the function returns true if 530 /// 3 tokens ending at this (ignoring comments) are [T3, T2, T1]. In other 531 /// words, the tokens passed to this function need to the reverse of the 532 /// order the tokens appear in code. 533 template <typename A, typename... Ts> 534 bool endsSequence(A K1, Ts... Tokens) const { 535 return endsSequenceInternal(K1, Tokens...); 536 } 537 538 bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); } 539 540 bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const { 541 return Tok.isObjCAtKeyword(Kind); 542 } 543 544 bool isAccessSpecifier(bool ColonRequired = true) const { 545 return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) && 546 (!ColonRequired || (Next && Next->is(tok::colon))); 547 } 548 549 bool canBePointerOrReferenceQualifier() const { 550 return isOneOf(tok::kw_const, tok::kw_restrict, tok::kw_volatile, 551 tok::kw___attribute, tok::kw__Nonnull, tok::kw__Nullable, 552 tok::kw__Null_unspecified, tok::kw___ptr32, tok::kw___ptr64, 553 TT_AttributeMacro); 554 } 555 556 /// Determine whether the token is a simple-type-specifier. 557 LLVM_NODISCARD bool isSimpleTypeSpecifier() const; 558 559 LLVM_NODISCARD bool isTypeOrIdentifier() const; 560 561 bool isObjCAccessSpecifier() const { 562 return is(tok::at) && Next && 563 (Next->isObjCAtKeyword(tok::objc_public) || 564 Next->isObjCAtKeyword(tok::objc_protected) || 565 Next->isObjCAtKeyword(tok::objc_package) || 566 Next->isObjCAtKeyword(tok::objc_private)); 567 } 568 569 /// Returns whether \p Tok is ([{ or an opening < of a template or in 570 /// protos. 571 bool opensScope() const { 572 if (is(TT_TemplateString) && TokenText.endswith("${")) 573 return true; 574 if (is(TT_DictLiteral) && is(tok::less)) 575 return true; 576 return isOneOf(tok::l_paren, tok::l_brace, tok::l_square, 577 TT_TemplateOpener); 578 } 579 /// Returns whether \p Tok is )]} or a closing > of a template or in 580 /// protos. 581 bool closesScope() const { 582 if (is(TT_TemplateString) && TokenText.startswith("}")) 583 return true; 584 if (is(TT_DictLiteral) && is(tok::greater)) 585 return true; 586 return isOneOf(tok::r_paren, tok::r_brace, tok::r_square, 587 TT_TemplateCloser); 588 } 589 590 /// Returns \c true if this is a "." or "->" accessing a member. 591 bool isMemberAccess() const { 592 return isOneOf(tok::arrow, tok::period, tok::arrowstar) && 593 !isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow, 594 TT_LambdaArrow, TT_LeadingJavaAnnotation); 595 } 596 597 bool isUnaryOperator() const { 598 switch (Tok.getKind()) { 599 case tok::plus: 600 case tok::plusplus: 601 case tok::minus: 602 case tok::minusminus: 603 case tok::exclaim: 604 case tok::tilde: 605 case tok::kw_sizeof: 606 case tok::kw_alignof: 607 return true; 608 default: 609 return false; 610 } 611 } 612 613 bool isBinaryOperator() const { 614 // Comma is a binary operator, but does not behave as such wrt. formatting. 615 return getPrecedence() > prec::Comma; 616 } 617 618 bool isTrailingComment() const { 619 return is(tok::comment) && 620 (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0); 621 } 622 623 /// Returns \c true if this is a keyword that can be used 624 /// like a function call (e.g. sizeof, typeid, ...). 625 bool isFunctionLikeKeyword() const { 626 switch (Tok.getKind()) { 627 case tok::kw_throw: 628 case tok::kw_typeid: 629 case tok::kw_return: 630 case tok::kw_sizeof: 631 case tok::kw_alignof: 632 case tok::kw_alignas: 633 case tok::kw_decltype: 634 case tok::kw_noexcept: 635 case tok::kw_static_assert: 636 case tok::kw__Atomic: 637 case tok::kw___attribute: 638 case tok::kw___underlying_type: 639 case tok::kw_requires: 640 return true; 641 default: 642 return false; 643 } 644 } 645 646 /// Returns \c true if this is a string literal that's like a label, 647 /// e.g. ends with "=" or ":". 648 bool isLabelString() const { 649 if (!is(tok::string_literal)) 650 return false; 651 StringRef Content = TokenText; 652 if (Content.startswith("\"") || Content.startswith("'")) 653 Content = Content.drop_front(1); 654 if (Content.endswith("\"") || Content.endswith("'")) 655 Content = Content.drop_back(1); 656 Content = Content.trim(); 657 return Content.size() > 1 && 658 (Content.back() == ':' || Content.back() == '='); 659 } 660 661 /// Returns actual token start location without leading escaped 662 /// newlines and whitespace. 663 /// 664 /// This can be different to Tok.getLocation(), which includes leading escaped 665 /// newlines. 666 SourceLocation getStartOfNonWhitespace() const { 667 return WhitespaceRange.getEnd(); 668 } 669 670 /// Returns \c true if the range of whitespace immediately preceding the \c 671 /// Token is not empty. 672 bool hasWhitespaceBefore() const { 673 return WhitespaceRange.getBegin() != WhitespaceRange.getEnd(); 674 } 675 676 prec::Level getPrecedence() const { 677 return getBinOpPrecedence(Tok.getKind(), /*GreaterThanIsOperator=*/true, 678 /*CPlusPlus11=*/true); 679 } 680 681 /// Returns the previous token ignoring comments. 682 FormatToken *getPreviousNonComment() const { 683 FormatToken *Tok = Previous; 684 while (Tok && Tok->is(tok::comment)) 685 Tok = Tok->Previous; 686 return Tok; 687 } 688 689 /// Returns the next token ignoring comments. 690 const FormatToken *getNextNonComment() const { 691 const FormatToken *Tok = Next; 692 while (Tok && Tok->is(tok::comment)) 693 Tok = Tok->Next; 694 return Tok; 695 } 696 697 /// Returns \c true if this tokens starts a block-type list, i.e. a 698 /// list that should be indented with a block indent. 699 LLVM_NODISCARD bool opensBlockOrBlockTypeList(const FormatStyle &Style) const; 700 701 /// Returns whether the token is the left square bracket of a C++ 702 /// structured binding declaration. 703 bool isCppStructuredBinding(const FormatStyle &Style) const { 704 if (!Style.isCpp() || isNot(tok::l_square)) 705 return false; 706 const FormatToken *T = this; 707 do { 708 T = T->getPreviousNonComment(); 709 } while (T && T->isOneOf(tok::kw_const, tok::kw_volatile, tok::amp, 710 tok::ampamp)); 711 return T && T->is(tok::kw_auto); 712 } 713 714 /// Same as opensBlockOrBlockTypeList, but for the closing token. 715 bool closesBlockOrBlockTypeList(const FormatStyle &Style) const { 716 if (is(TT_TemplateString) && closesScope()) 717 return true; 718 return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style); 719 } 720 721 /// Return the actual namespace token, if this token starts a namespace 722 /// block. 723 const FormatToken *getNamespaceToken() const { 724 const FormatToken *NamespaceTok = this; 725 if (is(tok::comment)) 726 NamespaceTok = NamespaceTok->getNextNonComment(); 727 // Detect "(inline|export)? namespace" in the beginning of a line. 728 if (NamespaceTok && NamespaceTok->isOneOf(tok::kw_inline, tok::kw_export)) 729 NamespaceTok = NamespaceTok->getNextNonComment(); 730 return NamespaceTok && 731 NamespaceTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) 732 ? NamespaceTok 733 : nullptr; 734 } 735 736 void copyFrom(const FormatToken &Tok) { *this = Tok; } 737 738 private: 739 // Only allow copying via the explicit copyFrom method. 740 FormatToken(const FormatToken &) = delete; 741 FormatToken &operator=(const FormatToken &) = default; 742 743 template <typename A, typename... Ts> 744 bool startsSequenceInternal(A K1, Ts... Tokens) const { 745 if (is(tok::comment) && Next) 746 return Next->startsSequenceInternal(K1, Tokens...); 747 return is(K1) && Next && Next->startsSequenceInternal(Tokens...); 748 } 749 750 template <typename A> bool startsSequenceInternal(A K1) const { 751 if (is(tok::comment) && Next) 752 return Next->startsSequenceInternal(K1); 753 return is(K1); 754 } 755 756 template <typename A, typename... Ts> bool endsSequenceInternal(A K1) const { 757 if (is(tok::comment) && Previous) 758 return Previous->endsSequenceInternal(K1); 759 return is(K1); 760 } 761 762 template <typename A, typename... Ts> 763 bool endsSequenceInternal(A K1, Ts... Tokens) const { 764 if (is(tok::comment) && Previous) 765 return Previous->endsSequenceInternal(K1, Tokens...); 766 return is(K1) && Previous && Previous->endsSequenceInternal(Tokens...); 767 } 768 }; 769 770 class ContinuationIndenter; 771 struct LineState; 772 773 class TokenRole { 774 public: 775 TokenRole(const FormatStyle &Style) : Style(Style) {} 776 virtual ~TokenRole(); 777 778 /// After the \c TokenAnnotator has finished annotating all the tokens, 779 /// this function precomputes required information for formatting. 780 virtual void precomputeFormattingInfos(const FormatToken *Token); 781 782 /// Apply the special formatting that the given role demands. 783 /// 784 /// Assumes that the token having this role is already formatted. 785 /// 786 /// Continues formatting from \p State leaving indentation to \p Indenter and 787 /// returns the total penalty that this formatting incurs. 788 virtual unsigned formatFromToken(LineState &State, 789 ContinuationIndenter *Indenter, 790 bool DryRun) { 791 return 0; 792 } 793 794 /// Same as \c formatFromToken, but assumes that the first token has 795 /// already been set thereby deciding on the first line break. 796 virtual unsigned formatAfterToken(LineState &State, 797 ContinuationIndenter *Indenter, 798 bool DryRun) { 799 return 0; 800 } 801 802 /// Notifies the \c Role that a comma was found. 803 virtual void CommaFound(const FormatToken *Token) {} 804 805 virtual const FormatToken *lastComma() { return nullptr; } 806 807 protected: 808 const FormatStyle &Style; 809 }; 810 811 class CommaSeparatedList : public TokenRole { 812 public: 813 CommaSeparatedList(const FormatStyle &Style) 814 : TokenRole(Style), HasNestedBracedList(false) {} 815 816 void precomputeFormattingInfos(const FormatToken *Token) override; 817 818 unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter, 819 bool DryRun) override; 820 821 unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter, 822 bool DryRun) override; 823 824 /// Adds \p Token as the next comma to the \c CommaSeparated list. 825 void CommaFound(const FormatToken *Token) override { 826 Commas.push_back(Token); 827 } 828 829 const FormatToken *lastComma() override { 830 if (Commas.empty()) 831 return nullptr; 832 return Commas.back(); 833 } 834 835 private: 836 /// A struct that holds information on how to format a given list with 837 /// a specific number of columns. 838 struct ColumnFormat { 839 /// The number of columns to use. 840 unsigned Columns; 841 842 /// The total width in characters. 843 unsigned TotalWidth; 844 845 /// The number of lines required for this format. 846 unsigned LineCount; 847 848 /// The size of each column in characters. 849 SmallVector<unsigned, 8> ColumnSizes; 850 }; 851 852 /// Calculate which \c ColumnFormat fits best into 853 /// \p RemainingCharacters. 854 const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const; 855 856 /// The ordered \c FormatTokens making up the commas of this list. 857 SmallVector<const FormatToken *, 8> Commas; 858 859 /// The length of each of the list's items in characters including the 860 /// trailing comma. 861 SmallVector<unsigned, 8> ItemLengths; 862 863 /// Precomputed formats that can be used for this list. 864 SmallVector<ColumnFormat, 4> Formats; 865 866 bool HasNestedBracedList; 867 }; 868 869 /// Encapsulates keywords that are context sensitive or for languages not 870 /// properly supported by Clang's lexer. 871 struct AdditionalKeywords { 872 AdditionalKeywords(IdentifierTable &IdentTable) { 873 kw_final = &IdentTable.get("final"); 874 kw_override = &IdentTable.get("override"); 875 kw_in = &IdentTable.get("in"); 876 kw_of = &IdentTable.get("of"); 877 kw_CF_CLOSED_ENUM = &IdentTable.get("CF_CLOSED_ENUM"); 878 kw_CF_ENUM = &IdentTable.get("CF_ENUM"); 879 kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS"); 880 kw_NS_CLOSED_ENUM = &IdentTable.get("NS_CLOSED_ENUM"); 881 kw_NS_ENUM = &IdentTable.get("NS_ENUM"); 882 kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS"); 883 884 kw_as = &IdentTable.get("as"); 885 kw_async = &IdentTable.get("async"); 886 kw_await = &IdentTable.get("await"); 887 kw_declare = &IdentTable.get("declare"); 888 kw_finally = &IdentTable.get("finally"); 889 kw_from = &IdentTable.get("from"); 890 kw_function = &IdentTable.get("function"); 891 kw_get = &IdentTable.get("get"); 892 kw_import = &IdentTable.get("import"); 893 kw_infer = &IdentTable.get("infer"); 894 kw_is = &IdentTable.get("is"); 895 kw_let = &IdentTable.get("let"); 896 kw_module = &IdentTable.get("module"); 897 kw_readonly = &IdentTable.get("readonly"); 898 kw_set = &IdentTable.get("set"); 899 kw_type = &IdentTable.get("type"); 900 kw_typeof = &IdentTable.get("typeof"); 901 kw_var = &IdentTable.get("var"); 902 kw_yield = &IdentTable.get("yield"); 903 904 kw_abstract = &IdentTable.get("abstract"); 905 kw_assert = &IdentTable.get("assert"); 906 kw_extends = &IdentTable.get("extends"); 907 kw_implements = &IdentTable.get("implements"); 908 kw_instanceof = &IdentTable.get("instanceof"); 909 kw_interface = &IdentTable.get("interface"); 910 kw_native = &IdentTable.get("native"); 911 kw_package = &IdentTable.get("package"); 912 kw_synchronized = &IdentTable.get("synchronized"); 913 kw_throws = &IdentTable.get("throws"); 914 kw___except = &IdentTable.get("__except"); 915 kw___has_include = &IdentTable.get("__has_include"); 916 kw___has_include_next = &IdentTable.get("__has_include_next"); 917 918 kw_mark = &IdentTable.get("mark"); 919 920 kw_extend = &IdentTable.get("extend"); 921 kw_option = &IdentTable.get("option"); 922 kw_optional = &IdentTable.get("optional"); 923 kw_repeated = &IdentTable.get("repeated"); 924 kw_required = &IdentTable.get("required"); 925 kw_returns = &IdentTable.get("returns"); 926 927 kw_signals = &IdentTable.get("signals"); 928 kw_qsignals = &IdentTable.get("Q_SIGNALS"); 929 kw_slots = &IdentTable.get("slots"); 930 kw_qslots = &IdentTable.get("Q_SLOTS"); 931 932 // C# keywords 933 kw_dollar = &IdentTable.get("dollar"); 934 kw_base = &IdentTable.get("base"); 935 kw_byte = &IdentTable.get("byte"); 936 kw_checked = &IdentTable.get("checked"); 937 kw_decimal = &IdentTable.get("decimal"); 938 kw_delegate = &IdentTable.get("delegate"); 939 kw_event = &IdentTable.get("event"); 940 kw_fixed = &IdentTable.get("fixed"); 941 kw_foreach = &IdentTable.get("foreach"); 942 kw_implicit = &IdentTable.get("implicit"); 943 kw_internal = &IdentTable.get("internal"); 944 kw_lock = &IdentTable.get("lock"); 945 kw_null = &IdentTable.get("null"); 946 kw_object = &IdentTable.get("object"); 947 kw_out = &IdentTable.get("out"); 948 kw_params = &IdentTable.get("params"); 949 kw_ref = &IdentTable.get("ref"); 950 kw_string = &IdentTable.get("string"); 951 kw_stackalloc = &IdentTable.get("stackalloc"); 952 kw_sbyte = &IdentTable.get("sbyte"); 953 kw_sealed = &IdentTable.get("sealed"); 954 kw_uint = &IdentTable.get("uint"); 955 kw_ulong = &IdentTable.get("ulong"); 956 kw_unchecked = &IdentTable.get("unchecked"); 957 kw_unsafe = &IdentTable.get("unsafe"); 958 kw_ushort = &IdentTable.get("ushort"); 959 kw_when = &IdentTable.get("when"); 960 kw_where = &IdentTable.get("where"); 961 962 // Keep this at the end of the constructor to make sure everything here 963 // is 964 // already initialized. 965 JsExtraKeywords = std::unordered_set<IdentifierInfo *>( 966 {kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from, 967 kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_override, 968 kw_readonly, kw_set, kw_type, kw_typeof, kw_var, kw_yield, 969 // Keywords from the Java section. 970 kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface}); 971 972 CSharpExtraKeywords = std::unordered_set<IdentifierInfo *>( 973 {kw_base, kw_byte, kw_checked, kw_decimal, kw_delegate, kw_event, 974 kw_fixed, kw_foreach, kw_implicit, kw_in, kw_interface, kw_internal, 975 kw_is, kw_lock, kw_null, kw_object, kw_out, kw_override, kw_params, 976 kw_readonly, kw_ref, kw_string, kw_stackalloc, kw_sbyte, kw_sealed, 977 kw_uint, kw_ulong, kw_unchecked, kw_unsafe, kw_ushort, kw_when, 978 kw_where, 979 // Keywords from the JavaScript section. 980 kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from, 981 kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_readonly, 982 kw_set, kw_type, kw_typeof, kw_var, kw_yield, 983 // Keywords from the Java section. 984 kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface}); 985 } 986 987 // Context sensitive keywords. 988 IdentifierInfo *kw_final; 989 IdentifierInfo *kw_override; 990 IdentifierInfo *kw_in; 991 IdentifierInfo *kw_of; 992 IdentifierInfo *kw_CF_CLOSED_ENUM; 993 IdentifierInfo *kw_CF_ENUM; 994 IdentifierInfo *kw_CF_OPTIONS; 995 IdentifierInfo *kw_NS_CLOSED_ENUM; 996 IdentifierInfo *kw_NS_ENUM; 997 IdentifierInfo *kw_NS_OPTIONS; 998 IdentifierInfo *kw___except; 999 IdentifierInfo *kw___has_include; 1000 IdentifierInfo *kw___has_include_next; 1001 1002 // JavaScript keywords. 1003 IdentifierInfo *kw_as; 1004 IdentifierInfo *kw_async; 1005 IdentifierInfo *kw_await; 1006 IdentifierInfo *kw_declare; 1007 IdentifierInfo *kw_finally; 1008 IdentifierInfo *kw_from; 1009 IdentifierInfo *kw_function; 1010 IdentifierInfo *kw_get; 1011 IdentifierInfo *kw_import; 1012 IdentifierInfo *kw_infer; 1013 IdentifierInfo *kw_is; 1014 IdentifierInfo *kw_let; 1015 IdentifierInfo *kw_module; 1016 IdentifierInfo *kw_readonly; 1017 IdentifierInfo *kw_set; 1018 IdentifierInfo *kw_type; 1019 IdentifierInfo *kw_typeof; 1020 IdentifierInfo *kw_var; 1021 IdentifierInfo *kw_yield; 1022 1023 // Java keywords. 1024 IdentifierInfo *kw_abstract; 1025 IdentifierInfo *kw_assert; 1026 IdentifierInfo *kw_extends; 1027 IdentifierInfo *kw_implements; 1028 IdentifierInfo *kw_instanceof; 1029 IdentifierInfo *kw_interface; 1030 IdentifierInfo *kw_native; 1031 IdentifierInfo *kw_package; 1032 IdentifierInfo *kw_synchronized; 1033 IdentifierInfo *kw_throws; 1034 1035 // Pragma keywords. 1036 IdentifierInfo *kw_mark; 1037 1038 // Proto keywords. 1039 IdentifierInfo *kw_extend; 1040 IdentifierInfo *kw_option; 1041 IdentifierInfo *kw_optional; 1042 IdentifierInfo *kw_repeated; 1043 IdentifierInfo *kw_required; 1044 IdentifierInfo *kw_returns; 1045 1046 // QT keywords. 1047 IdentifierInfo *kw_signals; 1048 IdentifierInfo *kw_qsignals; 1049 IdentifierInfo *kw_slots; 1050 IdentifierInfo *kw_qslots; 1051 1052 // C# keywords 1053 IdentifierInfo *kw_dollar; 1054 IdentifierInfo *kw_base; 1055 IdentifierInfo *kw_byte; 1056 IdentifierInfo *kw_checked; 1057 IdentifierInfo *kw_decimal; 1058 IdentifierInfo *kw_delegate; 1059 IdentifierInfo *kw_event; 1060 IdentifierInfo *kw_fixed; 1061 IdentifierInfo *kw_foreach; 1062 IdentifierInfo *kw_implicit; 1063 IdentifierInfo *kw_internal; 1064 1065 IdentifierInfo *kw_lock; 1066 IdentifierInfo *kw_null; 1067 IdentifierInfo *kw_object; 1068 IdentifierInfo *kw_out; 1069 1070 IdentifierInfo *kw_params; 1071 1072 IdentifierInfo *kw_ref; 1073 IdentifierInfo *kw_string; 1074 IdentifierInfo *kw_stackalloc; 1075 IdentifierInfo *kw_sbyte; 1076 IdentifierInfo *kw_sealed; 1077 IdentifierInfo *kw_uint; 1078 IdentifierInfo *kw_ulong; 1079 IdentifierInfo *kw_unchecked; 1080 IdentifierInfo *kw_unsafe; 1081 IdentifierInfo *kw_ushort; 1082 IdentifierInfo *kw_when; 1083 IdentifierInfo *kw_where; 1084 1085 /// Returns \c true if \p Tok is a true JavaScript identifier, returns 1086 /// \c false if it is a keyword or a pseudo keyword. 1087 /// If \c AcceptIdentifierName is true, returns true not only for keywords, 1088 // but also for IdentifierName tokens (aka pseudo-keywords), such as 1089 // ``yield``. 1090 bool IsJavaScriptIdentifier(const FormatToken &Tok, 1091 bool AcceptIdentifierName = true) const { 1092 // Based on the list of JavaScript & TypeScript keywords here: 1093 // https://github.com/microsoft/TypeScript/blob/main/src/compiler/scanner.ts#L74 1094 switch (Tok.Tok.getKind()) { 1095 case tok::kw_break: 1096 case tok::kw_case: 1097 case tok::kw_catch: 1098 case tok::kw_class: 1099 case tok::kw_continue: 1100 case tok::kw_const: 1101 case tok::kw_default: 1102 case tok::kw_delete: 1103 case tok::kw_do: 1104 case tok::kw_else: 1105 case tok::kw_enum: 1106 case tok::kw_export: 1107 case tok::kw_false: 1108 case tok::kw_for: 1109 case tok::kw_if: 1110 case tok::kw_import: 1111 case tok::kw_module: 1112 case tok::kw_new: 1113 case tok::kw_private: 1114 case tok::kw_protected: 1115 case tok::kw_public: 1116 case tok::kw_return: 1117 case tok::kw_static: 1118 case tok::kw_switch: 1119 case tok::kw_this: 1120 case tok::kw_throw: 1121 case tok::kw_true: 1122 case tok::kw_try: 1123 case tok::kw_typeof: 1124 case tok::kw_void: 1125 case tok::kw_while: 1126 // These are JS keywords that are lexed by LLVM/clang as keywords. 1127 return false; 1128 case tok::identifier: { 1129 // For identifiers, make sure they are true identifiers, excluding the 1130 // JavaScript pseudo-keywords (not lexed by LLVM/clang as keywords). 1131 bool IsPseudoKeyword = 1132 JsExtraKeywords.find(Tok.Tok.getIdentifierInfo()) != 1133 JsExtraKeywords.end(); 1134 return AcceptIdentifierName || !IsPseudoKeyword; 1135 } 1136 default: 1137 // Other keywords are handled in the switch below, to avoid problems due 1138 // to duplicate case labels when using the #include trick. 1139 break; 1140 } 1141 1142 switch (Tok.Tok.getKind()) { 1143 // Handle C++ keywords not included above: these are all JS identifiers. 1144 #define KEYWORD(X, Y) case tok::kw_##X: 1145 #include "clang/Basic/TokenKinds.def" 1146 // #undef KEYWORD is not needed -- it's #undef-ed at the end of 1147 // TokenKinds.def 1148 return true; 1149 default: 1150 // All other tokens (punctuation etc) are not JS identifiers. 1151 return false; 1152 } 1153 } 1154 1155 /// Returns \c true if \p Tok is a C# keyword, returns 1156 /// \c false if it is a anything else. 1157 bool isCSharpKeyword(const FormatToken &Tok) const { 1158 switch (Tok.Tok.getKind()) { 1159 case tok::kw_bool: 1160 case tok::kw_break: 1161 case tok::kw_case: 1162 case tok::kw_catch: 1163 case tok::kw_char: 1164 case tok::kw_class: 1165 case tok::kw_const: 1166 case tok::kw_continue: 1167 case tok::kw_default: 1168 case tok::kw_do: 1169 case tok::kw_double: 1170 case tok::kw_else: 1171 case tok::kw_enum: 1172 case tok::kw_explicit: 1173 case tok::kw_extern: 1174 case tok::kw_false: 1175 case tok::kw_float: 1176 case tok::kw_for: 1177 case tok::kw_goto: 1178 case tok::kw_if: 1179 case tok::kw_int: 1180 case tok::kw_long: 1181 case tok::kw_namespace: 1182 case tok::kw_new: 1183 case tok::kw_operator: 1184 case tok::kw_private: 1185 case tok::kw_protected: 1186 case tok::kw_public: 1187 case tok::kw_return: 1188 case tok::kw_short: 1189 case tok::kw_sizeof: 1190 case tok::kw_static: 1191 case tok::kw_struct: 1192 case tok::kw_switch: 1193 case tok::kw_this: 1194 case tok::kw_throw: 1195 case tok::kw_true: 1196 case tok::kw_try: 1197 case tok::kw_typeof: 1198 case tok::kw_using: 1199 case tok::kw_virtual: 1200 case tok::kw_void: 1201 case tok::kw_volatile: 1202 case tok::kw_while: 1203 return true; 1204 default: 1205 return Tok.is(tok::identifier) && 1206 CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) == 1207 CSharpExtraKeywords.end(); 1208 } 1209 } 1210 1211 private: 1212 /// The JavaScript keywords beyond the C++ keyword set. 1213 std::unordered_set<IdentifierInfo *> JsExtraKeywords; 1214 1215 /// The C# keywords beyond the C++ keyword set 1216 std::unordered_set<IdentifierInfo *> CSharpExtraKeywords; 1217 }; 1218 1219 } // namespace format 1220 } // namespace clang 1221 1222 #endif 1223