From 1ef6dbb8b910ce5a1bb24626faae4621496a4daa Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Wed, 23 May 2018 14:18:19 +0000 Subject: [PATCH] [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@333085 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/ContinuationIndenter.cpp | 5 +-- unittests/Format/FormatTest.cpp | 52 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 441df05a52..eacdfdc6e0 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -405,7 +405,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { // 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::addTokenOnNewLine(LineState &State, !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) || diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 624b086a21..87e88200bb 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -5521,6 +5521,58 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) { NeverBreak); } +TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { + FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); + Style.ColumnLimit = 60; + EXPECT_EQ(R"test( +// Baseline - no comments. +template < + typename aaaaaaaaaaaaaaaaaaaaaa::value> +void f() {})test", + format(R"test( +// Baseline - no comments. +template < + typename aaaaaaaaaaaaaaaaaaaaaa::value> +void f() {})test", Style)); + + EXPECT_EQ(R"test( +template < + typename aaaaaaaaaa::value> // trailing +void f() {})test", + format(R"test( +template < + typename aaaaaaaaaa::value> // trailing +void f() {})test", Style)); + + EXPECT_EQ(R"test( +template < + typename aaaaaaaaaa::value> /* line */ +void f() {})test", + format(R"test( +template ::value> /* line */ +void f() {})test", Style)); + + EXPECT_EQ(R"test( +template < + typename aaaaaaaaaa::value> // trailing + // multiline +void f() {})test", + format(R"test( +template < + typename aaaaaaaaaa::value> // trailing + // multiline +void f() {})test", Style)); + + EXPECT_EQ(R"test( +template ::value> // trailing loooong +void f() {})test", + format(R"test( +template < + typename aaaaaaaaaa::value> // trailing loooong +void f() {})test", Style)); +} + TEST_F(FormatTest, WrapsTemplateParameters) { FormatStyle Style = getLLVMStyle(); Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; -- 2.40.0