[PATCH] D22881: Fix NamedDeclFindingASTVisitor

Alexander Shaposhnikov via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 27 11:51:36 PDT 2016


alexshap created this revision.
alexshap added reviewers: klimek, omtcyfz.
alexshap added subscribers: compnerd, cfe-commits.
alexshap changed the visibility of this Differential Revision from "Public (No Login Required)" to "All Users".

Properly initialize the field Context in NamedDeclFindingASTVisitor constructor: 
it is dereferenced in VisitTypeLoc Context->getLangOpts(). 
Fix passing nullptr to setResult in VisitTypeLoc: 
it is dereferenced in setResult Decl->getQualifiedNameAsString().
Add a test case: 
clang-rename crashes on it without this patch, works correctly with this patch. 
All the other tests are green.

https://reviews.llvm.org/D22881

Files:
  clang-rename/USRFinder.cpp
  test/clang-rename/FunctionWithClassFindByName.cpp

Index: test/clang-rename/FunctionWithClassFindByName.cpp
===================================================================
--- test/clang-rename/FunctionWithClassFindByName.cpp
+++ test/clang-rename/FunctionWithClassFindByName.cpp
@@ -0,0 +1,12 @@
+// 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;
+}
Index: clang-rename/USRFinder.cpp
===================================================================
--- clang-rename/USRFinder.cpp
+++ clang-rename/USRFinder.cpp
@@ -42,8 +42,9 @@
   // \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:
 
@@ -76,8 +77,9 @@
     const auto TypeBeginLoc = Loc.getBeginLoc();
     const auto TypeEndLoc = Lexer::getLocForEndOfToken(
                    TypeBeginLoc, 0, SourceMgr, Context->getLangOpts());
-    return setResult(Loc.getType()->getAsCXXRecordDecl(), TypeBeginLoc,
-                     TypeEndLoc);
+    if (auto *RD = Loc.getType()->getAsCXXRecordDecl())
+      return setResult(RD, TypeBeginLoc, TypeEndLoc);
+    return true;
   }
 
   // Other:
@@ -170,7 +172,7 @@
 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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D22881.65777.patch
Type: text/x-patch
Size: 2062 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160727/64b71263/attachment-0001.bin>


More information about the cfe-commits mailing list