]> granicus.if.org Git - clang/commitdiff
Let clang-format remove empty lines before "}".
authorDaniel Jasper <djasper@google.com>
Mon, 3 Jun 2013 16:16:41 +0000 (16:16 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 3 Jun 2013 16:16:41 +0000 (16:16 +0000)
These lines almost never aid readability.

Before:
void f() {
  int i;  // some variable

}

After:
void f() {
  int i;  // some variable
}

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

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

index aa42bdb643a2bd432dba8a8b9323f2bc4c6e6736..63bf09317e3ca23c56fe491a60d8f411cb15dd92 100644 (file)
@@ -1592,6 +1592,11 @@ private:
                         bool InPPDirective) {
     unsigned Newlines =
         std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
+    // Remove empty lines before "}" where applicable.
+    if (RootToken.is(tok::r_brace) &&
+        (!RootToken.Next ||
+         (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
+      Newlines = std::min(Newlines, 1u);
     if (Newlines == 0 && !RootToken.IsFirst)
       Newlines = 1;
 
index 61f43a02010092937b3d91f0d3e959a15323cb8a..83dea841b5ec7c5041431513c6e735ebe26182a7 100644 (file)
@@ -54,7 +54,7 @@ private:
                                 tok::question, tok::colon))
         return false;
       // If a && or || is found and interpreted as a binary operator, this set
-      // of angles is like part of something like "a < b && c > d". If the
+      // of angles is likely part of something like "a < b && c > d". If the
       // angles are inside an expression, the ||/&& might also be a binary
       // operator that was misinterpreted because we are parsing template
       // parameters.
index 2dea38f790818c4090fdbc8ee2fa10a84551a713..80072b5ab57c6af618a26c9eadf34601ad072a62 100644 (file)
@@ -192,6 +192,53 @@ TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
             format("int  a;\n  \n\n int b;", 9, 0, getLLVMStyle()));
 }
 
+TEST_F(FormatTest, RemovesEmptyLines) {
+  EXPECT_EQ("class C {\n"
+            "  int i;\n"
+            "};",
+            format("class C {\n"
+                   " int i;\n"
+                   "\n"
+                   "};"));
+
+  // Don't remove empty lines in more complex control statements.
+  EXPECT_EQ("void f() {\n"
+            "  if (a) {\n"
+            "    f();\n"
+            "\n"
+            "  } else if (b) {\n"
+            "    f();\n"
+            "  }\n"
+            "}",
+            format("void f() {\n"
+                   "  if (a) {\n"
+                   "    f();\n"
+                   "\n"
+                   "  } else if (b) {\n"
+                   "    f();\n"
+                   "\n"
+                   "  }\n"
+                   "\n"
+                   "}"));
+
+  // FIXME: This is slightly inconsistent.
+  EXPECT_EQ("namespace {\n"
+            "int i;\n"
+            "}",
+            format("namespace {\n"
+                   "int i;\n"
+                   "\n"
+                   "}"));
+  EXPECT_EQ("namespace {\n"
+            "int i;\n"
+            "\n"
+            "} // namespace",
+            format("namespace {\n"
+                   "int i;\n"
+                   "\n"
+                   "}  // namespace"));
+}
+
 TEST_F(FormatTest, ReformatsMovedLines) {
   EXPECT_EQ(
       "template <typename T> T *getFETokenInfo() const {\n"