]> granicus.if.org Git - clang/commitdiff
Don't remove blank lines within unwrapped lines.
authorDaniel Jasper <djasper@google.com>
Wed, 20 Feb 2013 12:56:39 +0000 (12:56 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 20 Feb 2013 12:56:39 +0000 (12:56 +0000)
If the code author decides to put empty lines anywhere into the code we
should treat them equally, i.e. reduce them to the configured
MaxEmptyLinesToKeep.

With this change, we e.g. keep the newline in:
SomeType ST = {
  // First value
  a,

  // Second value
  b
};

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

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

index 21f32d257c15ac78263da7b00e90f86e935b1b65..625f93151ffed3b214a6fdf4f87bb2c6c7749974 100644 (file)
@@ -278,9 +278,9 @@ public:
     LineState State;
     State.Column = FirstIndent;
     State.NextToken = &RootToken;
-    State.Stack.push_back(
-        ParenState(FirstIndent + 4, FirstIndent, !Style.BinPackParameters,
-                   /*HasMultiParameterLine=*/ false));
+    State.Stack.push_back(ParenState(FirstIndent + 4, FirstIndent,
+                                     !Style.BinPackParameters,
+                                     /*HasMultiParameterLine=*/ false));
     State.VariablePos = 0;
     State.LineContainsContinuedForLoopSection = false;
     State.ParenLevel = 0;
@@ -368,7 +368,7 @@ private:
 
     /// \brief The position of the colon in an ObjC method declaration/call.
     unsigned ColonPos;
-    
+
     /// \brief Break before third operand in ternary expression.
     bool BreakBeforeThirdOperand;
 
@@ -520,11 +520,14 @@ private:
         State.LineContainsContinuedForLoopSection = Previous.isNot(tok::semi);
 
       if (!DryRun) {
+        unsigned NewLines =
+            std::max(1u, std::min(Current.FormatTok.NewlinesBefore,
+                                  Style.MaxEmptyLinesToKeep + 1));
         if (!Line.InPPDirective)
-          Whitespaces.replaceWhitespace(Current, 1, State.Column,
+          Whitespaces.replaceWhitespace(Current, NewLines, State.Column,
                                         WhitespaceStartColumn, Style);
         else
-          Whitespaces.replacePPWhitespace(Current, 1, State.Column,
+          Whitespaces.replacePPWhitespace(Current, NewLines, State.Column,
                                           WhitespaceStartColumn, Style);
       }
 
index ac7301eebd09004311f3f136c3831149de7e338e..2bce7c0f34904f0a6c0e1b983a339b3696b7ce9c 100644 (file)
@@ -266,7 +266,7 @@ private:
     }
     return true;
   }
-  
+
   void updateParameterCount(AnnotatedToken *Left, AnnotatedToken *Current) {
     if (Current->is(tok::comma))
       ++Left->ParameterCount;
@@ -835,6 +835,8 @@ 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 4fefb9627e85c3d073ed93bd94ffa4407b534ddb..dba6183e09cd8ccb08159b6fcc0aebafd401e5ee 100644 (file)
@@ -576,6 +576,20 @@ TEST_F(FormatTest, CommentsInStaticInitializers) {
                "          d, e, f },\n"
                "        { // Group #3\n"
                "          g, h, i } };");
+
+  EXPECT_EQ("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"
+                        "}"));
 }
 
 //===----------------------------------------------------------------------===//