]> granicus.if.org Git - clang/commitdiff
clang-format: Treat a trailing comment like a trailing comma in braced lists.
authorDaniel Jasper <djasper@google.com>
Wed, 9 Apr 2014 13:18:49 +0000 (13:18 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 9 Apr 2014 13:18:49 +0000 (13:18 +0000)
Before:
  static StructInitInfo module = {MODULE_BUILTIN, /* type */
                                  "streams" /* name */
  };

After:
  static StructInitInfo module = {
      MODULE_BUILTIN, /* type */
      "streams"       /* name */
  };

This fixes llvm.org/PR19378.

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

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

index a0109713a6ae6ca14cfac3e2468d7bc12086f0cb..df5603b268250b010a23770e9927a592382f99a2 100644 (file)
@@ -1546,14 +1546,16 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
     return true;
   }
 
-  // If the last token before a '}' is a comma, the intention is to insert a
-  // line break after it in order to make shuffling around entries easier.
+  // If the last token before a '}' is a comma or a 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->getPreviousNonComment();
+    BeforeClosingBrace = Left.MatchingParen->Previous;
   else if (Right.is(tok::r_brace))
-    BeforeClosingBrace = Right.getPreviousNonComment();
-  if (BeforeClosingBrace && BeforeClosingBrace->is(tok::comma))
+    BeforeClosingBrace = Right.Previous;
+  if (BeforeClosingBrace &&
+      BeforeClosingBrace->isOneOf(tok::comma, tok::comment))
     return true;
 
   return false;
index 5f44c5bb2b5ae36769bdc9c23d1294f4157b2f23..d7f1d0b4839e38341c6d693cf41d20ef92a5c50a 100644 (file)
@@ -4516,8 +4516,9 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
 
   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
   verifyFormat(
-      "int *MyValues = {*A, // Operator detection might be confused by the '{'\n"
-      "                 *BB // Operator detection might be confused by previous comment\n"
+      "int *MyValues = {\n"
+      "    *A, // Operator detection might be confused by the '{'\n"
+      "    *BB // Operator detection might be confused by previous comment\n"
       "};");
 
   verifyIndependentOfContext("if (int *a = &b)");
@@ -5072,7 +5073,10 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
       ExtraSpaces);
   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
-  verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
+  verifyFormat("DoSomethingWithVector({\n"
+               "                        {} /* No data */\n"
+               "                      },\n"
+               "                      { { 1, 2 } });",
                ExtraSpaces);
   verifyFormat(
       "someFunction(OtherParam,\n"