Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,8 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
Previous.isNot(TT_TableGenDAGArgOpenerToBreak) &&
!(Current.MacroParent && Previous.MacroParent) &&
(Current.isNot(TT_LineComment) ||
Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen))) {
(Style.AlignTrailingComments.Kind != FormatStyle::TCAS_Leave &&
Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen)))) {
CurrentState.Indent = State.Column + Spaces;
CurrentState.IsAligned = true;
}
Expand Down
28 changes: 24 additions & 4 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5588,6 +5588,23 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
}
if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
BeforeClosingBrace->isTrailingComment())) {
// Except let's not always break after the open brace/parenthesis/array.
// A style option allowing keeping trailing comments together with the
// open token can be desirable. E.g -
// int a[2][2] = {
// { // [0][...]
// 0, // [0][0]
// 1, // [0][1]
// },
// { // [1][...]
// 2, // [1][0]
// 3, // [1][1]
// }
// };
if (Right.isTrailingComment() &&
Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
return false;
}
return true;
}
}
Expand Down Expand Up @@ -5997,10 +6014,13 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
if (Right.isTrailingComment()) {
// We rely on MustBreakBefore being set correctly here as we should not
// change the "binding" behavior of a comment.
// The first comment in a braced lists is always interpreted as belonging to
// the first list element. Otherwise, it should be placed outside of the
// list.
return Left.is(BK_BracedInit) ||
// The first comment in a braced lists is usually interpreted as belonging
// to the first list element, unless the style is to leave trailing comments
// alone. Otherwise, it should be placed outside of the list.
bool AfterBracedInitAndBrakeable =
Left.is(BK_BracedInit) &&
Style.AlignTrailingComments.Kind != FormatStyle::TCAS_Leave;
return AfterBracedInitAndBrakeable ||
(Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 &&
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon);
}
Expand Down
31 changes: 31 additions & 0 deletions clang/unittests/Format/FormatTestComments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,37 @@ TEST_F(FormatTestComments, CommentsInStaticInitializers) {
" 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
" 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
" 0x00, 0x00, 0x00, 0x00}; // comment");

// The usual 'open brace with trailing comment' behaviour is to forcibly
// break the trailing comment onto onto a new line -
FormatStyle Style = getLLVMStyle();
Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
StringRef Input = "int a[2][2] = {\n"
" { // a\n"
" 0, // x\n"
" 1,\n"
" },\n"
" {\n"
" 2,\n"
" 3, // y\n"
" }\n"
"};";
verifyFormat("int a[2][2] = {\n"
" {\n"
" // a\n"
" 0, // x\n"
" 1,\n"
" },\n"
" {\n"
" 2,\n"
" 3, // y\n"
" }\n"
"};",
Input, Style);
// But, especially for nested, multi-dimensional initialization, allowing
// open braces with trailing comments can be desirable -
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extraneous - normally we end with a period (.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

"... -" is something I use to more explicitly link the comment to the lines that immediately follow (as opposed to anything before the comment), so it becomes more synonymous with "..., thus".

It hadn't occured to me that this could be misinterpreted or queried... until now, so I don't have a problem replacing the '-' with a '.'.

Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Leave;
verifyNoChange(Input, Style);
}

TEST_F(FormatTestComments, LineCommentsAfterRightBrace) {
Expand Down