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 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 @@ 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;