[Format] Cleanup after replacing constructor body with = default

Summary:
Remove colon and commas after replacing constructor body with = default.
Fix annotation of TT_CtorInitializerColon when preceded by a comment.

Reviewers: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 284732
This commit is contained in:
Malcolm Parsons 2016-10-20 14:58:45 +00:00
parent c1b73a1793
commit 5d8cdb83db
3 changed files with 21 additions and 1 deletions

View File

@ -1024,6 +1024,7 @@ public:
cleanupLeft(Line->First, tok::comma, tok::r_paren);
cleanupLeft(Line->First, TT_CtorInitializerComma, tok::l_brace);
cleanupLeft(Line->First, TT_CtorInitializerColon, tok::l_brace);
cleanupLeft(Line->First, TT_CtorInitializerColon, tok::equal);
}
}

View File

@ -520,7 +520,8 @@ private:
Tok->Type = TT_BitFieldColon;
} else if (Contexts.size() == 1 &&
!Line.First->isOneOf(tok::kw_enum, tok::kw_case)) {
if (Tok->Previous->isOneOf(tok::r_paren, tok::kw_noexcept))
if (Tok->getPreviousNonComment()->isOneOf(tok::r_paren,
tok::kw_noexcept))
Tok->Type = TT_CtorInitializerColon;
else
Tok->Type = TT_InheritanceColon;

View File

@ -151,6 +151,16 @@ TEST_F(CleanupTest, CtorInitializationSimpleRedundantComma) {
EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
}
TEST_F(CleanupTest, CtorInitializationSimpleRedundantColon) {
std::string Code = "class A {\nA() : =default; };";
std::string Expected = "class A {\nA() =default; };";
EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
Code = "class A {\nA() : , =default; };";
Expected = "class A {\nA() =default; };";
EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
}
TEST_F(CleanupTest, ListRedundantComma) {
std::string Code = "void f() { std::vector<int> v = {1,2,,,3,{4,5}}; }";
std::string Expected = "void f() { std::vector<int> v = {1,2,3,{4,5}}; }";
@ -239,6 +249,14 @@ TEST_F(CleanupTest, RemoveCommentsAroundDeleteCode) {
Code = "class A {\nA() : , // comment\n y(1),{} };";
Expected = "class A {\nA() : // comment\n y(1){} };";
EXPECT_EQ(Expected, cleanupAroundOffsets({17}, Code));
Code = "class A {\nA() // comment\n : ,,{} };";
Expected = "class A {\nA() // comment\n {} };";
EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code));
Code = "class A {\nA() // comment\n : ,,=default; };";
Expected = "class A {\nA() // comment\n =default; };";
EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code));
}
TEST_F(CleanupTest, CtorInitializerInNamespace) {