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/Support/Debug.h" 18 19 #define DEBUG_TYPE "format-parser" 20 21 namespace clang { 22 namespace format { 23 24 class FormatTokenSource { 25 public: 26 virtual ~FormatTokenSource() {} 27 virtual FormatToken *getNextToken() = 0; 28 29 virtual unsigned getPosition() = 0; 30 virtual FormatToken *setPosition(unsigned Position) = 0; 31 }; 32 33 namespace { 34 35 class ScopedDeclarationState { 36 public: 37 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack, 38 bool MustBeDeclaration) 39 : Line(Line), Stack(Stack) { 40 Line.MustBeDeclaration = MustBeDeclaration; 41 Stack.push_back(MustBeDeclaration); 42 } 43 ~ScopedDeclarationState() { 44 Stack.pop_back(); 45 if (!Stack.empty()) 46 Line.MustBeDeclaration = Stack.back(); 47 else 48 Line.MustBeDeclaration = true; 49 } 50 51 private: 52 UnwrappedLine &Line; 53 std::vector<bool> &Stack; 54 }; 55 56 class ScopedMacroState : public FormatTokenSource { 57 public: 58 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, 59 FormatToken *&ResetToken, bool &StructuralError) 60 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), 61 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), 62 StructuralError(StructuralError), 63 PreviousStructuralError(StructuralError), Token(nullptr) { 64 TokenSource = this; 65 Line.Level = 0; 66 Line.InPPDirective = true; 67 } 68 69 ~ScopedMacroState() { 70 TokenSource = PreviousTokenSource; 71 ResetToken = Token; 72 Line.InPPDirective = false; 73 Line.Level = PreviousLineLevel; 74 StructuralError = PreviousStructuralError; 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 bool &StructuralError; 114 bool PreviousStructuralError; 115 116 FormatToken *Token; 117 }; 118 119 } // end anonymous namespace 120 121 class ScopedLineState { 122 public: 123 ScopedLineState(UnwrappedLineParser &Parser, 124 bool SwitchToPreprocessorLines = false) 125 : Parser(Parser), OriginalLines(Parser.CurrentLines) { 126 if (SwitchToPreprocessorLines) 127 Parser.CurrentLines = &Parser.PreprocessorDirectives; 128 else if (!Parser.Line->Tokens.empty()) 129 Parser.CurrentLines = &Parser.Line->Tokens.back().Children; 130 PreBlockLine = std::move(Parser.Line); 131 Parser.Line = llvm::make_unique<UnwrappedLine>(); 132 Parser.Line->Level = PreBlockLine->Level; 133 Parser.Line->InPPDirective = PreBlockLine->InPPDirective; 134 } 135 136 ~ScopedLineState() { 137 if (!Parser.Line->Tokens.empty()) { 138 Parser.addUnwrappedLine(); 139 } 140 assert(Parser.Line->Tokens.empty()); 141 Parser.Line = std::move(PreBlockLine); 142 if (Parser.CurrentLines == &Parser.PreprocessorDirectives) 143 Parser.MustBreakBeforeNextToken = true; 144 Parser.CurrentLines = OriginalLines; 145 } 146 147 private: 148 UnwrappedLineParser &Parser; 149 150 std::unique_ptr<UnwrappedLine> PreBlockLine; 151 SmallVectorImpl<UnwrappedLine> *OriginalLines; 152 }; 153 154 class CompoundStatementIndenter { 155 public: 156 CompoundStatementIndenter(UnwrappedLineParser *Parser, 157 const FormatStyle &Style, unsigned &LineLevel) 158 : LineLevel(LineLevel), OldLineLevel(LineLevel) { 159 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) { 160 Parser->addUnwrappedLine(); 161 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) { 162 Parser->addUnwrappedLine(); 163 ++LineLevel; 164 } 165 } 166 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } 167 168 private: 169 unsigned &LineLevel; 170 unsigned OldLineLevel; 171 }; 172 173 namespace { 174 175 class IndexedTokenSource : public FormatTokenSource { 176 public: 177 IndexedTokenSource(ArrayRef<FormatToken *> Tokens) 178 : Tokens(Tokens), Position(-1) {} 179 180 FormatToken *getNextToken() override { 181 ++Position; 182 return Tokens[Position]; 183 } 184 185 unsigned getPosition() override { 186 assert(Position >= 0); 187 return Position; 188 } 189 190 FormatToken *setPosition(unsigned P) override { 191 Position = P; 192 return Tokens[Position]; 193 } 194 195 void reset() { Position = -1; } 196 197 private: 198 ArrayRef<FormatToken *> Tokens; 199 int Position; 200 }; 201 202 } // end anonymous namespace 203 204 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, 205 ArrayRef<FormatToken *> Tokens, 206 UnwrappedLineConsumer &Callback) 207 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), 208 CurrentLines(&Lines), StructuralError(false), Style(Style), 209 Tokens(nullptr), Callback(Callback), AllTokens(Tokens), 210 PPBranchLevel(-1) {} 211 212 void UnwrappedLineParser::reset() { 213 PPBranchLevel = -1; 214 Line.reset(new UnwrappedLine); 215 CommentsBeforeNextToken.clear(); 216 FormatTok = nullptr; 217 MustBreakBeforeNextToken = false; 218 PreprocessorDirectives.clear(); 219 CurrentLines = &Lines; 220 DeclarationScopeStack.clear(); 221 StructuralError = false; 222 PPStack.clear(); 223 } 224 225 bool UnwrappedLineParser::parse() { 226 IndexedTokenSource TokenSource(AllTokens); 227 do { 228 DEBUG(llvm::dbgs() << "----\n"); 229 reset(); 230 Tokens = &TokenSource; 231 TokenSource.reset(); 232 233 readToken(); 234 parseFile(); 235 // Create line with eof token. 236 pushToken(FormatTok); 237 addUnwrappedLine(); 238 239 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(), 240 E = Lines.end(); 241 I != E; ++I) { 242 Callback.consumeUnwrappedLine(*I); 243 } 244 Callback.finishRun(); 245 Lines.clear(); 246 while (!PPLevelBranchIndex.empty() && 247 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) { 248 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); 249 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); 250 } 251 if (!PPLevelBranchIndex.empty()) { 252 ++PPLevelBranchIndex.back(); 253 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); 254 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); 255 } 256 } while (!PPLevelBranchIndex.empty()); 257 258 return StructuralError; 259 } 260 261 void UnwrappedLineParser::parseFile() { 262 ScopedDeclarationState DeclarationState( 263 *Line, DeclarationScopeStack, 264 /*MustBeDeclaration=*/ !Line->InPPDirective); 265 parseLevel(/*HasOpeningBrace=*/false); 266 // Make sure to format the remaining tokens. 267 flushComments(true); 268 addUnwrappedLine(); 269 } 270 271 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { 272 bool SwitchLabelEncountered = false; 273 do { 274 switch (FormatTok->Tok.getKind()) { 275 case tok::comment: 276 nextToken(); 277 addUnwrappedLine(); 278 break; 279 case tok::l_brace: 280 // FIXME: Add parameter whether this can happen - if this happens, we must 281 // be in a non-declaration context. 282 parseBlock(/*MustBeDeclaration=*/false); 283 addUnwrappedLine(); 284 break; 285 case tok::r_brace: 286 if (HasOpeningBrace) 287 return; 288 StructuralError = true; 289 nextToken(); 290 addUnwrappedLine(); 291 break; 292 case tok::kw_default: 293 case tok::kw_case: 294 if (!SwitchLabelEncountered && 295 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1))) 296 ++Line->Level; 297 SwitchLabelEncountered = true; 298 parseStructuralElement(); 299 break; 300 default: 301 parseStructuralElement(); 302 break; 303 } 304 } while (!eof()); 305 } 306 307 void UnwrappedLineParser::calculateBraceTypes() { 308 // We'll parse forward through the tokens until we hit 309 // a closing brace or eof - note that getNextToken() will 310 // parse macros, so this will magically work inside macro 311 // definitions, too. 312 unsigned StoredPosition = Tokens->getPosition(); 313 unsigned Position = StoredPosition; 314 FormatToken *Tok = FormatTok; 315 // Keep a stack of positions of lbrace tokens. We will 316 // update information about whether an lbrace starts a 317 // braced init list or a different block during the loop. 318 SmallVector<FormatToken *, 8> LBraceStack; 319 assert(Tok->Tok.is(tok::l_brace)); 320 do { 321 // Get next none-comment token. 322 FormatToken *NextTok; 323 unsigned ReadTokens = 0; 324 do { 325 NextTok = Tokens->getNextToken(); 326 ++ReadTokens; 327 } while (NextTok->is(tok::comment)); 328 329 switch (Tok->Tok.getKind()) { 330 case tok::l_brace: 331 LBraceStack.push_back(Tok); 332 break; 333 case tok::r_brace: 334 if (!LBraceStack.empty()) { 335 if (LBraceStack.back()->BlockKind == BK_Unknown) { 336 bool ProbablyBracedList = false; 337 if (Style.Language == FormatStyle::LK_Proto) { 338 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square); 339 } else { 340 // Using OriginalColumn to distinguish between ObjC methods and 341 // binary operators is a bit hacky. 342 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) && 343 NextTok->OriginalColumn == 0; 344 345 // If there is a comma, semicolon or right paren after the closing 346 // brace, we assume this is a braced initializer list. Note that 347 // regardless how we mark inner braces here, we will overwrite the 348 // BlockKind later if we parse a braced list (where all blocks 349 // inside are by default braced lists), or when we explicitly detect 350 // blocks (for example while parsing lambdas). 351 // 352 // We exclude + and - as they can be ObjC visibility modifiers. 353 ProbablyBracedList = 354 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon, 355 tok::r_paren, tok::r_square, tok::l_brace, 356 tok::l_paren, tok::ellipsis) || 357 (NextTok->isBinaryOperator() && !NextIsObjCMethod); 358 } 359 if (ProbablyBracedList) { 360 Tok->BlockKind = BK_BracedInit; 361 LBraceStack.back()->BlockKind = BK_BracedInit; 362 } else { 363 Tok->BlockKind = BK_Block; 364 LBraceStack.back()->BlockKind = BK_Block; 365 } 366 } 367 LBraceStack.pop_back(); 368 } 369 break; 370 case tok::at: 371 case tok::semi: 372 case tok::kw_if: 373 case tok::kw_while: 374 case tok::kw_for: 375 case tok::kw_switch: 376 case tok::kw_try: 377 if (!LBraceStack.empty()) 378 LBraceStack.back()->BlockKind = BK_Block; 379 break; 380 default: 381 break; 382 } 383 Tok = NextTok; 384 Position += ReadTokens; 385 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty()); 386 // Assume other blocks for all unclosed opening braces. 387 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { 388 if (LBraceStack[i]->BlockKind == BK_Unknown) 389 LBraceStack[i]->BlockKind = BK_Block; 390 } 391 392 FormatTok = Tokens->setPosition(StoredPosition); 393 } 394 395 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, 396 bool MunchSemi) { 397 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected"); 398 unsigned InitialLevel = Line->Level; 399 nextToken(); 400 401 addUnwrappedLine(); 402 403 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 404 MustBeDeclaration); 405 if (AddLevel) 406 ++Line->Level; 407 parseLevel(/*HasOpeningBrace=*/true); 408 409 if (!FormatTok->Tok.is(tok::r_brace)) { 410 Line->Level = InitialLevel; 411 StructuralError = true; 412 return; 413 } 414 415 nextToken(); // Munch the closing brace. 416 if (MunchSemi && FormatTok->Tok.is(tok::semi)) 417 nextToken(); 418 Line->Level = InitialLevel; 419 } 420 421 static bool IsGoogScope(const UnwrappedLine &Line) { 422 if (Line.Tokens.size() < 4) 423 return false; 424 auto I = Line.Tokens.begin(); 425 if (I->Tok->TokenText != "goog") 426 return false; 427 ++I; 428 if (I->Tok->isNot(tok::period)) 429 return false; 430 ++I; 431 if (I->Tok->TokenText != "scope") 432 return false; 433 ++I; 434 return I->Tok->is(tok::l_paren); 435 } 436 437 void UnwrappedLineParser::parseChildBlock() { 438 FormatTok->BlockKind = BK_Block; 439 nextToken(); 440 { 441 bool GoogScope = 442 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line); 443 ScopedLineState LineState(*this); 444 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 445 /*MustBeDeclaration=*/false); 446 Line->Level += GoogScope ? 0 : 1; 447 parseLevel(/*HasOpeningBrace=*/true); 448 Line->Level -= GoogScope ? 0 : 1; 449 } 450 nextToken(); 451 } 452 453 void UnwrappedLineParser::parsePPDirective() { 454 assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); 455 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError); 456 nextToken(); 457 458 if (!FormatTok->Tok.getIdentifierInfo()) { 459 parsePPUnknown(); 460 return; 461 } 462 463 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { 464 case tok::pp_define: 465 parsePPDefine(); 466 return; 467 case tok::pp_if: 468 parsePPIf(/*IfDef=*/false); 469 break; 470 case tok::pp_ifdef: 471 case tok::pp_ifndef: 472 parsePPIf(/*IfDef=*/true); 473 break; 474 case tok::pp_else: 475 parsePPElse(); 476 break; 477 case tok::pp_elif: 478 parsePPElIf(); 479 break; 480 case tok::pp_endif: 481 parsePPEndIf(); 482 break; 483 default: 484 parsePPUnknown(); 485 break; 486 } 487 } 488 489 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { 490 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable)) 491 PPStack.push_back(PP_Unreachable); 492 else 493 PPStack.push_back(PP_Conditional); 494 } 495 496 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { 497 ++PPBranchLevel; 498 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); 499 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { 500 PPLevelBranchIndex.push_back(0); 501 PPLevelBranchCount.push_back(0); 502 } 503 PPChainBranchIndex.push(0); 504 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; 505 conditionalCompilationCondition(Unreachable || Skip); 506 } 507 508 void UnwrappedLineParser::conditionalCompilationAlternative() { 509 if (!PPStack.empty()) 510 PPStack.pop_back(); 511 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 512 if (!PPChainBranchIndex.empty()) 513 ++PPChainBranchIndex.top(); 514 conditionalCompilationCondition( 515 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && 516 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()); 517 } 518 519 void UnwrappedLineParser::conditionalCompilationEnd() { 520 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 521 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { 522 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { 523 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; 524 } 525 } 526 // Guard against #endif's without #if. 527 if (PPBranchLevel > 0) 528 --PPBranchLevel; 529 if (!PPChainBranchIndex.empty()) 530 PPChainBranchIndex.pop(); 531 if (!PPStack.empty()) 532 PPStack.pop_back(); 533 } 534 535 void UnwrappedLineParser::parsePPIf(bool IfDef) { 536 nextToken(); 537 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() && 538 StringRef(FormatTok->Tok.getLiteralData(), 539 FormatTok->Tok.getLength()) == "0") || 540 FormatTok->Tok.is(tok::kw_false); 541 conditionalCompilationStart(!IfDef && IsLiteralFalse); 542 parsePPUnknown(); 543 } 544 545 void UnwrappedLineParser::parsePPElse() { 546 conditionalCompilationAlternative(); 547 parsePPUnknown(); 548 } 549 550 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } 551 552 void UnwrappedLineParser::parsePPEndIf() { 553 conditionalCompilationEnd(); 554 parsePPUnknown(); 555 } 556 557 void UnwrappedLineParser::parsePPDefine() { 558 nextToken(); 559 560 if (FormatTok->Tok.getKind() != tok::identifier) { 561 parsePPUnknown(); 562 return; 563 } 564 nextToken(); 565 if (FormatTok->Tok.getKind() == tok::l_paren && 566 FormatTok->WhitespaceRange.getBegin() == 567 FormatTok->WhitespaceRange.getEnd()) { 568 parseParens(); 569 } 570 addUnwrappedLine(); 571 Line->Level = 1; 572 573 // Errors during a preprocessor directive can only affect the layout of the 574 // preprocessor directive, and thus we ignore them. An alternative approach 575 // would be to use the same approach we use on the file level (no 576 // re-indentation if there was a structural error) within the macro 577 // definition. 578 parseFile(); 579 } 580 581 void UnwrappedLineParser::parsePPUnknown() { 582 do { 583 nextToken(); 584 } while (!eof()); 585 addUnwrappedLine(); 586 } 587 588 // Here we blacklist certain tokens that are not usually the first token in an 589 // unwrapped line. This is used in attempt to distinguish macro calls without 590 // trailing semicolons from other constructs split to several lines. 591 bool tokenCanStartNewLine(clang::Token Tok) { 592 // Semicolon can be a null-statement, l_square can be a start of a macro or 593 // a C++11 attribute, but this doesn't seem to be common. 594 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && 595 Tok.isNot(tok::l_square) && 596 // Tokens that can only be used as binary operators and a part of 597 // overloaded operator names. 598 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && 599 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && 600 Tok.isNot(tok::less) && Tok.isNot(tok::greater) && 601 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && 602 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && 603 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && 604 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && 605 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && 606 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && 607 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && 608 Tok.isNot(tok::lesslessequal) && 609 // Colon is used in labels, base class lists, initializer lists, 610 // range-based for loops, ternary operator, but should never be the 611 // first token in an unwrapped line. 612 Tok.isNot(tok::colon) && 613 // 'noexcept' is a trailing annotation. 614 Tok.isNot(tok::kw_noexcept); 615 } 616 617 void UnwrappedLineParser::parseStructuralElement() { 618 assert(!FormatTok->Tok.is(tok::l_brace)); 619 switch (FormatTok->Tok.getKind()) { 620 case tok::at: 621 nextToken(); 622 if (FormatTok->Tok.is(tok::l_brace)) { 623 parseBracedList(); 624 break; 625 } 626 switch (FormatTok->Tok.getObjCKeywordID()) { 627 case tok::objc_public: 628 case tok::objc_protected: 629 case tok::objc_package: 630 case tok::objc_private: 631 return parseAccessSpecifier(); 632 case tok::objc_interface: 633 case tok::objc_implementation: 634 return parseObjCInterfaceOrImplementation(); 635 case tok::objc_protocol: 636 return parseObjCProtocol(); 637 case tok::objc_end: 638 return; // Handled by the caller. 639 case tok::objc_optional: 640 case tok::objc_required: 641 nextToken(); 642 addUnwrappedLine(); 643 return; 644 default: 645 break; 646 } 647 break; 648 case tok::kw_namespace: 649 parseNamespace(); 650 return; 651 case tok::kw_inline: 652 nextToken(); 653 if (FormatTok->Tok.is(tok::kw_namespace)) { 654 parseNamespace(); 655 return; 656 } 657 break; 658 case tok::kw_public: 659 case tok::kw_protected: 660 case tok::kw_private: 661 parseAccessSpecifier(); 662 return; 663 case tok::kw_if: 664 parseIfThenElse(); 665 return; 666 case tok::kw_for: 667 case tok::kw_while: 668 parseForOrWhileLoop(); 669 return; 670 case tok::kw_do: 671 parseDoWhile(); 672 return; 673 case tok::kw_switch: 674 parseSwitch(); 675 return; 676 case tok::kw_default: 677 nextToken(); 678 parseLabel(); 679 return; 680 case tok::kw_case: 681 parseCaseLabel(); 682 return; 683 case tok::kw_try: 684 parseTryCatch(); 685 return; 686 case tok::kw_extern: 687 nextToken(); 688 if (FormatTok->Tok.is(tok::string_literal)) { 689 nextToken(); 690 if (FormatTok->Tok.is(tok::l_brace)) { 691 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); 692 addUnwrappedLine(); 693 return; 694 } 695 } 696 break; 697 case tok::identifier: 698 if (FormatTok->IsForEachMacro) { 699 parseForOrWhileLoop(); 700 return; 701 } 702 // In all other cases, parse the declaration. 703 break; 704 default: 705 break; 706 } 707 do { 708 switch (FormatTok->Tok.getKind()) { 709 case tok::at: 710 nextToken(); 711 if (FormatTok->Tok.is(tok::l_brace)) 712 parseBracedList(); 713 break; 714 case tok::kw_enum: 715 parseEnum(); 716 break; 717 case tok::kw_typedef: 718 nextToken(); 719 // FIXME: Use the IdentifierTable instead. 720 if (FormatTok->TokenText == "NS_ENUM") 721 parseEnum(); 722 break; 723 case tok::kw_struct: 724 case tok::kw_union: 725 case tok::kw_class: 726 parseRecord(); 727 // A record declaration or definition is always the start of a structural 728 // element. 729 break; 730 case tok::semi: 731 nextToken(); 732 addUnwrappedLine(); 733 return; 734 case tok::r_brace: 735 addUnwrappedLine(); 736 return; 737 case tok::l_paren: 738 parseParens(); 739 break; 740 case tok::caret: 741 nextToken(); 742 if (FormatTok->Tok.isAnyIdentifier() || 743 FormatTok->isSimpleTypeSpecifier()) 744 nextToken(); 745 if (FormatTok->is(tok::l_paren)) 746 parseParens(); 747 if (FormatTok->is(tok::l_brace)) 748 parseChildBlock(); 749 break; 750 case tok::l_brace: 751 if (!tryToParseBracedList()) { 752 // A block outside of parentheses must be the last part of a 753 // structural element. 754 // FIXME: Figure out cases where this is not true, and add projections 755 // for them (the one we know is missing are lambdas). 756 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach) 757 addUnwrappedLine(); 758 FormatTok->Type = TT_FunctionLBrace; 759 parseBlock(/*MustBeDeclaration=*/false); 760 addUnwrappedLine(); 761 return; 762 } 763 // Otherwise this was a braced init list, and the structural 764 // element continues. 765 break; 766 case tok::kw_try: 767 // We arrive here when parsing function-try blocks. 768 parseTryCatch(); 769 return; 770 case tok::identifier: { 771 StringRef Text = FormatTok->TokenText; 772 // Parse function literal unless 'function' is the first token in a line 773 // in which case this should be treated as a free-standing function. 774 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" && 775 Line->Tokens.size() > 0) { 776 tryToParseJSFunction(); 777 break; 778 } 779 nextToken(); 780 if (Line->Tokens.size() == 1) { 781 if (FormatTok->Tok.is(tok::colon)) { 782 parseLabel(); 783 return; 784 } 785 // Recognize function-like macro usages without trailing semicolon. 786 if (FormatTok->Tok.is(tok::l_paren)) { 787 parseParens(); 788 if (FormatTok->NewlinesBefore > 0 && 789 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { 790 addUnwrappedLine(); 791 return; 792 } 793 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 && 794 Text == Text.upper()) { 795 // Recognize free-standing macros like Q_OBJECT. 796 addUnwrappedLine(); 797 return; 798 } 799 } 800 break; 801 } 802 case tok::equal: 803 nextToken(); 804 if (FormatTok->Tok.is(tok::l_brace)) { 805 parseBracedList(); 806 } 807 break; 808 case tok::l_square: 809 parseSquare(); 810 break; 811 default: 812 nextToken(); 813 break; 814 } 815 } while (!eof()); 816 } 817 818 bool UnwrappedLineParser::tryToParseLambda() { 819 // FIXME: This is a dirty way to access the previous token. Find a better 820 // solution. 821 if (!Line->Tokens.empty() && 822 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) || 823 Line->Tokens.back().Tok->closesScope() || 824 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) { 825 nextToken(); 826 return false; 827 } 828 assert(FormatTok->is(tok::l_square)); 829 FormatToken &LSquare = *FormatTok; 830 if (!tryToParseLambdaIntroducer()) 831 return false; 832 833 while (FormatTok->isNot(tok::l_brace)) { 834 if (FormatTok->isSimpleTypeSpecifier()) { 835 nextToken(); 836 continue; 837 } 838 switch (FormatTok->Tok.getKind()) { 839 case tok::l_brace: 840 break; 841 case tok::l_paren: 842 parseParens(); 843 break; 844 case tok::less: 845 case tok::greater: 846 case tok::identifier: 847 case tok::coloncolon: 848 case tok::kw_mutable: 849 nextToken(); 850 break; 851 case tok::arrow: 852 FormatTok->Type = TT_TrailingReturnArrow; 853 nextToken(); 854 break; 855 default: 856 return true; 857 } 858 } 859 LSquare.Type = TT_LambdaLSquare; 860 parseChildBlock(); 861 return true; 862 } 863 864 bool UnwrappedLineParser::tryToParseLambdaIntroducer() { 865 nextToken(); 866 if (FormatTok->is(tok::equal)) { 867 nextToken(); 868 if (FormatTok->is(tok::r_square)) { 869 nextToken(); 870 return true; 871 } 872 if (FormatTok->isNot(tok::comma)) 873 return false; 874 nextToken(); 875 } else if (FormatTok->is(tok::amp)) { 876 nextToken(); 877 if (FormatTok->is(tok::r_square)) { 878 nextToken(); 879 return true; 880 } 881 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) { 882 return false; 883 } 884 if (FormatTok->is(tok::comma)) 885 nextToken(); 886 } else if (FormatTok->is(tok::r_square)) { 887 nextToken(); 888 return true; 889 } 890 do { 891 if (FormatTok->is(tok::amp)) 892 nextToken(); 893 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this)) 894 return false; 895 nextToken(); 896 if (FormatTok->is(tok::ellipsis)) 897 nextToken(); 898 if (FormatTok->is(tok::comma)) { 899 nextToken(); 900 } else if (FormatTok->is(tok::r_square)) { 901 nextToken(); 902 return true; 903 } else { 904 return false; 905 } 906 } while (!eof()); 907 return false; 908 } 909 910 void UnwrappedLineParser::tryToParseJSFunction() { 911 nextToken(); 912 913 // Consume function name. 914 if (FormatTok->is(tok::identifier)) 915 nextToken(); 916 917 if (FormatTok->isNot(tok::l_paren)) 918 return; 919 nextToken(); 920 while (FormatTok->isNot(tok::l_brace)) { 921 // Err on the side of caution in order to avoid consuming the full file in 922 // case of incomplete code. 923 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren, 924 tok::comment)) 925 return; 926 nextToken(); 927 } 928 parseChildBlock(); 929 } 930 931 bool UnwrappedLineParser::tryToParseBracedList() { 932 if (FormatTok->BlockKind == BK_Unknown) 933 calculateBraceTypes(); 934 assert(FormatTok->BlockKind != BK_Unknown); 935 if (FormatTok->BlockKind == BK_Block) 936 return false; 937 parseBracedList(); 938 return true; 939 } 940 941 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) { 942 bool HasError = false; 943 nextToken(); 944 945 // FIXME: Once we have an expression parser in the UnwrappedLineParser, 946 // replace this by using parseAssigmentExpression() inside. 947 do { 948 if (Style.Language == FormatStyle::LK_JavaScript && 949 FormatTok->TokenText == "function") { 950 tryToParseJSFunction(); 951 continue; 952 } 953 switch (FormatTok->Tok.getKind()) { 954 case tok::caret: 955 nextToken(); 956 if (FormatTok->is(tok::l_brace)) { 957 parseChildBlock(); 958 } 959 break; 960 case tok::l_square: 961 tryToParseLambda(); 962 break; 963 case tok::l_brace: 964 // Assume there are no blocks inside a braced init list apart 965 // from the ones we explicitly parse out (like lambdas). 966 FormatTok->BlockKind = BK_BracedInit; 967 parseBracedList(); 968 break; 969 case tok::r_brace: 970 nextToken(); 971 return !HasError; 972 case tok::semi: 973 HasError = true; 974 if (!ContinueOnSemicolons) 975 return !HasError; 976 nextToken(); 977 break; 978 case tok::comma: 979 nextToken(); 980 break; 981 default: 982 nextToken(); 983 break; 984 } 985 } while (!eof()); 986 return false; 987 } 988 989 void UnwrappedLineParser::parseParens() { 990 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); 991 nextToken(); 992 do { 993 switch (FormatTok->Tok.getKind()) { 994 case tok::l_paren: 995 parseParens(); 996 break; 997 case tok::r_paren: 998 nextToken(); 999 return; 1000 case tok::r_brace: 1001 // A "}" inside parenthesis is an error if there wasn't a matching "{". 1002 return; 1003 case tok::l_square: 1004 tryToParseLambda(); 1005 break; 1006 case tok::l_brace: { 1007 if (!tryToParseBracedList()) { 1008 parseChildBlock(); 1009 } 1010 break; 1011 } 1012 case tok::at: 1013 nextToken(); 1014 if (FormatTok->Tok.is(tok::l_brace)) 1015 parseBracedList(); 1016 break; 1017 default: 1018 nextToken(); 1019 break; 1020 } 1021 } while (!eof()); 1022 } 1023 1024 void UnwrappedLineParser::parseSquare() { 1025 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); 1026 if (tryToParseLambda()) 1027 return; 1028 do { 1029 switch (FormatTok->Tok.getKind()) { 1030 case tok::l_paren: 1031 parseParens(); 1032 break; 1033 case tok::r_square: 1034 nextToken(); 1035 return; 1036 case tok::r_brace: 1037 // A "}" inside parenthesis is an error if there wasn't a matching "{". 1038 return; 1039 case tok::l_square: 1040 parseSquare(); 1041 break; 1042 case tok::l_brace: { 1043 if (!tryToParseBracedList()) { 1044 parseChildBlock(); 1045 } 1046 break; 1047 } 1048 case tok::at: 1049 nextToken(); 1050 if (FormatTok->Tok.is(tok::l_brace)) 1051 parseBracedList(); 1052 break; 1053 default: 1054 nextToken(); 1055 break; 1056 } 1057 } while (!eof()); 1058 } 1059 1060 void UnwrappedLineParser::parseIfThenElse() { 1061 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); 1062 nextToken(); 1063 if (FormatTok->Tok.is(tok::l_paren)) 1064 parseParens(); 1065 bool NeedsUnwrappedLine = false; 1066 if (FormatTok->Tok.is(tok::l_brace)) { 1067 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1068 parseBlock(/*MustBeDeclaration=*/false); 1069 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || 1070 Style.BreakBeforeBraces == FormatStyle::BS_GNU) { 1071 addUnwrappedLine(); 1072 } else { 1073 NeedsUnwrappedLine = true; 1074 } 1075 } else { 1076 addUnwrappedLine(); 1077 ++Line->Level; 1078 parseStructuralElement(); 1079 --Line->Level; 1080 } 1081 if (FormatTok->Tok.is(tok::kw_else)) { 1082 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) 1083 addUnwrappedLine(); 1084 nextToken(); 1085 if (FormatTok->Tok.is(tok::l_brace)) { 1086 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1087 parseBlock(/*MustBeDeclaration=*/false); 1088 addUnwrappedLine(); 1089 } else if (FormatTok->Tok.is(tok::kw_if)) { 1090 parseIfThenElse(); 1091 } else { 1092 addUnwrappedLine(); 1093 ++Line->Level; 1094 parseStructuralElement(); 1095 --Line->Level; 1096 } 1097 } else if (NeedsUnwrappedLine) { 1098 addUnwrappedLine(); 1099 } 1100 } 1101 1102 void UnwrappedLineParser::parseTryCatch() { 1103 assert(FormatTok->is(tok::kw_try) && "'try' expected"); 1104 nextToken(); 1105 bool NeedsUnwrappedLine = false; 1106 if (FormatTok->is(tok::colon)) { 1107 // We are in a function try block, what comes is an initializer list. 1108 nextToken(); 1109 while (FormatTok->is(tok::identifier)) { 1110 nextToken(); 1111 if (FormatTok->is(tok::l_paren)) 1112 parseParens(); 1113 else 1114 StructuralError = true; 1115 if (FormatTok->is(tok::comma)) 1116 nextToken(); 1117 } 1118 } 1119 if (FormatTok->is(tok::l_brace)) { 1120 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1121 parseBlock(/*MustBeDeclaration=*/false); 1122 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || 1123 Style.BreakBeforeBraces == FormatStyle::BS_GNU || 1124 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) { 1125 addUnwrappedLine(); 1126 } else { 1127 NeedsUnwrappedLine = true; 1128 } 1129 } else if (!FormatTok->is(tok::kw_catch)) { 1130 // The C++ standard requires a compound-statement after a try. 1131 // If there's none, we try to assume there's a structuralElement 1132 // and try to continue. 1133 StructuralError = true; 1134 addUnwrappedLine(); 1135 ++Line->Level; 1136 parseStructuralElement(); 1137 --Line->Level; 1138 } 1139 while (FormatTok->is(tok::kw_catch) || 1140 (Style.Language == FormatStyle::LK_JavaScript && 1141 FormatTok->TokenText == "finally")) { 1142 nextToken(); 1143 while (FormatTok->isNot(tok::l_brace)) { 1144 if (FormatTok->is(tok::l_paren)) { 1145 parseParens(); 1146 continue; 1147 } 1148 if (FormatTok->isOneOf(tok::semi, tok::r_brace)) 1149 return; 1150 nextToken(); 1151 } 1152 NeedsUnwrappedLine = false; 1153 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1154 parseBlock(/*MustBeDeclaration=*/false); 1155 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || 1156 Style.BreakBeforeBraces == FormatStyle::BS_GNU || 1157 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) { 1158 addUnwrappedLine(); 1159 } else { 1160 NeedsUnwrappedLine = true; 1161 } 1162 } 1163 if (NeedsUnwrappedLine) { 1164 addUnwrappedLine(); 1165 } 1166 } 1167 1168 void UnwrappedLineParser::parseNamespace() { 1169 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); 1170 nextToken(); 1171 if (FormatTok->Tok.is(tok::identifier)) 1172 nextToken(); 1173 if (FormatTok->Tok.is(tok::l_brace)) { 1174 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || 1175 Style.BreakBeforeBraces == FormatStyle::BS_Allman || 1176 Style.BreakBeforeBraces == FormatStyle::BS_GNU) 1177 addUnwrappedLine(); 1178 1179 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || 1180 (Style.NamespaceIndentation == FormatStyle::NI_Inner && 1181 DeclarationScopeStack.size() > 1); 1182 parseBlock(/*MustBeDeclaration=*/true, AddLevel); 1183 // Munch the semicolon after a namespace. This is more common than one would 1184 // think. Puttin the semicolon into its own line is very ugly. 1185 if (FormatTok->Tok.is(tok::semi)) 1186 nextToken(); 1187 addUnwrappedLine(); 1188 } 1189 // FIXME: Add error handling. 1190 } 1191 1192 void UnwrappedLineParser::parseForOrWhileLoop() { 1193 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) || 1194 FormatTok->IsForEachMacro) && 1195 "'for', 'while' or foreach macro expected"); 1196 nextToken(); 1197 if (FormatTok->Tok.is(tok::l_paren)) 1198 parseParens(); 1199 if (FormatTok->Tok.is(tok::l_brace)) { 1200 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1201 parseBlock(/*MustBeDeclaration=*/false); 1202 addUnwrappedLine(); 1203 } else { 1204 addUnwrappedLine(); 1205 ++Line->Level; 1206 parseStructuralElement(); 1207 --Line->Level; 1208 } 1209 } 1210 1211 void UnwrappedLineParser::parseDoWhile() { 1212 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); 1213 nextToken(); 1214 if (FormatTok->Tok.is(tok::l_brace)) { 1215 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1216 parseBlock(/*MustBeDeclaration=*/false); 1217 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) 1218 addUnwrappedLine(); 1219 } else { 1220 addUnwrappedLine(); 1221 ++Line->Level; 1222 parseStructuralElement(); 1223 --Line->Level; 1224 } 1225 1226 // FIXME: Add error handling. 1227 if (!FormatTok->Tok.is(tok::kw_while)) { 1228 addUnwrappedLine(); 1229 return; 1230 } 1231 1232 nextToken(); 1233 parseStructuralElement(); 1234 } 1235 1236 void UnwrappedLineParser::parseLabel() { 1237 nextToken(); 1238 unsigned OldLineLevel = Line->Level; 1239 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) 1240 --Line->Level; 1241 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { 1242 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1243 parseBlock(/*MustBeDeclaration=*/false); 1244 if (FormatTok->Tok.is(tok::kw_break)) { 1245 // "break;" after "}" on its own line only for BS_Allman and BS_GNU 1246 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || 1247 Style.BreakBeforeBraces == FormatStyle::BS_GNU) { 1248 addUnwrappedLine(); 1249 } 1250 parseStructuralElement(); 1251 } 1252 addUnwrappedLine(); 1253 } else { 1254 addUnwrappedLine(); 1255 } 1256 Line->Level = OldLineLevel; 1257 } 1258 1259 void UnwrappedLineParser::parseCaseLabel() { 1260 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); 1261 // FIXME: fix handling of complex expressions here. 1262 do { 1263 nextToken(); 1264 } while (!eof() && !FormatTok->Tok.is(tok::colon)); 1265 parseLabel(); 1266 } 1267 1268 void UnwrappedLineParser::parseSwitch() { 1269 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); 1270 nextToken(); 1271 if (FormatTok->Tok.is(tok::l_paren)) 1272 parseParens(); 1273 if (FormatTok->Tok.is(tok::l_brace)) { 1274 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1275 parseBlock(/*MustBeDeclaration=*/false); 1276 addUnwrappedLine(); 1277 } else { 1278 addUnwrappedLine(); 1279 ++Line->Level; 1280 parseStructuralElement(); 1281 --Line->Level; 1282 } 1283 } 1284 1285 void UnwrappedLineParser::parseAccessSpecifier() { 1286 nextToken(); 1287 // Understand Qt's slots. 1288 if (FormatTok->is(tok::identifier) && 1289 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS")) 1290 nextToken(); 1291 // Otherwise, we don't know what it is, and we'd better keep the next token. 1292 if (FormatTok->Tok.is(tok::colon)) 1293 nextToken(); 1294 addUnwrappedLine(); 1295 } 1296 1297 void UnwrappedLineParser::parseEnum() { 1298 if (FormatTok->Tok.is(tok::kw_enum)) { 1299 // Won't be 'enum' for NS_ENUMs. 1300 nextToken(); 1301 } 1302 // Eat up enum class ... 1303 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) 1304 nextToken(); 1305 while (FormatTok->Tok.getIdentifierInfo() || 1306 FormatTok->isOneOf(tok::colon, tok::coloncolon)) { 1307 nextToken(); 1308 // We can have macros or attributes in between 'enum' and the enum name. 1309 if (FormatTok->Tok.is(tok::l_paren)) { 1310 parseParens(); 1311 } 1312 if (FormatTok->Tok.is(tok::identifier)) 1313 nextToken(); 1314 } 1315 if (FormatTok->Tok.is(tok::l_brace)) { 1316 FormatTok->BlockKind = BK_Block; 1317 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); 1318 if (HasError) { 1319 if (FormatTok->is(tok::semi)) 1320 nextToken(); 1321 addUnwrappedLine(); 1322 } 1323 } 1324 // We fall through to parsing a structural element afterwards, so that in 1325 // enum A {} n, m; 1326 // "} n, m;" will end up in one unwrapped line. 1327 } 1328 1329 void UnwrappedLineParser::parseRecord() { 1330 nextToken(); 1331 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute, 1332 tok::kw___declspec, tok::kw_alignas)) { 1333 nextToken(); 1334 // We can have macros or attributes in between 'class' and the class name. 1335 if (FormatTok->Tok.is(tok::l_paren)) { 1336 parseParens(); 1337 } 1338 // The actual identifier can be a nested name specifier, and in macros 1339 // it is often token-pasted. 1340 while (FormatTok->Tok.is(tok::identifier) || 1341 FormatTok->Tok.is(tok::coloncolon) || 1342 FormatTok->Tok.is(tok::hashhash)) 1343 nextToken(); 1344 1345 // Note that parsing away template declarations here leads to incorrectly 1346 // accepting function declarations as record declarations. 1347 // In general, we cannot solve this problem. Consider: 1348 // class A<int> B() {} 1349 // which can be a function definition or a class definition when B() is a 1350 // macro. If we find enough real-world cases where this is a problem, we 1351 // can parse for the 'template' keyword in the beginning of the statement, 1352 // and thus rule out the record production in case there is no template 1353 // (this would still leave us with an ambiguity between template function 1354 // and class declarations). 1355 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) { 1356 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) { 1357 if (FormatTok->Tok.is(tok::semi)) 1358 return; 1359 nextToken(); 1360 } 1361 } 1362 } 1363 if (FormatTok->Tok.is(tok::l_brace)) { 1364 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || 1365 Style.BreakBeforeBraces == FormatStyle::BS_Allman || 1366 Style.BreakBeforeBraces == FormatStyle::BS_GNU) 1367 addUnwrappedLine(); 1368 1369 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, 1370 /*MunchSemi=*/false); 1371 } 1372 // We fall through to parsing a structural element afterwards, so 1373 // class A {} n, m; 1374 // will end up in one unwrapped line. 1375 } 1376 1377 void UnwrappedLineParser::parseObjCProtocolList() { 1378 assert(FormatTok->Tok.is(tok::less) && "'<' expected."); 1379 do 1380 nextToken(); 1381 while (!eof() && FormatTok->Tok.isNot(tok::greater)); 1382 nextToken(); // Skip '>'. 1383 } 1384 1385 void UnwrappedLineParser::parseObjCUntilAtEnd() { 1386 do { 1387 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { 1388 nextToken(); 1389 addUnwrappedLine(); 1390 break; 1391 } 1392 if (FormatTok->is(tok::l_brace)) { 1393 parseBlock(/*MustBeDeclaration=*/false); 1394 // In ObjC interfaces, nothing should be following the "}". 1395 addUnwrappedLine(); 1396 } else if (FormatTok->is(tok::r_brace)) { 1397 // Ignore stray "}". parseStructuralElement doesn't consume them. 1398 nextToken(); 1399 addUnwrappedLine(); 1400 } else { 1401 parseStructuralElement(); 1402 } 1403 } while (!eof()); 1404 } 1405 1406 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { 1407 nextToken(); 1408 nextToken(); // interface name 1409 1410 // @interface can be followed by either a base class, or a category. 1411 if (FormatTok->Tok.is(tok::colon)) { 1412 nextToken(); 1413 nextToken(); // base class name 1414 } else if (FormatTok->Tok.is(tok::l_paren)) 1415 // Skip category, if present. 1416 parseParens(); 1417 1418 if (FormatTok->Tok.is(tok::less)) 1419 parseObjCProtocolList(); 1420 1421 if (FormatTok->Tok.is(tok::l_brace)) { 1422 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || 1423 Style.BreakBeforeBraces == FormatStyle::BS_GNU) 1424 addUnwrappedLine(); 1425 parseBlock(/*MustBeDeclaration=*/true); 1426 } 1427 1428 // With instance variables, this puts '}' on its own line. Without instance 1429 // variables, this ends the @interface line. 1430 addUnwrappedLine(); 1431 1432 parseObjCUntilAtEnd(); 1433 } 1434 1435 void UnwrappedLineParser::parseObjCProtocol() { 1436 nextToken(); 1437 nextToken(); // protocol name 1438 1439 if (FormatTok->Tok.is(tok::less)) 1440 parseObjCProtocolList(); 1441 1442 // Check for protocol declaration. 1443 if (FormatTok->Tok.is(tok::semi)) { 1444 nextToken(); 1445 return addUnwrappedLine(); 1446 } 1447 1448 addUnwrappedLine(); 1449 parseObjCUntilAtEnd(); 1450 } 1451 1452 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, 1453 StringRef Prefix = "") { 1454 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")" 1455 << (Line.InPPDirective ? " MACRO" : "") << ": "; 1456 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 1457 E = Line.Tokens.end(); 1458 I != E; ++I) { 1459 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] "; 1460 } 1461 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 1462 E = Line.Tokens.end(); 1463 I != E; ++I) { 1464 const UnwrappedLineNode &Node = *I; 1465 for (SmallVectorImpl<UnwrappedLine>::const_iterator 1466 I = Node.Children.begin(), 1467 E = Node.Children.end(); 1468 I != E; ++I) { 1469 printDebugInfo(*I, "\nChild: "); 1470 } 1471 } 1472 llvm::dbgs() << "\n"; 1473 } 1474 1475 void UnwrappedLineParser::addUnwrappedLine() { 1476 if (Line->Tokens.empty()) 1477 return; 1478 DEBUG({ 1479 if (CurrentLines == &Lines) 1480 printDebugInfo(*Line); 1481 }); 1482 CurrentLines->push_back(*Line); 1483 Line->Tokens.clear(); 1484 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { 1485 for (SmallVectorImpl<UnwrappedLine>::iterator 1486 I = PreprocessorDirectives.begin(), 1487 E = PreprocessorDirectives.end(); 1488 I != E; ++I) { 1489 CurrentLines->push_back(*I); 1490 } 1491 PreprocessorDirectives.clear(); 1492 } 1493 } 1494 1495 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } 1496 1497 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { 1498 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && 1499 FormatTok.NewlinesBefore > 0; 1500 } 1501 1502 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { 1503 bool JustComments = Line->Tokens.empty(); 1504 for (SmallVectorImpl<FormatToken *>::const_iterator 1505 I = CommentsBeforeNextToken.begin(), 1506 E = CommentsBeforeNextToken.end(); 1507 I != E; ++I) { 1508 if (isOnNewLine(**I) && JustComments) { 1509 addUnwrappedLine(); 1510 } 1511 pushToken(*I); 1512 } 1513 if (NewlineBeforeNext && JustComments) { 1514 addUnwrappedLine(); 1515 } 1516 CommentsBeforeNextToken.clear(); 1517 } 1518 1519 void UnwrappedLineParser::nextToken() { 1520 if (eof()) 1521 return; 1522 flushComments(isOnNewLine(*FormatTok)); 1523 pushToken(FormatTok); 1524 readToken(); 1525 } 1526 1527 void UnwrappedLineParser::readToken() { 1528 bool CommentsInCurrentLine = true; 1529 do { 1530 FormatTok = Tokens->getNextToken(); 1531 assert(FormatTok); 1532 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && 1533 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { 1534 // If there is an unfinished unwrapped line, we flush the preprocessor 1535 // directives only after that unwrapped line was finished later. 1536 bool SwitchToPreprocessorLines = 1537 !Line->Tokens.empty() && CurrentLines == &Lines; 1538 ScopedLineState BlockState(*this, SwitchToPreprocessorLines); 1539 // Comments stored before the preprocessor directive need to be output 1540 // before the preprocessor directive, at the same level as the 1541 // preprocessor directive, as we consider them to apply to the directive. 1542 flushComments(isOnNewLine(*FormatTok)); 1543 parsePPDirective(); 1544 } 1545 while (FormatTok->Type == TT_ConflictStart || 1546 FormatTok->Type == TT_ConflictEnd || 1547 FormatTok->Type == TT_ConflictAlternative) { 1548 if (FormatTok->Type == TT_ConflictStart) { 1549 conditionalCompilationStart(/*Unreachable=*/false); 1550 } else if (FormatTok->Type == TT_ConflictAlternative) { 1551 conditionalCompilationAlternative(); 1552 } else if (FormatTok->Type == TT_ConflictEnd) { 1553 conditionalCompilationEnd(); 1554 } 1555 FormatTok = Tokens->getNextToken(); 1556 FormatTok->MustBreakBefore = true; 1557 } 1558 1559 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) && 1560 !Line->InPPDirective) { 1561 continue; 1562 } 1563 1564 if (!FormatTok->Tok.is(tok::comment)) 1565 return; 1566 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) { 1567 CommentsInCurrentLine = false; 1568 } 1569 if (CommentsInCurrentLine) { 1570 pushToken(FormatTok); 1571 } else { 1572 CommentsBeforeNextToken.push_back(FormatTok); 1573 } 1574 } while (!eof()); 1575 } 1576 1577 void UnwrappedLineParser::pushToken(FormatToken *Tok) { 1578 Line->Tokens.push_back(UnwrappedLineNode(Tok)); 1579 if (MustBreakBeforeNextToken) { 1580 Line->Tokens.back().Tok->MustBreakBefore = true; 1581 MustBreakBeforeNextToken = false; 1582 } 1583 } 1584 1585 } // end namespace format 1586 } // end namespace clang 1587