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 @@ -43,7 +43,8 @@ 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 @@ 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 @@ 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;