1 //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief This file contains the implementation of the UnwrappedLineParser, 12 /// which turns a stream of tokens into UnwrappedLines. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "UnwrappedLineParser.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 TokenSource = this; 87 Line.Level = 0; 88 Line.InPPDirective = true; 89 } 90 91 ~ScopedMacroState() override { 92 TokenSource = PreviousTokenSource; 93 ResetToken = Token; 94 Line.InPPDirective = false; 95 Line.Level = PreviousLineLevel; 96 } 97 98 FormatToken *getNextToken() override { 99 // The \c UnwrappedLineParser guards against this by never calling 100 // \c getNextToken() after it has encountered the first eof token. 101 assert(!eof()); 102 PreviousToken = Token; 103 Token = PreviousTokenSource->getNextToken(); 104 if (eof()) 105 return getFakeEOF(); 106 return Token; 107 } 108 109 unsigned getPosition() override { return PreviousTokenSource->getPosition(); } 110 111 FormatToken *setPosition(unsigned Position) override { 112 PreviousToken = nullptr; 113 Token = PreviousTokenSource->setPosition(Position); 114 return Token; 115 } 116 117 private: 118 bool eof() { 119 return Token && Token->HasUnescapedNewline && 120 !continuesLineComment(*Token, PreviousToken, 121 /*MinColumnToken=*/PreviousToken); 122 } 123 124 FormatToken *getFakeEOF() { 125 static bool EOFInitialized = false; 126 static FormatToken FormatTok; 127 if (!EOFInitialized) { 128 FormatTok.Tok.startToken(); 129 FormatTok.Tok.setKind(tok::eof); 130 EOFInitialized = true; 131 } 132 return &FormatTok; 133 } 134 135 UnwrappedLine &Line; 136 FormatTokenSource *&TokenSource; 137 FormatToken *&ResetToken; 138 unsigned PreviousLineLevel; 139 FormatTokenSource *PreviousTokenSource; 140 141 FormatToken *Token; 142 FormatToken *PreviousToken; 143 }; 144 145 } // end anonymous namespace 146 147 class ScopedLineState { 148 public: 149 ScopedLineState(UnwrappedLineParser &Parser, 150 bool SwitchToPreprocessorLines = false) 151 : Parser(Parser), OriginalLines(Parser.CurrentLines) { 152 if (SwitchToPreprocessorLines) 153 Parser.CurrentLines = &Parser.PreprocessorDirectives; 154 else if (!Parser.Line->Tokens.empty()) 155 Parser.CurrentLines = &Parser.Line->Tokens.back().Children; 156 PreBlockLine = std::move(Parser.Line); 157 Parser.Line = llvm::make_unique<UnwrappedLine>(); 158 Parser.Line->Level = PreBlockLine->Level; 159 Parser.Line->InPPDirective = PreBlockLine->InPPDirective; 160 } 161 162 ~ScopedLineState() { 163 if (!Parser.Line->Tokens.empty()) { 164 Parser.addUnwrappedLine(); 165 } 166 assert(Parser.Line->Tokens.empty()); 167 Parser.Line = std::move(PreBlockLine); 168 if (Parser.CurrentLines == &Parser.PreprocessorDirectives) 169 Parser.MustBreakBeforeNextToken = true; 170 Parser.CurrentLines = OriginalLines; 171 } 172 173 private: 174 UnwrappedLineParser &Parser; 175 176 std::unique_ptr<UnwrappedLine> PreBlockLine; 177 SmallVectorImpl<UnwrappedLine> *OriginalLines; 178 }; 179 180 class CompoundStatementIndenter { 181 public: 182 CompoundStatementIndenter(UnwrappedLineParser *Parser, 183 const FormatStyle &Style, unsigned &LineLevel) 184 : LineLevel(LineLevel), OldLineLevel(LineLevel) { 185 if (Style.BraceWrapping.AfterControlStatement) 186 Parser->addUnwrappedLine(); 187 if (Style.BraceWrapping.IndentBraces) 188 ++LineLevel; 189 } 190 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } 191 192 private: 193 unsigned &LineLevel; 194 unsigned OldLineLevel; 195 }; 196 197 namespace { 198 199 class IndexedTokenSource : public FormatTokenSource { 200 public: 201 IndexedTokenSource(ArrayRef<FormatToken *> Tokens) 202 : Tokens(Tokens), Position(-1) {} 203 204 FormatToken *getNextToken() override { 205 ++Position; 206 return Tokens[Position]; 207 } 208 209 unsigned getPosition() override { 210 assert(Position >= 0); 211 return Position; 212 } 213 214 FormatToken *setPosition(unsigned P) override { 215 Position = P; 216 return Tokens[Position]; 217 } 218 219 void reset() { Position = -1; } 220 221 private: 222 ArrayRef<FormatToken *> Tokens; 223 int Position; 224 }; 225 226 } // end anonymous namespace 227 228 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, 229 const AdditionalKeywords &Keywords, 230 unsigned FirstStartColumn, 231 ArrayRef<FormatToken *> Tokens, 232 UnwrappedLineConsumer &Callback) 233 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), 234 CurrentLines(&Lines), Style(Style), Keywords(Keywords), 235 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr), 236 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1), 237 IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None 238 ? IG_Rejected 239 : IG_Inited), 240 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {} 241 242 void UnwrappedLineParser::reset() { 243 PPBranchLevel = -1; 244 IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None 245 ? IG_Rejected 246 : IG_Inited; 247 IncludeGuardToken = nullptr; 248 Line.reset(new UnwrappedLine); 249 CommentsBeforeNextToken.clear(); 250 FormatTok = nullptr; 251 MustBreakBeforeNextToken = false; 252 PreprocessorDirectives.clear(); 253 CurrentLines = &Lines; 254 DeclarationScopeStack.clear(); 255 PPStack.clear(); 256 Line->FirstStartColumn = FirstStartColumn; 257 } 258 259 void UnwrappedLineParser::parse() { 260 IndexedTokenSource TokenSource(AllTokens); 261 Line->FirstStartColumn = FirstStartColumn; 262 do { 263 DEBUG(llvm::dbgs() << "----\n"); 264 reset(); 265 Tokens = &TokenSource; 266 TokenSource.reset(); 267 268 readToken(); 269 parseFile(); 270 271 // If we found an include guard then all preprocessor directives (other than 272 // the guard) are over-indented by one. 273 if (IncludeGuard == IG_Found) 274 for (auto &Line : Lines) 275 if (Line.InPPDirective && Line.Level > 0) 276 --Line.Level; 277 278 // Create line with eof token. 279 pushToken(FormatTok); 280 addUnwrappedLine(); 281 282 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(), 283 E = Lines.end(); 284 I != E; ++I) { 285 Callback.consumeUnwrappedLine(*I); 286 } 287 Callback.finishRun(); 288 Lines.clear(); 289 while (!PPLevelBranchIndex.empty() && 290 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) { 291 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); 292 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); 293 } 294 if (!PPLevelBranchIndex.empty()) { 295 ++PPLevelBranchIndex.back(); 296 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); 297 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); 298 } 299 } while (!PPLevelBranchIndex.empty()); 300 } 301 302 void UnwrappedLineParser::parseFile() { 303 // The top-level context in a file always has declarations, except for pre- 304 // processor directives and JavaScript files. 305 bool MustBeDeclaration = 306 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript; 307 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 308 MustBeDeclaration); 309 if (Style.Language == FormatStyle::LK_TextProto) 310 parseBracedList(); 311 else 312 parseLevel(/*HasOpeningBrace=*/false); 313 // Make sure to format the remaining tokens. 314 flushComments(true); 315 addUnwrappedLine(); 316 } 317 318 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { 319 bool SwitchLabelEncountered = false; 320 do { 321 tok::TokenKind kind = FormatTok->Tok.getKind(); 322 if (FormatTok->Type == TT_MacroBlockBegin) { 323 kind = tok::l_brace; 324 } else if (FormatTok->Type == TT_MacroBlockEnd) { 325 kind = tok::r_brace; 326 } 327 328 switch (kind) { 329 case tok::comment: 330 nextToken(); 331 addUnwrappedLine(); 332 break; 333 case tok::l_brace: 334 // FIXME: Add parameter whether this can happen - if this happens, we must 335 // be in a non-declaration context. 336 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList()) 337 continue; 338 parseBlock(/*MustBeDeclaration=*/false); 339 addUnwrappedLine(); 340 break; 341 case tok::r_brace: 342 if (HasOpeningBrace) 343 return; 344 nextToken(); 345 addUnwrappedLine(); 346 break; 347 case tok::kw_default: { 348 unsigned StoredPosition = Tokens->getPosition(); 349 FormatToken *Next = Tokens->getNextToken(); 350 FormatTok = Tokens->setPosition(StoredPosition); 351 if (Next && Next->isNot(tok::colon)) { 352 // default not followed by ':' is not a case label; treat it like 353 // an identifier. 354 parseStructuralElement(); 355 break; 356 } 357 // Else, if it is 'default:', fall through to the case handling. 358 LLVM_FALLTHROUGH; 359 } 360 case tok::kw_case: 361 if (Style.Language == FormatStyle::LK_JavaScript && 362 Line->MustBeDeclaration) { 363 // A 'case: string' style field declaration. 364 parseStructuralElement(); 365 break; 366 } 367 if (!SwitchLabelEncountered && 368 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1))) 369 ++Line->Level; 370 SwitchLabelEncountered = true; 371 parseStructuralElement(); 372 break; 373 default: 374 parseStructuralElement(); 375 break; 376 } 377 } while (!eof()); 378 } 379 380 void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { 381 // We'll parse forward through the tokens until we hit 382 // a closing brace or eof - note that getNextToken() will 383 // parse macros, so this will magically work inside macro 384 // definitions, too. 385 unsigned StoredPosition = Tokens->getPosition(); 386 FormatToken *Tok = FormatTok; 387 const FormatToken *PrevTok = Tok->Previous; 388 // Keep a stack of positions of lbrace tokens. We will 389 // update information about whether an lbrace starts a 390 // braced init list or a different block during the loop. 391 SmallVector<FormatToken *, 8> LBraceStack; 392 assert(Tok->Tok.is(tok::l_brace)); 393 do { 394 // Get next non-comment token. 395 FormatToken *NextTok; 396 unsigned ReadTokens = 0; 397 do { 398 NextTok = Tokens->getNextToken(); 399 ++ReadTokens; 400 } while (NextTok->is(tok::comment)); 401 402 switch (Tok->Tok.getKind()) { 403 case tok::l_brace: 404 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) { 405 if (PrevTok->isOneOf(tok::colon, tok::less)) 406 // A ':' indicates this code is in a type, or a braced list 407 // following a label in an object literal ({a: {b: 1}}). 408 // A '<' could be an object used in a comparison, but that is nonsense 409 // code (can never return true), so more likely it is a generic type 410 // argument (`X<{a: string; b: number}>`). 411 // The code below could be confused by semicolons between the 412 // individual members in a type member list, which would normally 413 // trigger BK_Block. In both cases, this must be parsed as an inline 414 // braced init. 415 Tok->BlockKind = BK_BracedInit; 416 else if (PrevTok->is(tok::r_paren)) 417 // `) { }` can only occur in function or method declarations in JS. 418 Tok->BlockKind = BK_Block; 419 } else { 420 Tok->BlockKind = BK_Unknown; 421 } 422 LBraceStack.push_back(Tok); 423 break; 424 case tok::r_brace: 425 if (LBraceStack.empty()) 426 break; 427 if (LBraceStack.back()->BlockKind == BK_Unknown) { 428 bool ProbablyBracedList = false; 429 if (Style.Language == FormatStyle::LK_Proto) { 430 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square); 431 } else { 432 // Using OriginalColumn to distinguish between ObjC methods and 433 // binary operators is a bit hacky. 434 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) && 435 NextTok->OriginalColumn == 0; 436 437 // If there is a comma, semicolon or right paren after the closing 438 // brace, we assume this is a braced initializer list. Note that 439 // regardless how we mark inner braces here, we will overwrite the 440 // BlockKind later if we parse a braced list (where all blocks 441 // inside are by default braced lists), or when we explicitly detect 442 // blocks (for example while parsing lambdas). 443 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a 444 // braced list in JS. 445 ProbablyBracedList = 446 (Style.Language == FormatStyle::LK_JavaScript && 447 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in, 448 Keywords.kw_as)) || 449 (Style.isCpp() && NextTok->is(tok::l_paren)) || 450 NextTok->isOneOf(tok::comma, tok::period, tok::colon, 451 tok::r_paren, tok::r_square, tok::l_brace, 452 tok::l_square, tok::ellipsis) || 453 (NextTok->is(tok::identifier) && 454 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) || 455 (NextTok->is(tok::semi) && 456 (!ExpectClassBody || LBraceStack.size() != 1)) || 457 (NextTok->isBinaryOperator() && !NextIsObjCMethod); 458 } 459 if (ProbablyBracedList) { 460 Tok->BlockKind = BK_BracedInit; 461 LBraceStack.back()->BlockKind = BK_BracedInit; 462 } else { 463 Tok->BlockKind = BK_Block; 464 LBraceStack.back()->BlockKind = BK_Block; 465 } 466 } 467 LBraceStack.pop_back(); 468 break; 469 case tok::at: 470 case tok::semi: 471 case tok::kw_if: 472 case tok::kw_while: 473 case tok::kw_for: 474 case tok::kw_switch: 475 case tok::kw_try: 476 case tok::kw___try: 477 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown) 478 LBraceStack.back()->BlockKind = BK_Block; 479 break; 480 default: 481 break; 482 } 483 PrevTok = Tok; 484 Tok = NextTok; 485 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty()); 486 487 // Assume other blocks for all unclosed opening braces. 488 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { 489 if (LBraceStack[i]->BlockKind == BK_Unknown) 490 LBraceStack[i]->BlockKind = BK_Block; 491 } 492 493 FormatTok = Tokens->setPosition(StoredPosition); 494 } 495 496 template <class T> 497 static inline void hash_combine(std::size_t &seed, const T &v) { 498 std::hash<T> hasher; 499 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); 500 } 501 502 size_t UnwrappedLineParser::computePPHash() const { 503 size_t h = 0; 504 for (const auto &i : PPStack) { 505 hash_combine(h, size_t(i.Kind)); 506 hash_combine(h, i.Line); 507 } 508 return h; 509 } 510 511 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, 512 bool MunchSemi) { 513 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) && 514 "'{' or macro block token expected"); 515 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin); 516 FormatTok->BlockKind = BK_Block; 517 518 size_t PPStartHash = computePPHash(); 519 520 unsigned InitialLevel = Line->Level; 521 nextToken(/*LevelDifference=*/AddLevel ? 1 : 0); 522 523 if (MacroBlock && FormatTok->is(tok::l_paren)) 524 parseParens(); 525 526 size_t NbPreprocessorDirectives = 527 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0; 528 addUnwrappedLine(); 529 size_t OpeningLineIndex = 530 CurrentLines->empty() 531 ? (UnwrappedLine::kInvalidIndex) 532 : (CurrentLines->size() - 1 - NbPreprocessorDirectives); 533 534 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 535 MustBeDeclaration); 536 if (AddLevel) 537 ++Line->Level; 538 parseLevel(/*HasOpeningBrace=*/true); 539 540 if (eof()) 541 return; 542 543 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd) 544 : !FormatTok->is(tok::r_brace)) { 545 Line->Level = InitialLevel; 546 FormatTok->BlockKind = BK_Block; 547 return; 548 } 549 550 size_t PPEndHash = computePPHash(); 551 552 // Munch the closing brace. 553 nextToken(/*LevelDifference=*/AddLevel ? -1 : 0); 554 555 if (MacroBlock && FormatTok->is(tok::l_paren)) 556 parseParens(); 557 558 if (MunchSemi && FormatTok->Tok.is(tok::semi)) 559 nextToken(); 560 Line->Level = InitialLevel; 561 562 if (PPStartHash == PPEndHash) { 563 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex; 564 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) { 565 // Update the opening line to add the forward reference as well 566 (*CurrentLines)[OpeningLineIndex].MatchingOpeningBlockLineIndex = 567 CurrentLines->size() - 1; 568 } 569 } 570 } 571 572 static bool isGoogScope(const UnwrappedLine &Line) { 573 // FIXME: Closure-library specific stuff should not be hard-coded but be 574 // configurable. 575 if (Line.Tokens.size() < 4) 576 return false; 577 auto I = Line.Tokens.begin(); 578 if (I->Tok->TokenText != "goog") 579 return false; 580 ++I; 581 if (I->Tok->isNot(tok::period)) 582 return false; 583 ++I; 584 if (I->Tok->TokenText != "scope") 585 return false; 586 ++I; 587 return I->Tok->is(tok::l_paren); 588 } 589 590 static bool isIIFE(const UnwrappedLine &Line, 591 const AdditionalKeywords &Keywords) { 592 // Look for the start of an immediately invoked anonymous function. 593 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression 594 // This is commonly done in JavaScript to create a new, anonymous scope. 595 // Example: (function() { ... })() 596 if (Line.Tokens.size() < 3) 597 return false; 598 auto I = Line.Tokens.begin(); 599 if (I->Tok->isNot(tok::l_paren)) 600 return false; 601 ++I; 602 if (I->Tok->isNot(Keywords.kw_function)) 603 return false; 604 ++I; 605 return I->Tok->is(tok::l_paren); 606 } 607 608 static bool ShouldBreakBeforeBrace(const FormatStyle &Style, 609 const FormatToken &InitialToken) { 610 if (InitialToken.is(tok::kw_namespace)) 611 return Style.BraceWrapping.AfterNamespace; 612 if (InitialToken.is(tok::kw_class)) 613 return Style.BraceWrapping.AfterClass; 614 if (InitialToken.is(tok::kw_union)) 615 return Style.BraceWrapping.AfterUnion; 616 if (InitialToken.is(tok::kw_struct)) 617 return Style.BraceWrapping.AfterStruct; 618 return false; 619 } 620 621 void UnwrappedLineParser::parseChildBlock() { 622 FormatTok->BlockKind = BK_Block; 623 nextToken(); 624 { 625 bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript && 626 (isGoogScope(*Line) || isIIFE(*Line, Keywords))); 627 ScopedLineState LineState(*this); 628 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 629 /*MustBeDeclaration=*/false); 630 Line->Level += SkipIndent ? 0 : 1; 631 parseLevel(/*HasOpeningBrace=*/true); 632 flushComments(isOnNewLine(*FormatTok)); 633 Line->Level -= SkipIndent ? 0 : 1; 634 } 635 nextToken(); 636 } 637 638 void UnwrappedLineParser::parsePPDirective() { 639 assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); 640 ScopedMacroState MacroState(*Line, Tokens, FormatTok); 641 nextToken(); 642 643 if (!FormatTok->Tok.getIdentifierInfo()) { 644 parsePPUnknown(); 645 return; 646 } 647 648 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { 649 case tok::pp_define: 650 parsePPDefine(); 651 return; 652 case tok::pp_if: 653 parsePPIf(/*IfDef=*/false); 654 break; 655 case tok::pp_ifdef: 656 case tok::pp_ifndef: 657 parsePPIf(/*IfDef=*/true); 658 break; 659 case tok::pp_else: 660 parsePPElse(); 661 break; 662 case tok::pp_elif: 663 parsePPElIf(); 664 break; 665 case tok::pp_endif: 666 parsePPEndIf(); 667 break; 668 default: 669 parsePPUnknown(); 670 break; 671 } 672 } 673 674 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { 675 size_t Line = CurrentLines->size(); 676 if (CurrentLines == &PreprocessorDirectives) 677 Line += Lines.size(); 678 679 if (Unreachable || 680 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable)) 681 PPStack.push_back({PP_Unreachable, Line}); 682 else 683 PPStack.push_back({PP_Conditional, Line}); 684 } 685 686 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { 687 ++PPBranchLevel; 688 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); 689 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { 690 PPLevelBranchIndex.push_back(0); 691 PPLevelBranchCount.push_back(0); 692 } 693 PPChainBranchIndex.push(0); 694 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; 695 conditionalCompilationCondition(Unreachable || Skip); 696 } 697 698 void UnwrappedLineParser::conditionalCompilationAlternative() { 699 if (!PPStack.empty()) 700 PPStack.pop_back(); 701 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 702 if (!PPChainBranchIndex.empty()) 703 ++PPChainBranchIndex.top(); 704 conditionalCompilationCondition( 705 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && 706 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()); 707 } 708 709 void UnwrappedLineParser::conditionalCompilationEnd() { 710 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 711 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { 712 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { 713 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; 714 } 715 } 716 // Guard against #endif's without #if. 717 if (PPBranchLevel > -1) 718 --PPBranchLevel; 719 if (!PPChainBranchIndex.empty()) 720 PPChainBranchIndex.pop(); 721 if (!PPStack.empty()) 722 PPStack.pop_back(); 723 } 724 725 void UnwrappedLineParser::parsePPIf(bool IfDef) { 726 bool IfNDef = FormatTok->is(tok::pp_ifndef); 727 nextToken(); 728 bool Unreachable = false; 729 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0")) 730 Unreachable = true; 731 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG") 732 Unreachable = true; 733 conditionalCompilationStart(Unreachable); 734 FormatToken *IfCondition = FormatTok; 735 // If there's a #ifndef on the first line, and the only lines before it are 736 // comments, it could be an include guard. 737 bool MaybeIncludeGuard = IfNDef; 738 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) 739 for (auto &Line : Lines) { 740 if (!Line.Tokens.front().Tok->is(tok::comment)) { 741 MaybeIncludeGuard = false; 742 IncludeGuard = IG_Rejected; 743 break; 744 } 745 } 746 --PPBranchLevel; 747 parsePPUnknown(); 748 ++PPBranchLevel; 749 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) { 750 IncludeGuard = IG_IfNdefed; 751 IncludeGuardToken = IfCondition; 752 } 753 } 754 755 void UnwrappedLineParser::parsePPElse() { 756 // If a potential include guard has an #else, it's not an include guard. 757 if (IncludeGuard == IG_Defined && PPBranchLevel == 0) 758 IncludeGuard = IG_Rejected; 759 conditionalCompilationAlternative(); 760 if (PPBranchLevel > -1) 761 --PPBranchLevel; 762 parsePPUnknown(); 763 ++PPBranchLevel; 764 } 765 766 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } 767 768 void UnwrappedLineParser::parsePPEndIf() { 769 conditionalCompilationEnd(); 770 parsePPUnknown(); 771 // If the #endif of a potential include guard is the last thing in the file, 772 // then we found an include guard. 773 unsigned TokenPosition = Tokens->getPosition(); 774 FormatToken *PeekNext = AllTokens[TokenPosition]; 775 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 && 776 PeekNext->is(tok::eof) && 777 Style.IndentPPDirectives != FormatStyle::PPDIS_None) 778 IncludeGuard = IG_Found; 779 } 780 781 void UnwrappedLineParser::parsePPDefine() { 782 nextToken(); 783 784 if (FormatTok->Tok.getKind() != tok::identifier) { 785 IncludeGuard = IG_Rejected; 786 IncludeGuardToken = nullptr; 787 parsePPUnknown(); 788 return; 789 } 790 791 if (IncludeGuard == IG_IfNdefed && 792 IncludeGuardToken->TokenText == FormatTok->TokenText) { 793 IncludeGuard = IG_Defined; 794 IncludeGuardToken = nullptr; 795 for (auto &Line : Lines) { 796 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) { 797 IncludeGuard = IG_Rejected; 798 break; 799 } 800 } 801 } 802 803 nextToken(); 804 if (FormatTok->Tok.getKind() == tok::l_paren && 805 FormatTok->WhitespaceRange.getBegin() == 806 FormatTok->WhitespaceRange.getEnd()) { 807 parseParens(); 808 } 809 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash) 810 Line->Level += PPBranchLevel + 1; 811 addUnwrappedLine(); 812 ++Line->Level; 813 814 // Errors during a preprocessor directive can only affect the layout of the 815 // preprocessor directive, and thus we ignore them. An alternative approach 816 // would be to use the same approach we use on the file level (no 817 // re-indentation if there was a structural error) within the macro 818 // definition. 819 parseFile(); 820 } 821 822 void UnwrappedLineParser::parsePPUnknown() { 823 do { 824 nextToken(); 825 } while (!eof()); 826 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash) 827 Line->Level += PPBranchLevel + 1; 828 addUnwrappedLine(); 829 } 830 831 // Here we blacklist certain tokens that are not usually the first token in an 832 // unwrapped line. This is used in attempt to distinguish macro calls without 833 // trailing semicolons from other constructs split to several lines. 834 static bool tokenCanStartNewLine(const clang::Token &Tok) { 835 // Semicolon can be a null-statement, l_square can be a start of a macro or 836 // a C++11 attribute, but this doesn't seem to be common. 837 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && 838 Tok.isNot(tok::l_square) && 839 // Tokens that can only be used as binary operators and a part of 840 // overloaded operator names. 841 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && 842 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && 843 Tok.isNot(tok::less) && Tok.isNot(tok::greater) && 844 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && 845 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && 846 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && 847 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && 848 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && 849 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && 850 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && 851 Tok.isNot(tok::lesslessequal) && 852 // Colon is used in labels, base class lists, initializer lists, 853 // range-based for loops, ternary operator, but should never be the 854 // first token in an unwrapped line. 855 Tok.isNot(tok::colon) && 856 // 'noexcept' is a trailing annotation. 857 Tok.isNot(tok::kw_noexcept); 858 } 859 860 static bool mustBeJSIdent(const AdditionalKeywords &Keywords, 861 const FormatToken *FormatTok) { 862 // FIXME: This returns true for C/C++ keywords like 'struct'. 863 return FormatTok->is(tok::identifier) && 864 (FormatTok->Tok.getIdentifierInfo() == nullptr || 865 !FormatTok->isOneOf( 866 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async, 867 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally, 868 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is, 869 Keywords.kw_let, Keywords.kw_var, tok::kw_const, 870 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements, 871 Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws, 872 Keywords.kw_from)); 873 } 874 875 static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords, 876 const FormatToken *FormatTok) { 877 return FormatTok->Tok.isLiteral() || 878 FormatTok->isOneOf(tok::kw_true, tok::kw_false) || 879 mustBeJSIdent(Keywords, FormatTok); 880 } 881 882 // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement 883 // when encountered after a value (see mustBeJSIdentOrValue). 884 static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords, 885 const FormatToken *FormatTok) { 886 return FormatTok->isOneOf( 887 tok::kw_return, Keywords.kw_yield, 888 // conditionals 889 tok::kw_if, tok::kw_else, 890 // loops 891 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break, 892 // switch/case 893 tok::kw_switch, tok::kw_case, 894 // exceptions 895 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally, 896 // declaration 897 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let, 898 Keywords.kw_async, Keywords.kw_function, 899 // import/export 900 Keywords.kw_import, tok::kw_export); 901 } 902 903 // readTokenWithJavaScriptASI reads the next token and terminates the current 904 // line if JavaScript Automatic Semicolon Insertion must 905 // happen between the current token and the next token. 906 // 907 // This method is conservative - it cannot cover all edge cases of JavaScript, 908 // but only aims to correctly handle certain well known cases. It *must not* 909 // return true in speculative cases. 910 void UnwrappedLineParser::readTokenWithJavaScriptASI() { 911 FormatToken *Previous = FormatTok; 912 readToken(); 913 FormatToken *Next = FormatTok; 914 915 bool IsOnSameLine = 916 CommentsBeforeNextToken.empty() 917 ? Next->NewlinesBefore == 0 918 : CommentsBeforeNextToken.front()->NewlinesBefore == 0; 919 if (IsOnSameLine) 920 return; 921 922 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous); 923 bool PreviousStartsTemplateExpr = 924 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${"); 925 if (PreviousMustBeValue || Previous->is(tok::r_paren)) { 926 // If the line contains an '@' sign, the previous token might be an 927 // annotation, which can precede another identifier/value. 928 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(), 929 [](UnwrappedLineNode &LineNode) { 930 return LineNode.Tok->is(tok::at); 931 }) != Line->Tokens.end(); 932 if (HasAt) 933 return; 934 } 935 if (Next->is(tok::exclaim) && PreviousMustBeValue) 936 return addUnwrappedLine(); 937 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next); 938 bool NextEndsTemplateExpr = 939 Next->is(TT_TemplateString) && Next->TokenText.startswith("}"); 940 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr && 941 (PreviousMustBeValue || 942 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus, 943 tok::minusminus))) 944 return addUnwrappedLine(); 945 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) && 946 isJSDeclOrStmt(Keywords, Next)) 947 return addUnwrappedLine(); 948 } 949 950 void UnwrappedLineParser::parseStructuralElement() { 951 assert(!FormatTok->is(tok::l_brace)); 952 if (Style.Language == FormatStyle::LK_TableGen && 953 FormatTok->is(tok::pp_include)) { 954 nextToken(); 955 if (FormatTok->is(tok::string_literal)) 956 nextToken(); 957 addUnwrappedLine(); 958 return; 959 } 960 switch (FormatTok->Tok.getKind()) { 961 case tok::kw_asm: 962 nextToken(); 963 if (FormatTok->is(tok::l_brace)) { 964 FormatTok->Type = TT_InlineASMBrace; 965 nextToken(); 966 while (FormatTok && FormatTok->isNot(tok::eof)) { 967 if (FormatTok->is(tok::r_brace)) { 968 FormatTok->Type = TT_InlineASMBrace; 969 nextToken(); 970 addUnwrappedLine(); 971 break; 972 } 973 FormatTok->Finalized = true; 974 nextToken(); 975 } 976 } 977 break; 978 case tok::kw_namespace: 979 parseNamespace(); 980 return; 981 case tok::kw_inline: 982 nextToken(); 983 if (FormatTok->Tok.is(tok::kw_namespace)) { 984 parseNamespace(); 985 return; 986 } 987 break; 988 case tok::kw_public: 989 case tok::kw_protected: 990 case tok::kw_private: 991 if (Style.Language == FormatStyle::LK_Java || 992 Style.Language == FormatStyle::LK_JavaScript) 993 nextToken(); 994 else 995 parseAccessSpecifier(); 996 return; 997 case tok::kw_if: 998 parseIfThenElse(); 999 return; 1000 case tok::kw_for: 1001 case tok::kw_while: 1002 parseForOrWhileLoop(); 1003 return; 1004 case tok::kw_do: 1005 parseDoWhile(); 1006 return; 1007 case tok::kw_switch: 1008 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1009 // 'switch: string' field declaration. 1010 break; 1011 parseSwitch(); 1012 return; 1013 case tok::kw_default: 1014 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1015 // 'default: string' field declaration. 1016 break; 1017 nextToken(); 1018 if (FormatTok->is(tok::colon)) { 1019 parseLabel(); 1020 return; 1021 } 1022 // e.g. "default void f() {}" in a Java interface. 1023 break; 1024 case tok::kw_case: 1025 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1026 // 'case: string' field declaration. 1027 break; 1028 parseCaseLabel(); 1029 return; 1030 case tok::kw_try: 1031 case tok::kw___try: 1032 parseTryCatch(); 1033 return; 1034 case tok::kw_extern: 1035 nextToken(); 1036 if (FormatTok->Tok.is(tok::string_literal)) { 1037 nextToken(); 1038 if (FormatTok->Tok.is(tok::l_brace)) { 1039 if (Style.BraceWrapping.AfterExternBlock) { 1040 addUnwrappedLine(); 1041 parseBlock(/*MustBeDeclaration=*/true); 1042 } else { 1043 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); 1044 } 1045 addUnwrappedLine(); 1046 return; 1047 } 1048 } 1049 break; 1050 case tok::kw_export: 1051 if (Style.Language == FormatStyle::LK_JavaScript) { 1052 parseJavaScriptEs6ImportExport(); 1053 return; 1054 } 1055 break; 1056 case tok::identifier: 1057 if (FormatTok->is(TT_ForEachMacro)) { 1058 parseForOrWhileLoop(); 1059 return; 1060 } 1061 if (FormatTok->is(TT_MacroBlockBegin)) { 1062 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true, 1063 /*MunchSemi=*/false); 1064 return; 1065 } 1066 if (FormatTok->is(Keywords.kw_import)) { 1067 if (Style.Language == FormatStyle::LK_JavaScript) { 1068 parseJavaScriptEs6ImportExport(); 1069 return; 1070 } 1071 if (Style.Language == FormatStyle::LK_Proto) { 1072 nextToken(); 1073 if (FormatTok->is(tok::kw_public)) 1074 nextToken(); 1075 if (!FormatTok->is(tok::string_literal)) 1076 return; 1077 nextToken(); 1078 if (FormatTok->is(tok::semi)) 1079 nextToken(); 1080 addUnwrappedLine(); 1081 return; 1082 } 1083 } 1084 if (Style.isCpp() && 1085 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals, 1086 Keywords.kw_slots, Keywords.kw_qslots)) { 1087 nextToken(); 1088 if (FormatTok->is(tok::colon)) { 1089 nextToken(); 1090 addUnwrappedLine(); 1091 return; 1092 } 1093 } 1094 // In all other cases, parse the declaration. 1095 break; 1096 default: 1097 break; 1098 } 1099 do { 1100 const FormatToken *Previous = FormatTok->Previous; 1101 switch (FormatTok->Tok.getKind()) { 1102 case tok::at: 1103 nextToken(); 1104 if (FormatTok->Tok.is(tok::l_brace)) { 1105 nextToken(); 1106 parseBracedList(); 1107 break; 1108 } 1109 switch (FormatTok->Tok.getObjCKeywordID()) { 1110 case tok::objc_public: 1111 case tok::objc_protected: 1112 case tok::objc_package: 1113 case tok::objc_private: 1114 return parseAccessSpecifier(); 1115 case tok::objc_interface: 1116 case tok::objc_implementation: 1117 return parseObjCInterfaceOrImplementation(); 1118 case tok::objc_protocol: 1119 if (parseObjCProtocol()) 1120 return; 1121 break; 1122 case tok::objc_end: 1123 return; // Handled by the caller. 1124 case tok::objc_optional: 1125 case tok::objc_required: 1126 nextToken(); 1127 addUnwrappedLine(); 1128 return; 1129 case tok::objc_autoreleasepool: 1130 nextToken(); 1131 if (FormatTok->Tok.is(tok::l_brace)) { 1132 if (Style.BraceWrapping.AfterObjCDeclaration) 1133 addUnwrappedLine(); 1134 parseBlock(/*MustBeDeclaration=*/false); 1135 } 1136 addUnwrappedLine(); 1137 return; 1138 case tok::objc_try: 1139 // This branch isn't strictly necessary (the kw_try case below would 1140 // do this too after the tok::at is parsed above). But be explicit. 1141 parseTryCatch(); 1142 return; 1143 default: 1144 break; 1145 } 1146 break; 1147 case tok::kw_enum: 1148 // Ignore if this is part of "template <enum ...". 1149 if (Previous && Previous->is(tok::less)) { 1150 nextToken(); 1151 break; 1152 } 1153 1154 // parseEnum falls through and does not yet add an unwrapped line as an 1155 // enum definition can start a structural element. 1156 if (!parseEnum()) 1157 break; 1158 // This only applies for C++. 1159 if (!Style.isCpp()) { 1160 addUnwrappedLine(); 1161 return; 1162 } 1163 break; 1164 case tok::kw_typedef: 1165 nextToken(); 1166 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS, 1167 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS)) 1168 parseEnum(); 1169 break; 1170 case tok::kw_struct: 1171 case tok::kw_union: 1172 case tok::kw_class: 1173 // parseRecord falls through and does not yet add an unwrapped line as a 1174 // record declaration or definition can start a structural element. 1175 parseRecord(); 1176 // This does not apply for Java and JavaScript. 1177 if (Style.Language == FormatStyle::LK_Java || 1178 Style.Language == FormatStyle::LK_JavaScript) { 1179 if (FormatTok->is(tok::semi)) 1180 nextToken(); 1181 addUnwrappedLine(); 1182 return; 1183 } 1184 break; 1185 case tok::period: 1186 nextToken(); 1187 // In Java, classes have an implicit static member "class". 1188 if (Style.Language == FormatStyle::LK_Java && FormatTok && 1189 FormatTok->is(tok::kw_class)) 1190 nextToken(); 1191 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok && 1192 FormatTok->Tok.getIdentifierInfo()) 1193 // JavaScript only has pseudo keywords, all keywords are allowed to 1194 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6 1195 nextToken(); 1196 break; 1197 case tok::semi: 1198 nextToken(); 1199 addUnwrappedLine(); 1200 return; 1201 case tok::r_brace: 1202 addUnwrappedLine(); 1203 return; 1204 case tok::l_paren: 1205 parseParens(); 1206 break; 1207 case tok::kw_operator: 1208 nextToken(); 1209 if (FormatTok->isBinaryOperator()) 1210 nextToken(); 1211 break; 1212 case tok::caret: 1213 nextToken(); 1214 if (FormatTok->Tok.isAnyIdentifier() || 1215 FormatTok->isSimpleTypeSpecifier()) 1216 nextToken(); 1217 if (FormatTok->is(tok::l_paren)) 1218 parseParens(); 1219 if (FormatTok->is(tok::l_brace)) 1220 parseChildBlock(); 1221 break; 1222 case tok::l_brace: 1223 if (!tryToParseBracedList()) { 1224 // A block outside of parentheses must be the last part of a 1225 // structural element. 1226 // FIXME: Figure out cases where this is not true, and add projections 1227 // for them (the one we know is missing are lambdas). 1228 if (Style.BraceWrapping.AfterFunction) 1229 addUnwrappedLine(); 1230 FormatTok->Type = TT_FunctionLBrace; 1231 parseBlock(/*MustBeDeclaration=*/false); 1232 addUnwrappedLine(); 1233 return; 1234 } 1235 // Otherwise this was a braced init list, and the structural 1236 // element continues. 1237 break; 1238 case tok::kw_try: 1239 // We arrive here when parsing function-try blocks. 1240 parseTryCatch(); 1241 return; 1242 case tok::identifier: { 1243 if (FormatTok->is(TT_MacroBlockEnd)) { 1244 addUnwrappedLine(); 1245 return; 1246 } 1247 1248 // Function declarations (as opposed to function expressions) are parsed 1249 // on their own unwrapped line by continuing this loop. Function 1250 // expressions (functions that are not on their own line) must not create 1251 // a new unwrapped line, so they are special cased below. 1252 size_t TokenCount = Line->Tokens.size(); 1253 if (Style.Language == FormatStyle::LK_JavaScript && 1254 FormatTok->is(Keywords.kw_function) && 1255 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is( 1256 Keywords.kw_async)))) { 1257 tryToParseJSFunction(); 1258 break; 1259 } 1260 if ((Style.Language == FormatStyle::LK_JavaScript || 1261 Style.Language == FormatStyle::LK_Java) && 1262 FormatTok->is(Keywords.kw_interface)) { 1263 if (Style.Language == FormatStyle::LK_JavaScript) { 1264 // In JavaScript/TypeScript, "interface" can be used as a standalone 1265 // identifier, e.g. in `var interface = 1;`. If "interface" is 1266 // followed by another identifier, it is very like to be an actual 1267 // interface declaration. 1268 unsigned StoredPosition = Tokens->getPosition(); 1269 FormatToken *Next = Tokens->getNextToken(); 1270 FormatTok = Tokens->setPosition(StoredPosition); 1271 if (Next && !mustBeJSIdent(Keywords, Next)) { 1272 nextToken(); 1273 break; 1274 } 1275 } 1276 parseRecord(); 1277 addUnwrappedLine(); 1278 return; 1279 } 1280 1281 // See if the following token should start a new unwrapped line. 1282 StringRef Text = FormatTok->TokenText; 1283 nextToken(); 1284 if (Line->Tokens.size() == 1 && 1285 // JS doesn't have macros, and within classes colons indicate fields, 1286 // not labels. 1287 Style.Language != FormatStyle::LK_JavaScript) { 1288 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) { 1289 Line->Tokens.begin()->Tok->MustBreakBefore = true; 1290 parseLabel(); 1291 return; 1292 } 1293 // Recognize function-like macro usages without trailing semicolon as 1294 // well as free-standing macros like Q_OBJECT. 1295 bool FunctionLike = FormatTok->is(tok::l_paren); 1296 if (FunctionLike) 1297 parseParens(); 1298 1299 bool FollowedByNewline = 1300 CommentsBeforeNextToken.empty() 1301 ? FormatTok->NewlinesBefore > 0 1302 : CommentsBeforeNextToken.front()->NewlinesBefore > 0; 1303 1304 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && 1305 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { 1306 addUnwrappedLine(); 1307 return; 1308 } 1309 } 1310 break; 1311 } 1312 case tok::equal: 1313 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType 1314 // TT_JsFatArrow. The always start an expression or a child block if 1315 // followed by a curly. 1316 if (FormatTok->is(TT_JsFatArrow)) { 1317 nextToken(); 1318 if (FormatTok->is(tok::l_brace)) 1319 parseChildBlock(); 1320 break; 1321 } 1322 1323 nextToken(); 1324 if (FormatTok->Tok.is(tok::l_brace)) { 1325 nextToken(); 1326 parseBracedList(); 1327 } else if (Style.Language == FormatStyle::LK_Proto && 1328 FormatTok->Tok.is(tok::less)) { 1329 nextToken(); 1330 parseBracedList(/*ContinueOnSemicolons=*/false, 1331 /*ClosingBraceKind=*/tok::greater); 1332 } 1333 break; 1334 case tok::l_square: 1335 parseSquare(); 1336 break; 1337 case tok::kw_new: 1338 parseNew(); 1339 break; 1340 default: 1341 nextToken(); 1342 break; 1343 } 1344 } while (!eof()); 1345 } 1346 1347 bool UnwrappedLineParser::tryToParseLambda() { 1348 if (!Style.isCpp()) { 1349 nextToken(); 1350 return false; 1351 } 1352 assert(FormatTok->is(tok::l_square)); 1353 FormatToken &LSquare = *FormatTok; 1354 if (!tryToParseLambdaIntroducer()) 1355 return false; 1356 1357 while (FormatTok->isNot(tok::l_brace)) { 1358 if (FormatTok->isSimpleTypeSpecifier()) { 1359 nextToken(); 1360 continue; 1361 } 1362 switch (FormatTok->Tok.getKind()) { 1363 case tok::l_brace: 1364 break; 1365 case tok::l_paren: 1366 parseParens(); 1367 break; 1368 case tok::amp: 1369 case tok::star: 1370 case tok::kw_const: 1371 case tok::comma: 1372 case tok::less: 1373 case tok::greater: 1374 case tok::identifier: 1375 case tok::numeric_constant: 1376 case tok::coloncolon: 1377 case tok::kw_mutable: 1378 nextToken(); 1379 break; 1380 case tok::arrow: 1381 FormatTok->Type = TT_LambdaArrow; 1382 nextToken(); 1383 break; 1384 default: 1385 return true; 1386 } 1387 } 1388 LSquare.Type = TT_LambdaLSquare; 1389 parseChildBlock(); 1390 return true; 1391 } 1392 1393 bool UnwrappedLineParser::tryToParseLambdaIntroducer() { 1394 const FormatToken *Previous = FormatTok->Previous; 1395 if (Previous && 1396 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new, 1397 tok::kw_delete) || 1398 FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() || 1399 Previous->isSimpleTypeSpecifier())) { 1400 nextToken(); 1401 return false; 1402 } 1403 nextToken(); 1404 parseSquare(/*LambdaIntroducer=*/true); 1405 return true; 1406 } 1407 1408 void UnwrappedLineParser::tryToParseJSFunction() { 1409 assert(FormatTok->is(Keywords.kw_function) || 1410 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)); 1411 if (FormatTok->is(Keywords.kw_async)) 1412 nextToken(); 1413 // Consume "function". 1414 nextToken(); 1415 1416 // Consume * (generator function). Treat it like C++'s overloaded operators. 1417 if (FormatTok->is(tok::star)) { 1418 FormatTok->Type = TT_OverloadedOperator; 1419 nextToken(); 1420 } 1421 1422 // Consume function name. 1423 if (FormatTok->is(tok::identifier)) 1424 nextToken(); 1425 1426 if (FormatTok->isNot(tok::l_paren)) 1427 return; 1428 1429 // Parse formal parameter list. 1430 parseParens(); 1431 1432 if (FormatTok->is(tok::colon)) { 1433 // Parse a type definition. 1434 nextToken(); 1435 1436 // Eat the type declaration. For braced inline object types, balance braces, 1437 // otherwise just parse until finding an l_brace for the function body. 1438 if (FormatTok->is(tok::l_brace)) 1439 tryToParseBracedList(); 1440 else 1441 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof()) 1442 nextToken(); 1443 } 1444 1445 if (FormatTok->is(tok::semi)) 1446 return; 1447 1448 parseChildBlock(); 1449 } 1450 1451 bool UnwrappedLineParser::tryToParseBracedList() { 1452 if (FormatTok->BlockKind == BK_Unknown) 1453 calculateBraceTypes(); 1454 assert(FormatTok->BlockKind != BK_Unknown); 1455 if (FormatTok->BlockKind == BK_Block) 1456 return false; 1457 nextToken(); 1458 parseBracedList(); 1459 return true; 1460 } 1461 1462 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons, 1463 tok::TokenKind ClosingBraceKind) { 1464 bool HasError = false; 1465 1466 // FIXME: Once we have an expression parser in the UnwrappedLineParser, 1467 // replace this by using parseAssigmentExpression() inside. 1468 do { 1469 if (Style.Language == FormatStyle::LK_JavaScript) { 1470 if (FormatTok->is(Keywords.kw_function) || 1471 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) { 1472 tryToParseJSFunction(); 1473 continue; 1474 } 1475 if (FormatTok->is(TT_JsFatArrow)) { 1476 nextToken(); 1477 // Fat arrows can be followed by simple expressions or by child blocks 1478 // in curly braces. 1479 if (FormatTok->is(tok::l_brace)) { 1480 parseChildBlock(); 1481 continue; 1482 } 1483 } 1484 if (FormatTok->is(tok::l_brace)) { 1485 // Could be a method inside of a braced list `{a() { return 1; }}`. 1486 if (tryToParseBracedList()) 1487 continue; 1488 parseChildBlock(); 1489 } 1490 } 1491 if (FormatTok->Tok.getKind() == ClosingBraceKind) { 1492 nextToken(); 1493 return !HasError; 1494 } 1495 switch (FormatTok->Tok.getKind()) { 1496 case tok::caret: 1497 nextToken(); 1498 if (FormatTok->is(tok::l_brace)) { 1499 parseChildBlock(); 1500 } 1501 break; 1502 case tok::l_square: 1503 tryToParseLambda(); 1504 break; 1505 case tok::l_paren: 1506 parseParens(); 1507 // JavaScript can just have free standing methods and getters/setters in 1508 // object literals. Detect them by a "{" following ")". 1509 if (Style.Language == FormatStyle::LK_JavaScript) { 1510 if (FormatTok->is(tok::l_brace)) 1511 parseChildBlock(); 1512 break; 1513 } 1514 break; 1515 case tok::l_brace: 1516 // Assume there are no blocks inside a braced init list apart 1517 // from the ones we explicitly parse out (like lambdas). 1518 FormatTok->BlockKind = BK_BracedInit; 1519 nextToken(); 1520 parseBracedList(); 1521 break; 1522 case tok::less: 1523 if (Style.Language == FormatStyle::LK_Proto) { 1524 nextToken(); 1525 parseBracedList(/*ContinueOnSemicolons=*/false, 1526 /*ClosingBraceKind=*/tok::greater); 1527 } else { 1528 nextToken(); 1529 } 1530 break; 1531 case tok::semi: 1532 // JavaScript (or more precisely TypeScript) can have semicolons in braced 1533 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be 1534 // used for error recovery if we have otherwise determined that this is 1535 // a braced list. 1536 if (Style.Language == FormatStyle::LK_JavaScript) { 1537 nextToken(); 1538 break; 1539 } 1540 HasError = true; 1541 if (!ContinueOnSemicolons) 1542 return !HasError; 1543 nextToken(); 1544 break; 1545 case tok::comma: 1546 nextToken(); 1547 break; 1548 default: 1549 nextToken(); 1550 break; 1551 } 1552 } while (!eof()); 1553 return false; 1554 } 1555 1556 void UnwrappedLineParser::parseParens() { 1557 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); 1558 nextToken(); 1559 do { 1560 switch (FormatTok->Tok.getKind()) { 1561 case tok::l_paren: 1562 parseParens(); 1563 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace)) 1564 parseChildBlock(); 1565 break; 1566 case tok::r_paren: 1567 nextToken(); 1568 return; 1569 case tok::r_brace: 1570 // A "}" inside parenthesis is an error if there wasn't a matching "{". 1571 return; 1572 case tok::l_square: 1573 tryToParseLambda(); 1574 break; 1575 case tok::l_brace: 1576 if (!tryToParseBracedList()) 1577 parseChildBlock(); 1578 break; 1579 case tok::at: 1580 nextToken(); 1581 if (FormatTok->Tok.is(tok::l_brace)) { 1582 nextToken(); 1583 parseBracedList(); 1584 } 1585 break; 1586 case tok::kw_class: 1587 if (Style.Language == FormatStyle::LK_JavaScript) 1588 parseRecord(/*ParseAsExpr=*/true); 1589 else 1590 nextToken(); 1591 break; 1592 case tok::identifier: 1593 if (Style.Language == FormatStyle::LK_JavaScript && 1594 (FormatTok->is(Keywords.kw_function) || 1595 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function))) 1596 tryToParseJSFunction(); 1597 else 1598 nextToken(); 1599 break; 1600 default: 1601 nextToken(); 1602 break; 1603 } 1604 } while (!eof()); 1605 } 1606 1607 void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) { 1608 if (!LambdaIntroducer) { 1609 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); 1610 if (tryToParseLambda()) 1611 return; 1612 } 1613 do { 1614 switch (FormatTok->Tok.getKind()) { 1615 case tok::l_paren: 1616 parseParens(); 1617 break; 1618 case tok::r_square: 1619 nextToken(); 1620 return; 1621 case tok::r_brace: 1622 // A "}" inside parenthesis is an error if there wasn't a matching "{". 1623 return; 1624 case tok::l_square: 1625 parseSquare(); 1626 break; 1627 case tok::l_brace: { 1628 if (!tryToParseBracedList()) 1629 parseChildBlock(); 1630 break; 1631 } 1632 case tok::at: 1633 nextToken(); 1634 if (FormatTok->Tok.is(tok::l_brace)) { 1635 nextToken(); 1636 parseBracedList(); 1637 } 1638 break; 1639 default: 1640 nextToken(); 1641 break; 1642 } 1643 } while (!eof()); 1644 } 1645 1646 void UnwrappedLineParser::parseIfThenElse() { 1647 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); 1648 nextToken(); 1649 if (FormatTok->Tok.is(tok::kw_constexpr)) 1650 nextToken(); 1651 if (FormatTok->Tok.is(tok::l_paren)) 1652 parseParens(); 1653 bool NeedsUnwrappedLine = false; 1654 if (FormatTok->Tok.is(tok::l_brace)) { 1655 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1656 parseBlock(/*MustBeDeclaration=*/false); 1657 if (Style.BraceWrapping.BeforeElse) 1658 addUnwrappedLine(); 1659 else 1660 NeedsUnwrappedLine = true; 1661 } else { 1662 addUnwrappedLine(); 1663 ++Line->Level; 1664 parseStructuralElement(); 1665 --Line->Level; 1666 } 1667 if (FormatTok->Tok.is(tok::kw_else)) { 1668 nextToken(); 1669 if (FormatTok->Tok.is(tok::l_brace)) { 1670 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1671 parseBlock(/*MustBeDeclaration=*/false); 1672 addUnwrappedLine(); 1673 } else if (FormatTok->Tok.is(tok::kw_if)) { 1674 parseIfThenElse(); 1675 } else { 1676 addUnwrappedLine(); 1677 ++Line->Level; 1678 parseStructuralElement(); 1679 if (FormatTok->is(tok::eof)) 1680 addUnwrappedLine(); 1681 --Line->Level; 1682 } 1683 } else if (NeedsUnwrappedLine) { 1684 addUnwrappedLine(); 1685 } 1686 } 1687 1688 void UnwrappedLineParser::parseTryCatch() { 1689 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected"); 1690 nextToken(); 1691 bool NeedsUnwrappedLine = false; 1692 if (FormatTok->is(tok::colon)) { 1693 // We are in a function try block, what comes is an initializer list. 1694 nextToken(); 1695 while (FormatTok->is(tok::identifier)) { 1696 nextToken(); 1697 if (FormatTok->is(tok::l_paren)) 1698 parseParens(); 1699 if (FormatTok->is(tok::comma)) 1700 nextToken(); 1701 } 1702 } 1703 // Parse try with resource. 1704 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) { 1705 parseParens(); 1706 } 1707 if (FormatTok->is(tok::l_brace)) { 1708 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1709 parseBlock(/*MustBeDeclaration=*/false); 1710 if (Style.BraceWrapping.BeforeCatch) { 1711 addUnwrappedLine(); 1712 } else { 1713 NeedsUnwrappedLine = true; 1714 } 1715 } else if (!FormatTok->is(tok::kw_catch)) { 1716 // The C++ standard requires a compound-statement after a try. 1717 // If there's none, we try to assume there's a structuralElement 1718 // and try to continue. 1719 addUnwrappedLine(); 1720 ++Line->Level; 1721 parseStructuralElement(); 1722 --Line->Level; 1723 } 1724 while (1) { 1725 if (FormatTok->is(tok::at)) 1726 nextToken(); 1727 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, 1728 tok::kw___finally) || 1729 ((Style.Language == FormatStyle::LK_Java || 1730 Style.Language == FormatStyle::LK_JavaScript) && 1731 FormatTok->is(Keywords.kw_finally)) || 1732 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || 1733 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) 1734 break; 1735 nextToken(); 1736 while (FormatTok->isNot(tok::l_brace)) { 1737 if (FormatTok->is(tok::l_paren)) { 1738 parseParens(); 1739 continue; 1740 } 1741 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) 1742 return; 1743 nextToken(); 1744 } 1745 NeedsUnwrappedLine = false; 1746 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1747 parseBlock(/*MustBeDeclaration=*/false); 1748 if (Style.BraceWrapping.BeforeCatch) 1749 addUnwrappedLine(); 1750 else 1751 NeedsUnwrappedLine = true; 1752 } 1753 if (NeedsUnwrappedLine) 1754 addUnwrappedLine(); 1755 } 1756 1757 void UnwrappedLineParser::parseNamespace() { 1758 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); 1759 1760 const FormatToken &InitialToken = *FormatTok; 1761 nextToken(); 1762 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon)) 1763 nextToken(); 1764 if (FormatTok->Tok.is(tok::l_brace)) { 1765 if (ShouldBreakBeforeBrace(Style, InitialToken)) 1766 addUnwrappedLine(); 1767 1768 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || 1769 (Style.NamespaceIndentation == FormatStyle::NI_Inner && 1770 DeclarationScopeStack.size() > 1); 1771 parseBlock(/*MustBeDeclaration=*/true, AddLevel); 1772 // Munch the semicolon after a namespace. This is more common than one would 1773 // think. Puttin the semicolon into its own line is very ugly. 1774 if (FormatTok->Tok.is(tok::semi)) 1775 nextToken(); 1776 addUnwrappedLine(); 1777 } 1778 // FIXME: Add error handling. 1779 } 1780 1781 void UnwrappedLineParser::parseNew() { 1782 assert(FormatTok->is(tok::kw_new) && "'new' expected"); 1783 nextToken(); 1784 if (Style.Language != FormatStyle::LK_Java) 1785 return; 1786 1787 // In Java, we can parse everything up to the parens, which aren't optional. 1788 do { 1789 // There should not be a ;, { or } before the new's open paren. 1790 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace)) 1791 return; 1792 1793 // Consume the parens. 1794 if (FormatTok->is(tok::l_paren)) { 1795 parseParens(); 1796 1797 // If there is a class body of an anonymous class, consume that as child. 1798 if (FormatTok->is(tok::l_brace)) 1799 parseChildBlock(); 1800 return; 1801 } 1802 nextToken(); 1803 } while (!eof()); 1804 } 1805 1806 void UnwrappedLineParser::parseForOrWhileLoop() { 1807 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && 1808 "'for', 'while' or foreach macro expected"); 1809 nextToken(); 1810 // JS' for await ( ... 1811 if (Style.Language == FormatStyle::LK_JavaScript && 1812 FormatTok->is(Keywords.kw_await)) 1813 nextToken(); 1814 if (FormatTok->Tok.is(tok::l_paren)) 1815 parseParens(); 1816 if (FormatTok->Tok.is(tok::l_brace)) { 1817 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1818 parseBlock(/*MustBeDeclaration=*/false); 1819 addUnwrappedLine(); 1820 } else { 1821 addUnwrappedLine(); 1822 ++Line->Level; 1823 parseStructuralElement(); 1824 --Line->Level; 1825 } 1826 } 1827 1828 void UnwrappedLineParser::parseDoWhile() { 1829 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); 1830 nextToken(); 1831 if (FormatTok->Tok.is(tok::l_brace)) { 1832 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1833 parseBlock(/*MustBeDeclaration=*/false); 1834 if (Style.BraceWrapping.IndentBraces) 1835 addUnwrappedLine(); 1836 } else { 1837 addUnwrappedLine(); 1838 ++Line->Level; 1839 parseStructuralElement(); 1840 --Line->Level; 1841 } 1842 1843 // FIXME: Add error handling. 1844 if (!FormatTok->Tok.is(tok::kw_while)) { 1845 addUnwrappedLine(); 1846 return; 1847 } 1848 1849 nextToken(); 1850 parseStructuralElement(); 1851 } 1852 1853 void UnwrappedLineParser::parseLabel() { 1854 nextToken(); 1855 unsigned OldLineLevel = Line->Level; 1856 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) 1857 --Line->Level; 1858 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { 1859 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1860 parseBlock(/*MustBeDeclaration=*/false); 1861 if (FormatTok->Tok.is(tok::kw_break)) { 1862 if (Style.BraceWrapping.AfterControlStatement) 1863 addUnwrappedLine(); 1864 parseStructuralElement(); 1865 } 1866 addUnwrappedLine(); 1867 } else { 1868 if (FormatTok->is(tok::semi)) 1869 nextToken(); 1870 addUnwrappedLine(); 1871 } 1872 Line->Level = OldLineLevel; 1873 if (FormatTok->isNot(tok::l_brace)) { 1874 parseStructuralElement(); 1875 addUnwrappedLine(); 1876 } 1877 } 1878 1879 void UnwrappedLineParser::parseCaseLabel() { 1880 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); 1881 // FIXME: fix handling of complex expressions here. 1882 do { 1883 nextToken(); 1884 } while (!eof() && !FormatTok->Tok.is(tok::colon)); 1885 parseLabel(); 1886 } 1887 1888 void UnwrappedLineParser::parseSwitch() { 1889 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); 1890 nextToken(); 1891 if (FormatTok->Tok.is(tok::l_paren)) 1892 parseParens(); 1893 if (FormatTok->Tok.is(tok::l_brace)) { 1894 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1895 parseBlock(/*MustBeDeclaration=*/false); 1896 addUnwrappedLine(); 1897 } else { 1898 addUnwrappedLine(); 1899 ++Line->Level; 1900 parseStructuralElement(); 1901 --Line->Level; 1902 } 1903 } 1904 1905 void UnwrappedLineParser::parseAccessSpecifier() { 1906 nextToken(); 1907 // Understand Qt's slots. 1908 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots)) 1909 nextToken(); 1910 // Otherwise, we don't know what it is, and we'd better keep the next token. 1911 if (FormatTok->Tok.is(tok::colon)) 1912 nextToken(); 1913 addUnwrappedLine(); 1914 } 1915 1916 bool UnwrappedLineParser::parseEnum() { 1917 // Won't be 'enum' for NS_ENUMs. 1918 if (FormatTok->Tok.is(tok::kw_enum)) 1919 nextToken(); 1920 1921 // In TypeScript, "enum" can also be used as property name, e.g. in interface 1922 // declarations. An "enum" keyword followed by a colon would be a syntax 1923 // error and thus assume it is just an identifier. 1924 if (Style.Language == FormatStyle::LK_JavaScript && 1925 FormatTok->isOneOf(tok::colon, tok::question)) 1926 return false; 1927 1928 // Eat up enum class ... 1929 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) 1930 nextToken(); 1931 1932 while (FormatTok->Tok.getIdentifierInfo() || 1933 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, 1934 tok::greater, tok::comma, tok::question)) { 1935 nextToken(); 1936 // We can have macros or attributes in between 'enum' and the enum name. 1937 if (FormatTok->is(tok::l_paren)) 1938 parseParens(); 1939 if (FormatTok->is(tok::identifier)) { 1940 nextToken(); 1941 // If there are two identifiers in a row, this is likely an elaborate 1942 // return type. In Java, this can be "implements", etc. 1943 if (Style.isCpp() && FormatTok->is(tok::identifier)) 1944 return false; 1945 } 1946 } 1947 1948 // Just a declaration or something is wrong. 1949 if (FormatTok->isNot(tok::l_brace)) 1950 return true; 1951 FormatTok->BlockKind = BK_Block; 1952 1953 if (Style.Language == FormatStyle::LK_Java) { 1954 // Java enums are different. 1955 parseJavaEnumBody(); 1956 return true; 1957 } 1958 if (Style.Language == FormatStyle::LK_Proto) { 1959 parseBlock(/*MustBeDeclaration=*/true); 1960 return true; 1961 } 1962 1963 // Parse enum body. 1964 nextToken(); 1965 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); 1966 if (HasError) { 1967 if (FormatTok->is(tok::semi)) 1968 nextToken(); 1969 addUnwrappedLine(); 1970 } 1971 return true; 1972 1973 // There is no addUnwrappedLine() here so that we fall through to parsing a 1974 // structural element afterwards. Thus, in "enum A {} n, m;", 1975 // "} n, m;" will end up in one unwrapped line. 1976 } 1977 1978 void UnwrappedLineParser::parseJavaEnumBody() { 1979 // Determine whether the enum is simple, i.e. does not have a semicolon or 1980 // constants with class bodies. Simple enums can be formatted like braced 1981 // lists, contracted to a single line, etc. 1982 unsigned StoredPosition = Tokens->getPosition(); 1983 bool IsSimple = true; 1984 FormatToken *Tok = Tokens->getNextToken(); 1985 while (Tok) { 1986 if (Tok->is(tok::r_brace)) 1987 break; 1988 if (Tok->isOneOf(tok::l_brace, tok::semi)) { 1989 IsSimple = false; 1990 break; 1991 } 1992 // FIXME: This will also mark enums with braces in the arguments to enum 1993 // constants as "not simple". This is probably fine in practice, though. 1994 Tok = Tokens->getNextToken(); 1995 } 1996 FormatTok = Tokens->setPosition(StoredPosition); 1997 1998 if (IsSimple) { 1999 nextToken(); 2000 parseBracedList(); 2001 addUnwrappedLine(); 2002 return; 2003 } 2004 2005 // Parse the body of a more complex enum. 2006 // First add a line for everything up to the "{". 2007 nextToken(); 2008 addUnwrappedLine(); 2009 ++Line->Level; 2010 2011 // Parse the enum constants. 2012 while (FormatTok) { 2013 if (FormatTok->is(tok::l_brace)) { 2014 // Parse the constant's class body. 2015 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, 2016 /*MunchSemi=*/false); 2017 } else if (FormatTok->is(tok::l_paren)) { 2018 parseParens(); 2019 } else if (FormatTok->is(tok::comma)) { 2020 nextToken(); 2021 addUnwrappedLine(); 2022 } else if (FormatTok->is(tok::semi)) { 2023 nextToken(); 2024 addUnwrappedLine(); 2025 break; 2026 } else if (FormatTok->is(tok::r_brace)) { 2027 addUnwrappedLine(); 2028 break; 2029 } else { 2030 nextToken(); 2031 } 2032 } 2033 2034 // Parse the class body after the enum's ";" if any. 2035 parseLevel(/*HasOpeningBrace=*/true); 2036 nextToken(); 2037 --Line->Level; 2038 addUnwrappedLine(); 2039 } 2040 2041 void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { 2042 const FormatToken &InitialToken = *FormatTok; 2043 nextToken(); 2044 2045 // The actual identifier can be a nested name specifier, and in macros 2046 // it is often token-pasted. 2047 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, 2048 tok::kw___attribute, tok::kw___declspec, 2049 tok::kw_alignas) || 2050 ((Style.Language == FormatStyle::LK_Java || 2051 Style.Language == FormatStyle::LK_JavaScript) && 2052 FormatTok->isOneOf(tok::period, tok::comma))) { 2053 if (Style.Language == FormatStyle::LK_JavaScript && 2054 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) { 2055 // JavaScript/TypeScript supports inline object types in 2056 // extends/implements positions: 2057 // class Foo implements {bar: number} { } 2058 nextToken(); 2059 if (FormatTok->is(tok::l_brace)) { 2060 tryToParseBracedList(); 2061 continue; 2062 } 2063 } 2064 bool IsNonMacroIdentifier = 2065 FormatTok->is(tok::identifier) && 2066 FormatTok->TokenText != FormatTok->TokenText.upper(); 2067 nextToken(); 2068 // We can have macros or attributes in between 'class' and the class name. 2069 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren)) 2070 parseParens(); 2071 } 2072 2073 // Note that parsing away template declarations here leads to incorrectly 2074 // accepting function declarations as record declarations. 2075 // In general, we cannot solve this problem. Consider: 2076 // class A<int> B() {} 2077 // which can be a function definition or a class definition when B() is a 2078 // macro. If we find enough real-world cases where this is a problem, we 2079 // can parse for the 'template' keyword in the beginning of the statement, 2080 // and thus rule out the record production in case there is no template 2081 // (this would still leave us with an ambiguity between template function 2082 // and class declarations). 2083 if (FormatTok->isOneOf(tok::colon, tok::less)) { 2084 while (!eof()) { 2085 if (FormatTok->is(tok::l_brace)) { 2086 calculateBraceTypes(/*ExpectClassBody=*/true); 2087 if (!tryToParseBracedList()) 2088 break; 2089 } 2090 if (FormatTok->Tok.is(tok::semi)) 2091 return; 2092 nextToken(); 2093 } 2094 } 2095 if (FormatTok->Tok.is(tok::l_brace)) { 2096 if (ParseAsExpr) { 2097 parseChildBlock(); 2098 } else { 2099 if (ShouldBreakBeforeBrace(Style, InitialToken)) 2100 addUnwrappedLine(); 2101 2102 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, 2103 /*MunchSemi=*/false); 2104 } 2105 } 2106 // There is no addUnwrappedLine() here so that we fall through to parsing a 2107 // structural element afterwards. Thus, in "class A {} n, m;", 2108 // "} n, m;" will end up in one unwrapped line. 2109 } 2110 2111 void UnwrappedLineParser::parseObjCProtocolList() { 2112 assert(FormatTok->Tok.is(tok::less) && "'<' expected."); 2113 do 2114 nextToken(); 2115 while (!eof() && FormatTok->Tok.isNot(tok::greater)); 2116 nextToken(); // Skip '>'. 2117 } 2118 2119 void UnwrappedLineParser::parseObjCUntilAtEnd() { 2120 do { 2121 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { 2122 nextToken(); 2123 addUnwrappedLine(); 2124 break; 2125 } 2126 if (FormatTok->is(tok::l_brace)) { 2127 parseBlock(/*MustBeDeclaration=*/false); 2128 // In ObjC interfaces, nothing should be following the "}". 2129 addUnwrappedLine(); 2130 } else if (FormatTok->is(tok::r_brace)) { 2131 // Ignore stray "}". parseStructuralElement doesn't consume them. 2132 nextToken(); 2133 addUnwrappedLine(); 2134 } else { 2135 parseStructuralElement(); 2136 } 2137 } while (!eof()); 2138 } 2139 2140 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { 2141 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface || 2142 FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation); 2143 nextToken(); 2144 nextToken(); // interface name 2145 2146 // @interface can be followed by either a base class, or a category. 2147 if (FormatTok->Tok.is(tok::colon)) { 2148 nextToken(); 2149 nextToken(); // base class name 2150 } else if (FormatTok->Tok.is(tok::l_paren)) 2151 // Skip category, if present. 2152 parseParens(); 2153 2154 if (FormatTok->Tok.is(tok::less)) 2155 parseObjCProtocolList(); 2156 2157 if (FormatTok->Tok.is(tok::l_brace)) { 2158 if (Style.BraceWrapping.AfterObjCDeclaration) 2159 addUnwrappedLine(); 2160 parseBlock(/*MustBeDeclaration=*/true); 2161 } 2162 2163 // With instance variables, this puts '}' on its own line. Without instance 2164 // variables, this ends the @interface line. 2165 addUnwrappedLine(); 2166 2167 parseObjCUntilAtEnd(); 2168 } 2169 2170 // Returns true for the declaration/definition form of @protocol, 2171 // false for the expression form. 2172 bool UnwrappedLineParser::parseObjCProtocol() { 2173 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol); 2174 nextToken(); 2175 2176 if (FormatTok->is(tok::l_paren)) 2177 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);". 2178 return false; 2179 2180 // The definition/declaration form, 2181 // @protocol Foo 2182 // - (int)someMethod; 2183 // @end 2184 2185 nextToken(); // protocol name 2186 2187 if (FormatTok->Tok.is(tok::less)) 2188 parseObjCProtocolList(); 2189 2190 // Check for protocol declaration. 2191 if (FormatTok->Tok.is(tok::semi)) { 2192 nextToken(); 2193 addUnwrappedLine(); 2194 return true; 2195 } 2196 2197 addUnwrappedLine(); 2198 parseObjCUntilAtEnd(); 2199 return true; 2200 } 2201 2202 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { 2203 bool IsImport = FormatTok->is(Keywords.kw_import); 2204 assert(IsImport || FormatTok->is(tok::kw_export)); 2205 nextToken(); 2206 2207 // Consume the "default" in "export default class/function". 2208 if (FormatTok->is(tok::kw_default)) 2209 nextToken(); 2210 2211 // Consume "async function", "function" and "default function", so that these 2212 // get parsed as free-standing JS functions, i.e. do not require a trailing 2213 // semicolon. 2214 if (FormatTok->is(Keywords.kw_async)) 2215 nextToken(); 2216 if (FormatTok->is(Keywords.kw_function)) { 2217 nextToken(); 2218 return; 2219 } 2220 2221 // For imports, `export *`, `export {...}`, consume the rest of the line up 2222 // to the terminating `;`. For everything else, just return and continue 2223 // parsing the structural element, i.e. the declaration or expression for 2224 // `export default`. 2225 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) && 2226 !FormatTok->isStringLiteral()) 2227 return; 2228 2229 while (!eof()) { 2230 if (FormatTok->is(tok::semi)) 2231 return; 2232 if (Line->Tokens.empty()) { 2233 // Common issue: Automatic Semicolon Insertion wrapped the line, so the 2234 // import statement should terminate. 2235 return; 2236 } 2237 if (FormatTok->is(tok::l_brace)) { 2238 FormatTok->BlockKind = BK_Block; 2239 nextToken(); 2240 parseBracedList(); 2241 } else { 2242 nextToken(); 2243 } 2244 } 2245 } 2246 2247 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, 2248 StringRef Prefix = "") { 2249 llvm::dbgs() << Prefix << "Line(" << Line.Level 2250 << ", FSC=" << Line.FirstStartColumn << ")" 2251 << (Line.InPPDirective ? " MACRO" : "") << ": "; 2252 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 2253 E = Line.Tokens.end(); 2254 I != E; ++I) { 2255 llvm::dbgs() << I->Tok->Tok.getName() << "[" 2256 << "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn 2257 << "] "; 2258 } 2259 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 2260 E = Line.Tokens.end(); 2261 I != E; ++I) { 2262 const UnwrappedLineNode &Node = *I; 2263 for (SmallVectorImpl<UnwrappedLine>::const_iterator 2264 I = Node.Children.begin(), 2265 E = Node.Children.end(); 2266 I != E; ++I) { 2267 printDebugInfo(*I, "\nChild: "); 2268 } 2269 } 2270 llvm::dbgs() << "\n"; 2271 } 2272 2273 void UnwrappedLineParser::addUnwrappedLine() { 2274 if (Line->Tokens.empty()) 2275 return; 2276 DEBUG({ 2277 if (CurrentLines == &Lines) 2278 printDebugInfo(*Line); 2279 }); 2280 CurrentLines->push_back(std::move(*Line)); 2281 Line->Tokens.clear(); 2282 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex; 2283 Line->FirstStartColumn = 0; 2284 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { 2285 CurrentLines->append( 2286 std::make_move_iterator(PreprocessorDirectives.begin()), 2287 std::make_move_iterator(PreprocessorDirectives.end())); 2288 PreprocessorDirectives.clear(); 2289 } 2290 // Disconnect the current token from the last token on the previous line. 2291 FormatTok->Previous = nullptr; 2292 } 2293 2294 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } 2295 2296 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { 2297 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && 2298 FormatTok.NewlinesBefore > 0; 2299 } 2300 2301 // Checks if \p FormatTok is a line comment that continues the line comment 2302 // section on \p Line. 2303 static bool continuesLineCommentSection(const FormatToken &FormatTok, 2304 const UnwrappedLine &Line, 2305 llvm::Regex &CommentPragmasRegex) { 2306 if (Line.Tokens.empty()) 2307 return false; 2308 2309 StringRef IndentContent = FormatTok.TokenText; 2310 if (FormatTok.TokenText.startswith("//") || 2311 FormatTok.TokenText.startswith("/*")) 2312 IndentContent = FormatTok.TokenText.substr(2); 2313 if (CommentPragmasRegex.match(IndentContent)) 2314 return false; 2315 2316 // If Line starts with a line comment, then FormatTok continues the comment 2317 // section if its original column is greater or equal to the original start 2318 // column of the line. 2319 // 2320 // Define the min column token of a line as follows: if a line ends in '{' or 2321 // contains a '{' followed by a line comment, then the min column token is 2322 // that '{'. Otherwise, the min column token of the line is the first token of 2323 // the line. 2324 // 2325 // If Line starts with a token other than a line comment, then FormatTok 2326 // continues the comment section if its original column is greater than the 2327 // original start column of the min column token of the line. 2328 // 2329 // For example, the second line comment continues the first in these cases: 2330 // 2331 // // first line 2332 // // second line 2333 // 2334 // and: 2335 // 2336 // // first line 2337 // // second line 2338 // 2339 // and: 2340 // 2341 // int i; // first line 2342 // // second line 2343 // 2344 // and: 2345 // 2346 // do { // first line 2347 // // second line 2348 // int i; 2349 // } while (true); 2350 // 2351 // and: 2352 // 2353 // enum { 2354 // a, // first line 2355 // // second line 2356 // b 2357 // }; 2358 // 2359 // The second line comment doesn't continue the first in these cases: 2360 // 2361 // // first line 2362 // // second line 2363 // 2364 // and: 2365 // 2366 // int i; // first line 2367 // // second line 2368 // 2369 // and: 2370 // 2371 // do { // first line 2372 // // second line 2373 // int i; 2374 // } while (true); 2375 // 2376 // and: 2377 // 2378 // enum { 2379 // a, // first line 2380 // // second line 2381 // }; 2382 const FormatToken *MinColumnToken = Line.Tokens.front().Tok; 2383 2384 // Scan for '{//'. If found, use the column of '{' as a min column for line 2385 // comment section continuation. 2386 const FormatToken *PreviousToken = nullptr; 2387 for (const UnwrappedLineNode &Node : Line.Tokens) { 2388 if (PreviousToken && PreviousToken->is(tok::l_brace) && 2389 isLineComment(*Node.Tok)) { 2390 MinColumnToken = PreviousToken; 2391 break; 2392 } 2393 PreviousToken = Node.Tok; 2394 2395 // Grab the last newline preceding a token in this unwrapped line. 2396 if (Node.Tok->NewlinesBefore > 0) { 2397 MinColumnToken = Node.Tok; 2398 } 2399 } 2400 if (PreviousToken && PreviousToken->is(tok::l_brace)) { 2401 MinColumnToken = PreviousToken; 2402 } 2403 2404 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok, 2405 MinColumnToken); 2406 } 2407 2408 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { 2409 bool JustComments = Line->Tokens.empty(); 2410 for (SmallVectorImpl<FormatToken *>::const_iterator 2411 I = CommentsBeforeNextToken.begin(), 2412 E = CommentsBeforeNextToken.end(); 2413 I != E; ++I) { 2414 // Line comments that belong to the same line comment section are put on the 2415 // same line since later we might want to reflow content between them. 2416 // Additional fine-grained breaking of line comment sections is controlled 2417 // by the class BreakableLineCommentSection in case it is desirable to keep 2418 // several line comment sections in the same unwrapped line. 2419 // 2420 // FIXME: Consider putting separate line comment sections as children to the 2421 // unwrapped line instead. 2422 (*I)->ContinuesLineCommentSection = 2423 continuesLineCommentSection(**I, *Line, CommentPragmasRegex); 2424 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection) 2425 addUnwrappedLine(); 2426 pushToken(*I); 2427 } 2428 if (NewlineBeforeNext && JustComments) 2429 addUnwrappedLine(); 2430 CommentsBeforeNextToken.clear(); 2431 } 2432 2433 void UnwrappedLineParser::nextToken(int LevelDifference) { 2434 if (eof()) 2435 return; 2436 flushComments(isOnNewLine(*FormatTok)); 2437 pushToken(FormatTok); 2438 FormatToken *Previous = FormatTok; 2439 if (Style.Language != FormatStyle::LK_JavaScript) 2440 readToken(LevelDifference); 2441 else 2442 readTokenWithJavaScriptASI(); 2443 FormatTok->Previous = Previous; 2444 } 2445 2446 void UnwrappedLineParser::distributeComments( 2447 const SmallVectorImpl<FormatToken *> &Comments, 2448 const FormatToken *NextTok) { 2449 // Whether or not a line comment token continues a line is controlled by 2450 // the method continuesLineCommentSection, with the following caveat: 2451 // 2452 // Define a trail of Comments to be a nonempty proper postfix of Comments such 2453 // that each comment line from the trail is aligned with the next token, if 2454 // the next token exists. If a trail exists, the beginning of the maximal 2455 // trail is marked as a start of a new comment section. 2456 // 2457 // For example in this code: 2458 // 2459 // int a; // line about a 2460 // // line 1 about b 2461 // // line 2 about b 2462 // int b; 2463 // 2464 // the two lines about b form a maximal trail, so there are two sections, the 2465 // first one consisting of the single comment "// line about a" and the 2466 // second one consisting of the next two comments. 2467 if (Comments.empty()) 2468 return; 2469 bool ShouldPushCommentsInCurrentLine = true; 2470 bool HasTrailAlignedWithNextToken = false; 2471 unsigned StartOfTrailAlignedWithNextToken = 0; 2472 if (NextTok) { 2473 // We are skipping the first element intentionally. 2474 for (unsigned i = Comments.size() - 1; i > 0; --i) { 2475 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) { 2476 HasTrailAlignedWithNextToken = true; 2477 StartOfTrailAlignedWithNextToken = i; 2478 } 2479 } 2480 } 2481 for (unsigned i = 0, e = Comments.size(); i < e; ++i) { 2482 FormatToken *FormatTok = Comments[i]; 2483 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) { 2484 FormatTok->ContinuesLineCommentSection = false; 2485 } else { 2486 FormatTok->ContinuesLineCommentSection = 2487 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex); 2488 } 2489 if (!FormatTok->ContinuesLineCommentSection && 2490 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) { 2491 ShouldPushCommentsInCurrentLine = false; 2492 } 2493 if (ShouldPushCommentsInCurrentLine) { 2494 pushToken(FormatTok); 2495 } else { 2496 CommentsBeforeNextToken.push_back(FormatTok); 2497 } 2498 } 2499 } 2500 2501 void UnwrappedLineParser::readToken(int LevelDifference) { 2502 SmallVector<FormatToken *, 1> Comments; 2503 do { 2504 FormatTok = Tokens->getNextToken(); 2505 assert(FormatTok); 2506 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && 2507 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { 2508 distributeComments(Comments, FormatTok); 2509 Comments.clear(); 2510 // If there is an unfinished unwrapped line, we flush the preprocessor 2511 // directives only after that unwrapped line was finished later. 2512 bool SwitchToPreprocessorLines = !Line->Tokens.empty(); 2513 ScopedLineState BlockState(*this, SwitchToPreprocessorLines); 2514 assert((LevelDifference >= 0 || 2515 static_cast<unsigned>(-LevelDifference) <= Line->Level) && 2516 "LevelDifference makes Line->Level negative"); 2517 Line->Level += LevelDifference; 2518 // Comments stored before the preprocessor directive need to be output 2519 // before the preprocessor directive, at the same level as the 2520 // preprocessor directive, as we consider them to apply to the directive. 2521 flushComments(isOnNewLine(*FormatTok)); 2522 parsePPDirective(); 2523 } 2524 while (FormatTok->Type == TT_ConflictStart || 2525 FormatTok->Type == TT_ConflictEnd || 2526 FormatTok->Type == TT_ConflictAlternative) { 2527 if (FormatTok->Type == TT_ConflictStart) { 2528 conditionalCompilationStart(/*Unreachable=*/false); 2529 } else if (FormatTok->Type == TT_ConflictAlternative) { 2530 conditionalCompilationAlternative(); 2531 } else if (FormatTok->Type == TT_ConflictEnd) { 2532 conditionalCompilationEnd(); 2533 } 2534 FormatTok = Tokens->getNextToken(); 2535 FormatTok->MustBreakBefore = true; 2536 } 2537 2538 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) && 2539 !Line->InPPDirective) { 2540 continue; 2541 } 2542 2543 if (!FormatTok->Tok.is(tok::comment)) { 2544 distributeComments(Comments, FormatTok); 2545 Comments.clear(); 2546 return; 2547 } 2548 2549 Comments.push_back(FormatTok); 2550 } while (!eof()); 2551 2552 distributeComments(Comments, nullptr); 2553 Comments.clear(); 2554 } 2555 2556 void UnwrappedLineParser::pushToken(FormatToken *Tok) { 2557 Line->Tokens.push_back(UnwrappedLineNode(Tok)); 2558 if (MustBreakBeforeNextToken) { 2559 Line->Tokens.back().Tok->MustBreakBefore = true; 2560 MustBreakBeforeNextToken = false; 2561 } 2562 } 2563 2564 } // end namespace format 2565 } // end namespace clang 2566