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 (Next->is(tok::exclaim) && PreviousMustBeValue) 719 addUnwrappedLine(); 720 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next); 721 if (NextMustBeValue && (PreviousMustBeValue || 722 Previous->isOneOf(tok::r_square, tok::r_paren, 723 tok::plusplus, tok::minusminus))) 724 addUnwrappedLine(); 725 if (PreviousMustBeValue && isJSDeclOrStmt(Keywords, Next)) 726 addUnwrappedLine(); 727 } 728 729 void UnwrappedLineParser::parseStructuralElement() { 730 assert(!FormatTok->is(tok::l_brace)); 731 if (Style.Language == FormatStyle::LK_TableGen && 732 FormatTok->is(tok::pp_include)) { 733 nextToken(); 734 if (FormatTok->is(tok::string_literal)) 735 nextToken(); 736 addUnwrappedLine(); 737 return; 738 } 739 switch (FormatTok->Tok.getKind()) { 740 case tok::at: 741 nextToken(); 742 if (FormatTok->Tok.is(tok::l_brace)) { 743 parseBracedList(); 744 break; 745 } 746 switch (FormatTok->Tok.getObjCKeywordID()) { 747 case tok::objc_public: 748 case tok::objc_protected: 749 case tok::objc_package: 750 case tok::objc_private: 751 return parseAccessSpecifier(); 752 case tok::objc_interface: 753 case tok::objc_implementation: 754 return parseObjCInterfaceOrImplementation(); 755 case tok::objc_protocol: 756 return parseObjCProtocol(); 757 case tok::objc_end: 758 return; // Handled by the caller. 759 case tok::objc_optional: 760 case tok::objc_required: 761 nextToken(); 762 addUnwrappedLine(); 763 return; 764 case tok::objc_autoreleasepool: 765 nextToken(); 766 if (FormatTok->Tok.is(tok::l_brace)) { 767 if (Style.BraceWrapping.AfterObjCDeclaration) 768 addUnwrappedLine(); 769 parseBlock(/*MustBeDeclaration=*/false); 770 } 771 addUnwrappedLine(); 772 return; 773 case tok::objc_try: 774 // This branch isn't strictly necessary (the kw_try case below would 775 // do this too after the tok::at is parsed above). But be explicit. 776 parseTryCatch(); 777 return; 778 default: 779 break; 780 } 781 break; 782 case tok::kw_asm: 783 nextToken(); 784 if (FormatTok->is(tok::l_brace)) { 785 FormatTok->Type = TT_InlineASMBrace; 786 nextToken(); 787 while (FormatTok && FormatTok->isNot(tok::eof)) { 788 if (FormatTok->is(tok::r_brace)) { 789 FormatTok->Type = TT_InlineASMBrace; 790 nextToken(); 791 addUnwrappedLine(); 792 break; 793 } 794 FormatTok->Finalized = true; 795 nextToken(); 796 } 797 } 798 break; 799 case tok::kw_namespace: 800 parseNamespace(); 801 return; 802 case tok::kw_inline: 803 nextToken(); 804 if (FormatTok->Tok.is(tok::kw_namespace)) { 805 parseNamespace(); 806 return; 807 } 808 break; 809 case tok::kw_public: 810 case tok::kw_protected: 811 case tok::kw_private: 812 if (Style.Language == FormatStyle::LK_Java || 813 Style.Language == FormatStyle::LK_JavaScript) 814 nextToken(); 815 else 816 parseAccessSpecifier(); 817 return; 818 case tok::kw_if: 819 parseIfThenElse(); 820 return; 821 case tok::kw_for: 822 case tok::kw_while: 823 parseForOrWhileLoop(); 824 return; 825 case tok::kw_do: 826 parseDoWhile(); 827 return; 828 case tok::kw_switch: 829 parseSwitch(); 830 return; 831 case tok::kw_default: 832 nextToken(); 833 parseLabel(); 834 return; 835 case tok::kw_case: 836 parseCaseLabel(); 837 return; 838 case tok::kw_try: 839 case tok::kw___try: 840 parseTryCatch(); 841 return; 842 case tok::kw_extern: 843 nextToken(); 844 if (FormatTok->Tok.is(tok::string_literal)) { 845 nextToken(); 846 if (FormatTok->Tok.is(tok::l_brace)) { 847 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); 848 addUnwrappedLine(); 849 return; 850 } 851 } 852 break; 853 case tok::kw_export: 854 if (Style.Language == FormatStyle::LK_JavaScript) { 855 parseJavaScriptEs6ImportExport(); 856 return; 857 } 858 break; 859 case tok::identifier: 860 if (FormatTok->is(TT_ForEachMacro)) { 861 parseForOrWhileLoop(); 862 return; 863 } 864 if (FormatTok->is(TT_MacroBlockBegin)) { 865 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true, 866 /*MunchSemi=*/false); 867 return; 868 } 869 if (Style.Language == FormatStyle::LK_JavaScript && 870 FormatTok->is(Keywords.kw_import)) { 871 parseJavaScriptEs6ImportExport(); 872 return; 873 } 874 if (FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals, 875 Keywords.kw_slots, Keywords.kw_qslots)) { 876 nextToken(); 877 if (FormatTok->is(tok::colon)) { 878 nextToken(); 879 addUnwrappedLine(); 880 } 881 return; 882 } 883 // In all other cases, parse the declaration. 884 break; 885 default: 886 break; 887 } 888 do { 889 switch (FormatTok->Tok.getKind()) { 890 case tok::at: 891 nextToken(); 892 if (FormatTok->Tok.is(tok::l_brace)) 893 parseBracedList(); 894 break; 895 case tok::kw_enum: 896 // parseEnum falls through and does not yet add an unwrapped line as an 897 // enum definition can start a structural element. 898 if (!parseEnum()) 899 break; 900 // This only applies for C++. 901 if (Style.Language != FormatStyle::LK_Cpp) { 902 addUnwrappedLine(); 903 return; 904 } 905 break; 906 case tok::kw_typedef: 907 nextToken(); 908 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS, 909 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS)) 910 parseEnum(); 911 break; 912 case tok::kw_struct: 913 case tok::kw_union: 914 case tok::kw_class: 915 // parseRecord falls through and does not yet add an unwrapped line as a 916 // record declaration or definition can start a structural element. 917 parseRecord(); 918 // This does not apply for Java and JavaScript. 919 if (Style.Language == FormatStyle::LK_Java || 920 Style.Language == FormatStyle::LK_JavaScript) { 921 if (FormatTok->is(tok::semi)) 922 nextToken(); 923 addUnwrappedLine(); 924 return; 925 } 926 break; 927 case tok::period: 928 nextToken(); 929 // In Java, classes have an implicit static member "class". 930 if (Style.Language == FormatStyle::LK_Java && FormatTok && 931 FormatTok->is(tok::kw_class)) 932 nextToken(); 933 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok && 934 FormatTok->Tok.getIdentifierInfo()) 935 // JavaScript only has pseudo keywords, all keywords are allowed to 936 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6 937 nextToken(); 938 break; 939 case tok::semi: 940 nextToken(); 941 addUnwrappedLine(); 942 return; 943 case tok::r_brace: 944 addUnwrappedLine(); 945 return; 946 case tok::l_paren: 947 parseParens(); 948 break; 949 case tok::kw_operator: 950 nextToken(); 951 if (FormatTok->isBinaryOperator()) 952 nextToken(); 953 break; 954 case tok::caret: 955 nextToken(); 956 if (FormatTok->Tok.isAnyIdentifier() || 957 FormatTok->isSimpleTypeSpecifier()) 958 nextToken(); 959 if (FormatTok->is(tok::l_paren)) 960 parseParens(); 961 if (FormatTok->is(tok::l_brace)) 962 parseChildBlock(); 963 break; 964 case tok::l_brace: 965 if (!tryToParseBracedList()) { 966 // A block outside of parentheses must be the last part of a 967 // structural element. 968 // FIXME: Figure out cases where this is not true, and add projections 969 // for them (the one we know is missing are lambdas). 970 if (Style.BraceWrapping.AfterFunction) 971 addUnwrappedLine(); 972 FormatTok->Type = TT_FunctionLBrace; 973 parseBlock(/*MustBeDeclaration=*/false); 974 addUnwrappedLine(); 975 return; 976 } 977 // Otherwise this was a braced init list, and the structural 978 // element continues. 979 break; 980 case tok::kw_try: 981 // We arrive here when parsing function-try blocks. 982 parseTryCatch(); 983 return; 984 case tok::identifier: { 985 if (FormatTok->is(TT_MacroBlockEnd)) { 986 addUnwrappedLine(); 987 return; 988 } 989 990 // Parse function literal unless 'function' is the first token in a line 991 // in which case this should be treated as a free-standing function. 992 if (Style.Language == FormatStyle::LK_JavaScript && 993 FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) { 994 tryToParseJSFunction(); 995 break; 996 } 997 if ((Style.Language == FormatStyle::LK_JavaScript || 998 Style.Language == FormatStyle::LK_Java) && 999 FormatTok->is(Keywords.kw_interface)) { 1000 parseRecord(); 1001 addUnwrappedLine(); 1002 return; 1003 } 1004 1005 // See if the following token should start a new unwrapped line. 1006 StringRef Text = FormatTok->TokenText; 1007 nextToken(); 1008 if (Line->Tokens.size() == 1 && 1009 // JS doesn't have macros, and within classes colons indicate fields, 1010 // not labels. 1011 Style.Language != FormatStyle::LK_JavaScript) { 1012 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) { 1013 Line->Tokens.begin()->Tok->MustBreakBefore = true; 1014 parseLabel(); 1015 return; 1016 } 1017 // Recognize function-like macro usages without trailing semicolon as 1018 // well as free-standing macros like Q_OBJECT. 1019 bool FunctionLike = FormatTok->is(tok::l_paren); 1020 if (FunctionLike) 1021 parseParens(); 1022 1023 bool FollowedByNewline = 1024 CommentsBeforeNextToken.empty() 1025 ? FormatTok->NewlinesBefore > 0 1026 : CommentsBeforeNextToken.front()->NewlinesBefore > 0; 1027 1028 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && 1029 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { 1030 addUnwrappedLine(); 1031 return; 1032 } 1033 } 1034 break; 1035 } 1036 case tok::equal: 1037 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType 1038 // TT_JsFatArrow. The always start an expression or a child block if 1039 // followed by a curly. 1040 if (FormatTok->is(TT_JsFatArrow)) { 1041 nextToken(); 1042 if (FormatTok->is(tok::l_brace)) 1043 parseChildBlock(); 1044 break; 1045 } 1046 1047 nextToken(); 1048 if (FormatTok->Tok.is(tok::l_brace)) { 1049 parseBracedList(); 1050 } 1051 break; 1052 case tok::l_square: 1053 parseSquare(); 1054 break; 1055 case tok::kw_new: 1056 parseNew(); 1057 break; 1058 default: 1059 nextToken(); 1060 break; 1061 } 1062 } while (!eof()); 1063 } 1064 1065 bool UnwrappedLineParser::tryToParseLambda() { 1066 if (Style.Language != FormatStyle::LK_Cpp) { 1067 nextToken(); 1068 return false; 1069 } 1070 const FormatToken* Previous = getPreviousToken(); 1071 if (Previous && 1072 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new, 1073 tok::kw_delete) || 1074 Previous->closesScope() || Previous->isSimpleTypeSpecifier())) { 1075 nextToken(); 1076 return false; 1077 } 1078 assert(FormatTok->is(tok::l_square)); 1079 FormatToken &LSquare = *FormatTok; 1080 if (!tryToParseLambdaIntroducer()) 1081 return false; 1082 1083 while (FormatTok->isNot(tok::l_brace)) { 1084 if (FormatTok->isSimpleTypeSpecifier()) { 1085 nextToken(); 1086 continue; 1087 } 1088 switch (FormatTok->Tok.getKind()) { 1089 case tok::l_brace: 1090 break; 1091 case tok::l_paren: 1092 parseParens(); 1093 break; 1094 case tok::amp: 1095 case tok::star: 1096 case tok::kw_const: 1097 case tok::comma: 1098 case tok::less: 1099 case tok::greater: 1100 case tok::identifier: 1101 case tok::numeric_constant: 1102 case tok::coloncolon: 1103 case tok::kw_mutable: 1104 nextToken(); 1105 break; 1106 case tok::arrow: 1107 FormatTok->Type = TT_LambdaArrow; 1108 nextToken(); 1109 break; 1110 default: 1111 return true; 1112 } 1113 } 1114 LSquare.Type = TT_LambdaLSquare; 1115 parseChildBlock(); 1116 return true; 1117 } 1118 1119 bool UnwrappedLineParser::tryToParseLambdaIntroducer() { 1120 nextToken(); 1121 if (FormatTok->is(tok::equal)) { 1122 nextToken(); 1123 if (FormatTok->is(tok::r_square)) { 1124 nextToken(); 1125 return true; 1126 } 1127 if (FormatTok->isNot(tok::comma)) 1128 return false; 1129 nextToken(); 1130 } else if (FormatTok->is(tok::amp)) { 1131 nextToken(); 1132 if (FormatTok->is(tok::r_square)) { 1133 nextToken(); 1134 return true; 1135 } 1136 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) { 1137 return false; 1138 } 1139 if (FormatTok->is(tok::comma)) 1140 nextToken(); 1141 } else if (FormatTok->is(tok::r_square)) { 1142 nextToken(); 1143 return true; 1144 } 1145 do { 1146 if (FormatTok->is(tok::amp)) 1147 nextToken(); 1148 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this)) 1149 return false; 1150 nextToken(); 1151 if (FormatTok->is(tok::ellipsis)) 1152 nextToken(); 1153 if (FormatTok->is(tok::comma)) { 1154 nextToken(); 1155 } else if (FormatTok->is(tok::r_square)) { 1156 nextToken(); 1157 return true; 1158 } else { 1159 return false; 1160 } 1161 } while (!eof()); 1162 return false; 1163 } 1164 1165 void UnwrappedLineParser::tryToParseJSFunction() { 1166 nextToken(); 1167 1168 // Consume function name. 1169 if (FormatTok->is(tok::identifier)) 1170 nextToken(); 1171 1172 if (FormatTok->isNot(tok::l_paren)) 1173 return; 1174 1175 // Parse formal parameter list. 1176 parseParens(); 1177 1178 if (FormatTok->is(tok::colon)) { 1179 // Parse a type definition. 1180 nextToken(); 1181 1182 // Eat the type declaration. For braced inline object types, balance braces, 1183 // otherwise just parse until finding an l_brace for the function body. 1184 if (FormatTok->is(tok::l_brace)) 1185 tryToParseBracedList(); 1186 else 1187 while (FormatTok->isNot(tok::l_brace) && !eof()) 1188 nextToken(); 1189 } 1190 1191 parseChildBlock(); 1192 } 1193 1194 bool UnwrappedLineParser::tryToParseBracedList() { 1195 if (FormatTok->BlockKind == BK_Unknown) 1196 calculateBraceTypes(); 1197 assert(FormatTok->BlockKind != BK_Unknown); 1198 if (FormatTok->BlockKind == BK_Block) 1199 return false; 1200 parseBracedList(); 1201 return true; 1202 } 1203 1204 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) { 1205 bool HasError = false; 1206 nextToken(); 1207 1208 // FIXME: Once we have an expression parser in the UnwrappedLineParser, 1209 // replace this by using parseAssigmentExpression() inside. 1210 do { 1211 if (Style.Language == FormatStyle::LK_JavaScript) { 1212 if (FormatTok->is(Keywords.kw_function)) { 1213 tryToParseJSFunction(); 1214 continue; 1215 } 1216 if (FormatTok->is(TT_JsFatArrow)) { 1217 nextToken(); 1218 // Fat arrows can be followed by simple expressions or by child blocks 1219 // in curly braces. 1220 if (FormatTok->is(tok::l_brace)) { 1221 parseChildBlock(); 1222 continue; 1223 } 1224 } 1225 } 1226 switch (FormatTok->Tok.getKind()) { 1227 case tok::caret: 1228 nextToken(); 1229 if (FormatTok->is(tok::l_brace)) { 1230 parseChildBlock(); 1231 } 1232 break; 1233 case tok::l_square: 1234 tryToParseLambda(); 1235 break; 1236 case tok::l_brace: 1237 // Assume there are no blocks inside a braced init list apart 1238 // from the ones we explicitly parse out (like lambdas). 1239 FormatTok->BlockKind = BK_BracedInit; 1240 parseBracedList(); 1241 break; 1242 case tok::l_paren: 1243 parseParens(); 1244 // JavaScript can just have free standing methods and getters/setters in 1245 // object literals. Detect them by a "{" following ")". 1246 if (Style.Language == FormatStyle::LK_JavaScript) { 1247 if (FormatTok->is(tok::l_brace)) 1248 parseChildBlock(); 1249 break; 1250 } 1251 break; 1252 case tok::r_brace: 1253 nextToken(); 1254 return !HasError; 1255 case tok::semi: 1256 // JavaScript (or more precisely TypeScript) can have semicolons in braced 1257 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be 1258 // used for error recovery if we have otherwise determined that this is 1259 // a braced list. 1260 if (Style.Language == FormatStyle::LK_JavaScript) { 1261 nextToken(); 1262 break; 1263 } 1264 HasError = true; 1265 if (!ContinueOnSemicolons) 1266 return !HasError; 1267 nextToken(); 1268 break; 1269 case tok::comma: 1270 nextToken(); 1271 break; 1272 default: 1273 nextToken(); 1274 break; 1275 } 1276 } while (!eof()); 1277 return false; 1278 } 1279 1280 void UnwrappedLineParser::parseParens() { 1281 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); 1282 nextToken(); 1283 do { 1284 switch (FormatTok->Tok.getKind()) { 1285 case tok::l_paren: 1286 parseParens(); 1287 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace)) 1288 parseChildBlock(); 1289 break; 1290 case tok::r_paren: 1291 nextToken(); 1292 return; 1293 case tok::r_brace: 1294 // A "}" inside parenthesis is an error if there wasn't a matching "{". 1295 return; 1296 case tok::l_square: 1297 tryToParseLambda(); 1298 break; 1299 case tok::l_brace: 1300 if (!tryToParseBracedList()) 1301 parseChildBlock(); 1302 break; 1303 case tok::at: 1304 nextToken(); 1305 if (FormatTok->Tok.is(tok::l_brace)) 1306 parseBracedList(); 1307 break; 1308 case tok::identifier: 1309 if (Style.Language == FormatStyle::LK_JavaScript && 1310 FormatTok->is(Keywords.kw_function)) 1311 tryToParseJSFunction(); 1312 else 1313 nextToken(); 1314 break; 1315 default: 1316 nextToken(); 1317 break; 1318 } 1319 } while (!eof()); 1320 } 1321 1322 void UnwrappedLineParser::parseSquare() { 1323 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); 1324 if (tryToParseLambda()) 1325 return; 1326 do { 1327 switch (FormatTok->Tok.getKind()) { 1328 case tok::l_paren: 1329 parseParens(); 1330 break; 1331 case tok::r_square: 1332 nextToken(); 1333 return; 1334 case tok::r_brace: 1335 // A "}" inside parenthesis is an error if there wasn't a matching "{". 1336 return; 1337 case tok::l_square: 1338 parseSquare(); 1339 break; 1340 case tok::l_brace: { 1341 if (!tryToParseBracedList()) 1342 parseChildBlock(); 1343 break; 1344 } 1345 case tok::at: 1346 nextToken(); 1347 if (FormatTok->Tok.is(tok::l_brace)) 1348 parseBracedList(); 1349 break; 1350 default: 1351 nextToken(); 1352 break; 1353 } 1354 } while (!eof()); 1355 } 1356 1357 void UnwrappedLineParser::parseIfThenElse() { 1358 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); 1359 nextToken(); 1360 if (FormatTok->Tok.is(tok::l_paren)) 1361 parseParens(); 1362 bool NeedsUnwrappedLine = false; 1363 if (FormatTok->Tok.is(tok::l_brace)) { 1364 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1365 parseBlock(/*MustBeDeclaration=*/false); 1366 if (Style.BraceWrapping.BeforeElse) 1367 addUnwrappedLine(); 1368 else 1369 NeedsUnwrappedLine = true; 1370 } else { 1371 addUnwrappedLine(); 1372 ++Line->Level; 1373 parseStructuralElement(); 1374 --Line->Level; 1375 } 1376 if (FormatTok->Tok.is(tok::kw_else)) { 1377 nextToken(); 1378 if (FormatTok->Tok.is(tok::l_brace)) { 1379 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1380 parseBlock(/*MustBeDeclaration=*/false); 1381 addUnwrappedLine(); 1382 } else if (FormatTok->Tok.is(tok::kw_if)) { 1383 parseIfThenElse(); 1384 } else { 1385 addUnwrappedLine(); 1386 ++Line->Level; 1387 parseStructuralElement(); 1388 --Line->Level; 1389 } 1390 } else if (NeedsUnwrappedLine) { 1391 addUnwrappedLine(); 1392 } 1393 } 1394 1395 void UnwrappedLineParser::parseTryCatch() { 1396 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected"); 1397 nextToken(); 1398 bool NeedsUnwrappedLine = false; 1399 if (FormatTok->is(tok::colon)) { 1400 // We are in a function try block, what comes is an initializer list. 1401 nextToken(); 1402 while (FormatTok->is(tok::identifier)) { 1403 nextToken(); 1404 if (FormatTok->is(tok::l_paren)) 1405 parseParens(); 1406 if (FormatTok->is(tok::comma)) 1407 nextToken(); 1408 } 1409 } 1410 // Parse try with resource. 1411 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) { 1412 parseParens(); 1413 } 1414 if (FormatTok->is(tok::l_brace)) { 1415 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1416 parseBlock(/*MustBeDeclaration=*/false); 1417 if (Style.BraceWrapping.BeforeCatch) { 1418 addUnwrappedLine(); 1419 } else { 1420 NeedsUnwrappedLine = true; 1421 } 1422 } else if (!FormatTok->is(tok::kw_catch)) { 1423 // The C++ standard requires a compound-statement after a try. 1424 // If there's none, we try to assume there's a structuralElement 1425 // and try to continue. 1426 addUnwrappedLine(); 1427 ++Line->Level; 1428 parseStructuralElement(); 1429 --Line->Level; 1430 } 1431 while (1) { 1432 if (FormatTok->is(tok::at)) 1433 nextToken(); 1434 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, 1435 tok::kw___finally) || 1436 ((Style.Language == FormatStyle::LK_Java || 1437 Style.Language == FormatStyle::LK_JavaScript) && 1438 FormatTok->is(Keywords.kw_finally)) || 1439 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || 1440 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) 1441 break; 1442 nextToken(); 1443 while (FormatTok->isNot(tok::l_brace)) { 1444 if (FormatTok->is(tok::l_paren)) { 1445 parseParens(); 1446 continue; 1447 } 1448 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) 1449 return; 1450 nextToken(); 1451 } 1452 NeedsUnwrappedLine = false; 1453 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1454 parseBlock(/*MustBeDeclaration=*/false); 1455 if (Style.BraceWrapping.BeforeCatch) 1456 addUnwrappedLine(); 1457 else 1458 NeedsUnwrappedLine = true; 1459 } 1460 if (NeedsUnwrappedLine) 1461 addUnwrappedLine(); 1462 } 1463 1464 void UnwrappedLineParser::parseNamespace() { 1465 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); 1466 1467 const FormatToken &InitialToken = *FormatTok; 1468 nextToken(); 1469 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon)) 1470 nextToken(); 1471 if (FormatTok->Tok.is(tok::l_brace)) { 1472 if (ShouldBreakBeforeBrace(Style, InitialToken)) 1473 addUnwrappedLine(); 1474 1475 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || 1476 (Style.NamespaceIndentation == FormatStyle::NI_Inner && 1477 DeclarationScopeStack.size() > 1); 1478 parseBlock(/*MustBeDeclaration=*/true, AddLevel); 1479 // Munch the semicolon after a namespace. This is more common than one would 1480 // think. Puttin the semicolon into its own line is very ugly. 1481 if (FormatTok->Tok.is(tok::semi)) 1482 nextToken(); 1483 addUnwrappedLine(); 1484 } 1485 // FIXME: Add error handling. 1486 } 1487 1488 void UnwrappedLineParser::parseNew() { 1489 assert(FormatTok->is(tok::kw_new) && "'new' expected"); 1490 nextToken(); 1491 if (Style.Language != FormatStyle::LK_Java) 1492 return; 1493 1494 // In Java, we can parse everything up to the parens, which aren't optional. 1495 do { 1496 // There should not be a ;, { or } before the new's open paren. 1497 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace)) 1498 return; 1499 1500 // Consume the parens. 1501 if (FormatTok->is(tok::l_paren)) { 1502 parseParens(); 1503 1504 // If there is a class body of an anonymous class, consume that as child. 1505 if (FormatTok->is(tok::l_brace)) 1506 parseChildBlock(); 1507 return; 1508 } 1509 nextToken(); 1510 } while (!eof()); 1511 } 1512 1513 void UnwrappedLineParser::parseForOrWhileLoop() { 1514 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && 1515 "'for', 'while' or foreach macro expected"); 1516 nextToken(); 1517 if (FormatTok->Tok.is(tok::l_paren)) 1518 parseParens(); 1519 if (FormatTok->Tok.is(tok::l_brace)) { 1520 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1521 parseBlock(/*MustBeDeclaration=*/false); 1522 addUnwrappedLine(); 1523 } else { 1524 addUnwrappedLine(); 1525 ++Line->Level; 1526 parseStructuralElement(); 1527 --Line->Level; 1528 } 1529 } 1530 1531 void UnwrappedLineParser::parseDoWhile() { 1532 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); 1533 nextToken(); 1534 if (FormatTok->Tok.is(tok::l_brace)) { 1535 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1536 parseBlock(/*MustBeDeclaration=*/false); 1537 if (Style.BraceWrapping.IndentBraces) 1538 addUnwrappedLine(); 1539 } else { 1540 addUnwrappedLine(); 1541 ++Line->Level; 1542 parseStructuralElement(); 1543 --Line->Level; 1544 } 1545 1546 // FIXME: Add error handling. 1547 if (!FormatTok->Tok.is(tok::kw_while)) { 1548 addUnwrappedLine(); 1549 return; 1550 } 1551 1552 nextToken(); 1553 parseStructuralElement(); 1554 } 1555 1556 void UnwrappedLineParser::parseLabel() { 1557 nextToken(); 1558 unsigned OldLineLevel = Line->Level; 1559 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) 1560 --Line->Level; 1561 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { 1562 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1563 parseBlock(/*MustBeDeclaration=*/false); 1564 if (FormatTok->Tok.is(tok::kw_break)) { 1565 if (Style.BraceWrapping.AfterControlStatement) 1566 addUnwrappedLine(); 1567 parseStructuralElement(); 1568 } 1569 addUnwrappedLine(); 1570 } else { 1571 if (FormatTok->is(tok::semi)) 1572 nextToken(); 1573 addUnwrappedLine(); 1574 } 1575 Line->Level = OldLineLevel; 1576 if (FormatTok->isNot(tok::l_brace)) { 1577 parseStructuralElement(); 1578 addUnwrappedLine(); 1579 } 1580 } 1581 1582 void UnwrappedLineParser::parseCaseLabel() { 1583 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); 1584 // FIXME: fix handling of complex expressions here. 1585 do { 1586 nextToken(); 1587 } while (!eof() && !FormatTok->Tok.is(tok::colon)); 1588 parseLabel(); 1589 } 1590 1591 void UnwrappedLineParser::parseSwitch() { 1592 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); 1593 nextToken(); 1594 if (FormatTok->Tok.is(tok::l_paren)) 1595 parseParens(); 1596 if (FormatTok->Tok.is(tok::l_brace)) { 1597 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1598 parseBlock(/*MustBeDeclaration=*/false); 1599 addUnwrappedLine(); 1600 } else { 1601 addUnwrappedLine(); 1602 ++Line->Level; 1603 parseStructuralElement(); 1604 --Line->Level; 1605 } 1606 } 1607 1608 void UnwrappedLineParser::parseAccessSpecifier() { 1609 nextToken(); 1610 // Understand Qt's slots. 1611 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots)) 1612 nextToken(); 1613 // Otherwise, we don't know what it is, and we'd better keep the next token. 1614 if (FormatTok->Tok.is(tok::colon)) 1615 nextToken(); 1616 addUnwrappedLine(); 1617 } 1618 1619 bool UnwrappedLineParser::parseEnum() { 1620 // Won't be 'enum' for NS_ENUMs. 1621 if (FormatTok->Tok.is(tok::kw_enum)) 1622 nextToken(); 1623 1624 // In TypeScript, "enum" can also be used as property name, e.g. in interface 1625 // declarations. An "enum" keyword followed by a colon would be a syntax 1626 // error and thus assume it is just an identifier. 1627 if (Style.Language == FormatStyle::LK_JavaScript && 1628 FormatTok->isOneOf(tok::colon, tok::question)) 1629 return false; 1630 1631 // Eat up enum class ... 1632 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) 1633 nextToken(); 1634 1635 while (FormatTok->Tok.getIdentifierInfo() || 1636 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, 1637 tok::greater, tok::comma, tok::question)) { 1638 nextToken(); 1639 // We can have macros or attributes in between 'enum' and the enum name. 1640 if (FormatTok->is(tok::l_paren)) 1641 parseParens(); 1642 if (FormatTok->is(tok::identifier)) { 1643 nextToken(); 1644 // If there are two identifiers in a row, this is likely an elaborate 1645 // return type. In Java, this can be "implements", etc. 1646 if (Style.Language == FormatStyle::LK_Cpp && 1647 FormatTok->is(tok::identifier)) 1648 return false; 1649 } 1650 } 1651 1652 // Just a declaration or something is wrong. 1653 if (FormatTok->isNot(tok::l_brace)) 1654 return true; 1655 FormatTok->BlockKind = BK_Block; 1656 1657 if (Style.Language == FormatStyle::LK_Java) { 1658 // Java enums are different. 1659 parseJavaEnumBody(); 1660 return true; 1661 } 1662 if (Style.Language == FormatStyle::LK_Proto) { 1663 parseBlock(/*MustBeDeclaration=*/true); 1664 return true; 1665 } 1666 1667 // Parse enum body. 1668 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); 1669 if (HasError) { 1670 if (FormatTok->is(tok::semi)) 1671 nextToken(); 1672 addUnwrappedLine(); 1673 } 1674 return true; 1675 1676 // There is no addUnwrappedLine() here so that we fall through to parsing a 1677 // structural element afterwards. Thus, in "enum A {} n, m;", 1678 // "} n, m;" will end up in one unwrapped line. 1679 } 1680 1681 void UnwrappedLineParser::parseJavaEnumBody() { 1682 // Determine whether the enum is simple, i.e. does not have a semicolon or 1683 // constants with class bodies. Simple enums can be formatted like braced 1684 // lists, contracted to a single line, etc. 1685 unsigned StoredPosition = Tokens->getPosition(); 1686 bool IsSimple = true; 1687 FormatToken *Tok = Tokens->getNextToken(); 1688 while (Tok) { 1689 if (Tok->is(tok::r_brace)) 1690 break; 1691 if (Tok->isOneOf(tok::l_brace, tok::semi)) { 1692 IsSimple = false; 1693 break; 1694 } 1695 // FIXME: This will also mark enums with braces in the arguments to enum 1696 // constants as "not simple". This is probably fine in practice, though. 1697 Tok = Tokens->getNextToken(); 1698 } 1699 FormatTok = Tokens->setPosition(StoredPosition); 1700 1701 if (IsSimple) { 1702 parseBracedList(); 1703 addUnwrappedLine(); 1704 return; 1705 } 1706 1707 // Parse the body of a more complex enum. 1708 // First add a line for everything up to the "{". 1709 nextToken(); 1710 addUnwrappedLine(); 1711 ++Line->Level; 1712 1713 // Parse the enum constants. 1714 while (FormatTok) { 1715 if (FormatTok->is(tok::l_brace)) { 1716 // Parse the constant's class body. 1717 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, 1718 /*MunchSemi=*/false); 1719 } else if (FormatTok->is(tok::l_paren)) { 1720 parseParens(); 1721 } else if (FormatTok->is(tok::comma)) { 1722 nextToken(); 1723 addUnwrappedLine(); 1724 } else if (FormatTok->is(tok::semi)) { 1725 nextToken(); 1726 addUnwrappedLine(); 1727 break; 1728 } else if (FormatTok->is(tok::r_brace)) { 1729 addUnwrappedLine(); 1730 break; 1731 } else { 1732 nextToken(); 1733 } 1734 } 1735 1736 // Parse the class body after the enum's ";" if any. 1737 parseLevel(/*HasOpeningBrace=*/true); 1738 nextToken(); 1739 --Line->Level; 1740 addUnwrappedLine(); 1741 } 1742 1743 void UnwrappedLineParser::parseRecord() { 1744 const FormatToken &InitialToken = *FormatTok; 1745 nextToken(); 1746 1747 // The actual identifier can be a nested name specifier, and in macros 1748 // it is often token-pasted. 1749 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, 1750 tok::kw___attribute, tok::kw___declspec, 1751 tok::kw_alignas) || 1752 ((Style.Language == FormatStyle::LK_Java || 1753 Style.Language == FormatStyle::LK_JavaScript) && 1754 FormatTok->isOneOf(tok::period, tok::comma))) { 1755 bool IsNonMacroIdentifier = 1756 FormatTok->is(tok::identifier) && 1757 FormatTok->TokenText != FormatTok->TokenText.upper(); 1758 nextToken(); 1759 // We can have macros or attributes in between 'class' and the class name. 1760 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren)) 1761 parseParens(); 1762 } 1763 1764 // Note that parsing away template declarations here leads to incorrectly 1765 // accepting function declarations as record declarations. 1766 // In general, we cannot solve this problem. Consider: 1767 // class A<int> B() {} 1768 // which can be a function definition or a class definition when B() is a 1769 // macro. If we find enough real-world cases where this is a problem, we 1770 // can parse for the 'template' keyword in the beginning of the statement, 1771 // and thus rule out the record production in case there is no template 1772 // (this would still leave us with an ambiguity between template function 1773 // and class declarations). 1774 if (FormatTok->isOneOf(tok::colon, tok::less)) { 1775 while (!eof()) { 1776 if (FormatTok->is(tok::l_brace)) { 1777 calculateBraceTypes(/*ExpectClassBody=*/true); 1778 if (!tryToParseBracedList()) 1779 break; 1780 } 1781 if (FormatTok->Tok.is(tok::semi)) 1782 return; 1783 nextToken(); 1784 } 1785 } 1786 if (FormatTok->Tok.is(tok::l_brace)) { 1787 if (ShouldBreakBeforeBrace(Style, InitialToken)) 1788 addUnwrappedLine(); 1789 1790 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true, 1791 /*MunchSemi=*/false); 1792 } 1793 // There is no addUnwrappedLine() here so that we fall through to parsing a 1794 // structural element afterwards. Thus, in "class A {} n, m;", 1795 // "} n, m;" will end up in one unwrapped line. 1796 } 1797 1798 void UnwrappedLineParser::parseObjCProtocolList() { 1799 assert(FormatTok->Tok.is(tok::less) && "'<' expected."); 1800 do 1801 nextToken(); 1802 while (!eof() && FormatTok->Tok.isNot(tok::greater)); 1803 nextToken(); // Skip '>'. 1804 } 1805 1806 void UnwrappedLineParser::parseObjCUntilAtEnd() { 1807 do { 1808 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { 1809 nextToken(); 1810 addUnwrappedLine(); 1811 break; 1812 } 1813 if (FormatTok->is(tok::l_brace)) { 1814 parseBlock(/*MustBeDeclaration=*/false); 1815 // In ObjC interfaces, nothing should be following the "}". 1816 addUnwrappedLine(); 1817 } else if (FormatTok->is(tok::r_brace)) { 1818 // Ignore stray "}". parseStructuralElement doesn't consume them. 1819 nextToken(); 1820 addUnwrappedLine(); 1821 } else { 1822 parseStructuralElement(); 1823 } 1824 } while (!eof()); 1825 } 1826 1827 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { 1828 nextToken(); 1829 nextToken(); // interface name 1830 1831 // @interface can be followed by either a base class, or a category. 1832 if (FormatTok->Tok.is(tok::colon)) { 1833 nextToken(); 1834 nextToken(); // base class name 1835 } else if (FormatTok->Tok.is(tok::l_paren)) 1836 // Skip category, if present. 1837 parseParens(); 1838 1839 if (FormatTok->Tok.is(tok::less)) 1840 parseObjCProtocolList(); 1841 1842 if (FormatTok->Tok.is(tok::l_brace)) { 1843 if (Style.BraceWrapping.AfterObjCDeclaration) 1844 addUnwrappedLine(); 1845 parseBlock(/*MustBeDeclaration=*/true); 1846 } 1847 1848 // With instance variables, this puts '}' on its own line. Without instance 1849 // variables, this ends the @interface line. 1850 addUnwrappedLine(); 1851 1852 parseObjCUntilAtEnd(); 1853 } 1854 1855 void UnwrappedLineParser::parseObjCProtocol() { 1856 nextToken(); 1857 nextToken(); // protocol name 1858 1859 if (FormatTok->Tok.is(tok::less)) 1860 parseObjCProtocolList(); 1861 1862 // Check for protocol declaration. 1863 if (FormatTok->Tok.is(tok::semi)) { 1864 nextToken(); 1865 return addUnwrappedLine(); 1866 } 1867 1868 addUnwrappedLine(); 1869 parseObjCUntilAtEnd(); 1870 } 1871 1872 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { 1873 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export)); 1874 nextToken(); 1875 1876 // Consume the "default" in "export default class/function". 1877 if (FormatTok->is(tok::kw_default)) 1878 nextToken(); 1879 1880 // Consume "function" and "default function", so that these get parsed as 1881 // free-standing JS functions, i.e. do not require a trailing semicolon. 1882 if (FormatTok->is(Keywords.kw_function)) { 1883 nextToken(); 1884 return; 1885 } 1886 1887 // Consume the "abstract" in "export abstract class". 1888 if (FormatTok->is(Keywords.kw_abstract)) 1889 nextToken(); 1890 1891 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, tok::kw_enum, 1892 Keywords.kw_interface, Keywords.kw_let, 1893 Keywords.kw_var)) 1894 return; // Fall through to parsing the corresponding structure. 1895 1896 while (!eof() && FormatTok->isNot(tok::semi)) { 1897 if (FormatTok->is(tok::l_brace)) { 1898 FormatTok->BlockKind = BK_Block; 1899 parseBracedList(); 1900 } else { 1901 nextToken(); 1902 } 1903 } 1904 } 1905 1906 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, 1907 StringRef Prefix = "") { 1908 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")" 1909 << (Line.InPPDirective ? " MACRO" : "") << ": "; 1910 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 1911 E = Line.Tokens.end(); 1912 I != E; ++I) { 1913 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] "; 1914 } 1915 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 1916 E = Line.Tokens.end(); 1917 I != E; ++I) { 1918 const UnwrappedLineNode &Node = *I; 1919 for (SmallVectorImpl<UnwrappedLine>::const_iterator 1920 I = Node.Children.begin(), 1921 E = Node.Children.end(); 1922 I != E; ++I) { 1923 printDebugInfo(*I, "\nChild: "); 1924 } 1925 } 1926 llvm::dbgs() << "\n"; 1927 } 1928 1929 void UnwrappedLineParser::addUnwrappedLine() { 1930 if (Line->Tokens.empty()) 1931 return; 1932 DEBUG({ 1933 if (CurrentLines == &Lines) 1934 printDebugInfo(*Line); 1935 }); 1936 CurrentLines->push_back(std::move(*Line)); 1937 Line->Tokens.clear(); 1938 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { 1939 CurrentLines->append( 1940 std::make_move_iterator(PreprocessorDirectives.begin()), 1941 std::make_move_iterator(PreprocessorDirectives.end())); 1942 PreprocessorDirectives.clear(); 1943 } 1944 } 1945 1946 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } 1947 1948 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { 1949 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && 1950 FormatTok.NewlinesBefore > 0; 1951 } 1952 1953 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { 1954 bool JustComments = Line->Tokens.empty(); 1955 for (SmallVectorImpl<FormatToken *>::const_iterator 1956 I = CommentsBeforeNextToken.begin(), 1957 E = CommentsBeforeNextToken.end(); 1958 I != E; ++I) { 1959 if (isOnNewLine(**I) && JustComments) 1960 addUnwrappedLine(); 1961 pushToken(*I); 1962 } 1963 if (NewlineBeforeNext && JustComments) 1964 addUnwrappedLine(); 1965 CommentsBeforeNextToken.clear(); 1966 } 1967 1968 void UnwrappedLineParser::nextToken() { 1969 if (eof()) 1970 return; 1971 flushComments(isOnNewLine(*FormatTok)); 1972 pushToken(FormatTok); 1973 if (Style.Language != FormatStyle::LK_JavaScript) 1974 readToken(); 1975 else 1976 readTokenWithJavaScriptASI(); 1977 } 1978 1979 const FormatToken *UnwrappedLineParser::getPreviousToken() { 1980 // FIXME: This is a dirty way to access the previous token. Find a better 1981 // solution. 1982 if (!Line || Line->Tokens.empty()) 1983 return nullptr; 1984 return Line->Tokens.back().Tok; 1985 } 1986 1987 void UnwrappedLineParser::readToken() { 1988 bool CommentsInCurrentLine = true; 1989 do { 1990 FormatTok = Tokens->getNextToken(); 1991 assert(FormatTok); 1992 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && 1993 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { 1994 // If there is an unfinished unwrapped line, we flush the preprocessor 1995 // directives only after that unwrapped line was finished later. 1996 bool SwitchToPreprocessorLines = !Line->Tokens.empty(); 1997 ScopedLineState BlockState(*this, SwitchToPreprocessorLines); 1998 // Comments stored before the preprocessor directive need to be output 1999 // before the preprocessor directive, at the same level as the 2000 // preprocessor directive, as we consider them to apply to the directive. 2001 flushComments(isOnNewLine(*FormatTok)); 2002 parsePPDirective(); 2003 } 2004 while (FormatTok->Type == TT_ConflictStart || 2005 FormatTok->Type == TT_ConflictEnd || 2006 FormatTok->Type == TT_ConflictAlternative) { 2007 if (FormatTok->Type == TT_ConflictStart) { 2008 conditionalCompilationStart(/*Unreachable=*/false); 2009 } else if (FormatTok->Type == TT_ConflictAlternative) { 2010 conditionalCompilationAlternative(); 2011 } else if (FormatTok->Type == TT_ConflictEnd) { 2012 conditionalCompilationEnd(); 2013 } 2014 FormatTok = Tokens->getNextToken(); 2015 FormatTok->MustBreakBefore = true; 2016 } 2017 2018 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) && 2019 !Line->InPPDirective) { 2020 continue; 2021 } 2022 2023 if (!FormatTok->Tok.is(tok::comment)) 2024 return; 2025 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) { 2026 CommentsInCurrentLine = false; 2027 } 2028 if (CommentsInCurrentLine) { 2029 pushToken(FormatTok); 2030 } else { 2031 CommentsBeforeNextToken.push_back(FormatTok); 2032 } 2033 } while (!eof()); 2034 } 2035 2036 void UnwrappedLineParser::pushToken(FormatToken *Tok) { 2037 Line->Tokens.push_back(UnwrappedLineNode(Tok)); 2038 if (MustBreakBeforeNextToken) { 2039 Line->Tokens.back().Tok->MustBreakBefore = true; 2040 MustBreakBeforeNextToken = false; 2041 } 2042 } 2043 2044 } // end namespace format 2045 } // end namespace clang 2046