1 //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===// 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 implementation of the UnwrappedLineParser, 11 /// which turns a stream of tokens into UnwrappedLines. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "UnwrappedLineParser.h" 16 #include "FormatToken.h" 17 #include "TokenAnnotator.h" 18 #include "clang/Basic/TokenKinds.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/Support/Debug.h" 21 #include "llvm/Support/raw_ostream.h" 22 23 #include <algorithm> 24 #include <utility> 25 26 #define DEBUG_TYPE "format-parser" 27 28 namespace clang { 29 namespace format { 30 31 class FormatTokenSource { 32 public: 33 virtual ~FormatTokenSource() {} 34 35 // Returns the next token in the token stream. 36 virtual FormatToken *getNextToken() = 0; 37 38 // Returns the token preceding the token returned by the last call to 39 // getNextToken() in the token stream, or nullptr if no such token exists. 40 virtual FormatToken *getPreviousToken() = 0; 41 42 // Returns the token that would be returned by the next call to 43 // getNextToken(). 44 virtual FormatToken *peekNextToken() = 0; 45 46 // Returns the token that would be returned after the next N calls to 47 // getNextToken(). N needs to be greater than zero, and small enough that 48 // there are still tokens. Check for tok::eof with N-1 before calling it with 49 // N. 50 virtual FormatToken *peekNextToken(int N) = 0; 51 52 // Returns whether we are at the end of the file. 53 // This can be different from whether getNextToken() returned an eof token 54 // when the FormatTokenSource is a view on a part of the token stream. 55 virtual bool isEOF() = 0; 56 57 // Gets the current position in the token stream, to be used by setPosition(). 58 virtual unsigned getPosition() = 0; 59 60 // Resets the token stream to the state it was in when getPosition() returned 61 // Position, and return the token at that position in the stream. 62 virtual FormatToken *setPosition(unsigned Position) = 0; 63 }; 64 65 namespace { 66 67 class ScopedDeclarationState { 68 public: 69 ScopedDeclarationState(UnwrappedLine &Line, llvm::BitVector &Stack, 70 bool MustBeDeclaration) 71 : Line(Line), Stack(Stack) { 72 Line.MustBeDeclaration = MustBeDeclaration; 73 Stack.push_back(MustBeDeclaration); 74 } 75 ~ScopedDeclarationState() { 76 Stack.pop_back(); 77 if (!Stack.empty()) 78 Line.MustBeDeclaration = Stack.back(); 79 else 80 Line.MustBeDeclaration = true; 81 } 82 83 private: 84 UnwrappedLine &Line; 85 llvm::BitVector &Stack; 86 }; 87 88 static bool isLineComment(const FormatToken &FormatTok) { 89 return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith("/*"); 90 } 91 92 // Checks if \p FormatTok is a line comment that continues the line comment 93 // \p Previous. The original column of \p MinColumnToken is used to determine 94 // whether \p FormatTok is indented enough to the right to continue \p Previous. 95 static bool continuesLineComment(const FormatToken &FormatTok, 96 const FormatToken *Previous, 97 const FormatToken *MinColumnToken) { 98 if (!Previous || !MinColumnToken) 99 return false; 100 unsigned MinContinueColumn = 101 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1); 102 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 && 103 isLineComment(*Previous) && 104 FormatTok.OriginalColumn >= MinContinueColumn; 105 } 106 107 class ScopedMacroState : public FormatTokenSource { 108 public: 109 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, 110 FormatToken *&ResetToken) 111 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), 112 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), 113 Token(nullptr), PreviousToken(nullptr) { 114 FakeEOF.Tok.startToken(); 115 FakeEOF.Tok.setKind(tok::eof); 116 TokenSource = this; 117 Line.Level = 0; 118 Line.InPPDirective = true; 119 } 120 121 ~ScopedMacroState() override { 122 TokenSource = PreviousTokenSource; 123 ResetToken = Token; 124 Line.InPPDirective = false; 125 Line.Level = PreviousLineLevel; 126 } 127 128 FormatToken *getNextToken() override { 129 // The \c UnwrappedLineParser guards against this by never calling 130 // \c getNextToken() after it has encountered the first eof token. 131 assert(!eof()); 132 PreviousToken = Token; 133 Token = PreviousTokenSource->getNextToken(); 134 if (eof()) 135 return &FakeEOF; 136 return Token; 137 } 138 139 FormatToken *getPreviousToken() override { 140 return PreviousTokenSource->getPreviousToken(); 141 } 142 143 FormatToken *peekNextToken() override { 144 if (eof()) 145 return &FakeEOF; 146 return PreviousTokenSource->peekNextToken(); 147 } 148 149 FormatToken *peekNextToken(int N) override { 150 assert(N > 0); 151 if (eof()) 152 return &FakeEOF; 153 return PreviousTokenSource->peekNextToken(N); 154 } 155 156 bool isEOF() override { return PreviousTokenSource->isEOF(); } 157 158 unsigned getPosition() override { return PreviousTokenSource->getPosition(); } 159 160 FormatToken *setPosition(unsigned Position) override { 161 PreviousToken = nullptr; 162 Token = PreviousTokenSource->setPosition(Position); 163 return Token; 164 } 165 166 private: 167 bool eof() { 168 return Token && Token->HasUnescapedNewline && 169 !continuesLineComment(*Token, PreviousToken, 170 /*MinColumnToken=*/PreviousToken); 171 } 172 173 FormatToken FakeEOF; 174 UnwrappedLine &Line; 175 FormatTokenSource *&TokenSource; 176 FormatToken *&ResetToken; 177 unsigned PreviousLineLevel; 178 FormatTokenSource *PreviousTokenSource; 179 180 FormatToken *Token; 181 FormatToken *PreviousToken; 182 }; 183 184 } // end anonymous namespace 185 186 class ScopedLineState { 187 public: 188 ScopedLineState(UnwrappedLineParser &Parser, 189 bool SwitchToPreprocessorLines = false) 190 : Parser(Parser), OriginalLines(Parser.CurrentLines) { 191 if (SwitchToPreprocessorLines) 192 Parser.CurrentLines = &Parser.PreprocessorDirectives; 193 else if (!Parser.Line->Tokens.empty()) 194 Parser.CurrentLines = &Parser.Line->Tokens.back().Children; 195 PreBlockLine = std::move(Parser.Line); 196 Parser.Line = std::make_unique<UnwrappedLine>(); 197 Parser.Line->Level = PreBlockLine->Level; 198 Parser.Line->InPPDirective = PreBlockLine->InPPDirective; 199 } 200 201 ~ScopedLineState() { 202 if (!Parser.Line->Tokens.empty()) 203 Parser.addUnwrappedLine(); 204 assert(Parser.Line->Tokens.empty()); 205 Parser.Line = std::move(PreBlockLine); 206 if (Parser.CurrentLines == &Parser.PreprocessorDirectives) 207 Parser.MustBreakBeforeNextToken = true; 208 Parser.CurrentLines = OriginalLines; 209 } 210 211 private: 212 UnwrappedLineParser &Parser; 213 214 std::unique_ptr<UnwrappedLine> PreBlockLine; 215 SmallVectorImpl<UnwrappedLine> *OriginalLines; 216 }; 217 218 class CompoundStatementIndenter { 219 public: 220 CompoundStatementIndenter(UnwrappedLineParser *Parser, 221 const FormatStyle &Style, unsigned &LineLevel) 222 : CompoundStatementIndenter(Parser, LineLevel, 223 Style.BraceWrapping.AfterControlStatement, 224 Style.BraceWrapping.IndentBraces) {} 225 CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel, 226 bool WrapBrace, bool IndentBrace) 227 : LineLevel(LineLevel), OldLineLevel(LineLevel) { 228 if (WrapBrace) 229 Parser->addUnwrappedLine(); 230 if (IndentBrace) 231 ++LineLevel; 232 } 233 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } 234 235 private: 236 unsigned &LineLevel; 237 unsigned OldLineLevel; 238 }; 239 240 namespace { 241 242 class IndexedTokenSource : public FormatTokenSource { 243 public: 244 IndexedTokenSource(ArrayRef<FormatToken *> Tokens) 245 : Tokens(Tokens), Position(-1) {} 246 247 FormatToken *getNextToken() override { 248 if (Position >= 0 && Tokens[Position]->is(tok::eof)) { 249 LLVM_DEBUG({ 250 llvm::dbgs() << "Next "; 251 dbgToken(Position); 252 }); 253 return Tokens[Position]; 254 } 255 ++Position; 256 LLVM_DEBUG({ 257 llvm::dbgs() << "Next "; 258 dbgToken(Position); 259 }); 260 return Tokens[Position]; 261 } 262 263 FormatToken *getPreviousToken() override { 264 return Position > 0 ? Tokens[Position - 1] : nullptr; 265 } 266 267 FormatToken *peekNextToken() override { 268 int Next = Position + 1; 269 LLVM_DEBUG({ 270 llvm::dbgs() << "Peeking "; 271 dbgToken(Next); 272 }); 273 return Tokens[Next]; 274 } 275 276 FormatToken *peekNextToken(int N) override { 277 assert(N > 0); 278 int Next = Position + N; 279 LLVM_DEBUG({ 280 llvm::dbgs() << "Peeking (+" << (N - 1) << ") "; 281 dbgToken(Next); 282 }); 283 return Tokens[Next]; 284 } 285 286 bool isEOF() override { return Tokens[Position]->is(tok::eof); } 287 288 unsigned getPosition() override { 289 LLVM_DEBUG(llvm::dbgs() << "Getting Position: " << Position << "\n"); 290 assert(Position >= 0); 291 return Position; 292 } 293 294 FormatToken *setPosition(unsigned P) override { 295 LLVM_DEBUG(llvm::dbgs() << "Setting Position: " << P << "\n"); 296 Position = P; 297 return Tokens[Position]; 298 } 299 300 void reset() { Position = -1; } 301 302 private: 303 void dbgToken(int Position, llvm::StringRef Indent = "") { 304 FormatToken *Tok = Tokens[Position]; 305 llvm::dbgs() << Indent << "[" << Position 306 << "] Token: " << Tok->Tok.getName() << " / " << Tok->TokenText 307 << ", Macro: " << !!Tok->MacroCtx << "\n"; 308 } 309 310 ArrayRef<FormatToken *> Tokens; 311 int Position; 312 }; 313 314 } // end anonymous namespace 315 316 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, 317 const AdditionalKeywords &Keywords, 318 unsigned FirstStartColumn, 319 ArrayRef<FormatToken *> Tokens, 320 UnwrappedLineConsumer &Callback) 321 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), 322 CurrentLines(&Lines), Style(Style), Keywords(Keywords), 323 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr), 324 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1), 325 IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None 326 ? IG_Rejected 327 : IG_Inited), 328 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {} 329 330 void UnwrappedLineParser::reset() { 331 PPBranchLevel = -1; 332 IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None 333 ? IG_Rejected 334 : IG_Inited; 335 IncludeGuardToken = nullptr; 336 Line.reset(new UnwrappedLine); 337 CommentsBeforeNextToken.clear(); 338 FormatTok = nullptr; 339 MustBreakBeforeNextToken = false; 340 PreprocessorDirectives.clear(); 341 CurrentLines = &Lines; 342 DeclarationScopeStack.clear(); 343 NestedTooDeep.clear(); 344 PPStack.clear(); 345 Line->FirstStartColumn = FirstStartColumn; 346 } 347 348 void UnwrappedLineParser::parse() { 349 IndexedTokenSource TokenSource(AllTokens); 350 Line->FirstStartColumn = FirstStartColumn; 351 do { 352 LLVM_DEBUG(llvm::dbgs() << "----\n"); 353 reset(); 354 Tokens = &TokenSource; 355 TokenSource.reset(); 356 357 readToken(); 358 parseFile(); 359 360 // If we found an include guard then all preprocessor directives (other than 361 // the guard) are over-indented by one. 362 if (IncludeGuard == IG_Found) { 363 for (auto &Line : Lines) 364 if (Line.InPPDirective && Line.Level > 0) 365 --Line.Level; 366 } 367 368 // Create line with eof token. 369 pushToken(FormatTok); 370 addUnwrappedLine(); 371 372 for (const UnwrappedLine &Line : Lines) 373 Callback.consumeUnwrappedLine(Line); 374 375 Callback.finishRun(); 376 Lines.clear(); 377 while (!PPLevelBranchIndex.empty() && 378 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) { 379 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); 380 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); 381 } 382 if (!PPLevelBranchIndex.empty()) { 383 ++PPLevelBranchIndex.back(); 384 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); 385 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); 386 } 387 } while (!PPLevelBranchIndex.empty()); 388 } 389 390 void UnwrappedLineParser::parseFile() { 391 // The top-level context in a file always has declarations, except for pre- 392 // processor directives and JavaScript files. 393 bool MustBeDeclaration = !Line->InPPDirective && !Style.isJavaScript(); 394 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 395 MustBeDeclaration); 396 if (Style.Language == FormatStyle::LK_TextProto) 397 parseBracedList(); 398 else 399 parseLevel(); 400 // Make sure to format the remaining tokens. 401 // 402 // LK_TextProto is special since its top-level is parsed as the body of a 403 // braced list, which does not necessarily have natural line separators such 404 // as a semicolon. Comments after the last entry that have been determined to 405 // not belong to that line, as in: 406 // key: value 407 // // endfile comment 408 // do not have a chance to be put on a line of their own until this point. 409 // Here we add this newline before end-of-file comments. 410 if (Style.Language == FormatStyle::LK_TextProto && 411 !CommentsBeforeNextToken.empty()) { 412 addUnwrappedLine(); 413 } 414 flushComments(true); 415 addUnwrappedLine(); 416 } 417 418 void UnwrappedLineParser::parseCSharpGenericTypeConstraint() { 419 do { 420 switch (FormatTok->Tok.getKind()) { 421 case tok::l_brace: 422 return; 423 default: 424 if (FormatTok->is(Keywords.kw_where)) { 425 addUnwrappedLine(); 426 nextToken(); 427 parseCSharpGenericTypeConstraint(); 428 break; 429 } 430 nextToken(); 431 break; 432 } 433 } while (!eof()); 434 } 435 436 void UnwrappedLineParser::parseCSharpAttribute() { 437 int UnpairedSquareBrackets = 1; 438 do { 439 switch (FormatTok->Tok.getKind()) { 440 case tok::r_square: 441 nextToken(); 442 --UnpairedSquareBrackets; 443 if (UnpairedSquareBrackets == 0) { 444 addUnwrappedLine(); 445 return; 446 } 447 break; 448 case tok::l_square: 449 ++UnpairedSquareBrackets; 450 nextToken(); 451 break; 452 default: 453 nextToken(); 454 break; 455 } 456 } while (!eof()); 457 } 458 459 bool UnwrappedLineParser::precededByCommentOrPPDirective() const { 460 if (!Lines.empty() && Lines.back().InPPDirective) 461 return true; 462 463 const FormatToken *Previous = Tokens->getPreviousToken(); 464 return Previous && Previous->is(tok::comment) && 465 (Previous->IsMultiline || Previous->NewlinesBefore > 0); 466 } 467 468 /// \brief Parses a level, that is ???. 469 /// \param OpeningBrace Opening brace (\p nullptr if absent) of that level 470 /// \param CanContainBracedList If the content can contain (at any level) a 471 /// braced list. 472 /// \param NextLBracesType The type for left brace found in this level. 473 /// \param IfKind The \p if statement kind in the level. 474 /// \param IfLeftBrace The left brace of the \p if block in the level. 475 /// \returns true if a simple block of if/else/for/while, or false otherwise. 476 /// (A simple block has a single statement.) 477 bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace, 478 bool CanContainBracedList, 479 TokenType NextLBracesType, 480 IfStmtKind *IfKind, 481 FormatToken **IfLeftBrace) { 482 auto NextLevelLBracesType = NextLBracesType == TT_CompoundRequirementLBrace 483 ? TT_BracedListLBrace 484 : TT_Unknown; 485 const bool IsPrecededByCommentOrPPDirective = 486 !Style.RemoveBracesLLVM || precededByCommentOrPPDirective(); 487 FormatToken *IfLBrace = nullptr; 488 bool HasDoWhile = false; 489 bool HasLabel = false; 490 unsigned StatementCount = 0; 491 bool SwitchLabelEncountered = false; 492 493 do { 494 if (FormatTok->getType() == TT_AttributeMacro) { 495 nextToken(); 496 continue; 497 } 498 tok::TokenKind kind = FormatTok->Tok.getKind(); 499 if (FormatTok->getType() == TT_MacroBlockBegin) 500 kind = tok::l_brace; 501 else if (FormatTok->getType() == TT_MacroBlockEnd) 502 kind = tok::r_brace; 503 504 auto ParseDefault = [this, OpeningBrace, NextLevelLBracesType, IfKind, 505 &IfLBrace, &HasDoWhile, &HasLabel, &StatementCount] { 506 parseStructuralElement(!OpeningBrace, NextLevelLBracesType, IfKind, 507 &IfLBrace, HasDoWhile ? nullptr : &HasDoWhile, 508 HasLabel ? nullptr : &HasLabel); 509 ++StatementCount; 510 assert(StatementCount > 0 && "StatementCount overflow!"); 511 }; 512 513 switch (kind) { 514 case tok::comment: 515 nextToken(); 516 addUnwrappedLine(); 517 break; 518 case tok::l_brace: 519 if (NextLBracesType != TT_Unknown) { 520 FormatTok->setFinalizedType(NextLBracesType); 521 } else if (FormatTok->Previous && 522 FormatTok->Previous->ClosesRequiresClause) { 523 // We need the 'default' case here to correctly parse a function 524 // l_brace. 525 ParseDefault(); 526 continue; 527 } 528 if (CanContainBracedList && !FormatTok->is(TT_MacroBlockBegin) && 529 tryToParseBracedList()) { 530 continue; 531 } 532 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, 533 /*MunchSemi=*/true, /*KeepBraces=*/true, /*IfKind=*/nullptr, 534 /*UnindentWhitesmithsBraces=*/false, CanContainBracedList, 535 NextLBracesType); 536 ++StatementCount; 537 assert(StatementCount > 0 && "StatementCount overflow!"); 538 addUnwrappedLine(); 539 break; 540 case tok::r_brace: 541 if (OpeningBrace) { 542 if (!Style.RemoveBracesLLVM || Line->InPPDirective || 543 !OpeningBrace->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)) { 544 return false; 545 } 546 if (FormatTok->isNot(tok::r_brace) || StatementCount != 1 || HasLabel || 547 HasDoWhile || IsPrecededByCommentOrPPDirective || 548 precededByCommentOrPPDirective()) { 549 return false; 550 } 551 const FormatToken *Next = Tokens->peekNextToken(); 552 if (Next->is(tok::comment) && Next->NewlinesBefore == 0) 553 return false; 554 if (IfLeftBrace) 555 *IfLeftBrace = IfLBrace; 556 return true; 557 } 558 nextToken(); 559 addUnwrappedLine(); 560 break; 561 case tok::kw_default: { 562 unsigned StoredPosition = Tokens->getPosition(); 563 FormatToken *Next; 564 do { 565 Next = Tokens->getNextToken(); 566 assert(Next); 567 } while (Next->is(tok::comment)); 568 FormatTok = Tokens->setPosition(StoredPosition); 569 if (Next->isNot(tok::colon)) { 570 // default not followed by ':' is not a case label; treat it like 571 // an identifier. 572 parseStructuralElement(); 573 break; 574 } 575 // Else, if it is 'default:', fall through to the case handling. 576 LLVM_FALLTHROUGH; 577 } 578 case tok::kw_case: 579 if (Style.isJavaScript() && Line->MustBeDeclaration) { 580 // A 'case: string' style field declaration. 581 parseStructuralElement(); 582 break; 583 } 584 if (!SwitchLabelEncountered && 585 (Style.IndentCaseLabels || 586 (Line->InPPDirective && Line->Level == 1))) { 587 ++Line->Level; 588 } 589 SwitchLabelEncountered = true; 590 parseStructuralElement(); 591 break; 592 case tok::l_square: 593 if (Style.isCSharp()) { 594 nextToken(); 595 parseCSharpAttribute(); 596 break; 597 } 598 if (handleCppAttributes()) 599 break; 600 LLVM_FALLTHROUGH; 601 default: 602 ParseDefault(); 603 break; 604 } 605 } while (!eof()); 606 607 return false; 608 } 609 610 void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { 611 // We'll parse forward through the tokens until we hit 612 // a closing brace or eof - note that getNextToken() will 613 // parse macros, so this will magically work inside macro 614 // definitions, too. 615 unsigned StoredPosition = Tokens->getPosition(); 616 FormatToken *Tok = FormatTok; 617 const FormatToken *PrevTok = Tok->Previous; 618 // Keep a stack of positions of lbrace tokens. We will 619 // update information about whether an lbrace starts a 620 // braced init list or a different block during the loop. 621 SmallVector<FormatToken *, 8> LBraceStack; 622 assert(Tok->is(tok::l_brace)); 623 do { 624 // Get next non-comment token. 625 FormatToken *NextTok; 626 do { 627 NextTok = Tokens->getNextToken(); 628 } while (NextTok->is(tok::comment)); 629 630 switch (Tok->Tok.getKind()) { 631 case tok::l_brace: 632 if (Style.isJavaScript() && PrevTok) { 633 if (PrevTok->isOneOf(tok::colon, tok::less)) { 634 // A ':' indicates this code is in a type, or a braced list 635 // following a label in an object literal ({a: {b: 1}}). 636 // A '<' could be an object used in a comparison, but that is nonsense 637 // code (can never return true), so more likely it is a generic type 638 // argument (`X<{a: string; b: number}>`). 639 // The code below could be confused by semicolons between the 640 // individual members in a type member list, which would normally 641 // trigger BK_Block. In both cases, this must be parsed as an inline 642 // braced init. 643 Tok->setBlockKind(BK_BracedInit); 644 } else if (PrevTok->is(tok::r_paren)) { 645 // `) { }` can only occur in function or method declarations in JS. 646 Tok->setBlockKind(BK_Block); 647 } 648 } else { 649 Tok->setBlockKind(BK_Unknown); 650 } 651 LBraceStack.push_back(Tok); 652 break; 653 case tok::r_brace: 654 if (LBraceStack.empty()) 655 break; 656 if (LBraceStack.back()->is(BK_Unknown)) { 657 bool ProbablyBracedList = false; 658 if (Style.Language == FormatStyle::LK_Proto) { 659 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square); 660 } else { 661 // Skip NextTok over preprocessor lines, otherwise we may not 662 // properly diagnose the block as a braced intializer 663 // if the comma separator appears after the pp directive. 664 while (NextTok->is(tok::hash)) { 665 ScopedMacroState MacroState(*Line, Tokens, NextTok); 666 do { 667 NextTok = Tokens->getNextToken(); 668 } while (NextTok->isNot(tok::eof)); 669 } 670 671 // Using OriginalColumn to distinguish between ObjC methods and 672 // binary operators is a bit hacky. 673 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) && 674 NextTok->OriginalColumn == 0; 675 676 // Try to detect a braced list. Note that regardless how we mark inner 677 // braces here, we will overwrite the BlockKind later if we parse a 678 // braced list (where all blocks inside are by default braced lists), 679 // or when we explicitly detect blocks (for example while parsing 680 // lambdas). 681 682 // If we already marked the opening brace as braced list, the closing 683 // must also be part of it. 684 ProbablyBracedList = LBraceStack.back()->is(TT_BracedListLBrace); 685 686 ProbablyBracedList = ProbablyBracedList || 687 (Style.isJavaScript() && 688 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in, 689 Keywords.kw_as)); 690 ProbablyBracedList = ProbablyBracedList || 691 (Style.isCpp() && NextTok->is(tok::l_paren)); 692 693 // If there is a comma, semicolon or right paren after the closing 694 // brace, we assume this is a braced initializer list. 695 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a 696 // braced list in JS. 697 ProbablyBracedList = 698 ProbablyBracedList || 699 NextTok->isOneOf(tok::comma, tok::period, tok::colon, 700 tok::r_paren, tok::r_square, tok::l_brace, 701 tok::ellipsis); 702 703 ProbablyBracedList = 704 ProbablyBracedList || 705 (NextTok->is(tok::identifier) && 706 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)); 707 708 ProbablyBracedList = ProbablyBracedList || 709 (NextTok->is(tok::semi) && 710 (!ExpectClassBody || LBraceStack.size() != 1)); 711 712 ProbablyBracedList = 713 ProbablyBracedList || 714 (NextTok->isBinaryOperator() && !NextIsObjCMethod); 715 716 if (!Style.isCSharp() && NextTok->is(tok::l_square)) { 717 // We can have an array subscript after a braced init 718 // list, but C++11 attributes are expected after blocks. 719 NextTok = Tokens->getNextToken(); 720 ProbablyBracedList = NextTok->isNot(tok::l_square); 721 } 722 } 723 if (ProbablyBracedList) { 724 Tok->setBlockKind(BK_BracedInit); 725 LBraceStack.back()->setBlockKind(BK_BracedInit); 726 } else { 727 Tok->setBlockKind(BK_Block); 728 LBraceStack.back()->setBlockKind(BK_Block); 729 } 730 } 731 LBraceStack.pop_back(); 732 break; 733 case tok::identifier: 734 if (!Tok->is(TT_StatementMacro)) 735 break; 736 LLVM_FALLTHROUGH; 737 case tok::at: 738 case tok::semi: 739 case tok::kw_if: 740 case tok::kw_while: 741 case tok::kw_for: 742 case tok::kw_switch: 743 case tok::kw_try: 744 case tok::kw___try: 745 if (!LBraceStack.empty() && LBraceStack.back()->is(BK_Unknown)) 746 LBraceStack.back()->setBlockKind(BK_Block); 747 break; 748 default: 749 break; 750 } 751 PrevTok = Tok; 752 Tok = NextTok; 753 } while (Tok->isNot(tok::eof) && !LBraceStack.empty()); 754 755 // Assume other blocks for all unclosed opening braces. 756 for (FormatToken *LBrace : LBraceStack) 757 if (LBrace->is(BK_Unknown)) 758 LBrace->setBlockKind(BK_Block); 759 760 FormatTok = Tokens->setPosition(StoredPosition); 761 } 762 763 template <class T> 764 static inline void hash_combine(std::size_t &seed, const T &v) { 765 std::hash<T> hasher; 766 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); 767 } 768 769 size_t UnwrappedLineParser::computePPHash() const { 770 size_t h = 0; 771 for (const auto &i : PPStack) { 772 hash_combine(h, size_t(i.Kind)); 773 hash_combine(h, i.Line); 774 } 775 return h; 776 } 777 778 // Checks whether \p ParsedLine might fit on a single line. If \p OpeningBrace 779 // is not null, subtracts its length (plus the preceding space) when computing 780 // the length of \p ParsedLine. We must clone the tokens of \p ParsedLine before 781 // running the token annotator on it so that we can restore them afterward. 782 bool UnwrappedLineParser::mightFitOnOneLine( 783 UnwrappedLine &ParsedLine, const FormatToken *OpeningBrace) const { 784 const auto ColumnLimit = Style.ColumnLimit; 785 if (ColumnLimit == 0) 786 return true; 787 788 auto &Tokens = ParsedLine.Tokens; 789 assert(!Tokens.empty()); 790 791 const auto *LastToken = Tokens.back().Tok; 792 assert(LastToken); 793 794 SmallVector<UnwrappedLineNode> SavedTokens(Tokens.size()); 795 796 int Index = 0; 797 for (const auto &Token : Tokens) { 798 assert(Token.Tok); 799 auto &SavedToken = SavedTokens[Index++]; 800 SavedToken.Tok = new FormatToken; 801 SavedToken.Tok->copyFrom(*Token.Tok); 802 SavedToken.Children = std::move(Token.Children); 803 } 804 805 AnnotatedLine Line(ParsedLine); 806 assert(Line.Last == LastToken); 807 808 TokenAnnotator Annotator(Style, Keywords); 809 Annotator.annotate(Line); 810 Annotator.calculateFormattingInformation(Line); 811 812 auto Length = LastToken->TotalLength; 813 if (OpeningBrace) { 814 assert(OpeningBrace != Tokens.front().Tok); 815 Length -= OpeningBrace->TokenText.size() + 1; 816 } 817 818 Index = 0; 819 for (auto &Token : Tokens) { 820 const auto &SavedToken = SavedTokens[Index++]; 821 Token.Tok->copyFrom(*SavedToken.Tok); 822 Token.Children = std::move(SavedToken.Children); 823 delete SavedToken.Tok; 824 } 825 826 return Line.Level * Style.IndentWidth + Length <= ColumnLimit; 827 } 828 829 FormatToken *UnwrappedLineParser::parseBlock( 830 bool MustBeDeclaration, unsigned AddLevels, bool MunchSemi, bool KeepBraces, 831 IfStmtKind *IfKind, bool UnindentWhitesmithsBraces, 832 bool CanContainBracedList, TokenType NextLBracesType) { 833 auto HandleVerilogBlockLabel = [this]() { 834 // ":" name 835 if (Style.isVerilog() && FormatTok->is(tok::colon)) { 836 nextToken(); 837 if (Keywords.isVerilogIdentifier(*FormatTok)) 838 nextToken(); 839 } 840 }; 841 842 assert((FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) || 843 (Style.isVerilog() && Keywords.isVerilogBegin(*FormatTok))) && 844 "'{' or macro block token expected"); 845 FormatToken *Tok = FormatTok; 846 const bool FollowedByComment = Tokens->peekNextToken()->is(tok::comment); 847 auto Index = CurrentLines->size(); 848 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin); 849 FormatTok->setBlockKind(BK_Block); 850 851 // For Whitesmiths mode, jump to the next level prior to skipping over the 852 // braces. 853 if (AddLevels > 0 && Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) 854 ++Line->Level; 855 856 size_t PPStartHash = computePPHash(); 857 858 const unsigned InitialLevel = Line->Level; 859 nextToken(/*LevelDifference=*/AddLevels); 860 HandleVerilogBlockLabel(); 861 862 // Bail out if there are too many levels. Otherwise, the stack might overflow. 863 if (Line->Level > 300) 864 return nullptr; 865 866 if (MacroBlock && FormatTok->is(tok::l_paren)) 867 parseParens(); 868 869 size_t NbPreprocessorDirectives = 870 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0; 871 addUnwrappedLine(); 872 size_t OpeningLineIndex = 873 CurrentLines->empty() 874 ? (UnwrappedLine::kInvalidIndex) 875 : (CurrentLines->size() - 1 - NbPreprocessorDirectives); 876 877 // Whitesmiths is weird here. The brace needs to be indented for the namespace 878 // block, but the block itself may not be indented depending on the style 879 // settings. This allows the format to back up one level in those cases. 880 if (UnindentWhitesmithsBraces) 881 --Line->Level; 882 883 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 884 MustBeDeclaration); 885 if (AddLevels > 0u && Style.BreakBeforeBraces != FormatStyle::BS_Whitesmiths) 886 Line->Level += AddLevels; 887 888 FormatToken *IfLBrace = nullptr; 889 const bool SimpleBlock = 890 parseLevel(Tok, CanContainBracedList, NextLBracesType, IfKind, &IfLBrace); 891 892 if (eof()) 893 return IfLBrace; 894 895 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd) 896 : !FormatTok->is(tok::r_brace)) { 897 Line->Level = InitialLevel; 898 FormatTok->setBlockKind(BK_Block); 899 return IfLBrace; 900 } 901 902 auto RemoveBraces = [=]() mutable { 903 if (!SimpleBlock) 904 return false; 905 assert(Tok->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)); 906 assert(FormatTok->is(tok::r_brace)); 907 const bool WrappedOpeningBrace = !Tok->Previous; 908 if (WrappedOpeningBrace && FollowedByComment) 909 return false; 910 const bool HasRequiredIfBraces = IfLBrace && !IfLBrace->Optional; 911 if (KeepBraces && !HasRequiredIfBraces) 912 return false; 913 if (Tok->isNot(TT_ElseLBrace) || !HasRequiredIfBraces) { 914 const FormatToken *Previous = Tokens->getPreviousToken(); 915 assert(Previous); 916 if (Previous->is(tok::r_brace) && !Previous->Optional) 917 return false; 918 } 919 assert(!CurrentLines->empty()); 920 auto &LastLine = CurrentLines->back(); 921 if (LastLine.Level == InitialLevel + 1 && !mightFitOnOneLine(LastLine)) 922 return false; 923 if (Tok->is(TT_ElseLBrace)) 924 return true; 925 if (WrappedOpeningBrace) { 926 assert(Index > 0); 927 --Index; // The line above the wrapped l_brace. 928 Tok = nullptr; 929 } 930 return mightFitOnOneLine((*CurrentLines)[Index], Tok); 931 }; 932 if (RemoveBraces()) { 933 Tok->MatchingParen = FormatTok; 934 FormatTok->MatchingParen = Tok; 935 } 936 937 size_t PPEndHash = computePPHash(); 938 939 // Munch the closing brace. 940 nextToken(/*LevelDifference=*/-AddLevels); 941 HandleVerilogBlockLabel(); 942 943 if (MacroBlock && FormatTok->is(tok::l_paren)) 944 parseParens(); 945 946 if (FormatTok->is(tok::kw_noexcept)) { 947 // A noexcept in a requires expression. 948 nextToken(); 949 } 950 951 if (FormatTok->is(tok::arrow)) { 952 // Following the } or noexcept we can find a trailing return type arrow 953 // as part of an implicit conversion constraint. 954 nextToken(); 955 parseStructuralElement(); 956 } 957 958 if (MunchSemi && FormatTok->is(tok::semi)) 959 nextToken(); 960 961 Line->Level = InitialLevel; 962 963 if (PPStartHash == PPEndHash) { 964 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex; 965 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) { 966 // Update the opening line to add the forward reference as well 967 (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex = 968 CurrentLines->size() - 1; 969 } 970 } 971 972 return IfLBrace; 973 } 974 975 static bool isGoogScope(const UnwrappedLine &Line) { 976 // FIXME: Closure-library specific stuff should not be hard-coded but be 977 // configurable. 978 if (Line.Tokens.size() < 4) 979 return false; 980 auto I = Line.Tokens.begin(); 981 if (I->Tok->TokenText != "goog") 982 return false; 983 ++I; 984 if (I->Tok->isNot(tok::period)) 985 return false; 986 ++I; 987 if (I->Tok->TokenText != "scope") 988 return false; 989 ++I; 990 return I->Tok->is(tok::l_paren); 991 } 992 993 static bool isIIFE(const UnwrappedLine &Line, 994 const AdditionalKeywords &Keywords) { 995 // Look for the start of an immediately invoked anonymous function. 996 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression 997 // This is commonly done in JavaScript to create a new, anonymous scope. 998 // Example: (function() { ... })() 999 if (Line.Tokens.size() < 3) 1000 return false; 1001 auto I = Line.Tokens.begin(); 1002 if (I->Tok->isNot(tok::l_paren)) 1003 return false; 1004 ++I; 1005 if (I->Tok->isNot(Keywords.kw_function)) 1006 return false; 1007 ++I; 1008 return I->Tok->is(tok::l_paren); 1009 } 1010 1011 static bool ShouldBreakBeforeBrace(const FormatStyle &Style, 1012 const FormatToken &InitialToken) { 1013 tok::TokenKind Kind = InitialToken.Tok.getKind(); 1014 if (InitialToken.is(TT_NamespaceMacro)) 1015 Kind = tok::kw_namespace; 1016 1017 switch (Kind) { 1018 case tok::kw_namespace: 1019 return Style.BraceWrapping.AfterNamespace; 1020 case tok::kw_class: 1021 return Style.BraceWrapping.AfterClass; 1022 case tok::kw_union: 1023 return Style.BraceWrapping.AfterUnion; 1024 case tok::kw_struct: 1025 return Style.BraceWrapping.AfterStruct; 1026 case tok::kw_enum: 1027 return Style.BraceWrapping.AfterEnum; 1028 default: 1029 return false; 1030 } 1031 } 1032 1033 void UnwrappedLineParser::parseChildBlock( 1034 bool CanContainBracedList, clang::format::TokenType NextLBracesType) { 1035 assert(FormatTok->is(tok::l_brace)); 1036 FormatTok->setBlockKind(BK_Block); 1037 const FormatToken *OpeningBrace = FormatTok; 1038 nextToken(); 1039 { 1040 bool SkipIndent = (Style.isJavaScript() && 1041 (isGoogScope(*Line) || isIIFE(*Line, Keywords))); 1042 ScopedLineState LineState(*this); 1043 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 1044 /*MustBeDeclaration=*/false); 1045 Line->Level += SkipIndent ? 0 : 1; 1046 parseLevel(OpeningBrace, CanContainBracedList, NextLBracesType); 1047 flushComments(isOnNewLine(*FormatTok)); 1048 Line->Level -= SkipIndent ? 0 : 1; 1049 } 1050 nextToken(); 1051 } 1052 1053 void UnwrappedLineParser::parsePPDirective() { 1054 assert(FormatTok->is(tok::hash) && "'#' expected"); 1055 ScopedMacroState MacroState(*Line, Tokens, FormatTok); 1056 1057 nextToken(); 1058 1059 if (!FormatTok->Tok.getIdentifierInfo()) { 1060 parsePPUnknown(); 1061 return; 1062 } 1063 1064 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { 1065 case tok::pp_define: 1066 parsePPDefine(); 1067 return; 1068 case tok::pp_if: 1069 parsePPIf(/*IfDef=*/false); 1070 break; 1071 case tok::pp_ifdef: 1072 case tok::pp_ifndef: 1073 parsePPIf(/*IfDef=*/true); 1074 break; 1075 case tok::pp_else: 1076 parsePPElse(); 1077 break; 1078 case tok::pp_elifdef: 1079 case tok::pp_elifndef: 1080 case tok::pp_elif: 1081 parsePPElIf(); 1082 break; 1083 case tok::pp_endif: 1084 parsePPEndIf(); 1085 break; 1086 default: 1087 parsePPUnknown(); 1088 break; 1089 } 1090 } 1091 1092 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { 1093 size_t Line = CurrentLines->size(); 1094 if (CurrentLines == &PreprocessorDirectives) 1095 Line += Lines.size(); 1096 1097 if (Unreachable || 1098 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable)) { 1099 PPStack.push_back({PP_Unreachable, Line}); 1100 } else { 1101 PPStack.push_back({PP_Conditional, Line}); 1102 } 1103 } 1104 1105 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { 1106 ++PPBranchLevel; 1107 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); 1108 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { 1109 PPLevelBranchIndex.push_back(0); 1110 PPLevelBranchCount.push_back(0); 1111 } 1112 PPChainBranchIndex.push(0); 1113 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; 1114 conditionalCompilationCondition(Unreachable || Skip); 1115 } 1116 1117 void UnwrappedLineParser::conditionalCompilationAlternative() { 1118 if (!PPStack.empty()) 1119 PPStack.pop_back(); 1120 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 1121 if (!PPChainBranchIndex.empty()) 1122 ++PPChainBranchIndex.top(); 1123 conditionalCompilationCondition( 1124 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && 1125 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()); 1126 } 1127 1128 void UnwrappedLineParser::conditionalCompilationEnd() { 1129 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 1130 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { 1131 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) 1132 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; 1133 } 1134 // Guard against #endif's without #if. 1135 if (PPBranchLevel > -1) 1136 --PPBranchLevel; 1137 if (!PPChainBranchIndex.empty()) 1138 PPChainBranchIndex.pop(); 1139 if (!PPStack.empty()) 1140 PPStack.pop_back(); 1141 } 1142 1143 void UnwrappedLineParser::parsePPIf(bool IfDef) { 1144 bool IfNDef = FormatTok->is(tok::pp_ifndef); 1145 nextToken(); 1146 bool Unreachable = false; 1147 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0")) 1148 Unreachable = true; 1149 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG") 1150 Unreachable = true; 1151 conditionalCompilationStart(Unreachable); 1152 FormatToken *IfCondition = FormatTok; 1153 // If there's a #ifndef on the first line, and the only lines before it are 1154 // comments, it could be an include guard. 1155 bool MaybeIncludeGuard = IfNDef; 1156 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) { 1157 for (auto &Line : Lines) { 1158 if (!Line.Tokens.front().Tok->is(tok::comment)) { 1159 MaybeIncludeGuard = false; 1160 IncludeGuard = IG_Rejected; 1161 break; 1162 } 1163 } 1164 } 1165 --PPBranchLevel; 1166 parsePPUnknown(); 1167 ++PPBranchLevel; 1168 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) { 1169 IncludeGuard = IG_IfNdefed; 1170 IncludeGuardToken = IfCondition; 1171 } 1172 } 1173 1174 void UnwrappedLineParser::parsePPElse() { 1175 // If a potential include guard has an #else, it's not an include guard. 1176 if (IncludeGuard == IG_Defined && PPBranchLevel == 0) 1177 IncludeGuard = IG_Rejected; 1178 conditionalCompilationAlternative(); 1179 if (PPBranchLevel > -1) 1180 --PPBranchLevel; 1181 parsePPUnknown(); 1182 ++PPBranchLevel; 1183 } 1184 1185 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } 1186 1187 void UnwrappedLineParser::parsePPEndIf() { 1188 conditionalCompilationEnd(); 1189 parsePPUnknown(); 1190 // If the #endif of a potential include guard is the last thing in the file, 1191 // then we found an include guard. 1192 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 && Tokens->isEOF() && 1193 Style.IndentPPDirectives != FormatStyle::PPDIS_None) { 1194 IncludeGuard = IG_Found; 1195 } 1196 } 1197 1198 void UnwrappedLineParser::parsePPDefine() { 1199 nextToken(); 1200 1201 if (!FormatTok->Tok.getIdentifierInfo()) { 1202 IncludeGuard = IG_Rejected; 1203 IncludeGuardToken = nullptr; 1204 parsePPUnknown(); 1205 return; 1206 } 1207 1208 if (IncludeGuard == IG_IfNdefed && 1209 IncludeGuardToken->TokenText == FormatTok->TokenText) { 1210 IncludeGuard = IG_Defined; 1211 IncludeGuardToken = nullptr; 1212 for (auto &Line : Lines) { 1213 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) { 1214 IncludeGuard = IG_Rejected; 1215 break; 1216 } 1217 } 1218 } 1219 1220 // In the context of a define, even keywords should be treated as normal 1221 // identifiers. Setting the kind to identifier is not enough, because we need 1222 // to treat additional keywords like __except as well, which are already 1223 // identifiers. Setting the identifier info to null interferes with include 1224 // guard processing above, and changes preprocessing nesting. 1225 FormatTok->Tok.setKind(tok::identifier); 1226 FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define); 1227 nextToken(); 1228 if (FormatTok->Tok.getKind() == tok::l_paren && 1229 !FormatTok->hasWhitespaceBefore()) { 1230 parseParens(); 1231 } 1232 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None) 1233 Line->Level += PPBranchLevel + 1; 1234 addUnwrappedLine(); 1235 ++Line->Level; 1236 1237 // Errors during a preprocessor directive can only affect the layout of the 1238 // preprocessor directive, and thus we ignore them. An alternative approach 1239 // would be to use the same approach we use on the file level (no 1240 // re-indentation if there was a structural error) within the macro 1241 // definition. 1242 parseFile(); 1243 } 1244 1245 void UnwrappedLineParser::parsePPUnknown() { 1246 do { 1247 nextToken(); 1248 } while (!eof()); 1249 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None) 1250 Line->Level += PPBranchLevel + 1; 1251 addUnwrappedLine(); 1252 } 1253 1254 // Here we exclude certain tokens that are not usually the first token in an 1255 // unwrapped line. This is used in attempt to distinguish macro calls without 1256 // trailing semicolons from other constructs split to several lines. 1257 static bool tokenCanStartNewLine(const FormatToken &Tok) { 1258 // Semicolon can be a null-statement, l_square can be a start of a macro or 1259 // a C++11 attribute, but this doesn't seem to be common. 1260 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && 1261 Tok.isNot(TT_AttributeSquare) && 1262 // Tokens that can only be used as binary operators and a part of 1263 // overloaded operator names. 1264 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && 1265 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && 1266 Tok.isNot(tok::less) && Tok.isNot(tok::greater) && 1267 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && 1268 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && 1269 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && 1270 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && 1271 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && 1272 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && 1273 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && 1274 Tok.isNot(tok::lesslessequal) && 1275 // Colon is used in labels, base class lists, initializer lists, 1276 // range-based for loops, ternary operator, but should never be the 1277 // first token in an unwrapped line. 1278 Tok.isNot(tok::colon) && 1279 // 'noexcept' is a trailing annotation. 1280 Tok.isNot(tok::kw_noexcept); 1281 } 1282 1283 static bool mustBeJSIdent(const AdditionalKeywords &Keywords, 1284 const FormatToken *FormatTok) { 1285 // FIXME: This returns true for C/C++ keywords like 'struct'. 1286 return FormatTok->is(tok::identifier) && 1287 (FormatTok->Tok.getIdentifierInfo() == nullptr || 1288 !FormatTok->isOneOf( 1289 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async, 1290 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally, 1291 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is, 1292 Keywords.kw_let, Keywords.kw_var, tok::kw_const, 1293 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements, 1294 Keywords.kw_instanceof, Keywords.kw_interface, 1295 Keywords.kw_override, Keywords.kw_throws, Keywords.kw_from)); 1296 } 1297 1298 static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords, 1299 const FormatToken *FormatTok) { 1300 return FormatTok->Tok.isLiteral() || 1301 FormatTok->isOneOf(tok::kw_true, tok::kw_false) || 1302 mustBeJSIdent(Keywords, FormatTok); 1303 } 1304 1305 // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement 1306 // when encountered after a value (see mustBeJSIdentOrValue). 1307 static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords, 1308 const FormatToken *FormatTok) { 1309 return FormatTok->isOneOf( 1310 tok::kw_return, Keywords.kw_yield, 1311 // conditionals 1312 tok::kw_if, tok::kw_else, 1313 // loops 1314 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break, 1315 // switch/case 1316 tok::kw_switch, tok::kw_case, 1317 // exceptions 1318 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally, 1319 // declaration 1320 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let, 1321 Keywords.kw_async, Keywords.kw_function, 1322 // import/export 1323 Keywords.kw_import, tok::kw_export); 1324 } 1325 1326 // Checks whether a token is a type in K&R C (aka C78). 1327 static bool isC78Type(const FormatToken &Tok) { 1328 return Tok.isOneOf(tok::kw_char, tok::kw_short, tok::kw_int, tok::kw_long, 1329 tok::kw_unsigned, tok::kw_float, tok::kw_double, 1330 tok::identifier); 1331 } 1332 1333 // This function checks whether a token starts the first parameter declaration 1334 // in a K&R C (aka C78) function definition, e.g.: 1335 // int f(a, b) 1336 // short a, b; 1337 // { 1338 // return a + b; 1339 // } 1340 static bool isC78ParameterDecl(const FormatToken *Tok, const FormatToken *Next, 1341 const FormatToken *FuncName) { 1342 assert(Tok); 1343 assert(Next); 1344 assert(FuncName); 1345 1346 if (FuncName->isNot(tok::identifier)) 1347 return false; 1348 1349 const FormatToken *Prev = FuncName->Previous; 1350 if (!Prev || (Prev->isNot(tok::star) && !isC78Type(*Prev))) 1351 return false; 1352 1353 if (!isC78Type(*Tok) && 1354 !Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union)) { 1355 return false; 1356 } 1357 1358 if (Next->isNot(tok::star) && !Next->Tok.getIdentifierInfo()) 1359 return false; 1360 1361 Tok = Tok->Previous; 1362 if (!Tok || Tok->isNot(tok::r_paren)) 1363 return false; 1364 1365 Tok = Tok->Previous; 1366 if (!Tok || Tok->isNot(tok::identifier)) 1367 return false; 1368 1369 return Tok->Previous && Tok->Previous->isOneOf(tok::l_paren, tok::comma); 1370 } 1371 1372 void UnwrappedLineParser::parseModuleImport() { 1373 nextToken(); 1374 while (!eof()) { 1375 if (FormatTok->is(tok::colon)) { 1376 FormatTok->setFinalizedType(TT_ModulePartitionColon); 1377 } 1378 // Handle import <foo/bar.h> as we would an include statement. 1379 else if (FormatTok->is(tok::less)) { 1380 nextToken(); 1381 while (!FormatTok->isOneOf(tok::semi, tok::greater, tok::eof)) { 1382 // Mark tokens up to the trailing line comments as implicit string 1383 // literals. 1384 if (FormatTok->isNot(tok::comment) && 1385 !FormatTok->TokenText.startswith("//")) { 1386 FormatTok->setFinalizedType(TT_ImplicitStringLiteral); 1387 } 1388 nextToken(); 1389 } 1390 } 1391 if (FormatTok->is(tok::semi)) { 1392 nextToken(); 1393 break; 1394 } 1395 nextToken(); 1396 } 1397 1398 addUnwrappedLine(); 1399 } 1400 1401 // readTokenWithJavaScriptASI reads the next token and terminates the current 1402 // line if JavaScript Automatic Semicolon Insertion must 1403 // happen between the current token and the next token. 1404 // 1405 // This method is conservative - it cannot cover all edge cases of JavaScript, 1406 // but only aims to correctly handle certain well known cases. It *must not* 1407 // return true in speculative cases. 1408 void UnwrappedLineParser::readTokenWithJavaScriptASI() { 1409 FormatToken *Previous = FormatTok; 1410 readToken(); 1411 FormatToken *Next = FormatTok; 1412 1413 bool IsOnSameLine = 1414 CommentsBeforeNextToken.empty() 1415 ? Next->NewlinesBefore == 0 1416 : CommentsBeforeNextToken.front()->NewlinesBefore == 0; 1417 if (IsOnSameLine) 1418 return; 1419 1420 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous); 1421 bool PreviousStartsTemplateExpr = 1422 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${"); 1423 if (PreviousMustBeValue || Previous->is(tok::r_paren)) { 1424 // If the line contains an '@' sign, the previous token might be an 1425 // annotation, which can precede another identifier/value. 1426 bool HasAt = llvm::any_of(Line->Tokens, [](UnwrappedLineNode &LineNode) { 1427 return LineNode.Tok->is(tok::at); 1428 }); 1429 if (HasAt) 1430 return; 1431 } 1432 if (Next->is(tok::exclaim) && PreviousMustBeValue) 1433 return addUnwrappedLine(); 1434 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next); 1435 bool NextEndsTemplateExpr = 1436 Next->is(TT_TemplateString) && Next->TokenText.startswith("}"); 1437 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr && 1438 (PreviousMustBeValue || 1439 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus, 1440 tok::minusminus))) { 1441 return addUnwrappedLine(); 1442 } 1443 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) && 1444 isJSDeclOrStmt(Keywords, Next)) { 1445 return addUnwrappedLine(); 1446 } 1447 } 1448 1449 void UnwrappedLineParser::parseStructuralElement( 1450 bool IsTopLevel, TokenType NextLBracesType, IfStmtKind *IfKind, 1451 FormatToken **IfLeftBrace, bool *HasDoWhile, bool *HasLabel) { 1452 if (Style.Language == FormatStyle::LK_TableGen && 1453 FormatTok->is(tok::pp_include)) { 1454 nextToken(); 1455 if (FormatTok->is(tok::string_literal)) 1456 nextToken(); 1457 addUnwrappedLine(); 1458 return; 1459 } 1460 switch (FormatTok->Tok.getKind()) { 1461 case tok::kw_asm: 1462 nextToken(); 1463 if (FormatTok->is(tok::l_brace)) { 1464 FormatTok->setFinalizedType(TT_InlineASMBrace); 1465 nextToken(); 1466 while (FormatTok && FormatTok->isNot(tok::eof)) { 1467 if (FormatTok->is(tok::r_brace)) { 1468 FormatTok->setFinalizedType(TT_InlineASMBrace); 1469 nextToken(); 1470 addUnwrappedLine(); 1471 break; 1472 } 1473 FormatTok->Finalized = true; 1474 nextToken(); 1475 } 1476 } 1477 break; 1478 case tok::kw_namespace: 1479 parseNamespace(); 1480 return; 1481 case tok::kw_public: 1482 case tok::kw_protected: 1483 case tok::kw_private: 1484 if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() || 1485 Style.isCSharp()) { 1486 nextToken(); 1487 } else { 1488 parseAccessSpecifier(); 1489 } 1490 return; 1491 case tok::kw_if: { 1492 if (Style.isJavaScript() && Line->MustBeDeclaration) { 1493 // field/method declaration. 1494 break; 1495 } 1496 FormatToken *Tok = parseIfThenElse(IfKind); 1497 if (IfLeftBrace) 1498 *IfLeftBrace = Tok; 1499 return; 1500 } 1501 case tok::kw_for: 1502 case tok::kw_while: 1503 if (Style.isJavaScript() && Line->MustBeDeclaration) { 1504 // field/method declaration. 1505 break; 1506 } 1507 parseForOrWhileLoop(); 1508 return; 1509 case tok::kw_do: 1510 if (Style.isJavaScript() && Line->MustBeDeclaration) { 1511 // field/method declaration. 1512 break; 1513 } 1514 parseDoWhile(); 1515 if (HasDoWhile) 1516 *HasDoWhile = true; 1517 return; 1518 case tok::kw_switch: 1519 if (Style.isJavaScript() && Line->MustBeDeclaration) { 1520 // 'switch: string' field declaration. 1521 break; 1522 } 1523 parseSwitch(); 1524 return; 1525 case tok::kw_default: 1526 if (Style.isJavaScript() && Line->MustBeDeclaration) { 1527 // 'default: string' field declaration. 1528 break; 1529 } 1530 nextToken(); 1531 if (FormatTok->is(tok::colon)) { 1532 parseLabel(); 1533 return; 1534 } 1535 // e.g. "default void f() {}" in a Java interface. 1536 break; 1537 case tok::kw_case: 1538 if (Style.isJavaScript() && Line->MustBeDeclaration) { 1539 // 'case: string' field declaration. 1540 nextToken(); 1541 break; 1542 } 1543 parseCaseLabel(); 1544 return; 1545 case tok::kw_try: 1546 case tok::kw___try: 1547 if (Style.isJavaScript() && Line->MustBeDeclaration) { 1548 // field/method declaration. 1549 break; 1550 } 1551 parseTryCatch(); 1552 return; 1553 case tok::kw_extern: 1554 nextToken(); 1555 if (FormatTok->is(tok::string_literal)) { 1556 nextToken(); 1557 if (FormatTok->is(tok::l_brace)) { 1558 if (Style.BraceWrapping.AfterExternBlock) 1559 addUnwrappedLine(); 1560 // Either we indent or for backwards compatibility we follow the 1561 // AfterExternBlock style. 1562 unsigned AddLevels = 1563 (Style.IndentExternBlock == FormatStyle::IEBS_Indent) || 1564 (Style.BraceWrapping.AfterExternBlock && 1565 Style.IndentExternBlock == 1566 FormatStyle::IEBS_AfterExternBlock) 1567 ? 1u 1568 : 0u; 1569 parseBlock(/*MustBeDeclaration=*/true, AddLevels); 1570 addUnwrappedLine(); 1571 return; 1572 } 1573 } 1574 break; 1575 case tok::kw_export: 1576 if (Style.isJavaScript()) { 1577 parseJavaScriptEs6ImportExport(); 1578 return; 1579 } 1580 if (!Style.isCpp()) 1581 break; 1582 // Handle C++ "(inline|export) namespace". 1583 LLVM_FALLTHROUGH; 1584 case tok::kw_inline: 1585 nextToken(); 1586 if (FormatTok->is(tok::kw_namespace)) { 1587 parseNamespace(); 1588 return; 1589 } 1590 break; 1591 case tok::identifier: 1592 if (FormatTok->is(TT_ForEachMacro)) { 1593 parseForOrWhileLoop(); 1594 return; 1595 } 1596 if (FormatTok->is(TT_MacroBlockBegin)) { 1597 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, 1598 /*MunchSemi=*/false); 1599 return; 1600 } 1601 if (FormatTok->is(Keywords.kw_import)) { 1602 if (Style.isJavaScript()) { 1603 parseJavaScriptEs6ImportExport(); 1604 return; 1605 } 1606 if (Style.Language == FormatStyle::LK_Proto) { 1607 nextToken(); 1608 if (FormatTok->is(tok::kw_public)) 1609 nextToken(); 1610 if (!FormatTok->is(tok::string_literal)) 1611 return; 1612 nextToken(); 1613 if (FormatTok->is(tok::semi)) 1614 nextToken(); 1615 addUnwrappedLine(); 1616 return; 1617 } 1618 if (Style.isCpp()) { 1619 parseModuleImport(); 1620 return; 1621 } 1622 } 1623 if (Style.isCpp() && 1624 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals, 1625 Keywords.kw_slots, Keywords.kw_qslots)) { 1626 nextToken(); 1627 if (FormatTok->is(tok::colon)) { 1628 nextToken(); 1629 addUnwrappedLine(); 1630 return; 1631 } 1632 } 1633 if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) { 1634 parseStatementMacro(); 1635 return; 1636 } 1637 if (Style.isCpp() && FormatTok->is(TT_NamespaceMacro)) { 1638 parseNamespace(); 1639 return; 1640 } 1641 // In all other cases, parse the declaration. 1642 break; 1643 default: 1644 break; 1645 } 1646 do { 1647 const FormatToken *Previous = FormatTok->Previous; 1648 switch (FormatTok->Tok.getKind()) { 1649 case tok::at: 1650 nextToken(); 1651 if (FormatTok->is(tok::l_brace)) { 1652 nextToken(); 1653 parseBracedList(); 1654 break; 1655 } else if (Style.Language == FormatStyle::LK_Java && 1656 FormatTok->is(Keywords.kw_interface)) { 1657 nextToken(); 1658 break; 1659 } 1660 switch (FormatTok->Tok.getObjCKeywordID()) { 1661 case tok::objc_public: 1662 case tok::objc_protected: 1663 case tok::objc_package: 1664 case tok::objc_private: 1665 return parseAccessSpecifier(); 1666 case tok::objc_interface: 1667 case tok::objc_implementation: 1668 return parseObjCInterfaceOrImplementation(); 1669 case tok::objc_protocol: 1670 if (parseObjCProtocol()) 1671 return; 1672 break; 1673 case tok::objc_end: 1674 return; // Handled by the caller. 1675 case tok::objc_optional: 1676 case tok::objc_required: 1677 nextToken(); 1678 addUnwrappedLine(); 1679 return; 1680 case tok::objc_autoreleasepool: 1681 nextToken(); 1682 if (FormatTok->is(tok::l_brace)) { 1683 if (Style.BraceWrapping.AfterControlStatement == 1684 FormatStyle::BWACS_Always) { 1685 addUnwrappedLine(); 1686 } 1687 parseBlock(); 1688 } 1689 addUnwrappedLine(); 1690 return; 1691 case tok::objc_synchronized: 1692 nextToken(); 1693 if (FormatTok->is(tok::l_paren)) { 1694 // Skip synchronization object 1695 parseParens(); 1696 } 1697 if (FormatTok->is(tok::l_brace)) { 1698 if (Style.BraceWrapping.AfterControlStatement == 1699 FormatStyle::BWACS_Always) { 1700 addUnwrappedLine(); 1701 } 1702 parseBlock(); 1703 } 1704 addUnwrappedLine(); 1705 return; 1706 case tok::objc_try: 1707 // This branch isn't strictly necessary (the kw_try case below would 1708 // do this too after the tok::at is parsed above). But be explicit. 1709 parseTryCatch(); 1710 return; 1711 default: 1712 break; 1713 } 1714 break; 1715 case tok::kw_concept: 1716 parseConcept(); 1717 return; 1718 case tok::kw_requires: { 1719 if (Style.isCpp()) { 1720 bool ParsedClause = parseRequires(); 1721 if (ParsedClause) 1722 return; 1723 } else { 1724 nextToken(); 1725 } 1726 break; 1727 } 1728 case tok::kw_enum: 1729 // Ignore if this is part of "template <enum ...". 1730 if (Previous && Previous->is(tok::less)) { 1731 nextToken(); 1732 break; 1733 } 1734 1735 // parseEnum falls through and does not yet add an unwrapped line as an 1736 // enum definition can start a structural element. 1737 if (!parseEnum()) 1738 break; 1739 // This only applies for C++. 1740 if (!Style.isCpp()) { 1741 addUnwrappedLine(); 1742 return; 1743 } 1744 break; 1745 case tok::kw_typedef: 1746 nextToken(); 1747 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS, 1748 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS, 1749 Keywords.kw_CF_CLOSED_ENUM, 1750 Keywords.kw_NS_CLOSED_ENUM)) { 1751 parseEnum(); 1752 } 1753 break; 1754 case tok::kw_struct: 1755 case tok::kw_union: 1756 case tok::kw_class: 1757 if (parseStructLike()) 1758 return; 1759 break; 1760 case tok::period: 1761 nextToken(); 1762 // In Java, classes have an implicit static member "class". 1763 if (Style.Language == FormatStyle::LK_Java && FormatTok && 1764 FormatTok->is(tok::kw_class)) { 1765 nextToken(); 1766 } 1767 if (Style.isJavaScript() && FormatTok && 1768 FormatTok->Tok.getIdentifierInfo()) { 1769 // JavaScript only has pseudo keywords, all keywords are allowed to 1770 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6 1771 nextToken(); 1772 } 1773 break; 1774 case tok::semi: 1775 nextToken(); 1776 addUnwrappedLine(); 1777 return; 1778 case tok::r_brace: 1779 addUnwrappedLine(); 1780 return; 1781 case tok::l_paren: { 1782 parseParens(); 1783 // Break the unwrapped line if a K&R C function definition has a parameter 1784 // declaration. 1785 if (!IsTopLevel || !Style.isCpp() || !Previous || FormatTok->is(tok::eof)) 1786 break; 1787 if (isC78ParameterDecl(FormatTok, Tokens->peekNextToken(), Previous)) { 1788 addUnwrappedLine(); 1789 return; 1790 } 1791 break; 1792 } 1793 case tok::kw_operator: 1794 nextToken(); 1795 if (FormatTok->isBinaryOperator()) 1796 nextToken(); 1797 break; 1798 case tok::caret: 1799 nextToken(); 1800 if (FormatTok->Tok.isAnyIdentifier() || 1801 FormatTok->isSimpleTypeSpecifier()) { 1802 nextToken(); 1803 } 1804 if (FormatTok->is(tok::l_paren)) 1805 parseParens(); 1806 if (FormatTok->is(tok::l_brace)) 1807 parseChildBlock(); 1808 break; 1809 case tok::l_brace: 1810 if (NextLBracesType != TT_Unknown) 1811 FormatTok->setFinalizedType(NextLBracesType); 1812 if (!tryToParsePropertyAccessor() && !tryToParseBracedList()) { 1813 // A block outside of parentheses must be the last part of a 1814 // structural element. 1815 // FIXME: Figure out cases where this is not true, and add projections 1816 // for them (the one we know is missing are lambdas). 1817 if (Style.Language == FormatStyle::LK_Java && 1818 Line->Tokens.front().Tok->is(Keywords.kw_synchronized)) { 1819 // If necessary, we could set the type to something different than 1820 // TT_FunctionLBrace. 1821 if (Style.BraceWrapping.AfterControlStatement == 1822 FormatStyle::BWACS_Always) { 1823 addUnwrappedLine(); 1824 } 1825 } else if (Style.BraceWrapping.AfterFunction) { 1826 addUnwrappedLine(); 1827 } 1828 if (!Line->InPPDirective) 1829 FormatTok->setFinalizedType(TT_FunctionLBrace); 1830 parseBlock(); 1831 addUnwrappedLine(); 1832 return; 1833 } 1834 // Otherwise this was a braced init list, and the structural 1835 // element continues. 1836 break; 1837 case tok::kw_try: 1838 if (Style.isJavaScript() && Line->MustBeDeclaration) { 1839 // field/method declaration. 1840 nextToken(); 1841 break; 1842 } 1843 // We arrive here when parsing function-try blocks. 1844 if (Style.BraceWrapping.AfterFunction) 1845 addUnwrappedLine(); 1846 parseTryCatch(); 1847 return; 1848 case tok::identifier: { 1849 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where) && 1850 Line->MustBeDeclaration) { 1851 addUnwrappedLine(); 1852 parseCSharpGenericTypeConstraint(); 1853 break; 1854 } 1855 if (FormatTok->is(TT_MacroBlockEnd)) { 1856 addUnwrappedLine(); 1857 return; 1858 } 1859 1860 // Function declarations (as opposed to function expressions) are parsed 1861 // on their own unwrapped line by continuing this loop. Function 1862 // expressions (functions that are not on their own line) must not create 1863 // a new unwrapped line, so they are special cased below. 1864 size_t TokenCount = Line->Tokens.size(); 1865 if (Style.isJavaScript() && FormatTok->is(Keywords.kw_function) && 1866 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is( 1867 Keywords.kw_async)))) { 1868 tryToParseJSFunction(); 1869 break; 1870 } 1871 if ((Style.isJavaScript() || Style.Language == FormatStyle::LK_Java) && 1872 FormatTok->is(Keywords.kw_interface)) { 1873 if (Style.isJavaScript()) { 1874 // In JavaScript/TypeScript, "interface" can be used as a standalone 1875 // identifier, e.g. in `var interface = 1;`. If "interface" is 1876 // followed by another identifier, it is very like to be an actual 1877 // interface declaration. 1878 unsigned StoredPosition = Tokens->getPosition(); 1879 FormatToken *Next = Tokens->getNextToken(); 1880 FormatTok = Tokens->setPosition(StoredPosition); 1881 if (!mustBeJSIdent(Keywords, Next)) { 1882 nextToken(); 1883 break; 1884 } 1885 } 1886 parseRecord(); 1887 addUnwrappedLine(); 1888 return; 1889 } 1890 1891 if (FormatTok->is(Keywords.kw_interface)) { 1892 if (parseStructLike()) 1893 return; 1894 break; 1895 } 1896 1897 if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) { 1898 parseStatementMacro(); 1899 return; 1900 } 1901 1902 // See if the following token should start a new unwrapped line. 1903 StringRef Text = FormatTok->TokenText; 1904 1905 FormatToken *PreviousToken = FormatTok; 1906 nextToken(); 1907 1908 // JS doesn't have macros, and within classes colons indicate fields, not 1909 // labels. 1910 if (Style.isJavaScript()) 1911 break; 1912 1913 auto OneTokenSoFar = [&]() { 1914 auto I = Line->Tokens.begin(), E = Line->Tokens.end(); 1915 while (I != E && I->Tok->is(tok::comment)) 1916 ++I; 1917 while (I != E && Style.isVerilog() && I->Tok->is(tok::hash)) 1918 ++I; 1919 return I != E && (++I == E); 1920 }; 1921 if (OneTokenSoFar()) { 1922 if (FormatTok->is(tok::colon) && !Line->MustBeDeclaration) { 1923 Line->Tokens.begin()->Tok->MustBreakBefore = true; 1924 parseLabel(!Style.IndentGotoLabels); 1925 if (HasLabel) 1926 *HasLabel = true; 1927 return; 1928 } 1929 // Recognize function-like macro usages without trailing semicolon as 1930 // well as free-standing macros like Q_OBJECT. 1931 bool FunctionLike = FormatTok->is(tok::l_paren); 1932 if (FunctionLike) 1933 parseParens(); 1934 1935 bool FollowedByNewline = 1936 CommentsBeforeNextToken.empty() 1937 ? FormatTok->NewlinesBefore > 0 1938 : CommentsBeforeNextToken.front()->NewlinesBefore > 0; 1939 1940 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && 1941 tokenCanStartNewLine(*FormatTok) && Text == Text.upper()) { 1942 PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro); 1943 addUnwrappedLine(); 1944 return; 1945 } 1946 } 1947 break; 1948 } 1949 case tok::equal: 1950 if ((Style.isJavaScript() || Style.isCSharp()) && 1951 FormatTok->is(TT_FatArrow)) { 1952 tryToParseChildBlock(); 1953 break; 1954 } 1955 1956 nextToken(); 1957 if (FormatTok->is(tok::l_brace)) { 1958 // Block kind should probably be set to BK_BracedInit for any language. 1959 // C# needs this change to ensure that array initialisers and object 1960 // initialisers are indented the same way. 1961 if (Style.isCSharp()) 1962 FormatTok->setBlockKind(BK_BracedInit); 1963 nextToken(); 1964 parseBracedList(); 1965 } else if (Style.Language == FormatStyle::LK_Proto && 1966 FormatTok->is(tok::less)) { 1967 nextToken(); 1968 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 1969 /*ClosingBraceKind=*/tok::greater); 1970 } 1971 break; 1972 case tok::l_square: 1973 parseSquare(); 1974 break; 1975 case tok::kw_new: 1976 parseNew(); 1977 break; 1978 case tok::kw_case: 1979 if (Style.isJavaScript() && Line->MustBeDeclaration) { 1980 // 'case: string' field declaration. 1981 nextToken(); 1982 break; 1983 } 1984 parseCaseLabel(); 1985 break; 1986 default: 1987 nextToken(); 1988 break; 1989 } 1990 } while (!eof()); 1991 } 1992 1993 bool UnwrappedLineParser::tryToParsePropertyAccessor() { 1994 assert(FormatTok->is(tok::l_brace)); 1995 if (!Style.isCSharp()) 1996 return false; 1997 // See if it's a property accessor. 1998 if (FormatTok->Previous->isNot(tok::identifier)) 1999 return false; 2000 2001 // See if we are inside a property accessor. 2002 // 2003 // Record the current tokenPosition so that we can advance and 2004 // reset the current token. `Next` is not set yet so we need 2005 // another way to advance along the token stream. 2006 unsigned int StoredPosition = Tokens->getPosition(); 2007 FormatToken *Tok = Tokens->getNextToken(); 2008 2009 // A trivial property accessor is of the form: 2010 // { [ACCESS_SPECIFIER] [get]; [ACCESS_SPECIFIER] [set|init] } 2011 // Track these as they do not require line breaks to be introduced. 2012 bool HasSpecialAccessor = false; 2013 bool IsTrivialPropertyAccessor = true; 2014 while (!eof()) { 2015 if (Tok->isOneOf(tok::semi, tok::kw_public, tok::kw_private, 2016 tok::kw_protected, Keywords.kw_internal, Keywords.kw_get, 2017 Keywords.kw_init, Keywords.kw_set)) { 2018 if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set)) 2019 HasSpecialAccessor = true; 2020 Tok = Tokens->getNextToken(); 2021 continue; 2022 } 2023 if (Tok->isNot(tok::r_brace)) 2024 IsTrivialPropertyAccessor = false; 2025 break; 2026 } 2027 2028 if (!HasSpecialAccessor) { 2029 Tokens->setPosition(StoredPosition); 2030 return false; 2031 } 2032 2033 // Try to parse the property accessor: 2034 // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties 2035 Tokens->setPosition(StoredPosition); 2036 if (!IsTrivialPropertyAccessor && Style.BraceWrapping.AfterFunction) 2037 addUnwrappedLine(); 2038 nextToken(); 2039 do { 2040 switch (FormatTok->Tok.getKind()) { 2041 case tok::r_brace: 2042 nextToken(); 2043 if (FormatTok->is(tok::equal)) { 2044 while (!eof() && FormatTok->isNot(tok::semi)) 2045 nextToken(); 2046 nextToken(); 2047 } 2048 addUnwrappedLine(); 2049 return true; 2050 case tok::l_brace: 2051 ++Line->Level; 2052 parseBlock(/*MustBeDeclaration=*/true); 2053 addUnwrappedLine(); 2054 --Line->Level; 2055 break; 2056 case tok::equal: 2057 if (FormatTok->is(TT_FatArrow)) { 2058 ++Line->Level; 2059 do { 2060 nextToken(); 2061 } while (!eof() && FormatTok->isNot(tok::semi)); 2062 nextToken(); 2063 addUnwrappedLine(); 2064 --Line->Level; 2065 break; 2066 } 2067 nextToken(); 2068 break; 2069 default: 2070 if (FormatTok->isOneOf(Keywords.kw_get, Keywords.kw_init, 2071 Keywords.kw_set) && 2072 !IsTrivialPropertyAccessor) { 2073 // Non-trivial get/set needs to be on its own line. 2074 addUnwrappedLine(); 2075 } 2076 nextToken(); 2077 } 2078 } while (!eof()); 2079 2080 // Unreachable for well-formed code (paired '{' and '}'). 2081 return true; 2082 } 2083 2084 bool UnwrappedLineParser::tryToParseLambda() { 2085 assert(FormatTok->is(tok::l_square)); 2086 if (!Style.isCpp()) { 2087 nextToken(); 2088 return false; 2089 } 2090 FormatToken &LSquare = *FormatTok; 2091 if (!tryToParseLambdaIntroducer()) 2092 return false; 2093 2094 bool SeenArrow = false; 2095 bool InTemplateParameterList = false; 2096 2097 while (FormatTok->isNot(tok::l_brace)) { 2098 if (FormatTok->isSimpleTypeSpecifier()) { 2099 nextToken(); 2100 continue; 2101 } 2102 switch (FormatTok->Tok.getKind()) { 2103 case tok::l_brace: 2104 break; 2105 case tok::l_paren: 2106 parseParens(); 2107 break; 2108 case tok::l_square: 2109 parseSquare(); 2110 break; 2111 case tok::kw_class: 2112 case tok::kw_template: 2113 case tok::kw_typename: 2114 assert(FormatTok->Previous); 2115 if (FormatTok->Previous->is(tok::less)) 2116 InTemplateParameterList = true; 2117 nextToken(); 2118 break; 2119 case tok::amp: 2120 case tok::star: 2121 case tok::kw_const: 2122 case tok::comma: 2123 case tok::less: 2124 case tok::greater: 2125 case tok::identifier: 2126 case tok::numeric_constant: 2127 case tok::coloncolon: 2128 case tok::kw_mutable: 2129 case tok::kw_noexcept: 2130 nextToken(); 2131 break; 2132 // Specialization of a template with an integer parameter can contain 2133 // arithmetic, logical, comparison and ternary operators. 2134 // 2135 // FIXME: This also accepts sequences of operators that are not in the scope 2136 // of a template argument list. 2137 // 2138 // In a C++ lambda a template type can only occur after an arrow. We use 2139 // this as an heuristic to distinguish between Objective-C expressions 2140 // followed by an `a->b` expression, such as: 2141 // ([obj func:arg] + a->b) 2142 // Otherwise the code below would parse as a lambda. 2143 // 2144 // FIXME: This heuristic is incorrect for C++20 generic lambdas with 2145 // explicit template lists: []<bool b = true && false>(U &&u){} 2146 case tok::plus: 2147 case tok::minus: 2148 case tok::exclaim: 2149 case tok::tilde: 2150 case tok::slash: 2151 case tok::percent: 2152 case tok::lessless: 2153 case tok::pipe: 2154 case tok::pipepipe: 2155 case tok::ampamp: 2156 case tok::caret: 2157 case tok::equalequal: 2158 case tok::exclaimequal: 2159 case tok::greaterequal: 2160 case tok::lessequal: 2161 case tok::question: 2162 case tok::colon: 2163 case tok::ellipsis: 2164 case tok::kw_true: 2165 case tok::kw_false: 2166 if (SeenArrow || InTemplateParameterList) { 2167 nextToken(); 2168 break; 2169 } 2170 return true; 2171 case tok::arrow: 2172 // This might or might not actually be a lambda arrow (this could be an 2173 // ObjC method invocation followed by a dereferencing arrow). We might 2174 // reset this back to TT_Unknown in TokenAnnotator. 2175 FormatTok->setFinalizedType(TT_LambdaArrow); 2176 SeenArrow = true; 2177 nextToken(); 2178 break; 2179 default: 2180 return true; 2181 } 2182 } 2183 FormatTok->setFinalizedType(TT_LambdaLBrace); 2184 LSquare.setFinalizedType(TT_LambdaLSquare); 2185 parseChildBlock(); 2186 return true; 2187 } 2188 2189 bool UnwrappedLineParser::tryToParseLambdaIntroducer() { 2190 const FormatToken *Previous = FormatTok->Previous; 2191 const FormatToken *LeftSquare = FormatTok; 2192 nextToken(); 2193 if (Previous && 2194 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new, 2195 tok::kw_delete, tok::l_square) || 2196 LeftSquare->isCppStructuredBinding(Style) || Previous->closesScope() || 2197 Previous->isSimpleTypeSpecifier())) { 2198 return false; 2199 } 2200 if (FormatTok->is(tok::l_square)) 2201 return false; 2202 if (FormatTok->is(tok::r_square)) { 2203 const FormatToken *Next = Tokens->peekNextToken(); 2204 if (Next->is(tok::greater)) 2205 return false; 2206 } 2207 parseSquare(/*LambdaIntroducer=*/true); 2208 return true; 2209 } 2210 2211 void UnwrappedLineParser::tryToParseJSFunction() { 2212 assert(FormatTok->is(Keywords.kw_function) || 2213 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)); 2214 if (FormatTok->is(Keywords.kw_async)) 2215 nextToken(); 2216 // Consume "function". 2217 nextToken(); 2218 2219 // Consume * (generator function). Treat it like C++'s overloaded operators. 2220 if (FormatTok->is(tok::star)) { 2221 FormatTok->setFinalizedType(TT_OverloadedOperator); 2222 nextToken(); 2223 } 2224 2225 // Consume function name. 2226 if (FormatTok->is(tok::identifier)) 2227 nextToken(); 2228 2229 if (FormatTok->isNot(tok::l_paren)) 2230 return; 2231 2232 // Parse formal parameter list. 2233 parseParens(); 2234 2235 if (FormatTok->is(tok::colon)) { 2236 // Parse a type definition. 2237 nextToken(); 2238 2239 // Eat the type declaration. For braced inline object types, balance braces, 2240 // otherwise just parse until finding an l_brace for the function body. 2241 if (FormatTok->is(tok::l_brace)) 2242 tryToParseBracedList(); 2243 else 2244 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof()) 2245 nextToken(); 2246 } 2247 2248 if (FormatTok->is(tok::semi)) 2249 return; 2250 2251 parseChildBlock(); 2252 } 2253 2254 bool UnwrappedLineParser::tryToParseBracedList() { 2255 if (FormatTok->is(BK_Unknown)) 2256 calculateBraceTypes(); 2257 assert(FormatTok->isNot(BK_Unknown)); 2258 if (FormatTok->is(BK_Block)) 2259 return false; 2260 nextToken(); 2261 parseBracedList(); 2262 return true; 2263 } 2264 2265 bool UnwrappedLineParser::tryToParseChildBlock() { 2266 assert(Style.isJavaScript() || Style.isCSharp()); 2267 assert(FormatTok->is(TT_FatArrow)); 2268 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType TT_FatArrow. 2269 // They always start an expression or a child block if followed by a curly 2270 // brace. 2271 nextToken(); 2272 if (FormatTok->isNot(tok::l_brace)) 2273 return false; 2274 parseChildBlock(); 2275 return true; 2276 } 2277 2278 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons, 2279 bool IsEnum, 2280 tok::TokenKind ClosingBraceKind) { 2281 bool HasError = false; 2282 2283 // FIXME: Once we have an expression parser in the UnwrappedLineParser, 2284 // replace this by using parseAssignmentExpression() inside. 2285 do { 2286 if (Style.isCSharp() && FormatTok->is(TT_FatArrow) && 2287 tryToParseChildBlock()) { 2288 continue; 2289 } 2290 if (Style.isJavaScript()) { 2291 if (FormatTok->is(Keywords.kw_function) || 2292 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) { 2293 tryToParseJSFunction(); 2294 continue; 2295 } 2296 if (FormatTok->is(tok::l_brace)) { 2297 // Could be a method inside of a braced list `{a() { return 1; }}`. 2298 if (tryToParseBracedList()) 2299 continue; 2300 parseChildBlock(); 2301 } 2302 } 2303 if (FormatTok->Tok.getKind() == ClosingBraceKind) { 2304 if (IsEnum && !Style.AllowShortEnumsOnASingleLine) 2305 addUnwrappedLine(); 2306 nextToken(); 2307 return !HasError; 2308 } 2309 switch (FormatTok->Tok.getKind()) { 2310 case tok::l_square: 2311 if (Style.isCSharp()) 2312 parseSquare(); 2313 else 2314 tryToParseLambda(); 2315 break; 2316 case tok::l_paren: 2317 parseParens(); 2318 // JavaScript can just have free standing methods and getters/setters in 2319 // object literals. Detect them by a "{" following ")". 2320 if (Style.isJavaScript()) { 2321 if (FormatTok->is(tok::l_brace)) 2322 parseChildBlock(); 2323 break; 2324 } 2325 break; 2326 case tok::l_brace: 2327 // Assume there are no blocks inside a braced init list apart 2328 // from the ones we explicitly parse out (like lambdas). 2329 FormatTok->setBlockKind(BK_BracedInit); 2330 nextToken(); 2331 parseBracedList(); 2332 break; 2333 case tok::less: 2334 if (Style.Language == FormatStyle::LK_Proto || 2335 ClosingBraceKind == tok::greater) { 2336 nextToken(); 2337 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 2338 /*ClosingBraceKind=*/tok::greater); 2339 } else { 2340 nextToken(); 2341 } 2342 break; 2343 case tok::semi: 2344 // JavaScript (or more precisely TypeScript) can have semicolons in braced 2345 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be 2346 // used for error recovery if we have otherwise determined that this is 2347 // a braced list. 2348 if (Style.isJavaScript()) { 2349 nextToken(); 2350 break; 2351 } 2352 HasError = true; 2353 if (!ContinueOnSemicolons) 2354 return !HasError; 2355 nextToken(); 2356 break; 2357 case tok::comma: 2358 nextToken(); 2359 if (IsEnum && !Style.AllowShortEnumsOnASingleLine) 2360 addUnwrappedLine(); 2361 break; 2362 default: 2363 nextToken(); 2364 break; 2365 } 2366 } while (!eof()); 2367 return false; 2368 } 2369 2370 /// \brief Parses a pair of parentheses (and everything between them). 2371 /// \param AmpAmpTokenType If different than TT_Unknown sets this type for all 2372 /// double ampersands. This only counts for the current parens scope. 2373 void UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) { 2374 assert(FormatTok->is(tok::l_paren) && "'(' expected."); 2375 nextToken(); 2376 do { 2377 switch (FormatTok->Tok.getKind()) { 2378 case tok::l_paren: 2379 parseParens(); 2380 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace)) 2381 parseChildBlock(); 2382 break; 2383 case tok::r_paren: 2384 nextToken(); 2385 return; 2386 case tok::r_brace: 2387 // A "}" inside parenthesis is an error if there wasn't a matching "{". 2388 return; 2389 case tok::l_square: 2390 tryToParseLambda(); 2391 break; 2392 case tok::l_brace: 2393 if (!tryToParseBracedList()) 2394 parseChildBlock(); 2395 break; 2396 case tok::at: 2397 nextToken(); 2398 if (FormatTok->is(tok::l_brace)) { 2399 nextToken(); 2400 parseBracedList(); 2401 } 2402 break; 2403 case tok::equal: 2404 if (Style.isCSharp() && FormatTok->is(TT_FatArrow)) 2405 tryToParseChildBlock(); 2406 else 2407 nextToken(); 2408 break; 2409 case tok::kw_class: 2410 if (Style.isJavaScript()) 2411 parseRecord(/*ParseAsExpr=*/true); 2412 else 2413 nextToken(); 2414 break; 2415 case tok::identifier: 2416 if (Style.isJavaScript() && 2417 (FormatTok->is(Keywords.kw_function) || 2418 FormatTok->startsSequence(Keywords.kw_async, 2419 Keywords.kw_function))) { 2420 tryToParseJSFunction(); 2421 } else { 2422 nextToken(); 2423 } 2424 break; 2425 case tok::kw_requires: { 2426 auto RequiresToken = FormatTok; 2427 nextToken(); 2428 parseRequiresExpression(RequiresToken); 2429 break; 2430 } 2431 case tok::ampamp: 2432 if (AmpAmpTokenType != TT_Unknown) 2433 FormatTok->setFinalizedType(AmpAmpTokenType); 2434 LLVM_FALLTHROUGH; 2435 default: 2436 nextToken(); 2437 break; 2438 } 2439 } while (!eof()); 2440 } 2441 2442 void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) { 2443 if (!LambdaIntroducer) { 2444 assert(FormatTok->is(tok::l_square) && "'[' expected."); 2445 if (tryToParseLambda()) 2446 return; 2447 } 2448 do { 2449 switch (FormatTok->Tok.getKind()) { 2450 case tok::l_paren: 2451 parseParens(); 2452 break; 2453 case tok::r_square: 2454 nextToken(); 2455 return; 2456 case tok::r_brace: 2457 // A "}" inside parenthesis is an error if there wasn't a matching "{". 2458 return; 2459 case tok::l_square: 2460 parseSquare(); 2461 break; 2462 case tok::l_brace: { 2463 if (!tryToParseBracedList()) 2464 parseChildBlock(); 2465 break; 2466 } 2467 case tok::at: 2468 nextToken(); 2469 if (FormatTok->is(tok::l_brace)) { 2470 nextToken(); 2471 parseBracedList(); 2472 } 2473 break; 2474 default: 2475 nextToken(); 2476 break; 2477 } 2478 } while (!eof()); 2479 } 2480 2481 void UnwrappedLineParser::keepAncestorBraces() { 2482 if (!Style.RemoveBracesLLVM) 2483 return; 2484 2485 const int MaxNestingLevels = 2; 2486 const int Size = NestedTooDeep.size(); 2487 if (Size >= MaxNestingLevels) 2488 NestedTooDeep[Size - MaxNestingLevels] = true; 2489 NestedTooDeep.push_back(false); 2490 } 2491 2492 static FormatToken *getLastNonComment(const UnwrappedLine &Line) { 2493 for (const auto &Token : llvm::reverse(Line.Tokens)) 2494 if (Token.Tok->isNot(tok::comment)) 2495 return Token.Tok; 2496 2497 return nullptr; 2498 } 2499 2500 void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) { 2501 FormatToken *Tok = nullptr; 2502 2503 if (Style.InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() && 2504 PreprocessorDirectives.empty()) { 2505 Tok = getLastNonComment(*Line); 2506 assert(Tok); 2507 if (Tok->BraceCount < 0) { 2508 assert(Tok->BraceCount == -1); 2509 Tok = nullptr; 2510 } else { 2511 Tok->BraceCount = -1; 2512 } 2513 } 2514 2515 addUnwrappedLine(); 2516 ++Line->Level; 2517 parseStructuralElement(); 2518 2519 if (Tok) { 2520 assert(!Line->InPPDirective); 2521 Tok = nullptr; 2522 for (const auto &L : llvm::reverse(*CurrentLines)) { 2523 if (!L.InPPDirective && getLastNonComment(L)) { 2524 Tok = L.Tokens.back().Tok; 2525 break; 2526 } 2527 } 2528 assert(Tok); 2529 ++Tok->BraceCount; 2530 } 2531 2532 if (CheckEOF && FormatTok->is(tok::eof)) 2533 addUnwrappedLine(); 2534 2535 --Line->Level; 2536 } 2537 2538 static void markOptionalBraces(FormatToken *LeftBrace) { 2539 if (!LeftBrace) 2540 return; 2541 2542 assert(LeftBrace->is(tok::l_brace)); 2543 2544 FormatToken *RightBrace = LeftBrace->MatchingParen; 2545 if (!RightBrace) { 2546 assert(!LeftBrace->Optional); 2547 return; 2548 } 2549 2550 assert(RightBrace->is(tok::r_brace)); 2551 assert(RightBrace->MatchingParen == LeftBrace); 2552 assert(LeftBrace->Optional == RightBrace->Optional); 2553 2554 LeftBrace->Optional = true; 2555 RightBrace->Optional = true; 2556 } 2557 2558 void UnwrappedLineParser::handleAttributes() { 2559 // Handle AttributeMacro, e.g. `if (x) UNLIKELY`. 2560 if (FormatTok->is(TT_AttributeMacro)) 2561 nextToken(); 2562 handleCppAttributes(); 2563 } 2564 2565 bool UnwrappedLineParser::handleCppAttributes() { 2566 // Handle [[likely]] / [[unlikely]] attributes. 2567 if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute()) { 2568 parseSquare(); 2569 return true; 2570 } 2571 return false; 2572 } 2573 2574 FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind, 2575 bool KeepBraces) { 2576 assert(FormatTok->is(tok::kw_if) && "'if' expected"); 2577 nextToken(); 2578 if (FormatTok->is(tok::exclaim)) 2579 nextToken(); 2580 2581 bool KeepIfBraces = true; 2582 if (FormatTok->is(tok::kw_consteval)) { 2583 nextToken(); 2584 } else { 2585 KeepIfBraces = !Style.RemoveBracesLLVM || KeepBraces; 2586 if (FormatTok->isOneOf(tok::kw_constexpr, tok::identifier)) 2587 nextToken(); 2588 if (FormatTok->is(tok::l_paren)) 2589 parseParens(); 2590 } 2591 handleAttributes(); 2592 2593 bool NeedsUnwrappedLine = false; 2594 keepAncestorBraces(); 2595 2596 FormatToken *IfLeftBrace = nullptr; 2597 IfStmtKind IfBlockKind = IfStmtKind::NotIf; 2598 2599 if (Keywords.isBlockBegin(*FormatTok, Style)) { 2600 FormatTok->setFinalizedType(TT_ControlStatementLBrace); 2601 IfLeftBrace = FormatTok; 2602 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2603 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, 2604 /*MunchSemi=*/true, KeepIfBraces, &IfBlockKind); 2605 if (Style.BraceWrapping.BeforeElse) 2606 addUnwrappedLine(); 2607 else 2608 NeedsUnwrappedLine = true; 2609 } else { 2610 parseUnbracedBody(); 2611 } 2612 2613 if (Style.RemoveBracesLLVM) { 2614 assert(!NestedTooDeep.empty()); 2615 KeepIfBraces = KeepIfBraces || 2616 (IfLeftBrace && !IfLeftBrace->MatchingParen) || 2617 NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly || 2618 IfBlockKind == IfStmtKind::IfElseIf; 2619 } 2620 2621 bool KeepElseBraces = KeepIfBraces; 2622 FormatToken *ElseLeftBrace = nullptr; 2623 IfStmtKind Kind = IfStmtKind::IfOnly; 2624 2625 if (FormatTok->is(tok::kw_else)) { 2626 if (Style.RemoveBracesLLVM) { 2627 NestedTooDeep.back() = false; 2628 Kind = IfStmtKind::IfElse; 2629 } 2630 nextToken(); 2631 handleAttributes(); 2632 if (Keywords.isBlockBegin(*FormatTok, Style)) { 2633 const bool FollowedByIf = Tokens->peekNextToken()->is(tok::kw_if); 2634 FormatTok->setFinalizedType(TT_ElseLBrace); 2635 ElseLeftBrace = FormatTok; 2636 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2637 IfStmtKind ElseBlockKind = IfStmtKind::NotIf; 2638 FormatToken *IfLBrace = 2639 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, 2640 /*MunchSemi=*/true, KeepElseBraces, &ElseBlockKind); 2641 if (FormatTok->is(tok::kw_else)) { 2642 KeepElseBraces = KeepElseBraces || 2643 ElseBlockKind == IfStmtKind::IfOnly || 2644 ElseBlockKind == IfStmtKind::IfElseIf; 2645 } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) { 2646 KeepElseBraces = true; 2647 assert(ElseLeftBrace->MatchingParen); 2648 markOptionalBraces(ElseLeftBrace); 2649 } 2650 addUnwrappedLine(); 2651 } else if (FormatTok->is(tok::kw_if)) { 2652 const FormatToken *Previous = Tokens->getPreviousToken(); 2653 assert(Previous); 2654 const bool IsPrecededByComment = Previous->is(tok::comment); 2655 if (IsPrecededByComment) { 2656 addUnwrappedLine(); 2657 ++Line->Level; 2658 } 2659 bool TooDeep = true; 2660 if (Style.RemoveBracesLLVM) { 2661 Kind = IfStmtKind::IfElseIf; 2662 TooDeep = NestedTooDeep.pop_back_val(); 2663 } 2664 ElseLeftBrace = parseIfThenElse(/*IfKind=*/nullptr, KeepIfBraces); 2665 if (Style.RemoveBracesLLVM) 2666 NestedTooDeep.push_back(TooDeep); 2667 if (IsPrecededByComment) 2668 --Line->Level; 2669 } else { 2670 parseUnbracedBody(/*CheckEOF=*/true); 2671 } 2672 } else { 2673 KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse; 2674 if (NeedsUnwrappedLine) 2675 addUnwrappedLine(); 2676 } 2677 2678 if (!Style.RemoveBracesLLVM) 2679 return nullptr; 2680 2681 assert(!NestedTooDeep.empty()); 2682 KeepElseBraces = KeepElseBraces || 2683 (ElseLeftBrace && !ElseLeftBrace->MatchingParen) || 2684 NestedTooDeep.back(); 2685 2686 NestedTooDeep.pop_back(); 2687 2688 if (!KeepIfBraces && !KeepElseBraces) { 2689 markOptionalBraces(IfLeftBrace); 2690 markOptionalBraces(ElseLeftBrace); 2691 } else if (IfLeftBrace) { 2692 FormatToken *IfRightBrace = IfLeftBrace->MatchingParen; 2693 if (IfRightBrace) { 2694 assert(IfRightBrace->MatchingParen == IfLeftBrace); 2695 assert(!IfLeftBrace->Optional); 2696 assert(!IfRightBrace->Optional); 2697 IfLeftBrace->MatchingParen = nullptr; 2698 IfRightBrace->MatchingParen = nullptr; 2699 } 2700 } 2701 2702 if (IfKind) 2703 *IfKind = Kind; 2704 2705 return IfLeftBrace; 2706 } 2707 2708 void UnwrappedLineParser::parseTryCatch() { 2709 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected"); 2710 nextToken(); 2711 bool NeedsUnwrappedLine = false; 2712 if (FormatTok->is(tok::colon)) { 2713 // We are in a function try block, what comes is an initializer list. 2714 nextToken(); 2715 2716 // In case identifiers were removed by clang-tidy, what might follow is 2717 // multiple commas in sequence - before the first identifier. 2718 while (FormatTok->is(tok::comma)) 2719 nextToken(); 2720 2721 while (FormatTok->is(tok::identifier)) { 2722 nextToken(); 2723 if (FormatTok->is(tok::l_paren)) 2724 parseParens(); 2725 if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) && 2726 FormatTok->is(tok::l_brace)) { 2727 do { 2728 nextToken(); 2729 } while (!FormatTok->is(tok::r_brace)); 2730 nextToken(); 2731 } 2732 2733 // In case identifiers were removed by clang-tidy, what might follow is 2734 // multiple commas in sequence - after the first identifier. 2735 while (FormatTok->is(tok::comma)) 2736 nextToken(); 2737 } 2738 } 2739 // Parse try with resource. 2740 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) 2741 parseParens(); 2742 2743 keepAncestorBraces(); 2744 2745 if (FormatTok->is(tok::l_brace)) { 2746 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2747 parseBlock(); 2748 if (Style.BraceWrapping.BeforeCatch) 2749 addUnwrappedLine(); 2750 else 2751 NeedsUnwrappedLine = true; 2752 } else if (!FormatTok->is(tok::kw_catch)) { 2753 // The C++ standard requires a compound-statement after a try. 2754 // If there's none, we try to assume there's a structuralElement 2755 // and try to continue. 2756 addUnwrappedLine(); 2757 ++Line->Level; 2758 parseStructuralElement(); 2759 --Line->Level; 2760 } 2761 while (true) { 2762 if (FormatTok->is(tok::at)) 2763 nextToken(); 2764 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, 2765 tok::kw___finally) || 2766 ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) && 2767 FormatTok->is(Keywords.kw_finally)) || 2768 (FormatTok->isObjCAtKeyword(tok::objc_catch) || 2769 FormatTok->isObjCAtKeyword(tok::objc_finally)))) { 2770 break; 2771 } 2772 nextToken(); 2773 while (FormatTok->isNot(tok::l_brace)) { 2774 if (FormatTok->is(tok::l_paren)) { 2775 parseParens(); 2776 continue; 2777 } 2778 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) { 2779 if (Style.RemoveBracesLLVM) 2780 NestedTooDeep.pop_back(); 2781 return; 2782 } 2783 nextToken(); 2784 } 2785 NeedsUnwrappedLine = false; 2786 Line->MustBeDeclaration = false; 2787 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2788 parseBlock(); 2789 if (Style.BraceWrapping.BeforeCatch) 2790 addUnwrappedLine(); 2791 else 2792 NeedsUnwrappedLine = true; 2793 } 2794 2795 if (Style.RemoveBracesLLVM) 2796 NestedTooDeep.pop_back(); 2797 2798 if (NeedsUnwrappedLine) 2799 addUnwrappedLine(); 2800 } 2801 2802 void UnwrappedLineParser::parseNamespace() { 2803 assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) && 2804 "'namespace' expected"); 2805 2806 const FormatToken &InitialToken = *FormatTok; 2807 nextToken(); 2808 if (InitialToken.is(TT_NamespaceMacro)) { 2809 parseParens(); 2810 } else { 2811 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline, 2812 tok::l_square, tok::period, tok::l_paren) || 2813 (Style.isCSharp() && FormatTok->is(tok::kw_union))) { 2814 if (FormatTok->is(tok::l_square)) 2815 parseSquare(); 2816 else if (FormatTok->is(tok::l_paren)) 2817 parseParens(); 2818 else 2819 nextToken(); 2820 } 2821 } 2822 if (FormatTok->is(tok::l_brace)) { 2823 if (ShouldBreakBeforeBrace(Style, InitialToken)) 2824 addUnwrappedLine(); 2825 2826 unsigned AddLevels = 2827 Style.NamespaceIndentation == FormatStyle::NI_All || 2828 (Style.NamespaceIndentation == FormatStyle::NI_Inner && 2829 DeclarationScopeStack.size() > 1) 2830 ? 1u 2831 : 0u; 2832 bool ManageWhitesmithsBraces = 2833 AddLevels == 0u && 2834 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths; 2835 2836 // If we're in Whitesmiths mode, indent the brace if we're not indenting 2837 // the whole block. 2838 if (ManageWhitesmithsBraces) 2839 ++Line->Level; 2840 2841 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/true, 2842 /*KeepBraces=*/true, /*IfKind=*/nullptr, 2843 ManageWhitesmithsBraces); 2844 2845 // Munch the semicolon after a namespace. This is more common than one would 2846 // think. Putting the semicolon into its own line is very ugly. 2847 if (FormatTok->is(tok::semi)) 2848 nextToken(); 2849 2850 addUnwrappedLine(AddLevels > 0 ? LineLevel::Remove : LineLevel::Keep); 2851 2852 if (ManageWhitesmithsBraces) 2853 --Line->Level; 2854 } 2855 // FIXME: Add error handling. 2856 } 2857 2858 void UnwrappedLineParser::parseNew() { 2859 assert(FormatTok->is(tok::kw_new) && "'new' expected"); 2860 nextToken(); 2861 2862 if (Style.isCSharp()) { 2863 do { 2864 if (FormatTok->is(tok::l_brace)) 2865 parseBracedList(); 2866 2867 if (FormatTok->isOneOf(tok::semi, tok::comma)) 2868 return; 2869 2870 nextToken(); 2871 } while (!eof()); 2872 } 2873 2874 if (Style.Language != FormatStyle::LK_Java) 2875 return; 2876 2877 // In Java, we can parse everything up to the parens, which aren't optional. 2878 do { 2879 // There should not be a ;, { or } before the new's open paren. 2880 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace)) 2881 return; 2882 2883 // Consume the parens. 2884 if (FormatTok->is(tok::l_paren)) { 2885 parseParens(); 2886 2887 // If there is a class body of an anonymous class, consume that as child. 2888 if (FormatTok->is(tok::l_brace)) 2889 parseChildBlock(); 2890 return; 2891 } 2892 nextToken(); 2893 } while (!eof()); 2894 } 2895 2896 void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) { 2897 keepAncestorBraces(); 2898 2899 if (Keywords.isBlockBegin(*FormatTok, Style)) { 2900 if (!KeepBraces) 2901 FormatTok->setFinalizedType(TT_ControlStatementLBrace); 2902 FormatToken *LeftBrace = FormatTok; 2903 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2904 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, 2905 /*MunchSemi=*/true, KeepBraces); 2906 if (!KeepBraces) { 2907 assert(!NestedTooDeep.empty()); 2908 if (!NestedTooDeep.back()) 2909 markOptionalBraces(LeftBrace); 2910 } 2911 if (WrapRightBrace) 2912 addUnwrappedLine(); 2913 } else { 2914 parseUnbracedBody(); 2915 } 2916 2917 if (!KeepBraces) 2918 NestedTooDeep.pop_back(); 2919 } 2920 2921 void UnwrappedLineParser::parseForOrWhileLoop() { 2922 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && 2923 "'for', 'while' or foreach macro expected"); 2924 const bool KeepBraces = !Style.RemoveBracesLLVM || 2925 !FormatTok->isOneOf(tok::kw_for, tok::kw_while); 2926 2927 nextToken(); 2928 // JS' for await ( ... 2929 if (Style.isJavaScript() && FormatTok->is(Keywords.kw_await)) 2930 nextToken(); 2931 if (Style.isCpp() && FormatTok->is(tok::kw_co_await)) 2932 nextToken(); 2933 if (FormatTok->is(tok::l_paren)) 2934 parseParens(); 2935 2936 handleAttributes(); 2937 parseLoopBody(KeepBraces, /*WrapRightBrace=*/true); 2938 } 2939 2940 void UnwrappedLineParser::parseDoWhile() { 2941 assert(FormatTok->is(tok::kw_do) && "'do' expected"); 2942 nextToken(); 2943 2944 parseLoopBody(/*KeepBraces=*/true, Style.BraceWrapping.BeforeWhile); 2945 2946 // FIXME: Add error handling. 2947 if (!FormatTok->is(tok::kw_while)) { 2948 addUnwrappedLine(); 2949 return; 2950 } 2951 2952 // If in Whitesmiths mode, the line with the while() needs to be indented 2953 // to the same level as the block. 2954 if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) 2955 ++Line->Level; 2956 2957 nextToken(); 2958 parseStructuralElement(); 2959 } 2960 2961 void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) { 2962 nextToken(); 2963 unsigned OldLineLevel = Line->Level; 2964 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) 2965 --Line->Level; 2966 if (LeftAlignLabel) 2967 Line->Level = 0; 2968 2969 if (!Style.IndentCaseBlocks && CommentsBeforeNextToken.empty() && 2970 FormatTok->is(tok::l_brace)) { 2971 2972 CompoundStatementIndenter Indenter(this, Line->Level, 2973 Style.BraceWrapping.AfterCaseLabel, 2974 Style.BraceWrapping.IndentBraces); 2975 parseBlock(); 2976 if (FormatTok->is(tok::kw_break)) { 2977 if (Style.BraceWrapping.AfterControlStatement == 2978 FormatStyle::BWACS_Always) { 2979 addUnwrappedLine(); 2980 if (!Style.IndentCaseBlocks && 2981 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) { 2982 ++Line->Level; 2983 } 2984 } 2985 parseStructuralElement(); 2986 } 2987 addUnwrappedLine(); 2988 } else { 2989 if (FormatTok->is(tok::semi)) 2990 nextToken(); 2991 addUnwrappedLine(); 2992 } 2993 Line->Level = OldLineLevel; 2994 if (FormatTok->isNot(tok::l_brace)) { 2995 parseStructuralElement(); 2996 addUnwrappedLine(); 2997 } 2998 } 2999 3000 void UnwrappedLineParser::parseCaseLabel() { 3001 assert(FormatTok->is(tok::kw_case) && "'case' expected"); 3002 3003 // FIXME: fix handling of complex expressions here. 3004 do { 3005 nextToken(); 3006 } while (!eof() && !FormatTok->is(tok::colon)); 3007 parseLabel(); 3008 } 3009 3010 void UnwrappedLineParser::parseSwitch() { 3011 assert(FormatTok->is(tok::kw_switch) && "'switch' expected"); 3012 nextToken(); 3013 if (FormatTok->is(tok::l_paren)) 3014 parseParens(); 3015 3016 keepAncestorBraces(); 3017 3018 if (FormatTok->is(tok::l_brace)) { 3019 CompoundStatementIndenter Indenter(this, Style, Line->Level); 3020 parseBlock(); 3021 addUnwrappedLine(); 3022 } else { 3023 addUnwrappedLine(); 3024 ++Line->Level; 3025 parseStructuralElement(); 3026 --Line->Level; 3027 } 3028 3029 if (Style.RemoveBracesLLVM) 3030 NestedTooDeep.pop_back(); 3031 } 3032 3033 // Operators that can follow a C variable. 3034 static bool isCOperatorFollowingVar(tok::TokenKind kind) { 3035 switch (kind) { 3036 case tok::ampamp: 3037 case tok::ampequal: 3038 case tok::arrow: 3039 case tok::caret: 3040 case tok::caretequal: 3041 case tok::comma: 3042 case tok::ellipsis: 3043 case tok::equal: 3044 case tok::equalequal: 3045 case tok::exclaim: 3046 case tok::exclaimequal: 3047 case tok::greater: 3048 case tok::greaterequal: 3049 case tok::greatergreater: 3050 case tok::greatergreaterequal: 3051 case tok::l_paren: 3052 case tok::l_square: 3053 case tok::less: 3054 case tok::lessequal: 3055 case tok::lessless: 3056 case tok::lesslessequal: 3057 case tok::minus: 3058 case tok::minusequal: 3059 case tok::minusminus: 3060 case tok::percent: 3061 case tok::percentequal: 3062 case tok::period: 3063 case tok::pipe: 3064 case tok::pipeequal: 3065 case tok::pipepipe: 3066 case tok::plus: 3067 case tok::plusequal: 3068 case tok::plusplus: 3069 case tok::question: 3070 case tok::r_brace: 3071 case tok::r_paren: 3072 case tok::r_square: 3073 case tok::semi: 3074 case tok::slash: 3075 case tok::slashequal: 3076 case tok::star: 3077 case tok::starequal: 3078 return true; 3079 default: 3080 return false; 3081 } 3082 } 3083 3084 void UnwrappedLineParser::parseAccessSpecifier() { 3085 FormatToken *AccessSpecifierCandidate = FormatTok; 3086 nextToken(); 3087 // Understand Qt's slots. 3088 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots)) 3089 nextToken(); 3090 // Otherwise, we don't know what it is, and we'd better keep the next token. 3091 if (FormatTok->is(tok::colon)) { 3092 nextToken(); 3093 addUnwrappedLine(); 3094 } else if (!FormatTok->is(tok::coloncolon) && 3095 !isCOperatorFollowingVar(FormatTok->Tok.getKind())) { 3096 // Not a variable name nor namespace name. 3097 addUnwrappedLine(); 3098 } else if (AccessSpecifierCandidate) { 3099 // Consider the access specifier to be a C identifier. 3100 AccessSpecifierCandidate->Tok.setKind(tok::identifier); 3101 } 3102 } 3103 3104 /// \brief Parses a concept definition. 3105 /// \pre The current token has to be the concept keyword. 3106 /// 3107 /// Returns if either the concept has been completely parsed, or if it detects 3108 /// that the concept definition is incorrect. 3109 void UnwrappedLineParser::parseConcept() { 3110 assert(FormatTok->is(tok::kw_concept) && "'concept' expected"); 3111 nextToken(); 3112 if (!FormatTok->is(tok::identifier)) 3113 return; 3114 nextToken(); 3115 if (!FormatTok->is(tok::equal)) 3116 return; 3117 nextToken(); 3118 parseConstraintExpression(); 3119 if (FormatTok->is(tok::semi)) 3120 nextToken(); 3121 addUnwrappedLine(); 3122 } 3123 3124 /// \brief Parses a requires, decides if it is a clause or an expression. 3125 /// \pre The current token has to be the requires keyword. 3126 /// \returns true if it parsed a clause. 3127 bool clang::format::UnwrappedLineParser::parseRequires() { 3128 assert(FormatTok->is(tok::kw_requires) && "'requires' expected"); 3129 auto RequiresToken = FormatTok; 3130 3131 // We try to guess if it is a requires clause, or a requires expression. For 3132 // that we first consume the keyword and check the next token. 3133 nextToken(); 3134 3135 switch (FormatTok->Tok.getKind()) { 3136 case tok::l_brace: 3137 // This can only be an expression, never a clause. 3138 parseRequiresExpression(RequiresToken); 3139 return false; 3140 case tok::l_paren: 3141 // Clauses and expression can start with a paren, it's unclear what we have. 3142 break; 3143 default: 3144 // All other tokens can only be a clause. 3145 parseRequiresClause(RequiresToken); 3146 return true; 3147 } 3148 3149 // Looking forward we would have to decide if there are function declaration 3150 // like arguments to the requires expression: 3151 // requires (T t) { 3152 // Or there is a constraint expression for the requires clause: 3153 // requires (C<T> && ... 3154 3155 // But first let's look behind. 3156 auto *PreviousNonComment = RequiresToken->getPreviousNonComment(); 3157 3158 if (!PreviousNonComment || 3159 PreviousNonComment->is(TT_RequiresExpressionLBrace)) { 3160 // If there is no token, or an expression left brace, we are a requires 3161 // clause within a requires expression. 3162 parseRequiresClause(RequiresToken); 3163 return true; 3164 } 3165 3166 switch (PreviousNonComment->Tok.getKind()) { 3167 case tok::greater: 3168 case tok::r_paren: 3169 case tok::kw_noexcept: 3170 case tok::kw_const: 3171 // This is a requires clause. 3172 parseRequiresClause(RequiresToken); 3173 return true; 3174 case tok::amp: 3175 case tok::ampamp: { 3176 // This can be either: 3177 // if (... && requires (T t) ...) 3178 // Or 3179 // void member(...) && requires (C<T> ... 3180 // We check the one token before that for a const: 3181 // void member(...) const && requires (C<T> ... 3182 auto PrevPrev = PreviousNonComment->getPreviousNonComment(); 3183 if (PrevPrev && PrevPrev->is(tok::kw_const)) { 3184 parseRequiresClause(RequiresToken); 3185 return true; 3186 } 3187 break; 3188 } 3189 default: 3190 if (PreviousNonComment->isTypeOrIdentifier()) { 3191 // This is a requires clause. 3192 parseRequiresClause(RequiresToken); 3193 return true; 3194 } 3195 // It's an expression. 3196 parseRequiresExpression(RequiresToken); 3197 return false; 3198 } 3199 3200 // Now we look forward and try to check if the paren content is a parameter 3201 // list. The parameters can be cv-qualified and contain references or 3202 // pointers. 3203 // So we want basically to check for TYPE NAME, but TYPE can contain all kinds 3204 // of stuff: typename, const, *, &, &&, ::, identifiers. 3205 3206 int NextTokenOffset = 1; 3207 auto NextToken = Tokens->peekNextToken(NextTokenOffset); 3208 auto PeekNext = [&NextTokenOffset, &NextToken, this] { 3209 ++NextTokenOffset; 3210 NextToken = Tokens->peekNextToken(NextTokenOffset); 3211 }; 3212 3213 bool FoundType = false; 3214 bool LastWasColonColon = false; 3215 int OpenAngles = 0; 3216 3217 for (; NextTokenOffset < 50; PeekNext()) { 3218 switch (NextToken->Tok.getKind()) { 3219 case tok::kw_volatile: 3220 case tok::kw_const: 3221 case tok::comma: 3222 parseRequiresExpression(RequiresToken); 3223 return false; 3224 case tok::r_paren: 3225 case tok::pipepipe: 3226 parseRequiresClause(RequiresToken); 3227 return true; 3228 case tok::eof: 3229 // Break out of the loop. 3230 NextTokenOffset = 50; 3231 break; 3232 case tok::coloncolon: 3233 LastWasColonColon = true; 3234 break; 3235 case tok::identifier: 3236 if (FoundType && !LastWasColonColon && OpenAngles == 0) { 3237 parseRequiresExpression(RequiresToken); 3238 return false; 3239 } 3240 FoundType = true; 3241 LastWasColonColon = false; 3242 break; 3243 case tok::less: 3244 ++OpenAngles; 3245 break; 3246 case tok::greater: 3247 --OpenAngles; 3248 break; 3249 default: 3250 if (NextToken->isSimpleTypeSpecifier()) { 3251 parseRequiresExpression(RequiresToken); 3252 return false; 3253 } 3254 break; 3255 } 3256 } 3257 3258 // This seems to be a complicated expression, just assume it's a clause. 3259 parseRequiresClause(RequiresToken); 3260 return true; 3261 } 3262 3263 /// \brief Parses a requires clause. 3264 /// \param RequiresToken The requires keyword token, which starts this clause. 3265 /// \pre We need to be on the next token after the requires keyword. 3266 /// \sa parseRequiresExpression 3267 /// 3268 /// Returns if it either has finished parsing the clause, or it detects, that 3269 /// the clause is incorrect. 3270 void UnwrappedLineParser::parseRequiresClause(FormatToken *RequiresToken) { 3271 assert(FormatTok->getPreviousNonComment() == RequiresToken); 3272 assert(RequiresToken->is(tok::kw_requires) && "'requires' expected"); 3273 3274 // If there is no previous token, we are within a requires expression, 3275 // otherwise we will always have the template or function declaration in front 3276 // of it. 3277 bool InRequiresExpression = 3278 !RequiresToken->Previous || 3279 RequiresToken->Previous->is(TT_RequiresExpressionLBrace); 3280 3281 RequiresToken->setFinalizedType(InRequiresExpression 3282 ? TT_RequiresClauseInARequiresExpression 3283 : TT_RequiresClause); 3284 3285 parseConstraintExpression(); 3286 3287 if (!InRequiresExpression) 3288 FormatTok->Previous->ClosesRequiresClause = true; 3289 } 3290 3291 /// \brief Parses a requires expression. 3292 /// \param RequiresToken The requires keyword token, which starts this clause. 3293 /// \pre We need to be on the next token after the requires keyword. 3294 /// \sa parseRequiresClause 3295 /// 3296 /// Returns if it either has finished parsing the expression, or it detects, 3297 /// that the expression is incorrect. 3298 void UnwrappedLineParser::parseRequiresExpression(FormatToken *RequiresToken) { 3299 assert(FormatTok->getPreviousNonComment() == RequiresToken); 3300 assert(RequiresToken->is(tok::kw_requires) && "'requires' expected"); 3301 3302 RequiresToken->setFinalizedType(TT_RequiresExpression); 3303 3304 if (FormatTok->is(tok::l_paren)) { 3305 FormatTok->setFinalizedType(TT_RequiresExpressionLParen); 3306 parseParens(); 3307 } 3308 3309 if (FormatTok->is(tok::l_brace)) { 3310 FormatTok->setFinalizedType(TT_RequiresExpressionLBrace); 3311 parseChildBlock(/*CanContainBracedList=*/false, 3312 /*NextLBracesType=*/TT_CompoundRequirementLBrace); 3313 } 3314 } 3315 3316 /// \brief Parses a constraint expression. 3317 /// 3318 /// This is either the definition of a concept, or the body of a requires 3319 /// clause. It returns, when the parsing is complete, or the expression is 3320 /// incorrect. 3321 void UnwrappedLineParser::parseConstraintExpression() { 3322 // The special handling for lambdas is needed since tryToParseLambda() eats a 3323 // token and if a requires expression is the last part of a requires clause 3324 // and followed by an attribute like [[nodiscard]] the ClosesRequiresClause is 3325 // not set on the correct token. Thus we need to be aware if we even expect a 3326 // lambda to be possible. 3327 // template <typename T> requires requires { ... } [[nodiscard]] ...; 3328 bool LambdaNextTimeAllowed = true; 3329 do { 3330 bool LambdaThisTimeAllowed = std::exchange(LambdaNextTimeAllowed, false); 3331 3332 switch (FormatTok->Tok.getKind()) { 3333 case tok::kw_requires: { 3334 auto RequiresToken = FormatTok; 3335 nextToken(); 3336 parseRequiresExpression(RequiresToken); 3337 break; 3338 } 3339 3340 case tok::l_paren: 3341 parseParens(/*AmpAmpTokenType=*/TT_BinaryOperator); 3342 break; 3343 3344 case tok::l_square: 3345 if (!LambdaThisTimeAllowed || !tryToParseLambda()) 3346 return; 3347 break; 3348 3349 case tok::kw_const: 3350 case tok::semi: 3351 case tok::kw_class: 3352 case tok::kw_struct: 3353 case tok::kw_union: 3354 return; 3355 3356 case tok::l_brace: 3357 // Potential function body. 3358 return; 3359 3360 case tok::ampamp: 3361 case tok::pipepipe: 3362 FormatTok->setFinalizedType(TT_BinaryOperator); 3363 nextToken(); 3364 LambdaNextTimeAllowed = true; 3365 break; 3366 3367 case tok::comma: 3368 case tok::comment: 3369 LambdaNextTimeAllowed = LambdaThisTimeAllowed; 3370 nextToken(); 3371 break; 3372 3373 case tok::kw_sizeof: 3374 case tok::greater: 3375 case tok::greaterequal: 3376 case tok::greatergreater: 3377 case tok::less: 3378 case tok::lessequal: 3379 case tok::lessless: 3380 case tok::equalequal: 3381 case tok::exclaim: 3382 case tok::exclaimequal: 3383 case tok::plus: 3384 case tok::minus: 3385 case tok::star: 3386 case tok::slash: 3387 case tok::kw_decltype: 3388 LambdaNextTimeAllowed = true; 3389 // Just eat them. 3390 nextToken(); 3391 break; 3392 3393 case tok::numeric_constant: 3394 case tok::coloncolon: 3395 case tok::kw_true: 3396 case tok::kw_false: 3397 // Just eat them. 3398 nextToken(); 3399 break; 3400 3401 case tok::kw_static_cast: 3402 case tok::kw_const_cast: 3403 case tok::kw_reinterpret_cast: 3404 case tok::kw_dynamic_cast: 3405 nextToken(); 3406 if (!FormatTok->is(tok::less)) 3407 return; 3408 3409 nextToken(); 3410 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 3411 /*ClosingBraceKind=*/tok::greater); 3412 break; 3413 3414 case tok::kw_bool: 3415 // bool is only allowed if it is directly followed by a paren for a cast: 3416 // concept C = bool(...); 3417 // and bool is the only type, all other types as cast must be inside a 3418 // cast to bool an thus are handled by the other cases. 3419 nextToken(); 3420 if (FormatTok->isNot(tok::l_paren)) 3421 return; 3422 parseParens(); 3423 break; 3424 3425 default: 3426 if (!FormatTok->Tok.getIdentifierInfo()) { 3427 // Identifiers are part of the default case, we check for more then 3428 // tok::identifier to handle builtin type traits. 3429 return; 3430 } 3431 3432 // We need to differentiate identifiers for a template deduction guide, 3433 // variables, or function return types (the constraint expression has 3434 // ended before that), and basically all other cases. But it's easier to 3435 // check the other way around. 3436 assert(FormatTok->Previous); 3437 switch (FormatTok->Previous->Tok.getKind()) { 3438 case tok::coloncolon: // Nested identifier. 3439 case tok::ampamp: // Start of a function or variable for the 3440 case tok::pipepipe: // constraint expression. 3441 case tok::kw_requires: // Initial identifier of a requires clause. 3442 case tok::equal: // Initial identifier of a concept declaration. 3443 break; 3444 default: 3445 return; 3446 } 3447 3448 // Read identifier with optional template declaration. 3449 nextToken(); 3450 if (FormatTok->is(tok::less)) { 3451 nextToken(); 3452 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 3453 /*ClosingBraceKind=*/tok::greater); 3454 } 3455 break; 3456 } 3457 } while (!eof()); 3458 } 3459 3460 bool UnwrappedLineParser::parseEnum() { 3461 const FormatToken &InitialToken = *FormatTok; 3462 3463 // Won't be 'enum' for NS_ENUMs. 3464 if (FormatTok->is(tok::kw_enum)) 3465 nextToken(); 3466 3467 // In TypeScript, "enum" can also be used as property name, e.g. in interface 3468 // declarations. An "enum" keyword followed by a colon would be a syntax 3469 // error and thus assume it is just an identifier. 3470 if (Style.isJavaScript() && FormatTok->isOneOf(tok::colon, tok::question)) 3471 return false; 3472 3473 // In protobuf, "enum" can be used as a field name. 3474 if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal)) 3475 return false; 3476 3477 // Eat up enum class ... 3478 if (FormatTok->isOneOf(tok::kw_class, tok::kw_struct)) 3479 nextToken(); 3480 3481 while (FormatTok->Tok.getIdentifierInfo() || 3482 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, 3483 tok::greater, tok::comma, tok::question, 3484 tok::l_square, tok::r_square)) { 3485 nextToken(); 3486 // We can have macros or attributes in between 'enum' and the enum name. 3487 if (FormatTok->is(tok::l_paren)) 3488 parseParens(); 3489 if (FormatTok->is(TT_AttributeSquare)) { 3490 parseSquare(); 3491 // Consume the closing TT_AttributeSquare. 3492 if (FormatTok->Next && FormatTok->is(TT_AttributeSquare)) 3493 nextToken(); 3494 } 3495 if (FormatTok->is(tok::identifier)) { 3496 nextToken(); 3497 // If there are two identifiers in a row, this is likely an elaborate 3498 // return type. In Java, this can be "implements", etc. 3499 if (Style.isCpp() && FormatTok->is(tok::identifier)) 3500 return false; 3501 } 3502 } 3503 3504 // Just a declaration or something is wrong. 3505 if (FormatTok->isNot(tok::l_brace)) 3506 return true; 3507 FormatTok->setFinalizedType(TT_EnumLBrace); 3508 FormatTok->setBlockKind(BK_Block); 3509 3510 if (Style.Language == FormatStyle::LK_Java) { 3511 // Java enums are different. 3512 parseJavaEnumBody(); 3513 return true; 3514 } 3515 if (Style.Language == FormatStyle::LK_Proto) { 3516 parseBlock(/*MustBeDeclaration=*/true); 3517 return true; 3518 } 3519 3520 if (!Style.AllowShortEnumsOnASingleLine && 3521 ShouldBreakBeforeBrace(Style, InitialToken)) { 3522 addUnwrappedLine(); 3523 } 3524 // Parse enum body. 3525 nextToken(); 3526 if (!Style.AllowShortEnumsOnASingleLine) { 3527 addUnwrappedLine(); 3528 Line->Level += 1; 3529 } 3530 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true, 3531 /*IsEnum=*/true); 3532 if (!Style.AllowShortEnumsOnASingleLine) 3533 Line->Level -= 1; 3534 if (HasError) { 3535 if (FormatTok->is(tok::semi)) 3536 nextToken(); 3537 addUnwrappedLine(); 3538 } 3539 return true; 3540 3541 // There is no addUnwrappedLine() here so that we fall through to parsing a 3542 // structural element afterwards. Thus, in "enum A {} n, m;", 3543 // "} n, m;" will end up in one unwrapped line. 3544 } 3545 3546 bool UnwrappedLineParser::parseStructLike() { 3547 // parseRecord falls through and does not yet add an unwrapped line as a 3548 // record declaration or definition can start a structural element. 3549 parseRecord(); 3550 // This does not apply to Java, JavaScript and C#. 3551 if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() || 3552 Style.isCSharp()) { 3553 if (FormatTok->is(tok::semi)) 3554 nextToken(); 3555 addUnwrappedLine(); 3556 return true; 3557 } 3558 return false; 3559 } 3560 3561 namespace { 3562 // A class used to set and restore the Token position when peeking 3563 // ahead in the token source. 3564 class ScopedTokenPosition { 3565 unsigned StoredPosition; 3566 FormatTokenSource *Tokens; 3567 3568 public: 3569 ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) { 3570 assert(Tokens && "Tokens expected to not be null"); 3571 StoredPosition = Tokens->getPosition(); 3572 } 3573 3574 ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); } 3575 }; 3576 } // namespace 3577 3578 // Look to see if we have [[ by looking ahead, if 3579 // its not then rewind to the original position. 3580 bool UnwrappedLineParser::tryToParseSimpleAttribute() { 3581 ScopedTokenPosition AutoPosition(Tokens); 3582 FormatToken *Tok = Tokens->getNextToken(); 3583 // We already read the first [ check for the second. 3584 if (!Tok->is(tok::l_square)) 3585 return false; 3586 // Double check that the attribute is just something 3587 // fairly simple. 3588 while (Tok->isNot(tok::eof)) { 3589 if (Tok->is(tok::r_square)) 3590 break; 3591 Tok = Tokens->getNextToken(); 3592 } 3593 if (Tok->is(tok::eof)) 3594 return false; 3595 Tok = Tokens->getNextToken(); 3596 if (!Tok->is(tok::r_square)) 3597 return false; 3598 Tok = Tokens->getNextToken(); 3599 if (Tok->is(tok::semi)) 3600 return false; 3601 return true; 3602 } 3603 3604 void UnwrappedLineParser::parseJavaEnumBody() { 3605 assert(FormatTok->is(tok::l_brace)); 3606 const FormatToken *OpeningBrace = FormatTok; 3607 3608 // Determine whether the enum is simple, i.e. does not have a semicolon or 3609 // constants with class bodies. Simple enums can be formatted like braced 3610 // lists, contracted to a single line, etc. 3611 unsigned StoredPosition = Tokens->getPosition(); 3612 bool IsSimple = true; 3613 FormatToken *Tok = Tokens->getNextToken(); 3614 while (!Tok->is(tok::eof)) { 3615 if (Tok->is(tok::r_brace)) 3616 break; 3617 if (Tok->isOneOf(tok::l_brace, tok::semi)) { 3618 IsSimple = false; 3619 break; 3620 } 3621 // FIXME: This will also mark enums with braces in the arguments to enum 3622 // constants as "not simple". This is probably fine in practice, though. 3623 Tok = Tokens->getNextToken(); 3624 } 3625 FormatTok = Tokens->setPosition(StoredPosition); 3626 3627 if (IsSimple) { 3628 nextToken(); 3629 parseBracedList(); 3630 addUnwrappedLine(); 3631 return; 3632 } 3633 3634 // Parse the body of a more complex enum. 3635 // First add a line for everything up to the "{". 3636 nextToken(); 3637 addUnwrappedLine(); 3638 ++Line->Level; 3639 3640 // Parse the enum constants. 3641 while (FormatTok->isNot(tok::eof)) { 3642 if (FormatTok->is(tok::l_brace)) { 3643 // Parse the constant's class body. 3644 parseBlock(/*MustBeDeclaration=*/true, /*AddLevels=*/1u, 3645 /*MunchSemi=*/false); 3646 } else if (FormatTok->is(tok::l_paren)) { 3647 parseParens(); 3648 } else if (FormatTok->is(tok::comma)) { 3649 nextToken(); 3650 addUnwrappedLine(); 3651 } else if (FormatTok->is(tok::semi)) { 3652 nextToken(); 3653 addUnwrappedLine(); 3654 break; 3655 } else if (FormatTok->is(tok::r_brace)) { 3656 addUnwrappedLine(); 3657 break; 3658 } else { 3659 nextToken(); 3660 } 3661 } 3662 3663 // Parse the class body after the enum's ";" if any. 3664 parseLevel(OpeningBrace); 3665 nextToken(); 3666 --Line->Level; 3667 addUnwrappedLine(); 3668 } 3669 3670 void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { 3671 const FormatToken &InitialToken = *FormatTok; 3672 nextToken(); 3673 3674 // The actual identifier can be a nested name specifier, and in macros 3675 // it is often token-pasted. 3676 // An [[attribute]] can be before the identifier. 3677 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, 3678 tok::kw___attribute, tok::kw___declspec, 3679 tok::kw_alignas, tok::l_square, tok::r_square) || 3680 ((Style.Language == FormatStyle::LK_Java || Style.isJavaScript()) && 3681 FormatTok->isOneOf(tok::period, tok::comma))) { 3682 if (Style.isJavaScript() && 3683 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) { 3684 // JavaScript/TypeScript supports inline object types in 3685 // extends/implements positions: 3686 // class Foo implements {bar: number} { } 3687 nextToken(); 3688 if (FormatTok->is(tok::l_brace)) { 3689 tryToParseBracedList(); 3690 continue; 3691 } 3692 } 3693 bool IsNonMacroIdentifier = 3694 FormatTok->is(tok::identifier) && 3695 FormatTok->TokenText != FormatTok->TokenText.upper(); 3696 nextToken(); 3697 // We can have macros or attributes in between 'class' and the class name. 3698 if (!IsNonMacroIdentifier) { 3699 if (FormatTok->is(tok::l_paren)) { 3700 parseParens(); 3701 } else if (FormatTok->is(TT_AttributeSquare)) { 3702 parseSquare(); 3703 // Consume the closing TT_AttributeSquare. 3704 if (FormatTok->Next && FormatTok->is(TT_AttributeSquare)) 3705 nextToken(); 3706 } 3707 } 3708 } 3709 3710 // Note that parsing away template declarations here leads to incorrectly 3711 // accepting function declarations as record declarations. 3712 // In general, we cannot solve this problem. Consider: 3713 // class A<int> B() {} 3714 // which can be a function definition or a class definition when B() is a 3715 // macro. If we find enough real-world cases where this is a problem, we 3716 // can parse for the 'template' keyword in the beginning of the statement, 3717 // and thus rule out the record production in case there is no template 3718 // (this would still leave us with an ambiguity between template function 3719 // and class declarations). 3720 if (FormatTok->isOneOf(tok::colon, tok::less)) { 3721 do { 3722 if (FormatTok->is(tok::l_brace)) { 3723 calculateBraceTypes(/*ExpectClassBody=*/true); 3724 if (!tryToParseBracedList()) 3725 break; 3726 } 3727 if (FormatTok->is(tok::l_square)) { 3728 FormatToken *Previous = FormatTok->Previous; 3729 if (!Previous || 3730 !(Previous->is(tok::r_paren) || Previous->isTypeOrIdentifier())) { 3731 // Don't try parsing a lambda if we had a closing parenthesis before, 3732 // it was probably a pointer to an array: int (*)[]. 3733 if (!tryToParseLambda()) 3734 break; 3735 } else { 3736 parseSquare(); 3737 continue; 3738 } 3739 } 3740 if (FormatTok->is(tok::semi)) 3741 return; 3742 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)) { 3743 addUnwrappedLine(); 3744 nextToken(); 3745 parseCSharpGenericTypeConstraint(); 3746 break; 3747 } 3748 nextToken(); 3749 } while (!eof()); 3750 } 3751 3752 auto GetBraceType = [](const FormatToken &RecordTok) { 3753 switch (RecordTok.Tok.getKind()) { 3754 case tok::kw_class: 3755 return TT_ClassLBrace; 3756 case tok::kw_struct: 3757 return TT_StructLBrace; 3758 case tok::kw_union: 3759 return TT_UnionLBrace; 3760 default: 3761 // Useful for e.g. interface. 3762 return TT_RecordLBrace; 3763 } 3764 }; 3765 if (FormatTok->is(tok::l_brace)) { 3766 FormatTok->setFinalizedType(GetBraceType(InitialToken)); 3767 if (ParseAsExpr) { 3768 parseChildBlock(); 3769 } else { 3770 if (ShouldBreakBeforeBrace(Style, InitialToken)) 3771 addUnwrappedLine(); 3772 3773 unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u; 3774 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false); 3775 } 3776 } 3777 // There is no addUnwrappedLine() here so that we fall through to parsing a 3778 // structural element afterwards. Thus, in "class A {} n, m;", 3779 // "} n, m;" will end up in one unwrapped line. 3780 } 3781 3782 void UnwrappedLineParser::parseObjCMethod() { 3783 assert(FormatTok->isOneOf(tok::l_paren, tok::identifier) && 3784 "'(' or identifier expected."); 3785 do { 3786 if (FormatTok->is(tok::semi)) { 3787 nextToken(); 3788 addUnwrappedLine(); 3789 return; 3790 } else if (FormatTok->is(tok::l_brace)) { 3791 if (Style.BraceWrapping.AfterFunction) 3792 addUnwrappedLine(); 3793 parseBlock(); 3794 addUnwrappedLine(); 3795 return; 3796 } else { 3797 nextToken(); 3798 } 3799 } while (!eof()); 3800 } 3801 3802 void UnwrappedLineParser::parseObjCProtocolList() { 3803 assert(FormatTok->is(tok::less) && "'<' expected."); 3804 do { 3805 nextToken(); 3806 // Early exit in case someone forgot a close angle. 3807 if (FormatTok->isOneOf(tok::semi, tok::l_brace) || 3808 FormatTok->isObjCAtKeyword(tok::objc_end)) { 3809 return; 3810 } 3811 } while (!eof() && FormatTok->isNot(tok::greater)); 3812 nextToken(); // Skip '>'. 3813 } 3814 3815 void UnwrappedLineParser::parseObjCUntilAtEnd() { 3816 do { 3817 if (FormatTok->isObjCAtKeyword(tok::objc_end)) { 3818 nextToken(); 3819 addUnwrappedLine(); 3820 break; 3821 } 3822 if (FormatTok->is(tok::l_brace)) { 3823 parseBlock(); 3824 // In ObjC interfaces, nothing should be following the "}". 3825 addUnwrappedLine(); 3826 } else if (FormatTok->is(tok::r_brace)) { 3827 // Ignore stray "}". parseStructuralElement doesn't consume them. 3828 nextToken(); 3829 addUnwrappedLine(); 3830 } else if (FormatTok->isOneOf(tok::minus, tok::plus)) { 3831 nextToken(); 3832 parseObjCMethod(); 3833 } else { 3834 parseStructuralElement(); 3835 } 3836 } while (!eof()); 3837 } 3838 3839 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { 3840 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface || 3841 FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation); 3842 nextToken(); 3843 nextToken(); // interface name 3844 3845 // @interface can be followed by a lightweight generic 3846 // specialization list, then either a base class or a category. 3847 if (FormatTok->is(tok::less)) 3848 parseObjCLightweightGenerics(); 3849 if (FormatTok->is(tok::colon)) { 3850 nextToken(); 3851 nextToken(); // base class name 3852 // The base class can also have lightweight generics applied to it. 3853 if (FormatTok->is(tok::less)) 3854 parseObjCLightweightGenerics(); 3855 } else if (FormatTok->is(tok::l_paren)) { 3856 // Skip category, if present. 3857 parseParens(); 3858 } 3859 3860 if (FormatTok->is(tok::less)) 3861 parseObjCProtocolList(); 3862 3863 if (FormatTok->is(tok::l_brace)) { 3864 if (Style.BraceWrapping.AfterObjCDeclaration) 3865 addUnwrappedLine(); 3866 parseBlock(/*MustBeDeclaration=*/true); 3867 } 3868 3869 // With instance variables, this puts '}' on its own line. Without instance 3870 // variables, this ends the @interface line. 3871 addUnwrappedLine(); 3872 3873 parseObjCUntilAtEnd(); 3874 } 3875 3876 void UnwrappedLineParser::parseObjCLightweightGenerics() { 3877 assert(FormatTok->is(tok::less)); 3878 // Unlike protocol lists, generic parameterizations support 3879 // nested angles: 3880 // 3881 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> : 3882 // NSObject <NSCopying, NSSecureCoding> 3883 // 3884 // so we need to count how many open angles we have left. 3885 unsigned NumOpenAngles = 1; 3886 do { 3887 nextToken(); 3888 // Early exit in case someone forgot a close angle. 3889 if (FormatTok->isOneOf(tok::semi, tok::l_brace) || 3890 FormatTok->isObjCAtKeyword(tok::objc_end)) { 3891 break; 3892 } 3893 if (FormatTok->is(tok::less)) { 3894 ++NumOpenAngles; 3895 } else if (FormatTok->is(tok::greater)) { 3896 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative"); 3897 --NumOpenAngles; 3898 } 3899 } while (!eof() && NumOpenAngles != 0); 3900 nextToken(); // Skip '>'. 3901 } 3902 3903 // Returns true for the declaration/definition form of @protocol, 3904 // false for the expression form. 3905 bool UnwrappedLineParser::parseObjCProtocol() { 3906 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol); 3907 nextToken(); 3908 3909 if (FormatTok->is(tok::l_paren)) { 3910 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);". 3911 return false; 3912 } 3913 3914 // The definition/declaration form, 3915 // @protocol Foo 3916 // - (int)someMethod; 3917 // @end 3918 3919 nextToken(); // protocol name 3920 3921 if (FormatTok->is(tok::less)) 3922 parseObjCProtocolList(); 3923 3924 // Check for protocol declaration. 3925 if (FormatTok->is(tok::semi)) { 3926 nextToken(); 3927 addUnwrappedLine(); 3928 return true; 3929 } 3930 3931 addUnwrappedLine(); 3932 parseObjCUntilAtEnd(); 3933 return true; 3934 } 3935 3936 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { 3937 bool IsImport = FormatTok->is(Keywords.kw_import); 3938 assert(IsImport || FormatTok->is(tok::kw_export)); 3939 nextToken(); 3940 3941 // Consume the "default" in "export default class/function". 3942 if (FormatTok->is(tok::kw_default)) 3943 nextToken(); 3944 3945 // Consume "async function", "function" and "default function", so that these 3946 // get parsed as free-standing JS functions, i.e. do not require a trailing 3947 // semicolon. 3948 if (FormatTok->is(Keywords.kw_async)) 3949 nextToken(); 3950 if (FormatTok->is(Keywords.kw_function)) { 3951 nextToken(); 3952 return; 3953 } 3954 3955 // For imports, `export *`, `export {...}`, consume the rest of the line up 3956 // to the terminating `;`. For everything else, just return and continue 3957 // parsing the structural element, i.e. the declaration or expression for 3958 // `export default`. 3959 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) && 3960 !FormatTok->isStringLiteral()) { 3961 return; 3962 } 3963 3964 while (!eof()) { 3965 if (FormatTok->is(tok::semi)) 3966 return; 3967 if (Line->Tokens.empty()) { 3968 // Common issue: Automatic Semicolon Insertion wrapped the line, so the 3969 // import statement should terminate. 3970 return; 3971 } 3972 if (FormatTok->is(tok::l_brace)) { 3973 FormatTok->setBlockKind(BK_Block); 3974 nextToken(); 3975 parseBracedList(); 3976 } else { 3977 nextToken(); 3978 } 3979 } 3980 } 3981 3982 void UnwrappedLineParser::parseStatementMacro() { 3983 nextToken(); 3984 if (FormatTok->is(tok::l_paren)) 3985 parseParens(); 3986 if (FormatTok->is(tok::semi)) 3987 nextToken(); 3988 addUnwrappedLine(); 3989 } 3990 3991 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, 3992 StringRef Prefix = "") { 3993 llvm::dbgs() << Prefix << "Line(" << Line.Level 3994 << ", FSC=" << Line.FirstStartColumn << ")" 3995 << (Line.InPPDirective ? " MACRO" : "") << ": "; 3996 for (const auto &Node : Line.Tokens) { 3997 llvm::dbgs() << Node.Tok->Tok.getName() << "[" 3998 << "T=" << static_cast<unsigned>(Node.Tok->getType()) 3999 << ", OC=" << Node.Tok->OriginalColumn << "] "; 4000 } 4001 for (const auto &Node : Line.Tokens) 4002 for (const auto &ChildNode : Node.Children) 4003 printDebugInfo(ChildNode, "\nChild: "); 4004 4005 llvm::dbgs() << "\n"; 4006 } 4007 4008 void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) { 4009 if (Line->Tokens.empty()) 4010 return; 4011 LLVM_DEBUG({ 4012 if (CurrentLines == &Lines) 4013 printDebugInfo(*Line); 4014 }); 4015 4016 // If this line closes a block when in Whitesmiths mode, remember that 4017 // information so that the level can be decreased after the line is added. 4018 // This has to happen after the addition of the line since the line itself 4019 // needs to be indented. 4020 bool ClosesWhitesmithsBlock = 4021 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex && 4022 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths; 4023 4024 CurrentLines->push_back(std::move(*Line)); 4025 Line->Tokens.clear(); 4026 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex; 4027 Line->FirstStartColumn = 0; 4028 4029 if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove) 4030 --Line->Level; 4031 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { 4032 CurrentLines->append( 4033 std::make_move_iterator(PreprocessorDirectives.begin()), 4034 std::make_move_iterator(PreprocessorDirectives.end())); 4035 PreprocessorDirectives.clear(); 4036 } 4037 // Disconnect the current token from the last token on the previous line. 4038 FormatTok->Previous = nullptr; 4039 } 4040 4041 bool UnwrappedLineParser::eof() const { return FormatTok->is(tok::eof); } 4042 4043 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { 4044 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && 4045 FormatTok.NewlinesBefore > 0; 4046 } 4047 4048 // Checks if \p FormatTok is a line comment that continues the line comment 4049 // section on \p Line. 4050 static bool 4051 continuesLineCommentSection(const FormatToken &FormatTok, 4052 const UnwrappedLine &Line, 4053 const llvm::Regex &CommentPragmasRegex) { 4054 if (Line.Tokens.empty()) 4055 return false; 4056 4057 StringRef IndentContent = FormatTok.TokenText; 4058 if (FormatTok.TokenText.startswith("//") || 4059 FormatTok.TokenText.startswith("/*")) { 4060 IndentContent = FormatTok.TokenText.substr(2); 4061 } 4062 if (CommentPragmasRegex.match(IndentContent)) 4063 return false; 4064 4065 // If Line starts with a line comment, then FormatTok continues the comment 4066 // section if its original column is greater or equal to the original start 4067 // column of the line. 4068 // 4069 // Define the min column token of a line as follows: if a line ends in '{' or 4070 // contains a '{' followed by a line comment, then the min column token is 4071 // that '{'. Otherwise, the min column token of the line is the first token of 4072 // the line. 4073 // 4074 // If Line starts with a token other than a line comment, then FormatTok 4075 // continues the comment section if its original column is greater than the 4076 // original start column of the min column token of the line. 4077 // 4078 // For example, the second line comment continues the first in these cases: 4079 // 4080 // // first line 4081 // // second line 4082 // 4083 // and: 4084 // 4085 // // first line 4086 // // second line 4087 // 4088 // and: 4089 // 4090 // int i; // first line 4091 // // second line 4092 // 4093 // and: 4094 // 4095 // do { // first line 4096 // // second line 4097 // int i; 4098 // } while (true); 4099 // 4100 // and: 4101 // 4102 // enum { 4103 // a, // first line 4104 // // second line 4105 // b 4106 // }; 4107 // 4108 // The second line comment doesn't continue the first in these cases: 4109 // 4110 // // first line 4111 // // second line 4112 // 4113 // and: 4114 // 4115 // int i; // first line 4116 // // second line 4117 // 4118 // and: 4119 // 4120 // do { // first line 4121 // // second line 4122 // int i; 4123 // } while (true); 4124 // 4125 // and: 4126 // 4127 // enum { 4128 // a, // first line 4129 // // second line 4130 // }; 4131 const FormatToken *MinColumnToken = Line.Tokens.front().Tok; 4132 4133 // Scan for '{//'. If found, use the column of '{' as a min column for line 4134 // comment section continuation. 4135 const FormatToken *PreviousToken = nullptr; 4136 for (const UnwrappedLineNode &Node : Line.Tokens) { 4137 if (PreviousToken && PreviousToken->is(tok::l_brace) && 4138 isLineComment(*Node.Tok)) { 4139 MinColumnToken = PreviousToken; 4140 break; 4141 } 4142 PreviousToken = Node.Tok; 4143 4144 // Grab the last newline preceding a token in this unwrapped line. 4145 if (Node.Tok->NewlinesBefore > 0) 4146 MinColumnToken = Node.Tok; 4147 } 4148 if (PreviousToken && PreviousToken->is(tok::l_brace)) 4149 MinColumnToken = PreviousToken; 4150 4151 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok, 4152 MinColumnToken); 4153 } 4154 4155 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { 4156 bool JustComments = Line->Tokens.empty(); 4157 for (FormatToken *Tok : CommentsBeforeNextToken) { 4158 // Line comments that belong to the same line comment section are put on the 4159 // same line since later we might want to reflow content between them. 4160 // Additional fine-grained breaking of line comment sections is controlled 4161 // by the class BreakableLineCommentSection in case it is desirable to keep 4162 // several line comment sections in the same unwrapped line. 4163 // 4164 // FIXME: Consider putting separate line comment sections as children to the 4165 // unwrapped line instead. 4166 Tok->ContinuesLineCommentSection = 4167 continuesLineCommentSection(*Tok, *Line, CommentPragmasRegex); 4168 if (isOnNewLine(*Tok) && JustComments && !Tok->ContinuesLineCommentSection) 4169 addUnwrappedLine(); 4170 pushToken(Tok); 4171 } 4172 if (NewlineBeforeNext && JustComments) 4173 addUnwrappedLine(); 4174 CommentsBeforeNextToken.clear(); 4175 } 4176 4177 void UnwrappedLineParser::nextToken(int LevelDifference) { 4178 if (eof()) 4179 return; 4180 flushComments(isOnNewLine(*FormatTok)); 4181 pushToken(FormatTok); 4182 FormatToken *Previous = FormatTok; 4183 if (!Style.isJavaScript()) 4184 readToken(LevelDifference); 4185 else 4186 readTokenWithJavaScriptASI(); 4187 FormatTok->Previous = Previous; 4188 if (Style.isVerilog()) { 4189 // Blocks in Verilog can have `begin` and `end` instead of braces. For 4190 // keywords like `begin`, we can't treat them the same as left braces 4191 // because some contexts require one of them. For example structs use 4192 // braces and if blocks use keywords, and a left brace can occur in an if 4193 // statement, but it is not a block. For keywords like `end`, we simply 4194 // treat them the same as right braces. 4195 if (Keywords.isVerilogEnd(*FormatTok)) 4196 FormatTok->Tok.setKind(tok::r_brace); 4197 } 4198 } 4199 4200 void UnwrappedLineParser::distributeComments( 4201 const SmallVectorImpl<FormatToken *> &Comments, 4202 const FormatToken *NextTok) { 4203 // Whether or not a line comment token continues a line is controlled by 4204 // the method continuesLineCommentSection, with the following caveat: 4205 // 4206 // Define a trail of Comments to be a nonempty proper postfix of Comments such 4207 // that each comment line from the trail is aligned with the next token, if 4208 // the next token exists. If a trail exists, the beginning of the maximal 4209 // trail is marked as a start of a new comment section. 4210 // 4211 // For example in this code: 4212 // 4213 // int a; // line about a 4214 // // line 1 about b 4215 // // line 2 about b 4216 // int b; 4217 // 4218 // the two lines about b form a maximal trail, so there are two sections, the 4219 // first one consisting of the single comment "// line about a" and the 4220 // second one consisting of the next two comments. 4221 if (Comments.empty()) 4222 return; 4223 bool ShouldPushCommentsInCurrentLine = true; 4224 bool HasTrailAlignedWithNextToken = false; 4225 unsigned StartOfTrailAlignedWithNextToken = 0; 4226 if (NextTok) { 4227 // We are skipping the first element intentionally. 4228 for (unsigned i = Comments.size() - 1; i > 0; --i) { 4229 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) { 4230 HasTrailAlignedWithNextToken = true; 4231 StartOfTrailAlignedWithNextToken = i; 4232 } 4233 } 4234 } 4235 for (unsigned i = 0, e = Comments.size(); i < e; ++i) { 4236 FormatToken *FormatTok = Comments[i]; 4237 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) { 4238 FormatTok->ContinuesLineCommentSection = false; 4239 } else { 4240 FormatTok->ContinuesLineCommentSection = 4241 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex); 4242 } 4243 if (!FormatTok->ContinuesLineCommentSection && 4244 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) { 4245 ShouldPushCommentsInCurrentLine = false; 4246 } 4247 if (ShouldPushCommentsInCurrentLine) 4248 pushToken(FormatTok); 4249 else 4250 CommentsBeforeNextToken.push_back(FormatTok); 4251 } 4252 } 4253 4254 void UnwrappedLineParser::readToken(int LevelDifference) { 4255 SmallVector<FormatToken *, 1> Comments; 4256 bool PreviousWasComment = false; 4257 bool FirstNonCommentOnLine = false; 4258 do { 4259 FormatTok = Tokens->getNextToken(); 4260 assert(FormatTok); 4261 while (FormatTok->getType() == TT_ConflictStart || 4262 FormatTok->getType() == TT_ConflictEnd || 4263 FormatTok->getType() == TT_ConflictAlternative) { 4264 if (FormatTok->getType() == TT_ConflictStart) 4265 conditionalCompilationStart(/*Unreachable=*/false); 4266 else if (FormatTok->getType() == TT_ConflictAlternative) 4267 conditionalCompilationAlternative(); 4268 else if (FormatTok->getType() == TT_ConflictEnd) 4269 conditionalCompilationEnd(); 4270 FormatTok = Tokens->getNextToken(); 4271 FormatTok->MustBreakBefore = true; 4272 } 4273 4274 auto IsFirstNonCommentOnLine = [](bool FirstNonCommentOnLine, 4275 const FormatToken &Tok, 4276 bool PreviousWasComment) { 4277 auto IsFirstOnLine = [](const FormatToken &Tok) { 4278 return Tok.HasUnescapedNewline || Tok.IsFirst; 4279 }; 4280 4281 // Consider preprocessor directives preceded by block comments as first 4282 // on line. 4283 if (PreviousWasComment) 4284 return FirstNonCommentOnLine || IsFirstOnLine(Tok); 4285 return IsFirstOnLine(Tok); 4286 }; 4287 4288 FirstNonCommentOnLine = IsFirstNonCommentOnLine( 4289 FirstNonCommentOnLine, *FormatTok, PreviousWasComment); 4290 PreviousWasComment = FormatTok->is(tok::comment); 4291 4292 while (!Line->InPPDirective && FormatTok->is(tok::hash) && 4293 (!Style.isVerilog() || 4294 Keywords.isVerilogPPDirective(*Tokens->peekNextToken())) && 4295 FirstNonCommentOnLine) { 4296 distributeComments(Comments, FormatTok); 4297 Comments.clear(); 4298 // If there is an unfinished unwrapped line, we flush the preprocessor 4299 // directives only after that unwrapped line was finished later. 4300 bool SwitchToPreprocessorLines = !Line->Tokens.empty(); 4301 ScopedLineState BlockState(*this, SwitchToPreprocessorLines); 4302 assert((LevelDifference >= 0 || 4303 static_cast<unsigned>(-LevelDifference) <= Line->Level) && 4304 "LevelDifference makes Line->Level negative"); 4305 Line->Level += LevelDifference; 4306 // Comments stored before the preprocessor directive need to be output 4307 // before the preprocessor directive, at the same level as the 4308 // preprocessor directive, as we consider them to apply to the directive. 4309 if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash && 4310 PPBranchLevel > 0) { 4311 Line->Level += PPBranchLevel; 4312 } 4313 flushComments(isOnNewLine(*FormatTok)); 4314 parsePPDirective(); 4315 PreviousWasComment = FormatTok->is(tok::comment); 4316 FirstNonCommentOnLine = IsFirstNonCommentOnLine( 4317 FirstNonCommentOnLine, *FormatTok, PreviousWasComment); 4318 } 4319 4320 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) && 4321 !Line->InPPDirective) { 4322 continue; 4323 } 4324 4325 if (!FormatTok->is(tok::comment)) { 4326 distributeComments(Comments, FormatTok); 4327 Comments.clear(); 4328 return; 4329 } 4330 4331 Comments.push_back(FormatTok); 4332 } while (!eof()); 4333 4334 distributeComments(Comments, nullptr); 4335 Comments.clear(); 4336 } 4337 4338 void UnwrappedLineParser::pushToken(FormatToken *Tok) { 4339 Line->Tokens.push_back(UnwrappedLineNode(Tok)); 4340 if (MustBreakBeforeNextToken) { 4341 Line->Tokens.back().Tok->MustBreakBefore = true; 4342 MustBreakBeforeNextToken = false; 4343 } 4344 } 4345 4346 } // end namespace format 4347 } // end namespace clang 4348