[clang-rename] Fix the missing template constructors.

Summary:
When renaming a class with template constructors, we are missing the
occurrences of the template constructors, because getUSRsForDeclaration doesn't
give USRs of the templated constructors (they are not in the normal `ctors()`
method).

Reviewers: kbobyrev

Subscribers: jkorous, arphaman, kadircet, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D74216
This commit is contained in:
Haojian Wu 2020-02-07 14:27:54 +01:00
parent 487621237d
commit a7fd548a4f
3 changed files with 31 additions and 0 deletions

View File

@ -137,6 +137,17 @@ TEST(RenameTest, WithinFileRename) {
};
)cpp",
// Rename template class constructor.
R"cpp(
class [[F^oo]] {
template<typename T>
[[Fo^o]]();
template<typename T>
[[F^oo]](T t);
};
)cpp",
// Class in template argument.
R"cpp(
class [[F^oo]] {};

View File

@ -135,6 +135,13 @@ private:
for (const auto *CtorDecl : RecordDecl->ctors())
USRSet.insert(getUSRForDecl(CtorDecl));
// Add template constructor decls, they are not in ctors() unfortunately.
if (RecordDecl->hasUserDeclaredConstructor())
for (const auto *D : RecordDecl->decls())
if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
if (const auto *Ctor =
dyn_cast<CXXConstructorDecl>(FTD->getTemplatedDecl()))
USRSet.insert(getUSRForDecl(Ctor));
USRSet.insert(getUSRForDecl(RecordDecl->getDestructor()));
USRSet.insert(getUSRForDecl(RecordDecl));

View File

@ -5,10 +5,23 @@ public:
Foo::Foo() /* Test 2 */ {} // CHECK: Bar::Bar() /* Test 2 */ {}
class Foo2 { /* Test 3 */ // CHECK: class Bar2 {
public:
template <typename T>
Foo2(); // CHECK: Bar2();
template <typename T>
Foo2(Foo2 &); // CHECK: Bar2(Bar2 &);
};
// Test 1.
// RUN: clang-rename -offset=62 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s
// Test 2.
// RUN: clang-rename -offset=116 -new-name=Bar %s -- | sed 's,//.*,,' | FileCheck %s
// Test 3.
// RUN: clang-rename -offset=187 -new-name=Bar2 %s -- | sed 's,//.*,,' | FileCheck %s
// To find offsets after modifying the file, use:
// grep -Ubo 'Foo.*' <file>