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