[PATCH] D47257: [clang-format] Break template declarations followed by comments

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 23 07:22:19 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL333085: [clang-format] Break template declarations followed by comments (authored by krasimir, committed by ).
Herald added a subscriber: llvm-commits.

Repository:
  rL LLVM

https://reviews.llvm.org/D47257

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


Index: cfe/trunk/lib/Format/ContinuationIndenter.cpp
===================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp
@@ -405,7 +405,7 @@
   // 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 @@
        !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) ||
Index: cfe/trunk/unittests/Format/FormatTest.cpp
===================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp
+++ cfe/trunk/unittests/Format/FormatTest.cpp
@@ -5521,6 +5521,58 @@
                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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47257.148208.patch
Type: text/x-patch
Size: 3003 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180523/37e995bc/attachment.bin>


More information about the cfe-commits mailing list