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