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