diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -934,9 +934,6 @@ return IfLBrace; } - Tok->MatchingParen = FormatTok; - FormatTok->MatchingParen = Tok; - const bool IsFunctionRBrace = FormatTok->is(tok::r_brace) && Tok->is(TT_FunctionLBrace); @@ -970,7 +967,10 @@ } return mightFitOnOneLine((*CurrentLines)[Index], Tok); }; - Tok->Optional = RemoveBraces(); + if (RemoveBraces()) { + Tok->MatchingParen = FormatTok; + FormatTok->MatchingParen = Tok; + } size_t PPEndHash = computePPHash(); @@ -2707,20 +2707,10 @@ assert(RightBrace->is(tok::r_brace)); assert(RightBrace->MatchingParen == LeftBrace); + assert(LeftBrace->Optional == RightBrace->Optional); - RightBrace->Optional = LeftBrace->Optional; -} - -static void resetOptional(FormatToken *LeftBrace) { - if (!LeftBrace) - return; - - const auto *RightBrace = LeftBrace->MatchingParen; - const bool IsOptionalRightBrace = RightBrace && RightBrace->Optional; - assert(LeftBrace->Optional || !IsOptionalRightBrace); - - if (!IsOptionalRightBrace) - LeftBrace->Optional = false; + LeftBrace->Optional = true; + RightBrace->Optional = true; } void UnwrappedLineParser::handleAttributes() { @@ -2788,7 +2778,8 @@ if (Style.RemoveBracesLLVM) { assert(!NestedTooDeep.empty()); - KeepIfBraces = KeepIfBraces || (IfLeftBrace && !IfLeftBrace->Optional) || + KeepIfBraces = KeepIfBraces || + (IfLeftBrace && !IfLeftBrace->MatchingParen) || NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly || IfBlockKind == IfStmtKind::IfElseIf; } @@ -2819,9 +2810,8 @@ ElseBlockKind == IfStmtKind::IfElseIf; } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) { KeepElseBraces = true; - assert(ElseLeftBrace->Optional); assert(ElseLeftBrace->MatchingParen); - ElseLeftBrace->MatchingParen->Optional = true; + markOptionalBraces(ElseLeftBrace); } addUnwrappedLine(); } else if (FormatTok->is(tok::kw_if)) { @@ -2856,7 +2846,7 @@ assert(!NestedTooDeep.empty()); KeepElseBraces = KeepElseBraces || - (ElseLeftBrace && !ElseLeftBrace->Optional) || + (ElseLeftBrace && !ElseLeftBrace->MatchingParen) || NestedTooDeep.back(); NestedTooDeep.pop_back(); @@ -2864,11 +2854,17 @@ 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; @@ -3083,7 +3079,6 @@ if (!NestedTooDeep.back()) markOptionalBraces(LeftBrace); } - resetOptional(LeftBrace); if (WrapRightBrace) addUnwrappedLine(); } else { diff --git a/clang/unittests/Format/BracesRemoverTest.cpp b/clang/unittests/Format/BracesRemoverTest.cpp --- a/clang/unittests/Format/BracesRemoverTest.cpp +++ b/clang/unittests/Format/BracesRemoverTest.cpp @@ -683,6 +683,41 @@ "return a;", Style); + verifyFormat("if (a)\n" + "#ifdef FOO\n" + " if (b)\n" + " bar = c;\n" + " else\n" + "#endif\n" + " {\n" + " foo = d;\n" + "#ifdef FOO\n" + " bar = e;\n" + "#else\n" + " bar = f;\n" // FIXME: should be indented 1 more level. + "#endif\n" + " }\n" + "else\n" + " bar = g;", + "if (a)\n" + "#ifdef FOO\n" + " if (b)\n" + " bar = c;\n" + " else\n" + "#endif\n" + " {\n" + " foo = d;\n" + "#ifdef FOO\n" + " bar = e;\n" + "#else\n" + " bar = f;\n" + "#endif\n" + " }\n" + "else {\n" + " bar = g;\n" + "}", + Style); + Style.ColumnLimit = 65; verifyFormat("if (condition) {\n" " ff(Indices,\n"