Lines Matching +full:semi +full:- +full:static

1 //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
13 //===----------------------------------------------------------------------===//
31 #define DEBUG_TYPE "format-parser"
50 OS << I->Tok->Tok.getName() << "["
51 << "T=" << (unsigned)I->Tok->getType()
52 << ", OC=" << I->Tok->OriginalColumn << ", \"" << I->Tok->TokenText
55 CI = I->Children.begin(),
56 CE = I->Children.end();
67 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line) {
107 else if (!Parser.Line->Tokens.empty())
108 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
111 Parser.Line->Level = PreBlockLine->Level;
112 Parser.Line->PPLevel = PreBlockLine->PPLevel;
113 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
114 Parser.Line->InMacroBody = PreBlockLine->InMacroBody;
115 Parser.Line->UnbracedBodyLevel = PreBlockLine->UnbracedBodyLevel;
119 if (!Parser.Line->Tokens.empty())
121 assert(Parser.Line->Tokens.empty());
146 Parser->addUnwrappedLine();
167 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
177 PPBranchLevel = -1;
193 Line->FirstStartColumn = FirstStartColumn;
197 Token->MacroCtx.reset();
207 Line->FirstStartColumn = FirstStartColumn;
209 LLVM_DEBUG(llvm::dbgs() << "----\n");
218 // the guard) are over-indented by one.
222 --Line.Level;
236 auto it = ExpandedLines.find(Line.Tokens.begin()->Tok);
238 for (const auto &Expanded : it->second) {
260 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
261 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
272 // The top-level context in a file always has declarations, except for pre-
274 bool MustBeDeclaration = !Line->InPPDirective && !Style.isJavaScript();
283 // LK_TextProto is special since its top-level is parsed as the body of a
290 // Here we add this newline before end-of-file comments.
301 switch (FormatTok->Tok.getKind()) {
305 if (FormatTok->is(Keywords.kw_where)) {
320 switch (FormatTok->Tok.getKind()) {
323 --UnpairedSquareBrackets;
344 const FormatToken *Previous = Tokens->getPreviousToken();
345 return Previous && Previous->is(tok::comment) &&
346 (Previous->IsMultiline || Previous->NewlinesBefore > 0);
359 OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
369 if (FormatTok->isAttribute()) {
371 if (FormatTok->is(tok::l_paren))
375 tok::TokenKind Kind = FormatTok->Tok.getKind();
376 if (FormatTok->is(TT_MacroBlockBegin))
378 else if (FormatTok->is(TT_MacroBlockEnd))
397 FormatTok->setFinalizedType(TT_RequiresExpressionLBrace);
398 } else if (FormatTok->Previous &&
399 FormatTok->Previous->ClosesRequiresClause) {
405 if (!InRequiresExpression && FormatTok->isNot(TT_MacroBlockBegin)) {
408 FormatTok->setFinalizedType(TT_BlockLBrace);
417 if (!Style.RemoveBracesLLVM || Line->InPPDirective ||
418 !OpeningBrace->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace)) {
421 if (FormatTok->isNot(tok::r_brace) || StatementCount != 1 || HasLabel ||
426 const FormatToken *Next = Tokens->peekNextToken();
427 if (Next->is(tok::comment) && Next->NewlinesBefore == 0)
437 unsigned StoredPosition = Tokens->getPosition();
438 auto *Next = Tokens->getNextNonComment();
439 FormatTok = Tokens->setPosition(StoredPosition);
440 if (!Next->isOneOf(tok::colon, tok::arrow)) {
441 // default not followed by `:` or `->` is not a case label; treat it
451 (Style.isJavaScript() && Line->MustBeDeclaration)) {
461 (OpeningBrace && OpeningBrace->is(TT_SwitchExpressionLBrace)) ||
462 (Line->InPPDirective && Line->Level == 1))) {
463 ++Line->Level;
488 // a closing brace or eof - note that getNextToken() will
491 unsigned StoredPosition = Tokens->getPosition();
493 const FormatToken *PrevTok = Tok->Previous;
502 assert(Tok->is(tok::l_brace));
505 auto *NextTok = Tokens->getNextNonComment();
507 if (!Line->InMacroBody && !Style.isTableGen()) {
509 while (NextTok->is(tok::hash)) {
510 NextTok = Tokens->getNextToken();
511 if (NextTok->is(tok::pp_not_keyword))
514 NextTok = Tokens->getNextToken();
515 } while (!NextTok->HasUnescapedNewline && NextTok->isNot(tok::eof));
517 while (NextTok->is(tok::comment))
518 NextTok = Tokens->getNextToken();
522 switch (Tok->Tok.getKind()) {
525 if (PrevTok->isOneOf(tok::colon, tok::less)) {
535 Tok->setBlockKind(BK_BracedInit);
536 } else if (PrevTok->is(tok::r_paren)) {
538 Tok->setBlockKind(BK_Block);
541 Tok->setBlockKind(BK_Unknown);
548 if (auto *LBrace = LBraceStack.back().Tok; LBrace->is(BK_Unknown)) {
551 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
552 } else if (LBrace->isNot(TT_EnumLBrace)) {
555 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
556 NextTok->OriginalColumn == 0;
566 ProbablyBracedList = LBrace->is(TT_BracedListLBrace);
570 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
573 ProbablyBracedList || (IsCpp && (PrevTok->Tok.isLiteral() ||
574 NextTok->is(tok::l_paren)));
582 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
589 (NextTok->is(tok::l_brace) && LBraceStack.back().PrevTok &&
590 LBraceStack.back().PrevTok->isOneOf(tok::identifier,
595 (NextTok->is(tok::identifier) &&
596 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace));
599 (NextTok->is(tok::semi) &&
604 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
606 if (!Style.isCSharp() && NextTok->is(tok::l_square)) {
609 NextTok = Tokens->getNextToken();
610 ProbablyBracedList = NextTok->isNot(tok::l_square);
614 if (IsCpp && Line->InMacroBody && PrevTok != FormatTok &&
615 !FormatTok->Previous && NextTok->is(tok::eof) &&
619 !PrevTok->isOneOf(tok::semi, BK_Block, tok::colon)) {
624 Tok->setBlockKind(BlockKind);
625 LBrace->setBlockKind(BlockKind);
630 if (Tok->isNot(TT_StatementMacro))
634 case tok::semi:
641 if (!LBraceStack.empty() && LBraceStack.back().Tok->is(BK_Unknown))
642 LBraceStack.back().Tok->setBlockKind(BK_Block);
650 } while (Tok->isNot(tok::eof) && !LBraceStack.empty());
654 if (Entry.Tok->is(BK_Unknown))
655 Entry.Tok->setBlockKind(BK_Block);
657 FormatTok = Tokens->setPosition(StoredPosition);
662 if (auto Prev = FormatTok->getPreviousNonComment();
663 Prev && Prev->is(tok::r_brace)) {
664 Prev->setFinalizedType(Type);
669 static inline void hash_combine(std::size_t &seed, const T &v) {
706 SavedToken.Tok->copyFrom(*Token.Tok);
717 auto Length = LastToken->TotalLength;
720 if (auto Prev = OpeningBrace->Previous;
721 Prev && Prev->TotalLength + ColumnLimit == OpeningBrace->TotalLength) {
722 Length -= ColumnLimit;
724 Length -= OpeningBrace->TokenText.size() + 1;
727 if (const auto *FirstToken = Line.First; FirstToken->is(tok::r_brace)) {
728 assert(!OpeningBrace || OpeningBrace->is(TT_ControlStatementLBrace));
729 Length -= FirstToken->TokenText.size() + 1;
735 Token.Tok->copyFrom(*SavedToken.Tok);
753 if (Style.isVerilog() && FormatTok->is(tok::colon)) {
760 // Whether this is a Verilog-specific block that has a special header like a
764 assert((FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) ||
769 const bool FollowedByComment = Tokens->peekNextToken()->is(tok::comment);
770 auto Index = CurrentLines->size();
771 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
772 FormatTok->setBlockKind(BK_Block);
778 ++Line->Level;
783 const unsigned InitialLevel = Line->Level;
792 if (Line->Level > 300)
795 if (MacroBlock && FormatTok->is(tok::l_paren))
802 CurrentLines->empty()
804 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
810 --Line->Level;
815 Line->Level += AddLevels;
823 if (MacroBlock ? FormatTok->isNot(TT_MacroBlockEnd)
824 : FormatTok->isNot(tok::r_brace)) {
825 Line->Level = InitialLevel;
826 FormatTok->setBlockKind(BK_Block);
830 if (FormatTok->is(tok::r_brace)) {
831 FormatTok->setBlockKind(BK_Block);
832 if (Tok->is(TT_NamespaceLBrace))
833 FormatTok->setFinalizedType(TT_NamespaceRBrace);
837 FormatTok->is(tok::r_brace) && Tok->is(TT_FunctionLBrace);
842 assert(Tok->isOneOf(TT_ControlStatementLBrace, TT_ElseLBrace));
843 assert(FormatTok->is(tok::r_brace));
844 const bool WrappedOpeningBrace = !Tok->Previous;
847 const bool HasRequiredIfBraces = IfLBrace && !IfLBrace->Optional;
850 if (Tok->isNot(TT_ElseLBrace) || !HasRequiredIfBraces) {
851 const FormatToken *Previous = Tokens->getPreviousToken();
853 if (Previous->is(tok::r_brace) && !Previous->Optional)
856 assert(!CurrentLines->empty());
857 auto &LastLine = CurrentLines->back();
860 if (Tok->is(TT_ElseLBrace))
864 --Index; // The line above the wrapped l_brace.
870 Tok->MatchingParen = FormatTok;
871 FormatTok->MatchingParen = Tok;
877 nextToken(/*LevelDifference=*/-AddLevels);
883 while (FormatTok->is(tok::semi)) {
884 FormatTok->Optional = true;
891 if (MacroBlock && FormatTok->is(tok::l_paren))
894 Line->Level = InitialLevel;
896 if (FormatTok->is(tok::kw_noexcept)) {
901 if (FormatTok->is(tok::arrow)) {
908 if (MunchSemi && FormatTok->is(tok::semi))
912 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
916 CurrentLines->size() - 1;
923 static bool isGoogScope(const UnwrappedLine &Line) {
924 // FIXME: Closure-library specific stuff should not be hard-coded but be
929 if (I->Tok->TokenText != "goog")
932 if (I->Tok->isNot(tok::period))
935 if (I->Tok->TokenText != "scope")
938 return I->Tok->is(tok::l_paren);
941 static bool isIIFE(const UnwrappedLine &Line,
944 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
950 if (I->Tok->isNot(tok::l_paren))
953 if (I->Tok->isNot(Keywords.kw_function))
956 return I->Tok->is(tok::l_paren);
959 static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
982 assert(FormatTok->is(tok::l_brace));
983 FormatTok->setBlockKind(BK_Block);
992 Line->Level += SkipIndent ? 0 : 1;
995 Line->Level -= SkipIndent ? 0 : 1;
1001 assert(FormatTok->is(tok::hash) && "'#' expected");
1006 if (!FormatTok->Tok.getIdentifierInfo()) {
1011 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
1041 size_t Line = CurrentLines->size();
1060 PPChainBranchIndex.push(Unreachable ? -1 : 0);
1083 if (PPBranchLevel > -1)
1084 --PPBranchLevel;
1092 bool IfNDef = FormatTok->is(tok::pp_ifndef);
1095 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
1097 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
1106 if (Line.Tokens.front().Tok->isNot(tok::comment)) {
1113 --PPBranchLevel;
1127 assert(PPBranchLevel >= -1);
1128 if (PPBranchLevel == -1)
1131 --PPBranchLevel;
1141 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 && Tokens->isEOF() &&
1150 if (!FormatTok->Tok.getIdentifierInfo()) {
1158 IncludeGuardToken->TokenText == FormatTok->TokenText) {
1162 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
1174 FormatTok->Tok.setKind(tok::identifier);
1175 FormatTok->Tok.setIdentifierInfo(Keywords.kw_internal_ident_after_define);
1177 if (FormatTok->Tok.getKind() == tok::l_paren &&
1178 !FormatTok->hasWhitespaceBefore()) {
1182 Line->Level += PPBranchLevel + 1;
1184 ++Line->Level;
1186 Line->PPLevel = PPBranchLevel + (IncludeGuard == IG_Defined ? 0 : 1);
1187 assert((int)Line->PPLevel >= 0);
1188 Line->InMacroBody = true;
1192 FormatTok->Finalized = true;
1193 FormatTok = Tokens->getNextToken();
1202 // re-indentation if there was a structural error) within the macro
1208 Line->InPragmaDirective = true;
1217 Line->Level += PPBranchLevel + 1;
1224 static bool tokenCanStartNewLine(const FormatToken &Tok) {
1225 // Semicolon can be a null-statement, l_square can be a start of a macro or
1227 return !Tok.isOneOf(tok::semi, tok::l_brace,
1238 // lists, range-based for loops, ternary operator, but
1245 static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
1248 return FormatTok->is(tok::identifier) &&
1249 (!FormatTok->Tok.getIdentifierInfo() ||
1250 !FormatTok->isOneOf(
1260 static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
1262 return FormatTok->Tok.isLiteral() ||
1263 FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
1269 static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
1271 return FormatTok->isOneOf(
1289 static bool isC78Type(const FormatToken &Tok) {
1302 static bool isC78ParameterDecl(const FormatToken *Tok, const FormatToken *Next,
1308 if (FuncName->isNot(tok::identifier))
1311 const FormatToken *Prev = FuncName->Previous;
1312 if (!Prev || (Prev->isNot(tok::star) && !isC78Type(*Prev)))
1316 !Tok->isOneOf(tok::kw_register, tok::kw_struct, tok::kw_union)) {
1320 if (Next->isNot(tok::star) && !Next->Tok.getIdentifierInfo())
1323 Tok = Tok->Previous;
1324 if (!Tok || Tok->isNot(tok::r_paren))
1327 Tok = Tok->Previous;
1328 if (!Tok || Tok->isNot(tok::identifier))
1331 return Tok->Previous && Tok->Previous->isOneOf(tok::l_paren, tok::comma);
1335 assert(FormatTok->is(Keywords.kw_import) && "'import' expected");
1337 if (auto Token = Tokens->peekNextToken(/*SkipComment=*/true);
1338 !Token->Tok.getIdentifierInfo() &&
1339 !Token->isOneOf(tok::colon, tok::less, tok::string_literal)) {
1345 if (FormatTok->is(tok::colon)) {
1346 FormatTok->setFinalizedType(TT_ModulePartitionColon);
1349 else if (FormatTok->is(tok::less)) {
1351 while (!FormatTok->isOneOf(tok::semi, tok::greater, tok::eof)) {
1354 if (FormatTok->isNot(tok::comment) &&
1355 !FormatTok->TokenText.starts_with("//")) {
1356 FormatTok->setFinalizedType(TT_ImplicitStringLiteral);
1361 if (FormatTok->is(tok::semi)) {
1376 // This method is conservative - it cannot cover all edge cases of JavaScript,
1386 ? Next->NewlinesBefore == 0
1387 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
1393 Previous->is(TT_TemplateString) && Previous->TokenText.ends_with("${");
1394 if (PreviousMustBeValue || Previous->is(tok::r_paren)) {
1397 bool HasAt = llvm::any_of(Line->Tokens, [](UnwrappedLineNode &LineNode) {
1398 return LineNode.Tok->is(tok::at);
1403 if (Next->is(tok::exclaim) && PreviousMustBeValue)
1407 Next->is(TT_TemplateString) && Next->TokenText.starts_with("}");
1410 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
1414 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
1424 FormatTok->is(tok::pp_include)) {
1426 if (FormatTok->is(tok::string_literal))
1433 while (FormatTok->is(tok::l_square) && handleCppAttributes()) {
1440 if (FormatTok->isOneOf(Keywords.kw_foreach, Keywords.kw_repeat)) {
1444 if (FormatTok->isOneOf(tok::kw_restrict, Keywords.kw_assert,
1452 if (FormatTok->isOneOf(Keywords.kw_priority, Keywords.kw_unique,
1455 } else if (FormatTok->is(tok::l_paren) &&
1456 Tokens->peekNextToken()->is(tok::star)) {
1465 if (FormatTok->isAccessSpecifierKeyword()) {
1474 switch (FormatTok->Tok.getKind()) {
1477 if (FormatTok->is(tok::l_brace)) {
1478 FormatTok->setFinalizedType(TT_InlineASMBrace);
1481 if (FormatTok->is(tok::r_brace)) {
1482 FormatTok->setFinalizedType(TT_InlineASMBrace);
1487 FormatTok->Finalized = true;
1496 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1507 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1514 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1523 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1533 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1539 if (FormatTok->is(tok::colon)) {
1540 FormatTok->setFinalizedType(TT_CaseLabelColon);
1544 if (FormatTok->is(tok::arrow)) {
1545 FormatTok->setFinalizedType(TT_CaseLabelArrow);
1546 Default->setFinalizedType(TT_SwitchExpressionLabel);
1564 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1573 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1588 } else if (FormatTok->is(tok::string_literal)) {
1590 if (FormatTok->is(tok::l_brace)) {
1615 if (FormatTok->is(tok::kw_namespace)) {
1619 if (FormatTok->is(Keywords.kw_import) && parseModuleImport())
1625 if (FormatTok->is(tok::kw_namespace)) {
1631 if (FormatTok->is(TT_ForEachMacro)) {
1635 if (FormatTok->is(TT_MacroBlockBegin)) {
1640 if (FormatTok->is(Keywords.kw_import)) {
1647 if (FormatTok->is(tok::kw_public))
1649 if (FormatTok->isNot(tok::string_literal))
1652 if (FormatTok->is(tok::semi))
1660 if (IsCpp && FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
1663 if (FormatTok->is(tok::colon)) {
1669 if (IsCpp && FormatTok->is(TT_StatementMacro)) {
1673 if (IsCpp && FormatTok->is(TT_NamespaceMacro)) {
1682 Tokens->peekNextToken()->is(tok::colon) && !Line->MustBeDeclaration) {
1684 if (!Line->InMacroBody || CurrentLines->size() > 1)
1685 Line->Tokens.begin()->Tok->MustBreakBefore = true;
1686 FormatTok->setFinalizedType(TT_GotoLabelColon);
1699 OpeningBrace && OpeningBrace->is(TT_RequiresExpressionLBrace);
1701 if (IsCpp && FormatTok->isCppAlternativeOperatorKeyword()) {
1702 if (auto *Next = Tokens->peekNextToken(/*SkipComment=*/true);
1703 Next && Next->isBinaryOperator()) {
1704 FormatTok->Tok.setKind(tok::identifier);
1707 const FormatToken *Previous = FormatTok->Previous;
1708 switch (FormatTok->Tok.getKind()) {
1711 if (FormatTok->is(tok::l_brace)) {
1716 FormatTok->is(Keywords.kw_interface)) {
1720 switch (FormatTok->Tok.getObjCKeywordID()) {
1742 if (FormatTok->is(tok::l_brace)) {
1753 if (FormatTok->is(tok::l_paren)) {
1757 if (FormatTok->is(tok::l_brace)) {
1786 // Ignore if this is part of "template <enum ..." or "... -> enum" or
1788 if (Previous && Previous->isOneOf(tok::less, tok::arrow, tok::comma)) {
1805 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1832 if (FormatTok->is(tok::l_paren)) {
1834 assert(FormatTok->Previous);
1835 if (FormatTok->Previous->endsSequence(tok::r_paren, tok::kw_auto,
1837 Line->SeenDecltypeAuto = true;
1843 // In Java, classes have an implicit static member "class".
1845 FormatTok->is(tok::kw_class)) {
1849 FormatTok->Tok.getIdentifierInfo()) {
1855 case tok::semi:
1869 Tokens->peekNextToken(/*SkipComment=*/true),
1878 if (FormatTok->isBinaryOperator())
1884 if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isTypeName(LangOpts)) {
1887 while (FormatTok->is(tok::star))
1891 if (FormatTok->is(tok::l_paren))
1894 if (FormatTok->is(tok::l_brace))
1899 FormatTok->setFinalizedType(TT_BracedListLBrace);
1901 IsDecltypeAutoFunction = Line->SeenDecltypeAuto;
1907 Line->Tokens.front().Tok->is(Keywords.kw_synchronized)) {
1917 if (!Previous || Previous->isNot(TT_TypeDeclarationParen))
1918 FormatTok->setFinalizedType(TT_FunctionLBrace);
1928 if (Style.isJavaScript() && Line->MustBeDeclaration) {
1933 // We arrive here when parsing function-try blocks.
1939 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where) &&
1940 Line->MustBeDeclaration) {
1945 if (FormatTok->is(TT_MacroBlockEnd)) {
1954 size_t TokenCount = Line->Tokens.size();
1955 if (Style.isJavaScript() && FormatTok->is(Keywords.kw_function) &&
1958 Line->Tokens.front().Tok->isNot(Keywords.kw_async)))) {
1963 FormatTok->is(Keywords.kw_interface)) {
1969 unsigned StoredPosition = Tokens->getPosition();
1970 FormatToken *Next = Tokens->getNextToken();
1971 FormatTok = Tokens->setPosition(StoredPosition);
1983 if (FormatTok->is(Keywords.kw_table)) {
1995 if (!IsCpp && FormatTok->is(Keywords.kw_interface)) {
2001 if (IsCpp && FormatTok->is(TT_StatementMacro)) {
2007 StringRef Text = FormatTok->TokenText;
2018 auto I = Line->Tokens.begin(), E = Line->Tokens.end();
2019 while (I != E && I->Tok->is(tok::comment))
2022 while (I != E && I->Tok->is(tok::hash))
2027 // Recognize function-like macro usages without trailing semicolon as
2028 // well as free-standing macros like Q_OBJECT.
2029 bool FunctionLike = FormatTok->is(tok::l_paren);
2035 ? FormatTok->NewlinesBefore > 0
2036 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
2040 if (PreviousToken->isNot(TT_UntouchableMacroFunc))
2041 PreviousToken->setFinalizedType(TT_FunctionLikeOrFreestandingMacro);
2050 FormatTok->is(TT_FatArrow)) {
2056 if (FormatTok->is(tok::l_brace)) {
2061 FormatTok->setBlockKind(BK_BracedInit);
2065 Line->Tokens.begin()->Tok->is(Keywords.kw_defset)) {
2066 FormatTok->setFinalizedType(TT_FunctionLBrace);
2075 FormatTok->is(tok::less)) {
2104 if (Style.isJavaScript() && Line->MustBeDeclaration) {
2114 if (FormatTok->is(tok::colon)) {
2118 if (FormatTok->is(Keywords.kw_clocking)) {
2137 if (FormatTok->is(tok::l_brace))
2138 FormatTok->Previous->setFinalizedType(TT_TemplateCloser);
2148 assert(FormatTok->is(tok::l_brace));
2152 if (FormatTok->Previous->isNot(tok::identifier))
2160 unsigned int StoredPosition = Tokens->getPosition();
2161 FormatToken *Tok = Tokens->getNextToken();
2169 if (Tok->isAccessSpecifierKeyword() ||
2170 Tok->isOneOf(tok::semi, Keywords.kw_internal, Keywords.kw_get,
2172 if (Tok->isOneOf(Keywords.kw_get, Keywords.kw_init, Keywords.kw_set))
2174 Tok = Tokens->getNextToken();
2177 if (Tok->isNot(tok::r_brace))
2183 Tokens->setPosition(StoredPosition);
2188 // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
2189 Tokens->setPosition(StoredPosition);
2194 switch (FormatTok->Tok.getKind()) {
2197 if (FormatTok->is(tok::equal)) {
2198 while (!eof() && FormatTok->isNot(tok::semi))
2205 ++Line->Level;
2208 --Line->Level;
2211 if (FormatTok->is(TT_FatArrow)) {
2212 ++Line->Level;
2215 } while (!eof() && FormatTok->isNot(tok::semi));
2218 --Line->Level;
2224 if (FormatTok->isOneOf(Keywords.kw_get, Keywords.kw_init,
2227 // Non-trivial get/set needs to be on its own line.
2234 // Unreachable for well-formed code (paired '{' and '}').
2239 assert(FormatTok->is(tok::l_square));
2251 while (FormatTok->isNot(tok::l_brace)) {
2252 if (FormatTok->isTypeName(LangOpts) || FormatTok->isAttribute()) {
2256 switch (FormatTok->Tok.getKind()) {
2266 assert(FormatTok->Previous);
2267 if (FormatTok->Previous->is(tok::r_square))
2299 // this as an heuristic to distinguish between Objective-C expressions
2300 // followed by an `a->b` expression, such as:
2301 // ([obj func:arg] + a->b)
2332 FormatTok->setFinalizedType(TT_LambdaArrow);
2352 FormatTok->setFinalizedType(TT_LambdaLBrace);
2355 NestedLambdas.push_back(Line->SeenDecltypeAuto);
2364 const FormatToken *Previous = FormatTok->Previous;
2367 if ((Previous && ((Previous->Tok.getIdentifierInfo() &&
2368 !Previous->isOneOf(tok::kw_return, tok::kw_co_await,
2370 Previous->closesScope())) ||
2371 LeftSquare->isCppStructuredBinding(IsCpp)) {
2374 if (FormatTok->is(tok::l_square) || tok::isLiteral(FormatTok->Tok.getKind()))
2376 if (FormatTok->is(tok::r_square)) {
2377 const FormatToken *Next = Tokens->peekNextToken(/*SkipComment=*/true);
2378 if (Next->is(tok::greater))
2386 assert(FormatTok->is(Keywords.kw_function));
2387 if (FormatTok->is(Keywords.kw_async))
2393 if (FormatTok->is(tok::star)) {
2394 FormatTok->setFinalizedType(TT_OverloadedOperator);
2399 if (FormatTok->is(tok::identifier))
2402 if (FormatTok->isNot(tok::l_paren))
2408 if (FormatTok->is(tok::colon)) {
2414 if (FormatTok->is(tok::l_brace))
2417 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
2421 if (FormatTok->is(tok::semi))
2428 if (FormatTok->is(BK_Unknown))
2430 assert(FormatTok->isNot(BK_Unknown));
2431 if (FormatTok->is(BK_Block))
2440 assert(FormatTok->is(TT_FatArrow));
2445 if (FormatTok->isNot(tok::l_brace))
2458 if (Style.isCSharp() && FormatTok->is(TT_FatArrow) &&
2463 if (FormatTok->is(Keywords.kw_function)) {
2467 if (FormatTok->is(tok::l_brace)) {
2474 if (FormatTok->is(IsAngleBracket ? tok::greater : tok::r_brace)) {
2476 FormatTok->setBlockKind(BK_Block);
2483 switch (FormatTok->Tok.getKind()) {
2495 if (FormatTok->is(tok::l_brace))
2503 FormatTok->setBlockKind(BK_BracedInit);
2505 auto *Prev = FormatTok->Previous;
2506 if (Prev && Prev->is(tok::greater))
2507 Prev->setFinalizedType(TT_TemplateCloser);
2517 case tok::semi:
2519 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
2550 assert(FormatTok->is(tok::l_paren) && "'(' expected.");
2554 const bool MightBeStmtExpr = Tokens->peekNextToken()->is(tok::l_brace);
2557 switch (FormatTok->Tok.getKind()) {
2561 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
2565 auto *Prev = LeftParen->Previous;
2566 if (!MightBeStmtExpr && !MightBeFoldExpr && !Line->InMacroBody &&
2568 const auto *Next = Tokens->peekNextToken();
2570 Prev && Prev->is(tok::l_paren) && Next && Next->is(tok::r_paren);
2571 const auto *PrevPrev = Prev ? Prev->getPreviousNonComment() : nullptr;
2574 (PrevPrev->isOneOf(tok::kw___attribute, tok::kw_decltype) ||
2576 (PrevPrev->isOneOf(tok::kw_if, tok::kw_while) ||
2577 PrevPrev->endsSequence(tok::kw_constexpr, tok::kw_if))));
2582 Prev && Prev->isOneOf(tok::kw_return, tok::kw_co_return) && Next &&
2583 Next->is(tok::semi);
2585 LeftParen->Optional = true;
2586 FormatTok->Optional = true;
2590 if (Prev->is(TT_TypenameMacro)) {
2591 LeftParen->setFinalizedType(TT_TypeDeclarationParen);
2592 FormatTok->setFinalizedType(TT_TypeDeclarationParen);
2593 } else if (Prev->is(tok::greater) && FormatTok->Previous == LeftParen) {
2594 Prev->setFinalizedType(TT_TemplateCloser);
2612 if (FormatTok->is(tok::l_brace)) {
2623 if (Style.isCSharp() && FormatTok->is(TT_FatArrow))
2635 if (Style.isJavaScript() && (FormatTok->is(Keywords.kw_function)))
2654 FormatTok->setFinalizedType(AmpAmpTokenType);
2666 assert(FormatTok->is(tok::l_square) && "'[' expected.");
2671 switch (FormatTok->Tok.getKind()) {
2692 if (FormatTok->is(tok::l_brace)) {
2711 NestedTooDeep[Size - MaxNestingLevels] = true;
2715 static FormatToken *getLastNonComment(const UnwrappedLine &Line) {
2717 if (Token.Tok->isNot(tok::comment))
2726 if (Style.InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() &&
2727 PreprocessorDirectives.empty() && FormatTok->isNot(tok::semi)) {
2730 : Line->Tokens.back().Tok;
2732 if (Tok->BraceCount < 0) {
2733 assert(Tok->BraceCount == -1);
2736 Tok->BraceCount = -1;
2741 ++Line->Level;
2742 ++Line->UnbracedBodyLevel;
2744 --Line->UnbracedBodyLevel;
2747 assert(!Line->InPPDirective);
2756 ++Tok->BraceCount;
2762 --Line->Level;
2765 static void markOptionalBraces(FormatToken *LeftBrace) {
2769 assert(LeftBrace->is(tok::l_brace));
2771 FormatToken *RightBrace = LeftBrace->MatchingParen;
2773 assert(!LeftBrace->Optional);
2777 assert(RightBrace->is(tok::r_brace));
2778 assert(RightBrace->MatchingParen == LeftBrace);
2779 assert(LeftBrace->Optional == RightBrace->Optional);
2781 LeftBrace->Optional = true;
2782 RightBrace->Optional = true;
2787 if (FormatTok->isAttribute())
2789 else if (FormatTok->is(tok::l_square))
2795 assert(FormatTok->is(tok::l_square));
2813 assert((FormatTok->is(tok::kw_if) ||
2815 FormatTok->isOneOf(tok::kw_restrict, Keywords.kw_assert,
2822 if (FormatTok->is(Keywords.kw_verilogHash)) {
2824 if (FormatTok->is(tok::numeric_constant))
2826 } else if (FormatTok->isOneOf(Keywords.kw_final, Keywords.kw_property,
2834 while (!eof() && FormatTok->isNot(Keywords.kw_then)) {
2841 if (FormatTok->is(tok::exclaim))
2845 if (FormatTok->is(tok::kw_consteval)) {
2849 if (FormatTok->isOneOf(tok::kw_constexpr, tok::identifier))
2851 if (FormatTok->is(tok::l_paren)) {
2852 FormatTok->setFinalizedType(TT_ConditionLParen);
2858 if (IsVerilogAssert && FormatTok->is(tok::semi)) {
2871 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
2873 CompoundStatementIndenter Indenter(this, Style, Line->Level);
2881 } else if (IsVerilogAssert && FormatTok->is(tok::kw_else)) {
2890 (IfLeftBrace && !IfLeftBrace->MatchingParen) ||
2899 if (FormatTok->is(tok::kw_else)) {
2907 const bool FollowedByIf = Tokens->peekNextToken()->is(tok::kw_if);
2908 FormatTok->setFinalizedType(TT_ElseLBrace);
2910 CompoundStatementIndenter Indenter(this, Style, Line->Level);
2916 if (FormatTok->is(tok::kw_else)) {
2920 } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) {
2922 assert(ElseLeftBrace->MatchingParen);
2926 } else if (!IsVerilogAssert && FormatTok->is(tok::kw_if)) {
2927 const FormatToken *Previous = Tokens->getPreviousToken();
2929 const bool IsPrecededByComment = Previous->is(tok::comment);
2932 ++Line->Level;
2943 --Line->Level;
2958 (ElseLeftBrace && !ElseLeftBrace->MatchingParen) ||
2967 FormatToken *IfRightBrace = IfLeftBrace->MatchingParen;
2969 assert(IfRightBrace->MatchingParen == IfLeftBrace);
2970 assert(!IfLeftBrace->Optional);
2971 assert(!IfRightBrace->Optional);
2972 IfLeftBrace->MatchingParen = nullptr;
2973 IfRightBrace->MatchingParen = nullptr;
2984 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
2988 if (FormatTok->is(tok::colon)) {
2992 if (FormatTok->is(tok::identifier)) {
2994 Colon->setFinalizedType(TT_CtorInitializerColon);
2997 // In case identifiers were removed by clang-tidy, what might follow is
2998 // multiple commas in sequence - before the first identifier.
2999 while (FormatTok->is(tok::comma))
3002 while (FormatTok->is(tok::identifier)) {
3004 if (FormatTok->is(tok::l_paren)) {
3006 } else if (FormatTok->is(tok::l_brace)) {
3011 // In case identifiers were removed by clang-tidy, what might follow is
3012 // multiple commas in sequence - after the first identifier.
3013 while (FormatTok->is(tok::comma))
3018 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren))
3023 if (FormatTok->is(tok::l_brace)) {
3025 FormatTok->setFinalizedType(TT_FunctionLBrace);
3026 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3032 } else if (FormatTok->isNot(tok::kw_catch)) {
3033 // The C++ standard requires a compound-statement after a try.
3037 ++Line->Level;
3039 --Line->Level;
3042 if (FormatTok->is(tok::at))
3044 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
3047 FormatTok->is(Keywords.kw_finally)) ||
3048 (FormatTok->isObjCAtKeyword(tok::objc_catch) ||
3049 FormatTok->isObjCAtKeyword(tok::objc_finally)))) {
3053 while (FormatTok->isNot(tok::l_brace)) {
3054 if (FormatTok->is(tok::l_paren)) {
3058 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof)) {
3066 Line->MustBeDeclaration = false;
3067 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3083 assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
3091 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
3093 (Style.isCSharp() && FormatTok->is(tok::kw_union))) {
3094 if (FormatTok->is(tok::l_square))
3096 else if (FormatTok->is(tok::l_paren))
3102 if (FormatTok->is(tok::l_brace)) {
3103 FormatTok->setFinalizedType(TT_NamespaceLBrace);
3121 ++Line->Level;
3132 --Line->Level;
3138 assert(FormatTok->is(tok::kw_new) && "'new' expected");
3144 if (FormatTok->is(tok::l_paren))
3148 if (FormatTok->is(tok::l_brace))
3151 if (FormatTok->isOneOf(tok::semi, tok::comma))
3164 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
3168 if (FormatTok->is(tok::l_paren)) {
3172 if (FormatTok->is(tok::l_brace))
3184 FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3186 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3206 assert((FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) ||
3208 FormatTok->isOneOf(Keywords.kw_always, Keywords.kw_always_comb,
3215 !FormatTok->isOneOf(tok::kw_for, tok::kw_while);
3219 if (Style.isJavaScript() && FormatTok->is(Keywords.kw_await))
3221 if (IsCpp && FormatTok->is(tok::kw_co_await))
3223 if (HasParens && FormatTok->is(tok::l_paren)) {
3228 FormatTok->setFinalizedType(TT_ConditionLParen);
3235 } else if (Style.AllowShortLoopsOnASingleLine && FormatTok->is(tok::semi) &&
3236 Tokens->getPreviousToken()->is(tok::r_paren)) {
3247 assert(FormatTok->is(tok::kw_do) && "'do' expected");
3253 if (FormatTok->isNot(tok::kw_while)) {
3258 FormatTok->setFinalizedType(TT_DoWhile);
3263 ++Line->Level;
3271 unsigned OldLineLevel = Line->Level;
3274 Line->Level = 0;
3275 else if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
3276 --Line->Level;
3279 FormatTok->is(tok::l_brace)) {
3281 CompoundStatementIndenter Indenter(this, Line->Level,
3285 if (FormatTok->is(tok::kw_break)) {
3291 ++Line->Level;
3298 if (FormatTok->is(tok::semi))
3302 Line->Level = OldLineLevel;
3303 if (FormatTok->isNot(tok::l_brace)) {
3310 assert(FormatTok->is(tok::kw_case) && "'case' expected");
3316 if (FormatTok->is(tok::colon)) {
3317 FormatTok->setFinalizedType(TT_CaseLabelColon);
3320 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::arrow)) {
3321 FormatTok->setFinalizedType(TT_CaseLabelArrow);
3322 Case->setFinalizedType(TT_SwitchExpressionLabel);
3330 assert(FormatTok->is(tok::kw_switch) && "'switch' expected");
3332 if (FormatTok->is(tok::l_paren))
3337 if (FormatTok->is(tok::l_brace)) {
3338 CompoundStatementIndenter Indenter(this, Style, Line->Level);
3339 FormatTok->setFinalizedType(IsExpr ? TT_SwitchExpressionLBrace
3350 ++Line->Level;
3352 --Line->Level;
3360 static bool isCOperatorFollowingVar(tok::TokenKind Kind) {
3399 case tok::semi:
3414 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
3417 if (FormatTok->is(tok::colon)) {
3420 } else if (FormatTok->isNot(tok::coloncolon) &&
3421 !isCOperatorFollowingVar(FormatTok->Tok.getKind())) {
3426 AccessSpecifierCandidate->Tok.setKind(tok::identifier);
3434 assert(FormatTok->is(tok::kw_requires) && "'requires' expected");
3441 switch (FormatTok->Tok.getKind()) {
3462 auto *PreviousNonComment = RequiresToken->getPreviousNonComment();
3465 PreviousNonComment->is(TT_RequiresExpressionLBrace)) {
3472 switch (PreviousNonComment->Tok.getKind()) {
3488 auto PrevPrev = PreviousNonComment->getPreviousNonComment();
3489 if (PrevPrev && PrevPrev->is(tok::kw_const)) {
3496 if (PreviousNonComment->isTypeOrIdentifier(LangOpts)) {
3507 // list. The parameters can be cv-qualified and contain references or
3512 unsigned StoredPosition = Tokens->getPosition();
3513 FormatToken *NextToken = Tokens->getNextToken();
3517 NextToken = Tokens->getNextToken();
3525 switch (NextToken->Tok.getKind()) {
3530 FormatTok = Tokens->setPosition(StoredPosition);
3545 FormatTok = Tokens->setPosition(StoredPosition);
3556 --OpenAngles;
3559 if (NextToken->isTypeName(LangOpts)) {
3560 FormatTok = Tokens->setPosition(StoredPosition);
3568 FormatTok = Tokens->setPosition(StoredPosition);
3581 assert(FormatTok->getPreviousNonComment() == RequiresToken);
3582 assert(RequiresToken->is(tok::kw_requires) && "'requires' expected");
3588 !RequiresToken->Previous ||
3589 RequiresToken->Previous->is(TT_RequiresExpressionLBrace);
3591 RequiresToken->setFinalizedType(InRequiresExpression
3600 FormatTok->Previous->ClosesRequiresClause = true;
3611 assert(FormatTok->getPreviousNonComment() == RequiresToken);
3612 assert(RequiresToken->is(tok::kw_requires) && "'requires' expected");
3614 RequiresToken->setFinalizedType(TT_RequiresExpression);
3616 if (FormatTok->is(tok::l_paren)) {
3617 FormatTok->setFinalizedType(TT_RequiresExpressionLParen);
3621 if (FormatTok->is(tok::l_brace)) {
3622 FormatTok->setFinalizedType(TT_RequiresExpressionLBrace);
3653 switch (FormatTok->Tok.getKind()) {
3674 case tok::semi:
3686 FormatTok->setFinalizedType(TT_BinaryOperator);
3732 if (FormatTok->isNot(tok::less))
3740 if (!FormatTok->Tok.getIdentifierInfo()) {
3750 assert(FormatTok->Previous);
3751 switch (FormatTok->Previous->Tok.getKind()) {
3765 if (FormatTok->is(tok::less)) {
3779 if (FormatTok->is(tok::kw_enum))
3785 if (Style.isJavaScript() && FormatTok->isOneOf(tok::colon, tok::question))
3789 if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal))
3794 if (FormatTok->isOneOf(tok::kw_class, tok::kw_struct))
3796 while (FormatTok->is(tok::l_square))
3801 while (FormatTok->Tok.getIdentifierInfo() ||
3802 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
3806 FormatTok->setFinalizedType(TT_VerilogDimensionedTypeName);
3809 while (FormatTok->is(tok::l_square))
3815 if (FormatTok->is(tok::l_paren))
3817 if (FormatTok->is(tok::identifier)) {
3821 if (IsCpp && FormatTok->is(tok::identifier))
3827 if (FormatTok->isNot(tok::l_brace))
3829 FormatTok->setFinalizedType(TT_EnumLBrace);
3830 FormatTok->setBlockKind(BK_Block);
3850 Line->Level += 1;
3854 Line->Level -= 1;
3856 if (FormatTok->is(tok::semi))
3875 if (FormatTok->is(tok::semi))
3893 StoredPosition = Tokens->getPosition();
3896 ~ScopedTokenPosition() { Tokens->setPosition(StoredPosition); }
3904 FormatToken *Tok = Tokens->getNextToken();
3906 if (Tok->isNot(tok::l_square))
3910 while (Tok->isNot(tok::eof)) {
3911 if (Tok->is(tok::r_square))
3913 Tok = Tokens->getNextToken();
3915 if (Tok->is(tok::eof))
3917 Tok = Tokens->getNextToken();
3918 if (Tok->isNot(tok::r_square))
3920 Tok = Tokens->getNextToken();
3921 if (Tok->is(tok::semi))
3927 assert(FormatTok->is(tok::l_brace));
3933 unsigned StoredPosition = Tokens->getPosition();
3935 FormatToken *Tok = Tokens->getNextToken();
3936 while (Tok->isNot(tok::eof)) {
3937 if (Tok->is(tok::r_brace))
3939 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
3945 Tok = Tokens->getNextToken();
3947 FormatTok = Tokens->setPosition(StoredPosition);
3960 ++Line->Level;
3964 if (FormatTok->is(tok::l_brace)) {
3968 } else if (FormatTok->is(tok::l_paren)) {
3970 } else if (FormatTok->is(tok::comma)) {
3973 } else if (FormatTok->is(tok::semi)) {
3977 } else if (FormatTok->is(tok::r_brace)) {
3988 --Line->Level;
3999 return Tok->is(tok::identifier) && Tok->TokenText != Tok->TokenText.upper();
4005 // it is often token-pasted.
4007 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
4009 FormatTok->isAttribute() ||
4011 FormatTok->isOneOf(tok::period, tok::comma))) {
4013 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
4019 if (FormatTok->is(tok::l_brace)) {
4024 if (FormatTok->is(tok::l_square) && handleCppAttributes())
4028 switch (FormatTok->Tok.getKind()) {
4033 Previous->Previous == &InitialToken) {
4042 Previous->is(tok::identifier) && Previous->isNot(TT_AttributeMacro)) {
4051 assert(FormatTok->is(tok::l_brace));
4052 const auto *Prev = FormatTok->getPreviousNonComment();
4054 return Prev != ClassName && Prev->is(tok::identifier) &&
4055 Prev->isNot(Keywords.kw_final) && tryToParseBracedList();
4058 if (FormatTok->isOneOf(tok::colon, tok::less)) {
4061 if (FormatTok->is(tok::less))
4063 else if (FormatTok->is(tok::greater))
4064 --AngleNestingLevel;
4067 if (FormatTok->is(tok::colon)) {
4069 } else if (FormatTok->is(tok::identifier) &&
4070 FormatTok->Previous->is(tok::coloncolon)) {
4072 } else if (FormatTok->is(tok::l_paren) &&
4073 IsNonMacroIdentifier(FormatTok->Previous)) {
4077 if (FormatTok->is(tok::l_brace)) {
4084 if (FormatTok->is(tok::l_square)) {
4085 FormatToken *Previous = FormatTok->Previous;
4086 if (!Previous || (Previous->isNot(tok::r_paren) &&
4087 !Previous->isTypeOrIdentifier(LangOpts))) {
4097 if (FormatTok->is(tok::semi))
4099 if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)) {
4110 [](const FormatToken &RecordTok) -> std::pair<TokenType, TokenType> {
4123 if (FormatTok->is(tok::l_brace)) {
4127 FormatTok->setFinalizedType(OpenBraceType);
4145 assert(FormatTok->isOneOf(tok::l_paren, tok::identifier) &&
4148 if (FormatTok->is(tok::semi)) {
4152 } else if (FormatTok->is(tok::l_brace)) {
4165 assert(FormatTok->is(tok::less) && "'<' expected.");
4169 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
4170 FormatTok->isObjCAtKeyword(tok::objc_end)) {
4173 } while (!eof() && FormatTok->isNot(tok::greater));
4179 if (FormatTok->isObjCAtKeyword(tok::objc_end)) {
4184 if (FormatTok->is(tok::l_brace)) {
4188 } else if (FormatTok->is(tok::r_brace)) {
4192 } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
4202 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
4203 FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
4209 if (FormatTok->is(tok::less))
4211 if (FormatTok->is(tok::colon)) {
4215 if (FormatTok->is(tok::less))
4217 } else if (FormatTok->is(tok::l_paren)) {
4222 if (FormatTok->is(tok::less))
4225 if (FormatTok->is(tok::l_brace)) {
4239 assert(FormatTok->is(tok::less));
4251 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
4252 FormatTok->isObjCAtKeyword(tok::objc_end)) {
4255 if (FormatTok->is(tok::less)) {
4257 } else if (FormatTok->is(tok::greater)) {
4259 --NumOpenAngles;
4268 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
4271 if (FormatTok->is(tok::l_paren)) {
4278 // - (int)someMethod;
4283 if (FormatTok->is(tok::less))
4287 if (FormatTok->is(tok::semi)) {
4299 bool IsImport = FormatTok->is(Keywords.kw_import);
4300 assert(IsImport || FormatTok->is(tok::kw_export));
4304 if (FormatTok->is(tok::kw_default))
4308 // get parsed as free-standing JS functions, i.e. do not require a trailing
4310 if (FormatTok->is(Keywords.kw_async))
4312 if (FormatTok->is(Keywords.kw_function)) {
4321 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
4322 !FormatTok->isStringLiteral() &&
4323 !(FormatTok->is(Keywords.kw_type) &&
4324 Tokens->peekNextToken()->isOneOf(tok::l_brace, tok::star))) {
4329 if (FormatTok->is(tok::semi))
4331 if (Line->Tokens.empty()) {
4336 if (FormatTok->is(tok::l_brace)) {
4337 FormatTok->setBlockKind(BK_Block);
4348 if (FormatTok->is(tok::l_paren))
4350 if (FormatTok->is(tok::semi))
4358 if (FormatTok->isOneOf(tok::star, tok::period, tok::periodstar,
4362 } else if (FormatTok->is(tok::l_square)) {
4371 if (FormatTok->isNot(tok::at))
4375 if (FormatTok->is(tok::at))
4377 switch (FormatTok->Tok.getKind()) {
4393 if (FormatTok->is(Keywords.kw_clocking)) {
4398 if (FormatTok->is(tok::semi))
4400 } else if (FormatTok->isOneOf(tok::kw_case, Keywords.kw_casex,
4406 if (FormatTok->is(tok::l_paren)) {
4407 FormatTok->setFinalizedType(TT_ConditionLParen);
4410 if (FormatTok->isOneOf(Keywords.kw_inside, Keywords.kw_matches))
4419 if (FormatTok->is(tok::l_square)) {
4420 auto Prev = FormatTok->getPreviousNonComment();
4422 Prev->setFinalizedType(TT_VerilogDimensionedTypeName);
4425 FormatTok->isOneOf(Keywords.kw_automatic, tok::kw_static)) {
4434 Line->IsContinuation = true;
4438 while (FormatTok->is(Keywords.kw_import)) {
4442 if (FormatTok->is(tok::semi))
4447 if (FormatTok->is(Keywords.kw_verilogHash)) {
4450 if (FormatTok->is(tok::l_paren)) {
4451 FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4455 if (FormatTok->is(tok::l_paren)) {
4457 FormatTok->setFinalizedType(TT_VerilogMultiLineListLParen);
4462 if (FormatTok->is(Keywords.kw_extends)) {
4466 if (FormatTok->is(tok::l_paren))
4469 if (FormatTok->is(Keywords.kw_implements)) {
4474 } while (FormatTok->is(tok::comma));
4478 if (FormatTok->is(tok::at)) {
4483 if (FormatTok->is(tok::semi))
4492 assert(FormatTok->is(Keywords.kw_table));
4496 auto InitialLevel = Line->Level++;
4500 if (Tok->is(tok::semi))
4502 else if (Tok->isOneOf(tok::star, tok::colon, tok::question, tok::minus))
4503 Tok->setFinalizedType(TT_VerilogTableItem);
4505 Line->Level = InitialLevel;
4506 nextToken(/*LevelDifference=*/-1);
4516 auto OrigLevel = Line->Level;
4517 auto FirstLine = CurrentLines->size();
4518 if (Line->Level == 0 || (Line->InPPDirective && Line->Level <= 1))
4519 ++Line->Level;
4521 --Line->Level;
4525 if (CurrentLines->size() > FirstLine)
4527 Line->Level = OrigLevel;
4532 if (N.Tok->MacroCtx)
4542 if (Line->Tokens.empty())
4556 Line->MatchingOpeningBlockLineIndex != UnwrappedLine::kInvalidIndex &&
4564 Reconstruct.emplace(Line->Level, Unexpanded);
4565 Reconstruct->addLine(*Line);
4572 if (Reconstruct->finished()) {
4581 ExpandedLines[Reconstructed.Tokens.begin()->Tok] = CurrentExpandedLines;
4590 CurrentLines->push_back(std::move(*Line));
4592 Line->Tokens.clear();
4593 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
4594 Line->FirstStartColumn = 0;
4595 Line->IsContinuation = false;
4596 Line->SeenDecltypeAuto = false;
4599 --Line->Level;
4601 CurrentLines->append(
4607 FormatTok->Previous = nullptr;
4610 bool UnwrappedLineParser::eof() const { return FormatTok->is(tok::eof); }
4613 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
4619 static bool
4706 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
4714 if (Node.Tok->NewlinesBefore > 0)
4717 if (PreviousToken && PreviousToken->is(tok::l_brace))
4725 bool JustComments = Line->Tokens.empty();
4729 // Additional fine-grained breaking of line comment sections is controlled
4735 Tok->ContinuesLineCommentSection =
4737 if (isOnNewLine(*Tok) && JustComments && !Tok->ContinuesLineCommentSection)
4756 FormatTok->Previous = Previous;
4765 FormatTok->Tok.setKind(tok::r_brace);
4797 for (unsigned i = Comments.size() - 1; i > 0; --i) {
4798 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
4807 FormatTok->ContinuesLineCommentSection = false;
4809 FormatTok->ContinuesLineCommentSection =
4812 if (!FormatTok->ContinuesLineCommentSection &&
4813 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
4828 FormatTok = Tokens->getNextToken();
4830 while (FormatTok->isOneOf(TT_ConflictStart, TT_ConflictEnd,
4832 if (FormatTok->is(TT_ConflictStart))
4834 else if (FormatTok->is(TT_ConflictAlternative))
4836 else if (FormatTok->is(TT_ConflictEnd))
4838 FormatTok = Tokens->getNextToken();
4839 FormatTok->MustBreakBefore = true;
4840 FormatTok->MustBreakBeforeFinalized = true;
4859 PreviousWasComment = FormatTok->is(tok::comment);
4861 while (!Line->InPPDirective && FormatTok->is(tok::hash) &&
4863 Keywords.isVerilogPPDirective(*Tokens->peekNextToken())) &&
4869 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
4872 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
4873 "LevelDifference makes Line->Level negative");
4874 Line->Level += LevelDifference;
4880 Line->Level += PPBranchLevel;
4882 assert(Line->Level >= Line->UnbracedBodyLevel);
4883 Line->Level -= Line->UnbracedBodyLevel;
4886 PreviousWasComment = FormatTok->is(tok::comment);
4892 !Line->InPPDirective) {
4896 if (FormatTok->is(tok::identifier) &&
4897 Macros.defined(FormatTok->TokenText) &&
4899 !Line->InPPDirective) {
4901 unsigned Position = Tokens->getPosition();
4912 assert(Line->Tokens.front().Tok == ID);
4919 llvm::dbgs() << "Macro call: " << ID->TokenText << "(";
4924 llvm::dbgs() << T->TokenText << " ";
4929 if (Macros.objectLike(ID->TokenText) && Args &&
4930 !Macros.hasArity(ID->TokenText, Args->size())) {
4932 // - object-like, but we got argumnets, or
4933 // - overloaded to be both object-like and function-like, but none of
4934 // the function-like arities match the number of arguments.
4935 // Thus, expand as object-like macro.
4937 << "Macro \"" << ID->TokenText
4938 << "\" not overloaded for arity " << Args->size()
4939 << "or not function-like, using object-like overload.");
4941 UnexpandedLine->Tokens.resize(1);
4942 Tokens->setPosition(Position);
4944 assert(!Args && Macros.objectLike(ID->TokenText));
4946 if ((!Args && Macros.objectLike(ID->TokenText)) ||
4947 (Args && Macros.hasArity(ID->TokenText, Args->size()))) {
4954 FormatTok = Tokens->insertTokens(Expansion);
4959 llvm::dbgs() << T->TokenText << " ";
4964 llvm::dbgs() << "Did not expand macro \"" << ID->TokenText
4967 llvm::dbgs() << "with " << Args->size();
4972 Tokens->setPosition(Position);
4977 if (FormatTok->isNot(tok::comment)) {
4995 Into.push_back(I->Tok);
4996 for (const auto &Child : I->Children)
5005 assert(Line->Tokens.empty());
5007 if (FormatTok->isNot(tok::l_paren))
5009 unsigned Position = Tokens->getPosition();
5013 auto ArgStart = std::prev(Line->Tokens.end());
5017 switch (FormatTok->Tok.getKind()) {
5024 --Parens;
5028 Args->push_back({});
5029 pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
5038 Args->push_back({});
5039 pushTokens(std::next(ArgStart), Line->Tokens.end(), Args->back());
5041 ArgStart = std::prev(Line->Tokens.end());
5049 Line->Tokens.resize(1);
5050 Tokens->setPosition(Position);
5056 Line->Tokens.push_back(UnwrappedLineNode(Tok));
5058 Line->Tokens.back().Tok->MustBreakBefore = true;
5059 Line->Tokens.back().Tok->MustBreakBeforeFinalized = true;