[PATCH] D122468: [clang-format] Fix SeparateDefinitionBlocks breaking up function-try-block.

Marek Kurdej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 13 04:55:08 PDT 2022


curdeius updated this revision to Diff 422479.
curdeius marked 5 inline comments as done.
curdeius added a comment.

Address review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122468/new/

https://reviews.llvm.org/D122468

Files:
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/DefinitionBlockSeparatorTest.cpp


Index: clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
===================================================================
--- clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -43,7 +43,8 @@
 
   static void _verifyFormat(const char *File, int Line, llvm::StringRef Code,
                             const FormatStyle &Style = getLLVMStyle(),
-                            llvm::StringRef ExpectedCode = "") {
+                            llvm::StringRef ExpectedCode = "",
+                            bool Inverse = true) {
     ::testing::ScopedTrace t(File, Line, ::testing::Message() << Code.str());
     bool HasOriginalCode = true;
     if (ExpectedCode == "") {
@@ -51,16 +52,18 @@
       HasOriginalCode = false;
     }
 
-    FormatStyle InverseStyle = Style;
-    if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
-      InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
-    else
-      InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
     EXPECT_EQ(ExpectedCode, separateDefinitionBlocks(ExpectedCode, Style))
         << "Expected code is not stable";
-    EXPECT_NE(ExpectedCode,
-              separateDefinitionBlocks(ExpectedCode, InverseStyle))
-        << "Inverse formatting makes no difference";
+    if (Inverse) {
+      FormatStyle InverseStyle = Style;
+      if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always)
+        InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
+      else
+        InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+      EXPECT_NE(ExpectedCode,
+                separateDefinitionBlocks(ExpectedCode, InverseStyle))
+          << "Inverse formatting makes no difference";
+    }
     std::string CodeToFormat =
         HasOriginalCode ? Code.str() : removeEmptyLines(Code);
     std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
@@ -448,6 +451,32 @@
                Style);
 }
 
+TEST_F(DefinitionBlockSeparatorTest, TryBlocks) {
+  FormatStyle Style = getLLVMStyle();
+  Style.BreakBeforeBraces = FormatStyle::BS_Allman;
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("void FunctionWithInternalTry()\n"
+               "{\n"
+               "  try\n"
+               "  {\n"
+               "    return;\n"
+               "  }\n"
+               "  catch (const std::exception &)\n"
+               "  {\n"
+               "  }\n"
+               "}",
+               Style, "", /*Inverse=*/false);
+  verifyFormat("void FunctionWithTryBlock()\n"
+               "try\n"
+               "{\n"
+               "  return;\n"
+               "}\n"
+               "catch (const std::exception &)\n"
+               "{\n"
+               "}",
+               Style, "", /*Inverse=*/false);
+}
+
 TEST_F(DefinitionBlockSeparatorTest, Leave) {
   FormatStyle Style = getLLVMStyle();
   Style.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2603,6 +2603,7 @@
       nextToken();
     }
     NeedsUnwrappedLine = false;
+    Line->MustBeDeclaration = false;
     CompoundStatementIndenter Indenter(this, Style, Line->Level);
     parseBlock();
     if (Style.BraceWrapping.BeforeCatch)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122468.422479.patch
Type: text/x-patch
Size: 3458 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220413/70f1bb87/attachment-0001.bin>


More information about the cfe-commits mailing list