diff --git a/clang-tools-extra/clang-rename/USRFinder.cpp b/clang-tools-extra/clang-rename/USRFinder.cpp index b64b20d54bfb..0c8eefaa37ce 100644 --- a/clang-tools-extra/clang-rename/USRFinder.cpp +++ b/clang-tools-extra/clang-rename/USRFinder.cpp @@ -42,8 +42,9 @@ public: // \brief Finds the NamedDecl for a name in the source. // \param Name the fully qualified name. explicit NamedDeclFindingASTVisitor(const SourceManager &SourceMgr, - const std::string &Name) - : Result(nullptr), SourceMgr(SourceMgr), Name(Name) {} + const std::string &Name, + const ASTContext *Context) + : Result(nullptr), SourceMgr(SourceMgr), Name(Name), Context(Context) {} // Declaration visitors: @@ -75,9 +76,10 @@ public: bool VisitTypeLoc(const TypeLoc Loc) { const auto TypeBeginLoc = Loc.getBeginLoc(); const auto TypeEndLoc = Lexer::getLocForEndOfToken( - TypeBeginLoc, 0, SourceMgr, Context->getLangOpts()); - return setResult(Loc.getType()->getAsCXXRecordDecl(), TypeBeginLoc, - TypeEndLoc); + TypeBeginLoc, 0, SourceMgr, Context->getLangOpts()); + if (auto *RD = Loc.getType()->getAsCXXRecordDecl()) + return setResult(RD, TypeBeginLoc, TypeEndLoc); + return true; } // Other: @@ -170,7 +172,7 @@ const NamedDecl *getNamedDeclAt(const ASTContext &Context, const NamedDecl *getNamedDeclFor(const ASTContext &Context, const std::string &Name) { const auto &SourceMgr = Context.getSourceManager(); - NamedDeclFindingASTVisitor Visitor(SourceMgr, Name); + NamedDeclFindingASTVisitor Visitor(SourceMgr, Name, &Context); Visitor.TraverseDecl(Context.getTranslationUnitDecl()); return Visitor.getNamedDecl(); diff --git a/clang-tools-extra/test/clang-rename/FunctionWithClassFindByName.cpp b/clang-tools-extra/test/clang-rename/FunctionWithClassFindByName.cpp new file mode 100644 index 000000000000..32681b6714c1 --- /dev/null +++ b/clang-tools-extra/test/clang-rename/FunctionWithClassFindByName.cpp @@ -0,0 +1,13 @@ +// RUN: clang-rename -old-name=Foo -new-name=Bar %s -- | FileCheck %s + +void foo() { +} + +class Foo { // CHECK: class Bar +}; + +int main() { + Foo *Pointer = 0; // CHECK: Bar *Pointer = 0; + return 0; +} +