]> granicus.if.org Git - clang/commitdiff
clang-format: Recognize lists ending in trailing commas correctly.
authorDaniel Jasper <djasper@google.com>
Wed, 9 Apr 2014 09:53:23 +0000 (09:53 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 9 Apr 2014 09:53:23 +0000 (09:53 +0000)
Previously, this did not look through trailing comments leading to a few
formatting oddities.

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

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

index a7536d2d244dd8823b8f3d82c174f4aebe0e3589..03921b04b1e6913035f2acf54e9682cd68f7e0e7 100644 (file)
@@ -1546,14 +1546,18 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
              Style.Language == FormatStyle::LK_Proto) {
     // Don't enums onto single lines in protocol buffers.
     return true;
-  } else if ((Left.is(tok::l_brace) && Left.MatchingParen &&
-              Left.MatchingParen->Previous &&
-              Left.MatchingParen->Previous->is(tok::comma)) ||
-             (Right.is(tok::r_brace) && Left.is(tok::comma))) {
-    // 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.
-    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.
+  const FormatToken *BeforeClosingBrace = nullptr;
+  if (Left.is(tok::l_brace) && Left.MatchingParen)
+    BeforeClosingBrace = Left.MatchingParen->getPreviousNonComment();
+  else if (Right.is(tok::r_brace))
+    BeforeClosingBrace = Right.getPreviousNonComment();
+  if (BeforeClosingBrace && BeforeClosingBrace->is(tok::comma))
+    return true;
+
   return false;
 }
 
index 975f9c6939b65a48c48ede0c2da46ca034716528..5adc0d5725b4440c12453a1dc21edd414672957e 100644 (file)
@@ -5129,13 +5129,14 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
                "};",
                getLLVMStyleWithColumns(39));
-  verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
-               "                 1, 1, 1, 1, //\n"
+  verifyFormat("vector<int> x = {\n"
+               "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
                "};",
                getLLVMStyleWithColumns(39));
-  verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
-               "                 1, 1, 1, 1,\n"
-               "                 /**/ /**/};",
+  verifyFormat("vector<int> x = {\n"
+               "    1, 1, 1, 1, 1, 1, 1, 1,\n"
+               "    /**/ /**/\n"
+               "};",
                getLLVMStyleWithColumns(39));
   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
                "        {aaaaaaaaaaaaaaaaaaa},\n"