]> granicus.if.org Git - clang/commitdiff
Only keep empty lines in unwrapped lines if they preceed a line comment.
authorDaniel Jasper <djasper@google.com>
Tue, 26 Feb 2013 13:10:34 +0000 (13:10 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 26 Feb 2013 13:10:34 +0000 (13:10 +0000)
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 };

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

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

index 7687aa347bd60dc9a31abbd8dd874d5b86a6a9f7..8bc414cd021611ee3b6a7e398958221def59e537 100644 (file)
@@ -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);
index 86daa8d60e89f3df9cc0973b2d6442c2da7d6b87..08290b905a4433d2ddf17003dcf2140156c65f4c 100644 (file)
@@ -842,8 +842,6 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
                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;
     }
index a902466c4ce0a004ef36e4aa621afb36054345b8..53f3773d5c1a4cbb34e9e159993cf3d4342b586e 100644 (file)
@@ -588,17 +588,23 @@ TEST_F(FormatTest, CommentsInStaticInitializers) {
 
   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"
+            "};"));
 }
 
 //===----------------------------------------------------------------------===//