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