Author: sstwcw
Date: 2026-05-31T20:52:59Z
New Revision: 0631ac764a565ed13283e3c7b70a746d0a8018cc

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

LOG: [clang-format] Remove the blank line in the function try block (#199086)

old with config `{SeparateDefinitionBlocks: Always}`

```C++
void foo() try {
  // do something
} catch (const std::exception &e) {

  // handle exception
}
```

new

```C++
void foo() try {
  // do something
} catch (const std::exception &e) {
  // handle exception
}
```

Fixes #58183.

With the option `SeparateDefinitionBlocks` set, the program previously
added a blank line in the catch block. Now the problem is fixed.

Added: 
    

Modified: 
    clang/lib/Format/DefinitionBlockSeparator.cpp
    clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/DefinitionBlockSeparator.cpp 
b/clang/lib/Format/DefinitionBlockSeparator.cpp
index fd1ae8c4f600e..d16f8ddd5a2ec 100644
--- a/clang/lib/Format/DefinitionBlockSeparator.cpp
+++ b/clang/lib/Format/DefinitionBlockSeparator.cpp
@@ -202,6 +202,9 @@ void DefinitionBlockSeparator::separateBlocks(
     } else if (CurrentLine->First->closesScope()) {
       if (OpeningLineIndex > Lines.size())
         continue;
+      // A function try block should be together.
+      if (CurrentLine->First->startsSequence(tok::r_brace, tok::kw_catch))
+        continue;
       // Handling the case that opening brace has its own line, with checking
       // whether the last line already had an opening brace to guard against
       // misrecognition.

diff  --git a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp 
b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
index 01336a8d37314..d2b43ca2d70aa 100644
--- a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -478,8 +478,24 @@ TEST_F(DefinitionBlockSeparatorTest, 
OpeningBracketOwnsLine) {
 
 TEST_F(DefinitionBlockSeparatorTest, TryBlocks) {
   FormatStyle Style = getLLVMStyle();
-  Style.BreakBeforeBraces = FormatStyle::BS_Allman;
   Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
+  verifyFormat("void foo() try {\n"
+               "  // do something\n"
+               "} catch (const std::exception &) {\n"
+               "  // handle exception\n"
+               "}",
+               Style, "", /*Inverse=*/false);
+  Style.BreakBeforeBraces = FormatStyle::BS_Allman;
+  verifyFormat("void foo()\n"
+               "try\n"
+               "{\n"
+               "  // do something\n"
+               "}\n"
+               "catch (const std::exception &)\n"
+               "{\n"
+               "  // handle exception\n"
+               "}",
+               Style, "", /*Inverse=*/false);
   verifyFormat("void FunctionWithInternalTry()\n"
                "{\n"
                "  try\n"


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to