1 //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file contains the implementation of the UnwrappedLineParser, 11 /// which turns a stream of tokens into UnwrappedLines. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "UnwrappedLineParser.h" 16 #include "FormatToken.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/Support/Debug.h" 19 #include "llvm/Support/raw_ostream.h" 20 21 #include <algorithm> 22 23 #define DEBUG_TYPE "format-parser" 24 25 namespace clang { 26 namespace format { 27 28 class FormatTokenSource { 29 public: 30 virtual ~FormatTokenSource() {} 31 virtual FormatToken *getNextToken() = 0; 32 33 virtual unsigned getPosition() = 0; 34 virtual FormatToken *setPosition(unsigned Position) = 0; 35 }; 36 37 namespace { 38 39 class ScopedDeclarationState { 40 public: 41 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack, 42 bool MustBeDeclaration) 43 : Line(Line), Stack(Stack) { 44 Line.MustBeDeclaration = MustBeDeclaration; 45 Stack.push_back(MustBeDeclaration); 46 } 47 ~ScopedDeclarationState() { 48 Stack.pop_back(); 49 if (!Stack.empty()) 50 Line.MustBeDeclaration = Stack.back(); 51 else 52 Line.MustBeDeclaration = true; 53 } 54 55 private: 56 UnwrappedLine &Line; 57 std::vector<bool> &Stack; 58 }; 59 60 static bool isLineComment(const FormatToken &FormatTok) { 61 return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith("/*"); 62 } 63 64 // Checks if \p FormatTok is a line comment that continues the line comment 65 // \p Previous. The original column of \p MinColumnToken is used to determine 66 // whether \p FormatTok is indented enough to the right to continue \p Previous. 67 static bool continuesLineComment(const FormatToken &FormatTok, 68 const FormatToken *Previous, 69 const FormatToken *MinColumnToken) { 70 if (!Previous || !MinColumnToken) 71 return false; 72 unsigned MinContinueColumn = 73 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1); 74 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 && 75 isLineComment(*Previous) && 76 FormatTok.OriginalColumn >= MinContinueColumn; 77 } 78 79 class ScopedMacroState : public FormatTokenSource { 80 public: 81 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, 82 FormatToken *&ResetToken) 83 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), 84 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), 85 Token(nullptr), PreviousToken(nullptr) { 86 FakeEOF.Tok.startToken(); 87 FakeEOF.Tok.setKind(tok::eof); 88 TokenSource = this; 89 Line.Level = 0; 90 Line.InPPDirective = true; 91 } 92 93 ~ScopedMacroState() override { 94 TokenSource = PreviousTokenSource; 95 ResetToken = Token; 96 Line.InPPDirective = false; 97 Line.Level = PreviousLineLevel; 98 } 99 100 FormatToken *getNextToken() override { 101 // The \c UnwrappedLineParser guards against this by never calling 102 // \c getNextToken() after it has encountered the first eof token. 103 assert(!eof()); 104 PreviousToken = Token; 105 Token = PreviousTokenSource->getNextToken(); 106 if (eof()) 107 return &FakeEOF; 108 return Token; 109 } 110 111 unsigned getPosition() override { return PreviousTokenSource->getPosition(); } 112 113 FormatToken *setPosition(unsigned Position) override { 114 PreviousToken = nullptr; 115 Token = PreviousTokenSource->setPosition(Position); 116 return Token; 117 } 118 119 private: 120 bool eof() { 121 return Token && Token->HasUnescapedNewline && 122 !continuesLineComment(*Token, PreviousToken, 123 /*MinColumnToken=*/PreviousToken); 124 } 125 126 FormatToken FakeEOF; 127 UnwrappedLine &Line; 128 FormatTokenSource *&TokenSource; 129 FormatToken *&ResetToken; 130 unsigned PreviousLineLevel; 131 FormatTokenSource *PreviousTokenSource; 132 133 FormatToken *Token; 134 FormatToken *PreviousToken; 135 }; 136 137 } // end anonymous namespace 138 139 class ScopedLineState { 140 public: 141 ScopedLineState(UnwrappedLineParser &Parser, 142 bool SwitchToPreprocessorLines = false) 143 : Parser(Parser), OriginalLines(Parser.CurrentLines) { 144 if (SwitchToPreprocessorLines) 145 Parser.CurrentLines = &Parser.PreprocessorDirectives; 146 else if (!Parser.Line->Tokens.empty()) 147 Parser.CurrentLines = &Parser.Line->Tokens.back().Children; 148 PreBlockLine = std::move(Parser.Line); 149 Parser.Line = std::make_unique<UnwrappedLine>(); 150 Parser.Line->Level = PreBlockLine->Level; 151 Parser.Line->InPPDirective = PreBlockLine->InPPDirective; 152 } 153 154 ~ScopedLineState() { 155 if (!Parser.Line->Tokens.empty()) { 156 Parser.addUnwrappedLine(); 157 } 158 assert(Parser.Line->Tokens.empty()); 159 Parser.Line = std::move(PreBlockLine); 160 if (Parser.CurrentLines == &Parser.PreprocessorDirectives) 161 Parser.MustBreakBeforeNextToken = true; 162 Parser.CurrentLines = OriginalLines; 163 } 164 165 private: 166 UnwrappedLineParser &Parser; 167 168 std::unique_ptr<UnwrappedLine> PreBlockLine; 169 SmallVectorImpl<UnwrappedLine> *OriginalLines; 170 }; 171 172 class CompoundStatementIndenter { 173 public: 174 CompoundStatementIndenter(UnwrappedLineParser *Parser, 175 const FormatStyle &Style, unsigned &LineLevel) 176 : CompoundStatementIndenter(Parser, LineLevel, 177 Style.BraceWrapping.AfterControlStatement, 178 Style.BraceWrapping.IndentBraces) {} 179 CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel, 180 bool WrapBrace, bool IndentBrace) 181 : LineLevel(LineLevel), OldLineLevel(LineLevel) { 182 if (WrapBrace) 183 Parser->addUnwrappedLine(); 184 if (IndentBrace) 185 ++LineLevel; 186 } 187 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; } 188 189 private: 190 unsigned &LineLevel; 191 unsigned OldLineLevel; 192 }; 193 194 namespace { 195 196 class IndexedTokenSource : public FormatTokenSource { 197 public: 198 IndexedTokenSource(ArrayRef<FormatToken *> Tokens) 199 : Tokens(Tokens), Position(-1) {} 200 201 FormatToken *getNextToken() override { 202 ++Position; 203 return Tokens[Position]; 204 } 205 206 unsigned getPosition() override { 207 assert(Position >= 0); 208 return Position; 209 } 210 211 FormatToken *setPosition(unsigned P) override { 212 Position = P; 213 return Tokens[Position]; 214 } 215 216 void reset() { Position = -1; } 217 218 private: 219 ArrayRef<FormatToken *> Tokens; 220 int Position; 221 }; 222 223 } // end anonymous namespace 224 225 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, 226 const AdditionalKeywords &Keywords, 227 unsigned FirstStartColumn, 228 ArrayRef<FormatToken *> Tokens, 229 UnwrappedLineConsumer &Callback) 230 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), 231 CurrentLines(&Lines), Style(Style), Keywords(Keywords), 232 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr), 233 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1), 234 IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None 235 ? IG_Rejected 236 : IG_Inited), 237 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {} 238 239 void UnwrappedLineParser::reset() { 240 PPBranchLevel = -1; 241 IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None 242 ? IG_Rejected 243 : IG_Inited; 244 IncludeGuardToken = nullptr; 245 Line.reset(new UnwrappedLine); 246 CommentsBeforeNextToken.clear(); 247 FormatTok = nullptr; 248 MustBreakBeforeNextToken = false; 249 PreprocessorDirectives.clear(); 250 CurrentLines = &Lines; 251 DeclarationScopeStack.clear(); 252 PPStack.clear(); 253 Line->FirstStartColumn = FirstStartColumn; 254 } 255 256 void UnwrappedLineParser::parse() { 257 IndexedTokenSource TokenSource(AllTokens); 258 Line->FirstStartColumn = FirstStartColumn; 259 do { 260 LLVM_DEBUG(llvm::dbgs() << "----\n"); 261 reset(); 262 Tokens = &TokenSource; 263 TokenSource.reset(); 264 265 readToken(); 266 parseFile(); 267 268 // If we found an include guard then all preprocessor directives (other than 269 // the guard) are over-indented by one. 270 if (IncludeGuard == IG_Found) 271 for (auto &Line : Lines) 272 if (Line.InPPDirective && Line.Level > 0) 273 --Line.Level; 274 275 // Create line with eof token. 276 pushToken(FormatTok); 277 addUnwrappedLine(); 278 279 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(), 280 E = Lines.end(); 281 I != E; ++I) { 282 Callback.consumeUnwrappedLine(*I); 283 } 284 Callback.finishRun(); 285 Lines.clear(); 286 while (!PPLevelBranchIndex.empty() && 287 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) { 288 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); 289 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); 290 } 291 if (!PPLevelBranchIndex.empty()) { 292 ++PPLevelBranchIndex.back(); 293 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); 294 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); 295 } 296 } while (!PPLevelBranchIndex.empty()); 297 } 298 299 void UnwrappedLineParser::parseFile() { 300 // The top-level context in a file always has declarations, except for pre- 301 // processor directives and JavaScript files. 302 bool MustBeDeclaration = 303 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript; 304 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 305 MustBeDeclaration); 306 if (Style.Language == FormatStyle::LK_TextProto) 307 parseBracedList(); 308 else 309 parseLevel(/*HasOpeningBrace=*/false); 310 // Make sure to format the remaining tokens. 311 // 312 // LK_TextProto is special since its top-level is parsed as the body of a 313 // braced list, which does not necessarily have natural line separators such 314 // as a semicolon. Comments after the last entry that have been determined to 315 // not belong to that line, as in: 316 // key: value 317 // // endfile comment 318 // do not have a chance to be put on a line of their own until this point. 319 // Here we add this newline before end-of-file comments. 320 if (Style.Language == FormatStyle::LK_TextProto && 321 !CommentsBeforeNextToken.empty()) 322 addUnwrappedLine(); 323 flushComments(true); 324 addUnwrappedLine(); 325 } 326 327 void UnwrappedLineParser::parseCSharpGenericTypeConstraint() { 328 do { 329 switch (FormatTok->Tok.getKind()) { 330 case tok::l_brace: 331 return; 332 default: 333 if (FormatTok->is(Keywords.kw_where)) { 334 addUnwrappedLine(); 335 nextToken(); 336 parseCSharpGenericTypeConstraint(); 337 break; 338 } 339 nextToken(); 340 break; 341 } 342 } while (!eof()); 343 } 344 345 void UnwrappedLineParser::parseCSharpAttribute() { 346 int UnpairedSquareBrackets = 1; 347 do { 348 switch (FormatTok->Tok.getKind()) { 349 case tok::r_square: 350 nextToken(); 351 --UnpairedSquareBrackets; 352 if (UnpairedSquareBrackets == 0) { 353 addUnwrappedLine(); 354 return; 355 } 356 break; 357 case tok::l_square: 358 ++UnpairedSquareBrackets; 359 nextToken(); 360 break; 361 default: 362 nextToken(); 363 break; 364 } 365 } while (!eof()); 366 } 367 368 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { 369 bool SwitchLabelEncountered = false; 370 do { 371 tok::TokenKind kind = FormatTok->Tok.getKind(); 372 if (FormatTok->getType() == TT_MacroBlockBegin) { 373 kind = tok::l_brace; 374 } else if (FormatTok->getType() == TT_MacroBlockEnd) { 375 kind = tok::r_brace; 376 } 377 378 switch (kind) { 379 case tok::comment: 380 nextToken(); 381 addUnwrappedLine(); 382 break; 383 case tok::l_brace: 384 // FIXME: Add parameter whether this can happen - if this happens, we must 385 // be in a non-declaration context. 386 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList()) 387 continue; 388 parseBlock(); 389 addUnwrappedLine(); 390 break; 391 case tok::r_brace: 392 if (HasOpeningBrace) 393 return; 394 nextToken(); 395 addUnwrappedLine(); 396 break; 397 case tok::kw_default: { 398 unsigned StoredPosition = Tokens->getPosition(); 399 FormatToken *Next; 400 do { 401 Next = Tokens->getNextToken(); 402 } while (Next && Next->is(tok::comment)); 403 FormatTok = Tokens->setPosition(StoredPosition); 404 if (Next && Next->isNot(tok::colon)) { 405 // default not followed by ':' is not a case label; treat it like 406 // an identifier. 407 parseStructuralElement(); 408 break; 409 } 410 // Else, if it is 'default:', fall through to the case handling. 411 LLVM_FALLTHROUGH; 412 } 413 case tok::kw_case: 414 if (Style.Language == FormatStyle::LK_JavaScript && 415 Line->MustBeDeclaration) { 416 // A 'case: string' style field declaration. 417 parseStructuralElement(); 418 break; 419 } 420 if (!SwitchLabelEncountered && 421 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1))) 422 ++Line->Level; 423 SwitchLabelEncountered = true; 424 parseStructuralElement(); 425 break; 426 case tok::l_square: 427 if (Style.isCSharp()) { 428 nextToken(); 429 parseCSharpAttribute(); 430 break; 431 } 432 LLVM_FALLTHROUGH; 433 default: 434 parseStructuralElement(!HasOpeningBrace); 435 break; 436 } 437 } while (!eof()); 438 } 439 440 void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) { 441 // We'll parse forward through the tokens until we hit 442 // a closing brace or eof - note that getNextToken() will 443 // parse macros, so this will magically work inside macro 444 // definitions, too. 445 unsigned StoredPosition = Tokens->getPosition(); 446 FormatToken *Tok = FormatTok; 447 const FormatToken *PrevTok = Tok->Previous; 448 // Keep a stack of positions of lbrace tokens. We will 449 // update information about whether an lbrace starts a 450 // braced init list or a different block during the loop. 451 SmallVector<FormatToken *, 8> LBraceStack; 452 assert(Tok->Tok.is(tok::l_brace)); 453 do { 454 // Get next non-comment token. 455 FormatToken *NextTok; 456 unsigned ReadTokens = 0; 457 do { 458 NextTok = Tokens->getNextToken(); 459 ++ReadTokens; 460 } while (NextTok->is(tok::comment)); 461 462 switch (Tok->Tok.getKind()) { 463 case tok::l_brace: 464 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) { 465 if (PrevTok->isOneOf(tok::colon, tok::less)) 466 // A ':' indicates this code is in a type, or a braced list 467 // following a label in an object literal ({a: {b: 1}}). 468 // A '<' could be an object used in a comparison, but that is nonsense 469 // code (can never return true), so more likely it is a generic type 470 // argument (`X<{a: string; b: number}>`). 471 // The code below could be confused by semicolons between the 472 // individual members in a type member list, which would normally 473 // trigger BK_Block. In both cases, this must be parsed as an inline 474 // braced init. 475 Tok->setBlockKind(BK_BracedInit); 476 else if (PrevTok->is(tok::r_paren)) 477 // `) { }` can only occur in function or method declarations in JS. 478 Tok->setBlockKind(BK_Block); 479 } else { 480 Tok->setBlockKind(BK_Unknown); 481 } 482 LBraceStack.push_back(Tok); 483 break; 484 case tok::r_brace: 485 if (LBraceStack.empty()) 486 break; 487 if (LBraceStack.back()->is(BK_Unknown)) { 488 bool ProbablyBracedList = false; 489 if (Style.Language == FormatStyle::LK_Proto) { 490 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square); 491 } else { 492 // Skip NextTok over preprocessor lines, otherwise we may not 493 // properly diagnose the block as a braced intializer 494 // if the comma separator appears after the pp directive. 495 while (NextTok->is(tok::hash)) { 496 ScopedMacroState MacroState(*Line, Tokens, NextTok); 497 do { 498 NextTok = Tokens->getNextToken(); 499 ++ReadTokens; 500 } while (NextTok->isNot(tok::eof)); 501 } 502 503 // Using OriginalColumn to distinguish between ObjC methods and 504 // binary operators is a bit hacky. 505 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) && 506 NextTok->OriginalColumn == 0; 507 508 // If there is a comma, semicolon or right paren after the closing 509 // brace, we assume this is a braced initializer list. Note that 510 // regardless how we mark inner braces here, we will overwrite the 511 // BlockKind later if we parse a braced list (where all blocks 512 // inside are by default braced lists), or when we explicitly detect 513 // blocks (for example while parsing lambdas). 514 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a 515 // braced list in JS. 516 ProbablyBracedList = 517 (Style.Language == FormatStyle::LK_JavaScript && 518 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in, 519 Keywords.kw_as)) || 520 (Style.isCpp() && NextTok->is(tok::l_paren)) || 521 NextTok->isOneOf(tok::comma, tok::period, tok::colon, 522 tok::r_paren, tok::r_square, tok::l_brace, 523 tok::ellipsis) || 524 (NextTok->is(tok::identifier) && 525 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) || 526 (NextTok->is(tok::semi) && 527 (!ExpectClassBody || LBraceStack.size() != 1)) || 528 (NextTok->isBinaryOperator() && !NextIsObjCMethod); 529 if (!Style.isCSharp() && NextTok->is(tok::l_square)) { 530 // We can have an array subscript after a braced init 531 // list, but C++11 attributes are expected after blocks. 532 NextTok = Tokens->getNextToken(); 533 ++ReadTokens; 534 ProbablyBracedList = NextTok->isNot(tok::l_square); 535 } 536 } 537 if (ProbablyBracedList) { 538 Tok->setBlockKind(BK_BracedInit); 539 LBraceStack.back()->setBlockKind(BK_BracedInit); 540 } else { 541 Tok->setBlockKind(BK_Block); 542 LBraceStack.back()->setBlockKind(BK_Block); 543 } 544 } 545 LBraceStack.pop_back(); 546 break; 547 case tok::identifier: 548 if (!Tok->is(TT_StatementMacro)) 549 break; 550 LLVM_FALLTHROUGH; 551 case tok::at: 552 case tok::semi: 553 case tok::kw_if: 554 case tok::kw_while: 555 case tok::kw_for: 556 case tok::kw_switch: 557 case tok::kw_try: 558 case tok::kw___try: 559 if (!LBraceStack.empty() && LBraceStack.back()->is(BK_Unknown)) 560 LBraceStack.back()->setBlockKind(BK_Block); 561 break; 562 default: 563 break; 564 } 565 PrevTok = Tok; 566 Tok = NextTok; 567 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty()); 568 569 // Assume other blocks for all unclosed opening braces. 570 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { 571 if (LBraceStack[i]->is(BK_Unknown)) 572 LBraceStack[i]->setBlockKind(BK_Block); 573 } 574 575 FormatTok = Tokens->setPosition(StoredPosition); 576 } 577 578 template <class T> 579 static inline void hash_combine(std::size_t &seed, const T &v) { 580 std::hash<T> hasher; 581 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); 582 } 583 584 size_t UnwrappedLineParser::computePPHash() const { 585 size_t h = 0; 586 for (const auto &i : PPStack) { 587 hash_combine(h, size_t(i.Kind)); 588 hash_combine(h, i.Line); 589 } 590 return h; 591 } 592 593 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels, 594 bool MunchSemi, 595 bool UnindentWhitesmithsBraces) { 596 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) && 597 "'{' or macro block token expected"); 598 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin); 599 FormatTok->setBlockKind(BK_Block); 600 601 // For Whitesmiths mode, jump to the next level prior to skipping over the 602 // braces. 603 if (AddLevels > 0 && Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) 604 ++Line->Level; 605 606 size_t PPStartHash = computePPHash(); 607 608 unsigned InitialLevel = Line->Level; 609 nextToken(/*LevelDifference=*/AddLevels); 610 611 if (MacroBlock && FormatTok->is(tok::l_paren)) 612 parseParens(); 613 614 size_t NbPreprocessorDirectives = 615 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0; 616 addUnwrappedLine(); 617 size_t OpeningLineIndex = 618 CurrentLines->empty() 619 ? (UnwrappedLine::kInvalidIndex) 620 : (CurrentLines->size() - 1 - NbPreprocessorDirectives); 621 622 // Whitesmiths is weird here. The brace needs to be indented for the namespace 623 // block, but the block itself may not be indented depending on the style 624 // settings. This allows the format to back up one level in those cases. 625 if (UnindentWhitesmithsBraces) 626 --Line->Level; 627 628 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 629 MustBeDeclaration); 630 if (AddLevels > 0u && Style.BreakBeforeBraces != FormatStyle::BS_Whitesmiths) 631 Line->Level += AddLevels; 632 parseLevel(/*HasOpeningBrace=*/true); 633 634 if (eof()) 635 return; 636 637 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd) 638 : !FormatTok->is(tok::r_brace)) { 639 Line->Level = InitialLevel; 640 FormatTok->setBlockKind(BK_Block); 641 return; 642 } 643 644 size_t PPEndHash = computePPHash(); 645 646 // Munch the closing brace. 647 nextToken(/*LevelDifference=*/-AddLevels); 648 649 if (MacroBlock && FormatTok->is(tok::l_paren)) 650 parseParens(); 651 652 if (FormatTok->is(tok::arrow)) { 653 // Following the } we can find a trailing return type arrow 654 // as part of an implicit conversion constraint. 655 nextToken(); 656 parseStructuralElement(); 657 } 658 659 if (MunchSemi && FormatTok->Tok.is(tok::semi)) 660 nextToken(); 661 662 Line->Level = InitialLevel; 663 664 if (PPStartHash == PPEndHash) { 665 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex; 666 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) { 667 // Update the opening line to add the forward reference as well 668 (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex = 669 CurrentLines->size() - 1; 670 } 671 } 672 } 673 674 static bool isGoogScope(const UnwrappedLine &Line) { 675 // FIXME: Closure-library specific stuff should not be hard-coded but be 676 // configurable. 677 if (Line.Tokens.size() < 4) 678 return false; 679 auto I = Line.Tokens.begin(); 680 if (I->Tok->TokenText != "goog") 681 return false; 682 ++I; 683 if (I->Tok->isNot(tok::period)) 684 return false; 685 ++I; 686 if (I->Tok->TokenText != "scope") 687 return false; 688 ++I; 689 return I->Tok->is(tok::l_paren); 690 } 691 692 static bool isIIFE(const UnwrappedLine &Line, 693 const AdditionalKeywords &Keywords) { 694 // Look for the start of an immediately invoked anonymous function. 695 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression 696 // This is commonly done in JavaScript to create a new, anonymous scope. 697 // Example: (function() { ... })() 698 if (Line.Tokens.size() < 3) 699 return false; 700 auto I = Line.Tokens.begin(); 701 if (I->Tok->isNot(tok::l_paren)) 702 return false; 703 ++I; 704 if (I->Tok->isNot(Keywords.kw_function)) 705 return false; 706 ++I; 707 return I->Tok->is(tok::l_paren); 708 } 709 710 static bool ShouldBreakBeforeBrace(const FormatStyle &Style, 711 const FormatToken &InitialToken) { 712 if (InitialToken.isOneOf(tok::kw_namespace, TT_NamespaceMacro)) 713 return Style.BraceWrapping.AfterNamespace; 714 if (InitialToken.is(tok::kw_class)) 715 return Style.BraceWrapping.AfterClass; 716 if (InitialToken.is(tok::kw_union)) 717 return Style.BraceWrapping.AfterUnion; 718 if (InitialToken.is(tok::kw_struct)) 719 return Style.BraceWrapping.AfterStruct; 720 return false; 721 } 722 723 void UnwrappedLineParser::parseChildBlock() { 724 FormatTok->setBlockKind(BK_Block); 725 nextToken(); 726 { 727 bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript && 728 (isGoogScope(*Line) || isIIFE(*Line, Keywords))); 729 ScopedLineState LineState(*this); 730 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, 731 /*MustBeDeclaration=*/false); 732 Line->Level += SkipIndent ? 0 : 1; 733 parseLevel(/*HasOpeningBrace=*/true); 734 flushComments(isOnNewLine(*FormatTok)); 735 Line->Level -= SkipIndent ? 0 : 1; 736 } 737 nextToken(); 738 } 739 740 void UnwrappedLineParser::parsePPDirective() { 741 assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); 742 ScopedMacroState MacroState(*Line, Tokens, FormatTok); 743 744 nextToken(); 745 746 if (!FormatTok->Tok.getIdentifierInfo()) { 747 parsePPUnknown(); 748 return; 749 } 750 751 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { 752 case tok::pp_define: 753 parsePPDefine(); 754 return; 755 case tok::pp_if: 756 parsePPIf(/*IfDef=*/false); 757 break; 758 case tok::pp_ifdef: 759 case tok::pp_ifndef: 760 parsePPIf(/*IfDef=*/true); 761 break; 762 case tok::pp_else: 763 parsePPElse(); 764 break; 765 case tok::pp_elifdef: 766 case tok::pp_elifndef: 767 case tok::pp_elif: 768 parsePPElIf(); 769 break; 770 case tok::pp_endif: 771 parsePPEndIf(); 772 break; 773 default: 774 parsePPUnknown(); 775 break; 776 } 777 } 778 779 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) { 780 size_t Line = CurrentLines->size(); 781 if (CurrentLines == &PreprocessorDirectives) 782 Line += Lines.size(); 783 784 if (Unreachable || 785 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable)) 786 PPStack.push_back({PP_Unreachable, Line}); 787 else 788 PPStack.push_back({PP_Conditional, Line}); 789 } 790 791 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) { 792 ++PPBranchLevel; 793 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); 794 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { 795 PPLevelBranchIndex.push_back(0); 796 PPLevelBranchCount.push_back(0); 797 } 798 PPChainBranchIndex.push(0); 799 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0; 800 conditionalCompilationCondition(Unreachable || Skip); 801 } 802 803 void UnwrappedLineParser::conditionalCompilationAlternative() { 804 if (!PPStack.empty()) 805 PPStack.pop_back(); 806 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 807 if (!PPChainBranchIndex.empty()) 808 ++PPChainBranchIndex.top(); 809 conditionalCompilationCondition( 810 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && 811 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()); 812 } 813 814 void UnwrappedLineParser::conditionalCompilationEnd() { 815 assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); 816 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { 817 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { 818 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; 819 } 820 } 821 // Guard against #endif's without #if. 822 if (PPBranchLevel > -1) 823 --PPBranchLevel; 824 if (!PPChainBranchIndex.empty()) 825 PPChainBranchIndex.pop(); 826 if (!PPStack.empty()) 827 PPStack.pop_back(); 828 } 829 830 void UnwrappedLineParser::parsePPIf(bool IfDef) { 831 bool IfNDef = FormatTok->is(tok::pp_ifndef); 832 nextToken(); 833 bool Unreachable = false; 834 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0")) 835 Unreachable = true; 836 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG") 837 Unreachable = true; 838 conditionalCompilationStart(Unreachable); 839 FormatToken *IfCondition = FormatTok; 840 // If there's a #ifndef on the first line, and the only lines before it are 841 // comments, it could be an include guard. 842 bool MaybeIncludeGuard = IfNDef; 843 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) 844 for (auto &Line : Lines) { 845 if (!Line.Tokens.front().Tok->is(tok::comment)) { 846 MaybeIncludeGuard = false; 847 IncludeGuard = IG_Rejected; 848 break; 849 } 850 } 851 --PPBranchLevel; 852 parsePPUnknown(); 853 ++PPBranchLevel; 854 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) { 855 IncludeGuard = IG_IfNdefed; 856 IncludeGuardToken = IfCondition; 857 } 858 } 859 860 void UnwrappedLineParser::parsePPElse() { 861 // If a potential include guard has an #else, it's not an include guard. 862 if (IncludeGuard == IG_Defined && PPBranchLevel == 0) 863 IncludeGuard = IG_Rejected; 864 conditionalCompilationAlternative(); 865 if (PPBranchLevel > -1) 866 --PPBranchLevel; 867 parsePPUnknown(); 868 ++PPBranchLevel; 869 } 870 871 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } 872 873 void UnwrappedLineParser::parsePPEndIf() { 874 conditionalCompilationEnd(); 875 parsePPUnknown(); 876 // If the #endif of a potential include guard is the last thing in the file, 877 // then we found an include guard. 878 unsigned TokenPosition = Tokens->getPosition(); 879 FormatToken *PeekNext = AllTokens[TokenPosition]; 880 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 && 881 PeekNext->is(tok::eof) && 882 Style.IndentPPDirectives != FormatStyle::PPDIS_None) 883 IncludeGuard = IG_Found; 884 } 885 886 void UnwrappedLineParser::parsePPDefine() { 887 nextToken(); 888 889 if (!FormatTok->Tok.getIdentifierInfo()) { 890 IncludeGuard = IG_Rejected; 891 IncludeGuardToken = nullptr; 892 parsePPUnknown(); 893 return; 894 } 895 896 if (IncludeGuard == IG_IfNdefed && 897 IncludeGuardToken->TokenText == FormatTok->TokenText) { 898 IncludeGuard = IG_Defined; 899 IncludeGuardToken = nullptr; 900 for (auto &Line : Lines) { 901 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) { 902 IncludeGuard = IG_Rejected; 903 break; 904 } 905 } 906 } 907 908 nextToken(); 909 if (FormatTok->Tok.getKind() == tok::l_paren && 910 FormatTok->WhitespaceRange.getBegin() == 911 FormatTok->WhitespaceRange.getEnd()) { 912 parseParens(); 913 } 914 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None) 915 Line->Level += PPBranchLevel + 1; 916 addUnwrappedLine(); 917 ++Line->Level; 918 919 // Errors during a preprocessor directive can only affect the layout of the 920 // preprocessor directive, and thus we ignore them. An alternative approach 921 // would be to use the same approach we use on the file level (no 922 // re-indentation if there was a structural error) within the macro 923 // definition. 924 parseFile(); 925 } 926 927 void UnwrappedLineParser::parsePPUnknown() { 928 do { 929 nextToken(); 930 } while (!eof()); 931 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None) 932 Line->Level += PPBranchLevel + 1; 933 addUnwrappedLine(); 934 } 935 936 // Here we exclude certain tokens that are not usually the first token in an 937 // unwrapped line. This is used in attempt to distinguish macro calls without 938 // trailing semicolons from other constructs split to several lines. 939 static bool tokenCanStartNewLine(const FormatToken &Tok) { 940 // Semicolon can be a null-statement, l_square can be a start of a macro or 941 // a C++11 attribute, but this doesn't seem to be common. 942 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && 943 Tok.isNot(TT_AttributeSquare) && 944 // Tokens that can only be used as binary operators and a part of 945 // overloaded operator names. 946 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && 947 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && 948 Tok.isNot(tok::less) && Tok.isNot(tok::greater) && 949 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && 950 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && 951 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && 952 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && 953 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && 954 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && 955 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && 956 Tok.isNot(tok::lesslessequal) && 957 // Colon is used in labels, base class lists, initializer lists, 958 // range-based for loops, ternary operator, but should never be the 959 // first token in an unwrapped line. 960 Tok.isNot(tok::colon) && 961 // 'noexcept' is a trailing annotation. 962 Tok.isNot(tok::kw_noexcept); 963 } 964 965 static bool mustBeJSIdent(const AdditionalKeywords &Keywords, 966 const FormatToken *FormatTok) { 967 // FIXME: This returns true for C/C++ keywords like 'struct'. 968 return FormatTok->is(tok::identifier) && 969 (FormatTok->Tok.getIdentifierInfo() == nullptr || 970 !FormatTok->isOneOf( 971 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async, 972 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally, 973 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is, 974 Keywords.kw_let, Keywords.kw_var, tok::kw_const, 975 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements, 976 Keywords.kw_instanceof, Keywords.kw_interface, 977 Keywords.kw_override, Keywords.kw_throws, Keywords.kw_from)); 978 } 979 980 static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords, 981 const FormatToken *FormatTok) { 982 return FormatTok->Tok.isLiteral() || 983 FormatTok->isOneOf(tok::kw_true, tok::kw_false) || 984 mustBeJSIdent(Keywords, FormatTok); 985 } 986 987 // isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement 988 // when encountered after a value (see mustBeJSIdentOrValue). 989 static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords, 990 const FormatToken *FormatTok) { 991 return FormatTok->isOneOf( 992 tok::kw_return, Keywords.kw_yield, 993 // conditionals 994 tok::kw_if, tok::kw_else, 995 // loops 996 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break, 997 // switch/case 998 tok::kw_switch, tok::kw_case, 999 // exceptions 1000 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally, 1001 // declaration 1002 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let, 1003 Keywords.kw_async, Keywords.kw_function, 1004 // import/export 1005 Keywords.kw_import, tok::kw_export); 1006 } 1007 1008 // Checks whether a token is a type in K&R C (aka C78). 1009 static bool isC78Type(const FormatToken &Tok) { 1010 return Tok.isOneOf(tok::kw_char, tok::kw_short, tok::kw_int, tok::kw_long, 1011 tok::kw_unsigned, tok::kw_float, tok::kw_double, 1012 tok::identifier); 1013 } 1014 1015 // This function checks whether a token starts the first parameter declaration 1016 // in a K&R C (aka C78) function definition, e.g.: 1017 // int f(a, b) 1018 // short a, b; 1019 // { 1020 // return a + b; 1021 // } 1022 static bool isC78ParameterDecl(const FormatToken *Tok, const FormatToken *Next, 1023 const FormatToken *FuncName) { 1024 assert(Tok); 1025 assert(Next); 1026 assert(FuncName); 1027 1028 if (FuncName->isNot(tok::identifier)) 1029 return false; 1030 1031 const FormatToken *Prev = FuncName->Previous; 1032 if (!Prev || (Prev->isNot(tok::star) && !isC78Type(*Prev))) 1033 return false; 1034 1035 if (!isC78Type(*Tok) && 1036 !Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union)) 1037 return false; 1038 1039 if (Next->isNot(tok::star) && !Next->Tok.getIdentifierInfo()) 1040 return false; 1041 1042 Tok = Tok->Previous; 1043 if (!Tok || Tok->isNot(tok::r_paren)) 1044 return false; 1045 1046 Tok = Tok->Previous; 1047 if (!Tok || Tok->isNot(tok::identifier)) 1048 return false; 1049 1050 return Tok->Previous && Tok->Previous->isOneOf(tok::l_paren, tok::comma); 1051 } 1052 1053 // readTokenWithJavaScriptASI reads the next token and terminates the current 1054 // line if JavaScript Automatic Semicolon Insertion must 1055 // happen between the current token and the next token. 1056 // 1057 // This method is conservative - it cannot cover all edge cases of JavaScript, 1058 // but only aims to correctly handle certain well known cases. It *must not* 1059 // return true in speculative cases. 1060 void UnwrappedLineParser::readTokenWithJavaScriptASI() { 1061 FormatToken *Previous = FormatTok; 1062 readToken(); 1063 FormatToken *Next = FormatTok; 1064 1065 bool IsOnSameLine = 1066 CommentsBeforeNextToken.empty() 1067 ? Next->NewlinesBefore == 0 1068 : CommentsBeforeNextToken.front()->NewlinesBefore == 0; 1069 if (IsOnSameLine) 1070 return; 1071 1072 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous); 1073 bool PreviousStartsTemplateExpr = 1074 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${"); 1075 if (PreviousMustBeValue || Previous->is(tok::r_paren)) { 1076 // If the line contains an '@' sign, the previous token might be an 1077 // annotation, which can precede another identifier/value. 1078 bool HasAt = llvm::any_of(Line->Tokens, [](UnwrappedLineNode &LineNode) { 1079 return LineNode.Tok->is(tok::at); 1080 }); 1081 if (HasAt) 1082 return; 1083 } 1084 if (Next->is(tok::exclaim) && PreviousMustBeValue) 1085 return addUnwrappedLine(); 1086 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next); 1087 bool NextEndsTemplateExpr = 1088 Next->is(TT_TemplateString) && Next->TokenText.startswith("}"); 1089 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr && 1090 (PreviousMustBeValue || 1091 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus, 1092 tok::minusminus))) 1093 return addUnwrappedLine(); 1094 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) && 1095 isJSDeclOrStmt(Keywords, Next)) 1096 return addUnwrappedLine(); 1097 } 1098 1099 void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel) { 1100 assert(!FormatTok->is(tok::l_brace)); 1101 if (Style.Language == FormatStyle::LK_TableGen && 1102 FormatTok->is(tok::pp_include)) { 1103 nextToken(); 1104 if (FormatTok->is(tok::string_literal)) 1105 nextToken(); 1106 addUnwrappedLine(); 1107 return; 1108 } 1109 switch (FormatTok->Tok.getKind()) { 1110 case tok::kw_asm: 1111 nextToken(); 1112 if (FormatTok->is(tok::l_brace)) { 1113 FormatTok->setType(TT_InlineASMBrace); 1114 nextToken(); 1115 while (FormatTok && FormatTok->isNot(tok::eof)) { 1116 if (FormatTok->is(tok::r_brace)) { 1117 FormatTok->setType(TT_InlineASMBrace); 1118 nextToken(); 1119 addUnwrappedLine(); 1120 break; 1121 } 1122 FormatTok->Finalized = true; 1123 nextToken(); 1124 } 1125 } 1126 break; 1127 case tok::kw_namespace: 1128 parseNamespace(); 1129 return; 1130 case tok::kw_public: 1131 case tok::kw_protected: 1132 case tok::kw_private: 1133 if (Style.Language == FormatStyle::LK_Java || 1134 Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) 1135 nextToken(); 1136 else 1137 parseAccessSpecifier(); 1138 return; 1139 case tok::kw_if: 1140 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1141 // field/method declaration. 1142 break; 1143 parseIfThenElse(); 1144 return; 1145 case tok::kw_for: 1146 case tok::kw_while: 1147 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1148 // field/method declaration. 1149 break; 1150 parseForOrWhileLoop(); 1151 return; 1152 case tok::kw_do: 1153 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1154 // field/method declaration. 1155 break; 1156 parseDoWhile(); 1157 return; 1158 case tok::kw_switch: 1159 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1160 // 'switch: string' field declaration. 1161 break; 1162 parseSwitch(); 1163 return; 1164 case tok::kw_default: 1165 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1166 // 'default: string' field declaration. 1167 break; 1168 nextToken(); 1169 if (FormatTok->is(tok::colon)) { 1170 parseLabel(); 1171 return; 1172 } 1173 // e.g. "default void f() {}" in a Java interface. 1174 break; 1175 case tok::kw_case: 1176 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1177 // 'case: string' field declaration. 1178 break; 1179 parseCaseLabel(); 1180 return; 1181 case tok::kw_try: 1182 case tok::kw___try: 1183 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) 1184 // field/method declaration. 1185 break; 1186 parseTryCatch(); 1187 return; 1188 case tok::kw_extern: 1189 nextToken(); 1190 if (FormatTok->Tok.is(tok::string_literal)) { 1191 nextToken(); 1192 if (FormatTok->Tok.is(tok::l_brace)) { 1193 if (!Style.IndentExternBlock) { 1194 if (Style.BraceWrapping.AfterExternBlock) { 1195 addUnwrappedLine(); 1196 } 1197 unsigned AddLevels = Style.BraceWrapping.AfterExternBlock ? 1u : 0u; 1198 parseBlock(/*MustBeDeclaration=*/true, AddLevels); 1199 } else { 1200 unsigned AddLevels = 1201 Style.IndentExternBlock == FormatStyle::IEBS_Indent ? 1u : 0u; 1202 parseBlock(/*MustBeDeclaration=*/true, AddLevels); 1203 } 1204 addUnwrappedLine(); 1205 return; 1206 } 1207 } 1208 break; 1209 case tok::kw_export: 1210 if (Style.Language == FormatStyle::LK_JavaScript) { 1211 parseJavaScriptEs6ImportExport(); 1212 return; 1213 } 1214 if (!Style.isCpp()) 1215 break; 1216 // Handle C++ "(inline|export) namespace". 1217 LLVM_FALLTHROUGH; 1218 case tok::kw_inline: 1219 nextToken(); 1220 if (FormatTok->Tok.is(tok::kw_namespace)) { 1221 parseNamespace(); 1222 return; 1223 } 1224 break; 1225 case tok::identifier: 1226 if (FormatTok->is(TT_ForEachMacro)) { 1227 parseForOrWhileLoop(); 1228 return; 1229 } 1230 if (FormatTok->is(TT_MacroBlockBegin)) { 1231 parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, 1232 /*MunchSemi=*/false); 1233 return; 1234 } 1235 if (FormatTok->is(Keywords.kw_import)) { 1236 if (Style.Language == FormatStyle::LK_JavaScript) { 1237 parseJavaScriptEs6ImportExport(); 1238 return; 1239 } 1240 if (Style.Language == FormatStyle::LK_Proto) { 1241 nextToken(); 1242 if (FormatTok->is(tok::kw_public)) 1243 nextToken(); 1244 if (!FormatTok->is(tok::string_literal)) 1245 return; 1246 nextToken(); 1247 if (FormatTok->is(tok::semi)) 1248 nextToken(); 1249 addUnwrappedLine(); 1250 return; 1251 } 1252 } 1253 if (Style.isCpp() && 1254 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals, 1255 Keywords.kw_slots, Keywords.kw_qslots)) { 1256 nextToken(); 1257 if (FormatTok->is(tok::colon)) { 1258 nextToken(); 1259 addUnwrappedLine(); 1260 return; 1261 } 1262 } 1263 if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) { 1264 parseStatementMacro(); 1265 return; 1266 } 1267 if (Style.isCpp() && FormatTok->is(TT_NamespaceMacro)) { 1268 parseNamespace(); 1269 return; 1270 } 1271 // In all other cases, parse the declaration. 1272 break; 1273 default: 1274 break; 1275 } 1276 do { 1277 const FormatToken *Previous = FormatTok->Previous; 1278 switch (FormatTok->Tok.getKind()) { 1279 case tok::at: 1280 nextToken(); 1281 if (FormatTok->Tok.is(tok::l_brace)) { 1282 nextToken(); 1283 parseBracedList(); 1284 break; 1285 } else if (Style.Language == FormatStyle::LK_Java && 1286 FormatTok->is(Keywords.kw_interface)) { 1287 nextToken(); 1288 break; 1289 } 1290 switch (FormatTok->Tok.getObjCKeywordID()) { 1291 case tok::objc_public: 1292 case tok::objc_protected: 1293 case tok::objc_package: 1294 case tok::objc_private: 1295 return parseAccessSpecifier(); 1296 case tok::objc_interface: 1297 case tok::objc_implementation: 1298 return parseObjCInterfaceOrImplementation(); 1299 case tok::objc_protocol: 1300 if (parseObjCProtocol()) 1301 return; 1302 break; 1303 case tok::objc_end: 1304 return; // Handled by the caller. 1305 case tok::objc_optional: 1306 case tok::objc_required: 1307 nextToken(); 1308 addUnwrappedLine(); 1309 return; 1310 case tok::objc_autoreleasepool: 1311 nextToken(); 1312 if (FormatTok->Tok.is(tok::l_brace)) { 1313 if (Style.BraceWrapping.AfterControlStatement == 1314 FormatStyle::BWACS_Always) 1315 addUnwrappedLine(); 1316 parseBlock(); 1317 } 1318 addUnwrappedLine(); 1319 return; 1320 case tok::objc_synchronized: 1321 nextToken(); 1322 if (FormatTok->Tok.is(tok::l_paren)) 1323 // Skip synchronization object 1324 parseParens(); 1325 if (FormatTok->Tok.is(tok::l_brace)) { 1326 if (Style.BraceWrapping.AfterControlStatement == 1327 FormatStyle::BWACS_Always) 1328 addUnwrappedLine(); 1329 parseBlock(); 1330 } 1331 addUnwrappedLine(); 1332 return; 1333 case tok::objc_try: 1334 // This branch isn't strictly necessary (the kw_try case below would 1335 // do this too after the tok::at is parsed above). But be explicit. 1336 parseTryCatch(); 1337 return; 1338 default: 1339 break; 1340 } 1341 break; 1342 case tok::kw_concept: 1343 parseConcept(); 1344 break; 1345 case tok::kw_requires: 1346 parseRequires(); 1347 break; 1348 case tok::kw_enum: 1349 // Ignore if this is part of "template <enum ...". 1350 if (Previous && Previous->is(tok::less)) { 1351 nextToken(); 1352 break; 1353 } 1354 1355 // parseEnum falls through and does not yet add an unwrapped line as an 1356 // enum definition can start a structural element. 1357 if (!parseEnum()) 1358 break; 1359 // This only applies for C++. 1360 if (!Style.isCpp()) { 1361 addUnwrappedLine(); 1362 return; 1363 } 1364 break; 1365 case tok::kw_typedef: 1366 nextToken(); 1367 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS, 1368 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS, 1369 Keywords.kw_CF_CLOSED_ENUM, 1370 Keywords.kw_NS_CLOSED_ENUM)) 1371 parseEnum(); 1372 break; 1373 case tok::kw_struct: 1374 case tok::kw_union: 1375 case tok::kw_class: 1376 if (parseStructLike()) { 1377 return; 1378 } 1379 break; 1380 case tok::period: 1381 nextToken(); 1382 // In Java, classes have an implicit static member "class". 1383 if (Style.Language == FormatStyle::LK_Java && FormatTok && 1384 FormatTok->is(tok::kw_class)) 1385 nextToken(); 1386 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok && 1387 FormatTok->Tok.getIdentifierInfo()) 1388 // JavaScript only has pseudo keywords, all keywords are allowed to 1389 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6 1390 nextToken(); 1391 break; 1392 case tok::semi: 1393 nextToken(); 1394 addUnwrappedLine(); 1395 return; 1396 case tok::r_brace: 1397 addUnwrappedLine(); 1398 return; 1399 case tok::l_paren: { 1400 parseParens(); 1401 // Break the unwrapped line if a K&R C function definition has a parameter 1402 // declaration. 1403 if (!IsTopLevel || !Style.isCpp() || !Previous || FormatTok->is(tok::eof)) 1404 break; 1405 const unsigned Position = Tokens->getPosition() + 1; 1406 assert(Position < AllTokens.size()); 1407 if (isC78ParameterDecl(FormatTok, AllTokens[Position], Previous)) { 1408 addUnwrappedLine(); 1409 return; 1410 } 1411 break; 1412 } 1413 case tok::kw_operator: 1414 nextToken(); 1415 if (FormatTok->isBinaryOperator()) 1416 nextToken(); 1417 break; 1418 case tok::caret: 1419 nextToken(); 1420 if (FormatTok->Tok.isAnyIdentifier() || 1421 FormatTok->isSimpleTypeSpecifier()) 1422 nextToken(); 1423 if (FormatTok->is(tok::l_paren)) 1424 parseParens(); 1425 if (FormatTok->is(tok::l_brace)) 1426 parseChildBlock(); 1427 break; 1428 case tok::l_brace: 1429 if (!tryToParsePropertyAccessor() && !tryToParseBracedList()) { 1430 // A block outside of parentheses must be the last part of a 1431 // structural element. 1432 // FIXME: Figure out cases where this is not true, and add projections 1433 // for them (the one we know is missing are lambdas). 1434 if (Style.BraceWrapping.AfterFunction) 1435 addUnwrappedLine(); 1436 FormatTok->setType(TT_FunctionLBrace); 1437 parseBlock(); 1438 addUnwrappedLine(); 1439 return; 1440 } 1441 // Otherwise this was a braced init list, and the structural 1442 // element continues. 1443 break; 1444 case tok::kw_try: 1445 if (Style.Language == FormatStyle::LK_JavaScript && 1446 Line->MustBeDeclaration) { 1447 // field/method declaration. 1448 nextToken(); 1449 break; 1450 } 1451 // We arrive here when parsing function-try blocks. 1452 if (Style.BraceWrapping.AfterFunction) 1453 addUnwrappedLine(); 1454 parseTryCatch(); 1455 return; 1456 case tok::identifier: { 1457 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where) && 1458 Line->MustBeDeclaration) { 1459 addUnwrappedLine(); 1460 parseCSharpGenericTypeConstraint(); 1461 break; 1462 } 1463 if (FormatTok->is(TT_MacroBlockEnd)) { 1464 addUnwrappedLine(); 1465 return; 1466 } 1467 1468 // Function declarations (as opposed to function expressions) are parsed 1469 // on their own unwrapped line by continuing this loop. Function 1470 // expressions (functions that are not on their own line) must not create 1471 // a new unwrapped line, so they are special cased below. 1472 size_t TokenCount = Line->Tokens.size(); 1473 if (Style.Language == FormatStyle::LK_JavaScript && 1474 FormatTok->is(Keywords.kw_function) && 1475 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is( 1476 Keywords.kw_async)))) { 1477 tryToParseJSFunction(); 1478 break; 1479 } 1480 if ((Style.Language == FormatStyle::LK_JavaScript || 1481 Style.Language == FormatStyle::LK_Java) && 1482 FormatTok->is(Keywords.kw_interface)) { 1483 if (Style.Language == FormatStyle::LK_JavaScript) { 1484 // In JavaScript/TypeScript, "interface" can be used as a standalone 1485 // identifier, e.g. in `var interface = 1;`. If "interface" is 1486 // followed by another identifier, it is very like to be an actual 1487 // interface declaration. 1488 unsigned StoredPosition = Tokens->getPosition(); 1489 FormatToken *Next = Tokens->getNextToken(); 1490 FormatTok = Tokens->setPosition(StoredPosition); 1491 if (Next && !mustBeJSIdent(Keywords, Next)) { 1492 nextToken(); 1493 break; 1494 } 1495 } 1496 parseRecord(); 1497 addUnwrappedLine(); 1498 return; 1499 } 1500 1501 if (FormatTok->is(Keywords.kw_interface)) { 1502 if (parseStructLike()) { 1503 return; 1504 } 1505 break; 1506 } 1507 1508 if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) { 1509 parseStatementMacro(); 1510 return; 1511 } 1512 1513 // See if the following token should start a new unwrapped line. 1514 StringRef Text = FormatTok->TokenText; 1515 nextToken(); 1516 1517 // JS doesn't have macros, and within classes colons indicate fields, not 1518 // labels. 1519 if (Style.Language == FormatStyle::LK_JavaScript) 1520 break; 1521 1522 TokenCount = Line->Tokens.size(); 1523 if (TokenCount == 1 || 1524 (TokenCount == 2 && Line->Tokens.front().Tok->is(tok::comment))) { 1525 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) { 1526 Line->Tokens.begin()->Tok->MustBreakBefore = true; 1527 parseLabel(!Style.IndentGotoLabels); 1528 return; 1529 } 1530 // Recognize function-like macro usages without trailing semicolon as 1531 // well as free-standing macros like Q_OBJECT. 1532 bool FunctionLike = FormatTok->is(tok::l_paren); 1533 if (FunctionLike) 1534 parseParens(); 1535 1536 bool FollowedByNewline = 1537 CommentsBeforeNextToken.empty() 1538 ? FormatTok->NewlinesBefore > 0 1539 : CommentsBeforeNextToken.front()->NewlinesBefore > 0; 1540 1541 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) && 1542 tokenCanStartNewLine(*FormatTok) && Text == Text.upper()) { 1543 addUnwrappedLine(); 1544 return; 1545 } 1546 } 1547 break; 1548 } 1549 case tok::equal: 1550 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType 1551 // TT_FatArrow. They always start an expression or a child block if 1552 // followed by a curly brace. 1553 if (FormatTok->is(TT_FatArrow)) { 1554 nextToken(); 1555 if (FormatTok->is(tok::l_brace)) { 1556 // C# may break after => if the next character is a newline. 1557 if (Style.isCSharp() && Style.BraceWrapping.AfterFunction == true) { 1558 // calling `addUnwrappedLine()` here causes odd parsing errors. 1559 FormatTok->MustBreakBefore = true; 1560 } 1561 parseChildBlock(); 1562 } 1563 break; 1564 } 1565 1566 nextToken(); 1567 if (FormatTok->Tok.is(tok::l_brace)) { 1568 // Block kind should probably be set to BK_BracedInit for any language. 1569 // C# needs this change to ensure that array initialisers and object 1570 // initialisers are indented the same way. 1571 if (Style.isCSharp()) 1572 FormatTok->setBlockKind(BK_BracedInit); 1573 nextToken(); 1574 parseBracedList(); 1575 } else if (Style.Language == FormatStyle::LK_Proto && 1576 FormatTok->Tok.is(tok::less)) { 1577 nextToken(); 1578 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 1579 /*ClosingBraceKind=*/tok::greater); 1580 } 1581 break; 1582 case tok::l_square: 1583 parseSquare(); 1584 break; 1585 case tok::kw_new: 1586 parseNew(); 1587 break; 1588 default: 1589 nextToken(); 1590 break; 1591 } 1592 } while (!eof()); 1593 } 1594 1595 bool UnwrappedLineParser::tryToParsePropertyAccessor() { 1596 assert(FormatTok->is(tok::l_brace)); 1597 if (!Style.isCSharp()) 1598 return false; 1599 // See if it's a property accessor. 1600 if (FormatTok->Previous->isNot(tok::identifier)) 1601 return false; 1602 1603 // See if we are inside a property accessor. 1604 // 1605 // Record the current tokenPosition so that we can advance and 1606 // reset the current token. `Next` is not set yet so we need 1607 // another way to advance along the token stream. 1608 unsigned int StoredPosition = Tokens->getPosition(); 1609 FormatToken *Tok = Tokens->getNextToken(); 1610 1611 // A trivial property accessor is of the form: 1612 // { [ACCESS_SPECIFIER] [get]; [ACCESS_SPECIFIER] [set] } 1613 // Track these as they do not require line breaks to be introduced. 1614 bool HasGetOrSet = false; 1615 bool IsTrivialPropertyAccessor = true; 1616 while (!eof()) { 1617 if (Tok->isOneOf(tok::semi, tok::kw_public, tok::kw_private, 1618 tok::kw_protected, Keywords.kw_internal, Keywords.kw_get, 1619 Keywords.kw_set)) { 1620 if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_set)) 1621 HasGetOrSet = true; 1622 Tok = Tokens->getNextToken(); 1623 continue; 1624 } 1625 if (Tok->isNot(tok::r_brace)) 1626 IsTrivialPropertyAccessor = false; 1627 break; 1628 } 1629 1630 if (!HasGetOrSet) { 1631 Tokens->setPosition(StoredPosition); 1632 return false; 1633 } 1634 1635 // Try to parse the property accessor: 1636 // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties 1637 Tokens->setPosition(StoredPosition); 1638 if (!IsTrivialPropertyAccessor && Style.BraceWrapping.AfterFunction == true) 1639 addUnwrappedLine(); 1640 nextToken(); 1641 do { 1642 switch (FormatTok->Tok.getKind()) { 1643 case tok::r_brace: 1644 nextToken(); 1645 if (FormatTok->is(tok::equal)) { 1646 while (!eof() && FormatTok->isNot(tok::semi)) 1647 nextToken(); 1648 nextToken(); 1649 } 1650 addUnwrappedLine(); 1651 return true; 1652 case tok::l_brace: 1653 ++Line->Level; 1654 parseBlock(/*MustBeDeclaration=*/true); 1655 addUnwrappedLine(); 1656 --Line->Level; 1657 break; 1658 case tok::equal: 1659 if (FormatTok->is(TT_FatArrow)) { 1660 ++Line->Level; 1661 do { 1662 nextToken(); 1663 } while (!eof() && FormatTok->isNot(tok::semi)); 1664 nextToken(); 1665 addUnwrappedLine(); 1666 --Line->Level; 1667 break; 1668 } 1669 nextToken(); 1670 break; 1671 default: 1672 if (FormatTok->isOneOf(Keywords.kw_get, Keywords.kw_set) && 1673 !IsTrivialPropertyAccessor) { 1674 // Non-trivial get/set needs to be on its own line. 1675 addUnwrappedLine(); 1676 } 1677 nextToken(); 1678 } 1679 } while (!eof()); 1680 1681 // Unreachable for well-formed code (paired '{' and '}'). 1682 return true; 1683 } 1684 1685 bool UnwrappedLineParser::tryToParseLambda() { 1686 if (!Style.isCpp()) { 1687 nextToken(); 1688 return false; 1689 } 1690 assert(FormatTok->is(tok::l_square)); 1691 FormatToken &LSquare = *FormatTok; 1692 if (!tryToParseLambdaIntroducer()) 1693 return false; 1694 1695 bool SeenArrow = false; 1696 1697 while (FormatTok->isNot(tok::l_brace)) { 1698 if (FormatTok->isSimpleTypeSpecifier()) { 1699 nextToken(); 1700 continue; 1701 } 1702 switch (FormatTok->Tok.getKind()) { 1703 case tok::l_brace: 1704 break; 1705 case tok::l_paren: 1706 parseParens(); 1707 break; 1708 case tok::amp: 1709 case tok::star: 1710 case tok::kw_const: 1711 case tok::comma: 1712 case tok::less: 1713 case tok::greater: 1714 case tok::identifier: 1715 case tok::numeric_constant: 1716 case tok::coloncolon: 1717 case tok::kw_class: 1718 case tok::kw_mutable: 1719 case tok::kw_noexcept: 1720 case tok::kw_template: 1721 case tok::kw_typename: 1722 nextToken(); 1723 break; 1724 // Specialization of a template with an integer parameter can contain 1725 // arithmetic, logical, comparison and ternary operators. 1726 // 1727 // FIXME: This also accepts sequences of operators that are not in the scope 1728 // of a template argument list. 1729 // 1730 // In a C++ lambda a template type can only occur after an arrow. We use 1731 // this as an heuristic to distinguish between Objective-C expressions 1732 // followed by an `a->b` expression, such as: 1733 // ([obj func:arg] + a->b) 1734 // Otherwise the code below would parse as a lambda. 1735 // 1736 // FIXME: This heuristic is incorrect for C++20 generic lambdas with 1737 // explicit template lists: []<bool b = true && false>(U &&u){} 1738 case tok::plus: 1739 case tok::minus: 1740 case tok::exclaim: 1741 case tok::tilde: 1742 case tok::slash: 1743 case tok::percent: 1744 case tok::lessless: 1745 case tok::pipe: 1746 case tok::pipepipe: 1747 case tok::ampamp: 1748 case tok::caret: 1749 case tok::equalequal: 1750 case tok::exclaimequal: 1751 case tok::greaterequal: 1752 case tok::lessequal: 1753 case tok::question: 1754 case tok::colon: 1755 case tok::ellipsis: 1756 case tok::kw_true: 1757 case tok::kw_false: 1758 if (SeenArrow) { 1759 nextToken(); 1760 break; 1761 } 1762 return true; 1763 case tok::arrow: 1764 // This might or might not actually be a lambda arrow (this could be an 1765 // ObjC method invocation followed by a dereferencing arrow). We might 1766 // reset this back to TT_Unknown in TokenAnnotator. 1767 FormatTok->setType(TT_LambdaArrow); 1768 SeenArrow = true; 1769 nextToken(); 1770 break; 1771 default: 1772 return true; 1773 } 1774 } 1775 FormatTok->setType(TT_LambdaLBrace); 1776 LSquare.setType(TT_LambdaLSquare); 1777 parseChildBlock(); 1778 return true; 1779 } 1780 1781 bool UnwrappedLineParser::tryToParseLambdaIntroducer() { 1782 const FormatToken *Previous = FormatTok->Previous; 1783 if (Previous && 1784 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new, 1785 tok::kw_delete, tok::l_square) || 1786 FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() || 1787 Previous->isSimpleTypeSpecifier())) { 1788 nextToken(); 1789 return false; 1790 } 1791 nextToken(); 1792 if (FormatTok->is(tok::l_square)) { 1793 return false; 1794 } 1795 parseSquare(/*LambdaIntroducer=*/true); 1796 return true; 1797 } 1798 1799 void UnwrappedLineParser::tryToParseJSFunction() { 1800 assert(FormatTok->is(Keywords.kw_function) || 1801 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)); 1802 if (FormatTok->is(Keywords.kw_async)) 1803 nextToken(); 1804 // Consume "function". 1805 nextToken(); 1806 1807 // Consume * (generator function). Treat it like C++'s overloaded operators. 1808 if (FormatTok->is(tok::star)) { 1809 FormatTok->setType(TT_OverloadedOperator); 1810 nextToken(); 1811 } 1812 1813 // Consume function name. 1814 if (FormatTok->is(tok::identifier)) 1815 nextToken(); 1816 1817 if (FormatTok->isNot(tok::l_paren)) 1818 return; 1819 1820 // Parse formal parameter list. 1821 parseParens(); 1822 1823 if (FormatTok->is(tok::colon)) { 1824 // Parse a type definition. 1825 nextToken(); 1826 1827 // Eat the type declaration. For braced inline object types, balance braces, 1828 // otherwise just parse until finding an l_brace for the function body. 1829 if (FormatTok->is(tok::l_brace)) 1830 tryToParseBracedList(); 1831 else 1832 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof()) 1833 nextToken(); 1834 } 1835 1836 if (FormatTok->is(tok::semi)) 1837 return; 1838 1839 parseChildBlock(); 1840 } 1841 1842 bool UnwrappedLineParser::tryToParseBracedList() { 1843 if (FormatTok->is(BK_Unknown)) 1844 calculateBraceTypes(); 1845 assert(FormatTok->isNot(BK_Unknown)); 1846 if (FormatTok->is(BK_Block)) 1847 return false; 1848 nextToken(); 1849 parseBracedList(); 1850 return true; 1851 } 1852 1853 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons, 1854 bool IsEnum, 1855 tok::TokenKind ClosingBraceKind) { 1856 bool HasError = false; 1857 1858 // FIXME: Once we have an expression parser in the UnwrappedLineParser, 1859 // replace this by using parseAssignmentExpression() inside. 1860 do { 1861 if (Style.isCSharp()) { 1862 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType 1863 // TT_FatArrow. They always start an expression or a child block if 1864 // followed by a curly brace. 1865 if (FormatTok->is(TT_FatArrow)) { 1866 nextToken(); 1867 if (FormatTok->is(tok::l_brace)) { 1868 // C# may break after => if the next character is a newline. 1869 if (Style.isCSharp() && Style.BraceWrapping.AfterFunction == true) { 1870 // calling `addUnwrappedLine()` here causes odd parsing errors. 1871 FormatTok->MustBreakBefore = true; 1872 } 1873 parseChildBlock(); 1874 continue; 1875 } 1876 } 1877 } 1878 if (Style.Language == FormatStyle::LK_JavaScript) { 1879 if (FormatTok->is(Keywords.kw_function) || 1880 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) { 1881 tryToParseJSFunction(); 1882 continue; 1883 } 1884 if (FormatTok->is(TT_FatArrow)) { 1885 nextToken(); 1886 // Fat arrows can be followed by simple expressions or by child blocks 1887 // in curly braces. 1888 if (FormatTok->is(tok::l_brace)) { 1889 parseChildBlock(); 1890 continue; 1891 } 1892 } 1893 if (FormatTok->is(tok::l_brace)) { 1894 // Could be a method inside of a braced list `{a() { return 1; }}`. 1895 if (tryToParseBracedList()) 1896 continue; 1897 parseChildBlock(); 1898 } 1899 } 1900 if (FormatTok->Tok.getKind() == ClosingBraceKind) { 1901 if (IsEnum && !Style.AllowShortEnumsOnASingleLine) 1902 addUnwrappedLine(); 1903 nextToken(); 1904 return !HasError; 1905 } 1906 switch (FormatTok->Tok.getKind()) { 1907 case tok::caret: 1908 nextToken(); 1909 if (FormatTok->is(tok::l_brace)) { 1910 parseChildBlock(); 1911 } 1912 break; 1913 case tok::l_square: 1914 if (Style.isCSharp()) 1915 parseSquare(); 1916 else 1917 tryToParseLambda(); 1918 break; 1919 case tok::l_paren: 1920 parseParens(); 1921 // JavaScript can just have free standing methods and getters/setters in 1922 // object literals. Detect them by a "{" following ")". 1923 if (Style.Language == FormatStyle::LK_JavaScript) { 1924 if (FormatTok->is(tok::l_brace)) 1925 parseChildBlock(); 1926 break; 1927 } 1928 break; 1929 case tok::l_brace: 1930 // Assume there are no blocks inside a braced init list apart 1931 // from the ones we explicitly parse out (like lambdas). 1932 FormatTok->setBlockKind(BK_BracedInit); 1933 nextToken(); 1934 parseBracedList(); 1935 break; 1936 case tok::less: 1937 if (Style.Language == FormatStyle::LK_Proto) { 1938 nextToken(); 1939 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 1940 /*ClosingBraceKind=*/tok::greater); 1941 } else { 1942 nextToken(); 1943 } 1944 break; 1945 case tok::semi: 1946 // JavaScript (or more precisely TypeScript) can have semicolons in braced 1947 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be 1948 // used for error recovery if we have otherwise determined that this is 1949 // a braced list. 1950 if (Style.Language == FormatStyle::LK_JavaScript) { 1951 nextToken(); 1952 break; 1953 } 1954 HasError = true; 1955 if (!ContinueOnSemicolons) 1956 return !HasError; 1957 nextToken(); 1958 break; 1959 case tok::comma: 1960 nextToken(); 1961 if (IsEnum && !Style.AllowShortEnumsOnASingleLine) 1962 addUnwrappedLine(); 1963 break; 1964 default: 1965 nextToken(); 1966 break; 1967 } 1968 } while (!eof()); 1969 return false; 1970 } 1971 1972 void UnwrappedLineParser::parseParens() { 1973 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); 1974 nextToken(); 1975 do { 1976 switch (FormatTok->Tok.getKind()) { 1977 case tok::l_paren: 1978 parseParens(); 1979 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace)) 1980 parseChildBlock(); 1981 break; 1982 case tok::r_paren: 1983 nextToken(); 1984 return; 1985 case tok::r_brace: 1986 // A "}" inside parenthesis is an error if there wasn't a matching "{". 1987 return; 1988 case tok::l_square: 1989 tryToParseLambda(); 1990 break; 1991 case tok::l_brace: 1992 if (!tryToParseBracedList()) 1993 parseChildBlock(); 1994 break; 1995 case tok::at: 1996 nextToken(); 1997 if (FormatTok->Tok.is(tok::l_brace)) { 1998 nextToken(); 1999 parseBracedList(); 2000 } 2001 break; 2002 case tok::equal: 2003 if (Style.isCSharp() && FormatTok->is(TT_FatArrow)) 2004 parseStructuralElement(); 2005 else 2006 nextToken(); 2007 break; 2008 case tok::kw_class: 2009 if (Style.Language == FormatStyle::LK_JavaScript) 2010 parseRecord(/*ParseAsExpr=*/true); 2011 else 2012 nextToken(); 2013 break; 2014 case tok::identifier: 2015 if (Style.Language == FormatStyle::LK_JavaScript && 2016 (FormatTok->is(Keywords.kw_function) || 2017 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function))) 2018 tryToParseJSFunction(); 2019 else 2020 nextToken(); 2021 break; 2022 default: 2023 nextToken(); 2024 break; 2025 } 2026 } while (!eof()); 2027 } 2028 2029 void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) { 2030 if (!LambdaIntroducer) { 2031 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected."); 2032 if (tryToParseLambda()) 2033 return; 2034 } 2035 do { 2036 switch (FormatTok->Tok.getKind()) { 2037 case tok::l_paren: 2038 parseParens(); 2039 break; 2040 case tok::r_square: 2041 nextToken(); 2042 return; 2043 case tok::r_brace: 2044 // A "}" inside parenthesis is an error if there wasn't a matching "{". 2045 return; 2046 case tok::l_square: 2047 parseSquare(); 2048 break; 2049 case tok::l_brace: { 2050 if (!tryToParseBracedList()) 2051 parseChildBlock(); 2052 break; 2053 } 2054 case tok::at: 2055 nextToken(); 2056 if (FormatTok->Tok.is(tok::l_brace)) { 2057 nextToken(); 2058 parseBracedList(); 2059 } 2060 break; 2061 default: 2062 nextToken(); 2063 break; 2064 } 2065 } while (!eof()); 2066 } 2067 2068 void UnwrappedLineParser::parseIfThenElse() { 2069 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); 2070 nextToken(); 2071 if (FormatTok->Tok.isOneOf(tok::kw_constexpr, tok::identifier)) 2072 nextToken(); 2073 if (FormatTok->Tok.is(tok::l_paren)) 2074 parseParens(); 2075 // handle [[likely]] / [[unlikely]] 2076 if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute()) 2077 parseSquare(); 2078 bool NeedsUnwrappedLine = false; 2079 if (FormatTok->Tok.is(tok::l_brace)) { 2080 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2081 parseBlock(); 2082 if (Style.BraceWrapping.BeforeElse) 2083 addUnwrappedLine(); 2084 else 2085 NeedsUnwrappedLine = true; 2086 } else { 2087 addUnwrappedLine(); 2088 ++Line->Level; 2089 parseStructuralElement(); 2090 --Line->Level; 2091 } 2092 if (FormatTok->Tok.is(tok::kw_else)) { 2093 nextToken(); 2094 // handle [[likely]] / [[unlikely]] 2095 if (FormatTok->Tok.is(tok::l_square) && tryToParseSimpleAttribute()) 2096 parseSquare(); 2097 if (FormatTok->Tok.is(tok::l_brace)) { 2098 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2099 parseBlock(); 2100 addUnwrappedLine(); 2101 } else if (FormatTok->Tok.is(tok::kw_if)) { 2102 FormatToken *Previous = AllTokens[Tokens->getPosition() - 1]; 2103 bool PrecededByComment = Previous->is(tok::comment); 2104 if (PrecededByComment) { 2105 addUnwrappedLine(); 2106 ++Line->Level; 2107 } 2108 parseIfThenElse(); 2109 if (PrecededByComment) 2110 --Line->Level; 2111 } else { 2112 addUnwrappedLine(); 2113 ++Line->Level; 2114 parseStructuralElement(); 2115 if (FormatTok->is(tok::eof)) 2116 addUnwrappedLine(); 2117 --Line->Level; 2118 } 2119 } else if (NeedsUnwrappedLine) { 2120 addUnwrappedLine(); 2121 } 2122 } 2123 2124 void UnwrappedLineParser::parseTryCatch() { 2125 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected"); 2126 nextToken(); 2127 bool NeedsUnwrappedLine = false; 2128 if (FormatTok->is(tok::colon)) { 2129 // We are in a function try block, what comes is an initializer list. 2130 nextToken(); 2131 2132 // In case identifiers were removed by clang-tidy, what might follow is 2133 // multiple commas in sequence - before the first identifier. 2134 while (FormatTok->is(tok::comma)) 2135 nextToken(); 2136 2137 while (FormatTok->is(tok::identifier)) { 2138 nextToken(); 2139 if (FormatTok->is(tok::l_paren)) 2140 parseParens(); 2141 if (FormatTok->Previous && FormatTok->Previous->is(tok::identifier) && 2142 FormatTok->is(tok::l_brace)) { 2143 do { 2144 nextToken(); 2145 } while (!FormatTok->is(tok::r_brace)); 2146 nextToken(); 2147 } 2148 2149 // In case identifiers were removed by clang-tidy, what might follow is 2150 // multiple commas in sequence - after the first identifier. 2151 while (FormatTok->is(tok::comma)) 2152 nextToken(); 2153 } 2154 } 2155 // Parse try with resource. 2156 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) { 2157 parseParens(); 2158 } 2159 if (FormatTok->is(tok::l_brace)) { 2160 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2161 parseBlock(); 2162 if (Style.BraceWrapping.BeforeCatch) { 2163 addUnwrappedLine(); 2164 } else { 2165 NeedsUnwrappedLine = true; 2166 } 2167 } else if (!FormatTok->is(tok::kw_catch)) { 2168 // The C++ standard requires a compound-statement after a try. 2169 // If there's none, we try to assume there's a structuralElement 2170 // and try to continue. 2171 addUnwrappedLine(); 2172 ++Line->Level; 2173 parseStructuralElement(); 2174 --Line->Level; 2175 } 2176 while (1) { 2177 if (FormatTok->is(tok::at)) 2178 nextToken(); 2179 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, 2180 tok::kw___finally) || 2181 ((Style.Language == FormatStyle::LK_Java || 2182 Style.Language == FormatStyle::LK_JavaScript) && 2183 FormatTok->is(Keywords.kw_finally)) || 2184 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || 2185 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) 2186 break; 2187 nextToken(); 2188 while (FormatTok->isNot(tok::l_brace)) { 2189 if (FormatTok->is(tok::l_paren)) { 2190 parseParens(); 2191 continue; 2192 } 2193 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) 2194 return; 2195 nextToken(); 2196 } 2197 NeedsUnwrappedLine = false; 2198 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2199 parseBlock(); 2200 if (Style.BraceWrapping.BeforeCatch) 2201 addUnwrappedLine(); 2202 else 2203 NeedsUnwrappedLine = true; 2204 } 2205 if (NeedsUnwrappedLine) 2206 addUnwrappedLine(); 2207 } 2208 2209 void UnwrappedLineParser::parseNamespace() { 2210 assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) && 2211 "'namespace' expected"); 2212 2213 const FormatToken &InitialToken = *FormatTok; 2214 nextToken(); 2215 if (InitialToken.is(TT_NamespaceMacro)) { 2216 parseParens(); 2217 } else { 2218 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline, 2219 tok::l_square, tok::period)) { 2220 if (FormatTok->is(tok::l_square)) 2221 parseSquare(); 2222 else 2223 nextToken(); 2224 } 2225 } 2226 if (FormatTok->Tok.is(tok::l_brace)) { 2227 if (ShouldBreakBeforeBrace(Style, InitialToken)) 2228 addUnwrappedLine(); 2229 2230 unsigned AddLevels = 2231 Style.NamespaceIndentation == FormatStyle::NI_All || 2232 (Style.NamespaceIndentation == FormatStyle::NI_Inner && 2233 DeclarationScopeStack.size() > 1) 2234 ? 1u 2235 : 0u; 2236 bool ManageWhitesmithsBraces = 2237 AddLevels == 0u && 2238 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths; 2239 2240 // If we're in Whitesmiths mode, indent the brace if we're not indenting 2241 // the whole block. 2242 if (ManageWhitesmithsBraces) 2243 ++Line->Level; 2244 2245 parseBlock(/*MustBeDeclaration=*/true, AddLevels, 2246 /*MunchSemi=*/true, 2247 /*UnindentWhitesmithsBraces=*/ManageWhitesmithsBraces); 2248 2249 // Munch the semicolon after a namespace. This is more common than one would 2250 // think. Putting the semicolon into its own line is very ugly. 2251 if (FormatTok->Tok.is(tok::semi)) 2252 nextToken(); 2253 2254 addUnwrappedLine(AddLevels > 0 ? LineLevel::Remove : LineLevel::Keep); 2255 2256 if (ManageWhitesmithsBraces) 2257 --Line->Level; 2258 } 2259 // FIXME: Add error handling. 2260 } 2261 2262 void UnwrappedLineParser::parseNew() { 2263 assert(FormatTok->is(tok::kw_new) && "'new' expected"); 2264 nextToken(); 2265 2266 if (Style.isCSharp()) { 2267 do { 2268 if (FormatTok->is(tok::l_brace)) 2269 parseBracedList(); 2270 2271 if (FormatTok->isOneOf(tok::semi, tok::comma)) 2272 return; 2273 2274 nextToken(); 2275 } while (!eof()); 2276 } 2277 2278 if (Style.Language != FormatStyle::LK_Java) 2279 return; 2280 2281 // In Java, we can parse everything up to the parens, which aren't optional. 2282 do { 2283 // There should not be a ;, { or } before the new's open paren. 2284 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace)) 2285 return; 2286 2287 // Consume the parens. 2288 if (FormatTok->is(tok::l_paren)) { 2289 parseParens(); 2290 2291 // If there is a class body of an anonymous class, consume that as child. 2292 if (FormatTok->is(tok::l_brace)) 2293 parseChildBlock(); 2294 return; 2295 } 2296 nextToken(); 2297 } while (!eof()); 2298 } 2299 2300 void UnwrappedLineParser::parseForOrWhileLoop() { 2301 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) && 2302 "'for', 'while' or foreach macro expected"); 2303 nextToken(); 2304 // JS' for await ( ... 2305 if (Style.Language == FormatStyle::LK_JavaScript && 2306 FormatTok->is(Keywords.kw_await)) 2307 nextToken(); 2308 if (FormatTok->Tok.is(tok::l_paren)) 2309 parseParens(); 2310 if (FormatTok->Tok.is(tok::l_brace)) { 2311 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2312 parseBlock(); 2313 addUnwrappedLine(); 2314 } else { 2315 addUnwrappedLine(); 2316 ++Line->Level; 2317 parseStructuralElement(); 2318 --Line->Level; 2319 } 2320 } 2321 2322 void UnwrappedLineParser::parseDoWhile() { 2323 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); 2324 nextToken(); 2325 if (FormatTok->Tok.is(tok::l_brace)) { 2326 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2327 parseBlock(); 2328 if (Style.BraceWrapping.BeforeWhile) 2329 addUnwrappedLine(); 2330 } else { 2331 addUnwrappedLine(); 2332 ++Line->Level; 2333 parseStructuralElement(); 2334 --Line->Level; 2335 } 2336 2337 // FIXME: Add error handling. 2338 if (!FormatTok->Tok.is(tok::kw_while)) { 2339 addUnwrappedLine(); 2340 return; 2341 } 2342 2343 // If in Whitesmiths mode, the line with the while() needs to be indented 2344 // to the same level as the block. 2345 if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) 2346 ++Line->Level; 2347 2348 nextToken(); 2349 parseStructuralElement(); 2350 } 2351 2352 void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) { 2353 nextToken(); 2354 unsigned OldLineLevel = Line->Level; 2355 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) 2356 --Line->Level; 2357 if (LeftAlignLabel) 2358 Line->Level = 0; 2359 2360 if (!Style.IndentCaseBlocks && CommentsBeforeNextToken.empty() && 2361 FormatTok->Tok.is(tok::l_brace)) { 2362 2363 CompoundStatementIndenter Indenter(this, Line->Level, 2364 Style.BraceWrapping.AfterCaseLabel, 2365 Style.BraceWrapping.IndentBraces); 2366 parseBlock(); 2367 if (FormatTok->Tok.is(tok::kw_break)) { 2368 if (Style.BraceWrapping.AfterControlStatement == 2369 FormatStyle::BWACS_Always) { 2370 addUnwrappedLine(); 2371 if (!Style.IndentCaseBlocks && 2372 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) { 2373 Line->Level++; 2374 } 2375 } 2376 parseStructuralElement(); 2377 } 2378 addUnwrappedLine(); 2379 } else { 2380 if (FormatTok->is(tok::semi)) 2381 nextToken(); 2382 addUnwrappedLine(); 2383 } 2384 Line->Level = OldLineLevel; 2385 if (FormatTok->isNot(tok::l_brace)) { 2386 parseStructuralElement(); 2387 addUnwrappedLine(); 2388 } 2389 } 2390 2391 void UnwrappedLineParser::parseCaseLabel() { 2392 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); 2393 2394 // FIXME: fix handling of complex expressions here. 2395 do { 2396 nextToken(); 2397 } while (!eof() && !FormatTok->Tok.is(tok::colon)); 2398 parseLabel(); 2399 } 2400 2401 void UnwrappedLineParser::parseSwitch() { 2402 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); 2403 nextToken(); 2404 if (FormatTok->Tok.is(tok::l_paren)) 2405 parseParens(); 2406 if (FormatTok->Tok.is(tok::l_brace)) { 2407 CompoundStatementIndenter Indenter(this, Style, Line->Level); 2408 parseBlock(); 2409 addUnwrappedLine(); 2410 } else { 2411 addUnwrappedLine(); 2412 ++Line->Level; 2413 parseStructuralElement(); 2414 --Line->Level; 2415 } 2416 } 2417 2418 void UnwrappedLineParser::parseAccessSpecifier() { 2419 nextToken(); 2420 // Understand Qt's slots. 2421 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots)) 2422 nextToken(); 2423 // Otherwise, we don't know what it is, and we'd better keep the next token. 2424 if (FormatTok->Tok.is(tok::colon)) 2425 nextToken(); 2426 addUnwrappedLine(); 2427 } 2428 2429 void UnwrappedLineParser::parseConcept() { 2430 assert(FormatTok->Tok.is(tok::kw_concept) && "'concept' expected"); 2431 nextToken(); 2432 if (!FormatTok->Tok.is(tok::identifier)) 2433 return; 2434 nextToken(); 2435 if (!FormatTok->Tok.is(tok::equal)) 2436 return; 2437 nextToken(); 2438 if (FormatTok->Tok.is(tok::kw_requires)) { 2439 nextToken(); 2440 parseRequiresExpression(Line->Level); 2441 } else { 2442 parseConstraintExpression(Line->Level); 2443 } 2444 } 2445 2446 void UnwrappedLineParser::parseRequiresExpression(unsigned int OriginalLevel) { 2447 // requires (R range) 2448 if (FormatTok->Tok.is(tok::l_paren)) { 2449 parseParens(); 2450 if (Style.IndentRequires && OriginalLevel != Line->Level) { 2451 addUnwrappedLine(); 2452 --Line->Level; 2453 } 2454 } 2455 2456 if (FormatTok->Tok.is(tok::l_brace)) { 2457 if (Style.BraceWrapping.AfterFunction) 2458 addUnwrappedLine(); 2459 FormatTok->setType(TT_FunctionLBrace); 2460 parseBlock(); 2461 addUnwrappedLine(); 2462 } else { 2463 parseConstraintExpression(OriginalLevel); 2464 } 2465 } 2466 2467 void UnwrappedLineParser::parseConstraintExpression( 2468 unsigned int OriginalLevel) { 2469 // requires Id<T> && Id<T> || Id<T> 2470 while ( 2471 FormatTok->isOneOf(tok::identifier, tok::kw_requires, tok::coloncolon)) { 2472 nextToken(); 2473 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::less, 2474 tok::greater, tok::comma, tok::ellipsis)) { 2475 if (FormatTok->Tok.is(tok::less)) { 2476 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 2477 /*ClosingBraceKind=*/tok::greater); 2478 continue; 2479 } 2480 nextToken(); 2481 } 2482 if (FormatTok->Tok.is(tok::kw_requires)) { 2483 parseRequiresExpression(OriginalLevel); 2484 } 2485 if (FormatTok->Tok.is(tok::less)) { 2486 parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false, 2487 /*ClosingBraceKind=*/tok::greater); 2488 } 2489 2490 if (FormatTok->Tok.is(tok::l_paren)) { 2491 parseParens(); 2492 } 2493 if (FormatTok->Tok.is(tok::l_brace)) { 2494 if (Style.BraceWrapping.AfterFunction) 2495 addUnwrappedLine(); 2496 FormatTok->setType(TT_FunctionLBrace); 2497 parseBlock(); 2498 } 2499 if (FormatTok->Tok.is(tok::semi)) { 2500 // Eat any trailing semi. 2501 nextToken(); 2502 addUnwrappedLine(); 2503 } 2504 if (FormatTok->Tok.is(tok::colon)) { 2505 return; 2506 } 2507 if (!FormatTok->Tok.isOneOf(tok::ampamp, tok::pipepipe)) { 2508 if (FormatTok->Previous && 2509 !FormatTok->Previous->isOneOf(tok::identifier, tok::kw_requires, 2510 tok::coloncolon)) { 2511 addUnwrappedLine(); 2512 } 2513 if (Style.IndentRequires && OriginalLevel != Line->Level) { 2514 --Line->Level; 2515 } 2516 break; 2517 } else { 2518 FormatTok->setType(TT_ConstraintJunctions); 2519 } 2520 2521 nextToken(); 2522 } 2523 } 2524 2525 void UnwrappedLineParser::parseRequires() { 2526 assert(FormatTok->Tok.is(tok::kw_requires) && "'requires' expected"); 2527 2528 unsigned OriginalLevel = Line->Level; 2529 if (FormatTok->Previous && FormatTok->Previous->is(tok::greater)) { 2530 addUnwrappedLine(); 2531 if (Style.IndentRequires) { 2532 Line->Level++; 2533 } 2534 } 2535 nextToken(); 2536 2537 parseRequiresExpression(OriginalLevel); 2538 } 2539 2540 bool UnwrappedLineParser::parseEnum() { 2541 // Won't be 'enum' for NS_ENUMs. 2542 if (FormatTok->Tok.is(tok::kw_enum)) 2543 nextToken(); 2544 2545 const FormatToken &InitialToken = *FormatTok; 2546 2547 // In TypeScript, "enum" can also be used as property name, e.g. in interface 2548 // declarations. An "enum" keyword followed by a colon would be a syntax 2549 // error and thus assume it is just an identifier. 2550 if (Style.Language == FormatStyle::LK_JavaScript && 2551 FormatTok->isOneOf(tok::colon, tok::question)) 2552 return false; 2553 2554 // In protobuf, "enum" can be used as a field name. 2555 if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal)) 2556 return false; 2557 2558 // Eat up enum class ... 2559 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct)) 2560 nextToken(); 2561 2562 while (FormatTok->Tok.getIdentifierInfo() || 2563 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less, 2564 tok::greater, tok::comma, tok::question)) { 2565 nextToken(); 2566 // We can have macros or attributes in between 'enum' and the enum name. 2567 if (FormatTok->is(tok::l_paren)) 2568 parseParens(); 2569 if (FormatTok->is(tok::identifier)) { 2570 nextToken(); 2571 // If there are two identifiers in a row, this is likely an elaborate 2572 // return type. In Java, this can be "implements", etc. 2573 if (Style.isCpp() && FormatTok->is(tok::identifier)) 2574 return false; 2575 } 2576 } 2577 2578 // Just a declaration or something is wrong. 2579 if (FormatTok->isNot(tok::l_brace)) 2580 return true; 2581 FormatTok->setBlockKind(BK_Block); 2582 2583 if (Style.Language == FormatStyle::LK_Java) { 2584 // Java enums are different. 2585 parseJavaEnumBody(); 2586 return true; 2587 } 2588 if (Style.Language == FormatStyle::LK_Proto) { 2589 parseBlock(/*MustBeDeclaration=*/true); 2590 return true; 2591 } 2592 2593 if (!Style.AllowShortEnumsOnASingleLine && 2594 ShouldBreakBeforeBrace(Style, InitialToken)) 2595 addUnwrappedLine(); 2596 // Parse enum body. 2597 nextToken(); 2598 if (!Style.AllowShortEnumsOnASingleLine) { 2599 addUnwrappedLine(); 2600 Line->Level += 1; 2601 } 2602 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true, 2603 /*IsEnum=*/true); 2604 if (!Style.AllowShortEnumsOnASingleLine) 2605 Line->Level -= 1; 2606 if (HasError) { 2607 if (FormatTok->is(tok::semi)) 2608 nextToken(); 2609 addUnwrappedLine(); 2610 } 2611 return true; 2612 2613 // There is no addUnwrappedLine() here so that we fall through to parsing a 2614 // structural element afterwards. Thus, in "enum A {} n, m;", 2615 // "} n, m;" will end up in one unwrapped line. 2616 } 2617 2618 bool UnwrappedLineParser::parseStructLike() { 2619 // parseRecord falls through and does not yet add an unwrapped line as a 2620 // record declaration or definition can start a structural element. 2621 parseRecord(); 2622 // This does not apply to Java, JavaScript and C#. 2623 if (Style.Language == FormatStyle::LK_Java || 2624 Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) { 2625 if (FormatTok->is(tok::semi)) 2626 nextToken(); 2627 addUnwrappedLine(); 2628 return true; 2629 } 2630 return false; 2631 } 2632 2633 namespace { 2634 // A class used to set and restore the Token position when peeking 2635 // ahead in the token source. 2636 class ScopedTokenPosition { 2637 unsigned StoredPosition; 2638 FormatTokenSource *Tokens; 2639 2640 public: 2641 ScopedTokenPosition(FormatTokenSource *Tokens) : Tokens(Tokens) { 2642 assert(Tokens && "Tokens expected to not be null"); 2643 StoredPosition = Tokens->getPosition(); 2644 } 2645 2646 ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); } 2647 }; 2648 } // namespace 2649 2650 // Look to see if we have [[ by looking ahead, if 2651 // its not then rewind to the original position. 2652 bool UnwrappedLineParser::tryToParseSimpleAttribute() { 2653 ScopedTokenPosition AutoPosition(Tokens); 2654 FormatToken *Tok = Tokens->getNextToken(); 2655 // We already read the first [ check for the second. 2656 if (Tok && !Tok->is(tok::l_square)) { 2657 return false; 2658 } 2659 // Double check that the attribute is just something 2660 // fairly simple. 2661 while (Tok) { 2662 if (Tok->is(tok::r_square)) { 2663 break; 2664 } 2665 Tok = Tokens->getNextToken(); 2666 } 2667 Tok = Tokens->getNextToken(); 2668 if (Tok && !Tok->is(tok::r_square)) { 2669 return false; 2670 } 2671 Tok = Tokens->getNextToken(); 2672 if (Tok && Tok->is(tok::semi)) { 2673 return false; 2674 } 2675 return true; 2676 } 2677 2678 void UnwrappedLineParser::parseJavaEnumBody() { 2679 // Determine whether the enum is simple, i.e. does not have a semicolon or 2680 // constants with class bodies. Simple enums can be formatted like braced 2681 // lists, contracted to a single line, etc. 2682 unsigned StoredPosition = Tokens->getPosition(); 2683 bool IsSimple = true; 2684 FormatToken *Tok = Tokens->getNextToken(); 2685 while (Tok) { 2686 if (Tok->is(tok::r_brace)) 2687 break; 2688 if (Tok->isOneOf(tok::l_brace, tok::semi)) { 2689 IsSimple = false; 2690 break; 2691 } 2692 // FIXME: This will also mark enums with braces in the arguments to enum 2693 // constants as "not simple". This is probably fine in practice, though. 2694 Tok = Tokens->getNextToken(); 2695 } 2696 FormatTok = Tokens->setPosition(StoredPosition); 2697 2698 if (IsSimple) { 2699 nextToken(); 2700 parseBracedList(); 2701 addUnwrappedLine(); 2702 return; 2703 } 2704 2705 // Parse the body of a more complex enum. 2706 // First add a line for everything up to the "{". 2707 nextToken(); 2708 addUnwrappedLine(); 2709 ++Line->Level; 2710 2711 // Parse the enum constants. 2712 while (FormatTok) { 2713 if (FormatTok->is(tok::l_brace)) { 2714 // Parse the constant's class body. 2715 parseBlock(/*MustBeDeclaration=*/true, /*AddLevels=*/1u, 2716 /*MunchSemi=*/false); 2717 } else if (FormatTok->is(tok::l_paren)) { 2718 parseParens(); 2719 } else if (FormatTok->is(tok::comma)) { 2720 nextToken(); 2721 addUnwrappedLine(); 2722 } else if (FormatTok->is(tok::semi)) { 2723 nextToken(); 2724 addUnwrappedLine(); 2725 break; 2726 } else if (FormatTok->is(tok::r_brace)) { 2727 addUnwrappedLine(); 2728 break; 2729 } else { 2730 nextToken(); 2731 } 2732 } 2733 2734 // Parse the class body after the enum's ";" if any. 2735 parseLevel(/*HasOpeningBrace=*/true); 2736 nextToken(); 2737 --Line->Level; 2738 addUnwrappedLine(); 2739 } 2740 2741 void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { 2742 const FormatToken &InitialToken = *FormatTok; 2743 nextToken(); 2744 2745 // The actual identifier can be a nested name specifier, and in macros 2746 // it is often token-pasted. 2747 // An [[attribute]] can be before the identifier. 2748 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash, 2749 tok::kw___attribute, tok::kw___declspec, 2750 tok::kw_alignas, tok::l_square, tok::r_square) || 2751 ((Style.Language == FormatStyle::LK_Java || 2752 Style.Language == FormatStyle::LK_JavaScript) && 2753 FormatTok->isOneOf(tok::period, tok::comma))) { 2754 if (Style.Language == FormatStyle::LK_JavaScript && 2755 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) { 2756 // JavaScript/TypeScript supports inline object types in 2757 // extends/implements positions: 2758 // class Foo implements {bar: number} { } 2759 nextToken(); 2760 if (FormatTok->is(tok::l_brace)) { 2761 tryToParseBracedList(); 2762 continue; 2763 } 2764 } 2765 bool IsNonMacroIdentifier = 2766 FormatTok->is(tok::identifier) && 2767 FormatTok->TokenText != FormatTok->TokenText.upper(); 2768 nextToken(); 2769 // We can have macros or attributes in between 'class' and the class name. 2770 if (!IsNonMacroIdentifier) { 2771 if (FormatTok->Tok.is(tok::l_paren)) { 2772 parseParens(); 2773 } else if (FormatTok->is(TT_AttributeSquare)) { 2774 parseSquare(); 2775 // Consume the closing TT_AttributeSquare. 2776 if (FormatTok->Next && FormatTok->is(TT_AttributeSquare)) 2777 nextToken(); 2778 } 2779 } 2780 } 2781 2782 // Note that parsing away template declarations here leads to incorrectly 2783 // accepting function declarations as record declarations. 2784 // In general, we cannot solve this problem. Consider: 2785 // class A<int> B() {} 2786 // which can be a function definition or a class definition when B() is a 2787 // macro. If we find enough real-world cases where this is a problem, we 2788 // can parse for the 'template' keyword in the beginning of the statement, 2789 // and thus rule out the record production in case there is no template 2790 // (this would still leave us with an ambiguity between template function 2791 // and class declarations). 2792 if (FormatTok->isOneOf(tok::colon, tok::less)) { 2793 while (!eof()) { 2794 if (FormatTok->is(tok::l_brace)) { 2795 calculateBraceTypes(/*ExpectClassBody=*/true); 2796 if (!tryToParseBracedList()) 2797 break; 2798 } 2799 if (FormatTok->Tok.is(tok::semi)) 2800 return; 2801 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)) { 2802 addUnwrappedLine(); 2803 nextToken(); 2804 parseCSharpGenericTypeConstraint(); 2805 break; 2806 } 2807 nextToken(); 2808 } 2809 } 2810 if (FormatTok->Tok.is(tok::l_brace)) { 2811 if (ParseAsExpr) { 2812 parseChildBlock(); 2813 } else { 2814 if (ShouldBreakBeforeBrace(Style, InitialToken)) 2815 addUnwrappedLine(); 2816 2817 unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u; 2818 parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/false); 2819 } 2820 } 2821 // There is no addUnwrappedLine() here so that we fall through to parsing a 2822 // structural element afterwards. Thus, in "class A {} n, m;", 2823 // "} n, m;" will end up in one unwrapped line. 2824 } 2825 2826 void UnwrappedLineParser::parseObjCMethod() { 2827 assert(FormatTok->Tok.isOneOf(tok::l_paren, tok::identifier) && 2828 "'(' or identifier expected."); 2829 do { 2830 if (FormatTok->Tok.is(tok::semi)) { 2831 nextToken(); 2832 addUnwrappedLine(); 2833 return; 2834 } else if (FormatTok->Tok.is(tok::l_brace)) { 2835 if (Style.BraceWrapping.AfterFunction) 2836 addUnwrappedLine(); 2837 parseBlock(); 2838 addUnwrappedLine(); 2839 return; 2840 } else { 2841 nextToken(); 2842 } 2843 } while (!eof()); 2844 } 2845 2846 void UnwrappedLineParser::parseObjCProtocolList() { 2847 assert(FormatTok->Tok.is(tok::less) && "'<' expected."); 2848 do { 2849 nextToken(); 2850 // Early exit in case someone forgot a close angle. 2851 if (FormatTok->isOneOf(tok::semi, tok::l_brace) || 2852 FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) 2853 return; 2854 } while (!eof() && FormatTok->Tok.isNot(tok::greater)); 2855 nextToken(); // Skip '>'. 2856 } 2857 2858 void UnwrappedLineParser::parseObjCUntilAtEnd() { 2859 do { 2860 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { 2861 nextToken(); 2862 addUnwrappedLine(); 2863 break; 2864 } 2865 if (FormatTok->is(tok::l_brace)) { 2866 parseBlock(); 2867 // In ObjC interfaces, nothing should be following the "}". 2868 addUnwrappedLine(); 2869 } else if (FormatTok->is(tok::r_brace)) { 2870 // Ignore stray "}". parseStructuralElement doesn't consume them. 2871 nextToken(); 2872 addUnwrappedLine(); 2873 } else if (FormatTok->isOneOf(tok::minus, tok::plus)) { 2874 nextToken(); 2875 parseObjCMethod(); 2876 } else { 2877 parseStructuralElement(); 2878 } 2879 } while (!eof()); 2880 } 2881 2882 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { 2883 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface || 2884 FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation); 2885 nextToken(); 2886 nextToken(); // interface name 2887 2888 // @interface can be followed by a lightweight generic 2889 // specialization list, then either a base class or a category. 2890 if (FormatTok->Tok.is(tok::less)) { 2891 parseObjCLightweightGenerics(); 2892 } 2893 if (FormatTok->Tok.is(tok::colon)) { 2894 nextToken(); 2895 nextToken(); // base class name 2896 // The base class can also have lightweight generics applied to it. 2897 if (FormatTok->Tok.is(tok::less)) { 2898 parseObjCLightweightGenerics(); 2899 } 2900 } else if (FormatTok->Tok.is(tok::l_paren)) 2901 // Skip category, if present. 2902 parseParens(); 2903 2904 if (FormatTok->Tok.is(tok::less)) 2905 parseObjCProtocolList(); 2906 2907 if (FormatTok->Tok.is(tok::l_brace)) { 2908 if (Style.BraceWrapping.AfterObjCDeclaration) 2909 addUnwrappedLine(); 2910 parseBlock(/*MustBeDeclaration=*/true); 2911 } 2912 2913 // With instance variables, this puts '}' on its own line. Without instance 2914 // variables, this ends the @interface line. 2915 addUnwrappedLine(); 2916 2917 parseObjCUntilAtEnd(); 2918 } 2919 2920 void UnwrappedLineParser::parseObjCLightweightGenerics() { 2921 assert(FormatTok->Tok.is(tok::less)); 2922 // Unlike protocol lists, generic parameterizations support 2923 // nested angles: 2924 // 2925 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> : 2926 // NSObject <NSCopying, NSSecureCoding> 2927 // 2928 // so we need to count how many open angles we have left. 2929 unsigned NumOpenAngles = 1; 2930 do { 2931 nextToken(); 2932 // Early exit in case someone forgot a close angle. 2933 if (FormatTok->isOneOf(tok::semi, tok::l_brace) || 2934 FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) 2935 break; 2936 if (FormatTok->Tok.is(tok::less)) 2937 ++NumOpenAngles; 2938 else if (FormatTok->Tok.is(tok::greater)) { 2939 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative"); 2940 --NumOpenAngles; 2941 } 2942 } while (!eof() && NumOpenAngles != 0); 2943 nextToken(); // Skip '>'. 2944 } 2945 2946 // Returns true for the declaration/definition form of @protocol, 2947 // false for the expression form. 2948 bool UnwrappedLineParser::parseObjCProtocol() { 2949 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol); 2950 nextToken(); 2951 2952 if (FormatTok->is(tok::l_paren)) 2953 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);". 2954 return false; 2955 2956 // The definition/declaration form, 2957 // @protocol Foo 2958 // - (int)someMethod; 2959 // @end 2960 2961 nextToken(); // protocol name 2962 2963 if (FormatTok->Tok.is(tok::less)) 2964 parseObjCProtocolList(); 2965 2966 // Check for protocol declaration. 2967 if (FormatTok->Tok.is(tok::semi)) { 2968 nextToken(); 2969 addUnwrappedLine(); 2970 return true; 2971 } 2972 2973 addUnwrappedLine(); 2974 parseObjCUntilAtEnd(); 2975 return true; 2976 } 2977 2978 void UnwrappedLineParser::parseJavaScriptEs6ImportExport() { 2979 bool IsImport = FormatTok->is(Keywords.kw_import); 2980 assert(IsImport || FormatTok->is(tok::kw_export)); 2981 nextToken(); 2982 2983 // Consume the "default" in "export default class/function". 2984 if (FormatTok->is(tok::kw_default)) 2985 nextToken(); 2986 2987 // Consume "async function", "function" and "default function", so that these 2988 // get parsed as free-standing JS functions, i.e. do not require a trailing 2989 // semicolon. 2990 if (FormatTok->is(Keywords.kw_async)) 2991 nextToken(); 2992 if (FormatTok->is(Keywords.kw_function)) { 2993 nextToken(); 2994 return; 2995 } 2996 2997 // For imports, `export *`, `export {...}`, consume the rest of the line up 2998 // to the terminating `;`. For everything else, just return and continue 2999 // parsing the structural element, i.e. the declaration or expression for 3000 // `export default`. 3001 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) && 3002 !FormatTok->isStringLiteral()) 3003 return; 3004 3005 while (!eof()) { 3006 if (FormatTok->is(tok::semi)) 3007 return; 3008 if (Line->Tokens.empty()) { 3009 // Common issue: Automatic Semicolon Insertion wrapped the line, so the 3010 // import statement should terminate. 3011 return; 3012 } 3013 if (FormatTok->is(tok::l_brace)) { 3014 FormatTok->setBlockKind(BK_Block); 3015 nextToken(); 3016 parseBracedList(); 3017 } else { 3018 nextToken(); 3019 } 3020 } 3021 } 3022 3023 void UnwrappedLineParser::parseStatementMacro() { 3024 nextToken(); 3025 if (FormatTok->is(tok::l_paren)) 3026 parseParens(); 3027 if (FormatTok->is(tok::semi)) 3028 nextToken(); 3029 addUnwrappedLine(); 3030 } 3031 3032 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, 3033 StringRef Prefix = "") { 3034 llvm::dbgs() << Prefix << "Line(" << Line.Level 3035 << ", FSC=" << Line.FirstStartColumn << ")" 3036 << (Line.InPPDirective ? " MACRO" : "") << ": "; 3037 for (const auto &Node : Line.Tokens) { 3038 llvm::dbgs() << Node.Tok->Tok.getName() << "[" 3039 << "T=" << static_cast<unsigned>(Node.Tok->getType()) 3040 << ", OC=" << Node.Tok->OriginalColumn << "] "; 3041 } 3042 for (const auto &Node : Line.Tokens) 3043 for (const auto &ChildNode : Node.Children) 3044 printDebugInfo(ChildNode, "\nChild: "); 3045 3046 llvm::dbgs() << "\n"; 3047 } 3048 3049 void UnwrappedLineParser::addUnwrappedLine(LineLevel AdjustLevel) { 3050 if (Line->Tokens.empty()) 3051 return; 3052 LLVM_DEBUG({ 3053 if (CurrentLines == &Lines) 3054 printDebugInfo(*Line); 3055 }); 3056 3057 // If this line closes a block when in Whitesmiths mode, remember that 3058 // information so that the level can be decreased after the line is added. 3059 // This has to happen after the addition of the line since the line itself 3060 // needs to be indented. 3061 bool ClosesWhitesmithsBlock = 3062 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex && 3063 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths; 3064 3065 CurrentLines->push_back(std::move(*Line)); 3066 Line->Tokens.clear(); 3067 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex; 3068 Line->FirstStartColumn = 0; 3069 3070 if (ClosesWhitesmithsBlock && AdjustLevel == LineLevel::Remove) 3071 --Line->Level; 3072 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { 3073 CurrentLines->append( 3074 std::make_move_iterator(PreprocessorDirectives.begin()), 3075 std::make_move_iterator(PreprocessorDirectives.end())); 3076 PreprocessorDirectives.clear(); 3077 } 3078 // Disconnect the current token from the last token on the previous line. 3079 FormatTok->Previous = nullptr; 3080 } 3081 3082 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } 3083 3084 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) { 3085 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) && 3086 FormatTok.NewlinesBefore > 0; 3087 } 3088 3089 // Checks if \p FormatTok is a line comment that continues the line comment 3090 // section on \p Line. 3091 static bool 3092 continuesLineCommentSection(const FormatToken &FormatTok, 3093 const UnwrappedLine &Line, 3094 const llvm::Regex &CommentPragmasRegex) { 3095 if (Line.Tokens.empty()) 3096 return false; 3097 3098 StringRef IndentContent = FormatTok.TokenText; 3099 if (FormatTok.TokenText.startswith("//") || 3100 FormatTok.TokenText.startswith("/*")) 3101 IndentContent = FormatTok.TokenText.substr(2); 3102 if (CommentPragmasRegex.match(IndentContent)) 3103 return false; 3104 3105 // If Line starts with a line comment, then FormatTok continues the comment 3106 // section if its original column is greater or equal to the original start 3107 // column of the line. 3108 // 3109 // Define the min column token of a line as follows: if a line ends in '{' or 3110 // contains a '{' followed by a line comment, then the min column token is 3111 // that '{'. Otherwise, the min column token of the line is the first token of 3112 // the line. 3113 // 3114 // If Line starts with a token other than a line comment, then FormatTok 3115 // continues the comment section if its original column is greater than the 3116 // original start column of the min column token of the line. 3117 // 3118 // For example, the second line comment continues the first in these cases: 3119 // 3120 // // first line 3121 // // second line 3122 // 3123 // and: 3124 // 3125 // // first line 3126 // // second line 3127 // 3128 // and: 3129 // 3130 // int i; // first line 3131 // // second line 3132 // 3133 // and: 3134 // 3135 // do { // first line 3136 // // second line 3137 // int i; 3138 // } while (true); 3139 // 3140 // and: 3141 // 3142 // enum { 3143 // a, // first line 3144 // // second line 3145 // b 3146 // }; 3147 // 3148 // The second line comment doesn't continue the first in these cases: 3149 // 3150 // // first line 3151 // // second line 3152 // 3153 // and: 3154 // 3155 // int i; // first line 3156 // // second line 3157 // 3158 // and: 3159 // 3160 // do { // first line 3161 // // second line 3162 // int i; 3163 // } while (true); 3164 // 3165 // and: 3166 // 3167 // enum { 3168 // a, // first line 3169 // // second line 3170 // }; 3171 const FormatToken *MinColumnToken = Line.Tokens.front().Tok; 3172 3173 // Scan for '{//'. If found, use the column of '{' as a min column for line 3174 // comment section continuation. 3175 const FormatToken *PreviousToken = nullptr; 3176 for (const UnwrappedLineNode &Node : Line.Tokens) { 3177 if (PreviousToken && PreviousToken->is(tok::l_brace) && 3178 isLineComment(*Node.Tok)) { 3179 MinColumnToken = PreviousToken; 3180 break; 3181 } 3182 PreviousToken = Node.Tok; 3183 3184 // Grab the last newline preceding a token in this unwrapped line. 3185 if (Node.Tok->NewlinesBefore > 0) { 3186 MinColumnToken = Node.Tok; 3187 } 3188 } 3189 if (PreviousToken && PreviousToken->is(tok::l_brace)) { 3190 MinColumnToken = PreviousToken; 3191 } 3192 3193 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok, 3194 MinColumnToken); 3195 } 3196 3197 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { 3198 bool JustComments = Line->Tokens.empty(); 3199 for (SmallVectorImpl<FormatToken *>::const_iterator 3200 I = CommentsBeforeNextToken.begin(), 3201 E = CommentsBeforeNextToken.end(); 3202 I != E; ++I) { 3203 // Line comments that belong to the same line comment section are put on the 3204 // same line since later we might want to reflow content between them. 3205 // Additional fine-grained breaking of line comment sections is controlled 3206 // by the class BreakableLineCommentSection in case it is desirable to keep 3207 // several line comment sections in the same unwrapped line. 3208 // 3209 // FIXME: Consider putting separate line comment sections as children to the 3210 // unwrapped line instead. 3211 (*I)->ContinuesLineCommentSection = 3212 continuesLineCommentSection(**I, *Line, CommentPragmasRegex); 3213 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection) 3214 addUnwrappedLine(); 3215 pushToken(*I); 3216 } 3217 if (NewlineBeforeNext && JustComments) 3218 addUnwrappedLine(); 3219 CommentsBeforeNextToken.clear(); 3220 } 3221 3222 void UnwrappedLineParser::nextToken(int LevelDifference) { 3223 if (eof()) 3224 return; 3225 flushComments(isOnNewLine(*FormatTok)); 3226 pushToken(FormatTok); 3227 FormatToken *Previous = FormatTok; 3228 if (Style.Language != FormatStyle::LK_JavaScript) 3229 readToken(LevelDifference); 3230 else 3231 readTokenWithJavaScriptASI(); 3232 FormatTok->Previous = Previous; 3233 } 3234 3235 void UnwrappedLineParser::distributeComments( 3236 const SmallVectorImpl<FormatToken *> &Comments, 3237 const FormatToken *NextTok) { 3238 // Whether or not a line comment token continues a line is controlled by 3239 // the method continuesLineCommentSection, with the following caveat: 3240 // 3241 // Define a trail of Comments to be a nonempty proper postfix of Comments such 3242 // that each comment line from the trail is aligned with the next token, if 3243 // the next token exists. If a trail exists, the beginning of the maximal 3244 // trail is marked as a start of a new comment section. 3245 // 3246 // For example in this code: 3247 // 3248 // int a; // line about a 3249 // // line 1 about b 3250 // // line 2 about b 3251 // int b; 3252 // 3253 // the two lines about b form a maximal trail, so there are two sections, the 3254 // first one consisting of the single comment "// line about a" and the 3255 // second one consisting of the next two comments. 3256 if (Comments.empty()) 3257 return; 3258 bool ShouldPushCommentsInCurrentLine = true; 3259 bool HasTrailAlignedWithNextToken = false; 3260 unsigned StartOfTrailAlignedWithNextToken = 0; 3261 if (NextTok) { 3262 // We are skipping the first element intentionally. 3263 for (unsigned i = Comments.size() - 1; i > 0; --i) { 3264 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) { 3265 HasTrailAlignedWithNextToken = true; 3266 StartOfTrailAlignedWithNextToken = i; 3267 } 3268 } 3269 } 3270 for (unsigned i = 0, e = Comments.size(); i < e; ++i) { 3271 FormatToken *FormatTok = Comments[i]; 3272 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) { 3273 FormatTok->ContinuesLineCommentSection = false; 3274 } else { 3275 FormatTok->ContinuesLineCommentSection = 3276 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex); 3277 } 3278 if (!FormatTok->ContinuesLineCommentSection && 3279 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) { 3280 ShouldPushCommentsInCurrentLine = false; 3281 } 3282 if (ShouldPushCommentsInCurrentLine) { 3283 pushToken(FormatTok); 3284 } else { 3285 CommentsBeforeNextToken.push_back(FormatTok); 3286 } 3287 } 3288 } 3289 3290 void UnwrappedLineParser::readToken(int LevelDifference) { 3291 SmallVector<FormatToken *, 1> Comments; 3292 do { 3293 FormatTok = Tokens->getNextToken(); 3294 assert(FormatTok); 3295 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && 3296 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { 3297 distributeComments(Comments, FormatTok); 3298 Comments.clear(); 3299 // If there is an unfinished unwrapped line, we flush the preprocessor 3300 // directives only after that unwrapped line was finished later. 3301 bool SwitchToPreprocessorLines = !Line->Tokens.empty(); 3302 ScopedLineState BlockState(*this, SwitchToPreprocessorLines); 3303 assert((LevelDifference >= 0 || 3304 static_cast<unsigned>(-LevelDifference) <= Line->Level) && 3305 "LevelDifference makes Line->Level negative"); 3306 Line->Level += LevelDifference; 3307 // Comments stored before the preprocessor directive need to be output 3308 // before the preprocessor directive, at the same level as the 3309 // preprocessor directive, as we consider them to apply to the directive. 3310 if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash && 3311 PPBranchLevel > 0) 3312 Line->Level += PPBranchLevel; 3313 flushComments(isOnNewLine(*FormatTok)); 3314 parsePPDirective(); 3315 } 3316 while (FormatTok->getType() == TT_ConflictStart || 3317 FormatTok->getType() == TT_ConflictEnd || 3318 FormatTok->getType() == TT_ConflictAlternative) { 3319 if (FormatTok->getType() == TT_ConflictStart) { 3320 conditionalCompilationStart(/*Unreachable=*/false); 3321 } else if (FormatTok->getType() == TT_ConflictAlternative) { 3322 conditionalCompilationAlternative(); 3323 } else if (FormatTok->getType() == TT_ConflictEnd) { 3324 conditionalCompilationEnd(); 3325 } 3326 FormatTok = Tokens->getNextToken(); 3327 FormatTok->MustBreakBefore = true; 3328 } 3329 3330 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) && 3331 !Line->InPPDirective) { 3332 continue; 3333 } 3334 3335 if (!FormatTok->Tok.is(tok::comment)) { 3336 distributeComments(Comments, FormatTok); 3337 Comments.clear(); 3338 return; 3339 } 3340 3341 Comments.push_back(FormatTok); 3342 } while (!eof()); 3343 3344 distributeComments(Comments, nullptr); 3345 Comments.clear(); 3346 } 3347 3348 void UnwrappedLineParser::pushToken(FormatToken *Tok) { 3349 Line->Tokens.push_back(UnwrappedLineNode(Tok)); 3350 if (MustBreakBeforeNextToken) { 3351 Line->Tokens.back().Tok->MustBreakBefore = true; 3352 MustBreakBeforeNextToken = false; 3353 } 3354 } 3355 3356 } // end namespace format 3357 } // end namespace clang 3358