Skip to content

Commit fe268fc

Browse files
committedAug 22, 2017
[clang-format] Break non-trailing block comments
Summary: This patch is an alternative to https://reviews.llvm.org/D36614, by resolving a non-idempotency issue by breaking non-trailing comments: Consider formatting the following code with column limit at `V`: ``` V const /* comment comment */ A = B; ``` The comment is not a trailing comment, breaking before it doesn't bring it under the column limit. The formatter breaks after it, resulting in: ``` V const /* comment comment */ A = B; ``` For a next reformat, the formatter considers the comment as a trailing comment, so it is free to break it further, resulting in: ``` V const /* comment comment */ A = B; ``` This patch improves the situation by directly producing the third case. Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D37007 llvm-svn: 311457
1 parent 5fb51dd commit fe268fc

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
 

‎clang/lib/Format/ContinuationIndenter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
12821282
return 0;
12831283
}
12841284
} else if (Current.is(TT_BlockComment)) {
1285-
if (!Current.isTrailingComment() || !Style.ReflowComments ||
1285+
if (!Style.ReflowComments ||
12861286
// If a comment token switches formatting, like
12871287
// /* clang-format on */, we don't want to break it further,
12881288
// but we may still want to adjust its indentation.

‎clang/unittests/Format/FormatTestComments.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -2780,6 +2780,22 @@ TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
27802780
"* long */",
27812781
getLLVMStyleWithColumns(20)));
27822782
}
2783+
2784+
TEST_F(FormatTestComments, NonTrailingBlockComments) {
2785+
verifyFormat("const /** comment comment */ A = B;",
2786+
getLLVMStyleWithColumns(40));
2787+
2788+
verifyFormat("const /** comment comment comment */ A =\n"
2789+
" B;",
2790+
getLLVMStyleWithColumns(40));
2791+
2792+
EXPECT_EQ("const /** comment comment comment\n"
2793+
" comment */\n"
2794+
" A = B;",
2795+
format("const /** comment comment comment comment */\n"
2796+
" A = B;",
2797+
getLLVMStyleWithColumns(40)));
2798+
}
27832799
} // end namespace
27842800
} // end namespace format
27852801
} // end namespace clang

0 commit comments

Comments
 (0)
Please sign in to comment.