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 #define DEBUG_TYPE "format-parser" 22 23 namespace clang { 24 namespace format { 25 26 class FormatTokenSource { 27 public: 28 virtual ~FormatTokenSource() {} 29 virtual FormatToken *getNextToken() = 0; 30 31 virtual unsigned getPosition() = 0; 32 virtual FormatToken *setPosition(unsigned Position) = 0; 33 }; 34 35 namespace { 36 37 class ScopedDeclarationState { 38 public: 39 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack, 40 bool MustBeDeclaration) 41 : Line(Line), Stack(Stack) { 42 Line.MustBeDeclaration = MustBeDeclaration; 43 Stack.push_back(MustBeDeclaration); 44 } 45 ~ScopedDeclarationState() { 46 Stack.pop_back(); 47 if (!Stack.empty()) 48 Line.MustBeDeclaration = Stack.back(); 49 else 50 Line.MustBeDeclaration = true; 51 } 52 53 private: 54 UnwrappedLine &Line; 55 std::vector<bool> &Stack; 56 }; 57 58 class ScopedMacroState : public FormatTokenSource { 59 public: 60 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, 61 FormatToken *&ResetToken) 62 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), 63 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), 64 Token(nullptr) { 65 TokenSource = this; 66 Line.Level = 0; 67 Line.InPPDirective = true; 68 } 69 70 ~ScopedMacroState() override { 71 TokenSource = PreviousTokenSource; 72 ResetToken = Token; 73 Line.InPPDirective = false; 74 Line.Level = PreviousLineLevel; 75 } 76 77 FormatToken *getNextToken() override { 78 // The \c UnwrappedLineParser guards against this by never calling 79 // \c getNextToken() after it has encountered the first eof token. 80 assert(!eof()); 81 Token = PreviousTokenSource->getNextToken(); 82 if (eof()) 83 return getFakeEOF(); 84 return Token; 85 } 86 87 unsigned getPosition() override { return PreviousTokenSource->getPosition(); } 88 89 FormatToken *setPosition(unsigned Position) override { 90 Token = PreviousTokenSource->setPosition(Position); 91 return Token; 92 } 93 94 private: 95 bool eof() { return Token && Token->HasUnescapedNewline; } 96 97 FormatToken *getFakeEOF() { 98 static bool EOFInitialized = false; 99 static FormatToken FormatTok; 100 if (!EOFInitialized) { 101 FormatTok.Tok.startToken(); 102 FormatTok.Tok.setKind(tok::eof); 103 EOFInitialized = true; 104 } 105 return &FormatTok; 106 } 107 108 UnwrappedLine &Line; 109 FormatTokenSource *&TokenSource; 110 FormatToken *&ResetToken; 111 unsigned PreviousLineLevel; 112 FormatTokenSource *PreviousTokenSource; 113 114 FormatToken *Token; 115 }; 116 117 } // end anonymous namespace 118 119 class ScopedLineState { 120 public: 121 ScopedLineState(UnwrappedLineParser &Parser, 122 bool SwitchToPreprocessorLines = false) 123 : Parser(Parser), OriginalLines(Parser.CurrentLines) { 124 if (SwitchToPreprocessorLines) 125 Parser.CurrentLines = &Parser.PreprocessorDirectives; 126 else if (!Parser.Line->Tokens.empty()) 127 Parser.CurrentLines = &Parser.Line->Tokens.back().Children; 128 PreBlockLine = std::move(Parser.Line); 129 Parser.Line = llvm::make_unique<UnwrappedLine>(); 130 Parser.Line->Level = PreBlockLine->Level; 131 Parser.Line->InPPDirective = PreBlockLine->InPPDirective; 132 } 133 134 ~ScopedLineState() { 135 if (!Parser.Line->Tokens.empty()) { 136 Parser.addUnwrappedLine(); 137 } 138 assert(Parser.Line->Tokens.empty()); 139 Parser.Line = std::move(PreBlockLine); 140 if (Parser.CurrentLines == &Parser.PreprocessorDirectives) 141 Parser.MustBreakBeforeNextToken = true; 142 Parser.CurrentLines = OriginalLines; 143 } 144 145 private: 146 UnwrappedLineParser &Parser; 147 148 std::unique_ptr<UnwrappedLine> PreBlockLine; 149 SmallVectorImpl<UnwrappedLine> *OriginalLines; 150 }; 151 152 class CompoundStatementIndenter { 153 public: 154 CompoundStatementIndenter(UnwrappedLineParser *Parser, 155 const FormatStyle &Style, unsigned &LineLevel) 156 : LineLevel(LineLevel), OldLineLevel(LineLevel) { 157 if (Style.BraceWrapping.AfterControlStatement) 158 Parser->addUnwrappedLine(); 159 if (Style.BraceWrapping.IndentBraces) 160 ++LineLevel; 161 } 162 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } 163 164 private: 165 unsigned &LineLevel; 166 unsigned OldLineLevel; 167 }; 168 169 namespace { 170 171 class IndexedTokenSource : public FormatTokenSource { 172 public: 173 IndexedTokenSource(ArrayRef<FormatToken *> Tokens) 174 : Tokens(Tokens), Position(-1) {} 175 176 FormatToken *getNextToken() override { 177 ++Position; 178 return Tokens[Position]; 179 } 180 181 unsigned getPosition() override { 182 assert(Position >= 0); 183 return Position; 184 } 185 186 FormatToken *setPosition(unsigned P) override { 187 Position = P; 188 return Tokens[Position]; 189 } 190 191 void reset() { Position = -1; } 192 193 private: 194 ArrayRef<FormatToken *> Tokens; 195 int Position; 196 }; 197 198 } // end anonymous namespace 199 200 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, 201 const AdditionalKeywords &Keywords, 202 ArrayRef<FormatToken *> Tokens, 203 UnwrappedLineConsumer &Callback) 204 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), 205 CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr), 206 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {} 207 208 void UnwrappedLineParser::reset() { 209 PPBranchLevel = -1; 210 Line.reset(new UnwrappedLine); 211 CommentsBeforeNextToken.clear(); 212 FormatTok = nullptr; 213 MustBreakBeforeNextToken = false; 214 PreprocessorDirectives.clear(); 215 CurrentLines = &Lines; 216 DeclarationScopeStack.clear(); 217 PPStack.clear(); 218 } 219 220 void UnwrappedLineParser::parse() { 221 IndexedTokenSource TokenSource(AllTokens); 222 do { 223 DEBUG(llvm::dbgs() << "----\n"); 224 reset(); 225 Tokens = &TokenSource; 226 TokenSource.reset(); 227 228 readToken(); 229 parseFile(); 230 // Create line with eof token. 231 pushToken(FormatTok); 232 addUnwrappedLine(); 233 234 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(), 235 E = Lines.end(); 236 I != E; ++I) { 237 Callback.consumeUnwrappedLine(*I); 238 } 239 Callback.finishRun(); 240 Lines.clear(); 241 while (!PPLevelBranchIndex.empty() && 242 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) { 243 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); 244 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); 245 } 246 if (!PPLevelBranchIndex.empty()) { 247 ++PPLevelBranchIndex.back(); 248 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); 249 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); 250 } 251 } while (!PPLevelBranchIndex.empty()); 252 } 253 254 void UnwrappedLineParser::parseFile() { 255 // The top-level context in a file always has declarations, except for pre- 256 // processor directives and JavaScript files. 257 bool MustBeDeclaration = 258 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript; 259 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 260 MustBeDeclaration); 261 parseLevel(/*HasOpeningBrace=*/false); 262 // Make sure to format the remaining tokens. 263 flushComments(true); 264 addUnwrappedLine(); 265 } 266 267 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { 268 bool SwitchLabelEncountered = false; 269 do { 270 tok::TokenKind kind = FormatTok->Tok.getKind(); 271 if (FormatTok->Type == TT_MacroBlockBegin) { 272 kind = tok::l_brace; 273 } else if (FormatTok->Type == TT_MacroBlockEnd) { 274 kind = tok::r_brace; 275 } 276 277 switch (kind) { 278 case tok::comment: 279 nextToken(); 280 addUnwrappedLine(); 281 break; 282 case tok::l_brace: 283 // FIXME: Add parameter whether this can happen - if this happens, we must 284 // be in a non-declaration context. 285 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList()) 286 continue; 287 parseBlock(/*MustBeDeclaration=*/false); 288 addUnwrappedLine(); 289 break; 290 case tok::r_brace: 291 if (HasOpeningBrace) 292 return; 293 nextToken(); 294 addUnwrappedLine(); 295 break; 296 case tok::kw_default: 297 case tok::kw_case: 298 if (!SwitchLabelEncountered && 299 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1))) 300 ++Line->Level; 301 SwitchLabelEncountered = true; 302 parseStructuralElement(); 303 break; 304 default: 305 parseStructuralElement(); 306 break; 307 } 308 } while (!eof()); 309 } 310 311 void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { 312 // We'll parse forward through the tokens until we hit 313 // a closing brace or eof - note that getNextToken() will 314 // parse macros, so this will magically work inside macro 315 // definitions, too. 316 unsigned StoredPosition = Tokens->getPosition(); 317 FormatToken *Tok = FormatTok; 318 const FormatToken *PrevTok = getPreviousToken(); 319 // Keep a stack of positions of lbrace tokens. We will 320 // update information about whether an lbrace starts a 321 // braced init list or a different block during the loop. 322 SmallVector<FormatToken *, 8> LBraceStack; 323 assert(Tok->Tok.is(tok::l_brace)); 324 do { 325 // Get next non-comment token. 326 FormatToken *NextTok; 327 unsigned ReadTokens = 0; 328 do { 329 NextTok = Tokens->getNextToken(); 330 ++ReadTokens; 331 } while (NextTok->is(tok::comment)); 332 333 switch (Tok->Tok.getKind()) { 334 case tok::l_brace: 335 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok && 336 PrevTok->is(tok::colon)) 337 // In TypeScript's TypeMemberLists, there can be semicolons between the 338 // individual members. 339 Tok->BlockKind = BK_BracedInit; 340 else 341 Tok->BlockKind = BK_Unknown; 342 LBraceStack.push_back(Tok); 343 break; 344 case tok::r_brace: 345 if (LBraceStack.empty()) 346 break; 347 if (LBraceStack.back()->BlockKind == BK_Unknown) { 348 bool ProbablyBracedList = false; 349 if (Style.Language == FormatStyle::LK_Proto) { 350 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square); 351 } else { 352 // Using OriginalColumn to distinguish between ObjC methods and 353 // binary operators is a bit hacky. 354 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) && 355 NextTok->OriginalColumn == 0; 356 357 // If there is a comma, semicolon or right paren after the closing 358 // brace, we assume this is a braced initializer list. Note that 359 // regardless how we mark inner braces here, we will overwrite the 360 // BlockKind later if we parse a braced list (where all blocks 361 // inside are by default braced lists), or when we explicitly detect 362 // blocks (for example while parsing lambdas). 363 // 364 // We exclude + and - as they can be ObjC visibility modifiers. 365 ProbablyBracedList = 366 (Style.Language == FormatStyle::LK_JavaScript && 367 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in)) || 368 NextTok->isOneOf(tok::comma, tok::period, tok::colon, 369 tok::r_paren, tok::r_square, tok::l_brace, 370 tok::l_square, tok::l_paren, tok::ellipsis) || 371 (NextTok->is(tok::semi) && 372 (!ExpectClassBody || LBraceStack.size() != 1)) || 373 (NextTok->isBinaryOperator() && !NextIsObjCMethod); 374 } 375 if (ProbablyBracedList) { 376 Tok->BlockKind = BK_BracedInit; 377 LBraceStack.back()->BlockKind = BK_BracedInit; 378 } else { 379 Tok->BlockKind = BK_Block; 380 LBraceStack.back()->BlockKind = BK_Block; 381 } 382 } 383 LBraceStack.pop_back(); 384 break; 385 case tok::at: 386 case tok::semi: 387 case tok::kw_if: 388 case tok::kw_while: 389 case tok::kw_for: 390 case tok::kw_switch: 391 case tok::kw_try: 392 case tok::kw___try: 393 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown) 394 LBraceStack.back()->BlockKind = BK_Block; 395 break; 396 default: 397 break; 398 } 399 PrevTok = Tok; 400 Tok = NextTok; 401 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty()); 402 403 // Assume other blocks for all unclosed opening braces. 404 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { 405 if (LBraceStack[i]->BlockKind == BK_Unknown) 406 LBraceStack[i]->BlockKind = BK_Block; 407 } 408 409 FormatTok = Tokens->setPosition(StoredPosition); 410 } 411 412 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, 413 bool MunchSemi) { 414 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) && 415 "'{' or macro block token expected"); 416 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin); 417 FormatTok->BlockKind = BK_Block; 418 419 unsigned InitialLevel = Line->Level; 420 nextToken(); 421 422 if (MacroBlock && FormatTok->is(tok::l_paren)) 423 parseParens(); 424 425 addUnwrappedLine(); 426 427 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 428 MustBeDeclaration); 429 if (AddLevel) 430 ++Line->Level; 431 parseLevel(/*HasOpeningBrace=*/true); 432 433 if (eof()) 434 return; 435 436 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd) 437 : !FormatTok->is(tok::r_brace)) { 438 Line->Level = InitialLevel; 439 FormatTok->BlockKind = BK_Block; 440 return; 441 } 442 443 nextToken(); // Munch the closing brace. 444 445 if (MacroBlock && FormatTok->is(tok::l_paren)) 446 parseParens(); 447 448 if (MunchSemi && FormatTok->Tok.is(tok::semi)) 449 nextToken(); 450 Line->Level = InitialLevel; 451 } 452 453 static bool isGoogScope(const UnwrappedLine &Line) { 454 // FIXME: Closure-library specific stuff should not be hard-coded but be 455 // configurable. 456 if (Line.Tokens.size() < 4) 457 return false; 458 auto I = Line.Tokens.begin(); 459 if (I->Tok->TokenText != "goog") 460 return false; 461 ++I; 462 if (I->Tok->isNot(tok::period)) 463 return false; 464 ++I; 465 if (I->Tok->TokenText != "scope") 466 return false; 467 ++I; 468 return I->Tok->is(tok::l_paren); 469 } 470 471 static bool ShouldBreakBeforeBrace(const FormatStyle &Style, 472 const FormatToken &InitialToken) { 473 if (InitialToken.is(tok::kw_namespace)) 474 return Style.BraceWrapping.AfterNamespace; 475 if (InitialToken.is(tok::kw_class)) 476 return Style.BraceWrapping.AfterClass; 477 if (InitialToken.is(tok::kw_union)) 478 return Style.BraceWrapping.AfterUnion; 479 if (InitialToken.is(tok::kw_struct)) 480 return Style.BraceWrapping.AfterStruct; 481 return false; 482 } 483 484 void UnwrappedLineParser::parseChildBlock() { 485 FormatTok->BlockKind = BK_Block; 486 nextToken(); 487 { 488 bool GoogScope = 489 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line); 490 ScopedLineState LineState(*this); 491 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 492 /*MustBeDeclaration=*/false); 493 Line->Level += GoogScope ? 0 : 1; 494 parseLevel(/*HasOpeningBrace=*/true); 495 flushComments(isOnNewLine(*FormatTok)); 496 Line->Level -= GoogScope ? 0 : 1; 497 } 498 nextToken(); 499 } 500 501 void UnwrappedLineParser::parsePPDirective() { 502 assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); 503 ScopedMacroState MacroState(*Line, Tokens, FormatTok); 504 nextToken(); 505 506 if (!FormatTok->Tok.getIdentifierInfo()) { 507 parsePPUnknown(); 508 return; 509 } 510 511 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { 512 case tok::pp_define: 513 parsePPDefine(); 514 return; 515 case tok::pp_if: 516 parsePPIf(/*IfDef=*/false); 517 break; 518 case tok::pp_ifdef: 519 case tok::pp_ifndef: 520 parsePPIf(/*IfDef=*/true); 521 break; 522 case tok::pp_else: 523 parsePPElse(); 524 break; 525 case tok::pp_elif: 526 parsePPElIf(); 527 break; 528 case tok::pp_endif: 529 parsePPEndIf(); 530 break; 531 default: 532 parsePPUnknown(); 533 break; 534 } 535 } 536 537 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { 538 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable)) 539 PPStack.push_back(PP_Unreachable); 540 else 541 PPStack.push_back(PP_Conditional); 542 } 543 544 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { 545 ++PPBranchLevel; 546 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); 547 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { 548 PPLevelBranchIndex.push_back(0); 549 PPLevelBranchCount.push_back(0); 550 } 551 PPChainBranchIndex.push(0); 552 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; 553 conditionalCompilationCondition(Unreachable || Skip); 554 } 555 556 void UnwrappedLineParser::conditionalCompilationAlternative() { 557 if (!PPStack.empty()) 558 PPStack.pop_back(); 559 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 560 if (!PPChainBranchIndex.empty()) 561 ++PPChainBranchIndex.top(); 562 conditionalCompilationCondition( 563 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && 564 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()); 565 } 566 567 void UnwrappedLineParser::conditionalCompilationEnd() { 568 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 569 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { 570 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { 571 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; 572 } 573 } 574 // Guard against #endif's without #if. 575 if (PPBranchLevel > 0) 576 --PPBranchLevel; 577 if (!PPChainBranchIndex.empty()) 578 PPChainBranchIndex.pop(); 579 if (!PPStack.empty()) 580 PPStack.pop_back(); 581 } 582 583 void UnwrappedLineParser::parsePPIf(bool IfDef) { 584 nextToken(); 585 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() && 586 FormatTok->Tok.getLiteralData() != nullptr && 587 StringRef(FormatTok->Tok.getLiteralData(), 588 FormatTok->Tok.getLength()) == "0") || 589 FormatTok->Tok.is(tok::kw_false); 590 conditionalCompilationStart(!IfDef && IsLiteralFalse); 591 parsePPUnknown(); 592 } 593 594 void UnwrappedLineParser::parsePPElse() { 595 conditionalCompilationAlternative(); 596 parsePPUnknown(); 597 } 598 599 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } 600 601 void UnwrappedLineParser::parsePPEndIf() { 602 conditionalCompilationEnd(); 603 parsePPUnknown(); 604 } 605 606 void UnwrappedLineParser::parsePPDefine() { 607 nextToken(); 608 609 if (FormatTok->Tok.getKind() != tok::identifier) { 610 parsePPUnknown(); 611 return; 612 } 613 nextToken(); 614 if (FormatTok->Tok.getKind() == tok::l_paren && 615 FormatTok->WhitespaceRange.getBegin() == 616 FormatTok->WhitespaceRange.getEnd()) { 617 parseParens(); 618 } 619 addUnwrappedLine(); 620 Line->Level = 1; 621 622 // Errors during a preprocessor directive can only affect the layout of the 623 // preprocessor directive, and thus we ignore them. An alternative approach 624 // would be to use the same approach we use on the file level (no 625 // re-indentation if there was a structural error) within the macro 626 // definition. 627 parseFile(); 628 } 629 630 void UnwrappedLineParser::parsePPUnknown() { 631 do { 632 nextToken(); 633 } while (!eof()); 634 addUnwrappedLine(); 635 } 636 637 // Here we blacklist certain tokens that are not usually the first token in an 638 // unwrapped line. This is used in attempt to distinguish macro calls without 639 // trailing semicolons from other constructs split to several lines. 640 static bool tokenCanStartNewLine(const clang::Token &Tok) { 641 // Semicolon can be a null-statement, l_square can be a start of a macro or 642 // a C++11 attribute, but this doesn't seem to be common. 643 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && 644 Tok.isNot(tok::l_square) && 645 // Tokens that can only be used as binary operators and a part of 646 // overloaded operator names. 647 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && 648 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && 649 Tok.isNot(tok::less) && Tok.isNot(tok::greater) && 650 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && 651 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && 652 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && 653 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && 654 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && 655 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && 656 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && 657 Tok.isNot(tok::lesslessequal) && 658 // Colon is used in labels, base class lists, initializer lists, 659 // range-based for loops, ternary operator, but should never be the 660 // first token in an unwrapped line. 661 Tok.isNot(tok::colon) && 662 // 'noexcept' is a trailing annotation. 663 Tok.isNot(tok::kw_noexcept); 664 } 665 666 static bool mustBeJSIdent(const AdditionalKeywords &Keywords, 667 const FormatToken *FormatTok) { 668 // FIXME: This returns true for C/C++ keywords like 'struct'. 669 return FormatTok->is(tok::identifier) && 670 (FormatTok->Tok.getIdentifierInfo() == nullptr || 671 !FormatTok->isOneOf(Keywords.kw_in, Keywords.kw_of, Keywords.kw_async, 672 Keywords.kw_await, Keywords.kw_yield, 673 Keywords.kw_finally, Keywords.kw_function, 674 Keywords.kw_import, Keywords.kw_is, 675 Keywords.kw_let, Keywords.kw_var, 676 Keywords.kw_abstract, Keywords.kw_extends, 677 Keywords.kw_implements, Keywords.kw_instanceof, 678 Keywords.kw_interface, Keywords.kw_throws)); 679 } 680 681 static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords, 682 const FormatToken *FormatTok) { 683 return FormatTok->Tok.isLiteral() || mustBeJSIdent(Keywords, FormatTok); 684 } 685 686 // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement 687 // when encountered after a value (see mustBeJSIdentOrValue). 688 static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords, 689 const FormatToken *FormatTok) { 690 return FormatTok->isOneOf( 691 tok::kw_return, Keywords.kw_yield, 692 // conditionals 693 tok::kw_if, tok::kw_else, 694 // loops 695 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break, 696 // switch/case 697 tok::kw_switch, tok::kw_case, 698 // exceptions 699 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally, 700 // declaration 701 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let, 702 Keywords.kw_async, Keywords.kw_function, 703 // import/export 704 Keywords.kw_import, tok::kw_export); 705 } 706 707 // readTokenWithJavaScriptASI reads the next token and terminates the current 708 // line if JavaScript Automatic Semicolon Insertion must 709 // happen between the current token and the next token. 710 // 711 // This method is conservative - it cannot cover all edge cases of JavaScript, 712 // but only aims to correctly handle certain well known cases. It *must not* 713 // return true in speculative cases. 714 void UnwrappedLineParser::readTokenWithJavaScriptASI() { 715 FormatToken *Previous = FormatTok; 716 readToken(); 717 FormatToken *Next = FormatTok; 718 719 bool IsOnSameLine = 720 CommentsBeforeNextToken.empty() 721 ? Next->NewlinesBefore == 0 722 : CommentsBeforeNextToken.front()->NewlinesBefore == 0; 723 if (IsOnSameLine) 724 return; 725 726 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous); 727 if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) { 728 // If the token before the previous one is an '@', the previous token is an 729 // annotation and can precede another identifier/value. 730 const FormatToken *PrePrevious = std::prev(Line->Tokens.end(), 2)->Tok; 731 if (PrePrevious->is(tok::at)) 732 return; 733 } 734 if (Next->is(tok::exclaim) && PreviousMustBeValue) 735 addUnwrappedLine(); 736 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next); 737 if (NextMustBeValue && (PreviousMustBeValue || 738 Previous->isOneOf(tok::r_square, tok::r_paren, 739 tok::plusplus, tok::minusminus))) 740 addUnwrappedLine(); 741 if (PreviousMustBeValue && isJSDeclOrStmt(Keywords, Next)) 742 addUnwrappedLine(); 743 } 744 745 void UnwrappedLineParser::parseStructuralElement() { 746 assert(!FormatTok->is(tok::l_brace)); 747 if (Style.Language == FormatStyle::LK_TableGen && 748 FormatTok->is(tok::pp_include)) { 749 nextToken(); 750 if (FormatTok->is(tok::string_literal)) 751 nextToken(); 752 addUnwrappedLine(); 753 return; 754 } 755 switch (FormatTok->Tok.getKind()) { 756 case tok::at: 757 nextToken(); 758 if (FormatTok->Tok.is(tok::l_brace)) { 759 parseBracedList(); 760 break; 761 } 762 switch (FormatTok->Tok.getObjCKeywordID()) { 763 case tok::objc_public: 764 case tok::objc_protected: 765 case tok::objc_package: 766 case tok::objc_private: 767 return parseAccessSpecifier(); 768 case tok::objc_interface: 769 case tok::objc_implementation: 770 return parseObjCInterfaceOrImplementation(); 771 case tok::objc_protocol: 772 return parseObjCProtocol(); 773 case tok::objc_end: 774 return; // Handled by the caller. 775 case tok::objc_optional: 776 case tok::objc_required: 777 nextToken(); 778 addUnwrappedLine(); 779 return; 780 case tok::objc_autoreleasepool: 781 nextToken(); 782 if (FormatTok->Tok.is(tok::l_brace)) { 783 if (Style.BraceWrapping.AfterObjCDeclaration) 784 addUnwrappedLine(); 785 parseBlock(/*MustBeDeclaration=*/false); 786 } 787 addUnwrappedLine(); 788 return; 789 case tok::objc_try: 790 // This branch isn't strictly necessary (the kw_try case below would 791 // do this too after the tok::at is parsed above). But be explicit. 792 parseTryCatch(); 793 return; 794 default: 795 break; 796 } 797 break; 798 case tok::kw_asm: 799 nextToken(); 800 if (FormatTok->is(tok::l_brace)) { 801 FormatTok->Type = TT_InlineASMBrace; 802 nextToken(); 803 while (FormatTok && FormatTok->isNot(tok::eof)) { 804 if (FormatTok->is(tok::r_brace)) { 805 FormatTok->Type = TT_InlineASMBrace; 806 nextToken(); 807 addUnwrappedLine(); 808 break; 809 } 810 FormatTok->Finalized = true; 811 nextToken(); 812 } 813 } 814 break; 815 case tok::kw_namespace: 816 parseNamespace(); 817 return; 818 case tok::kw_inline: 819 nextToken(); 820 if (FormatTok->Tok.is(tok::kw_namespace)) { 821 parseNamespace(); 822 return; 823 } 824 break; 825 case tok::kw_public: 826 case tok::kw_protected: 827 case tok::kw_private: 828 if (Style.Language == FormatStyle::LK_Java || 829 Style.Language == FormatStyle::LK_JavaScript) 830 nextToken(); 831 else 832 parseAccessSpecifier(); 833 return; 834 case tok::kw_if: 835 parseIfThenElse(); 836 return; 837 case tok::kw_for: 838 case tok::kw_while: 839 parseForOrWhileLoop(); 840 return; 841 case tok::kw_do: 842 parseDoWhile(); 843 return; 844 case tok::kw_switch: 845 parseSwitch(); 846 return; 847 case tok::kw_default: 848 nextToken(); 849 parseLabel(); 850 return; 851 case tok::kw_case: 852 parseCaseLabel(); 853 return; 854 case tok::kw_try: 855 case tok::kw___try: 856 parseTryCatch(); 857 return; 858 case tok::kw_extern: 859 nextToken(); 860 if (FormatTok->Tok.is(tok::string_literal)) { 861 nextToken(); 862 if (FormatTok->Tok.is(tok::l_brace)) { 863 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); 864 addUnwrappedLine(); 865 return; 866 } 867 } 868 break; 869 case tok::kw_export: 870 if (Style.Language == FormatStyle::LK_JavaScript) { 871 parseJavaScriptEs6ImportExport(); 872 return; 873 } 874 break; 875 case tok::identifier: 876 if (FormatTok->is(TT_ForEachMacro)) { 877 parseForOrWhileLoop(); 878 return; 879 } 880 if (FormatTok->is(TT_MacroBlockBegin)) { 881 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true, 882 /*MunchSemi=*/false); 883 return; 884 } 885 if (Style.Language == FormatStyle::LK_JavaScript && 886 FormatTok->is(Keywords.kw_import)) { 887 parseJavaScriptEs6ImportExport(); 888 return; 889 } 890 if (FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals, 891 Keywords.kw_slots, Keywords.kw_qslots)) { 892 nextToken(); 893 if (FormatTok->is(tok::colon)) { 894 nextToken(); 895 addUnwrappedLine(); 896 } 897 return; 898 } 899 // In all other cases, parse the declaration. 900 break; 901 default: 902 break; 903 } 904 do { 905 switch (FormatTok->Tok.getKind()) { 906 case tok::at: 907 nextToken(); 908 if (FormatTok->Tok.is(tok::l_brace)) 909 parseBracedList(); 910 break; 911 case tok::kw_enum: 912 // parseEnum falls through and does not yet add an unwrapped line as an 913 // enum definition can start a structural element. 914 if (!parseEnum()) 915 break; 916 // This only applies for C++. 917 if (Style.Language != FormatStyle::LK_Cpp) { 918 addUnwrappedLine(); 919 return; 920 } 921 break; 922 case tok::kw_typedef: 923 nextToken(); 924 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS, 925 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS)) 926 parseEnum(); 927 break; 928 case tok::kw_struct: 929 case tok::kw_union: 930 case tok::kw_class: 931 // parseRecord falls through and does not yet add an unwrapped line as a 932 // record declaration or definition can start a structural element. 933 parseRecord(); 934 // This does not apply for Java and JavaScript. 935 if (Style.Language == FormatStyle::LK_Java || 936 Style.Language == FormatStyle::LK_JavaScript) { 937 if (FormatTok->is(tok::semi)) 938 nextToken(); 939 addUnwrappedLine(); 940 return; 941 } 942 break; 943 case tok::period: 944 nextToken(); 945 // In Java, classes have an implicit static member "class". 946 if (Style.Language == FormatStyle::LK_Java && FormatTok && 947 FormatTok->is(tok::kw_class)) 948 nextToken(); 949 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok && 950 FormatTok->Tok.getIdentifierInfo()) 951 // JavaScript only has pseudo keywords, all keywords are allowed to 952 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6 953 nextToken(); 954 break; 955 case tok::semi: 956 nextToken(); 957 addUnwrappedLine(); 958 return; 959 case tok::r_brace: 960 addUnwrappedLine(); 961 return; 962 case tok::l_paren: 963 parseParens(); 964 break; 965 case tok::kw_operator: 966 nextToken(); 967 if (FormatTok->isBinaryOperator()) 968 nextToken(); 969 break; 970 case tok::caret: 971 nextToken(); 972 if (FormatTok->Tok.isAnyIdentifier() || 973 FormatTok->isSimpleTypeSpecifier()) 974 nextToken(); 975 if (FormatTok->is(tok::l_paren)) 976 parseParens(); 977 if (FormatTok->is(tok::l_brace)) 978 parseChildBlock(); 979 break; 980 case tok::l_brace: 981 if (!tryToParseBracedList()) { 982 // A block outside of parentheses must be the last part of a 983 // structural element. 984 // FIXME: Figure out cases where this is not true, and add projections 985 // for them (the one we know is missing are lambdas). 986 if (Style.BraceWrapping.AfterFunction) 987 addUnwrappedLine(); 988 FormatTok->Type = TT_FunctionLBrace; 989 parseBlock(/*MustBeDeclaration=*/false); 990 addUnwrappedLine(); 991 return; 992 } 993 // Otherwise this was a braced init list, and the structural 994 // element continues. 995 break; 996 case tok::kw_try: 997 // We arrive here when parsing function-try blocks. 998 parseTryCatch(); 999 return; 1000 case tok::identifier: { 1001 if (FormatTok->is(TT_MacroBlockEnd)) { 1002 addUnwrappedLine(); 1003 return; 1004 } 1005 1006 // Parse function literal unless 'function' is the first token in a line 1007 // in which case this should be treated as a free-standing function. 1008 if (Style.Language == FormatStyle::LK_JavaScript && 1009 FormatTok->isOneOf(Keywords.kw_async, Keywords.kw_function) && 1010 Line->Tokens.size() > 0) { 1011 tryToParseJSFunction(); 1012 break; 1013 } 1014 if ((Style.Language == FormatStyle::LK_JavaScript || 1015 Style.Language == FormatStyle::LK_Java) && 1016 FormatTok->is(Keywords.kw_interface)) { 1017 if (Style.Language == FormatStyle::LK_JavaScript) { 1018 // In JavaScript/TypeScript, "interface" can be used as a standalone 1019 // identifier, e.g. in `var interface = 1;`. If "interface" is 1020 // followed by another identifier, it is very like to be an actual 1021 // interface declaration. 1022 unsigned StoredPosition = Tokens->getPosition(); 1023 FormatToken *Next = Tokens->getNextToken(); 1024 FormatTok = Tokens->setPosition(StoredPosition); 1025 if (Next && !mustBeJSIdent(Keywords, Next)) { 1026 nextToken(); 1027 break; 1028 } 1029 } 1030 parseRecord(); 1031 addUnwrappedLine(); 1032 return; 1033 } 1034 1035 // See if the following token should start a new unwrapped line. 1036 StringRef Text = FormatTok->TokenText; 1037 nextToken(); 1038 if (Line->Tokens.size() == 1 && 1039 // JS doesn't have macros, and within classes colons indicate fields, 1040 // not labels. 1041 Style.Language != FormatStyle::LK_JavaScript) { 1042 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) { 1043 Line->Tokens.begin()->Tok->MustBreakBefore = true; 1044 parseLabel(); 1045 return; 1046 } 1047 // Recognize function-like macro usages without trailing semicolon as 1048 // well as free-standing macros like Q_OBJECT. 1049 bool FunctionLike = FormatTok->is(tok::l_paren); 1050 if (FunctionLike) 1051 parseParens(); 1052 1053 bool FollowedByNewline = 1054 CommentsBeforeNextToken.empty() 1055 ? FormatTok->NewlinesBefore > 0 1056 : CommentsBeforeNextToken.front()->NewlinesBefore > 0; 1057 1058 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && 1059 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { 1060 addUnwrappedLine(); 1061 return; 1062 } 1063 } 1064 break; 1065 } 1066 case tok::equal: 1067 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType 1068 // TT_JsFatArrow. The always start an expression or a child block if 1069 // followed by a curly. 1070 if (FormatTok->is(TT_JsFatArrow)) { 1071 nextToken(); 1072 if (FormatTok->is(tok::l_brace)) 1073 parseChildBlock(); 1074 break; 1075 } 1076 1077 nextToken(); 1078 if (FormatTok->Tok.is(tok::l_brace)) { 1079 parseBracedList(); 1080 } 1081 break; 1082 case tok::l_square: 1083 parseSquare(); 1084 break; 1085 case tok::kw_new: 1086 parseNew(); 1087 break; 1088 default: 1089 nextToken(); 1090 break; 1091 } 1092 } while (!eof()); 1093 } 1094 1095 bool UnwrappedLineParser::tryToParseLambda() { 1096 if (Style.Language != FormatStyle::LK_Cpp) { 1097 nextToken(); 1098 return false; 1099 } 1100 const FormatToken* Previous = getPreviousToken(); 1101 if (Previous && 1102 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new, 1103 tok::kw_delete) || 1104 Previous->closesScope() || Previous->isSimpleTypeSpecifier())) { 1105 nextToken(); 1106 return false; 1107 } 1108 assert(FormatTok->is(tok::l_square)); 1109 FormatToken &LSquare = *FormatTok; 1110 if (!tryToParseLambdaIntroducer()) 1111 return false; 1112 1113 while (FormatTok->isNot(tok::l_brace)) { 1114 if (FormatTok->isSimpleTypeSpecifier()) { 1115 nextToken(); 1116 continue; 1117 } 1118 switch (FormatTok->Tok.getKind()) { 1119 case tok::l_brace: 1120 break; 1121 case tok::l_paren: 1122 parseParens(); 1123 break; 1124 case tok::amp: 1125 case tok::star: 1126 case tok::kw_const: 1127 case tok::comma: 1128 case tok::less: 1129 case tok::greater: 1130 case tok::identifier: 1131 case tok::numeric_constant: 1132 case tok::coloncolon: 1133 case tok::kw_mutable: 1134 nextToken(); 1135 break; 1136 case tok::arrow: 1137 FormatTok->Type = TT_LambdaArrow; 1138 nextToken(); 1139 break; 1140 default: 1141 return true; 1142 } 1143 } 1144 LSquare.Type = TT_LambdaLSquare; 1145 parseChildBlock(); 1146 return true; 1147 } 1148 1149 bool UnwrappedLineParser::tryToParseLambdaIntroducer() { 1150 nextToken(); 1151 if (FormatTok->is(tok::equal)) { 1152 nextToken(); 1153 if (FormatTok->is(tok::r_square)) { 1154 nextToken(); 1155 return true; 1156 } 1157 if (FormatTok->isNot(tok::comma)) 1158 return false; 1159 nextToken(); 1160 } else if (FormatTok->is(tok::amp)) { 1161 nextToken(); 1162 if (FormatTok->is(tok::r_square)) { 1163 nextToken(); 1164 return true; 1165 } 1166 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) { 1167 return false; 1168 } 1169 if (FormatTok->is(tok::comma)) 1170 nextToken(); 1171 } else if (FormatTok->is(tok::r_square)) { 1172 nextToken(); 1173 return true; 1174 } 1175 do { 1176 if (FormatTok->is(tok::amp)) 1177 nextToken(); 1178 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this)) 1179 return false; 1180 nextToken(); 1181 if (FormatTok->is(tok::ellipsis)) 1182 nextToken(); 1183 if (FormatTok->is(tok::comma)) { 1184 nextToken(); 1185 } else if (FormatTok->is(tok::r_square)) { 1186 nextToken(); 1187 return true; 1188 } else { 1189 return false; 1190 } 1191 } while (!eof()); 1192 return false; 1193 } 1194 1195 void UnwrappedLineParser::tryToParseJSFunction() { 1196 assert(FormatTok->isOneOf(Keywords.kw_async, Keywords.kw_function)); 1197 if (FormatTok->is(Keywords.kw_async)) 1198 nextToken(); 1199 // Consume "function". 1200 nextToken(); 1201 1202 // Consume * (generator function). 1203 if (FormatTok->is(tok::star)) 1204 nextToken(); 1205 1206 // Consume function name. 1207 if (FormatTok->is(tok::identifier)) 1208 nextToken(); 1209 1210 if (FormatTok->isNot(tok::l_paren)) 1211 return; 1212 1213 // Parse formal parameter list. 1214 parseParens(); 1215 1216 if (FormatTok->is(tok::colon)) { 1217 // Parse a type definition. 1218 nextToken(); 1219 1220 // Eat the type declaration. For braced inline object types, balance braces, 1221 // otherwise just parse until finding an l_brace for the function body. 1222 if (FormatTok->is(tok::l_brace)) 1223 tryToParseBracedList(); 1224 else 1225 while (FormatTok->isNot(tok::l_brace) && !eof()) 1226 nextToken(); 1227 } 1228 1229 parseChildBlock(); 1230 } 1231 1232 bool UnwrappedLineParser::tryToParseBracedList() { 1233 if (FormatTok->BlockKind == BK_Unknown) 1234 calculateBraceTypes(); 1235 assert(FormatTok->BlockKind != BK_Unknown); 1236 if (FormatTok->BlockKind == BK_Block) 1237 return false; 1238 parseBracedList(); 1239 return true; 1240 } 1241 1242 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) { 1243 bool HasError = false; 1244 nextToken(); 1245 1246 // FIXME: Once we have an expression parser in the UnwrappedLineParser, 1247 // replace this by using parseAssigmentExpression() inside. 1248 do { 1249 if (Style.Language == FormatStyle::LK_JavaScript) { 1250 if (FormatTok->isOneOf(Keywords.kw_async, Keywords.kw_function)) { 1251 tryToParseJSFunction(); 1252 continue; 1253 } 1254 if (FormatTok->is(TT_JsFatArrow)) { 1255 nextToken(); 1256 // Fat arrows can be followed by simple expressions or by child blocks 1257 // in curly braces. 1258 if (FormatTok->is(tok::l_brace)) { 1259 parseChildBlock(); 1260 continue; 1261 } 1262 } 1263 } 1264 switch (FormatTok->Tok.getKind()) { 1265 case tok::caret: 1266 nextToken(); 1267 if (FormatTok->is(tok::l_brace)) { 1268 parseChildBlock(); 1269 } 1270 break; 1271 case tok::l_square: 1272 tryToParseLambda(); 1273 break; 1274 case tok::l_brace: 1275 // Assume there are no blocks inside a braced init list apart 1276 // from the ones we explicitly parse out (like lambdas). 1277 FormatTok->BlockKind = BK_BracedInit; 1278 parseBracedList(); 1279 break; 1280 case tok::l_paren: 1281 parseParens(); 1282 // JavaScript can just have free standing methods and getters/setters in 1283 // object literals. Detect them by a "{" following ")". 1284 if (Style.Language == FormatStyle::LK_JavaScript) { 1285 if (FormatTok->is(tok::l_brace)) 1286 parseChildBlock(); 1287 break; 1288 } 1289 break; 1290 case tok::r_brace: 1291 nextToken(); 1292 return !HasError; 1293 case tok::semi: 1294 // JavaScript (or more precisely TypeScript) can have semicolons in braced 1295 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be 1296 // used for error recovery if we have otherwise determined that this is 1297 // a braced list. 1298 if (Style.Language == FormatStyle::LK_JavaScript) { 1299 nextToken(); 1300 break; 1301 } 1302 HasError = true; 1303 if (!ContinueOnSemicolons) 1304 return !HasError; 1305 nextToken(); 1306 break; 1307 case tok::comma: 1308 nextToken(); 1309 break; 1310 default: 1311 nextToken(); 1312 break; 1313 } 1314 } while (!eof()); 1315 return false; 1316 } 1317 1318 void UnwrappedLineParser::parseParens() { 1319 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); 1320 nextToken(); 1321 do { 1322 switch (FormatTok->Tok.getKind()) { 1323 case tok::l_paren: 1324 parseParens(); 1325 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace)) 1326 parseChildBlock(); 1327 break; 1328 case tok::r_paren: 1329 nextToken(); 1330 return; 1331 case tok::r_brace: 1332 // A "}" inside parenthesis is an error if there wasn't a matching "{". 1333 return; 1334 case tok::l_square: 1335 tryToParseLambda(); 1336 break; 1337 case tok::l_brace: 1338 if (!tryToParseBracedList()) 1339 parseChildBlock(); 1340 break; 1341 case tok::at: 1342 nextToken(); 1343 if (FormatTok->Tok.is(tok::l_brace)) 1344 parseBracedList(); 1345 break; 1346 case tok::identifier: 1347 if (Style.Language == FormatStyle::LK_JavaScript && 1348 FormatTok->isOneOf(Keywords.kw_async, Keywords.kw_function)) 1349 tryToParseJSFunction(); 1350 else 1351 nextToken(); 1352 break; 1353 default: 1354 nextToken(); 1355 break; 1356 } 1357 } while (!eof()); 1358 } 1359 1360 void UnwrappedLineParser::parseSquare() { 1361 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); 1362 if (tryToParseLambda()) 1363 return; 1364 do { 1365 switch (FormatTok->Tok.getKind()) { 1366 case tok::l_paren: 1367 parseParens(); 1368 break; 1369 case tok::r_square: 1370 nextToken(); 1371 return; 1372 case tok::r_brace: 1373 // A "}" inside parenthesis is an error if there wasn't a matching "{". 1374 return; 1375 case tok::l_square: 1376 parseSquare(); 1377 break; 1378 case tok::l_brace: { 1379 if (!tryToParseBracedList()) 1380 parseChildBlock(); 1381 break; 1382 } 1383 case tok::at: 1384 nextToken(); 1385 if (FormatTok->Tok.is(tok::l_brace)) 1386 parseBracedList(); 1387 break; 1388 default: 1389 nextToken(); 1390 break; 1391 } 1392 } while (!eof()); 1393 } 1394 1395 void UnwrappedLineParser::parseIfThenElse() { 1396 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); 1397 nextToken(); 1398 if (FormatTok->Tok.is(tok::l_paren)) 1399 parseParens(); 1400 bool NeedsUnwrappedLine = false; 1401 if (FormatTok->Tok.is(tok::l_brace)) { 1402 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1403 parseBlock(/*MustBeDeclaration=*/false); 1404 if (Style.BraceWrapping.BeforeElse) 1405 addUnwrappedLine(); 1406 else 1407 NeedsUnwrappedLine = true; 1408 } else { 1409 addUnwrappedLine(); 1410 ++Line->Level; 1411 parseStructuralElement(); 1412 --Line->Level; 1413 } 1414 if (FormatTok->Tok.is(tok::kw_else)) { 1415 nextToken(); 1416 if (FormatTok->Tok.is(tok::l_brace)) { 1417 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1418 parseBlock(/*MustBeDeclaration=*/false); 1419 addUnwrappedLine(); 1420 } else if (FormatTok->Tok.is(tok::kw_if)) { 1421 parseIfThenElse(); 1422 } else { 1423 addUnwrappedLine(); 1424 ++Line->Level; 1425 parseStructuralElement(); 1426 --Line->Level; 1427 } 1428 } else if (NeedsUnwrappedLine) { 1429 addUnwrappedLine(); 1430 } 1431 } 1432 1433 void UnwrappedLineParser::parseTryCatch() { 1434 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected"); 1435 nextToken(); 1436 bool NeedsUnwrappedLine = false; 1437 if (FormatTok->is(tok::colon)) { 1438 // We are in a function try block, what comes is an initializer list. 1439 nextToken(); 1440 while (FormatTok->is(tok::identifier)) { 1441 nextToken(); 1442 if (FormatTok->is(tok::l_paren)) 1443 parseParens(); 1444 if (FormatTok->is(tok::comma)) 1445 nextToken(); 1446 } 1447 } 1448 // Parse try with resource. 1449 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) { 1450 parseParens(); 1451 } 1452 if (FormatTok->is(tok::l_brace)) { 1453 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1454 parseBlock(/*MustBeDeclaration=*/false); 1455 if (Style.BraceWrapping.BeforeCatch) { 1456 addUnwrappedLine(); 1457 } else { 1458 NeedsUnwrappedLine = true; 1459 } 1460 } else if (!FormatTok->is(tok::kw_catch)) { 1461 // The C++ standard requires a compound-statement after a try. 1462 // If there's none, we try to assume there's a structuralElement 1463 // and try to continue. 1464 addUnwrappedLine(); 1465 ++Line->Level; 1466 parseStructuralElement(); 1467 --Line->Level; 1468 } 1469 while (1) { 1470 if (FormatTok->is(tok::at)) 1471 nextToken(); 1472 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, 1473 tok::kw___finally) || 1474 ((Style.Language == FormatStyle::LK_Java || 1475 Style.Language == FormatStyle::LK_JavaScript) && 1476 FormatTok->is(Keywords.kw_finally)) || 1477 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || 1478 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) 1479 break; 1480 nextToken(); 1481 while (FormatTok->isNot(tok::l_brace)) { 1482 if (FormatTok->is(tok::l_paren)) { 1483 parseParens(); 1484 continue; 1485 } 1486 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) 1487 return; 1488 nextToken(); 1489 } 1490 NeedsUnwrappedLine = false; 1491 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1492 parseBlock(/*MustBeDeclaration=*/false); 1493 if (Style.BraceWrapping.BeforeCatch) 1494 addUnwrappedLine(); 1495 else 1496 NeedsUnwrappedLine = true; 1497 } 1498 if (NeedsUnwrappedLine) 1499 addUnwrappedLine(); 1500 } 1501 1502 void UnwrappedLineParser::parseNamespace() { 1503 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); 1504 1505 const FormatToken &InitialToken = *FormatTok; 1506 nextToken(); 1507 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon)) 1508 nextToken(); 1509 if (FormatTok->Tok.is(tok::l_brace)) { 1510 if (ShouldBreakBeforeBrace(Style, InitialToken)) 1511 addUnwrappedLine(); 1512 1513 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || 1514 (Style.NamespaceIndentation == FormatStyle::NI_Inner && 1515 DeclarationScopeStack.size() > 1); 1516 parseBlock(/*MustBeDeclaration=*/true, AddLevel); 1517 // Munch the semicolon after a namespace. This is more common than one would 1518 // think. Puttin the semicolon into its own line is very ugly. 1519 if (FormatTok->Tok.is(tok::semi)) 1520 nextToken(); 1521 addUnwrappedLine(); 1522 } 1523 // FIXME: Add error handling. 1524 } 1525 1526 void UnwrappedLineParser::parseNew() { 1527 assert(FormatTok->is(tok::kw_new) && "'new' expected"); 1528 nextToken(); 1529 if (Style.Language != FormatStyle::LK_Java) 1530 return; 1531 1532 // In Java, we can parse everything up to the parens, which aren't optional. 1533 do { 1534 // There should not be a ;, { or } before the new's open paren. 1535 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace)) 1536 return; 1537 1538 // Consume the parens. 1539 if (FormatTok->is(tok::l_paren)) { 1540 parseParens(); 1541 1542 // If there is a class body of an anonymous class, consume that as child. 1543 if (FormatTok->is(tok::l_brace)) 1544 parseChildBlock(); 1545 return; 1546 } 1547 nextToken(); 1548 } while (!eof()); 1549 } 1550 1551 void UnwrappedLineParser::parseForOrWhileLoop() { 1552 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && 1553 "'for', 'while' or foreach macro expected"); 1554 nextToken(); 1555 if (FormatTok->Tok.is(tok::l_paren)) 1556 parseParens(); 1557 if (FormatTok->Tok.is(tok::l_brace)) { 1558 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1559 parseBlock(/*MustBeDeclaration=*/false); 1560 addUnwrappedLine(); 1561 } else { 1562 addUnwrappedLine(); 1563 ++Line->Level; 1564 parseStructuralElement(); 1565 --Line->Level; 1566 } 1567 } 1568 1569 void UnwrappedLineParser::parseDoWhile() { 1570 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); 1571 nextToken(); 1572 if (FormatTok->Tok.is(tok::l_brace)) { 1573 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1574 parseBlock(/*MustBeDeclaration=*/false); 1575 if (Style.BraceWrapping.IndentBraces) 1576 addUnwrappedLine(); 1577 } else { 1578 addUnwrappedLine(); 1579 ++Line->Level; 1580 parseStructuralElement(); 1581 --Line->Level; 1582 } 1583 1584 // FIXME: Add error handling. 1585 if (!FormatTok->Tok.is(tok::kw_while)) { 1586 addUnwrappedLine(); 1587 return; 1588 } 1589 1590 nextToken(); 1591 parseStructuralElement(); 1592 } 1593 1594 void UnwrappedLineParser::parseLabel() { 1595 nextToken(); 1596 unsigned OldLineLevel = Line->Level; 1597 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) 1598 --Line->Level; 1599 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { 1600 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1601 parseBlock(/*MustBeDeclaration=*/false); 1602 if (FormatTok->Tok.is(tok::kw_break)) { 1603 if (Style.BraceWrapping.AfterControlStatement) 1604 addUnwrappedLine(); 1605 parseStructuralElement(); 1606 } 1607 addUnwrappedLine(); 1608 } else { 1609 if (FormatTok->is(tok::semi)) 1610 nextToken(); 1611 addUnwrappedLine(); 1612 } 1613 Line->Level = OldLineLevel; 1614 if (FormatTok->isNot(tok::l_brace)) { 1615 parseStructuralElement(); 1616 addUnwrappedLine(); 1617 } 1618 } 1619 1620 void UnwrappedLineParser::parseCaseLabel() { 1621 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); 1622 // FIXME: fix handling of complex expressions here. 1623 do { 1624 nextToken(); 1625 } while (!eof() && !FormatTok->Tok.is(tok::colon)); 1626 parseLabel(); 1627 } 1628 1629 void UnwrappedLineParser::parseSwitch() { 1630 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); 1631 nextToken(); 1632 if (FormatTok->Tok.is(tok::l_paren)) 1633 parseParens(); 1634 if (FormatTok->Tok.is(tok::l_brace)) { 1635 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1636 parseBlock(/*MustBeDeclaration=*/false); 1637 addUnwrappedLine(); 1638 } else { 1639 addUnwrappedLine(); 1640 ++Line->Level; 1641 parseStructuralElement(); 1642 --Line->Level; 1643 } 1644 } 1645 1646 void UnwrappedLineParser::parseAccessSpecifier() { 1647 nextToken(); 1648 // Understand Qt's slots. 1649 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots)) 1650 nextToken(); 1651 // Otherwise, we don't know what it is, and we'd better keep the next token. 1652 if (FormatTok->Tok.is(tok::colon)) 1653 nextToken(); 1654 addUnwrappedLine(); 1655 } 1656 1657 bool UnwrappedLineParser::parseEnum() { 1658 // Won't be 'enum' for NS_ENUMs. 1659 if (FormatTok->Tok.is(tok::kw_enum)) 1660 nextToken(); 1661 1662 // In TypeScript, "enum" can also be used as property name, e.g. in interface 1663 // declarations. An "enum" keyword followed by a colon would be a syntax 1664 // error and thus assume it is just an identifier. 1665 if (Style.Language == FormatStyle::LK_JavaScript && 1666 FormatTok->isOneOf(tok::colon, tok::question)) 1667 return false; 1668 1669 // Eat up enum class ... 1670 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) 1671 nextToken(); 1672 1673 while (FormatTok->Tok.getIdentifierInfo() || 1674 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, 1675 tok::greater, tok::comma, tok::question)) { 1676 nextToken(); 1677 // We can have macros or attributes in between 'enum' and the enum name. 1678 if (FormatTok->is(tok::l_paren)) 1679 parseParens(); 1680 if (FormatTok->is(tok::identifier)) { 1681 nextToken(); 1682 // If there are two identifiers in a row, this is likely an elaborate 1683 // return type. In Java, this can be "implements", etc. 1684 if (Style.Language == FormatStyle::LK_Cpp && 1685 FormatTok->is(tok::identifier)) 1686 return false; 1687 } 1688 } 1689 1690 // Just a declaration or something is wrong. 1691 if (FormatTok->isNot(tok::l_brace)) 1692 return true; 1693 FormatTok->BlockKind = BK_Block; 1694 1695 if (Style.Language == FormatStyle::LK_Java) { 1696 // Java enums are different. 1697 parseJavaEnumBody(); 1698 return true; 1699 } 1700 if (Style.Language == FormatStyle::LK_Proto) { 1701 parseBlock(/*MustBeDeclaration=*/true); 1702 return true; 1703 } 1704 1705 // Parse enum body. 1706 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); 1707 if (HasError) { 1708 if (FormatTok->is(tok::semi)) 1709 nextToken(); 1710 addUnwrappedLine(); 1711 } 1712 return true; 1713 1714 // There is no addUnwrappedLine() here so that we fall through to parsing a 1715 // structural element afterwards. Thus, in "enum A {} n, m;", 1716 // "} n, m;" will end up in one unwrapped line. 1717 } 1718 1719 void UnwrappedLineParser::parseJavaEnumBody() { 1720 // Determine whether the enum is simple, i.e. does not have a semicolon or 1721 // constants with class bodies. Simple enums can be formatted like braced 1722 // lists, contracted to a single line, etc. 1723 unsigned StoredPosition = Tokens->getPosition(); 1724 bool IsSimple = true; 1725 FormatToken *Tok = Tokens->getNextToken(); 1726 while (Tok) { 1727 if (Tok->is(tok::r_brace)) 1728 break; 1729 if (Tok->isOneOf(tok::l_brace, tok::semi)) { 1730 IsSimple = false; 1731 break; 1732 } 1733 // FIXME: This will also mark enums with braces in the arguments to enum 1734 // constants as "not simple". This is probably fine in practice, though. 1735 Tok = Tokens->getNextToken(); 1736 } 1737 FormatTok = Tokens->setPosition(StoredPosition); 1738 1739 if (IsSimple) { 1740 parseBracedList(); 1741 addUnwrappedLine(); 1742 return; 1743 } 1744 1745 // Parse the body of a more complex enum. 1746 // First add a line for everything up to the "{". 1747 nextToken(); 1748 addUnwrappedLine(); 1749 ++Line->Level; 1750 1751 // Parse the enum constants. 1752 while (FormatTok) { 1753 if (FormatTok->is(tok::l_brace)) { 1754 // Parse the constant's class body. 1755 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, 1756 /*MunchSemi=*/false); 1757 } else if (FormatTok->is(tok::l_paren)) { 1758 parseParens(); 1759 } else if (FormatTok->is(tok::comma)) { 1760 nextToken(); 1761 addUnwrappedLine(); 1762 } else if (FormatTok->is(tok::semi)) { 1763 nextToken(); 1764 addUnwrappedLine(); 1765 break; 1766 } else if (FormatTok->is(tok::r_brace)) { 1767 addUnwrappedLine(); 1768 break; 1769 } else { 1770 nextToken(); 1771 } 1772 } 1773 1774 // Parse the class body after the enum's ";" if any. 1775 parseLevel(/*HasOpeningBrace=*/true); 1776 nextToken(); 1777 --Line->Level; 1778 addUnwrappedLine(); 1779 } 1780 1781 void UnwrappedLineParser::parseRecord() { 1782 const FormatToken &InitialToken = *FormatTok; 1783 nextToken(); 1784 1785 // The actual identifier can be a nested name specifier, and in macros 1786 // it is often token-pasted. 1787 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, 1788 tok::kw___attribute, tok::kw___declspec, 1789 tok::kw_alignas) || 1790 ((Style.Language == FormatStyle::LK_Java || 1791 Style.Language == FormatStyle::LK_JavaScript) && 1792 FormatTok->isOneOf(tok::period, tok::comma))) { 1793 bool IsNonMacroIdentifier = 1794 FormatTok->is(tok::identifier) && 1795 FormatTok->TokenText != FormatTok->TokenText.upper(); 1796 nextToken(); 1797 // We can have macros or attributes in between 'class' and the class name. 1798 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren)) 1799 parseParens(); 1800 } 1801 1802 // Note that parsing away template declarations here leads to incorrectly 1803 // accepting function declarations as record declarations. 1804 // In general, we cannot solve this problem. Consider: 1805 // class A<int> B() {} 1806 // which can be a function definition or a class definition when B() is a 1807 // macro. If we find enough real-world cases where this is a problem, we 1808 // can parse for the 'template' keyword in the beginning of the statement, 1809 // and thus rule out the record production in case there is no template 1810 // (this would still leave us with an ambiguity between template function 1811 // and class declarations). 1812 if (FormatTok->isOneOf(tok::colon, tok::less)) { 1813 while (!eof()) { 1814 if (FormatTok->is(tok::l_brace)) { 1815 calculateBraceTypes(/*ExpectClassBody=*/true); 1816 if (!tryToParseBracedList()) 1817 break; 1818 } 1819 if (FormatTok->Tok.is(tok::semi)) 1820 return; 1821 nextToken(); 1822 } 1823 } 1824 if (FormatTok->Tok.is(tok::l_brace)) { 1825 if (ShouldBreakBeforeBrace(Style, InitialToken)) 1826 addUnwrappedLine(); 1827 1828 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, 1829 /*MunchSemi=*/false); 1830 } 1831 // There is no addUnwrappedLine() here so that we fall through to parsing a 1832 // structural element afterwards. Thus, in "class A {} n, m;", 1833 // "} n, m;" will end up in one unwrapped line. 1834 } 1835 1836 void UnwrappedLineParser::parseObjCProtocolList() { 1837 assert(FormatTok->Tok.is(tok::less) && "'<' expected."); 1838 do 1839 nextToken(); 1840 while (!eof() && FormatTok->Tok.isNot(tok::greater)); 1841 nextToken(); // Skip '>'. 1842 } 1843 1844 void UnwrappedLineParser::parseObjCUntilAtEnd() { 1845 do { 1846 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { 1847 nextToken(); 1848 addUnwrappedLine(); 1849 break; 1850 } 1851 if (FormatTok->is(tok::l_brace)) { 1852 parseBlock(/*MustBeDeclaration=*/false); 1853 // In ObjC interfaces, nothing should be following the "}". 1854 addUnwrappedLine(); 1855 } else if (FormatTok->is(tok::r_brace)) { 1856 // Ignore stray "}". parseStructuralElement doesn't consume them. 1857 nextToken(); 1858 addUnwrappedLine(); 1859 } else { 1860 parseStructuralElement(); 1861 } 1862 } while (!eof()); 1863 } 1864 1865 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { 1866 nextToken(); 1867 nextToken(); // interface name 1868 1869 // @interface can be followed by either a base class, or a category. 1870 if (FormatTok->Tok.is(tok::colon)) { 1871 nextToken(); 1872 nextToken(); // base class name 1873 } else if (FormatTok->Tok.is(tok::l_paren)) 1874 // Skip category, if present. 1875 parseParens(); 1876 1877 if (FormatTok->Tok.is(tok::less)) 1878 parseObjCProtocolList(); 1879 1880 if (FormatTok->Tok.is(tok::l_brace)) { 1881 if (Style.BraceWrapping.AfterObjCDeclaration) 1882 addUnwrappedLine(); 1883 parseBlock(/*MustBeDeclaration=*/true); 1884 } 1885 1886 // With instance variables, this puts '}' on its own line. Without instance 1887 // variables, this ends the @interface line. 1888 addUnwrappedLine(); 1889 1890 parseObjCUntilAtEnd(); 1891 } 1892 1893 void UnwrappedLineParser::parseObjCProtocol() { 1894 nextToken(); 1895 nextToken(); // protocol name 1896 1897 if (FormatTok->Tok.is(tok::less)) 1898 parseObjCProtocolList(); 1899 1900 // Check for protocol declaration. 1901 if (FormatTok->Tok.is(tok::semi)) { 1902 nextToken(); 1903 return addUnwrappedLine(); 1904 } 1905 1906 addUnwrappedLine(); 1907 parseObjCUntilAtEnd(); 1908 } 1909 1910 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { 1911 bool IsImport = FormatTok->is(Keywords.kw_import); 1912 assert(IsImport || FormatTok->is(tok::kw_export)); 1913 nextToken(); 1914 1915 // Consume the "default" in "export default class/function". 1916 if (FormatTok->is(tok::kw_default)) 1917 nextToken(); 1918 1919 // Consume "async function", "function" and "default function", so that these 1920 // get parsed as free-standing JS functions, i.e. do not require a trailing 1921 // semicolon. 1922 if (FormatTok->is(Keywords.kw_async)) 1923 nextToken(); 1924 if (FormatTok->is(Keywords.kw_function)) { 1925 nextToken(); 1926 return; 1927 } 1928 1929 // For imports, `export *`, `export {...}`, consume the rest of the line up 1930 // to the terminating `;`. For everything else, just return and continue 1931 // parsing the structural element, i.e. the declaration or expression for 1932 // `export default`. 1933 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) && 1934 !FormatTok->isStringLiteral()) 1935 return; 1936 1937 while (!eof() && FormatTok->isNot(tok::semi)) { 1938 if (FormatTok->is(tok::l_brace)) { 1939 FormatTok->BlockKind = BK_Block; 1940 parseBracedList(); 1941 } else { 1942 nextToken(); 1943 } 1944 } 1945 } 1946 1947 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, 1948 StringRef Prefix = "") { 1949 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")" 1950 << (Line.InPPDirective ? " MACRO" : "") << ": "; 1951 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 1952 E = Line.Tokens.end(); 1953 I != E; ++I) { 1954 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] "; 1955 } 1956 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 1957 E = Line.Tokens.end(); 1958 I != E; ++I) { 1959 const UnwrappedLineNode &Node = *I; 1960 for (SmallVectorImpl<UnwrappedLine>::const_iterator 1961 I = Node.Children.begin(), 1962 E = Node.Children.end(); 1963 I != E; ++I) { 1964 printDebugInfo(*I, "\nChild: "); 1965 } 1966 } 1967 llvm::dbgs() << "\n"; 1968 } 1969 1970 void UnwrappedLineParser::addUnwrappedLine() { 1971 if (Line->Tokens.empty()) 1972 return; 1973 DEBUG({ 1974 if (CurrentLines == &Lines) 1975 printDebugInfo(*Line); 1976 }); 1977 CurrentLines->push_back(std::move(*Line)); 1978 Line->Tokens.clear(); 1979 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { 1980 CurrentLines->append( 1981 std::make_move_iterator(PreprocessorDirectives.begin()), 1982 std::make_move_iterator(PreprocessorDirectives.end())); 1983 PreprocessorDirectives.clear(); 1984 } 1985 } 1986 1987 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } 1988 1989 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { 1990 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && 1991 FormatTok.NewlinesBefore > 0; 1992 } 1993 1994 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { 1995 bool JustComments = Line->Tokens.empty(); 1996 for (SmallVectorImpl<FormatToken *>::const_iterator 1997 I = CommentsBeforeNextToken.begin(), 1998 E = CommentsBeforeNextToken.end(); 1999 I != E; ++I) { 2000 if (isOnNewLine(**I) && JustComments) 2001 addUnwrappedLine(); 2002 pushToken(*I); 2003 } 2004 if (NewlineBeforeNext && JustComments) 2005 addUnwrappedLine(); 2006 CommentsBeforeNextToken.clear(); 2007 } 2008 2009 void UnwrappedLineParser::nextToken() { 2010 if (eof()) 2011 return; 2012 flushComments(isOnNewLine(*FormatTok)); 2013 pushToken(FormatTok); 2014 if (Style.Language != FormatStyle::LK_JavaScript) 2015 readToken(); 2016 else 2017 readTokenWithJavaScriptASI(); 2018 } 2019 2020 const FormatToken *UnwrappedLineParser::getPreviousToken() { 2021 // FIXME: This is a dirty way to access the previous token. Find a better 2022 // solution. 2023 if (!Line || Line->Tokens.empty()) 2024 return nullptr; 2025 return Line->Tokens.back().Tok; 2026 } 2027 2028 void UnwrappedLineParser::readToken() { 2029 bool CommentsInCurrentLine = true; 2030 do { 2031 FormatTok = Tokens->getNextToken(); 2032 assert(FormatTok); 2033 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && 2034 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { 2035 // If there is an unfinished unwrapped line, we flush the preprocessor 2036 // directives only after that unwrapped line was finished later. 2037 bool SwitchToPreprocessorLines = !Line->Tokens.empty(); 2038 ScopedLineState BlockState(*this, SwitchToPreprocessorLines); 2039 // Comments stored before the preprocessor directive need to be output 2040 // before the preprocessor directive, at the same level as the 2041 // preprocessor directive, as we consider them to apply to the directive. 2042 flushComments(isOnNewLine(*FormatTok)); 2043 parsePPDirective(); 2044 } 2045 while (FormatTok->Type == TT_ConflictStart || 2046 FormatTok->Type == TT_ConflictEnd || 2047 FormatTok->Type == TT_ConflictAlternative) { 2048 if (FormatTok->Type == TT_ConflictStart) { 2049 conditionalCompilationStart(/*Unreachable=*/false); 2050 } else if (FormatTok->Type == TT_ConflictAlternative) { 2051 conditionalCompilationAlternative(); 2052 } else if (FormatTok->Type == TT_ConflictEnd) { 2053 conditionalCompilationEnd(); 2054 } 2055 FormatTok = Tokens->getNextToken(); 2056 FormatTok->MustBreakBefore = true; 2057 } 2058 2059 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) && 2060 !Line->InPPDirective) { 2061 continue; 2062 } 2063 2064 if (!FormatTok->Tok.is(tok::comment)) 2065 return; 2066 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) { 2067 CommentsInCurrentLine = false; 2068 } 2069 if (CommentsInCurrentLine) { 2070 pushToken(FormatTok); 2071 } else { 2072 CommentsBeforeNextToken.push_back(FormatTok); 2073 } 2074 } while (!eof()); 2075 } 2076 2077 void UnwrappedLineParser::pushToken(FormatToken *Tok) { 2078 Line->Tokens.push_back(UnwrappedLineNode(Tok)); 2079 if (MustBreakBeforeNextToken) { 2080 Line->Tokens.back().Tok->MustBreakBefore = true; 2081 MustBreakBeforeNextToken = false; 2082 } 2083 } 2084 2085 } // end namespace format 2086 } // end namespace clang 2087