1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
//===-- ClangMemberTests.cpp - unit tests for renaming class members ------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ClangRenameTest.h"
namespace clang {
namespace clang_rename {
namespace test {
namespace {
class RenameMemberTest : public ClangRenameTest {
public:
RenameMemberTest() {
AppendToHeader(R"(
struct NA {
void Foo();
void NotFoo();
static void SFoo();
static void SNotFoo();
int Moo;
};
struct A {
virtual void Foo();
void NotFoo();
static void SFoo();
static void SNotFoo();
int Moo;
int NotMoo;
static int SMoo;
};
struct B : public A {
void Foo() override;
};
template <typename T> struct TA {
T* Foo();
T* NotFoo();
static T* SFoo();
static T* NotSFoo();
};
template <typename T> struct TB : public TA<T> {};
namespace ns {
template <typename T> struct TA {
T* Foo();
T* NotFoo();
static T* SFoo();
static T* NotSFoo();
static int SMoo;
};
template <typename T> struct TB : public TA<T> {};
struct A {
void Foo();
void NotFoo();
static void SFoo();
static void SNotFoo();
};
struct B : public A {};
struct C {
template <class T>
void SFoo(const T& t) {}
template <class T>
void Foo() {}
};
})");
}
};
INSTANTIATE_TEST_SUITE_P(
DISABLED_RenameTemplatedClassStaticVariableTest, RenameMemberTest,
testing::ValuesIn(std::vector<Case>({
// FIXME: support renaming static variables for template classes.
{"void f() { ns::TA<int>::SMoo; }",
"void f() { ns::TA<int>::SMeh; }", "ns::TA::SMoo", "ns::TA::SMeh"},
})) );
INSTANTIATE_TEST_SUITE_P(
RenameMemberTest, RenameMemberTest,
testing::ValuesIn(std::vector<Case>({
// Normal methods and fields.
{"void f() { A a; a.Foo(); }", "void f() { A a; a.Bar(); }", "A::Foo",
"A::Bar"},
{"void f() { ns::A a; a.Foo(); }", "void f() { ns::A a; a.Bar(); }",
"ns::A::Foo", "ns::A::Bar"},
{"void f() { A a; int x = a.Moo; }", "void f() { A a; int x = a.Meh; }",
"A::Moo", "A::Meh"},
{"void f() { B b; b.Foo(); }", "void f() { B b; b.Bar(); }", "B::Foo",
"B::Bar"},
{"void f() { ns::B b; b.Foo(); }", "void f() { ns::B b; b.Bar(); }",
"ns::A::Foo", "ns::A::Bar"},
{"void f() { B b; int x = b.Moo; }", "void f() { B b; int x = b.Meh; }",
"A::Moo", "A::Meh"},
// Static methods.
{"void f() { A::SFoo(); }", "void f() { A::SBar(); }", "A::SFoo",
"A::SBar"},
{"void f() { ns::A::SFoo(); }", "void f() { ns::A::SBar(); }",
"ns::A::SFoo", "ns::A::SBar"},
{"void f() { TA<int>::SFoo(); }", "void f() { TA<int>::SBar(); }",
"TA::SFoo", "TA::SBar"},
{"void f() { ns::TA<int>::SFoo(); }",
"void f() { ns::TA<int>::SBar(); }", "ns::TA::SFoo", "ns::TA::SBar"},
// Static variables.
{"void f() { A::SMoo; }",
"void f() { A::SMeh; }", "A::SMoo", "A::SMeh"},
// Templated methods.
{"void f() { TA<int> a; a.Foo(); }", "void f() { TA<int> a; a.Bar(); }",
"TA::Foo", "TA::Bar"},
{"void f() { ns::TA<int> a; a.Foo(); }",
"void f() { ns::TA<int> a; a.Bar(); }", "ns::TA::Foo", "ns::TA::Bar"},
{"void f() { TB<int> b; b.Foo(); }", "void f() { TB<int> b; b.Bar(); }",
"TA::Foo", "TA::Bar"},
{"void f() { ns::TB<int> b; b.Foo(); }",
"void f() { ns::TB<int> b; b.Bar(); }", "ns::TA::Foo", "ns::TA::Bar"},
{"void f() { ns::C c; int x; c.SFoo(x); }",
"void f() { ns::C c; int x; c.SBar(x); }", "ns::C::SFoo",
"ns::C::SBar"},
{"void f() { ns::C c; c.Foo<int>(); }",
"void f() { ns::C c; c.Bar<int>(); }", "ns::C::Foo", "ns::C::Bar"},
// Pointers to methods.
{"void f() { auto p = &A::Foo; }", "void f() { auto p = &A::Bar; }",
"A::Foo", "A::Bar"},
{"void f() { auto p = &A::SFoo; }", "void f() { auto p = &A::SBar; }",
"A::SFoo", "A::SBar"},
{"void f() { auto p = &B::Foo; }", "void f() { auto p = &B::Bar; }",
"B::Foo", "B::Bar"},
{"void f() { auto p = &ns::A::Foo; }",
"void f() { auto p = &ns::A::Bar; }", "ns::A::Foo", "ns::A::Bar"},
{"void f() { auto p = &ns::A::SFoo; }",
"void f() { auto p = &ns::A::SBar; }", "ns::A::SFoo", "ns::A::SBar"},
{"void f() { auto p = &ns::C::SFoo<int>; }",
"void f() { auto p = &ns::C::SBar<int>; }", "ns::C::SFoo",
"ns::C::SBar"},
// These methods are not declared or overridden in the subclass B, we
// have to use the qualified name with parent class A to identify them.
{"void f() { auto p = &ns::B::Foo; }",
"void f() { auto p = &ns::B::Bar; }", "ns::A::Foo", "ns::B::Bar"},
{"void f() { B::SFoo(); }", "void f() { B::SBar(); }", "A::SFoo",
"B::SBar"},
{"void f() { ns::B::SFoo(); }", "void f() { ns::B::SBar(); }",
"ns::A::SFoo", "ns::B::SBar"},
{"void f() { auto p = &B::SFoo; }", "void f() { auto p = &B::SBar; }",
"A::SFoo", "B::SBar"},
{"void f() { auto p = &ns::B::SFoo; }",
"void f() { auto p = &ns::B::SBar; }", "ns::A::SFoo", "ns::B::SBar"},
{"void f() { TB<int>::SFoo(); }", "void f() { TB<int>::SBar(); }",
"TA::SFoo", "TB::SBar"},
{"void f() { ns::TB<int>::SFoo(); }",
"void f() { ns::TB<int>::SBar(); }", "ns::TA::SFoo", "ns::TB::SBar"},
})) );
TEST_P(RenameMemberTest, RenameMembers) {
auto Param = GetParam();
assert(!Param.OldName.empty());
assert(!Param.NewName.empty());
std::string Actual =
runClangRenameOnCode(Param.Before, Param.OldName, Param.NewName);
CompareSnippets(Param.After, Actual);
}
TEST_F(RenameMemberTest, RenameMemberInsideClassMethods) {
std::string Before = R"(
struct X {
int Moo;
void Baz() { Moo = 1; }
};)";
std::string Expected = R"(
struct X {
int Meh;
void Baz() { Meh = 1; }
};)";
std::string After = runClangRenameOnCode(Before, "X::Moo", "Y::Meh");
CompareSnippets(Expected, After);
}
TEST_F(RenameMemberTest, RenameMethodInsideClassMethods) {
std::string Before = R"(
struct X {
void Foo() {}
void Baz() { Foo(); }
};)";
std::string Expected = R"(
struct X {
void Bar() {}
void Baz() { Bar(); }
};)";
std::string After = runClangRenameOnCode(Before, "X::Foo", "X::Bar");
CompareSnippets(Expected, After);
}
TEST_F(RenameMemberTest, RenameCtorInitializer) {
std::string Before = R"(
class X {
public:
X();
A a;
A a2;
B b;
};
X::X():a(), b() {}
)";
std::string Expected = R"(
class X {
public:
X();
A bar;
A a2;
B b;
};
X::X():bar(), b() {}
)";
std::string After = runClangRenameOnCode(Before, "X::a", "X::bar");
CompareSnippets(Expected, After);
}
} // anonymous namespace
} // namespace test
} // namespace clang_rename
} // namesdpace clang
|