]> granicus.if.org Git - clang/commitdiff
clang-format: Preserve trailing-comma logic even with comments.
authorDaniel Jasper <djasper@google.com>
Tue, 11 Nov 2014 19:34:57 +0000 (19:34 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 11 Nov 2014 19:34:57 +0000 (19:34 +0000)
Before:
  vector<int> SomeVector = {// aaa
                            1, 2,
  };

After:
  vector<int> SomeVector = {
      // aaa
      1, 2,
  };

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

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

index d3d4bb14fdb2d2050f39fd7bc6187c658692537b..715cbf0a48339c035412842289f3a0300317c808 100644 (file)
@@ -1775,9 +1775,22 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
   const FormatToken &Left = *Right.Previous;
   if (Right.NewlinesBefore > 1)
     return true;
+
+  // If the last token before a '}' is a comma or a trailing comment, the
+  // intention is to insert a line break after it in order to make shuffling
+  // around entries easier.
+  const FormatToken *BeforeClosingBrace = nullptr;
+  if (Left.is(tok::l_brace) && Left.BlockKind != BK_Block && Left.MatchingParen)
+    BeforeClosingBrace = Left.MatchingParen->Previous;
+  else if (Right.is(tok::r_brace) && Right.BlockKind != BK_Block)
+    BeforeClosingBrace = &Left;
+  if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
+                             BeforeClosingBrace->isTrailingComment()))
+    return true;
+
   if (Right.is(tok::comment)) {
-    return Right.Previous->BlockKind != BK_BracedInit &&
-           Right.Previous->Type != TT_CtorInitializerColon &&
+    return Left.BlockKind != BK_BracedInit &&
+           Left.Type != TT_CtorInitializerColon &&
            (Right.NewlinesBefore > 0 && Right.HasUnescapedNewline);
   } else if (Right.Previous->isTrailingComment() ||
              (Right.isStringLiteral() && Right.Previous->isStringLiteral())) {
@@ -1826,18 +1839,6 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
     return true;
   }
 
-  // If the last token before a '}' is a comma or a trailing comment, the
-  // intention is to insert a line break after it in order to make shuffling
-  // around entries easier.
-  const FormatToken *BeforeClosingBrace = nullptr;
-  if (Left.is(tok::l_brace) && Left.MatchingParen)
-    BeforeClosingBrace = Left.MatchingParen->Previous;
-  else if (Right.is(tok::r_brace))
-    BeforeClosingBrace = Right.Previous;
-  if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
-                             BeforeClosingBrace->isTrailingComment()))
-    return true;
-
   if (Style.Language == FormatStyle::LK_JavaScript) {
     // FIXME: This might apply to other languages and token kinds.
     if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
index 3485392d4fdf28f9293d089922769472957a5957..1f95a89893d559958d9b52cacf2629de09d5a73a 100644 (file)
@@ -5645,9 +5645,9 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
   // FIXME: The alignment of these trailing comments might be bad. Then again,
   // this might be utterly useless in real code.
   verifyFormat("Constructor::Constructor()\n"
-               "    : some_value{        //\n"
-               "                 aaaaaaa //\n"
-               "      } {}");
+               "    : some_value{         //\n"
+               "                 aaaaaaa, //\n"
+               "                 bbbbbbb} {}");
 
   // In braced lists, the first comment is always assumed to belong to the
   // first element. Thus, it can be moved to the next or previous line as
@@ -5671,6 +5671,13 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
                    "                           // Second element:\n"
                    "                           2};",
                    getLLVMStyleWithColumns(30)));
+  // A trailing comma should still lead to an enforced line break.
+  EXPECT_EQ("vector<int> SomeVector = {\n"
+            "    // aaa\n"
+            "    1, 2,\n"
+            "};",
+            format("vector<int> SomeVector = { // aaa\n"
+                   "    1, 2, };"));
 
   FormatStyle ExtraSpaces = getLLVMStyle();
   ExtraSpaces.Cpp11BracedListStyle = false;