summaryrefslogtreecommitdiffstats
path: root/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2017-07-13 10:36:33 +0000
committerAlex Lorenz <arphaman@gmail.com>2017-07-13 10:36:33 +0000
commitffd70202d375d7ff98509a2ecd3c8318dd692fc9 (patch)
tree21b6d3dc3b86522ae39d5ccc66c25ccbab08609a /lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
parentf37177d01fdd617c15879c8a9c0d94de0528996d (diff)
[refactor][rename] Use a single base class for class that finds
a declaration at location and for class that searches for all occurrences of a specific declaration This commit uses a single RecursiveSymbolVisitor class for both USRLocFindingASTVisitor and NamedDeclOccurrenceFindingVisitor to avoid duplicate traversal code. It also traverses nested name specifier locs in the new class and remove the separate matching step. Differential Revision: https://reviews.llvm.org/D34949 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@307898 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Tooling/Refactoring/Rename/USRLocFinder.cpp')
-rw-r--r--lib/Tooling/Refactoring/Rename/USRLocFinder.cpp90
1 files changed, 16 insertions, 74 deletions
diff --git a/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp b/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
index 934507fe6e..dc21a94610 100644
--- a/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
+++ b/lib/Tooling/Refactoring/Rename/USRLocFinder.cpp
@@ -22,6 +22,7 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
#include "clang/Tooling/Core/Lookup.h"
+#include "clang/Tooling/Refactoring/RecursiveSymbolVisitor.h"
#include "clang/Tooling/Refactoring/Rename/USRFinder.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
@@ -40,70 +41,27 @@ 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 RecursiveSymbolVisitor<USRLocFindingASTVisitor> {
public:
explicit USRLocFindingASTVisitor(const std::vector<std::string> &USRs,
StringRef PrevName,
const ASTContext &Context)
- : USRSet(USRs.begin(), USRs.end()), PrevName(PrevName), Context(Context) {
+ : RecursiveSymbolVisitor(Context.getSourceManager(),
+ Context.getLangOpts()),
+ USRSet(USRs.begin(), USRs.end()), PrevName(PrevName), Context(Context) {
}
- // Declaration visitors:
-
- bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *ConstructorDecl) {
- for (const auto *Initializer : ConstructorDecl->inits()) {
- // Ignore implicit initializers.
- if (!Initializer->isWritten())
- continue;
- if (const clang::FieldDecl *FieldDecl = Initializer->getMember()) {
- if (USRSet.find(getUSRForDecl(FieldDecl)) != USRSet.end())
- LocationsFound.push_back(Initializer->getSourceLocation());
- }
- }
- return true;
- }
-
- bool VisitNamedDecl(const NamedDecl *Decl) {
- if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end())
- checkAndAddLocation(Decl->getLocation());
- return true;
- }
-
- // Expression visitors:
-
- bool VisitDeclRefExpr(const DeclRefExpr *Expr) {
- const NamedDecl *Decl = Expr->getFoundDecl();
-
- if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
- const SourceManager &Manager = Decl->getASTContext().getSourceManager();
- SourceLocation Location = Manager.getSpellingLoc(Expr->getLocation());
- checkAndAddLocation(Location);
- }
-
- return true;
- }
-
- bool VisitMemberExpr(const MemberExpr *Expr) {
- const NamedDecl *Decl = Expr->getFoundDecl().getDecl();
- if (USRSet.find(getUSRForDecl(Decl)) != USRSet.end()) {
- const SourceManager &Manager = Decl->getASTContext().getSourceManager();
- SourceLocation Location = Manager.getSpellingLoc(Expr->getMemberLoc());
- checkAndAddLocation(Location);
- }
- return true;
- }
-
- // Other visitors:
-
- bool VisitTypeLoc(const TypeLoc Loc) {
- if (USRSet.find(getUSRForDecl(Loc.getType()->getAsCXXRecordDecl())) !=
- USRSet.end())
- checkAndAddLocation(Loc.getBeginLoc());
- if (const auto *TemplateTypeParm =
- dyn_cast<TemplateTypeParmType>(Loc.getType())) {
- if (USRSet.find(getUSRForDecl(TemplateTypeParm->getDecl())) !=
- USRSet.end())
- checkAndAddLocation(Loc.getBeginLoc());
+ bool visitSymbolOccurrence(const NamedDecl *ND,
+ ArrayRef<SourceRange> NameRanges) {
+ if (USRSet.find(getUSRForDecl(ND)) != USRSet.end()) {
+ assert(NameRanges.size() == 1 &&
+ "Multiple name pieces are not supported yet!");
+ SourceLocation Loc = NameRanges[0].getBegin();
+ const SourceManager &SM = Context.getSourceManager();
+ // TODO: Deal with macro occurrences correctly.
+ if (Loc.isMacroID())
+ Loc = SM.getSpellingLoc(Loc);
+ checkAndAddLocation(Loc);
}
return true;
}
@@ -116,17 +74,6 @@ public:
return LocationsFound;
}
- // Namespace traversal:
- void handleNestedNameSpecifierLoc(NestedNameSpecifierLoc NameLoc) {
- while (NameLoc) {
- const NamespaceDecl *Decl =
- NameLoc.getNestedNameSpecifier()->getAsNamespace();
- if (Decl && USRSet.find(getUSRForDecl(Decl)) != USRSet.end())
- checkAndAddLocation(NameLoc.getLocalBeginLoc());
- NameLoc = NameLoc.getPrefix();
- }
- }
-
private:
void checkAndAddLocation(SourceLocation Loc) {
const SourceLocation BeginLoc = Loc;
@@ -449,11 +396,6 @@ getLocationsOfUSRs(const std::vector<std::string> &USRs, StringRef PrevName,
Decl *Decl) {
USRLocFindingASTVisitor Visitor(USRs, PrevName, Decl->getASTContext());
Visitor.TraverseDecl(Decl);
- NestedNameSpecifierLocFinder Finder(Decl->getASTContext());
-
- for (const auto &Location : Finder.getNestedNameSpecifierLocations())
- Visitor.handleNestedNameSpecifierLoc(Location);
-
return Visitor.getLocationsFound();
}