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