Skip to content

Commit

Permalink
Also cleanup comments around redundant colons/commas in format::cleanup.
Browse files Browse the repository at this point in the history
Reviewers: djasper

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D24400

llvm-svn: 281064
  • Loading branch information
Eric Liu committed Sep 9, 2016
1 parent c6d54da commit 01426ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
2 changes: 2 additions & 0 deletions clang/lib/Format/Format.cpp
Expand Up @@ -1132,6 +1132,8 @@ class Cleaner : public TokenAnalyzer {
break;
if (Left->is(LK) && Right->is(RK)) {
deleteToken(DeleteLeft ? Left : Right);
for (auto *Tok = Left->Next; Tok && Tok != Right; Tok = Tok->Next)
deleteToken(Tok);
// If the right token is deleted, we should keep the left token
// unchanged and pair it with the new right token.
if (!DeleteLeft)
Expand Down
21 changes: 8 additions & 13 deletions clang/unittests/Format/CleanupTest.cpp
Expand Up @@ -188,39 +188,34 @@ TEST_F(CleanupTest, RedundantCommaNotInAffectedRanges) {
EXPECT_EQ(Expected, Result);
}

// FIXME: delete comments too.
TEST_F(CleanupTest, CtorInitializationCommentAroundCommas) {
// Remove redundant commas around comment.
std::string Code = "class A {\nA() : x({1}), /* comment */, {} };";
std::string Expected = "class A {\nA() : x({1}) /* comment */ {} };";
TEST_F(CleanupTest, RemoveCommentsAroundDeleteCode) {
std::string Code =
"class A {\nA() : x({1}), /* comment */, /* comment */ {} };";
std::string Expected = "class A {\nA() : x({1}) {} };";
std::vector<tooling::Range> Ranges;
Ranges.push_back(tooling::Range(25, 0));
Ranges.push_back(tooling::Range(40, 0));
std::string Result = cleanup(Code, Ranges);
EXPECT_EQ(Expected, Result);

// Remove trailing comma and ignore comment.
Code = "class A {\nA() : x({1}), // comment\n{} };";
Expected = "class A {\nA() : x({1}) // comment\n{} };";
Code = "class A {\nA() : x({1}), // comment\n {} };";
Expected = "class A {\nA() : x({1})\n {} };";
Ranges = std::vector<tooling::Range>(1, tooling::Range(25, 0));
Result = cleanup(Code, Ranges);
EXPECT_EQ(Expected, Result);

// Remove trailing comma and ignore comment.
Code = "class A {\nA() : x({1}), // comment\n , y(1),{} };";
Expected = "class A {\nA() : x({1}), // comment\n y(1){} };";
Expected = "class A {\nA() : x({1}), y(1){} };";
Ranges = std::vector<tooling::Range>(1, tooling::Range(38, 0));
Result = cleanup(Code, Ranges);
EXPECT_EQ(Expected, Result);

// Remove trailing comma and ignore comment.
Code = "class A {\nA() : x({1}), \n/* comment */, y(1),{} };";
Expected = "class A {\nA() : x({1}), \n/* comment */ y(1){} };";
Expected = "class A {\nA() : x({1}), \n y(1){} };";
Ranges = std::vector<tooling::Range>(1, tooling::Range(40, 0));
Result = cleanup(Code, Ranges);
EXPECT_EQ(Expected, Result);

// Remove trailing comma and ignore comment.
Code = "class A {\nA() : , // comment\n y(1),{} };";
Expected = "class A {\nA() : // comment\n y(1){} };";
Ranges = std::vector<tooling::Range>(1, tooling::Range(17, 0));
Expand Down

0 comments on commit 01426ff

Please sign in to comment.