diff options
Diffstat (limited to 'clang-tools-extra/clang-rename/USRLocFinder.cpp')
-rw-r--r-- | clang-tools-extra/clang-rename/USRLocFinder.cpp | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/clang-tools-extra/clang-rename/USRLocFinder.cpp b/clang-tools-extra/clang-rename/USRLocFinder.cpp index f2d154eb277..ca4cf25268b 100644 --- a/clang-tools-extra/clang-rename/USRLocFinder.cpp +++ b/clang-tools-extra/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 @@ namespace { class USRLocFindingASTVisitor : public clang::RecursiveASTVisitor<USRLocFindingASTVisitor> { public: - explicit USRLocFindingASTVisitor(const std::string USR) : USR(USR) { + explicit USRLocFindingASTVisitor(StringRef USR, StringRef PrevName) : USR(USR), PrevName(PrevName) { } // Declaration visitors: @@ -58,6 +59,7 @@ public: } 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 @@ public: 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()); + SourceLocation Location = Initializer->getSourceLocation(); + StringRef TokenName = Lexer::getSourceText(CharSourceRange::getTokenRange(Location), Context.getSourceManager(), Context.getLangOpts()); + if (TokenName == PrevName) { + // The token of the source location we find actually has the old name. + LocationsFound.push_back(Initializer->getSourceLocation()); + } } } } @@ -116,14 +123,17 @@ private: } // All the locations of the USR were found. - const std::string USR; + StringRef USR; + // Old name that is renamed. + StringRef PrevName; std::vector<clang::SourceLocation> LocationsFound; }; } // namespace -std::vector<SourceLocation> getLocationsOfUSR(const std::string USR, +std::vector<SourceLocation> getLocationsOfUSR(StringRef USR, + StringRef PrevName, Decl *Decl) { - USRLocFindingASTVisitor visitor(USR); + USRLocFindingASTVisitor visitor(USR, PrevName); visitor.TraverseDecl(Decl); return visitor.getLocationsFound(); |