[clang] b58616c - [clang-format] Fix SeparateDefinitionBlocks breaking up function-try-block.
Marek Kurdej via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 13 07:44:11 PDT 2022
Author: Marek Kurdej
Date: 2022-04-13T16:44:04+02:00
New Revision: b58616c2cdf70e075b887a66edf6bbc568e2ff99
URL: https://github.com/llvm/llvm-project/commit/b58616c2cdf70e075b887a66edf6bbc568e2ff99
DIFF: https://github.com/llvm/llvm-project/commit/b58616c2cdf70e075b887a66edf6bbc568e2ff99.diff
LOG: [clang-format] Fix SeparateDefinitionBlocks breaking up function-try-block.
Fixes https://github.com/llvm/llvm-project/issues/54536.
Reviewed By: HazardyKnusperkeks, owenpan
Differential Revision: https://reviews.llvm.org/D122468
Added:
Modified:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index c5594a6d66484..018a7ae3ee794 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2603,6 +2603,7 @@ void UnwrappedLineParser::parseTryCatch() {
nextToken();
}
NeedsUnwrappedLine = false;
+ Line->MustBeDeclaration = false;
CompoundStatementIndenter Indenter(this, Style, Line->Level);
parseBlock();
if (Style.BraceWrapping.BeforeCatch)
diff --git a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
index 53a3c57ad59fa..cb24cc921bc05 100644
--- a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
+++ b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp
@@ -43,7 +43,8 @@ class DefinitionBlockSeparatorTest : public ::testing::Test {
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 @@ class DefinitionBlockSeparatorTest : public ::testing::Test {
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
diff erence";
+ 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
diff erence";
+ }
std::string CodeToFormat =
HasOriginalCode ? Code.str() : removeEmptyLines(Code);
std::string Result = separateDefinitionBlocks(CodeToFormat, Style);
@@ -448,6 +451,32 @@ TEST_F(DefinitionBlockSeparatorTest, OpeningBracketOwnsLine) {
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;
More information about the cfe-commits
mailing list