diff options
| author | Miklos Vajna <vmiklos@vmiklos.hu> | 2016-06-21 19:48:57 +0000 |
|---|---|---|
| committer | Miklos Vajna <vmiklos@vmiklos.hu> | 2016-06-21 19:48:57 +0000 |
| commit | 47bd46395ad8496bd067d4e50f2ac5b196c09977 (patch) | |
| tree | 746587a1977e83130827afd8167d86ae41ed3131 /clang-tools-extra/clang-rename/USRFinder.cpp | |
| parent | 48975881abde87dd8afc399678c00067044e09ce (diff) | |
| download | bcm5719-llvm-47bd46395ad8496bd067d4e50f2ac5b196c09977.tar.gz bcm5719-llvm-47bd46395ad8496bd067d4e50f2ac5b196c09977.zip | |
clang-rename: add a -old-name option
This is similar to -offset with the following differences:
1) -offset can refer to local variables as well.
2) -old-name makes it easier to refer to e.g. ClassName::MemberName by
spelling out the fully qualified name, instead of having to use e.g.
grep to look up the exact offset.
In other words, -offset is great when clang-rename is invoked by e.g. an
IDE, but not really user-friendly when the tool is invoked by the user
from commandline. That's the use case where -old-name is supposed to
improve the situation.
Reviewers: klimek
Differential Revision: http://reviews.llvm.org/D21517
llvm-svn: 273304
Diffstat (limited to 'clang-tools-extra/clang-rename/USRFinder.cpp')
| -rw-r--r-- | clang-tools-extra/clang-rename/USRFinder.cpp | 39 |
1 files changed, 36 insertions, 3 deletions
diff --git a/clang-tools-extra/clang-rename/USRFinder.cpp b/clang-tools-extra/clang-rename/USRFinder.cpp index 2c73c879b0a..01f7278dea9 100644 --- a/clang-tools-extra/clang-rename/USRFinder.cpp +++ b/clang-tools-extra/clang-rename/USRFinder.cpp @@ -40,6 +40,14 @@ public: Point(Point) { } + // \brief Finds the NamedDecl for a name in the source. + // \param Name the fully qualified name. + explicit NamedDeclFindingASTVisitor(const SourceManager &SourceMgr, + const std::string &Name) + : Result(nullptr), SourceMgr(SourceMgr), + Name(Name) { + } + // Declaration visitors: // \brief Checks if the point falls within the NameDecl. This covers every @@ -93,9 +101,17 @@ private: // \returns false on success. bool setResult(const NamedDecl *Decl, SourceLocation Start, SourceLocation End) { - if (!Start.isValid() || !Start.isFileID() || !End.isValid() || - !End.isFileID() || !isPointWithin(Start, End)) { - return true; + if (Name.empty()) { + // Offset is used to find the declaration. + if (!Start.isValid() || !Start.isFileID() || !End.isValid() || + !End.isFileID() || !isPointWithin(Start, End)) { + return true; + } + } else { + // Fully qualified name is used to find the declaration. + if (Name != Decl->getQualifiedNameAsString()) { + return true; + } } Result = Decl; return false; @@ -121,6 +137,7 @@ private: const NamedDecl *Result; const SourceManager &SourceMgr; const SourceLocation Point; // The location to find the NamedDecl. + const std::string Name; }; } @@ -148,6 +165,22 @@ const NamedDecl *getNamedDeclAt(const ASTContext &Context, return nullptr; } +const NamedDecl *getNamedDeclFor(const ASTContext &Context, + const std::string &Name) { + const auto &SourceMgr = Context.getSourceManager(); + NamedDeclFindingASTVisitor Visitor(SourceMgr, Name); + auto Decls = Context.getTranslationUnitDecl()->decls(); + + for (auto &CurrDecl : Decls) { + Visitor.TraverseDecl(CurrDecl); + if (const NamedDecl *Result = Visitor.getNamedDecl()) { + return Result; + } + } + + return nullptr; +} + std::string getUSRForDecl(const Decl *Decl) { llvm::SmallVector<char, 128> Buff; |

