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