r204460 - clang-format: Remove empty lines at the beginning of blocks.
Daniel Jasper
djasper at google.com
Fri Mar 21 05:58:53 PDT 2014
Author: djasper
Date: Fri Mar 21 07:58:53 2014
New Revision: 204460
URL: http://llvm.org/viewvc/llvm-project?rev=204460&view=rev
Log:
clang-format: Remove empty lines at the beginning of blocks.
They very rarely aid readability.
Formatting:
void f() {
if (a) {
f();
}
}
Now leads to:
void f() {
if (a) {
f();
}
}
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=204460&r1=204459&r2=204460&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Mar 21 07:58:53 2014
@@ -888,6 +888,10 @@ private:
if (Newlines == 0 && !RootToken.IsFirst)
Newlines = 1;
+ // Remove empty lines after "{".
+ if (PreviousLine && PreviousLine->Last->is(tok::l_brace))
+ Newlines = 1;
+
// Insert extra new line before access specifiers.
if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) &&
RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=204460&r1=204459&r2=204460&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Mar 21 07:58:53 2014
@@ -174,6 +174,22 @@ TEST_F(FormatTest, RemovesEmptyLines) {
"\n"
"};"));
+ // Remove empty lines at the beginning and end of blocks.
+ EXPECT_EQ("void f() {\n"
+ " if (a) {\n"
+ " f();\n"
+ " }\n"
+ "}",
+ format("void f() {\n"
+ "\n"
+ " if (a) {\n"
+ "\n"
+ " f();\n"
+ "\n"
+ " }\n"
+ "\n"
+ "}"));
+
// Don't remove empty lines in more complex control statements.
EXPECT_EQ("void f() {\n"
" if (a) {\n"
More information about the cfe-commits
mailing list