1 //===-- RenameAliasTest.cpp - unit tests for renaming alias ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "ClangRenameTest.h"
12 namespace clang_rename
{
16 class RenameAliasTest
: public ClangRenameTest
{
31 } // namespace some_ns
33 typedef some_ns::A TA;
34 using UA = some_ns::A;
37 typedef some_ns::A TA;
38 using UA = some_ns::A;
40 template <typename T> class ptr {};
43 using TPtr = ptr<int>;
48 INSTANTIATE_TEST_SUITE_P(
49 RenameAliasTests
, RenameAliasTest
,
50 testing::ValuesIn(std::vector
<Case
>({
52 {"void f(a::TA a1) {}", "void f(b::TB a1) {}", "a::TA", "b::TB"},
53 {"void f(a::UA a1) {}", "void f(b::UB a1) {}", "a::UA", "b::UB"},
54 {"void f(a::TA* a1) {}", "void f(b::TB* a1) {}", "a::TA", "b::TB"},
55 {"void f(a::TA** a1) {}", "void f(b::TB** a1) {}", "a::TA", "b::TB"},
56 {"a::TA f() { return a::TA(); }", "b::TB f() { return b::TB(); }",
58 {"a::TA f() { return a::UA(); }", "b::TB f() { return a::UA(); }",
60 {"a::TA f() { return a::UA(); }", "a::TA f() { return b::UB(); }",
62 {"void f() { a::TA a; }", "void f() { b::TB a; }", "a::TA", "b::TB"},
63 {"void f(const a::TA& a1) {}", "void f(const b::TB& a1) {}", "a::TA",
65 {"void f(const a::UA& a1) {}", "void f(const b::UB& a1) {}", "a::UA",
67 {"void f(const a::TA* a1) {}", "void f(const b::TB* a1) {}", "a::TA",
69 {"namespace a { void f(TA a1) {} }",
70 "namespace a { void f(b::TB a1) {} }", "a::TA", "b::TB"},
71 {"void f(MACRO(a::TA) a1) {}", "void f(MACRO(b::TB) a1) {}", "a::TA",
73 {"void f(MACRO(a::TA a1)) {}", "void f(MACRO(b::TB a1)) {}", "a::TA",
76 // shorten/add namespace.
77 {"namespace b { void f(a::UA a1) {} }",
78 "namespace b {void f(UB a1) {} }", "a::UA", "b::UB"},
79 {"namespace a { void f(UA a1) {} }",
80 "namespace a {void f(b::UB a1) {} }", "a::UA", "b::UB"},
82 // use namespace and typedefs
83 {"struct S { using T = a::TA; T a_; };",
84 "struct S { using T = b::TB; T a_; };", "a::TA", "b::TB"},
85 {"using T = a::TA; T gA;", "using T = b::TB; T gA;", "a::TA", "b::TB"},
86 {"using T = a::UA; T gA;", "using T = b::UB; T gA;", "a::UA", "b::UB"},
87 {"typedef a::TA T; T gA;", "typedef b::TB T; T gA;", "a::TA", "b::TB"},
88 {"typedef a::UA T; T gA;", "typedef b::UB T; T gA;", "a::UA", "b::UB"},
89 {"typedef MACRO(a::TA) T; T gA;", "typedef MACRO(b::TB) T; T gA;",
92 // types in using shadows.
93 {"using a::TA; TA gA;", "using b::TB; b::TB gA;", "a::TA", "b::TB"},
94 {"using a::UA; UA gA;", "using b::UB; b::UB gA;", "a::UA", "b::UB"},
96 // struct members and other oddities
97 {"struct S : public a::TA {};", "struct S : public b::TB {};", "a::TA",
99 {"struct S : public a::UA {};", "struct S : public b::UB {};", "a::UA",
101 {"struct F { void f(a::TA a1) {} };",
102 "struct F { void f(b::TB a1) {} };", "a::TA", "b::TB"},
103 {"struct F { a::TA a_; };", "struct F { b::TB a_; };", "a::TA",
105 {"struct F { ptr<a::TA> a_; };", "struct F { ptr<b::TB> a_; };",
107 {"struct F { ptr<a::UA> a_; };", "struct F { ptr<b::UB> a_; };",
110 // types in nested name specifiers
111 {"void f() { a::TA::Nested ne; }", "void f() { b::TB::Nested ne; }",
113 {"void f() { a::UA::Nested ne; }", "void f() { b::UB::Nested ne; }",
115 {"void f() { a::TA::Nested::NestedEnum e; }",
116 "void f() { b::TB::Nested::NestedEnum e; }", "a::TA", "b::TB"},
117 {"void f() { auto e = a::TA::Nested::NestedEnum::E1; }",
118 "void f() { auto e = b::TB::Nested::NestedEnum::E1; }", "a::TA",
120 {"void f() { auto e = a::TA::Nested::E1; }",
121 "void f() { auto e = b::TB::Nested::E1; }", "a::TA", "b::TB"},
124 {"template <typename T> struct Foo { T t; }; void f() { Foo<a::TA> "
126 "template <typename T> struct Foo { T t; }; void f() { Foo<b::TB> "
129 {"template <typename T> struct Foo { a::TA a; };",
130 "template <typename T> struct Foo { b::TB a; };", "a::TA", "b::TB"},
131 {"template <typename T> void f(T t) {} void g() { f<a::TA>(a::TA()); }",
132 "template <typename T> void f(T t) {} void g() { f<b::TB>(b::TB()); }",
134 {"template <typename T> void f(T t) {} void g() { f<a::UA>(a::UA()); }",
135 "template <typename T> void f(T t) {} void g() { f<b::UB>(b::UB()); }",
137 {"template <typename T> int f() { return 1; } template <> int "
138 "f<a::TA>() { return 2; } int g() { return f<a::TA>(); }",
139 "template <typename T> int f() { return 1; } template <> int "
140 "f<b::TB>() { return 2; } int g() { return f<b::TB>(); }",
142 {"struct Foo { template <typename T> T foo(); }; void g() { Foo f; "
143 "auto a = f.template foo<a::TA>(); }",
144 "struct Foo { template <typename T> T foo(); }; void g() { Foo f; "
145 "auto a = f.template foo<b::TB>(); }",
147 {"struct Foo { template <typename T> T foo(); }; void g() { Foo f; "
148 "auto a = f.template foo<a::UA>(); }",
149 "struct Foo { template <typename T> T foo(); }; void g() { Foo f; "
150 "auto a = f.template foo<b::UB>(); }",
153 // The following two templates are distilled from regressions found in
154 // unique_ptr<> and type_traits.h
155 {"template <typename T> struct outer { typedef T type; type Baz(); }; "
157 "template <typename T> struct outer { typedef T type; type Baz(); }; "
160 {"template <typename T> struct nested { typedef T type; }; template "
161 "<typename T> struct outer { typename nested<T>::type Foo(); }; "
163 "template <typename T> struct nested { typedef T type; }; template "
164 "<typename T> struct outer { typename nested<T>::type Foo(); }; "
169 {"#define FOO(T, t) T t\nvoid f() { FOO(a::TA, a1); FOO(a::TA, a2); }",
170 "#define FOO(T, t) T t\nvoid f() { FOO(b::TB, a1); FOO(b::TB, a2); }",
172 {"#define FOO(n) a::TA n\nvoid f() { FOO(a1); FOO(a2); }",
173 "#define FOO(n) b::TB n\nvoid f() { FOO(a1); FOO(a2); }", "a::TA",
175 {"#define FOO(n) a::UA n\nvoid f() { FOO(a1); FOO(a2); }",
176 "#define FOO(n) b::UB n\nvoid f() { FOO(a1); FOO(a2); }", "a::UA",
179 // Pointer to member functions
180 {"auto gA = &a::TA::foo;", "auto gA = &b::TB::foo;", "a::TA", "b::TB"},
181 {"using a::TA; auto gA = &TA::foo;",
182 "using b::TB; auto gA = &b::TB::foo;", "a::TA", "b::TB"},
183 {"typedef a::TA T; auto gA = &T::foo;",
184 "typedef b::TB T; auto gA = &T::foo;", "a::TA", "b::TB"},
185 {"auto gA = &MACRO(a::TA)::foo;", "auto gA = &MACRO(b::TB)::foo;",
188 // templated using alias.
189 {"void f(TPtr<int> p) {}", "void f(NewTPtr<int> p) {}", "TPtr",
191 {"void f(::TPtr<int> p) {}", "void f(::NewTPtr<int> p) {}", "TPtr",
195 TEST_P(RenameAliasTest
, RenameAlias
) {
196 auto Param
= GetParam();
197 assert(!Param
.OldName
.empty());
198 assert(!Param
.NewName
.empty());
200 runClangRenameOnCode(Param
.Before
, Param
.OldName
, Param
.NewName
);
201 CompareSnippets(Param
.After
, Actual
);
204 TEST_F(RenameAliasTest
, RenameTypedefDefinitions
) {
205 std::string Before
= R
"(
209 std::string Expected
= R
"(
213 std::string After
= runClangRenameOnCode(Before
, "TOld", "TNew");
214 CompareSnippets(Expected
, After
);
217 TEST_F(RenameAliasTest
, RenameUsingAliasDefinitions
) {
218 std::string Before
= R
"(
222 std::string Expected
= R
"(
226 std::string After
= runClangRenameOnCode(Before
, "UOld", "UNew");
227 CompareSnippets(Expected
, After
);
230 TEST_F(RenameAliasTest
, RenameTemplatedAliasDefinitions
) {
231 std::string Before
= R
"(
232 template <typename T>
235 template <typename T>
238 std::string Expected
= R
"(
239 template <typename T>
242 template <typename T>
245 std::string After
= runClangRenameOnCode(Before
, "Old", "New");
246 CompareSnippets(Expected
, After
);
249 TEST_F(RenameAliasTest
, RenameAliasesInNamespaces
) {
250 std::string Before
= R
"(
251 namespace x { class X {}; }
256 std::string Expected
= R
"(
257 namespace x { class X {}; }
262 std::string After
= runClangRenameOnCode(Before
, "ns::UOld", "ns::UNew");
263 CompareSnippets(Expected
, After
);
266 TEST_F(RenameAliasTest
, AliasesInMacros
) {
267 std::string Before
= R
"(
268 namespace x { class Old {}; }
270 #define REF(alias) alias alias_var;
273 using old##Alias = x::old; \
281 std::string Expected
= R
"(
282 namespace x { class Old {}; }
284 #define REF(alias) alias alias_var;
287 using old##Alias = x::old; \
296 runClangRenameOnCode(Before
, "ns::OldAlias", "ns::NewAlias");
297 CompareSnippets(Expected
, After
);
300 } // anonymous namespace
302 } // namespace clang_rename
303 } // namesdpace clang