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 #define DEBUG_TYPE "format-parser" 17 18 #include "UnwrappedLineParser.h" 19 #include "llvm/Support/Debug.h" 20 21 namespace clang { 22 namespace format { 23 24 class FormatTokenSource { 25 public: 26 virtual ~FormatTokenSource() {} 27 virtual FormatToken *getNextToken() = 0; 28 29 virtual unsigned getPosition() = 0; 30 virtual FormatToken *setPosition(unsigned Position) = 0; 31 }; 32 33 namespace { 34 35 class ScopedDeclarationState { 36 public: 37 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack, 38 bool MustBeDeclaration) 39 : Line(Line), Stack(Stack) { 40 Line.MustBeDeclaration = MustBeDeclaration; 41 Stack.push_back(MustBeDeclaration); 42 } 43 ~ScopedDeclarationState() { 44 Stack.pop_back(); 45 if (!Stack.empty()) 46 Line.MustBeDeclaration = Stack.back(); 47 else 48 Line.MustBeDeclaration = true; 49 } 50 51 private: 52 UnwrappedLine &Line; 53 std::vector<bool> &Stack; 54 }; 55 56 class ScopedMacroState : public FormatTokenSource { 57 public: 58 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, 59 FormatToken *&ResetToken, bool &StructuralError) 60 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), 61 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), 62 StructuralError(StructuralError), 63 PreviousStructuralError(StructuralError), Token(NULL) { 64 TokenSource = this; 65 Line.Level = 0; 66 Line.InPPDirective = true; 67 } 68 69 ~ScopedMacroState() { 70 TokenSource = PreviousTokenSource; 71 ResetToken = Token; 72 Line.InPPDirective = false; 73 Line.Level = PreviousLineLevel; 74 StructuralError = PreviousStructuralError; 75 } 76 77 virtual FormatToken *getNextToken() { 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 virtual unsigned getPosition() { return PreviousTokenSource->getPosition(); } 88 89 virtual FormatToken *setPosition(unsigned Position) { 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 bool &StructuralError; 114 bool PreviousStructuralError; 115 116 FormatToken *Token; 117 }; 118 119 } // end anonymous namespace 120 121 class ScopedLineState { 122 public: 123 ScopedLineState(UnwrappedLineParser &Parser, 124 bool SwitchToPreprocessorLines = false) 125 : Parser(Parser) { 126 OriginalLines = Parser.CurrentLines; 127 if (SwitchToPreprocessorLines) 128 Parser.CurrentLines = &Parser.PreprocessorDirectives; 129 else if (!Parser.Line->Tokens.empty()) 130 Parser.CurrentLines = &Parser.Line->Tokens.back().Children; 131 PreBlockLine = Parser.Line.release(); 132 Parser.Line.reset(new UnwrappedLine()); 133 Parser.Line->Level = PreBlockLine->Level; 134 Parser.Line->InPPDirective = PreBlockLine->InPPDirective; 135 } 136 137 ~ScopedLineState() { 138 if (!Parser.Line->Tokens.empty()) { 139 Parser.addUnwrappedLine(); 140 } 141 assert(Parser.Line->Tokens.empty()); 142 Parser.Line.reset(PreBlockLine); 143 if (Parser.CurrentLines == &Parser.PreprocessorDirectives) 144 Parser.MustBreakBeforeNextToken = true; 145 Parser.CurrentLines = OriginalLines; 146 } 147 148 private: 149 UnwrappedLineParser &Parser; 150 151 UnwrappedLine *PreBlockLine; 152 SmallVectorImpl<UnwrappedLine> *OriginalLines; 153 }; 154 155 class CompoundStatementIndenter { 156 public: 157 CompoundStatementIndenter(UnwrappedLineParser *Parser, 158 const FormatStyle &Style, unsigned &LineLevel) 159 : LineLevel(LineLevel), OldLineLevel(LineLevel) { 160 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) { 161 Parser->addUnwrappedLine(); 162 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) { 163 Parser->addUnwrappedLine(); 164 ++LineLevel; 165 } 166 } 167 ~CompoundStatementIndenter() { 168 LineLevel = OldLineLevel; 169 } 170 171 private: 172 unsigned &LineLevel; 173 unsigned OldLineLevel; 174 }; 175 176 namespace { 177 178 class IndexedTokenSource : public FormatTokenSource { 179 public: 180 IndexedTokenSource(ArrayRef<FormatToken *> Tokens) 181 : Tokens(Tokens), Position(-1) {} 182 183 virtual FormatToken *getNextToken() { 184 ++Position; 185 return Tokens[Position]; 186 } 187 188 virtual unsigned getPosition() { 189 assert(Position >= 0); 190 return Position; 191 } 192 193 virtual FormatToken *setPosition(unsigned P) { 194 Position = P; 195 return Tokens[Position]; 196 } 197 198 void reset() { Position = -1; } 199 200 private: 201 ArrayRef<FormatToken *> Tokens; 202 int Position; 203 }; 204 205 } // end anonymous namespace 206 207 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, 208 ArrayRef<FormatToken *> Tokens, 209 UnwrappedLineConsumer &Callback) 210 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), 211 CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL), 212 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {} 213 214 void UnwrappedLineParser::reset() { 215 PPBranchLevel = -1; 216 Line.reset(new UnwrappedLine); 217 CommentsBeforeNextToken.clear(); 218 FormatTok = NULL; 219 MustBreakBeforeNextToken = false; 220 PreprocessorDirectives.clear(); 221 CurrentLines = &Lines; 222 DeclarationScopeStack.clear(); 223 StructuralError = false; 224 PPStack.clear(); 225 } 226 227 bool UnwrappedLineParser::parse() { 228 IndexedTokenSource TokenSource(AllTokens); 229 do { 230 DEBUG(llvm::dbgs() << "----\n"); 231 reset(); 232 Tokens = &TokenSource; 233 TokenSource.reset(); 234 235 readToken(); 236 parseFile(); 237 // Create line with eof token. 238 pushToken(FormatTok); 239 addUnwrappedLine(); 240 241 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(), 242 E = Lines.end(); 243 I != E; ++I) { 244 Callback.consumeUnwrappedLine(*I); 245 } 246 Callback.finishRun(); 247 Lines.clear(); 248 while (!PPLevelBranchIndex.empty() && 249 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) { 250 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); 251 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); 252 } 253 if (!PPLevelBranchIndex.empty()) { 254 ++PPLevelBranchIndex.back(); 255 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); 256 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); 257 } 258 } while (!PPLevelBranchIndex.empty()); 259 260 return StructuralError; 261 } 262 263 void UnwrappedLineParser::parseFile() { 264 ScopedDeclarationState DeclarationState( 265 *Line, DeclarationScopeStack, 266 /*MustBeDeclaration=*/ !Line->InPPDirective); 267 parseLevel(/*HasOpeningBrace=*/false); 268 // Make sure to format the remaining tokens. 269 flushComments(true); 270 addUnwrappedLine(); 271 } 272 273 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { 274 bool SwitchLabelEncountered = false; 275 do { 276 switch (FormatTok->Tok.getKind()) { 277 case tok::comment: 278 nextToken(); 279 addUnwrappedLine(); 280 break; 281 case tok::l_brace: 282 // FIXME: Add parameter whether this can happen - if this happens, we must 283 // be in a non-declaration context. 284 parseBlock(/*MustBeDeclaration=*/false); 285 addUnwrappedLine(); 286 break; 287 case tok::r_brace: 288 if (HasOpeningBrace) 289 return; 290 StructuralError = true; 291 nextToken(); 292 addUnwrappedLine(); 293 break; 294 case tok::kw_default: 295 case tok::kw_case: 296 if (!SwitchLabelEncountered && 297 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1))) 298 ++Line->Level; 299 SwitchLabelEncountered = true; 300 parseStructuralElement(); 301 break; 302 default: 303 parseStructuralElement(); 304 break; 305 } 306 } while (!eof()); 307 } 308 309 void UnwrappedLineParser::calculateBraceTypes() { 310 // We'll parse forward through the tokens until we hit 311 // a closing brace or eof - note that getNextToken() will 312 // parse macros, so this will magically work inside macro 313 // definitions, too. 314 unsigned StoredPosition = Tokens->getPosition(); 315 unsigned Position = StoredPosition; 316 FormatToken *Tok = FormatTok; 317 // Keep a stack of positions of lbrace tokens. We will 318 // update information about whether an lbrace starts a 319 // braced init list or a different block during the loop. 320 SmallVector<FormatToken *, 8> LBraceStack; 321 assert(Tok->Tok.is(tok::l_brace)); 322 do { 323 // Get next none-comment token. 324 FormatToken *NextTok; 325 unsigned ReadTokens = 0; 326 do { 327 NextTok = Tokens->getNextToken(); 328 ++ReadTokens; 329 } while (NextTok->is(tok::comment)); 330 331 switch (Tok->Tok.getKind()) { 332 case tok::l_brace: 333 LBraceStack.push_back(Tok); 334 break; 335 case tok::r_brace: 336 if (!LBraceStack.empty()) { 337 if (LBraceStack.back()->BlockKind == BK_Unknown) { 338 // If there is a comma, semicolon or right paren after the closing 339 // brace, we assume this is a braced initializer list. Note that 340 // regardless how we mark inner braces here, we will overwrite the 341 // BlockKind later if we parse a braced list (where all blocks inside 342 // are by default braced lists), or when we explicitly detect blocks 343 // (for example while parsing lambdas). 344 // 345 // We exclude + and - as they can be ObjC visibility modifiers. 346 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren, tok::period, 347 tok::r_square, tok::l_brace, tok::colon) || 348 (NextTok->isBinaryOperator() && 349 !NextTok->isOneOf(tok::plus, tok::minus))) { 350 Tok->BlockKind = BK_BracedInit; 351 LBraceStack.back()->BlockKind = BK_BracedInit; 352 } else { 353 Tok->BlockKind = BK_Block; 354 LBraceStack.back()->BlockKind = BK_Block; 355 } 356 } 357 LBraceStack.pop_back(); 358 } 359 break; 360 case tok::semi: 361 case tok::kw_if: 362 case tok::kw_while: 363 case tok::kw_for: 364 case tok::kw_switch: 365 case tok::kw_try: 366 if (!LBraceStack.empty()) 367 LBraceStack.back()->BlockKind = BK_Block; 368 break; 369 default: 370 break; 371 } 372 Tok = NextTok; 373 Position += ReadTokens; 374 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty()); 375 // Assume other blocks for all unclosed opening braces. 376 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { 377 if (LBraceStack[i]->BlockKind == BK_Unknown) 378 LBraceStack[i]->BlockKind = BK_Block; 379 } 380 381 FormatTok = Tokens->setPosition(StoredPosition); 382 } 383 384 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, 385 bool MunchSemi) { 386 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected"); 387 unsigned InitialLevel = Line->Level; 388 nextToken(); 389 390 addUnwrappedLine(); 391 392 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 393 MustBeDeclaration); 394 if (AddLevel) 395 ++Line->Level; 396 parseLevel(/*HasOpeningBrace=*/true); 397 398 if (!FormatTok->Tok.is(tok::r_brace)) { 399 Line->Level = InitialLevel; 400 StructuralError = true; 401 return; 402 } 403 404 nextToken(); // Munch the closing brace. 405 if (MunchSemi && FormatTok->Tok.is(tok::semi)) 406 nextToken(); 407 Line->Level = InitialLevel; 408 } 409 410 void UnwrappedLineParser::parseChildBlock() { 411 FormatTok->BlockKind = BK_Block; 412 nextToken(); 413 { 414 ScopedLineState LineState(*this); 415 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 416 /*MustBeDeclaration=*/false); 417 Line->Level += 1; 418 parseLevel(/*HasOpeningBrace=*/true); 419 Line->Level -= 1; 420 } 421 nextToken(); 422 } 423 424 void UnwrappedLineParser::parsePPDirective() { 425 assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); 426 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError); 427 nextToken(); 428 429 if (FormatTok->Tok.getIdentifierInfo() == NULL) { 430 parsePPUnknown(); 431 return; 432 } 433 434 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { 435 case tok::pp_define: 436 parsePPDefine(); 437 return; 438 case tok::pp_if: 439 parsePPIf(/*IfDef=*/false); 440 break; 441 case tok::pp_ifdef: 442 case tok::pp_ifndef: 443 parsePPIf(/*IfDef=*/true); 444 break; 445 case tok::pp_else: 446 parsePPElse(); 447 break; 448 case tok::pp_elif: 449 parsePPElIf(); 450 break; 451 case tok::pp_endif: 452 parsePPEndIf(); 453 break; 454 default: 455 parsePPUnknown(); 456 break; 457 } 458 } 459 460 void UnwrappedLineParser::pushPPConditional() { 461 if (!PPStack.empty() && PPStack.back() == PP_Unreachable) 462 PPStack.push_back(PP_Unreachable); 463 else 464 PPStack.push_back(PP_Conditional); 465 } 466 467 void UnwrappedLineParser::parsePPIf(bool IfDef) { 468 ++PPBranchLevel; 469 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); 470 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { 471 PPLevelBranchIndex.push_back(0); 472 PPLevelBranchCount.push_back(0); 473 } 474 PPChainBranchIndex.push(0); 475 nextToken(); 476 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() && 477 StringRef(FormatTok->Tok.getLiteralData(), 478 FormatTok->Tok.getLength()) == "0") || 479 FormatTok->Tok.is(tok::kw_false); 480 if ((!IfDef && IsLiteralFalse) || PPLevelBranchIndex[PPBranchLevel] > 0) { 481 PPStack.push_back(PP_Unreachable); 482 } else { 483 pushPPConditional(); 484 } 485 parsePPUnknown(); 486 } 487 488 void UnwrappedLineParser::parsePPElse() { 489 if (!PPStack.empty()) 490 PPStack.pop_back(); 491 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 492 if (!PPChainBranchIndex.empty()) 493 ++PPChainBranchIndex.top(); 494 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && 495 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()) { 496 PPStack.push_back(PP_Unreachable); 497 } else { 498 pushPPConditional(); 499 } 500 parsePPUnknown(); 501 } 502 503 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } 504 505 void UnwrappedLineParser::parsePPEndIf() { 506 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 507 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { 508 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { 509 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; 510 } 511 } 512 // Guard against #endif's without #if. 513 if (PPBranchLevel > 0) 514 --PPBranchLevel; 515 if (!PPChainBranchIndex.empty()) 516 PPChainBranchIndex.pop(); 517 if (!PPStack.empty()) 518 PPStack.pop_back(); 519 parsePPUnknown(); 520 } 521 522 void UnwrappedLineParser::parsePPDefine() { 523 nextToken(); 524 525 if (FormatTok->Tok.getKind() != tok::identifier) { 526 parsePPUnknown(); 527 return; 528 } 529 nextToken(); 530 if (FormatTok->Tok.getKind() == tok::l_paren && 531 FormatTok->WhitespaceRange.getBegin() == 532 FormatTok->WhitespaceRange.getEnd()) { 533 parseParens(); 534 } 535 addUnwrappedLine(); 536 Line->Level = 1; 537 538 // Errors during a preprocessor directive can only affect the layout of the 539 // preprocessor directive, and thus we ignore them. An alternative approach 540 // would be to use the same approach we use on the file level (no 541 // re-indentation if there was a structural error) within the macro 542 // definition. 543 parseFile(); 544 } 545 546 void UnwrappedLineParser::parsePPUnknown() { 547 do { 548 nextToken(); 549 } while (!eof()); 550 addUnwrappedLine(); 551 } 552 553 // Here we blacklist certain tokens that are not usually the first token in an 554 // unwrapped line. This is used in attempt to distinguish macro calls without 555 // trailing semicolons from other constructs split to several lines. 556 bool tokenCanStartNewLine(clang::Token Tok) { 557 // Semicolon can be a null-statement, l_square can be a start of a macro or 558 // a C++11 attribute, but this doesn't seem to be common. 559 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && 560 Tok.isNot(tok::l_square) && 561 // Tokens that can only be used as binary operators and a part of 562 // overloaded operator names. 563 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && 564 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && 565 Tok.isNot(tok::less) && Tok.isNot(tok::greater) && 566 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && 567 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && 568 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && 569 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && 570 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && 571 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && 572 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && 573 Tok.isNot(tok::lesslessequal) && 574 // Colon is used in labels, base class lists, initializer lists, 575 // range-based for loops, ternary operator, but should never be the 576 // first token in an unwrapped line. 577 Tok.isNot(tok::colon); 578 } 579 580 void UnwrappedLineParser::parseStructuralElement() { 581 assert(!FormatTok->Tok.is(tok::l_brace)); 582 switch (FormatTok->Tok.getKind()) { 583 case tok::at: 584 nextToken(); 585 if (FormatTok->Tok.is(tok::l_brace)) { 586 parseBracedList(); 587 break; 588 } 589 switch (FormatTok->Tok.getObjCKeywordID()) { 590 case tok::objc_public: 591 case tok::objc_protected: 592 case tok::objc_package: 593 case tok::objc_private: 594 return parseAccessSpecifier(); 595 case tok::objc_interface: 596 case tok::objc_implementation: 597 return parseObjCInterfaceOrImplementation(); 598 case tok::objc_protocol: 599 return parseObjCProtocol(); 600 case tok::objc_end: 601 return; // Handled by the caller. 602 case tok::objc_optional: 603 case tok::objc_required: 604 nextToken(); 605 addUnwrappedLine(); 606 return; 607 default: 608 break; 609 } 610 break; 611 case tok::kw_namespace: 612 parseNamespace(); 613 return; 614 case tok::kw_inline: 615 nextToken(); 616 if (FormatTok->Tok.is(tok::kw_namespace)) { 617 parseNamespace(); 618 return; 619 } 620 break; 621 case tok::kw_public: 622 case tok::kw_protected: 623 case tok::kw_private: 624 parseAccessSpecifier(); 625 return; 626 case tok::kw_if: 627 parseIfThenElse(); 628 return; 629 case tok::kw_for: 630 case tok::kw_while: 631 parseForOrWhileLoop(); 632 return; 633 case tok::kw_do: 634 parseDoWhile(); 635 return; 636 case tok::kw_switch: 637 parseSwitch(); 638 return; 639 case tok::kw_default: 640 nextToken(); 641 parseLabel(); 642 return; 643 case tok::kw_case: 644 parseCaseLabel(); 645 return; 646 case tok::kw_extern: 647 nextToken(); 648 if (FormatTok->Tok.is(tok::string_literal)) { 649 nextToken(); 650 if (FormatTok->Tok.is(tok::l_brace)) { 651 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); 652 addUnwrappedLine(); 653 return; 654 } 655 } 656 // In all other cases, parse the declaration. 657 break; 658 default: 659 break; 660 } 661 do { 662 switch (FormatTok->Tok.getKind()) { 663 case tok::at: 664 nextToken(); 665 if (FormatTok->Tok.is(tok::l_brace)) 666 parseBracedList(); 667 break; 668 case tok::kw_enum: 669 parseEnum(); 670 break; 671 case tok::kw_typedef: 672 nextToken(); 673 // FIXME: Use the IdentifierTable instead. 674 if (FormatTok->TokenText == "NS_ENUM") 675 parseEnum(); 676 break; 677 case tok::kw_struct: 678 case tok::kw_union: 679 case tok::kw_class: 680 parseRecord(); 681 // A record declaration or definition is always the start of a structural 682 // element. 683 break; 684 case tok::semi: 685 nextToken(); 686 addUnwrappedLine(); 687 return; 688 case tok::r_brace: 689 addUnwrappedLine(); 690 return; 691 case tok::l_paren: 692 parseParens(); 693 break; 694 case tok::caret: 695 nextToken(); 696 if (FormatTok->is(tok::l_brace)) { 697 parseChildBlock(); 698 } 699 break; 700 case tok::l_brace: 701 if (!tryToParseBracedList()) { 702 // A block outside of parentheses must be the last part of a 703 // structural element. 704 // FIXME: Figure out cases where this is not true, and add projections 705 // for them (the one we know is missing are lambdas). 706 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach) 707 addUnwrappedLine(); 708 FormatTok->Type = TT_FunctionLBrace; 709 parseBlock(/*MustBeDeclaration=*/false); 710 addUnwrappedLine(); 711 return; 712 } 713 // Otherwise this was a braced init list, and the structural 714 // element continues. 715 break; 716 case tok::identifier: { 717 StringRef Text = FormatTok->TokenText; 718 nextToken(); 719 if (Line->Tokens.size() == 1) { 720 if (FormatTok->Tok.is(tok::colon)) { 721 parseLabel(); 722 return; 723 } 724 // Recognize function-like macro usages without trailing semicolon. 725 if (FormatTok->Tok.is(tok::l_paren)) { 726 parseParens(); 727 if (FormatTok->NewlinesBefore > 0 && 728 tokenCanStartNewLine(FormatTok->Tok)) { 729 addUnwrappedLine(); 730 return; 731 } 732 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 && 733 Text == Text.upper()) { 734 // Recognize free-standing macros like Q_OBJECT. 735 addUnwrappedLine(); 736 return; 737 } 738 } 739 break; 740 } 741 case tok::equal: 742 nextToken(); 743 if (FormatTok->Tok.is(tok::l_brace)) { 744 parseBracedList(); 745 } 746 break; 747 case tok::l_square: 748 parseSquare(); 749 break; 750 default: 751 nextToken(); 752 break; 753 } 754 } while (!eof()); 755 } 756 757 bool UnwrappedLineParser::tryToParseLambda() { 758 // FIXME: This is a dirty way to access the previous token. Find a better 759 // solution. 760 if (!Line->Tokens.empty() && 761 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) || 762 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) { 763 nextToken(); 764 return false; 765 } 766 assert(FormatTok->is(tok::l_square)); 767 FormatToken &LSquare = *FormatTok; 768 if (!tryToParseLambdaIntroducer()) 769 return false; 770 771 while (FormatTok && FormatTok->isNot(tok::l_brace)) { 772 if (FormatTok->isSimpleTypeSpecifier()) { 773 nextToken(); 774 continue; 775 } 776 switch (FormatTok->Tok.getKind()) { 777 case tok::l_brace: 778 break; 779 case tok::l_paren: 780 parseParens(); 781 break; 782 case tok::less: 783 case tok::greater: 784 case tok::identifier: 785 case tok::coloncolon: 786 case tok::kw_mutable: 787 nextToken(); 788 break; 789 case tok::arrow: 790 FormatTok->Type = TT_TrailingReturnArrow; 791 nextToken(); 792 break; 793 default: 794 return true; 795 } 796 } 797 LSquare.Type = TT_LambdaLSquare; 798 parseChildBlock(); 799 return true; 800 } 801 802 bool UnwrappedLineParser::tryToParseLambdaIntroducer() { 803 nextToken(); 804 if (FormatTok->is(tok::equal)) { 805 nextToken(); 806 if (FormatTok->is(tok::r_square)) { 807 nextToken(); 808 return true; 809 } 810 if (FormatTok->isNot(tok::comma)) 811 return false; 812 nextToken(); 813 } else if (FormatTok->is(tok::amp)) { 814 nextToken(); 815 if (FormatTok->is(tok::r_square)) { 816 nextToken(); 817 return true; 818 } 819 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) { 820 return false; 821 } 822 if (FormatTok->is(tok::comma)) 823 nextToken(); 824 } else if (FormatTok->is(tok::r_square)) { 825 nextToken(); 826 return true; 827 } 828 do { 829 if (FormatTok->is(tok::amp)) 830 nextToken(); 831 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this)) 832 return false; 833 nextToken(); 834 if (FormatTok->is(tok::comma)) { 835 nextToken(); 836 } else if (FormatTok->is(tok::r_square)) { 837 nextToken(); 838 return true; 839 } else { 840 return false; 841 } 842 } while (!eof()); 843 return false; 844 } 845 846 bool UnwrappedLineParser::tryToParseBracedList() { 847 if (FormatTok->BlockKind == BK_Unknown) 848 calculateBraceTypes(); 849 assert(FormatTok->BlockKind != BK_Unknown); 850 if (FormatTok->BlockKind == BK_Block) 851 return false; 852 parseBracedList(); 853 return true; 854 } 855 856 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) { 857 bool HasError = false; 858 nextToken(); 859 860 // FIXME: Once we have an expression parser in the UnwrappedLineParser, 861 // replace this by using parseAssigmentExpression() inside. 862 do { 863 // FIXME: When we start to support lambdas, we'll want to parse them away 864 // here, otherwise our bail-out scenarios below break. The better solution 865 // might be to just implement a more or less complete expression parser. 866 switch (FormatTok->Tok.getKind()) { 867 case tok::caret: 868 nextToken(); 869 if (FormatTok->is(tok::l_brace)) { 870 parseChildBlock(); 871 } 872 break; 873 case tok::l_square: 874 tryToParseLambda(); 875 break; 876 case tok::l_brace: 877 // Assume there are no blocks inside a braced init list apart 878 // from the ones we explicitly parse out (like lambdas). 879 FormatTok->BlockKind = BK_BracedInit; 880 parseBracedList(); 881 break; 882 case tok::r_brace: 883 nextToken(); 884 return !HasError; 885 case tok::semi: 886 HasError = true; 887 if (!ContinueOnSemicolons) 888 return !HasError; 889 nextToken(); 890 break; 891 case tok::comma: 892 nextToken(); 893 break; 894 default: 895 nextToken(); 896 break; 897 } 898 } while (!eof()); 899 return false; 900 } 901 902 void UnwrappedLineParser::parseParens() { 903 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); 904 nextToken(); 905 do { 906 switch (FormatTok->Tok.getKind()) { 907 case tok::l_paren: 908 parseParens(); 909 break; 910 case tok::r_paren: 911 nextToken(); 912 return; 913 case tok::r_brace: 914 // A "}" inside parenthesis is an error if there wasn't a matching "{". 915 return; 916 case tok::l_square: 917 tryToParseLambda(); 918 break; 919 case tok::l_brace: { 920 if (!tryToParseBracedList()) { 921 parseChildBlock(); 922 } 923 break; 924 } 925 case tok::at: 926 nextToken(); 927 if (FormatTok->Tok.is(tok::l_brace)) 928 parseBracedList(); 929 break; 930 default: 931 nextToken(); 932 break; 933 } 934 } while (!eof()); 935 } 936 937 void UnwrappedLineParser::parseSquare() { 938 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); 939 if (tryToParseLambda()) 940 return; 941 do { 942 switch (FormatTok->Tok.getKind()) { 943 case tok::l_paren: 944 parseParens(); 945 break; 946 case tok::r_square: 947 nextToken(); 948 return; 949 case tok::r_brace: 950 // A "}" inside parenthesis is an error if there wasn't a matching "{". 951 return; 952 case tok::l_square: 953 parseSquare(); 954 break; 955 case tok::l_brace: { 956 if (!tryToParseBracedList()) { 957 parseChildBlock(); 958 } 959 break; 960 } 961 case tok::at: 962 nextToken(); 963 if (FormatTok->Tok.is(tok::l_brace)) 964 parseBracedList(); 965 break; 966 default: 967 nextToken(); 968 break; 969 } 970 } while (!eof()); 971 } 972 973 void UnwrappedLineParser::parseIfThenElse() { 974 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); 975 nextToken(); 976 if (FormatTok->Tok.is(tok::l_paren)) 977 parseParens(); 978 bool NeedsUnwrappedLine = false; 979 if (FormatTok->Tok.is(tok::l_brace)) { 980 CompoundStatementIndenter Indenter(this, Style, Line->Level); 981 parseBlock(/*MustBeDeclaration=*/false); 982 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || 983 Style.BreakBeforeBraces == FormatStyle::BS_GNU) { 984 addUnwrappedLine(); 985 } else { 986 NeedsUnwrappedLine = true; 987 } 988 } else { 989 addUnwrappedLine(); 990 ++Line->Level; 991 parseStructuralElement(); 992 --Line->Level; 993 } 994 if (FormatTok->Tok.is(tok::kw_else)) { 995 nextToken(); 996 if (FormatTok->Tok.is(tok::l_brace)) { 997 CompoundStatementIndenter Indenter(this, Style, Line->Level); 998 parseBlock(/*MustBeDeclaration=*/false); 999 addUnwrappedLine(); 1000 } else if (FormatTok->Tok.is(tok::kw_if)) { 1001 parseIfThenElse(); 1002 } else { 1003 addUnwrappedLine(); 1004 ++Line->Level; 1005 parseStructuralElement(); 1006 --Line->Level; 1007 } 1008 } else if (NeedsUnwrappedLine) { 1009 addUnwrappedLine(); 1010 } 1011 } 1012 1013 void UnwrappedLineParser::parseNamespace() { 1014 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); 1015 nextToken(); 1016 if (FormatTok->Tok.is(tok::identifier)) 1017 nextToken(); 1018 if (FormatTok->Tok.is(tok::l_brace)) { 1019 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || 1020 Style.BreakBeforeBraces == FormatStyle::BS_Allman || 1021 Style.BreakBeforeBraces == FormatStyle::BS_GNU) 1022 addUnwrappedLine(); 1023 1024 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || 1025 (Style.NamespaceIndentation == FormatStyle::NI_Inner && 1026 DeclarationScopeStack.size() > 1); 1027 parseBlock(/*MustBeDeclaration=*/true, AddLevel); 1028 // Munch the semicolon after a namespace. This is more common than one would 1029 // think. Puttin the semicolon into its own line is very ugly. 1030 if (FormatTok->Tok.is(tok::semi)) 1031 nextToken(); 1032 addUnwrappedLine(); 1033 } 1034 // FIXME: Add error handling. 1035 } 1036 1037 void UnwrappedLineParser::parseForOrWhileLoop() { 1038 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) && 1039 "'for' or 'while' expected"); 1040 nextToken(); 1041 if (FormatTok->Tok.is(tok::l_paren)) 1042 parseParens(); 1043 if (FormatTok->Tok.is(tok::l_brace)) { 1044 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1045 parseBlock(/*MustBeDeclaration=*/false); 1046 addUnwrappedLine(); 1047 } else { 1048 addUnwrappedLine(); 1049 ++Line->Level; 1050 parseStructuralElement(); 1051 --Line->Level; 1052 } 1053 } 1054 1055 void UnwrappedLineParser::parseDoWhile() { 1056 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); 1057 nextToken(); 1058 if (FormatTok->Tok.is(tok::l_brace)) { 1059 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1060 parseBlock(/*MustBeDeclaration=*/false); 1061 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) 1062 addUnwrappedLine(); 1063 } else { 1064 addUnwrappedLine(); 1065 ++Line->Level; 1066 parseStructuralElement(); 1067 --Line->Level; 1068 } 1069 1070 // FIXME: Add error handling. 1071 if (!FormatTok->Tok.is(tok::kw_while)) { 1072 addUnwrappedLine(); 1073 return; 1074 } 1075 1076 nextToken(); 1077 parseStructuralElement(); 1078 } 1079 1080 void UnwrappedLineParser::parseLabel() { 1081 nextToken(); 1082 unsigned OldLineLevel = Line->Level; 1083 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) 1084 --Line->Level; 1085 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { 1086 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1087 parseBlock(/*MustBeDeclaration=*/false); 1088 if (FormatTok->Tok.is(tok::kw_break)) { 1089 // "break;" after "}" on its own line only for BS_Allman and BS_GNU 1090 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman || 1091 Style.BreakBeforeBraces == FormatStyle::BS_GNU) { 1092 addUnwrappedLine(); 1093 } 1094 parseStructuralElement(); 1095 } 1096 addUnwrappedLine(); 1097 } else { 1098 addUnwrappedLine(); 1099 } 1100 Line->Level = OldLineLevel; 1101 } 1102 1103 void UnwrappedLineParser::parseCaseLabel() { 1104 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); 1105 // FIXME: fix handling of complex expressions here. 1106 do { 1107 nextToken(); 1108 } while (!eof() && !FormatTok->Tok.is(tok::colon)); 1109 parseLabel(); 1110 } 1111 1112 void UnwrappedLineParser::parseSwitch() { 1113 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); 1114 nextToken(); 1115 if (FormatTok->Tok.is(tok::l_paren)) 1116 parseParens(); 1117 if (FormatTok->Tok.is(tok::l_brace)) { 1118 CompoundStatementIndenter Indenter(this, Style, Line->Level); 1119 parseBlock(/*MustBeDeclaration=*/false); 1120 addUnwrappedLine(); 1121 } else { 1122 addUnwrappedLine(); 1123 ++Line->Level; 1124 parseStructuralElement(); 1125 --Line->Level; 1126 } 1127 } 1128 1129 void UnwrappedLineParser::parseAccessSpecifier() { 1130 nextToken(); 1131 // Understand Qt's slots. 1132 if (FormatTok->is(tok::identifier) && 1133 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS")) 1134 nextToken(); 1135 // Otherwise, we don't know what it is, and we'd better keep the next token. 1136 if (FormatTok->Tok.is(tok::colon)) 1137 nextToken(); 1138 addUnwrappedLine(); 1139 } 1140 1141 void UnwrappedLineParser::parseEnum() { 1142 if (FormatTok->Tok.is(tok::kw_enum)) { 1143 // Won't be 'enum' for NS_ENUMs. 1144 nextToken(); 1145 } 1146 // Eat up enum class ... 1147 if (FormatTok->Tok.is(tok::kw_class) || 1148 FormatTok->Tok.is(tok::kw_struct)) 1149 nextToken(); 1150 while (FormatTok->Tok.getIdentifierInfo() || 1151 FormatTok->isOneOf(tok::colon, tok::coloncolon)) { 1152 nextToken(); 1153 // We can have macros or attributes in between 'enum' and the enum name. 1154 if (FormatTok->Tok.is(tok::l_paren)) { 1155 parseParens(); 1156 } 1157 if (FormatTok->Tok.is(tok::identifier)) 1158 nextToken(); 1159 } 1160 if (FormatTok->Tok.is(tok::l_brace)) { 1161 FormatTok->BlockKind = BK_Block; 1162 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); 1163 if (HasError) { 1164 if (FormatTok->is(tok::semi)) 1165 nextToken(); 1166 addUnwrappedLine(); 1167 } 1168 } 1169 // We fall through to parsing a structural element afterwards, so that in 1170 // enum A {} n, m; 1171 // "} n, m;" will end up in one unwrapped line. 1172 } 1173 1174 void UnwrappedLineParser::parseRecord() { 1175 nextToken(); 1176 if (FormatTok->Tok.is(tok::identifier) || 1177 FormatTok->Tok.is(tok::kw___attribute) || 1178 FormatTok->Tok.is(tok::kw___declspec) || 1179 FormatTok->Tok.is(tok::kw_alignas)) { 1180 nextToken(); 1181 // We can have macros or attributes in between 'class' and the class name. 1182 if (FormatTok->Tok.is(tok::l_paren)) { 1183 parseParens(); 1184 } 1185 // The actual identifier can be a nested name specifier, and in macros 1186 // it is often token-pasted. 1187 while (FormatTok->Tok.is(tok::identifier) || 1188 FormatTok->Tok.is(tok::coloncolon) || 1189 FormatTok->Tok.is(tok::hashhash)) 1190 nextToken(); 1191 1192 // Note that parsing away template declarations here leads to incorrectly 1193 // accepting function declarations as record declarations. 1194 // In general, we cannot solve this problem. Consider: 1195 // class A<int> B() {} 1196 // which can be a function definition or a class definition when B() is a 1197 // macro. If we find enough real-world cases where this is a problem, we 1198 // can parse for the 'template' keyword in the beginning of the statement, 1199 // and thus rule out the record production in case there is no template 1200 // (this would still leave us with an ambiguity between template function 1201 // and class declarations). 1202 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) { 1203 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) { 1204 if (FormatTok->Tok.is(tok::semi)) 1205 return; 1206 nextToken(); 1207 } 1208 } 1209 } 1210 if (FormatTok->Tok.is(tok::l_brace)) { 1211 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || 1212 Style.BreakBeforeBraces == FormatStyle::BS_Allman || 1213 Style.BreakBeforeBraces == FormatStyle::BS_GNU) 1214 addUnwrappedLine(); 1215 1216 parseBlock(/*MustBeDeclaration=*/true, /*Addlevel=*/true, 1217 /*MunchSemi=*/false); 1218 } 1219 // We fall through to parsing a structural element afterwards, so 1220 // class A {} n, m; 1221 // will end up in one unwrapped line. 1222 } 1223 1224 void UnwrappedLineParser::parseObjCProtocolList() { 1225 assert(FormatTok->Tok.is(tok::less) && "'<' expected."); 1226 do 1227 nextToken(); 1228 while (!eof() && FormatTok->Tok.isNot(tok::greater)); 1229 nextToken(); // Skip '>'. 1230 } 1231 1232 void UnwrappedLineParser::parseObjCUntilAtEnd() { 1233 do { 1234 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { 1235 nextToken(); 1236 addUnwrappedLine(); 1237 break; 1238 } 1239 if (FormatTok->is(tok::l_brace)) { 1240 parseBlock(/*MustBeDeclaration=*/false); 1241 // In ObjC interfaces, nothing should be following the "}". 1242 addUnwrappedLine(); 1243 } else if (FormatTok->is(tok::r_brace)) { 1244 // Ignore stray "}". parseStructuralElement doesn't consume them. 1245 nextToken(); 1246 addUnwrappedLine(); 1247 } else { 1248 parseStructuralElement(); 1249 } 1250 } while (!eof()); 1251 } 1252 1253 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { 1254 nextToken(); 1255 nextToken(); // interface name 1256 1257 // @interface can be followed by either a base class, or a category. 1258 if (FormatTok->Tok.is(tok::colon)) { 1259 nextToken(); 1260 nextToken(); // base class name 1261 } else if (FormatTok->Tok.is(tok::l_paren)) 1262 // Skip category, if present. 1263 parseParens(); 1264 1265 if (FormatTok->Tok.is(tok::less)) 1266 parseObjCProtocolList(); 1267 1268 // If instance variables are present, keep the '{' on the first line too. 1269 if (FormatTok->Tok.is(tok::l_brace)) 1270 parseBlock(/*MustBeDeclaration=*/true); 1271 1272 // With instance variables, this puts '}' on its own line. Without instance 1273 // variables, this ends the @interface line. 1274 addUnwrappedLine(); 1275 1276 parseObjCUntilAtEnd(); 1277 } 1278 1279 void UnwrappedLineParser::parseObjCProtocol() { 1280 nextToken(); 1281 nextToken(); // protocol name 1282 1283 if (FormatTok->Tok.is(tok::less)) 1284 parseObjCProtocolList(); 1285 1286 // Check for protocol declaration. 1287 if (FormatTok->Tok.is(tok::semi)) { 1288 nextToken(); 1289 return addUnwrappedLine(); 1290 } 1291 1292 addUnwrappedLine(); 1293 parseObjCUntilAtEnd(); 1294 } 1295 1296 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, 1297 StringRef Prefix = "") { 1298 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")" 1299 << (Line.InPPDirective ? " MACRO" : "") << ": "; 1300 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 1301 E = Line.Tokens.end(); 1302 I != E; ++I) { 1303 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] "; 1304 } 1305 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), 1306 E = Line.Tokens.end(); 1307 I != E; ++I) { 1308 const UnwrappedLineNode &Node = *I; 1309 for (SmallVectorImpl<UnwrappedLine>::const_iterator 1310 I = Node.Children.begin(), 1311 E = Node.Children.end(); 1312 I != E; ++I) { 1313 printDebugInfo(*I, "\nChild: "); 1314 } 1315 } 1316 llvm::dbgs() << "\n"; 1317 } 1318 1319 void UnwrappedLineParser::addUnwrappedLine() { 1320 if (Line->Tokens.empty()) 1321 return; 1322 DEBUG({ 1323 if (CurrentLines == &Lines) 1324 printDebugInfo(*Line); 1325 }); 1326 CurrentLines->push_back(*Line); 1327 Line->Tokens.clear(); 1328 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { 1329 for (SmallVectorImpl<UnwrappedLine>::iterator 1330 I = PreprocessorDirectives.begin(), 1331 E = PreprocessorDirectives.end(); 1332 I != E; ++I) { 1333 CurrentLines->push_back(*I); 1334 } 1335 PreprocessorDirectives.clear(); 1336 } 1337 } 1338 1339 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } 1340 1341 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { 1342 bool JustComments = Line->Tokens.empty(); 1343 for (SmallVectorImpl<FormatToken *>::const_iterator 1344 I = CommentsBeforeNextToken.begin(), 1345 E = CommentsBeforeNextToken.end(); 1346 I != E; ++I) { 1347 if ((*I)->NewlinesBefore && JustComments) { 1348 addUnwrappedLine(); 1349 } 1350 pushToken(*I); 1351 } 1352 if (NewlineBeforeNext && JustComments) { 1353 addUnwrappedLine(); 1354 } 1355 CommentsBeforeNextToken.clear(); 1356 } 1357 1358 void UnwrappedLineParser::nextToken() { 1359 if (eof()) 1360 return; 1361 flushComments(FormatTok->NewlinesBefore > 0); 1362 pushToken(FormatTok); 1363 readToken(); 1364 } 1365 1366 void UnwrappedLineParser::readToken() { 1367 bool CommentsInCurrentLine = true; 1368 do { 1369 FormatTok = Tokens->getNextToken(); 1370 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && 1371 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { 1372 // If there is an unfinished unwrapped line, we flush the preprocessor 1373 // directives only after that unwrapped line was finished later. 1374 bool SwitchToPreprocessorLines = 1375 !Line->Tokens.empty() && CurrentLines == &Lines; 1376 ScopedLineState BlockState(*this, SwitchToPreprocessorLines); 1377 // Comments stored before the preprocessor directive need to be output 1378 // before the preprocessor directive, at the same level as the 1379 // preprocessor directive, as we consider them to apply to the directive. 1380 flushComments(FormatTok->NewlinesBefore > 0); 1381 parsePPDirective(); 1382 } 1383 1384 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) && 1385 !Line->InPPDirective) { 1386 continue; 1387 } 1388 1389 if (!FormatTok->Tok.is(tok::comment)) 1390 return; 1391 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) { 1392 CommentsInCurrentLine = false; 1393 } 1394 if (CommentsInCurrentLine) { 1395 pushToken(FormatTok); 1396 } else { 1397 CommentsBeforeNextToken.push_back(FormatTok); 1398 } 1399 } while (!eof()); 1400 } 1401 1402 void UnwrappedLineParser::pushToken(FormatToken *Tok) { 1403 Line->Tokens.push_back(UnwrappedLineNode(Tok)); 1404 if (MustBreakBeforeNextToken) { 1405 Line->Tokens.back().Tok->MustBreakBefore = true; 1406 MustBreakBeforeNextToken = false; 1407 } 1408 } 1409 1410 } // end namespace format 1411 } // end namespace clang 1412