]> granicus.if.org Git - clang/commitdiff
Preserve newlines before block comments in static initializers.
authorAlexander Kornienko <alexfh@google.com>
Wed, 12 Jun 2013 19:04:12 +0000 (19:04 +0000)
committerAlexander Kornienko <alexfh@google.com>
Wed, 12 Jun 2013 19:04:12 +0000 (19:04 +0000)
Summary:
Basically, don't special-case line comments in this regard. And fixed
an incorrect test, that relied on the wrong behavior.

Reviewers: klimek

Reviewed By: klimek

CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D962

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

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

index 55107765f1fefa05e78be39dd741ddf42fd84825..e0a05b64ecc6f64b6327a3ec56298c9f2589ff2c 100644 (file)
@@ -556,7 +556,7 @@ private:
 
       if (!DryRun) {
         unsigned NewLines = 1;
-        if (Current.Type == TT_LineComment)
+        if (Current.is(tok::comment))
           NewLines = std::max(
               NewLines,
               std::min(Current.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1));
index 20709bbb3062451bf8c7e8ec85e68f62de3db714..85c5a36a0a719f1824f98c975db3b3efdfef18b7 100644 (file)
@@ -902,7 +902,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
           spaceRequiredBefore(Line, *Current) ? 1 : 0;
 
     if (Current->MustBreakBefore) {
-    } else if (Current->Type == TT_LineComment) {
+    } else if (Current->is(tok::comment)) {
       Current->MustBreakBefore = Current->NewlinesBefore > 0;
     } else if (Current->Previous->isTrailingComment() ||
                (Current->is(tok::string_literal) &&
index 3a4cd8d3244a95054c14673be4882edb392c14ed..c8bd945380a52c8cc0637843a187b3de7e14e92a 100644 (file)
@@ -1181,11 +1181,20 @@ TEST_F(FormatTest, CommentsInStaticInitializers) {
                    "     // Comment after empty line\n"
                    "      b\n"
                    "}"));
-  EXPECT_EQ("S s = { a, b };", format("S s = {\n"
-                                      "  a,\n"
-                                      "\n"
-                                      "  b\n"
-                                      "};"));
+  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"
+                   "}"));
   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
                "  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
                "  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
@@ -1530,6 +1539,11 @@ TEST_F(FormatTest, StaticInitializers) {
   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
                "  100000000000000000000000\n"
                "};");
+  EXPECT_EQ("S s = { a, b };", format("S s = {\n"
+                                      "  a,\n"
+                                      "\n"
+                                      "  b\n"
+                                      "};"));
 
   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
   // line. However, the formatting looks a bit off and this probably doesn't
@@ -3855,15 +3869,13 @@ TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
   EXPECT_EQ("a = {\n"
             "  1111 /*    */\n"
             "};",
-            format("a = {1111\n"
-                   "/*    */\n"
+            format("a = {1111 /*    */\n"
                    "};",
                    getLLVMStyleWithColumns(15)));
   EXPECT_EQ("a = {\n"
             "  1111 /*      */\n"
             "};",
-            format("a = {1111\n"
-                   "/*      */\n"
+            format("a = {1111 /*      */\n"
                    "};",
                    getLLVMStyleWithColumns(15)));
 
@@ -3872,8 +3884,7 @@ TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
             "  1111 /*      a\n"
             "          */\n"
             "};",
-            format("a = {1111\n"
-                   "/*      a */\n"
+            format("a = {1111 /*      a */\n"
                    "};",
                    getLLVMStyleWithColumns(15)));
 }