[clang] 91b5d50 - Revert "[clang-format] Link the braces of a block in UnwrappedLineParser"

Roman Lebedev via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 10 05:36:50 PST 2022


Reminder to please always mention the reason for the revert in the
commit message.

On Sat, Dec 10, 2022 at 1:32 PM Owen Pan via cfe-commits
<cfe-commits at lists.llvm.org> wrote:
>
>
> Author: Owen Pan
> Date: 2022-12-10T02:31:53-08:00
> New Revision: 91b5d508e350a26028b7fc1397f21334e49dda5d
>
> URL: https://github.com/llvm/llvm-project/commit/91b5d508e350a26028b7fc1397f21334e49dda5d
> DIFF: https://github.com/llvm/llvm-project/commit/91b5d508e350a26028b7fc1397f21334e49dda5d.diff
>
> LOG: Revert "[clang-format] Link the braces of a block in UnwrappedLineParser"
>
> This reverts commit e33243c950ac40d027ad8facbf7ccf0624604a16 but
> keeps the added test case and also adds another test case.
>
> Fixes #59417.
>
> Differential Revision: https://reviews.llvm.org/D139760
>
> Added:
>
>
> Modified:
>     clang/lib/Format/UnwrappedLineParser.cpp
>     clang/unittests/Format/BracesRemoverTest.cpp
>
> Removed:
>
>
>
> ################################################################################
> diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
> index df8cff6748b17..3be4ef7ca6a1a 100644
> --- a/clang/lib/Format/UnwrappedLineParser.cpp
> +++ b/clang/lib/Format/UnwrappedLineParser.cpp
> @@ -934,9 +934,6 @@ FormatToken *UnwrappedLineParser::parseBlock(
>      return IfLBrace;
>    }
>
> -  Tok->MatchingParen = FormatTok;
> -  FormatTok->MatchingParen = Tok;
> -
>    const bool IsFunctionRBrace =
>        FormatTok->is(tok::r_brace) && Tok->is(TT_FunctionLBrace);
>
> @@ -970,7 +967,10 @@ FormatToken *UnwrappedLineParser::parseBlock(
>      }
>      return mightFitOnOneLine((*CurrentLines)[Index], Tok);
>    };
> -  Tok->Optional = RemoveBraces();
> +  if (RemoveBraces()) {
> +    Tok->MatchingParen = FormatTok;
> +    FormatTok->MatchingParen = Tok;
> +  }
>
>    size_t PPEndHash = computePPHash();
>
> @@ -2707,20 +2707,10 @@ static void markOptionalBraces(FormatToken *LeftBrace) {
>
>    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 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
>
>    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 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
>                           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 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
>
>    assert(!NestedTooDeep.empty());
>    KeepElseBraces = KeepElseBraces ||
> -                   (ElseLeftBrace && !ElseLeftBrace->Optional) ||
> +                   (ElseLeftBrace && !ElseLeftBrace->MatchingParen) ||
>                     NestedTooDeep.back();
>
>    NestedTooDeep.pop_back();
> @@ -2864,11 +2854,17 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
>    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 @@ void UnwrappedLineParser::parseLoopBody(bool KeepBraces, bool WrapRightBrace) {
>        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
> index dea551c979397..57cf40d4c9e95 100644
> --- a/clang/unittests/Format/BracesRemoverTest.cpp
> +++ b/clang/unittests/Format/BracesRemoverTest.cpp
> @@ -683,6 +683,41 @@ TEST_F(BracesRemoverTest, RemoveBraces) {
>                 "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"
>
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


More information about the cfe-commits mailing list