diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/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) diff --git a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp --- a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp +++ b/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;