Skip to content

Commit 759527f

Browse files
committed
[rename] NFC, extract symbol canonicalization logic into function
This function will be used by the clang-refactor's rename actions git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@309813 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 81937d8 commit 759527f

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

include/clang/Tooling/Refactoring/Rename/USRFindingAction.h

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ class NamedDecl;
2828

2929
namespace tooling {
3030

31+
/// Returns the canonical declaration that best represents a symbol that can be
32+
/// renamed.
33+
///
34+
/// The following canonicalization rules are currently used:
35+
///
36+
/// - A constructor is canonicalized to its class.
37+
/// - A destructor is canonicalized to its class.
38+
const NamedDecl *getCanonicalSymbolDeclaration(const NamedDecl *FoundDecl);
39+
3140
struct USRFindingAction {
3241
USRFindingAction(ArrayRef<unsigned> SymbolOffsets,
3342
ArrayRef<std::string> QualifiedNames, bool Force)

lib/Tooling/Refactoring/Rename/USRFindingAction.cpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ using namespace llvm;
3939
namespace clang {
4040
namespace tooling {
4141

42+
const NamedDecl *getCanonicalSymbolDeclaration(const NamedDecl *FoundDecl) {
43+
// If FoundDecl is a constructor or destructor, we want to instead take
44+
// the Decl of the corresponding class.
45+
if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(FoundDecl))
46+
FoundDecl = CtorDecl->getParent();
47+
else if (const auto *DtorDecl = dyn_cast<CXXDestructorDecl>(FoundDecl))
48+
FoundDecl = DtorDecl->getParent();
49+
// FIXME: (Alex L): Canonicalize implicit template instantions, just like
50+
// the indexer does it.
51+
52+
// Note: please update the declaration's doc comment every time the
53+
// canonicalization rules are changed.
54+
return FoundDecl;
55+
}
56+
4257
namespace {
4358
// \brief NamedDeclFindingConsumer should delegate finding USRs of given Decl to
4459
// AdditionalUSRFinder. AdditionalUSRFinder adds USRs of ctor and dtor if given
@@ -193,13 +208,7 @@ class NamedDeclFindingConsumer : public ASTConsumer {
193208
return false;
194209
}
195210

196-
// If FoundDecl is a constructor or destructor, we want to instead take
197-
// the Decl of the corresponding class.
198-
if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(FoundDecl))
199-
FoundDecl = CtorDecl->getParent();
200-
else if (const auto *DtorDecl = dyn_cast<CXXDestructorDecl>(FoundDecl))
201-
FoundDecl = DtorDecl->getParent();
202-
211+
FoundDecl = getCanonicalSymbolDeclaration(FoundDecl);
203212
SpellingNames.push_back(FoundDecl->getNameAsString());
204213
AdditionalUSRFinder Finder(FoundDecl, Context);
205214
USRList.push_back(Finder.Find());

0 commit comments

Comments
 (0)