Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -934,6 +934,9 @@ return IfLBrace; } + Tok->MatchingParen = FormatTok; + FormatTok->MatchingParen = Tok; + const bool IsFunctionRBrace = FormatTok->is(tok::r_brace) && Tok->is(TT_FunctionLBrace); @@ -967,10 +970,7 @@ } return mightFitOnOneLine((*CurrentLines)[Index], Tok); }; - if (RemoveBraces()) { - Tok->MatchingParen = FormatTok; - FormatTok->MatchingParen = Tok; - } + Tok->Optional = RemoveBraces(); size_t PPEndHash = computePPHash(); @@ -2707,10 +2707,16 @@ assert(RightBrace->is(tok::r_brace)); assert(RightBrace->MatchingParen == LeftBrace); - assert(LeftBrace->Optional == RightBrace->Optional); - LeftBrace->Optional = true; - RightBrace->Optional = true; + RightBrace->Optional = LeftBrace->Optional; +} + +static void resetOptional(FormatToken *LBrace) { + if (!LBrace) + return; + + const auto *RBrace = LBrace->MatchingParen; + LBrace->Optional = RBrace && RBrace->Optional; } void UnwrappedLineParser::handleAttributes() { @@ -2770,8 +2776,7 @@ if (Style.RemoveBracesLLVM) { assert(!NestedTooDeep.empty()); - KeepIfBraces = KeepIfBraces || - (IfLeftBrace && !IfLeftBrace->MatchingParen) || + KeepIfBraces = KeepIfBraces || (IfLeftBrace && !IfLeftBrace->Optional) || NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly || IfBlockKind == IfStmtKind::IfElseIf; } @@ -2802,8 +2807,9 @@ ElseBlockKind == IfStmtKind::IfElseIf; } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) { KeepElseBraces = true; + assert(ElseLeftBrace->Optional); assert(ElseLeftBrace->MatchingParen); - markOptionalBraces(ElseLeftBrace); + ElseLeftBrace->MatchingParen->Optional = true; } addUnwrappedLine(); } else if (FormatTok->is(tok::kw_if)) { @@ -2838,7 +2844,7 @@ assert(!NestedTooDeep.empty()); KeepElseBraces = KeepElseBraces || - (ElseLeftBrace && !ElseLeftBrace->MatchingParen) || + (ElseLeftBrace && !ElseLeftBrace->Optional) || NestedTooDeep.back(); NestedTooDeep.pop_back(); @@ -2846,17 +2852,11 @@ if (!KeepIfBraces && !KeepElseBraces) { markOptionalBraces(IfLeftBrace); markOptionalBraces(ElseLeftBrace); - } else if (IfLeftBrace) { - FormatToken *IfRightBrace = IfLeftBrace->MatchingParen; - if (IfRightBrace) { - assert(IfRightBrace->MatchingParen == IfLeftBrace); - assert(!IfLeftBrace->Optional); - assert(!IfRightBrace->Optional); - IfLeftBrace->MatchingParen = nullptr; - IfRightBrace->MatchingParen = nullptr; - } } + resetOptional(IfLeftBrace); + resetOptional(ElseLeftBrace); + if (IfKind) *IfKind = Kind; @@ -3071,6 +3071,7 @@ if (!NestedTooDeep.back()) markOptionalBraces(LeftBrace); } + resetOptional(LeftBrace); if (WrapRightBrace) addUnwrappedLine(); } else { Index: clang/unittests/Format/BracesRemoverTest.cpp =================================================================== --- clang/unittests/Format/BracesRemoverTest.cpp +++ clang/unittests/Format/BracesRemoverTest.cpp @@ -204,6 +204,20 @@ "while (j < 0) { ++j; }", Style); + verifyFormat("for (;;) {\n" + " for (;;)\n" + " for (;;)\n" + " a;\n" + "}", + "for (;;) {\n" + " for (;;) {\n" + " for (;;) {\n" + " a;\n" + " }\n" + " }\n" + "}", + Style); + verifyFormat("if (a)\n" " b; // comment\n" "else if (c)\n"