]> granicus.if.org Git - clang/commitdiff
clang-format: Remove empty lines at the beginning of blocks.
authorDaniel Jasper <djasper@google.com>
Fri, 21 Mar 2014 12:58:53 +0000 (12:58 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 21 Mar 2014 12:58:53 +0000 (12:58 +0000)
They very rarely aid readability.

Formatting:
  void f() {

    if (a) {

      f();

    }

  }

Now leads to:
  void f() {
    if (a) {
      f();
    }
  }

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204460 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index bb2dc47f6630f57800047e2879f64ad7cd4d4fd1..e3e959ef33522461fedc8beb1389f4e84300e9c2 100644 (file)
@@ -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)
index e1b72d9a99f7d7e816baa0ad13d8cc87b0f88d6a..42b9e856e9772d35effad346d3f10172610f9f00 100644 (file)
@@ -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"