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