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

Marek Kurdej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 25 03:03:38 PDT 2022


curdeius created this revision.
curdeius added reviewers: MyDeveloperDay, HazardyKnusperkeks, owenpan.
Herald added a project: All.
curdeius requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fixes https://github.com/llvm/llvm-project/issues/54536.


Repository:
  rG LLVM Github Monorepo

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
@@ -67,6 +67,25 @@
     EXPECT_EQ(ExpectedCode, Result) << "Test failed. Formatted:\n" << Result;
   }
 
+  static void _verifyFormatNoInverse(const char *File, int Line,
+                                     llvm::StringRef Code,
+                                     const FormatStyle &Style = getLLVMStyle(),
+                                     llvm::StringRef ExpectedCode = "") {
+    ::testing::ScopedTrace t(File, Line, ::testing::Message() << Code.str());
+    bool HasOriginalCode = true;
+    if (ExpectedCode == "") {
+      ExpectedCode = Code;
+      HasOriginalCode = false;
+    }
+
+    EXPECT_EQ(ExpectedCode, separateDefinitionBlocks(ExpectedCode, Style))
+        << "Expected code is not stable";
+    std::string CodeToFormat =
+        HasOriginalCode ? Code.str() : removeEmptyLines(Code);
+    std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
+    EXPECT_EQ(ExpectedCode, Result) << "Test failed. Formatted:\n" << Result;
+  }
+
   static std::string removeEmptyLines(llvm::StringRef Code) {
     std::string Result = "";
     for (auto Char : Code.str()) {
@@ -83,6 +102,8 @@
 };
 
 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
+#define verifyFormatNoInverse(...)                                             \
+  _verifyFormatNoInverse(__FILE__, __LINE__, __VA_ARGS__)
 
 TEST_F(DefinitionBlockSeparatorTest, Basic) {
   FormatStyle Style = getLLVMStyle();
@@ -448,6 +469,32 @@
                Style);
 }
 
+TEST_F(DefinitionBlockSeparatorTest, TryBlocks) {
+  FormatStyle Style = getLLVMStyle();
+  Style.BreakBeforeBraces = FormatStyle::BS_Allman;
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormatNoInverse("void FunctionWithInternalTry()\n"
+                        "{\n"
+                        "  try\n"
+                        "  {\n"
+                        "    return;\n"
+                        "  }\n"
+                        "  catch (const std::exception &)\n"
+                        "  {\n"
+                        "  }\n"
+                        "}",
+                        Style);
+  verifyFormatNoInverse("void FunctionWithTryBlock()\n"
+                        "try\n"
+                        "{\n"
+                        "  return;\n"
+                        "}\n"
+                        "catch (const std::exception &)\n"
+                        "{\n"
+                        "}",
+                        Style);
+}
+
 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.418167.patch
Type: text/x-patch
Size: 3310 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220325/ed7051cb/attachment-0001.bin>


More information about the cfe-commits mailing list