clang-rename: adjust NamedDeclFindingASTVisitor for RecordDecls
authorSaleem Abdulrasool <compnerd@compnerd.org>
Thu, 28 Jul 2016 00:42:01 +0000 (00:42 +0000)
committerSaleem Abdulrasool <compnerd@compnerd.org>
Thu, 28 Jul 2016 00:42:01 +0000 (00:42 +0000)
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

clang-tools-extra/clang-rename/USRFinder.cpp
clang-tools-extra/test/clang-rename/FunctionWithClassFindByName.cpp [new file with mode: 0644]

index b64b20d..0c8eefa 100644 (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();
diff --git a/clang-tools-extra/test/clang-rename/FunctionWithClassFindByName.cpp b/clang-tools-extra/test/clang-rename/FunctionWithClassFindByName.cpp
new file mode 100644 (file)
index 0000000..32681b6
--- /dev/null
@@ -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;
+}
+