r176086 - Only keep empty lines in unwrapped lines if they preceed a line comment.

Daniel Jasper djasper at google.com
Tue Feb 26 05:10:34 PST 2013


Author: djasper
Date: Tue Feb 26 07:10:34 2013
New Revision: 176086

URL: http://llvm.org/viewvc/llvm-project?rev=176086&view=rev
Log:
Only keep empty lines in unwrapped lines if they preceed a line comment.

Empty lines followed by line comments are often used to highlight the
comment. Empty lines somewhere else are usually left over from manual or
automatic formatting and should probably be removed.

Before (clang-format would keep):
S s = {
  a,

  b
};

After:
S s = { a, b };

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/TokenAnnotator.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=176086&r1=176085&r2=176086&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Feb 26 07:10:34 2013
@@ -518,9 +518,11 @@ private:
         State.Stack.back().BreakBeforeParameter = false;
 
       if (!DryRun) {
-        unsigned NewLines =
-            std::max(1u, std::min(Current.FormatTok.NewlinesBefore,
-                                  Style.MaxEmptyLinesToKeep + 1));
+        unsigned NewLines = 1;
+        if (Current.Type == TT_LineComment)
+          NewLines =
+              std::max(NewLines, std::min(Current.FormatTok.NewlinesBefore,
+                                          Style.MaxEmptyLinesToKeep + 1));
         if (!Line.InPPDirective)
           Whitespaces.replaceWhitespace(Current, NewLines, State.Column,
                                         WhitespaceStartColumn, Style);

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=176086&r1=176085&r2=176086&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Feb 26 07:10:34 2013
@@ -842,8 +842,6 @@ void TokenAnnotator::calculateFormatting
                Current->Parent->is(tok::string_literal) &&
                Current->Children[0].is(tok::string_literal)) {
       Current->MustBreakBefore = true;
-    } else if (Current->FormatTok.NewlinesBefore > 1) {
-      Current->MustBreakBefore = true;
     } else {
       Current->MustBreakBefore = false;
     }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=176086&r1=176085&r2=176086&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Feb 26 07:10:34 2013
@@ -588,17 +588,23 @@ TEST_F(FormatTest, CommentsInStaticIniti
 
   EXPECT_EQ("S s = {\n"
             "  // Some comment\n"
-            "  a\n"
+            "  a,\n"
             "\n"
             "  // Comment after empty line\n"
             "  b\n"
-            "}", format("S s =    {\n"
-                        "      // Some comment\n"
-                        "  a\n"
-                        "  \n"
-                        "     // Comment after empty line\n"
-                        "      b\n"
-                        "}"));
+            "}",
+            format("S s =    {\n"
+                   "      // Some comment\n"
+                   "  a,\n"
+                   "  \n"
+                   "     // Comment after empty line\n"
+                   "      b\n"
+                   "}"));
+  EXPECT_EQ("S s = { a, b };", format("S s = {\n"
+            "  a,\n"
+            "\n"
+            "  b\n"
+            "};"));
 }
 
 //===----------------------------------------------------------------------===//





More information about the cfe-commits mailing list