[clang] a94aab1 - [clang-format] put non-empty catch block on one line with AllowShortBlocksOnASingleLine: Empty

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 15 15:07:04 PST 2021


Author: mydeveloperday
Date: 2021-12-15T23:06:52Z
New Revision: a94aab12a4e0bec6cd1eb676a3360fc1b5ac8eee

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

LOG: [clang-format] put non-empty catch block on one line with AllowShortBlocksOnASingleLine: Empty

https://github.com/llvm/llvm-project/issues/52715

Fixes #52715

`AllowShortBlocksOnASingleLine` seems to never be checked for "Empty" as such if its used it will be considered "Always" as we only ever check `AllowShortBlocksOnASingleLine != Never`

This impacts C++ as well as C# hence the slightly duplicated test.

Reviewed By: curdeius, jbcoe

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

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineFormatter.cpp
    clang/unittests/Format/FormatTest.cpp
    clang/unittests/Format/FormatTestCSharp.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 4c341aadbd017..a9beaef08e6e9 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -584,6 +584,9 @@ class LineJoiner {
                             Keywords.kw___except)) {
       if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
         return 0;
+      if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Empty &&
+          !I[1]->First->is(tok::r_brace))
+        return 0;
       // Don't merge when we can't except the case when
       // the control statement block is empty
       if (!Style.AllowShortIfStatementsOnASingleLine &&

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 271acf97c3f1e..f54139227e871 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22858,6 +22858,23 @@ TEST_F(FormatTest, CoroutineCoReturn) {
   verifyFormat("co_return co_yield foo();");
 }
 
+TEST_F(FormatTest, EmptyShortBlock) {
+  auto Style = getLLVMStyle();
+  Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
+
+  verifyFormat("try {\n"
+               "  doA();\n"
+               "} catch (Exception &e) {\n"
+               "  e.printStackTrace();\n"
+               "}\n",
+               Style);
+
+  verifyFormat("try {\n"
+               "  doA();\n"
+               "} catch (Exception &e) {}\n",
+               Style);
+}
+
 } // namespace
 } // namespace format
 } // namespace clang

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index 0536903ef4f10..78547e07d2150 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -1379,5 +1379,22 @@ TEST_F(FormatTestCSharp, SwitchExpression) {
                Style);
 }
 
+TEST_F(FormatTestCSharp, EmptyShortBlock) {
+  auto Style = getLLVMStyle();
+  Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
+
+  verifyFormat("try {\n"
+               "  doA();\n"
+               "} catch (Exception e) {\n"
+               "  e.printStackTrace();\n"
+               "}\n",
+               Style);
+
+  verifyFormat("try {\n"
+               "  doA();\n"
+               "} catch (Exception e) {}\n",
+               Style);
+}
+
 } // namespace format
 } // end namespace clang


        


More information about the cfe-commits mailing list