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