r199107 - clang-format: Fix corner case with comment in ctor initializer.

Daniel Jasper djasper at google.com
Mon Jan 13 06:10:04 PST 2014


Author: djasper
Date: Mon Jan 13 08:10:04 2014
New Revision: 199107

URL: http://llvm.org/viewvc/llvm-project?rev=199107&view=rev
Log:
clang-format: Fix corner case with comment in ctor initializer.

Formatting:
  Constructor() :
      // Comment forcing unwanted break.
      aaaa(aaaa) {}

Before:
  Constructor()
      :
        // Comment forcing unwanted break.
        aaaa(aaaa) {}

After:
  Constructor()
      : // Comment forcing unwanted break.
        aaaa(aaaa) {}

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=199107&r1=199106&r2=199107&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Jan 13 08:10:04 2014
@@ -1436,6 +1436,7 @@ bool TokenAnnotator::mustBreakBefore(con
                                      const FormatToken &Right) {
   if (Right.is(tok::comment)) {
     return Right.Previous->BlockKind != BK_BracedInit &&
+           Right.Previous->Type != TT_CtorInitializerColon &&
            Right.NewlinesBefore > 0;
   } else if (Right.Previous->isTrailingComment() ||
              (Right.isStringLiteral() && Right.Previous->isStringLiteral())) {

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=199107&r1=199106&r2=199107&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan 13 08:10:04 2014
@@ -2896,6 +2896,13 @@ TEST_F(FormatTest, ConstructorInitialize
                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
                OnePerLine);
+
+  EXPECT_EQ("Constructor()\n"
+            "    : // Comment forcing unwanted break.\n"
+            "      aaaa(aaaa) {}",
+            format("Constructor() :\n"
+                   "    // Comment forcing unwanted break.\n"
+                   "    aaaa(aaaa) {}"));
 }
 
 TEST_F(FormatTest, MemoizationTests) {





More information about the cfe-commits mailing list