Skip to content

Commit fccdc52

Browse files
[clang-format] Remove special handling of comments after brace/paren
Fixes http://llvm.org/PR55487 (#55487) The code did not match the documentation about Cpp11BracedListStyle. Changed handling of comments after opening braces, which are supposedly function call like to behave exactly like their parenthesis counter part.
1 parent 24c1bb6 commit fccdc52

File tree

4 files changed

+94
-27
lines changed

4 files changed

+94
-27
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
925925
TT_TableGenDAGArgOpenerToBreak) &&
926926
!(Current.MacroParent && Previous.MacroParent) &&
927927
(Current.isNot(TT_LineComment) ||
928-
Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen)) &&
928+
(Previous.is(BK_BracedInit) &&
929+
(!Style.Cpp11BracedListStyle || !Previous.Previous ||
930+
Previous.Previous->isNotOneOf(tok::identifier, tok::l_paren))) ||
931+
Previous.is(TT_VerilogMultiLineListLParen)) &&
929932
!IsInTemplateString(Current)) {
930933
CurrentState.Indent = State.Column + Spaces;
931934
CurrentState.IsAligned = true;

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4096,16 +4096,9 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
40964096
for (auto *Current = First->Next; Current; Current = Current->Next) {
40974097
const FormatToken *Prev = Current->Previous;
40984098
if (Current->is(TT_LineComment)) {
4099-
if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
4100-
Current->SpacesRequiredBefore =
4101-
(Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
4102-
? 0
4103-
: 1;
4104-
} else if (Prev->is(TT_VerilogMultiLineListLParen)) {
4105-
Current->SpacesRequiredBefore = 0;
4106-
} else {
4107-
Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
4108-
}
4099+
Current->SpacesRequiredBefore = Prev->is(TT_VerilogMultiLineListLParen)
4100+
? 0
4101+
: Style.SpacesBeforeTrailingComments;
41094102

41104103
// If we find a trailing comment, iterate backwards to determine whether
41114104
// it seems to relate to a specific parameter. If so, break before that

clang/unittests/Format/FormatTest.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14312,20 +14312,18 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
1431214312
" CDDDP83848_RBR_REGISTER};",
1431314313
NoBinPacking);
1431414314

14315-
// FIXME: The alignment of these trailing comments might be bad. Then again,
14316-
// this might be utterly useless in real code.
1431714315
verifyFormat("Constructor::Constructor()\n"
14318-
" : some_value{ //\n"
14319-
" aaaaaaa, //\n"
14320-
" bbbbbbb} {}");
14316+
" : some_value{ //\n"
14317+
" aaaaaaa, //\n"
14318+
" bbbbbbb} {}");
1432114319

1432214320
// In braced lists, the first comment is always assumed to belong to the
1432314321
// first element. Thus, it can be moved to the next or previous line as
1432414322
// appropriate.
14325-
verifyFormat("function({// First element:\n"
14326-
" 1,\n"
14327-
" // Second element:\n"
14328-
" 2});",
14323+
verifyFormat("function({ // First element:\n"
14324+
" 1,\n"
14325+
" // Second element:\n"
14326+
" 2});",
1432914327
"function({\n"
1433014328
" // First element:\n"
1433114329
" 1,\n"
@@ -14447,7 +14445,7 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
1444714445
verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
1444814446
verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
1444914447
verifyFormat("vector< int > x{ // comment 1\n"
14450-
" 1, 2, 3, 4 };",
14448+
" 1, 2, 3, 4 };",
1445114449
SpaceBetweenBraces);
1445214450
SpaceBetweenBraces.ColumnLimit = 20;
1445314451
verifyFormat("vector< int > x{\n"

clang/unittests/Format/FormatTestComments.cpp

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,12 +1474,12 @@ TEST_F(FormatTestComments, CommentsInStaticInitializers) {
14741474
verifyFormat("S s = {{a, b, c}, // Group #1\n"
14751475
" {d, e, f}, // Group #2\n"
14761476
" {g, h, i}}; // Group #3");
1477-
verifyFormat("S s = {{// Group #1\n"
1478-
" a, b, c},\n"
1479-
" {// Group #2\n"
1480-
" d, e, f},\n"
1481-
" {// Group #3\n"
1482-
" g, h, i}};");
1477+
verifyFormat("S s = {{ // Group #1\n"
1478+
" a, b, c},\n"
1479+
" { // Group #2\n"
1480+
" d, e, f},\n"
1481+
" { // Group #3\n"
1482+
" g, h, i}};");
14831483

14841484
EXPECT_EQ("S s = {\n"
14851485
" // Some comment\n"
@@ -4699,6 +4699,79 @@ TEST_F(FormatTestComments, SplitCommentIntroducers) {
46994699
getLLVMStyleWithColumns(10)));
47004700
}
47014701

4702+
TEST_F(FormatTestComments, LineCommentsOnStartOfFunctionCall) {
4703+
auto Style = getLLVMStyle();
4704+
4705+
EXPECT_TRUE(Style.Cpp11BracedListStyle);
4706+
4707+
verifyFormat("T foo( // Comment\n"
4708+
" arg);",
4709+
Style);
4710+
4711+
verifyFormat("T bar{ // Comment\n"
4712+
" arg};",
4713+
Style);
4714+
4715+
verifyFormat("T baz({ // Comment\n"
4716+
" arg});",
4717+
Style);
4718+
4719+
verifyFormat("T baz{{ // Comment\n"
4720+
" arg}};",
4721+
Style);
4722+
4723+
verifyFormat("T b0z(f( // Comment\n"
4724+
" arg));",
4725+
Style);
4726+
4727+
verifyFormat("T b0z(F{ // Comment\n"
4728+
" arg});",
4729+
Style);
4730+
4731+
verifyFormat("func( // Comment\n"
4732+
" arg);",
4733+
Style);
4734+
4735+
verifyFormat("func({ // Comment\n"
4736+
" arg});",
4737+
Style);
4738+
4739+
Style.Cpp11BracedListStyle = false;
4740+
4741+
verifyFormat("T foo( // Comment\n"
4742+
" arg);",
4743+
Style);
4744+
4745+
verifyFormat("T bar{ // Comment\n"
4746+
" arg\n"
4747+
"};",
4748+
Style);
4749+
4750+
verifyFormat("T baz({ // Comment\n"
4751+
" arg });",
4752+
Style);
4753+
4754+
verifyFormat("T baz{ { // Comment\n"
4755+
" arg } };",
4756+
Style);
4757+
4758+
verifyFormat("T b0z(f( // Comment\n"
4759+
" arg));",
4760+
Style);
4761+
4762+
verifyFormat("T b0z(F{ // Comment\n"
4763+
" arg });",
4764+
Style);
4765+
4766+
verifyFormat("func( // Comment\n"
4767+
" arg);",
4768+
Style);
4769+
4770+
verifyFormat("func({ // Comment\n"
4771+
" arg });",
4772+
Style);
4773+
}
4774+
47024775
} // end namespace
47034776
} // namespace test
47044777
} // end namespace format

0 commit comments

Comments
 (0)