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