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