Skip to content

Commit

Permalink
[clang-format] Remove special handling of comments after brace/paren
Browse files Browse the repository at this point in the history
  • Loading branch information
xgupta committed Nov 27, 2023
1 parent 511b2bb commit 39ce3c9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 27 deletions.
5 changes: 4 additions & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
Previous.isNot(TT_ObjCMethodExpr) && Previous.isNot(TT_RequiresClause) &&
!(Current.MacroParent && Previous.MacroParent) &&
(Current.isNot(TT_LineComment) ||
Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen))) {
(Previous.is(BK_BracedInit) &&
(!Style.Cpp11BracedListStyle || !Previous.Previous ||
Previous.Previous->isNot(tok::identifier))) ||
Previous.is(TT_VerilogMultiLineListLParen))) {
CurrentState.Indent = State.Column + Spaces;
CurrentState.IsAligned = true;
}
Expand Down
13 changes: 3 additions & 10 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3529,16 +3529,9 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
while (Current) {
const FormatToken *Prev = Current->Previous;
if (Current->is(TT_LineComment)) {
if (Prev->is(BK_BracedInit) && Prev->opensScope()) {
Current->SpacesRequiredBefore =
(Style.Cpp11BracedListStyle && !Style.SpacesInParensOptions.Other)
? 0
: 1;
} else if (Prev->is(TT_VerilogMultiLineListLParen)) {
Current->SpacesRequiredBefore = 0;
} else {
Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
}
Current->SpacesRequiredBefore = Prev->is(TT_VerilogMultiLineListLParen)
? 0
: Style.SpacesBeforeTrailingComments;

// If we find a trailing comment, iterate backwards to determine whether
// it seems to relate to a specific parameter. If so, break before that
Expand Down
18 changes: 8 additions & 10 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13507,20 +13507,18 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
" CDDDP83848_RBR_REGISTER};",
NoBinPacking);

// FIXME: The alignment of these trailing comments might be bad. Then again,
// this might be utterly useless in real code.
verifyFormat("Constructor::Constructor()\n"
" : some_value{ //\n"
" aaaaaaa, //\n"
" bbbbbbb} {}");
" : some_value{ //\n"
" aaaaaaa, //\n"
" bbbbbbb} {}");

// In braced lists, the first comment is always assumed to belong to the
// first element. Thus, it can be moved to the next or previous line as
// appropriate.
verifyFormat("function({// First element:\n"
" 1,\n"
" // Second element:\n"
" 2});",
verifyFormat("function({ // First element:\n"
" 1,\n"
" // Second element:\n"
" 2});",
"function({\n"
" // First element:\n"
" 1,\n"
Expand Down Expand Up @@ -13642,7 +13640,7 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces);
verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces);
verifyFormat("vector< int > x{ // comment 1\n"
" 1, 2, 3, 4 };",
" 1, 2, 3, 4 };",
SpaceBetweenBraces);
SpaceBetweenBraces.ColumnLimit = 20;
verifyFormat("vector< int > x{\n"
Expand Down
61 changes: 55 additions & 6 deletions clang/unittests/Format/FormatTestComments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1376,12 +1376,12 @@ TEST_F(FormatTestComments, CommentsInStaticInitializers) {
verifyFormat("S s = {{a, b, c}, // Group #1\n"
" {d, e, f}, // Group #2\n"
" {g, h, i}}; // Group #3");
verifyFormat("S s = {{// Group #1\n"
" a, b, c},\n"
" {// Group #2\n"
" d, e, f},\n"
" {// Group #3\n"
" g, h, i}};");
verifyFormat("S s = {{ // Group #1\n"
" a, b, c},\n"
" { // Group #2\n"
" d, e, f},\n"
" { // Group #3\n"
" g, h, i}};");

EXPECT_EQ("S s = {\n"
" // Some comment\n"
Expand Down Expand Up @@ -4576,6 +4576,55 @@ TEST_F(FormatTestComments, SplitCommentIntroducers) {
getLLVMStyleWithColumns(10)));
}

TEST_F(FormatTestComments, LineCommentsOnStartOfFunctionCall) {
auto Style = getLLVMStyle();

EXPECT_TRUE(Style.Cpp11BracedListStyle);

verifyFormat("T foo( // Comment\n"
" arg);",
Style);

verifyFormat("T bar{ // Comment\n"
" arg};",
Style);

verifyFormat("T baz({ // Comment\n"
" arg});",
Style);

verifyFormat("func( // Comment\n"
" arg);",
Style);

verifyFormat("func({ // Comment\n"
" arg});",
Style);

Style.Cpp11BracedListStyle = false;

verifyFormat("T foo( // Comment\n"
" arg);",
Style);

verifyFormat("T bar{ // Comment\n"
" arg\n"
"};",
Style);

verifyFormat("T baz({ // Comment\n"
" arg });",
Style);

verifyFormat("func( // Comment\n"
" arg);",
Style);

verifyFormat("func({ // Comment\n"
" arg });",
Style);
}

} // end namespace
} // namespace test
} // end namespace format
Expand Down

0 comments on commit 39ce3c9

Please sign in to comment.