浏览代码

Also cleanup comments around redundant colons/commas in format::cleanup.

Reviewers: djasper

Subscribers: klimek, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@281064 91177308-0d34-0410-b5e6-96231b3b80d8
Eric Liu 9 年之前
父节点
当前提交
09a0c48856
共有 2 个文件被更改,包括 10 次插入13 次删除
  1. 2 0
      lib/Format/Format.cpp
  2. 8 13
      unittests/Format/CleanupTest.cpp

+ 2 - 0
lib/Format/Format.cpp

@@ -1132,6 +1132,8 @@ private:
         break;
         break;
       if (Left->is(LK) && Right->is(RK)) {
       if (Left->is(LK) && Right->is(RK)) {
         deleteToken(DeleteLeft ? Left : Right);
         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
         // If the right token is deleted, we should keep the left token
         // unchanged and pair it with the new right token.
         // unchanged and pair it with the new right token.
         if (!DeleteLeft)
         if (!DeleteLeft)

+ 8 - 13
unittests/Format/CleanupTest.cpp

@@ -188,39 +188,34 @@ TEST_F(CleanupTest, RedundantCommaNotInAffectedRanges) {
   EXPECT_EQ(Expected, Result);
   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;
   std::vector<tooling::Range> Ranges;
   Ranges.push_back(tooling::Range(25, 0));
   Ranges.push_back(tooling::Range(25, 0));
   Ranges.push_back(tooling::Range(40, 0));
   Ranges.push_back(tooling::Range(40, 0));
   std::string Result = cleanup(Code, Ranges);
   std::string Result = cleanup(Code, Ranges);
   EXPECT_EQ(Expected, Result);
   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));
   Ranges = std::vector<tooling::Range>(1, tooling::Range(25, 0));
   Result = cleanup(Code, Ranges);
   Result = cleanup(Code, Ranges);
   EXPECT_EQ(Expected, Result);
   EXPECT_EQ(Expected, Result);
 
 
-  // Remove trailing comma and ignore comment.
   Code = "class A {\nA() : x({1}), // comment\n , y(1),{} };";
   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));
   Ranges = std::vector<tooling::Range>(1, tooling::Range(38, 0));
   Result = cleanup(Code, Ranges);
   Result = cleanup(Code, Ranges);
   EXPECT_EQ(Expected, Result);
   EXPECT_EQ(Expected, Result);
 
 
-  // Remove trailing comma and ignore comment.
   Code = "class A {\nA() : x({1}), \n/* comment */, y(1),{} };";
   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));
   Ranges = std::vector<tooling::Range>(1, tooling::Range(40, 0));
   Result = cleanup(Code, Ranges);
   Result = cleanup(Code, Ranges);
   EXPECT_EQ(Expected, Result);
   EXPECT_EQ(Expected, Result);
 
 
-  // Remove trailing comma and ignore comment.
   Code = "class A {\nA() : , // comment\n y(1),{} };";
   Code = "class A {\nA() : , // comment\n y(1),{} };";
   Expected = "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));
   Ranges = std::vector<tooling::Range>(1, tooling::Range(17, 0));