Author: compnerd Date: Wed Jul 27 19:42:01 2016 New Revision: 276948 URL: http://llvm.org/viewvc/llvm-project?rev=276948&view=rev Log: 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! Added: clang-tools-extra/trunk/test/clang-rename/FunctionWithClassFindByName.cpp Modified: clang-tools-extra/trunk/clang-rename/USRFinder.cpp Modified: clang-tools-extra/trunk/clang-rename/USRFinder.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-rename/USRFinder.cpp?rev=276948&r1=276947&r2=276948&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-rename/USRFinder.cpp (original) +++ clang-tools-extra/trunk/clang-rename/USRFinder.cpp Wed Jul 27 19:42:01 2016 @@ -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 AS 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(); Added: clang-tools-extra/trunk/test/clang-rename/FunctionWithClassFindByName.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-rename/FunctionWithClassFindByName.cpp?rev=276948&view=auto ============================================================================== --- clang-tools-extra/trunk/test/clang-rename/FunctionWithClassFindByName.cpp (added) +++ clang-tools-extra/trunk/test/clang-rename/FunctionWithClassFindByName.cpp Wed Jul 27 19:42:01 2016 @@ -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; +} + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits