From 1d55d15d7f23ee41436b23d2b3d179e881f8a883 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Mon, 19 Mar 2018 15:33:40 +0000 Subject: [PATCH] [clang-format] Remove empty lines before }[;] // comment Summary: This addresses bug 36766 and a FIXME in tests about empty lines before `}[;] // comment` lines. Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44631 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@327861 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UnwrappedLineFormatter.cpp | 6 +++++- unittests/Format/FormatTest.cpp | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/Format/UnwrappedLineFormatter.cpp b/lib/Format/UnwrappedLineFormatter.cpp index 2ce39fb04c..b0da0028bf 100644 --- a/lib/Format/UnwrappedLineFormatter.cpp +++ b/lib/Format/UnwrappedLineFormatter.cpp @@ -1133,8 +1133,12 @@ void UnwrappedLineFormatter::formatFirstToken(const AnnotatedLine &Line, std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); // Remove empty lines before "}" where applicable. if (RootToken.is(tok::r_brace) && + // Look for "}", "} // comment", "};" or "}; // comment". (!RootToken.Next || - (RootToken.Next->is(tok::semi) && !RootToken.Next->Next))) + (RootToken.Next->is(tok::comment) && !RootToken.Next->Next) || + (RootToken.Next->is(tok::semi) && + (!RootToken.Next->Next || (RootToken.Next->Next->is(tok::comment) && + !RootToken.Next->Next->Next))))) Newlines = std::min(Newlines, 1u); // Remove empty lines at the start of nested blocks (lambdas/arrow functions) if (PreviousLine == nullptr && Line.Level > 0) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index a9fbe4b6fc..bdb3fbe483 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -276,7 +276,6 @@ TEST_F(FormatTest, RemovesEmptyLines) { "\n" "}")); - // FIXME: This is slightly inconsistent. FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); LLVMWithNoNamespaceFix.FixNamespaceComments = false; EXPECT_EQ("namespace {\n" @@ -295,12 +294,25 @@ TEST_F(FormatTest, RemovesEmptyLines) { "}")); EXPECT_EQ("namespace {\n" "int i;\n" - "\n" + "};", + format("namespace {\n" + "int i;\n" + "\n" + "};")); + EXPECT_EQ("namespace {\n" + "int i;\n" "} // namespace", format("namespace {\n" "int i;\n" "\n" "} // namespace")); + EXPECT_EQ("namespace {\n" + "int i;\n" + "}; // namespace", + format("namespace {\n" + "int i;\n" + "\n" + "}; // namespace")); FormatStyle Style = getLLVMStyle(); Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; -- 2.40.0