[PATCH] D20216: clang-rename: check that the source location we find actually has the old name

Miklos Vajna via cfe-commits cfe-commits at lists.llvm.org
Thu May 12 11:36:07 PDT 2016


vmiklos created this revision.
vmiklos added subscribers: cfe-commits, klimek.

This more general check could have prevented the specific problem
"getSourceOrder() == -1" guards.

http://reviews.llvm.org/D20216

Files:
  clang-rename/RenamingAction.cpp
  clang-rename/USRLocFinder.cpp
  clang-rename/USRLocFinder.h

Index: clang-rename/USRLocFinder.h
===================================================================
--- clang-rename/USRLocFinder.h
+++ clang-rename/USRLocFinder.h
@@ -28,6 +28,7 @@
 
 // FIXME: make this an AST matcher. Wouldn't that be awesome??? I agree!
 std::vector<SourceLocation> getLocationsOfUSR(const std::string usr,
+                                              const std::string &PrevName,
                                               Decl *decl);
 }
 }
Index: clang-rename/USRLocFinder.cpp
===================================================================
--- clang-rename/USRLocFinder.cpp
+++ clang-rename/USRLocFinder.cpp
@@ -20,6 +20,7 @@
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Index/USRGeneration.h"
+#include "clang/Lex/Lexer.h"
 #include "llvm/ADT/SmallVector.h"
 
 using namespace llvm;
@@ -33,7 +34,7 @@
 class USRLocFindingASTVisitor
     : public clang::RecursiveASTVisitor<USRLocFindingASTVisitor> {
 public:
-  explicit USRLocFindingASTVisitor(const std::string USR) : USR(USR) {
+  explicit USRLocFindingASTVisitor(const std::string USR, const std::string &PrevName) : USR(USR), PrevName(PrevName) {
   }
 
   // Declaration visitors:
@@ -58,6 +59,7 @@
   }
 
   bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) {
+    const ASTContext &Context = ConstructorDecl->getASTContext();
     for (clang::CXXConstructorDecl::init_const_iterator it = ConstructorDecl->init_begin(); it != ConstructorDecl->init_end(); ++it) {
       const clang::CXXCtorInitializer* Initializer = *it;
       if (Initializer->getSourceOrder() == -1) {
@@ -68,7 +70,12 @@
       if (const clang::FieldDecl *FieldDecl = Initializer->getAnyMember()) {
         if (getUSRForDecl(FieldDecl) == USR) {
           // The initializer refers to a field that is to be renamed.
-          LocationsFound.push_back(Initializer->getSourceLocation());
+          SourceRange Range = Initializer->getSourceRange();
+          StringRef TokenName = Lexer::getSourceText(CharSourceRange::getTokenRange(Range), Context.getSourceManager(), Context.getLangOpts());
+          if (TokenName.startswith(PrevName)) {
+            // The token of the source location we find actually has the old name.
+            LocationsFound.push_back(Initializer->getSourceLocation());
+          }
         }
       }
     }
@@ -117,13 +124,16 @@
 
   // All the locations of the USR were found.
   const std::string USR;
+  // Old name that is renamed.
+  const std::string PrevName;
   std::vector<clang::SourceLocation> LocationsFound;
 };
 } // namespace
 
 std::vector<SourceLocation> getLocationsOfUSR(const std::string USR,
+                                              const std::string &PrevName,
                                               Decl *Decl) {
-  USRLocFindingASTVisitor visitor(USR);
+  USRLocFindingASTVisitor visitor(USR, PrevName);
 
   visitor.TraverseDecl(Decl);
   return visitor.getLocationsFound();
Index: clang-rename/RenamingAction.cpp
===================================================================
--- clang-rename/RenamingAction.cpp
+++ clang-rename/RenamingAction.cpp
@@ -49,7 +49,7 @@
     std::vector<SourceLocation> NewCandidates;
 
     for (const auto &USR : USRs) {
-      NewCandidates = getLocationsOfUSR(USR, Context.getTranslationUnitDecl());
+      NewCandidates = getLocationsOfUSR(USR, PrevName, Context.getTranslationUnitDecl());
       RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
                                 NewCandidates.end());
       NewCandidates.clear();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20216.57077.patch
Type: text/x-patch
Size: 3617 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160512/91aad634/attachment-0001.bin>


More information about the cfe-commits mailing list