r333085 - [clang-format] Break template declarations followed by comments

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Wed May 23 07:18:20 PDT 2018


Author: krasimir
Date: Wed May 23 07:18:19 2018
New Revision: 333085

URL: http://llvm.org/viewvc/llvm-project?rev=333085&view=rev
Log:
[clang-format] Break template declarations followed by comments

Summary:
This patch fixes two bugs in clang-format where the template wrapper doesn't skip over
comments causing a long template declaration to not be split into multiple lines.
These were latent and exposed by r332436.

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D47257

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

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=333085&r1=333084&r2=333085&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Wed May 23 07:18:19 2018
@@ -405,7 +405,7 @@ bool ContinuationIndenter::mustBreak(con
   // If the template declaration spans multiple lines, force wrap before the
   // function/class declaration
   if (Previous.ClosesTemplateDeclaration &&
-      State.Stack.back().BreakBeforeParameter)
+      State.Stack.back().BreakBeforeParameter && Current.CanBreakBefore)
     return true;
 
   if (State.Column <= NewLineColumn)
@@ -804,7 +804,8 @@ unsigned ContinuationIndenter::addTokenO
        !State.Stack.back().AvoidBinPacking) ||
       Previous.is(TT_BinaryOperator))
     State.Stack.back().BreakBeforeParameter = false;
-  if (Previous.isOneOf(TT_TemplateCloser, TT_JavaAnnotation) &&
+  if (PreviousNonComment &&
+      PreviousNonComment->isOneOf(TT_TemplateCloser, TT_JavaAnnotation) &&
       Current.NestingLevel == 0)
     State.Stack.back().BreakBeforeParameter = false;
   if (NextNonComment->is(tok::question) ||

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=333085&r1=333084&r2=333085&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed May 23 07:18:19 2018
@@ -5521,6 +5521,58 @@ TEST_F(FormatTest, WrapsTemplateDeclarat
                NeverBreak);
 }
 
+TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) {
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
+  Style.ColumnLimit = 60;
+  EXPECT_EQ(R"test(
+// Baseline - no comments.
+template <
+    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>
+void f() {})test",
+            format(R"test(
+// Baseline - no comments.
+template <
+    typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>
+void f() {})test", Style));
+
+  EXPECT_EQ(R"test(
+template <
+    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing
+void f() {})test",
+            format(R"test(
+template <
+    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing
+void f() {})test", Style));
+
+  EXPECT_EQ(R"test(
+template <
+    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */
+void f() {})test",
+            format(R"test(
+template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  /* line */
+void f() {})test", Style));
+
+  EXPECT_EQ(R"test(
+template <
+    typename aaaaaaaaaa<bbbbbbbbbbbb>::value>  // trailing
+                                               // multiline
+void f() {})test",
+            format(R"test(
+template <
+    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing
+                                              // multiline
+void f() {})test", Style));
+
+  EXPECT_EQ(R"test(
+template <typename aaaaaaaaaa<
+    bbbbbbbbbbbb>::value>  // trailing loooong
+void f() {})test",
+            format(R"test(
+template <
+    typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong
+void f() {})test", Style));
+}
+
 TEST_F(FormatTest, WrapsTemplateParameters) {
   FormatStyle Style = getLLVMStyle();
   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;




More information about the cfe-commits mailing list