From 55b08e769f05987942c307fde8eb168df01e504f Mon Sep 17 00:00:00 2001 From: Daniel Jasper <djasper@google.com> Date: Wed, 16 Jan 2013 07:02:34 +0000 Subject: [PATCH] Remove errors were if statements were incorrectly put on a single line. Before: if (a) // This comment confused clang-format f(); After: if (a) // No more confusion f(); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172600 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 7 +++++++ unittests/Format/FormatTest.cpp | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 445a4064b46..9477f5e014d 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1480,6 +1480,11 @@ private: unsigned Length = 0; if (!fitsIntoLimit(I->First, Limit, &Length)) return false; + + // We can never merge stuff if there are trailing line comments. + if (I->Last->Type == TT_LineComment) + return true; + if (Limit == Length) return true; // Couldn't fit a space. Limit -= Length + 1; // One space. @@ -1516,6 +1521,8 @@ private: if (!Style.AllowShortIfStatementsOnASingleLine) return; AnnotatedLine &Line = *I; + if (Line.Last->isNot(tok::r_paren)) + return; if (!fitsIntoLimit((I + 1)->First, Limit)) return; if ((I + 1)->First.is(tok::kw_if) || (I + 1)->First.Type == TT_LineComment) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index eb00d017ff3..ac61af3bfae 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -145,7 +145,17 @@ TEST_F(FormatTest, FormatIfWithoutCompountStatement) { verifyFormat("if (a) return;", getGoogleStyleWithColumns(14)); verifyFormat("if (a)\n return;", getGoogleStyleWithColumns(13)); verifyFormat("if (aaaaaaaaa)\n" - " return;", getGoogleStyleWithColumns(14)); + " return;", getGoogleStyleWithColumns(14)); + verifyGoogleFormat("if (a) // Can't merge this\n" + " f();\n"); + verifyGoogleFormat("if (a) /* still don't merge */\n" + " f();"); + verifyGoogleFormat("if (a) { // Never merge this\n" + " f();\n" + "}"); + verifyGoogleFormat("if (a) { /* Never merge this */\n" + " f();\n" + "}"); } TEST_F(FormatTest, ParseIfElse) { -- GitLab