2014-08-20 09:39:05 +08:00
|
|
|
//===--- tools/extra/clang-rename/USRLocFinder.cpp - Clang rename tool ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief Mehtods for finding all instances of a USR. Our strategy is very
|
|
|
|
/// simple; we just compare the USR at every relevant AST node with the one
|
|
|
|
/// provided.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "USRLocFinder.h"
|
|
|
|
#include "USRFinder.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Index/USRGeneration.h"
|
2016-05-13 17:17:32 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2014-08-20 09:39:05 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace rename {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// \brief This visitor recursively searches for all instances of a USR in a
|
|
|
|
// translation unit and stores them for later usage.
|
|
|
|
class USRLocFindingASTVisitor
|
|
|
|
: public clang::RecursiveASTVisitor<USRLocFindingASTVisitor> {
|
|
|
|
public:
|
2016-05-13 17:17:32 +08:00
|
|
|
explicit USRLocFindingASTVisitor(StringRef USR, StringRef PrevName) : USR(USR), PrevName(PrevName) {
|
2014-08-20 09:39:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Declaration visitors:
|
|
|
|
|
|
|
|
bool VisitNamedDecl(const NamedDecl *Decl) {
|
|
|
|
if (getUSRForDecl(Decl) == USR) {
|
|
|
|
LocationsFound.push_back(Decl->getLocation());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-04 17:45:44 +08:00
|
|
|
bool VisitVarDecl(clang::VarDecl *Decl) {
|
|
|
|
clang::QualType Type = Decl->getType();
|
|
|
|
const clang::RecordDecl *RecordDecl = Type->getPointeeCXXRecordDecl();
|
|
|
|
if (RecordDecl) {
|
|
|
|
if (getUSRForDecl(RecordDecl) == USR) {
|
|
|
|
// The declaration refers to a type that is to be renamed.
|
|
|
|
LocationsFound.push_back(Decl->getTypeSpecStartLoc());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-07 22:32:59 +08:00
|
|
|
bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) {
|
2016-05-13 17:17:32 +08:00
|
|
|
const ASTContext &Context = ConstructorDecl->getASTContext();
|
2016-05-07 22:32:59 +08:00
|
|
|
for (clang::CXXConstructorDecl::init_const_iterator it = ConstructorDecl->init_begin(); it != ConstructorDecl->init_end(); ++it) {
|
|
|
|
const clang::CXXCtorInitializer* Initializer = *it;
|
2016-05-11 16:08:07 +08:00
|
|
|
if (Initializer->getSourceOrder() == -1) {
|
|
|
|
// Ignore implicit initializers.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-07 22:32:59 +08:00
|
|
|
if (const clang::FieldDecl *FieldDecl = Initializer->getAnyMember()) {
|
|
|
|
if (getUSRForDecl(FieldDecl) == USR) {
|
|
|
|
// The initializer refers to a field that is to be renamed.
|
2016-05-13 17:17:32 +08:00
|
|
|
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());
|
|
|
|
}
|
2016-05-07 22:32:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 00:12:48 +08:00
|
|
|
|
|
|
|
if (getUSRForDecl(ConstructorDecl) == USR) {
|
|
|
|
// This takes care of the class name part of a non-inline ctor definition.
|
|
|
|
LocationsFound.push_back(ConstructorDecl->getLocStart());
|
|
|
|
}
|
2016-05-07 22:32:59 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-20 09:39:05 +08:00
|
|
|
// Expression visitors:
|
|
|
|
|
|
|
|
bool VisitDeclRefExpr(const DeclRefExpr *Expr) {
|
|
|
|
const auto *Decl = Expr->getFoundDecl();
|
|
|
|
|
|
|
|
checkNestedNameSpecifierLoc(Expr->getQualifierLoc());
|
|
|
|
if (getUSRForDecl(Decl) == USR) {
|
2016-05-25 03:08:53 +08:00
|
|
|
const SourceManager &Manager = Decl->getASTContext().getSourceManager();
|
|
|
|
SourceLocation Location = Manager.getSpellingLoc(Expr->getLocation());
|
|
|
|
LocationsFound.push_back(Location);
|
2014-08-20 09:39:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitMemberExpr(const MemberExpr *Expr) {
|
|
|
|
const auto *Decl = Expr->getFoundDecl().getDecl();
|
|
|
|
if (getUSRForDecl(Decl) == USR) {
|
2016-05-20 19:43:59 +08:00
|
|
|
const SourceManager &Manager = Decl->getASTContext().getSourceManager();
|
|
|
|
SourceLocation Location = Manager.getSpellingLoc(Expr->getMemberLoc());
|
|
|
|
LocationsFound.push_back(Location);
|
2014-08-20 09:39:05 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-03 04:00:22 +08:00
|
|
|
bool VisitCXXConstructExpr(const CXXConstructExpr *Expr) {
|
|
|
|
CXXConstructorDecl *Decl = Expr->getConstructor();
|
|
|
|
|
|
|
|
if (getUSRForDecl(Decl) == USR) {
|
|
|
|
// This takes care of 'new <name>' expressions.
|
|
|
|
LocationsFound.push_back(Expr->getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-07 03:40:12 +08:00
|
|
|
bool VisitCXXStaticCastExpr(clang::CXXStaticCastExpr *Expr) {
|
|
|
|
clang::QualType Type = Expr->getType();
|
|
|
|
// See if this a cast of a pointer.
|
|
|
|
const RecordDecl* Decl = Type->getPointeeCXXRecordDecl();
|
|
|
|
if (!Decl) {
|
|
|
|
// See if this is a cast of a reference.
|
|
|
|
Decl = Type->getAsCXXRecordDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Decl && getUSRForDecl(Decl) == USR) {
|
|
|
|
SourceLocation Location = Expr->getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
|
|
|
|
LocationsFound.push_back(Location);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-20 09:39:05 +08:00
|
|
|
// Non-visitors:
|
|
|
|
|
|
|
|
// \brief Returns a list of unique locations. Duplicate or overlapping
|
|
|
|
// locations are erroneous and should be reported!
|
|
|
|
const std::vector<clang::SourceLocation> &getLocationsFound() const {
|
|
|
|
return LocationsFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Namespace traversal:
|
|
|
|
void checkNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
|
|
|
|
while (NameLoc) {
|
|
|
|
const auto *Decl = NameLoc.getNestedNameSpecifier()->getAsNamespace();
|
|
|
|
if (Decl && getUSRForDecl(Decl) == USR)
|
|
|
|
LocationsFound.push_back(NameLoc.getLocalBeginLoc());
|
|
|
|
NameLoc = NameLoc.getPrefix();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All the locations of the USR were found.
|
2016-05-18 02:17:16 +08:00
|
|
|
const std::string USR;
|
2016-05-13 17:17:32 +08:00
|
|
|
// Old name that is renamed.
|
2016-05-18 02:17:16 +08:00
|
|
|
const std::string PrevName;
|
2014-08-20 09:39:05 +08:00
|
|
|
std::vector<clang::SourceLocation> LocationsFound;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2016-05-13 17:17:32 +08:00
|
|
|
std::vector<SourceLocation> getLocationsOfUSR(StringRef USR,
|
|
|
|
StringRef PrevName,
|
2014-08-20 09:39:05 +08:00
|
|
|
Decl *Decl) {
|
2016-05-13 17:17:32 +08:00
|
|
|
USRLocFindingASTVisitor visitor(USR, PrevName);
|
2014-08-20 09:39:05 +08:00
|
|
|
|
|
|
|
visitor.TraverseDecl(Decl);
|
|
|
|
return visitor.getLocationsFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rename
|
|
|
|
} // namespace clang
|