clang-rename: adjust NamedDeclFindingASTVisitor for RecordDecls

Ensure that Context is always properly initialised in the constructor.  It is
used for querying the LangOpts in VisitTypeLoc.  Prevent a null pointer
dereference in setResult by ensuring that a RecordDecl is being handled.

Patch by Alexander Shaposhnikov!

llvm-svn: 276948
This commit is contained in:
Saleem Abdulrasool 2016-07-28 00:42:01 +00:00
parent 2e46f720fa
commit c67dd95697
2 changed files with 21 additions and 6 deletions

View File

@ -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();

View File

@ -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;
}