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