Index: clang/lib/Format/UnwrappedLineParser.h =================================================================== --- clang/lib/Format/UnwrappedLineParser.h +++ clang/lib/Format/UnwrappedLineParser.h @@ -92,16 +92,18 @@ void reset(); void parseFile(); bool precededByCommentOrPPDirective() const; - bool parseLevel(const FormatToken *OpeningBrace, bool CanContainBracedList, - IfStmtKind *IfKind = nullptr, - TokenType NextLBracesType = TT_Unknown); + bool parseLevel(const FormatToken *OpeningBrace = nullptr, + bool CanContainBracedList = true, + TokenType NextLBracesType = TT_Unknown, + IfStmtKind *IfKind = nullptr); bool mightFitOnOneLine(UnwrappedLine &Line, const FormatToken *OpeningBrace = nullptr) const; - IfStmtKind parseBlock(bool MustBeDeclaration = false, unsigned AddLevels = 1u, - bool MunchSemi = true, bool KeepBraces = true, - bool UnindentWhitesmithsBraces = false, - bool CanContainBracedList = true, - TokenType NextLBracesType = TT_Unknown); + void parseBlock(bool MustBeDeclaration = false, unsigned AddLevels = 1u, + bool MunchSemi = true, bool KeepBraces = true, + IfStmtKind *IfKind = nullptr, + bool UnindentWhitesmithsBraces = false, + bool CanContainBracedList = true, + TokenType NextLBracesType = TT_Unknown); void parseChildBlock(bool CanContainBracedList = true, TokenType NextLBracesType = TT_Unknown); void parsePPDirective(); @@ -112,9 +114,9 @@ void parsePPEndIf(); void parsePPUnknown(); void readTokenWithJavaScriptASI(); - void parseStructuralElement(IfStmtKind *IfKind = nullptr, - bool IsTopLevel = false, + void parseStructuralElement(bool IsTopLevel = false, TokenType NextLBracesType = TT_Unknown, + IfStmtKind *IfKind = nullptr, bool *HasDoWhile = nullptr, bool *HasLabel = nullptr); bool tryToParseBracedList(); Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -395,7 +395,7 @@ if (Style.Language == FormatStyle::LK_TextProto) parseBracedList(); else - parseLevel(/*OpeningBrace=*/nullptr, /*CanContainBracedList=*/true); + parseLevel(); // Make sure to format the remaining tokens. // // LK_TextProto is special since its top-level is parsed as the body of a @@ -469,12 +469,13 @@ /// \param CanContainBracedList If the content can contain (at any level) a /// braced list. /// \param NextLBracesType The type for left brace found in this level. +/// \param IfKind The if statement kind in the level. /// \returns true if a simple block of if/else/for/while, or false otherwise. /// (A simple block has a single statement.) bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace, bool CanContainBracedList, - IfStmtKind *IfKind, - TokenType NextLBracesType) { + TokenType NextLBracesType, + IfStmtKind *IfKind) { auto NextLevelLBracesType = NextLBracesType == TT_CompoundRequirementLBrace ? TT_BracedListLBrace : TT_Unknown; @@ -484,6 +485,7 @@ bool HasLabel = false; unsigned StatementCount = 0; bool SwitchLabelEncountered = false; + do { if (FormatTok->getType() == TT_AttributeMacro) { nextToken(); @@ -495,9 +497,9 @@ else if (FormatTok->getType() == TT_MacroBlockEnd) kind = tok::r_brace; - auto ParseDefault = [this, OpeningBrace, IfKind, NextLevelLBracesType, + auto ParseDefault = [this, OpeningBrace, NextLevelLBracesType, IfKind, &HasDoWhile, &HasLabel, &StatementCount] { - parseStructuralElement(IfKind, !OpeningBrace, NextLevelLBracesType, + parseStructuralElement(!OpeningBrace, NextLevelLBracesType, IfKind, HasDoWhile ? nullptr : &HasDoWhile, HasLabel ? nullptr : &HasLabel); ++StatementCount; @@ -524,7 +526,7 @@ continue; } parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, - /*MunchSemi=*/true, /*KeepBraces=*/true, + /*MunchSemi=*/true, /*KeepBraces=*/true, /*IfKind=*/nullptr, /*UnindentWhitesmithsBraces=*/false, CanContainBracedList, NextLBracesType); ++StatementCount; @@ -593,6 +595,7 @@ break; } } while (!eof()); + return false; } @@ -815,10 +818,12 @@ return Line.Level * Style.IndentWidth + Length <= ColumnLimit; } -UnwrappedLineParser::IfStmtKind UnwrappedLineParser::parseBlock( - bool MustBeDeclaration, unsigned AddLevels, bool MunchSemi, bool KeepBraces, - bool UnindentWhitesmithsBraces, bool CanContainBracedList, - TokenType NextLBracesType) { +void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels, + bool MunchSemi, bool KeepBraces, + IfStmtKind *IfKind, + bool UnindentWhitesmithsBraces, + bool CanContainBracedList, + TokenType NextLBracesType) { assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) && "'{' or macro block token expected"); FormatToken *Tok = FormatTok; @@ -859,18 +864,17 @@ if (AddLevels > 0u && Style.BreakBeforeBraces != FormatStyle::BS_Whitesmiths) Line->Level += AddLevels; - IfStmtKind IfKind = IfStmtKind::NotIf; const bool SimpleBlock = - parseLevel(Tok, CanContainBracedList, &IfKind, NextLBracesType); + parseLevel(Tok, CanContainBracedList, NextLBracesType, IfKind); if (eof()) - return IfKind; + return; if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd) : !FormatTok->is(tok::r_brace)) { Line->Level = InitialLevel; FormatTok->setBlockKind(BK_Block); - return IfKind; + return; } auto RemoveBraces = [=]() mutable { @@ -935,8 +939,6 @@ CurrentLines->size() - 1; } } - - return IfKind; } static bool isGoogScope(const UnwrappedLine &Line) { @@ -1010,8 +1012,7 @@ ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, /*MustBeDeclaration=*/false); Line->Level += SkipIndent ? 0 : 1; - parseLevel(OpeningBrace, CanContainBracedList, /*IfKind=*/nullptr, - NextLBracesType); + parseLevel(OpeningBrace, CanContainBracedList, NextLBracesType); flushComments(isOnNewLine(*FormatTok)); Line->Level -= SkipIndent ? 0 : 1; } @@ -1414,9 +1415,9 @@ } } -void UnwrappedLineParser::parseStructuralElement(IfStmtKind *IfKind, - bool IsTopLevel, +void UnwrappedLineParser::parseStructuralElement(bool IsTopLevel, TokenType NextLBracesType, + IfStmtKind *IfKind, bool *HasDoWhile, bool *HasLabel) { if (Style.Language == FormatStyle::LK_TableGen && @@ -2543,8 +2544,7 @@ if (FormatTok->is(tok::kw_consteval)) { nextToken(); } else { - if (Style.RemoveBracesLLVM) - KeepIfBraces = KeepBraces; + KeepIfBraces = !Style.RemoveBracesLLVM || KeepBraces; if (FormatTok->isOneOf(tok::kw_constexpr, tok::identifier)) nextToken(); if (FormatTok->is(tok::l_paren)) @@ -2562,8 +2562,8 @@ FormatTok->setFinalizedType(TT_ControlStatementLBrace); IfLeftBrace = FormatTok; CompoundStatementIndenter Indenter(this, Style, Line->Level); - IfBlockKind = parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, - /*MunchSemi=*/true, KeepIfBraces); + parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, + /*MunchSemi=*/true, KeepIfBraces, &IfBlockKind); if (Style.BraceWrapping.BeforeElse) addUnwrappedLine(); else @@ -2595,9 +2595,9 @@ FormatTok->setFinalizedType(TT_ElseLBrace); ElseLeftBrace = FormatTok; CompoundStatementIndenter Indenter(this, Style, Line->Level); - const IfStmtKind ElseBlockKind = - parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, - /*MunchSemi=*/true, KeepElseBraces); + IfStmtKind ElseBlockKind; + parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u, + /*MunchSemi=*/true, KeepElseBraces, &ElseBlockKind); if ((ElseBlockKind == IfStmtKind::IfOnly || ElseBlockKind == IfStmtKind::IfElseIf) && FormatTok->is(tok::kw_else)) { @@ -2626,8 +2626,7 @@ parseUnbracedBody(/*CheckEOF=*/true); } } else { - if (Style.RemoveBracesLLVM) - KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse; + KeepIfBraces = KeepIfBraces || IfBlockKind == IfStmtKind::IfElse; if (NeedsUnwrappedLine) addUnwrappedLine(); } @@ -2796,7 +2795,8 @@ ++Line->Level; parseBlock(/*MustBeDeclaration=*/true, AddLevels, /*MunchSemi=*/true, - /*KeepBraces=*/true, ManageWhitesmithsBraces); + /*KeepBraces=*/true, /*IfKind=*/nullptr, + ManageWhitesmithsBraces); // Munch the semicolon after a namespace. This is more common than one would // think. Putting the semicolon into its own line is very ugly. @@ -3612,7 +3612,7 @@ } // Parse the class body after the enum's ";" if any. - parseLevel(OpeningBrace, /*CanContainBracedList=*/true); + parseLevel(OpeningBrace); nextToken(); --Line->Level; addUnwrappedLine();