[clang] 7cb0bc8 - [clang-format] Handle more cases for RemoveBracesLLVM

via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 13 12:10:42 PDT 2022


Author: owenca
Date: 2022-06-13T12:10:35-07:00
New Revision: 7cb0bc8abf6efd7e25b0091a7306de23c3a0f314

URL: https://github.com/llvm/llvm-project/commit/7cb0bc8abf6efd7e25b0091a7306de23c3a0f314
DIFF: https://github.com/llvm/llvm-project/commit/7cb0bc8abf6efd7e25b0091a7306de23c3a0f314.diff

LOG: [clang-format] Handle more cases for RemoveBracesLLVM

Call mightFitOneOneline() on the line before the closing brace only
if it is at the level of the block.

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

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 2721949794195..77a59d8585091 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -844,7 +844,7 @@ FormatToken *UnwrappedLineParser::parseBlock(
 
   size_t PPStartHash = computePPHash();
 
-  unsigned InitialLevel = Line->Level;
+  const unsigned InitialLevel = Line->Level;
   nextToken(/*LevelDifference=*/AddLevels);
 
   // Bail out if there are too many levels. Otherwise, the stack might overflow.
@@ -905,7 +905,8 @@ FormatToken *UnwrappedLineParser::parseBlock(
         return false;
     }
     assert(!CurrentLines->empty());
-    if (!mightFitOnOneLine(CurrentLines->back()))
+    auto &LastLine = CurrentLines->back();
+    if (LastLine.Level == InitialLevel + 1 && !mightFitOnOneLine(LastLine))
       return false;
     if (Tok->is(TT_ElseLBrace))
       return true;

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 27984ecb0fa53..972f3f195d856 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25784,6 +25784,25 @@ TEST_F(FormatTest, RemoveBraces) {
                "}",
                Style);
 
+  verifyFormat("if (a)\n"
+               "  b;\n"
+               "else if (c) {\n"
+               "  d;\n"
+               "  e;\n"
+               "} else\n"
+               "  f = g(foo, bar,\n"
+               "        baz);",
+               "if (a)\n"
+               "  b;\n"
+               "else {\n"
+               "  if (c) {\n"
+               "    d;\n"
+               "    e;\n"
+               "  } else\n"
+               "    f = g(foo, bar, baz);\n"
+               "}",
+               Style);
+
   Style.ColumnLimit = 0;
   verifyFormat("if (a)\n"
                "  b234567890223456789032345678904234567890 = "


        


More information about the cfe-commits mailing list