clang-rename: check that the source location we find actually has the old name
This more general check could have prevented the specific problem "getSourceOrder() == -1" guards. Reviewers: cfe-commits, klimek Differential Revision: http://reviews.llvm.org/D20216 llvm-svn: 269402
This commit is contained in:
parent
22014750b4
commit
1d48e50159
|
|
@ -49,7 +49,7 @@ public:
|
||||||
std::vector<SourceLocation> NewCandidates;
|
std::vector<SourceLocation> NewCandidates;
|
||||||
|
|
||||||
for (const auto &USR : USRs) {
|
for (const auto &USR : USRs) {
|
||||||
NewCandidates = getLocationsOfUSR(USR, Context.getTranslationUnitDecl());
|
NewCandidates = getLocationsOfUSR(USR, PrevName, Context.getTranslationUnitDecl());
|
||||||
RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
|
RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
|
||||||
NewCandidates.end());
|
NewCandidates.end());
|
||||||
NewCandidates.clear();
|
NewCandidates.clear();
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
#include "clang/AST/RecursiveASTVisitor.h"
|
#include "clang/AST/RecursiveASTVisitor.h"
|
||||||
#include "clang/Basic/SourceLocation.h"
|
#include "clang/Basic/SourceLocation.h"
|
||||||
#include "clang/Index/USRGeneration.h"
|
#include "clang/Index/USRGeneration.h"
|
||||||
|
#include "clang/Lex/Lexer.h"
|
||||||
#include "llvm/ADT/SmallVector.h"
|
#include "llvm/ADT/SmallVector.h"
|
||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
@ -33,7 +34,7 @@ namespace {
|
||||||
class USRLocFindingASTVisitor
|
class USRLocFindingASTVisitor
|
||||||
: public clang::RecursiveASTVisitor<USRLocFindingASTVisitor> {
|
: public clang::RecursiveASTVisitor<USRLocFindingASTVisitor> {
|
||||||
public:
|
public:
|
||||||
explicit USRLocFindingASTVisitor(const std::string USR) : USR(USR) {
|
explicit USRLocFindingASTVisitor(StringRef USR, StringRef PrevName) : USR(USR), PrevName(PrevName) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Declaration visitors:
|
// Declaration visitors:
|
||||||
|
|
@ -58,6 +59,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) {
|
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) {
|
for (clang::CXXConstructorDecl::init_const_iterator it = ConstructorDecl->init_begin(); it != ConstructorDecl->init_end(); ++it) {
|
||||||
const clang::CXXCtorInitializer* Initializer = *it;
|
const clang::CXXCtorInitializer* Initializer = *it;
|
||||||
if (Initializer->getSourceOrder() == -1) {
|
if (Initializer->getSourceOrder() == -1) {
|
||||||
|
|
@ -68,7 +70,12 @@ public:
|
||||||
if (const clang::FieldDecl *FieldDecl = Initializer->getAnyMember()) {
|
if (const clang::FieldDecl *FieldDecl = Initializer->getAnyMember()) {
|
||||||
if (getUSRForDecl(FieldDecl) == USR) {
|
if (getUSRForDecl(FieldDecl) == USR) {
|
||||||
// The initializer refers to a field that is to be renamed.
|
// 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.
|
// 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;
|
std::vector<clang::SourceLocation> LocationsFound;
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
std::vector<SourceLocation> getLocationsOfUSR(const std::string USR,
|
std::vector<SourceLocation> getLocationsOfUSR(StringRef USR,
|
||||||
|
StringRef PrevName,
|
||||||
Decl *Decl) {
|
Decl *Decl) {
|
||||||
USRLocFindingASTVisitor visitor(USR);
|
USRLocFindingASTVisitor visitor(USR, PrevName);
|
||||||
|
|
||||||
visitor.TraverseDecl(Decl);
|
visitor.TraverseDecl(Decl);
|
||||||
return visitor.getLocationsFound();
|
return visitor.getLocationsFound();
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "llvm/ADT/StringRef.h"
|
||||||
|
|
||||||
namespace clang {
|
namespace clang {
|
||||||
|
|
||||||
class Decl;
|
class Decl;
|
||||||
|
|
@ -27,7 +29,8 @@ class SourceLocation;
|
||||||
namespace rename {
|
namespace rename {
|
||||||
|
|
||||||
// FIXME: make this an AST matcher. Wouldn't that be awesome??? I agree!
|
// FIXME: make this an AST matcher. Wouldn't that be awesome??? I agree!
|
||||||
std::vector<SourceLocation> getLocationsOfUSR(const std::string usr,
|
std::vector<SourceLocation> getLocationsOfUSR(llvm::StringRef usr,
|
||||||
|
llvm::StringRef PrevName,
|
||||||
Decl *decl);
|
Decl *decl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue