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(); 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_elif: 755 parsePPElIf(); 756 break; 757 case tok::pp_endif: 758 parsePPEndIf(); 759 break; 760 default: 761 parsePPUnknown(); 762 break; 763 } 764 } 765 766 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { 767 size_t Line = CurrentLines->size(); 768 if (CurrentLines == &PreprocessorDirectives) 769 Line += Lines.size(); 770 771 if (Unreachable || 772 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable)) 773 PPStack.push_back({PP_Unreachable, Line}); 774 else 775 PPStack.push_back({PP_Conditional, Line}); 776 } 777 778 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { 779 ++PPBranchLevel; 780 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); 781 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { 782 PPLevelBranchIndex.push_back(0); 783 PPLevelBranchCount.push_back(0); 784 } 785 PPChainBranchIndex.push(0); 786 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; 787 conditionalCompilationCondition(Unreachable || Skip); 788 } 789 790 void UnwrappedLineParser::conditionalCompilationAlternative() { 791 if (!PPStack.empty()) 792 PPStack.pop_back(); 793 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 794 if (!PPChainBranchIndex.empty()) 795 ++PPChainBranchIndex.top(); 796 conditionalCompilationCondition( 797 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && 798 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()); 799 } 800 801 void UnwrappedLineParser::conditionalCompilationEnd() { 802 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 803 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { 804 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { 805 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; 806 } 807 } 808 // Guard against #endif's without #if. 809 if (PPBranchLevel > -1) 810 --PPBranchLevel; 811 if (!PPChainBranchIndex.empty()) 812 PPChainBranchIndex.pop(); 813 if (!PPStack.empty()) 814 PPStack.pop_back(); 815 } 816 817 void UnwrappedLineParser::parsePPIf(bool IfDef) { 818 bool IfNDef = FormatTok->is(tok::pp_ifndef); 819 nextToken(); 820 bool Unreachable = false; 821 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0")) 822 Unreachable = true; 823 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG") 824 Unreachable = true; 825 conditionalCompilationStart(Unreachable); 826 FormatToken *IfCondition = FormatTok; 827 // If there's a #ifndef on the first line, and the only lines before it are 828 // comments, it could be an include guard. 829 bool MaybeIncludeGuard = IfNDef; 830 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) 831 for (auto &Line : Lines) { 832 if (!Line.Tokens.front().Tok->is(tok::comment)) { 833 MaybeIncludeGuard = false; 834 IncludeGuard = IG_Rejected; 835 break; 836 } 837 } 838 --PPBranchLevel; 839 parsePPUnknown(); 840 ++PPBranchLevel; 841 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) { 842 IncludeGuard = IG_IfNdefed; 843 IncludeGuardToken = IfCondition; 844 } 845 } 846 847 void UnwrappedLineParser::parsePPElse() { 848 // If a potential include guard has an #else, it's not an include guard. 849 if (IncludeGuard == IG_Defined && PPBranchLevel == 0) 850 IncludeGuard = IG_Rejected; 851 conditionalCompilationAlternative(); 852 if (PPBranchLevel > -1) 853 --PPBranchLevel; 854 parsePPUnknown(); 855 ++PPBranchLevel; 856 } 857 858 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } 859 860 void UnwrappedLineParser::parsePPEndIf() { 861 conditionalCompilationEnd(); 862 parsePPUnknown(); 863 // If the #endif of a potential include guard is the last thing in the file, 864 // then we found an include guard. 865 unsigned TokenPosition = Tokens->getPosition(); 866 FormatToken *PeekNext = AllTokens[TokenPosition]; 867 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 && 868 PeekNext->is(tok::eof) && 869 Style.IndentPPDirectives != FormatStyle::PPDIS_None) 870 IncludeGuard = IG_Found; 871 } 872 873 void UnwrappedLineParser::parsePPDefine() { 874 nextToken(); 875 876 if (!FormatTok->Tok.getIdentifierInfo()) { 877 IncludeGuard = IG_Rejected; 878 IncludeGuardToken = nullptr; 879 parsePPUnknown(); 880 return; 881 } 882 883 if (IncludeGuard == IG_IfNdefed && 884 IncludeGuardToken->TokenText == FormatTok->TokenText) { 885 IncludeGuard = IG_Defined; 886 IncludeGuardToken = nullptr; 887 for (auto &Line : Lines) { 888 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) { 889 IncludeGuard = IG_Rejected; 890 break; 891 } 892 } 893 } 894 895 nextToken(); 896 if (FormatTok->Tok.getKind() == tok::l_paren && 897 FormatTok->WhitespaceRange.getBegin() == 898 FormatTok->WhitespaceRange.getEnd()) { 899 parseParens(); 900 } 901 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None) 902 Line->Level += PPBranchLevel + 1; 903 addUnwrappedLine(); 904 ++Line->Level; 905 906 // Errors during a preprocessor directive can only affect the layout of the 907 // preprocessor directive, and thus we ignore them. An alternative approach 908 // would be to use the same approach we use on the file level (no 909 // re-indentation if there was a structural error) within the macro 910 // definition. 911 parseFile(); 912 } 913 914 void UnwrappedLineParser::parsePPUnknown() { 915 do { 916 nextToken(); 917 } while (!eof()); 918 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None) 919 Line->Level += PPBranchLevel + 1; 920 addUnwrappedLine(); 921 } 922 923 // Here we exclude certain tokens that are not usually the first token in an 924 // unwrapped line. This is used in attempt to distinguish macro calls without 925 // trailing semicolons from other constructs split to several lines. 926 static bool tokenCanStartNewLine(const FormatToken &Tok) { 927 // Semicolon can be a null-statement, l_square can be a start of a macro or 928 // a C++11 attribute, but this doesn't seem to be common. 929 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && 930 Tok.isNot(TT_AttributeSquare) && 931 // Tokens that can only be used as binary operators and a part of 932 // overloaded operator names. 933 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && 934 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && 935 Tok.isNot(tok::less) && Tok.isNot(tok::greater) && 936 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && 937 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && 938 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && 939 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && 940 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && 941 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && 942 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && 943 Tok.isNot(tok::lesslessequal) && 944 // Colon is used in labels, base class lists, initializer lists, 945 // range-based for loops, ternary operator, but should never be the 946 // first token in an unwrapped line. 947 Tok.isNot(tok::colon) && 948 // 'noexcept' is a trailing annotation. 949 Tok.isNot(tok::kw_noexcept); 950 } 951 952 static bool mustBeJSIdent(const AdditionalKeywords &Keywords, 953 const FormatToken *FormatTok) { 954 // FIXME: This returns true for C/C++ keywords like 'struct'. 955 return FormatTok->is(tok::identifier) && 956 (FormatTok->Tok.getIdentifierInfo() == nullptr || 957 !FormatTok->isOneOf( 958 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async, 959 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally, 960 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is, 961 Keywords.kw_let, Keywords.kw_var, tok::kw_const, 962 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements, 963 Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws, 964 Keywords.kw_from)); 965 } 966 967 static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords, 968 const FormatToken *FormatTok) { 969 return FormatTok->Tok.isLiteral() || 970 FormatTok->isOneOf(tok::kw_true, tok::kw_false) || 971 mustBeJSIdent(Keywords, FormatTok); 972 } 973 974 // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement 975 // when encountered after a value (see mustBeJSIdentOrValue). 976 static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords, 977 const FormatToken *FormatTok) { 978 return FormatTok->isOneOf( 979 tok::kw_return, Keywords.kw_yield, 980 // conditionals 981 tok::kw_if, tok::kw_else, 982 // loops 983 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break, 984 // switch/case 985 tok::kw_switch, tok::kw_case, 986 // exceptions 987 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally, 988 // declaration 989 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let, 990 Keywords.kw_async, Keywords.kw_function, 991 // import/export 992 Keywords.kw_import, tok::kw_export); 993 } 994 995 // readTokenWithJavaScriptASI reads the next token and terminates the current 996 // line if JavaScript Automatic Semicolon Insertion must 997 // happen between the current token and the next token. 998 // 999 // This method is conservative - it cannot cover all edge cases of JavaScript, 1000 // but only aims to correctly handle certain well known cases. It *must not* 1001 // return true in speculative cases. 1002 void UnwrappedLineParser::readTokenWithJavaScriptASI() { 1003 FormatToken *Previous = FormatTok; 1004 readToken(); 1005 FormatToken *Next = FormatTok; 1006 1007 bool IsOnSameLine = 1008 CommentsBeforeNextToken.empty() 1009 ? Next->NewlinesBefore == 0 1010 : CommentsBeforeNextToken.front()->NewlinesBefore == 0; 1011 if (IsOnSameLine) 1012 return; 1013 1014 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous); 1015 bool PreviousStartsTemplateExpr = 1016 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${"); 1017 if (PreviousMustBeValue || Previous->is(tok::r_paren)) { 1018 // If the line contains an '@' sign, the previous token might be an 1019 // annotation, which can precede another identifier/value. 1020 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(), 1021 [](UnwrappedLineNode &LineNode) { 1022 return LineNode.Tok->is(tok::at); 1023 }) != Line->Tokens.end(); 1024 if (HasAt) 1025 return; 1026 } 1027 if (Next->is(tok::exclaim) && PreviousMustBeValue) 1028 return addUnwrappedLine(); 1029 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next); 1030 bool NextEndsTemplateExpr = 1031 Next->is(TT_TemplateString) && Next->TokenText.startswith("}"); 1032 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr && 1033 (PreviousMustBeValue || 1034 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus, 1035 tok::minusminus))) 1036 return addUnwrappedLine(); 1037 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) && 1038 isJSDeclOrStmt(Keywords, Next)) 1039 return addUnwrappedLine(); 1040 } 1041 1042 void UnwrappedLineParser::parseStructuralElement() { 1043 assert(!FormatTok->is(tok::l_brace)); 1044 if (Style.Language == FormatStyle::LK_TableGen && 1045 FormatTok->is(tok::pp_include)) { 1046 nextToken(); 1047 if (FormatTok->is(tok::string_literal)) 1048 nextToken(); 1049 addUnwrappedLine(); 1050 return; 1051 } 1052 switch (FormatTok->Tok.getKind()) { 1053 case tok::kw_asm: 1054 nextToken(); 1055 if (FormatTok->is(tok::l_brace)) { 1056 FormatTok->setType(TT_InlineASMBrace); 1057 nextToken(); 1058 while (FormatTok && FormatTok->isNot(tok::eof)) { 1059 if (FormatTok->is(tok::r_brace)) { 1060 FormatTok->setType(TT_InlineASMBrace); 1061 nextToken(); 1062 addUnwrappedLine(); 1063 break; 1064 } 1065 FormatTok->Finalized = true; 1066 nextToken(); 1067 } 1068 } 1069 break; 1070 case tok::kw_namespace: 1071 parseNamespace(); 1072 return; 1073 case tok::kw_public: 1074 case tok::kw_protected: 1075 case tok::kw_private: 1076 if (Style.Language == FormatStyle::LK_Java || 1077 Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) 1078 nextToken(); 1079 else 1080 parseAccessSpecifier(); 1081 return; 1082 case tok::kw_if: 1083 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1084 // field/method declaration. 1085 break; 1086 parseIfThenElse(); 1087 return; 1088 case tok::kw_for: 1089 case tok::kw_while: 1090 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1091 // field/method declaration. 1092 break; 1093 parseForOrWhileLoop(); 1094 return; 1095 case tok::kw_do: 1096 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1097 // field/method declaration. 1098 break; 1099 parseDoWhile(); 1100 return; 1101 case tok::kw_switch: 1102 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1103 // 'switch: string' field declaration. 1104 break; 1105 parseSwitch(); 1106 return; 1107 case tok::kw_default: 1108 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1109 // 'default: string' field declaration. 1110 break; 1111 nextToken(); 1112 if (FormatTok->is(tok::colon)) { 1113 parseLabel(); 1114 return; 1115 } 1116 // e.g. "default void f() {}" in a Java interface. 1117 break; 1118 case tok::kw_case: 1119 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1120 // 'case: string' field declaration. 1121 break; 1122 parseCaseLabel(); 1123 return; 1124 case tok::kw_try: 1125 case tok::kw___try: 1126 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1127 // field/method declaration. 1128 break; 1129 parseTryCatch(); 1130 return; 1131 case tok::kw_extern: 1132 nextToken(); 1133 if (FormatTok->Tok.is(tok::string_literal)) { 1134 nextToken(); 1135 if (FormatTok->Tok.is(tok::l_brace)) { 1136 if (!Style.IndentExternBlock) { 1137 if (Style.BraceWrapping.AfterExternBlock) { 1138 addUnwrappedLine(); 1139 } 1140 unsigned AddLevels = Style.BraceWrapping.AfterExternBlock ? 1u : 0u; 1141 parseBlock(/*MustBeDeclaration=*/true, AddLevels); 1142 } else { 1143 unsigned AddLevels = 1144 Style.IndentExternBlock == FormatStyle::IEBS_Indent ? 1u : 0u; 1145 parseBlock(/*MustBeDeclaration=*/true, AddLevels); 1146 } 1147 addUnwrappedLine(); 1148 return; 1149 } 1150 } 1151 break; 1152 case tok::kw_export: 1153 if (Style.Language == FormatStyle::LK_JavaScript) { 1154 parseJavaScriptEs6ImportExport(); 1155 return; 1156 } 1157 if (!Style.isCpp()) 1158 break; 1159 // Handle C++ "(inline|export) namespace". 1160 LLVM_FALLTHROUGH; 1161 case tok::kw_inline: 1162 nextToken(); 1163 if (FormatTok->Tok.is(tok::kw_namespace)) { 1164 parseNamespace(); 1165 return; 1166 } 1167 break; 1168 case tok::identifier: 1169 if (FormatTok->is(TT_ForEachMacro)) { 1170 parseForOrWhileLoop(); 1171 return; 1172 } 1173 if (FormatTok->is(TT_MacroBlockBegin)) { 1174 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, 1175 /*MunchSemi=*/false); 1176 return; 1177 } 1178 if (FormatTok->is(Keywords.kw_import)) { 1179 if (Style.Language == FormatStyle::LK_JavaScript) { 1180 parseJavaScriptEs6ImportExport(); 1181 return; 1182 } 1183 if (Style.Language == FormatStyle::LK_Proto) { 1184 nextToken(); 1185 if (FormatTok->is(tok::kw_public)) 1186 nextToken(); 1187 if (!FormatTok->is(tok::string_literal)) 1188 return; 1189 nextToken(); 1190 if (FormatTok->is(tok::semi)) 1191 nextToken(); 1192 addUnwrappedLine(); 1193 return; 1194 } 1195 } 1196 if (Style.isCpp() && 1197 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals, 1198 Keywords.kw_slots, Keywords.kw_qslots)) { 1199 nextToken(); 1200 if (FormatTok->is(tok::colon)) { 1201 nextToken(); 1202 addUnwrappedLine(); 1203 return; 1204 } 1205 } 1206 if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) { 1207 parseStatementMacro(); 1208 return; 1209 } 1210 if (Style.isCpp() && FormatTok->is(TT_NamespaceMacro)) { 1211 parseNamespace(); 1212 return; 1213 } 1214 // In all other cases, parse the declaration. 1215 break; 1216 default: 1217 break; 1218 } 1219 do { 1220 const FormatToken *Previous = FormatTok->Previous; 1221 switch (FormatTok->Tok.getKind()) { 1222 case tok::at: 1223 nextToken(); 1224 if (FormatTok->Tok.is(tok::l_brace)) { 1225 nextToken(); 1226 parseBracedList(); 1227 break; 1228 } else if (Style.Language == FormatStyle::LK_Java && 1229 FormatTok->is(Keywords.kw_interface)) { 1230 nextToken(); 1231 break; 1232 } 1233 switch (FormatTok->Tok.getObjCKeywordID()) { 1234 case tok::objc_public: 1235 case tok::objc_protected: 1236 case tok::objc_package: 1237 case tok::objc_private: 1238 return parseAccessSpecifier(); 1239 case tok::objc_interface: 1240 case tok::objc_implementation: 1241 return parseObjCInterfaceOrImplementation(); 1242 case tok::objc_protocol: 1243 if (parseObjCProtocol()) 1244 return; 1245 break; 1246 case tok::objc_end: 1247 return; // Handled by the caller. 1248 case tok::objc_optional: 1249 case tok::objc_required: 1250 nextToken(); 1251 addUnwrappedLine(); 1252 return; 1253 case tok::objc_autoreleasepool: 1254 nextToken(); 1255 if (FormatTok->Tok.is(tok::l_brace)) { 1256 if (Style.BraceWrapping.AfterControlStatement == 1257 FormatStyle::BWACS_Always) 1258 addUnwrappedLine(); 1259 parseBlock(/*MustBeDeclaration=*/false); 1260 } 1261 addUnwrappedLine(); 1262 return; 1263 case tok::objc_synchronized: 1264 nextToken(); 1265 if (FormatTok->Tok.is(tok::l_paren)) 1266 // Skip synchronization object 1267 parseParens(); 1268 if (FormatTok->Tok.is(tok::l_brace)) { 1269 if (Style.BraceWrapping.AfterControlStatement == 1270 FormatStyle::BWACS_Always) 1271 addUnwrappedLine(); 1272 parseBlock(/*MustBeDeclaration=*/false); 1273 } 1274 addUnwrappedLine(); 1275 return; 1276 case tok::objc_try: 1277 // This branch isn't strictly necessary (the kw_try case below would 1278 // do this too after the tok::at is parsed above). But be explicit. 1279 parseTryCatch(); 1280 return; 1281 default: 1282 break; 1283 } 1284 break; 1285 case tok::kw_concept: 1286 parseConcept(); 1287 break; 1288 case tok::kw_requires: 1289 parseRequires(); 1290 break; 1291 case tok::kw_enum: 1292 // Ignore if this is part of "template <enum ...". 1293 if (Previous && Previous->is(tok::less)) { 1294 nextToken(); 1295 break; 1296 } 1297 1298 // parseEnum falls through and does not yet add an unwrapped line as an 1299 // enum definition can start a structural element. 1300 if (!parseEnum()) 1301 break; 1302 // This only applies for C++. 1303 if (!Style.isCpp()) { 1304 addUnwrappedLine(); 1305 return; 1306 } 1307 break; 1308 case tok::kw_typedef: 1309 nextToken(); 1310 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS, 1311 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS, 1312 Keywords.kw_CF_CLOSED_ENUM, 1313 Keywords.kw_NS_CLOSED_ENUM)) 1314 parseEnum(); 1315 break; 1316 case tok::kw_struct: 1317 case tok::kw_union: 1318 case tok::kw_class: 1319 // parseRecord falls through and does not yet add an unwrapped line as a 1320 // record declaration or definition can start a structural element. 1321 parseRecord(); 1322 // This does not apply for Java, JavaScript and C#. 1323 if (Style.Language == FormatStyle::LK_Java || 1324 Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) { 1325 if (FormatTok->is(tok::semi)) 1326 nextToken(); 1327 addUnwrappedLine(); 1328 return; 1329 } 1330 break; 1331 case tok::period: 1332 nextToken(); 1333 // In Java, classes have an implicit static member "class". 1334 if (Style.Language == FormatStyle::LK_Java && FormatTok && 1335 FormatTok->is(tok::kw_class)) 1336 nextToken(); 1337 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok && 1338 FormatTok->Tok.getIdentifierInfo()) 1339 // JavaScript only has pseudo keywords, all keywords are allowed to 1340 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6 1341 nextToken(); 1342 break; 1343 case tok::semi: 1344 nextToken(); 1345 addUnwrappedLine(); 1346 return; 1347 case tok::r_brace: 1348 addUnwrappedLine(); 1349 return; 1350 case tok::l_paren: 1351 parseParens(); 1352 break; 1353 case tok::kw_operator: 1354 nextToken(); 1355 if (FormatTok->isBinaryOperator()) 1356 nextToken(); 1357 break; 1358 case tok::caret: 1359 nextToken(); 1360 if (FormatTok->Tok.isAnyIdentifier() || 1361 FormatTok->isSimpleTypeSpecifier()) 1362 nextToken(); 1363 if (FormatTok->is(tok::l_paren)) 1364 parseParens(); 1365 if (FormatTok->is(tok::l_brace)) 1366 parseChildBlock(); 1367 break; 1368 case tok::l_brace: 1369 if (!tryToParsePropertyAccessor() && !tryToParseBracedList()) { 1370 // A block outside of parentheses must be the last part of a 1371 // structural element. 1372 // FIXME: Figure out cases where this is not true, and add projections 1373 // for them (the one we know is missing are lambdas). 1374 if (Style.BraceWrapping.AfterFunction) 1375 addUnwrappedLine(); 1376 FormatTok->setType(TT_FunctionLBrace); 1377 parseBlock(/*MustBeDeclaration=*/false); 1378 addUnwrappedLine(); 1379 return; 1380 } 1381 // Otherwise this was a braced init list, and the structural 1382 // element continues. 1383 break; 1384 case tok::kw_try: 1385 if (Style.Language == FormatStyle::LK_JavaScript && 1386 Line->MustBeDeclaration) { 1387 // field/method declaration. 1388 nextToken(); 1389 break; 1390 } 1391 // We arrive here when parsing function-try blocks. 1392 if (Style.BraceWrapping.AfterFunction) 1393 addUnwrappedLine(); 1394 parseTryCatch(); 1395 return; 1396 case tok::identifier: { 1397 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where) && 1398 Line->MustBeDeclaration) { 1399 addUnwrappedLine(); 1400 parseCSharpGenericTypeConstraint(); 1401 break; 1402 } 1403 if (FormatTok->is(TT_MacroBlockEnd)) { 1404 addUnwrappedLine(); 1405 return; 1406 } 1407 1408 // Function declarations (as opposed to function expressions) are parsed 1409 // on their own unwrapped line by continuing this loop. Function 1410 // expressions (functions that are not on their own line) must not create 1411 // a new unwrapped line, so they are special cased below. 1412 size_t TokenCount = Line->Tokens.size(); 1413 if (Style.Language == FormatStyle::LK_JavaScript && 1414 FormatTok->is(Keywords.kw_function) && 1415 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is( 1416 Keywords.kw_async)))) { 1417 tryToParseJSFunction(); 1418 break; 1419 } 1420 if ((Style.Language == FormatStyle::LK_JavaScript || 1421 Style.Language == FormatStyle::LK_Java) && 1422 FormatTok->is(Keywords.kw_interface)) { 1423 if (Style.Language == FormatStyle::LK_JavaScript) { 1424 // In JavaScript/TypeScript, "interface" can be used as a standalone 1425 // identifier, e.g. in `var interface = 1;`. If "interface" is 1426 // followed by another identifier, it is very like to be an actual 1427 // interface declaration. 1428 unsigned StoredPosition = Tokens->getPosition(); 1429 FormatToken *Next = Tokens->getNextToken(); 1430 FormatTok = Tokens->setPosition(StoredPosition); 1431 if (Next && !mustBeJSIdent(Keywords, Next)) { 1432 nextToken(); 1433 break; 1434 } 1435 } 1436 parseRecord(); 1437 addUnwrappedLine(); 1438 return; 1439 } 1440 1441 if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) { 1442 parseStatementMacro(); 1443 return; 1444 } 1445 1446 // See if the following token should start a new unwrapped line. 1447 StringRef Text = FormatTok->TokenText; 1448 nextToken(); 1449 1450 // JS doesn't have macros, and within classes colons indicate fields, not 1451 // labels. 1452 if (Style.Language == FormatStyle::LK_JavaScript) 1453 break; 1454 1455 TokenCount = Line->Tokens.size(); 1456 if (TokenCount == 1 || 1457 (TokenCount == 2 && Line->Tokens.front().Tok->is(tok::comment))) { 1458 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) { 1459 Line->Tokens.begin()->Tok->MustBreakBefore = true; 1460 parseLabel(!Style.IndentGotoLabels); 1461 return; 1462 } 1463 // Recognize function-like macro usages without trailing semicolon as 1464 // well as free-standing macros like Q_OBJECT. 1465 bool FunctionLike = FormatTok->is(tok::l_paren); 1466 if (FunctionLike) 1467 parseParens(); 1468 1469 bool FollowedByNewline = 1470 CommentsBeforeNextToken.empty() 1471 ? FormatTok->NewlinesBefore > 0 1472 : CommentsBeforeNextToken.front()->NewlinesBefore > 0; 1473 1474 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && 1475 tokenCanStartNewLine(*FormatTok) && Text == Text.upper()) { 1476 addUnwrappedLine(); 1477 return; 1478 } 1479 } 1480 break; 1481 } 1482 case tok::equal: 1483 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType 1484 // TT_JsFatArrow. The always start an expression or a child block if 1485 // followed by a curly. 1486 if (FormatTok->is(TT_JsFatArrow)) { 1487 nextToken(); 1488 if (FormatTok->is(tok::l_brace)) { 1489 // C# may break after => if the next character is a newline. 1490 if (Style.isCSharp() && Style.BraceWrapping.AfterFunction == true) { 1491 // calling `addUnwrappedLine()` here causes odd parsing errors. 1492 FormatTok->MustBreakBefore = true; 1493 } 1494 parseChildBlock(); 1495 } 1496 break; 1497 } 1498 1499 nextToken(); 1500 if (FormatTok->Tok.is(tok::l_brace)) { 1501 // Block kind should probably be set to BK_BracedInit for any language. 1502 // C# needs this change to ensure that array initialisers and object 1503 // initialisers are indented the same way. 1504 if (Style.isCSharp()) 1505 FormatTok->setBlockKind(BK_BracedInit); 1506 nextToken(); 1507 parseBracedList(); 1508 } else if (Style.Language == FormatStyle::LK_Proto && 1509 FormatTok->Tok.is(tok::less)) { 1510 nextToken(); 1511 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 1512 /*ClosingBraceKind=*/tok::greater); 1513 } 1514 break; 1515 case tok::l_square: 1516 parseSquare(); 1517 break; 1518 case tok::kw_new: 1519 parseNew(); 1520 break; 1521 default: 1522 nextToken(); 1523 break; 1524 } 1525 } while (!eof()); 1526 } 1527 1528 bool UnwrappedLineParser::tryToParsePropertyAccessor() { 1529 assert(FormatTok->is(tok::l_brace)); 1530 if (!Style.isCSharp()) 1531 return false; 1532 // See if it's a property accessor. 1533 if (FormatTok->Previous->isNot(tok::identifier)) 1534 return false; 1535 1536 // See if we are inside a property accessor. 1537 // 1538 // Record the current tokenPosition so that we can advance and 1539 // reset the current token. `Next` is not set yet so we need 1540 // another way to advance along the token stream. 1541 unsigned int StoredPosition = Tokens->getPosition(); 1542 FormatToken *Tok = Tokens->getNextToken(); 1543 1544 // A trivial property accessor is of the form: 1545 // { [ACCESS_SPECIFIER] [get]; [ACCESS_SPECIFIER] [set] } 1546 // Track these as they do not require line breaks to be introduced. 1547 bool HasGetOrSet = false; 1548 bool IsTrivialPropertyAccessor = true; 1549 while (!eof()) { 1550 if (Tok->isOneOf(tok::semi, tok::kw_public, tok::kw_private, 1551 tok::kw_protected, Keywords.kw_internal, Keywords.kw_get, 1552 Keywords.kw_set)) { 1553 if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_set)) 1554 HasGetOrSet = true; 1555 Tok = Tokens->getNextToken(); 1556 continue; 1557 } 1558 if (Tok->isNot(tok::r_brace)) 1559 IsTrivialPropertyAccessor = false; 1560 break; 1561 } 1562 1563 if (!HasGetOrSet) { 1564 Tokens->setPosition(StoredPosition); 1565 return false; 1566 } 1567 1568 // Try to parse the property accessor: 1569 // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties 1570 Tokens->setPosition(StoredPosition); 1571 if (!IsTrivialPropertyAccessor && Style.BraceWrapping.AfterFunction == true) 1572 addUnwrappedLine(); 1573 nextToken(); 1574 do { 1575 switch (FormatTok->Tok.getKind()) { 1576 case tok::r_brace: 1577 nextToken(); 1578 if (FormatTok->is(tok::equal)) { 1579 while (!eof() && FormatTok->isNot(tok::semi)) 1580 nextToken(); 1581 nextToken(); 1582 } 1583 addUnwrappedLine(); 1584 return true; 1585 case tok::l_brace: 1586 ++Line->Level; 1587 parseBlock(/*MustBeDeclaration=*/true); 1588 addUnwrappedLine(); 1589 --Line->Level; 1590 break; 1591 case tok::equal: 1592 if (FormatTok->is(TT_JsFatArrow)) { 1593 ++Line->Level; 1594 do { 1595 nextToken(); 1596 } while (!eof() && FormatTok->isNot(tok::semi)); 1597 nextToken(); 1598 addUnwrappedLine(); 1599 --Line->Level; 1600 break; 1601 } 1602 nextToken(); 1603 break; 1604 default: 1605 if (FormatTok->isOneOf(Keywords.kw_get, Keywords.kw_set) && 1606 !IsTrivialPropertyAccessor) { 1607 // Non-trivial get/set needs to be on its own line. 1608 addUnwrappedLine(); 1609 } 1610 nextToken(); 1611 } 1612 } while (!eof()); 1613 1614 // Unreachable for well-formed code (paired '{' and '}'). 1615 return true; 1616 } 1617 1618 bool UnwrappedLineParser::tryToParseLambda() { 1619 if (!Style.isCpp()) { 1620 nextToken(); 1621 return false; 1622 } 1623 assert(FormatTok->is(tok::l_square)); 1624 FormatToken &LSquare = *FormatTok; 1625 if (!tryToParseLambdaIntroducer()) 1626 return false; 1627 1628 bool SeenArrow = false; 1629 1630 while (FormatTok->isNot(tok::l_brace)) { 1631 if (FormatTok->isSimpleTypeSpecifier()) { 1632 nextToken(); 1633 continue; 1634 } 1635 switch (FormatTok->Tok.getKind()) { 1636 case tok::l_brace: 1637 break; 1638 case tok::l_paren: 1639 parseParens(); 1640 break; 1641 case tok::amp: 1642 case tok::star: 1643 case tok::kw_const: 1644 case tok::comma: 1645 case tok::less: 1646 case tok::greater: 1647 case tok::identifier: 1648 case tok::numeric_constant: 1649 case tok::coloncolon: 1650 case tok::kw_class: 1651 case tok::kw_mutable: 1652 case tok::kw_noexcept: 1653 case tok::kw_template: 1654 case tok::kw_typename: 1655 nextToken(); 1656 break; 1657 // Specialization of a template with an integer parameter can contain 1658 // arithmetic, logical, comparison and ternary operators. 1659 // 1660 // FIXME: This also accepts sequences of operators that are not in the scope 1661 // of a template argument list. 1662 // 1663 // In a C++ lambda a template type can only occur after an arrow. We use 1664 // this as an heuristic to distinguish between Objective-C expressions 1665 // followed by an `a->b` expression, such as: 1666 // ([obj func:arg] + a->b) 1667 // Otherwise the code below would parse as a lambda. 1668 // 1669 // FIXME: This heuristic is incorrect for C++20 generic lambdas with 1670 // explicit template lists: []<bool b = true && false>(U &&u){} 1671 case tok::plus: 1672 case tok::minus: 1673 case tok::exclaim: 1674 case tok::tilde: 1675 case tok::slash: 1676 case tok::percent: 1677 case tok::lessless: 1678 case tok::pipe: 1679 case tok::pipepipe: 1680 case tok::ampamp: 1681 case tok::caret: 1682 case tok::equalequal: 1683 case tok::exclaimequal: 1684 case tok::greaterequal: 1685 case tok::lessequal: 1686 case tok::question: 1687 case tok::colon: 1688 case tok::ellipsis: 1689 case tok::kw_true: 1690 case tok::kw_false: 1691 if (SeenArrow) { 1692 nextToken(); 1693 break; 1694 } 1695 return true; 1696 case tok::arrow: 1697 // This might or might not actually be a lambda arrow (this could be an 1698 // ObjC method invocation followed by a dereferencing arrow). We might 1699 // reset this back to TT_Unknown in TokenAnnotator. 1700 FormatTok->setType(TT_LambdaArrow); 1701 SeenArrow = true; 1702 nextToken(); 1703 break; 1704 default: 1705 return true; 1706 } 1707 } 1708 FormatTok->setType(TT_LambdaLBrace); 1709 LSquare.setType(TT_LambdaLSquare); 1710 parseChildBlock(); 1711 return true; 1712 } 1713 1714 bool UnwrappedLineParser::tryToParseLambdaIntroducer() { 1715 const FormatToken *Previous = FormatTok->Previous; 1716 if (Previous && 1717 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new, 1718 tok::kw_delete, tok::l_square) || 1719 FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() || 1720 Previous->isSimpleTypeSpecifier())) { 1721 nextToken(); 1722 return false; 1723 } 1724 nextToken(); 1725 if (FormatTok->is(tok::l_square)) { 1726 return false; 1727 } 1728 parseSquare(/*LambdaIntroducer=*/true); 1729 return true; 1730 } 1731 1732 void UnwrappedLineParser::tryToParseJSFunction() { 1733 assert(FormatTok->is(Keywords.kw_function) || 1734 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)); 1735 if (FormatTok->is(Keywords.kw_async)) 1736 nextToken(); 1737 // Consume "function". 1738 nextToken(); 1739 1740 // Consume * (generator function). Treat it like C++'s overloaded operators. 1741 if (FormatTok->is(tok::star)) { 1742 FormatTok->setType(TT_OverloadedOperator); 1743 nextToken(); 1744 } 1745 1746 // Consume function name. 1747 if (FormatTok->is(tok::identifier)) 1748 nextToken(); 1749 1750 if (FormatTok->isNot(tok::l_paren)) 1751 return; 1752 1753 // Parse formal parameter list. 1754 parseParens(); 1755 1756 if (FormatTok->is(tok::colon)) { 1757 // Parse a type definition. 1758 nextToken(); 1759 1760 // Eat the type declaration. For braced inline object types, balance braces, 1761 // otherwise just parse until finding an l_brace for the function body. 1762 if (FormatTok->is(tok::l_brace)) 1763 tryToParseBracedList(); 1764 else 1765 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof()) 1766 nextToken(); 1767 } 1768 1769 if (FormatTok->is(tok::semi)) 1770 return; 1771 1772 parseChildBlock(); 1773 } 1774 1775 bool UnwrappedLineParser::tryToParseBracedList() { 1776 if (FormatTok->is(BK_Unknown)) 1777 calculateBraceTypes(); 1778 assert(FormatTok->isNot(BK_Unknown)); 1779 if (FormatTok->is(BK_Block)) 1780 return false; 1781 nextToken(); 1782 parseBracedList(); 1783 return true; 1784 } 1785 1786 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons, 1787 bool IsEnum, 1788 tok::TokenKind ClosingBraceKind) { 1789 bool HasError = false; 1790 1791 // FIXME: Once we have an expression parser in the UnwrappedLineParser, 1792 // replace this by using parseAssigmentExpression() inside. 1793 do { 1794 if (Style.isCSharp()) { 1795 if (FormatTok->is(TT_JsFatArrow)) { 1796 nextToken(); 1797 // Fat arrows can be followed by simple expressions or by child blocks 1798 // in curly braces. 1799 if (FormatTok->is(tok::l_brace)) { 1800 parseChildBlock(); 1801 continue; 1802 } 1803 } 1804 } 1805 if (Style.Language == FormatStyle::LK_JavaScript) { 1806 if (FormatTok->is(Keywords.kw_function) || 1807 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) { 1808 tryToParseJSFunction(); 1809 continue; 1810 } 1811 if (FormatTok->is(TT_JsFatArrow)) { 1812 nextToken(); 1813 // Fat arrows can be followed by simple expressions or by child blocks 1814 // in curly braces. 1815 if (FormatTok->is(tok::l_brace)) { 1816 parseChildBlock(); 1817 continue; 1818 } 1819 } 1820 if (FormatTok->is(tok::l_brace)) { 1821 // Could be a method inside of a braced list `{a() { return 1; }}`. 1822 if (tryToParseBracedList()) 1823 continue; 1824 parseChildBlock(); 1825 } 1826 } 1827 if (FormatTok->Tok.getKind() == ClosingBraceKind) { 1828 if (IsEnum && !Style.AllowShortEnumsOnASingleLine) 1829 addUnwrappedLine(); 1830 nextToken(); 1831 return !HasError; 1832 } 1833 switch (FormatTok->Tok.getKind()) { 1834 case tok::caret: 1835 nextToken(); 1836 if (FormatTok->is(tok::l_brace)) { 1837 parseChildBlock(); 1838 } 1839 break; 1840 case tok::l_square: 1841 if (Style.isCSharp()) 1842 parseSquare(); 1843 else 1844 tryToParseLambda(); 1845 break; 1846 case tok::l_paren: 1847 parseParens(); 1848 // JavaScript can just have free standing methods and getters/setters in 1849 // object literals. Detect them by a "{" following ")". 1850 if (Style.Language == FormatStyle::LK_JavaScript) { 1851 if (FormatTok->is(tok::l_brace)) 1852 parseChildBlock(); 1853 break; 1854 } 1855 break; 1856 case tok::l_brace: 1857 // Assume there are no blocks inside a braced init list apart 1858 // from the ones we explicitly parse out (like lambdas). 1859 FormatTok->setBlockKind(BK_BracedInit); 1860 nextToken(); 1861 parseBracedList(); 1862 break; 1863 case tok::less: 1864 if (Style.Language == FormatStyle::LK_Proto) { 1865 nextToken(); 1866 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 1867 /*ClosingBraceKind=*/tok::greater); 1868 } else { 1869 nextToken(); 1870 } 1871 break; 1872 case tok::semi: 1873 // JavaScript (or more precisely TypeScript) can have semicolons in braced 1874 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be 1875 // used for error recovery if we have otherwise determined that this is 1876 // a braced list. 1877 if (Style.Language == FormatStyle::LK_JavaScript) { 1878 nextToken(); 1879 break; 1880 } 1881 HasError = true; 1882 if (!ContinueOnSemicolons) 1883 return !HasError; 1884 nextToken(); 1885 break; 1886 case tok::comma: 1887 nextToken(); 1888 if (IsEnum && !Style.AllowShortEnumsOnASingleLine) 1889 addUnwrappedLine(); 1890 break; 1891 default: 1892 nextToken(); 1893 break; 1894 } 1895 } while (!eof()); 1896 return false; 1897 } 1898 1899 void UnwrappedLineParser::parseParens() { 1900 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); 1901 nextToken(); 1902 do { 1903 switch (FormatTok->Tok.getKind()) { 1904 case tok::l_paren: 1905 parseParens(); 1906 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace)) 1907 parseChildBlock(); 1908 break; 1909 case tok::r_paren: 1910 nextToken(); 1911 return; 1912 case tok::r_brace: 1913 // A "}" inside parenthesis is an error if there wasn't a matching "{". 1914 return; 1915 case tok::l_square: 1916 tryToParseLambda(); 1917 break; 1918 case tok::l_brace: 1919 if (!tryToParseBracedList()) 1920 parseChildBlock(); 1921 break; 1922 case tok::at: 1923 nextToken(); 1924 if (FormatTok->Tok.is(tok::l_brace)) { 1925 nextToken(); 1926 parseBracedList(); 1927 } 1928 break; 1929 case tok::kw_class: 1930 if (Style.Language == FormatStyle::LK_JavaScript) 1931 parseRecord(/*ParseAsExpr=*/true); 1932 else 1933 nextToken(); 1934 break; 1935 case tok::identifier: 1936 if (Style.Language == FormatStyle::LK_JavaScript && 1937 (FormatTok->is(Keywords.kw_function) || 1938 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function))) 1939 tryToParseJSFunction(); 1940 else 1941 nextToken(); 1942 break; 1943 default: 1944 nextToken(); 1945 break; 1946 } 1947 } while (!eof()); 1948 } 1949 1950 void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) { 1951 if (!LambdaIntroducer) { 1952 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); 1953 if (tryToParseLambda()) 1954 return; 1955 } 1956 do { 1957 switch (FormatTok->Tok.getKind()) { 1958 case tok::l_paren: 1959 parseParens(); 1960 break; 1961 case tok::r_square: 1962 nextToken(); 1963 return; 1964 case tok::r_brace: 1965 // A "}" inside parenthesis is an error if there wasn't a matching "{". 1966 return; 1967 case tok::l_square: 1968 parseSquare(); 1969 break; 1970 case tok::l_brace: { 1971 if (!tryToParseBracedList()) 1972 parseChildBlock(); 1973 break; 1974 } 1975 case tok::at: 1976 nextToken(); 1977 if (FormatTok->Tok.is(tok::l_brace)) { 1978 nextToken(); 1979 parseBracedList(); 1980 } 1981 break; 1982 default: 1983 nextToken(); 1984 break; 1985 } 1986 } while (!eof()); 1987 } 1988 1989 void UnwrappedLineParser::parseIfThenElse() { 1990 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); 1991 nextToken(); 1992 if (FormatTok->Tok.isOneOf(tok::kw_constexpr, tok::identifier)) 1993 nextToken(); 1994 if (FormatTok->Tok.is(tok::l_paren)) 1995 parseParens(); 1996 // handle [[likely]] / [[unlikely]] 1997 if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute()) 1998 parseSquare(); 1999 bool NeedsUnwrappedLine = false; 2000 if (FormatTok->Tok.is(tok::l_brace)) { 2001 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2002 parseBlock(/*MustBeDeclaration=*/false); 2003 if (Style.BraceWrapping.BeforeElse) 2004 addUnwrappedLine(); 2005 else 2006 NeedsUnwrappedLine = true; 2007 } else { 2008 addUnwrappedLine(); 2009 ++Line->Level; 2010 parseStructuralElement(); 2011 --Line->Level; 2012 } 2013 if (FormatTok->Tok.is(tok::kw_else)) { 2014 nextToken(); 2015 // handle [[likely]] / [[unlikely]] 2016 if (FormatTok->Tok.is(tok::l_square) && tryToParseSimpleAttribute()) 2017 parseSquare(); 2018 if (FormatTok->Tok.is(tok::l_brace)) { 2019 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2020 parseBlock(/*MustBeDeclaration=*/false); 2021 addUnwrappedLine(); 2022 } else if (FormatTok->Tok.is(tok::kw_if)) { 2023 parseIfThenElse(); 2024 } else { 2025 addUnwrappedLine(); 2026 ++Line->Level; 2027 parseStructuralElement(); 2028 if (FormatTok->is(tok::eof)) 2029 addUnwrappedLine(); 2030 --Line->Level; 2031 } 2032 } else if (NeedsUnwrappedLine) { 2033 addUnwrappedLine(); 2034 } 2035 } 2036 2037 void UnwrappedLineParser::parseTryCatch() { 2038 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected"); 2039 nextToken(); 2040 bool NeedsUnwrappedLine = false; 2041 if (FormatTok->is(tok::colon)) { 2042 // We are in a function try block, what comes is an initializer list. 2043 nextToken(); 2044 2045 // In case identifiers were removed by clang-tidy, what might follow is 2046 // multiple commas in sequence - before the first identifier. 2047 while (FormatTok->is(tok::comma)) 2048 nextToken(); 2049 2050 while (FormatTok->is(tok::identifier)) { 2051 nextToken(); 2052 if (FormatTok->is(tok::l_paren)) 2053 parseParens(); 2054 if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) && 2055 FormatTok->is(tok::l_brace)) { 2056 do { 2057 nextToken(); 2058 } while (!FormatTok->is(tok::r_brace)); 2059 nextToken(); 2060 } 2061 2062 // In case identifiers were removed by clang-tidy, what might follow is 2063 // multiple commas in sequence - after the first identifier. 2064 while (FormatTok->is(tok::comma)) 2065 nextToken(); 2066 } 2067 } 2068 // Parse try with resource. 2069 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) { 2070 parseParens(); 2071 } 2072 if (FormatTok->is(tok::l_brace)) { 2073 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2074 parseBlock(/*MustBeDeclaration=*/false); 2075 if (Style.BraceWrapping.BeforeCatch) { 2076 addUnwrappedLine(); 2077 } else { 2078 NeedsUnwrappedLine = true; 2079 } 2080 } else if (!FormatTok->is(tok::kw_catch)) { 2081 // The C++ standard requires a compound-statement after a try. 2082 // If there's none, we try to assume there's a structuralElement 2083 // and try to continue. 2084 addUnwrappedLine(); 2085 ++Line->Level; 2086 parseStructuralElement(); 2087 --Line->Level; 2088 } 2089 while (1) { 2090 if (FormatTok->is(tok::at)) 2091 nextToken(); 2092 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, 2093 tok::kw___finally) || 2094 ((Style.Language == FormatStyle::LK_Java || 2095 Style.Language == FormatStyle::LK_JavaScript) && 2096 FormatTok->is(Keywords.kw_finally)) || 2097 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || 2098 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) 2099 break; 2100 nextToken(); 2101 while (FormatTok->isNot(tok::l_brace)) { 2102 if (FormatTok->is(tok::l_paren)) { 2103 parseParens(); 2104 continue; 2105 } 2106 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) 2107 return; 2108 nextToken(); 2109 } 2110 NeedsUnwrappedLine = false; 2111 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2112 parseBlock(/*MustBeDeclaration=*/false); 2113 if (Style.BraceWrapping.BeforeCatch) 2114 addUnwrappedLine(); 2115 else 2116 NeedsUnwrappedLine = true; 2117 } 2118 if (NeedsUnwrappedLine) 2119 addUnwrappedLine(); 2120 } 2121 2122 void UnwrappedLineParser::parseNamespace() { 2123 assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) && 2124 "'namespace' expected"); 2125 2126 const FormatToken &InitialToken = *FormatTok; 2127 nextToken(); 2128 if (InitialToken.is(TT_NamespaceMacro)) { 2129 parseParens(); 2130 } else { 2131 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline, 2132 tok::l_square)) { 2133 if (FormatTok->is(tok::l_square)) 2134 parseSquare(); 2135 else 2136 nextToken(); 2137 } 2138 } 2139 if (FormatTok->Tok.is(tok::l_brace)) { 2140 if (ShouldBreakBeforeBrace(Style, InitialToken)) 2141 addUnwrappedLine(); 2142 2143 unsigned AddLevels = 2144 Style.NamespaceIndentation == FormatStyle::NI_All || 2145 (Style.NamespaceIndentation == FormatStyle::NI_Inner && 2146 DeclarationScopeStack.size() > 1) 2147 ? 1u 2148 : 0u; 2149 bool ManageWhitesmithsBraces = 2150 AddLevels == 0u && 2151 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths; 2152 2153 // If we're in Whitesmiths mode, indent the brace if we're not indenting 2154 // the whole block. 2155 if (ManageWhitesmithsBraces) 2156 ++Line->Level; 2157 2158 parseBlock(/*MustBeDeclaration=*/true, AddLevels, 2159 /*MunchSemi=*/true, 2160 /*UnindentWhitesmithsBraces=*/ManageWhitesmithsBraces); 2161 2162 // Munch the semicolon after a namespace. This is more common than one would 2163 // think. Putting the semicolon into its own line is very ugly. 2164 if (FormatTok->Tok.is(tok::semi)) 2165 nextToken(); 2166 2167 addUnwrappedLine(AddLevels > 0 ? LineLevel::Remove : LineLevel::Keep); 2168 2169 if (ManageWhitesmithsBraces) 2170 --Line->Level; 2171 } 2172 // FIXME: Add error handling. 2173 } 2174 2175 void UnwrappedLineParser::parseNew() { 2176 assert(FormatTok->is(tok::kw_new) && "'new' expected"); 2177 nextToken(); 2178 2179 if (Style.isCSharp()) { 2180 do { 2181 if (FormatTok->is(tok::l_brace)) 2182 parseBracedList(); 2183 2184 if (FormatTok->isOneOf(tok::semi, tok::comma)) 2185 return; 2186 2187 nextToken(); 2188 } while (!eof()); 2189 } 2190 2191 if (Style.Language != FormatStyle::LK_Java) 2192 return; 2193 2194 // In Java, we can parse everything up to the parens, which aren't optional. 2195 do { 2196 // There should not be a ;, { or } before the new's open paren. 2197 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace)) 2198 return; 2199 2200 // Consume the parens. 2201 if (FormatTok->is(tok::l_paren)) { 2202 parseParens(); 2203 2204 // If there is a class body of an anonymous class, consume that as child. 2205 if (FormatTok->is(tok::l_brace)) 2206 parseChildBlock(); 2207 return; 2208 } 2209 nextToken(); 2210 } while (!eof()); 2211 } 2212 2213 void UnwrappedLineParser::parseForOrWhileLoop() { 2214 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && 2215 "'for', 'while' or foreach macro expected"); 2216 nextToken(); 2217 // JS' for await ( ... 2218 if (Style.Language == FormatStyle::LK_JavaScript && 2219 FormatTok->is(Keywords.kw_await)) 2220 nextToken(); 2221 if (FormatTok->Tok.is(tok::l_paren)) 2222 parseParens(); 2223 if (FormatTok->Tok.is(tok::l_brace)) { 2224 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2225 parseBlock(/*MustBeDeclaration=*/false); 2226 addUnwrappedLine(); 2227 } else { 2228 addUnwrappedLine(); 2229 ++Line->Level; 2230 parseStructuralElement(); 2231 --Line->Level; 2232 } 2233 } 2234 2235 void UnwrappedLineParser::parseDoWhile() { 2236 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); 2237 nextToken(); 2238 if (FormatTok->Tok.is(tok::l_brace)) { 2239 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2240 parseBlock(/*MustBeDeclaration=*/false); 2241 if (Style.BraceWrapping.BeforeWhile) 2242 addUnwrappedLine(); 2243 } else { 2244 addUnwrappedLine(); 2245 ++Line->Level; 2246 parseStructuralElement(); 2247 --Line->Level; 2248 } 2249 2250 // FIXME: Add error handling. 2251 if (!FormatTok->Tok.is(tok::kw_while)) { 2252 addUnwrappedLine(); 2253 return; 2254 } 2255 2256 // If in Whitesmiths mode, the line with the while() needs to be indented 2257 // to the same level as the block. 2258 if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) 2259 ++Line->Level; 2260 2261 nextToken(); 2262 parseStructuralElement(); 2263 } 2264 2265 void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) { 2266 nextToken(); 2267 unsigned OldLineLevel = Line->Level; 2268 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) 2269 --Line->Level; 2270 if (LeftAlignLabel) 2271 Line->Level = 0; 2272 2273 if (!Style.IndentCaseBlocks && CommentsBeforeNextToken.empty() && 2274 FormatTok->Tok.is(tok::l_brace)) { 2275 2276 CompoundStatementIndenter Indenter(this, Line->Level, 2277 Style.BraceWrapping.AfterCaseLabel, 2278 Style.BraceWrapping.IndentBraces); 2279 parseBlock(/*MustBeDeclaration=*/false); 2280 if (FormatTok->Tok.is(tok::kw_break)) { 2281 if (Style.BraceWrapping.AfterControlStatement == 2282 FormatStyle::BWACS_Always) { 2283 addUnwrappedLine(); 2284 if (!Style.IndentCaseBlocks && 2285 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) { 2286 Line->Level++; 2287 } 2288 } 2289 parseStructuralElement(); 2290 } 2291 addUnwrappedLine(); 2292 } else { 2293 if (FormatTok->is(tok::semi)) 2294 nextToken(); 2295 addUnwrappedLine(); 2296 } 2297 Line->Level = OldLineLevel; 2298 if (FormatTok->isNot(tok::l_brace)) { 2299 parseStructuralElement(); 2300 addUnwrappedLine(); 2301 } 2302 } 2303 2304 void UnwrappedLineParser::parseCaseLabel() { 2305 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); 2306 2307 // FIXME: fix handling of complex expressions here. 2308 do { 2309 nextToken(); 2310 } while (!eof() && !FormatTok->Tok.is(tok::colon)); 2311 parseLabel(); 2312 } 2313 2314 void UnwrappedLineParser::parseSwitch() { 2315 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); 2316 nextToken(); 2317 if (FormatTok->Tok.is(tok::l_paren)) 2318 parseParens(); 2319 if (FormatTok->Tok.is(tok::l_brace)) { 2320 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2321 parseBlock(/*MustBeDeclaration=*/false); 2322 addUnwrappedLine(); 2323 } else { 2324 addUnwrappedLine(); 2325 ++Line->Level; 2326 parseStructuralElement(); 2327 --Line->Level; 2328 } 2329 } 2330 2331 void UnwrappedLineParser::parseAccessSpecifier() { 2332 nextToken(); 2333 // Understand Qt's slots. 2334 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots)) 2335 nextToken(); 2336 // Otherwise, we don't know what it is, and we'd better keep the next token. 2337 if (FormatTok->Tok.is(tok::colon)) 2338 nextToken(); 2339 addUnwrappedLine(); 2340 } 2341 2342 void UnwrappedLineParser::parseConcept() { 2343 assert(FormatTok->Tok.is(tok::kw_concept) && "'concept' expected"); 2344 nextToken(); 2345 if (!FormatTok->Tok.is(tok::identifier)) 2346 return; 2347 nextToken(); 2348 if (!FormatTok->Tok.is(tok::equal)) 2349 return; 2350 nextToken(); 2351 if (FormatTok->Tok.is(tok::kw_requires)) { 2352 nextToken(); 2353 parseRequiresExpression(Line->Level); 2354 } else { 2355 parseConstraintExpression(Line->Level); 2356 } 2357 } 2358 2359 void UnwrappedLineParser::parseRequiresExpression(unsigned int OriginalLevel) { 2360 // requires (R range) 2361 if (FormatTok->Tok.is(tok::l_paren)) { 2362 parseParens(); 2363 if (Style.IndentRequires && OriginalLevel != Line->Level) { 2364 addUnwrappedLine(); 2365 --Line->Level; 2366 } 2367 } 2368 2369 if (FormatTok->Tok.is(tok::l_brace)) { 2370 if (Style.BraceWrapping.AfterFunction) 2371 addUnwrappedLine(); 2372 FormatTok->setType(TT_FunctionLBrace); 2373 parseBlock(/*MustBeDeclaration=*/false); 2374 addUnwrappedLine(); 2375 } else { 2376 parseConstraintExpression(OriginalLevel); 2377 } 2378 } 2379 2380 void UnwrappedLineParser::parseConstraintExpression( 2381 unsigned int OriginalLevel) { 2382 // requires Id<T> && Id<T> || Id<T> 2383 while ( 2384 FormatTok->isOneOf(tok::identifier, tok::kw_requires, tok::coloncolon)) { 2385 nextToken(); 2386 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::less, 2387 tok::greater, tok::comma, tok::ellipsis)) { 2388 if (FormatTok->Tok.is(tok::less)) { 2389 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 2390 /*ClosingBraceKind=*/tok::greater); 2391 continue; 2392 } 2393 nextToken(); 2394 } 2395 if (FormatTok->Tok.is(tok::kw_requires)) { 2396 parseRequiresExpression(OriginalLevel); 2397 } 2398 if (FormatTok->Tok.is(tok::less)) { 2399 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 2400 /*ClosingBraceKind=*/tok::greater); 2401 } 2402 2403 if (FormatTok->Tok.is(tok::l_paren)) { 2404 parseParens(); 2405 } 2406 if (FormatTok->Tok.is(tok::l_brace)) { 2407 if (Style.BraceWrapping.AfterFunction) 2408 addUnwrappedLine(); 2409 FormatTok->setType(TT_FunctionLBrace); 2410 parseBlock(/*MustBeDeclaration=*/false); 2411 } 2412 if (FormatTok->Tok.is(tok::semi)) { 2413 // Eat any trailing semi. 2414 nextToken(); 2415 addUnwrappedLine(); 2416 } 2417 if (FormatTok->Tok.is(tok::colon)) { 2418 return; 2419 } 2420 if (!FormatTok->Tok.isOneOf(tok::ampamp, tok::pipepipe)) { 2421 if (FormatTok->Previous && 2422 !FormatTok->Previous->isOneOf(tok::identifier, tok::kw_requires, 2423 tok::coloncolon)) { 2424 addUnwrappedLine(); 2425 } 2426 if (Style.IndentRequires && OriginalLevel != Line->Level) { 2427 --Line->Level; 2428 } 2429 break; 2430 } else { 2431 FormatTok->setType(TT_ConstraintJunctions); 2432 } 2433 2434 nextToken(); 2435 } 2436 } 2437 2438 void UnwrappedLineParser::parseRequires() { 2439 assert(FormatTok->Tok.is(tok::kw_requires) && "'requires' expected"); 2440 2441 unsigned OriginalLevel = Line->Level; 2442 if (FormatTok->Previous && FormatTok->Previous->is(tok::greater)) { 2443 addUnwrappedLine(); 2444 if (Style.IndentRequires) { 2445 Line->Level++; 2446 } 2447 } 2448 nextToken(); 2449 2450 parseRequiresExpression(OriginalLevel); 2451 } 2452 2453 bool UnwrappedLineParser::parseEnum() { 2454 // Won't be 'enum' for NS_ENUMs. 2455 if (FormatTok->Tok.is(tok::kw_enum)) 2456 nextToken(); 2457 2458 // In TypeScript, "enum" can also be used as property name, e.g. in interface 2459 // declarations. An "enum" keyword followed by a colon would be a syntax 2460 // error and thus assume it is just an identifier. 2461 if (Style.Language == FormatStyle::LK_JavaScript && 2462 FormatTok->isOneOf(tok::colon, tok::question)) 2463 return false; 2464 2465 // In protobuf, "enum" can be used as a field name. 2466 if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal)) 2467 return false; 2468 2469 // Eat up enum class ... 2470 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) 2471 nextToken(); 2472 2473 while (FormatTok->Tok.getIdentifierInfo() || 2474 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, 2475 tok::greater, tok::comma, tok::question)) { 2476 nextToken(); 2477 // We can have macros or attributes in between 'enum' and the enum name. 2478 if (FormatTok->is(tok::l_paren)) 2479 parseParens(); 2480 if (FormatTok->is(tok::identifier)) { 2481 nextToken(); 2482 // If there are two identifiers in a row, this is likely an elaborate 2483 // return type. In Java, this can be "implements", etc. 2484 if (Style.isCpp() && FormatTok->is(tok::identifier)) 2485 return false; 2486 } 2487 } 2488 2489 // Just a declaration or something is wrong. 2490 if (FormatTok->isNot(tok::l_brace)) 2491 return true; 2492 FormatTok->setBlockKind(BK_Block); 2493 2494 if (Style.Language == FormatStyle::LK_Java) { 2495 // Java enums are different. 2496 parseJavaEnumBody(); 2497 return true; 2498 } 2499 if (Style.Language == FormatStyle::LK_Proto) { 2500 parseBlock(/*MustBeDeclaration=*/true); 2501 return true; 2502 } 2503 2504 if (!Style.AllowShortEnumsOnASingleLine) 2505 addUnwrappedLine(); 2506 // Parse enum body. 2507 nextToken(); 2508 if (!Style.AllowShortEnumsOnASingleLine) { 2509 addUnwrappedLine(); 2510 Line->Level += 1; 2511 } 2512 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true, 2513 /*IsEnum=*/true); 2514 if (!Style.AllowShortEnumsOnASingleLine) 2515 Line->Level -= 1; 2516 if (HasError) { 2517 if (FormatTok->is(tok::semi)) 2518 nextToken(); 2519 addUnwrappedLine(); 2520 } 2521 return true; 2522 2523 // There is no addUnwrappedLine() here so that we fall through to parsing a 2524 // structural element afterwards. Thus, in "enum A {} n, m;", 2525 // "} n, m;" will end up in one unwrapped line. 2526 } 2527 2528 namespace { 2529 // A class used to set and restore the Token position when peeking 2530 // ahead in the token source. 2531 class ScopedTokenPosition { 2532 unsigned StoredPosition; 2533 FormatTokenSource *Tokens; 2534 2535 public: 2536 ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) { 2537 assert(Tokens && "Tokens expected to not be null"); 2538 StoredPosition = Tokens->getPosition(); 2539 } 2540 2541 ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); } 2542 }; 2543 } // namespace 2544 2545 // Look to see if we have [[ by looking ahead, if 2546 // its not then rewind to the original position. 2547 bool UnwrappedLineParser::tryToParseSimpleAttribute() { 2548 ScopedTokenPosition AutoPosition(Tokens); 2549 FormatToken *Tok = Tokens->getNextToken(); 2550 // We already read the first [ check for the second. 2551 if (Tok && !Tok->is(tok::l_square)) { 2552 return false; 2553 } 2554 // Double check that the attribute is just something 2555 // fairly simple. 2556 while (Tok) { 2557 if (Tok->is(tok::r_square)) { 2558 break; 2559 } 2560 Tok = Tokens->getNextToken(); 2561 } 2562 Tok = Tokens->getNextToken(); 2563 if (Tok && !Tok->is(tok::r_square)) { 2564 return false; 2565 } 2566 Tok = Tokens->getNextToken(); 2567 if (Tok && Tok->is(tok::semi)) { 2568 return false; 2569 } 2570 return true; 2571 } 2572 2573 void UnwrappedLineParser::parseJavaEnumBody() { 2574 // Determine whether the enum is simple, i.e. does not have a semicolon or 2575 // constants with class bodies. Simple enums can be formatted like braced 2576 // lists, contracted to a single line, etc. 2577 unsigned StoredPosition = Tokens->getPosition(); 2578 bool IsSimple = true; 2579 FormatToken *Tok = Tokens->getNextToken(); 2580 while (Tok) { 2581 if (Tok->is(tok::r_brace)) 2582 break; 2583 if (Tok->isOneOf(tok::l_brace, tok::semi)) { 2584 IsSimple = false; 2585 break; 2586 } 2587 // FIXME: This will also mark enums with braces in the arguments to enum 2588 // constants as "not simple". This is probably fine in practice, though. 2589 Tok = Tokens->getNextToken(); 2590 } 2591 FormatTok = Tokens->setPosition(StoredPosition); 2592 2593 if (IsSimple) { 2594 nextToken(); 2595 parseBracedList(); 2596 addUnwrappedLine(); 2597 return; 2598 } 2599 2600 // Parse the body of a more complex enum. 2601 // First add a line for everything up to the "{". 2602 nextToken(); 2603 addUnwrappedLine(); 2604 ++Line->Level; 2605 2606 // Parse the enum constants. 2607 while (FormatTok) { 2608 if (FormatTok->is(tok::l_brace)) { 2609 // Parse the constant's class body. 2610 parseBlock(/*MustBeDeclaration=*/true, /*AddLevels=*/1u, 2611 /*MunchSemi=*/false); 2612 } else if (FormatTok->is(tok::l_paren)) { 2613 parseParens(); 2614 } else if (FormatTok->is(tok::comma)) { 2615 nextToken(); 2616 addUnwrappedLine(); 2617 } else if (FormatTok->is(tok::semi)) { 2618 nextToken(); 2619 addUnwrappedLine(); 2620 break; 2621 } else if (FormatTok->is(tok::r_brace)) { 2622 addUnwrappedLine(); 2623 break; 2624 } else { 2625 nextToken(); 2626 } 2627 } 2628 2629 // Parse the class body after the enum's ";" if any. 2630 parseLevel(/*HasOpeningBrace=*/true); 2631 nextToken(); 2632 --Line->Level; 2633 addUnwrappedLine(); 2634 } 2635 2636 void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { 2637 const FormatToken &InitialToken = *FormatTok; 2638 nextToken(); 2639 2640 // The actual identifier can be a nested name specifier, and in macros 2641 // it is often token-pasted. 2642 // An [[attribute]] can be before the identifier. 2643 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, 2644 tok::kw___attribute, tok::kw___declspec, 2645 tok::kw_alignas, tok::l_square, tok::r_square) || 2646 ((Style.Language == FormatStyle::LK_Java || 2647 Style.Language == FormatStyle::LK_JavaScript) && 2648 FormatTok->isOneOf(tok::period, tok::comma))) { 2649 if (Style.Language == FormatStyle::LK_JavaScript && 2650 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) { 2651 // JavaScript/TypeScript supports inline object types in 2652 // extends/implements positions: 2653 // class Foo implements {bar: number} { } 2654 nextToken(); 2655 if (FormatTok->is(tok::l_brace)) { 2656 tryToParseBracedList(); 2657 continue; 2658 } 2659 } 2660 bool IsNonMacroIdentifier = 2661 FormatTok->is(tok::identifier) && 2662 FormatTok->TokenText != FormatTok->TokenText.upper(); 2663 nextToken(); 2664 // We can have macros or attributes in between 'class' and the class name. 2665 if (!IsNonMacroIdentifier) { 2666 if (FormatTok->Tok.is(tok::l_paren)) { 2667 parseParens(); 2668 } else if (FormatTok->is(TT_AttributeSquare)) { 2669 parseSquare(); 2670 // Consume the closing TT_AttributeSquare. 2671 if (FormatTok->Next && FormatTok->is(TT_AttributeSquare)) 2672 nextToken(); 2673 } 2674 } 2675 } 2676 2677 // Note that parsing away template declarations here leads to incorrectly 2678 // accepting function declarations as record declarations. 2679 // In general, we cannot solve this problem. Consider: 2680 // class A<int> B() {} 2681 // which can be a function definition or a class definition when B() is a 2682 // macro. If we find enough real-world cases where this is a problem, we 2683 // can parse for the 'template' keyword in the beginning of the statement, 2684 // and thus rule out the record production in case there is no template 2685 // (this would still leave us with an ambiguity between template function 2686 // and class declarations). 2687 if (FormatTok->isOneOf(tok::colon, tok::less)) { 2688 while (!eof()) { 2689 if (FormatTok->is(tok::l_brace)) { 2690 calculateBraceTypes(/*ExpectClassBody=*/true); 2691 if (!tryToParseBracedList()) 2692 break; 2693 } 2694 if (FormatTok->Tok.is(tok::semi)) 2695 return; 2696 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)) { 2697 addUnwrappedLine(); 2698 nextToken(); 2699 parseCSharpGenericTypeConstraint(); 2700 break; 2701 } 2702 nextToken(); 2703 } 2704 } 2705 if (FormatTok->Tok.is(tok::l_brace)) { 2706 if (ParseAsExpr) { 2707 parseChildBlock(); 2708 } else { 2709 if (ShouldBreakBeforeBrace(Style, InitialToken)) 2710 addUnwrappedLine(); 2711 2712 unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u; 2713 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false); 2714 } 2715 } 2716 // There is no addUnwrappedLine() here so that we fall through to parsing a 2717 // structural element afterwards. Thus, in "class A {} n, m;", 2718 // "} n, m;" will end up in one unwrapped line. 2719 } 2720 2721 void UnwrappedLineParser::parseObjCMethod() { 2722 assert(FormatTok->Tok.isOneOf(tok::l_paren, tok::identifier) && 2723 "'(' or identifier expected."); 2724 do { 2725 if (FormatTok->Tok.is(tok::semi)) { 2726 nextToken(); 2727 addUnwrappedLine(); 2728 return; 2729 } else if (FormatTok->Tok.is(tok::l_brace)) { 2730 if (Style.BraceWrapping.AfterFunction) 2731 addUnwrappedLine(); 2732 parseBlock(/*MustBeDeclaration=*/false); 2733 addUnwrappedLine(); 2734 return; 2735 } else { 2736 nextToken(); 2737 } 2738 } while (!eof()); 2739 } 2740 2741 void UnwrappedLineParser::parseObjCProtocolList() { 2742 assert(FormatTok->Tok.is(tok::less) && "'<' expected."); 2743 do { 2744 nextToken(); 2745 // Early exit in case someone forgot a close angle. 2746 if (FormatTok->isOneOf(tok::semi, tok::l_brace) || 2747 FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) 2748 return; 2749 } while (!eof() && FormatTok->Tok.isNot(tok::greater)); 2750 nextToken(); // Skip '>'. 2751 } 2752 2753 void UnwrappedLineParser::parseObjCUntilAtEnd() { 2754 do { 2755 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { 2756 nextToken(); 2757 addUnwrappedLine(); 2758 break; 2759 } 2760 if (FormatTok->is(tok::l_brace)) { 2761 parseBlock(/*MustBeDeclaration=*/false); 2762 // In ObjC interfaces, nothing should be following the "}". 2763 addUnwrappedLine(); 2764 } else if (FormatTok->is(tok::r_brace)) { 2765 // Ignore stray "}". parseStructuralElement doesn't consume them. 2766 nextToken(); 2767 addUnwrappedLine(); 2768 } else if (FormatTok->isOneOf(tok::minus, tok::plus)) { 2769 nextToken(); 2770 parseObjCMethod(); 2771 } else { 2772 parseStructuralElement(); 2773 } 2774 } while (!eof()); 2775 } 2776 2777 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { 2778 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface || 2779 FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation); 2780 nextToken(); 2781 nextToken(); // interface name 2782 2783 // @interface can be followed by a lightweight generic 2784 // specialization list, then either a base class or a category. 2785 if (FormatTok->Tok.is(tok::less)) { 2786 parseObjCLightweightGenerics(); 2787 } 2788 if (FormatTok->Tok.is(tok::colon)) { 2789 nextToken(); 2790 nextToken(); // base class name 2791 // The base class can also have lightweight generics applied to it. 2792 if (FormatTok->Tok.is(tok::less)) { 2793 parseObjCLightweightGenerics(); 2794 } 2795 } else if (FormatTok->Tok.is(tok::l_paren)) 2796 // Skip category, if present. 2797 parseParens(); 2798 2799 if (FormatTok->Tok.is(tok::less)) 2800 parseObjCProtocolList(); 2801 2802 if (FormatTok->Tok.is(tok::l_brace)) { 2803 if (Style.BraceWrapping.AfterObjCDeclaration) 2804 addUnwrappedLine(); 2805 parseBlock(/*MustBeDeclaration=*/true); 2806 } 2807 2808 // With instance variables, this puts '}' on its own line. Without instance 2809 // variables, this ends the @interface line. 2810 addUnwrappedLine(); 2811 2812 parseObjCUntilAtEnd(); 2813 } 2814 2815 void UnwrappedLineParser::parseObjCLightweightGenerics() { 2816 assert(FormatTok->Tok.is(tok::less)); 2817 // Unlike protocol lists, generic parameterizations support 2818 // nested angles: 2819 // 2820 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> : 2821 // NSObject <NSCopying, NSSecureCoding> 2822 // 2823 // so we need to count how many open angles we have left. 2824 unsigned NumOpenAngles = 1; 2825 do { 2826 nextToken(); 2827 // Early exit in case someone forgot a close angle. 2828 if (FormatTok->isOneOf(tok::semi, tok::l_brace) || 2829 FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) 2830 break; 2831 if (FormatTok->Tok.is(tok::less)) 2832 ++NumOpenAngles; 2833 else if (FormatTok->Tok.is(tok::greater)) { 2834 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative"); 2835 --NumOpenAngles; 2836 } 2837 } while (!eof() && NumOpenAngles != 0); 2838 nextToken(); // Skip '>'. 2839 } 2840 2841 // Returns true for the declaration/definition form of @protocol, 2842 // false for the expression form. 2843 bool UnwrappedLineParser::parseObjCProtocol() { 2844 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol); 2845 nextToken(); 2846 2847 if (FormatTok->is(tok::l_paren)) 2848 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);". 2849 return false; 2850 2851 // The definition/declaration form, 2852 // @protocol Foo 2853 // - (int)someMethod; 2854 // @end 2855 2856 nextToken(); // protocol name 2857 2858 if (FormatTok->Tok.is(tok::less)) 2859 parseObjCProtocolList(); 2860 2861 // Check for protocol declaration. 2862 if (FormatTok->Tok.is(tok::semi)) { 2863 nextToken(); 2864 addUnwrappedLine(); 2865 return true; 2866 } 2867 2868 addUnwrappedLine(); 2869 parseObjCUntilAtEnd(); 2870 return true; 2871 } 2872 2873 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { 2874 bool IsImport = FormatTok->is(Keywords.kw_import); 2875 assert(IsImport || FormatTok->is(tok::kw_export)); 2876 nextToken(); 2877 2878 // Consume the "default" in "export default class/function". 2879 if (FormatTok->is(tok::kw_default)) 2880 nextToken(); 2881 2882 // Consume "async function", "function" and "default function", so that these 2883 // get parsed as free-standing JS functions, i.e. do not require a trailing 2884 // semicolon. 2885 if (FormatTok->is(Keywords.kw_async)) 2886 nextToken(); 2887 if (FormatTok->is(Keywords.kw_function)) { 2888 nextToken(); 2889 return; 2890 } 2891 2892 // For imports, `export *`, `export {...}`, consume the rest of the line up 2893 // to the terminating `;`. For everything else, just return and continue 2894 // parsing the structural element, i.e. the declaration or expression for 2895 // `export default`. 2896 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) && 2897 !FormatTok->isStringLiteral()) 2898 return; 2899 2900 while (!eof()) { 2901 if (FormatTok->is(tok::semi)) 2902 return; 2903 if (Line->Tokens.empty()) { 2904 // Common issue: Automatic Semicolon Insertion wrapped the line, so the 2905 // import statement should terminate. 2906 return; 2907 } 2908 if (FormatTok->is(tok::l_brace)) { 2909 FormatTok->setBlockKind(BK_Block); 2910 nextToken(); 2911 parseBracedList(); 2912 } else { 2913 nextToken(); 2914 } 2915 } 2916 } 2917 2918 void UnwrappedLineParser::parseStatementMacro() { 2919 nextToken(); 2920 if (FormatTok->is(tok::l_paren)) 2921 parseParens(); 2922 if (FormatTok->is(tok::semi)) 2923 nextToken(); 2924 addUnwrappedLine(); 2925 } 2926 2927 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, 2928 StringRef Prefix = "") { 2929 llvm::dbgs() << Prefix << "Line(" << Line.Level 2930 << ", FSC=" << Line.FirstStartColumn << ")" 2931 << (Line.InPPDirective ? " MACRO" : "") << ": "; 2932 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 2933 E = Line.Tokens.end(); 2934 I != E; ++I) { 2935 llvm::dbgs() << I->Tok->Tok.getName() << "[" 2936 << "T=" << (unsigned)I->Tok->getType() 2937 << ", OC=" << I->Tok->OriginalColumn << "] "; 2938 } 2939 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 2940 E = Line.Tokens.end(); 2941 I != E; ++I) { 2942 const UnwrappedLineNode &Node = *I; 2943 for (SmallVectorImpl<UnwrappedLine>::const_iterator 2944 I = Node.Children.begin(), 2945 E = Node.Children.end(); 2946 I != E; ++I) { 2947 printDebugInfo(*I, "\nChild: "); 2948 } 2949 } 2950 llvm::dbgs() << "\n"; 2951 } 2952 2953 void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) { 2954 if (Line->Tokens.empty()) 2955 return; 2956 LLVM_DEBUG({ 2957 if (CurrentLines == &Lines) 2958 printDebugInfo(*Line); 2959 }); 2960 2961 // If this line closes a block when in Whitesmiths mode, remember that 2962 // information so that the level can be decreased after the line is added. 2963 // This has to happen after the addition of the line since the line itself 2964 // needs to be indented. 2965 bool ClosesWhitesmithsBlock = 2966 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex && 2967 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths; 2968 2969 CurrentLines->push_back(std::move(*Line)); 2970 Line->Tokens.clear(); 2971 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex; 2972 Line->FirstStartColumn = 0; 2973 2974 if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove) 2975 --Line->Level; 2976 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { 2977 CurrentLines->append( 2978 std::make_move_iterator(PreprocessorDirectives.begin()), 2979 std::make_move_iterator(PreprocessorDirectives.end())); 2980 PreprocessorDirectives.clear(); 2981 } 2982 // Disconnect the current token from the last token on the previous line. 2983 FormatTok->Previous = nullptr; 2984 } 2985 2986 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } 2987 2988 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { 2989 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && 2990 FormatTok.NewlinesBefore > 0; 2991 } 2992 2993 // Checks if \p FormatTok is a line comment that continues the line comment 2994 // section on \p Line. 2995 static bool 2996 continuesLineCommentSection(const FormatToken &FormatTok, 2997 const UnwrappedLine &Line, 2998 const llvm::Regex &CommentPragmasRegex) { 2999 if (Line.Tokens.empty()) 3000 return false; 3001 3002 StringRef IndentContent = FormatTok.TokenText; 3003 if (FormatTok.TokenText.startswith("//") || 3004 FormatTok.TokenText.startswith("/*")) 3005 IndentContent = FormatTok.TokenText.substr(2); 3006 if (CommentPragmasRegex.match(IndentContent)) 3007 return false; 3008 3009 // If Line starts with a line comment, then FormatTok continues the comment 3010 // section if its original column is greater or equal to the original start 3011 // column of the line. 3012 // 3013 // Define the min column token of a line as follows: if a line ends in '{' or 3014 // contains a '{' followed by a line comment, then the min column token is 3015 // that '{'. Otherwise, the min column token of the line is the first token of 3016 // the line. 3017 // 3018 // If Line starts with a token other than a line comment, then FormatTok 3019 // continues the comment section if its original column is greater than the 3020 // original start column of the min column token of the line. 3021 // 3022 // For example, the second line comment continues the first in these cases: 3023 // 3024 // // first line 3025 // // second line 3026 // 3027 // and: 3028 // 3029 // // first line 3030 // // second line 3031 // 3032 // and: 3033 // 3034 // int i; // first line 3035 // // second line 3036 // 3037 // and: 3038 // 3039 // do { // first line 3040 // // second line 3041 // int i; 3042 // } while (true); 3043 // 3044 // and: 3045 // 3046 // enum { 3047 // a, // first line 3048 // // second line 3049 // b 3050 // }; 3051 // 3052 // The second line comment doesn't continue the first in these cases: 3053 // 3054 // // first line 3055 // // second line 3056 // 3057 // and: 3058 // 3059 // int i; // first line 3060 // // second line 3061 // 3062 // and: 3063 // 3064 // do { // first line 3065 // // second line 3066 // int i; 3067 // } while (true); 3068 // 3069 // and: 3070 // 3071 // enum { 3072 // a, // first line 3073 // // second line 3074 // }; 3075 const FormatToken *MinColumnToken = Line.Tokens.front().Tok; 3076 3077 // Scan for '{//'. If found, use the column of '{' as a min column for line 3078 // comment section continuation. 3079 const FormatToken *PreviousToken = nullptr; 3080 for (const UnwrappedLineNode &Node : Line.Tokens) { 3081 if (PreviousToken && PreviousToken->is(tok::l_brace) && 3082 isLineComment(*Node.Tok)) { 3083 MinColumnToken = PreviousToken; 3084 break; 3085 } 3086 PreviousToken = Node.Tok; 3087 3088 // Grab the last newline preceding a token in this unwrapped line. 3089 if (Node.Tok->NewlinesBefore > 0) { 3090 MinColumnToken = Node.Tok; 3091 } 3092 } 3093 if (PreviousToken && PreviousToken->is(tok::l_brace)) { 3094 MinColumnToken = PreviousToken; 3095 } 3096 3097 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok, 3098 MinColumnToken); 3099 } 3100 3101 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { 3102 bool JustComments = Line->Tokens.empty(); 3103 for (SmallVectorImpl<FormatToken *>::const_iterator 3104 I = CommentsBeforeNextToken.begin(), 3105 E = CommentsBeforeNextToken.end(); 3106 I != E; ++I) { 3107 // Line comments that belong to the same line comment section are put on the 3108 // same line since later we might want to reflow content between them. 3109 // Additional fine-grained breaking of line comment sections is controlled 3110 // by the class BreakableLineCommentSection in case it is desirable to keep 3111 // several line comment sections in the same unwrapped line. 3112 // 3113 // FIXME: Consider putting separate line comment sections as children to the 3114 // unwrapped line instead. 3115 (*I)->ContinuesLineCommentSection = 3116 continuesLineCommentSection(**I, *Line, CommentPragmasRegex); 3117 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection) 3118 addUnwrappedLine(); 3119 pushToken(*I); 3120 } 3121 if (NewlineBeforeNext && JustComments) 3122 addUnwrappedLine(); 3123 CommentsBeforeNextToken.clear(); 3124 } 3125 3126 void UnwrappedLineParser::nextToken(int LevelDifference) { 3127 if (eof()) 3128 return; 3129 flushComments(isOnNewLine(*FormatTok)); 3130 pushToken(FormatTok); 3131 FormatToken *Previous = FormatTok; 3132 if (Style.Language != FormatStyle::LK_JavaScript) 3133 readToken(LevelDifference); 3134 else 3135 readTokenWithJavaScriptASI(); 3136 FormatTok->Previous = Previous; 3137 } 3138 3139 void UnwrappedLineParser::distributeComments( 3140 const SmallVectorImpl<FormatToken *> &Comments, 3141 const FormatToken *NextTok) { 3142 // Whether or not a line comment token continues a line is controlled by 3143 // the method continuesLineCommentSection, with the following caveat: 3144 // 3145 // Define a trail of Comments to be a nonempty proper postfix of Comments such 3146 // that each comment line from the trail is aligned with the next token, if 3147 // the next token exists. If a trail exists, the beginning of the maximal 3148 // trail is marked as a start of a new comment section. 3149 // 3150 // For example in this code: 3151 // 3152 // int a; // line about a 3153 // // line 1 about b 3154 // // line 2 about b 3155 // int b; 3156 // 3157 // the two lines about b form a maximal trail, so there are two sections, the 3158 // first one consisting of the single comment "// line about a" and the 3159 // second one consisting of the next two comments. 3160 if (Comments.empty()) 3161 return; 3162 bool ShouldPushCommentsInCurrentLine = true; 3163 bool HasTrailAlignedWithNextToken = false; 3164 unsigned StartOfTrailAlignedWithNextToken = 0; 3165 if (NextTok) { 3166 // We are skipping the first element intentionally. 3167 for (unsigned i = Comments.size() - 1; i > 0; --i) { 3168 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) { 3169 HasTrailAlignedWithNextToken = true; 3170 StartOfTrailAlignedWithNextToken = i; 3171 } 3172 } 3173 } 3174 for (unsigned i = 0, e = Comments.size(); i < e; ++i) { 3175 FormatToken *FormatTok = Comments[i]; 3176 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) { 3177 FormatTok->ContinuesLineCommentSection = false; 3178 } else { 3179 FormatTok->ContinuesLineCommentSection = 3180 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex); 3181 } 3182 if (!FormatTok->ContinuesLineCommentSection && 3183 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) { 3184 ShouldPushCommentsInCurrentLine = false; 3185 } 3186 if (ShouldPushCommentsInCurrentLine) { 3187 pushToken(FormatTok); 3188 } else { 3189 CommentsBeforeNextToken.push_back(FormatTok); 3190 } 3191 } 3192 } 3193 3194 void UnwrappedLineParser::readToken(int LevelDifference) { 3195 SmallVector<FormatToken *, 1> Comments; 3196 do { 3197 FormatTok = Tokens->getNextToken(); 3198 assert(FormatTok); 3199 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && 3200 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { 3201 distributeComments(Comments, FormatTok); 3202 Comments.clear(); 3203 // If there is an unfinished unwrapped line, we flush the preprocessor 3204 // directives only after that unwrapped line was finished later. 3205 bool SwitchToPreprocessorLines = !Line->Tokens.empty(); 3206 ScopedLineState BlockState(*this, SwitchToPreprocessorLines); 3207 assert((LevelDifference >= 0 || 3208 static_cast<unsigned>(-LevelDifference) <= Line->Level) && 3209 "LevelDifference makes Line->Level negative"); 3210 Line->Level += LevelDifference; 3211 // Comments stored before the preprocessor directive need to be output 3212 // before the preprocessor directive, at the same level as the 3213 // preprocessor directive, as we consider them to apply to the directive. 3214 if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash && 3215 PPBranchLevel > 0) 3216 Line->Level += PPBranchLevel; 3217 flushComments(isOnNewLine(*FormatTok)); 3218 parsePPDirective(); 3219 } 3220 while (FormatTok->getType() == TT_ConflictStart || 3221 FormatTok->getType() == TT_ConflictEnd || 3222 FormatTok->getType() == TT_ConflictAlternative) { 3223 if (FormatTok->getType() == TT_ConflictStart) { 3224 conditionalCompilationStart(/*Unreachable=*/false); 3225 } else if (FormatTok->getType() == TT_ConflictAlternative) { 3226 conditionalCompilationAlternative(); 3227 } else if (FormatTok->getType() == TT_ConflictEnd) { 3228 conditionalCompilationEnd(); 3229 } 3230 FormatTok = Tokens->getNextToken(); 3231 FormatTok->MustBreakBefore = true; 3232 } 3233 3234 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) && 3235 !Line->InPPDirective) { 3236 continue; 3237 } 3238 3239 if (!FormatTok->Tok.is(tok::comment)) { 3240 distributeComments(Comments, FormatTok); 3241 Comments.clear(); 3242 return; 3243 } 3244 3245 Comments.push_back(FormatTok); 3246 } while (!eof()); 3247 3248 distributeComments(Comments, nullptr); 3249 Comments.clear(); 3250 } 3251 3252 void UnwrappedLineParser::pushToken(FormatToken *Tok) { 3253 Line->Tokens.push_back(UnwrappedLineNode(Tok)); 3254 if (MustBreakBeforeNextToken) { 3255 Line->Tokens.back().Tok->MustBreakBefore = true; 3256 MustBreakBeforeNextToken = false; 3257 } 3258 } 3259 3260 } // end namespace format 3261 } // end namespace clang 3262