From ce000e5010562b3bcf2ba60e3f40709cde3b6a70 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Fri, 19 Jan 2018 16:12:37 +0000 Subject: [PATCH] [clang-format] Fix shortening blocks in macros causing merged next line Summary: This patch addresses bug 36002, where a combination of options causes the line following a short block in macro to be merged with that macro. Reviewers: bkramer Reviewed By: bkramer Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D42298 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@322954 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UnwrappedLineFormatter.cpp | 12 +++++++++--- unittests/Format/FormatTest.cpp | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/Format/UnwrappedLineFormatter.cpp b/lib/Format/UnwrappedLineFormatter.cpp index 60dc1a7169..253f89da9d 100644 --- a/lib/Format/UnwrappedLineFormatter.cpp +++ b/lib/Format/UnwrappedLineFormatter.cpp @@ -304,9 +304,15 @@ private: if (TheLine->First->is(tok::l_brace) && TheLine->First == TheLine->Last && I != AnnotatedLines.begin() && I[-1]->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) { - return Style.AllowShortBlocksOnASingleLine - ? tryMergeSimpleBlock(I - 1, E, Limit) - : 0; + unsigned MergedLines = 0; + if (Style.AllowShortBlocksOnASingleLine) { + MergedLines = tryMergeSimpleBlock(I - 1, E, Limit); + // If we managed to merge the block, discard the first merged line + // since we are merging starting from I. + if (MergedLines > 0) + --MergedLines; + } + return MergedLines; } // Try to merge a block with left brace wrapped that wasn't yet covered if (TheLine->Last->is(tok::l_brace)) { diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 61973c621c..ac5184ef02 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -588,6 +588,23 @@ TEST_F(FormatTest, FormatShortBracedStatements) { AllowSimpleBracedStatements); } +TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { + FormatStyle Style = getLLVMStyleWithColumns(60); + Style.AllowShortBlocksOnASingleLine = true; + Style.AllowShortIfStatementsOnASingleLine = true; + Style.BreakBeforeBraces = FormatStyle::BS_Allman; + EXPECT_EQ("#define A \\\n" + " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" + " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" + "X;", + format("#define A \\\n" + " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" + " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" + " }\n" + "X;", + Style)); +} + TEST_F(FormatTest, ParseIfElse) { verifyFormat("if (true)\n" " if (true)\n" -- 2.40.0