[clang] 5221875 - [clang-format] Fix an invalid code generation in RemoveBracesLLVM

via cfe-commits cfe-commits at lists.llvm.org
Thu May 26 13:38:12 PDT 2022


Author: owenca
Date: 2022-05-26T13:38:04-07:00
New Revision: 5221875a957da927a889a705276d4e073ff737b1

URL: https://github.com/llvm/llvm-project/commit/5221875a957da927a889a705276d4e073ff737b1
DIFF: https://github.com/llvm/llvm-project/commit/5221875a957da927a889a705276d4e073ff737b1.diff

LOG: [clang-format] Fix an invalid code generation in RemoveBracesLLVM

Fixes #55706.

Differential Revision: https://reviews.llvm.org/D126438

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 0611e9eace476..fe57141407c0d 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2587,10 +2587,13 @@ FormatToken *UnwrappedLineParser::parseIfThenElse(IfStmtKind *IfKind,
       FormatTok->setFinalizedType(TT_ElseLBrace);
       ElseLeftBrace = FormatTok;
       CompoundStatementIndenter Indenter(this, Style, Line->Level);
-      if (parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
-                     /*MunchSemi=*/true,
-                     KeepElseBraces) == IfStmtKind::IfOnly) {
-        Kind = IfStmtKind::IfElseIf;
+      const IfStmtKind ElseBlockKind =
+          parseBlock(/*MustBeDeclaration=*/false, /*AddLevels=*/1u,
+                     /*MunchSemi=*/true, KeepElseBraces);
+      if ((ElseBlockKind == IfStmtKind::IfOnly ||
+           ElseBlockKind == IfStmtKind::IfElseIf) &&
+          FormatTok->is(tok::kw_else)) {
+        KeepElseBraces = true;
       }
       addUnwrappedLine();
     } else if (FormatTok->is(tok::kw_if)) {

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 44ef882fe7db4..c1257059c8649 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25385,6 +25385,30 @@ TEST_F(FormatTest, RemoveBraces) {
                "}",
                Style);
 
+  verifyFormat("if (a)\n"
+               "  if (b)\n"
+               "    c;\n"
+               "  else {\n"
+               "    if (d)\n"
+               "      e;\n"
+               "  }\n"
+               "else\n"
+               "  f;",
+               Style);
+
+  verifyFormat("if (a)\n"
+               "  if (b)\n"
+               "    c;\n"
+               "  else {\n"
+               "    if (d)\n"
+               "      e;\n"
+               "    else if (f)\n"
+               "      g;\n"
+               "  }\n"
+               "else\n"
+               "  h;",
+               Style);
+
   verifyFormat("if (a)\n"
                "  b;\n"
                "else if (c)\n"


        


More information about the cfe-commits mailing list