r205851 - clang-format: Treat a trailing comment like a trailing comma in braced lists.

Daniel Jasper djasper at google.com
Wed Apr 9 06:18:50 PDT 2014


Author: djasper
Date: Wed Apr  9 08:18:49 2014
New Revision: 205851

URL: http://llvm.org/viewvc/llvm-project?rev=205851&view=rev
Log:
clang-format: Treat a trailing comment like a trailing comma in braced lists.

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

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

This fixes llvm.org/PR19378.

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=205851&r1=205850&r2=205851&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Apr  9 08:18:49 2014
@@ -1546,14 +1546,16 @@ bool TokenAnnotator::mustBreakBefore(con
     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;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=205851&r1=205850&r2=205851&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Apr  9 08:18:49 2014
@@ -4516,8 +4516,9 @@ TEST_F(FormatTest, UnderstandsUsesOfStar
 
   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, LayoutCxx11Constructo
       "                                 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"





More information about the cfe-commits mailing list