Skip to content

Commit

Permalink
[clang-format] Break template declarations followed by comments
Browse files Browse the repository at this point in the history
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

llvm-svn: 333085
  • Loading branch information
krasimirgg committed May 23, 2018
1 parent 5c4fb45 commit 0fb19de
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
5 changes: 3 additions & 2 deletions clang/lib/Format/ContinuationIndenter.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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) ||
Expand Down
52 changes: 52 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Expand Up @@ -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<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;
Expand Down

0 comments on commit 0fb19de

Please sign in to comment.