diff options
| author | Haojian Wu <hokein@google.com> | 2017-04-04 09:30:06 +0000 |
|---|---|---|
| committer | Haojian Wu <hokein@google.com> | 2017-04-04 09:30:06 +0000 |
| commit | 74f823a0459e382bf75e39ba5563a9e1586ce7cc (patch) | |
| tree | 60c6e7c3fd379697945045e2ed49c23a9d40a204 /clang-tools-extra/clang-rename/RenamingAction.cpp | |
| parent | 6308ac22546b9d173169d9e2ec41cb516373aac3 (diff) | |
| download | bcm5719-llvm-74f823a0459e382bf75e39ba5563a9e1586ce7cc.tar.gz bcm5719-llvm-74f823a0459e382bf75e39ba5563a9e1586ce7cc.zip | |
[clang-rename] Support renaming qualified symbol
Summary:
The patch adds a new feature for renaming qualified symbol references.
Unlike orginal clang-rename behavior, when renaming a qualified symbol to a new
qualified symbol (e.g "A::Foo" => "B::Bar"), this new rename behavior will
consider the prefix qualifiers of the symbol, and calculate the new prefix
qualifiers. It aims to add as few additional qualifiers as possible.
As this is an early version (only supports renaming classes), I don't change
current clang-rename interfaces at the moment, and would like to keep its
(command-line tool) behavior. So I added new interfaces for the prototype.
In the long run, these interfaces should be unified.
No functionality changes in original clang-rename command-line tool.
This patch also contains a few bug fixes of clang-rename which are discovered by
the new unittest:
* fix a potential nullptr accessment when class declaration doesn't have definition.
* add USRs of nested declartaions in "getNamedDeclFor".
Reviewers: ioeric
Reviewed By: ioeric
Subscribers: alexfh, cfe-commits, mgorny
Differential Revision: https://reviews.llvm.org/D31176
llvm-svn: 299419
Diffstat (limited to 'clang-tools-extra/clang-rename/RenamingAction.cpp')
| -rw-r--r-- | clang-tools-extra/clang-rename/RenamingAction.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-rename/RenamingAction.cpp b/clang-tools-extra/clang-rename/RenamingAction.cpp index 08944552ac4..5956073290b 100644 --- a/clang-tools-extra/clang-rename/RenamingAction.cpp +++ b/clang-tools-extra/clang-rename/RenamingAction.cpp @@ -84,10 +84,52 @@ private: bool PrintLocations; }; +// A renamer to rename symbols which are identified by a give USRList to +// new name. +// +// FIXME: Merge with the above RenamingASTConsumer. +class USRSymbolRenamer: public ASTConsumer { +public: + USRSymbolRenamer(const std::vector<std::string> &NewNames, + const std::vector<std::vector<std::string>> &USRList, + std::map<std::string, tooling::Replacements> &FileToReplaces) + : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) { + assert(USRList.size() == NewNames.size()); + } + + void HandleTranslationUnit(ASTContext &Context) override { + for (unsigned I = 0; I < NewNames.size(); ++I) { + // FIXME: Apply AtomicChanges directly once the refactoring APIs are + // ready. + auto AtomicChanges = createRenameAtomicChanges( + USRList[I], NewNames[I], Context.getTranslationUnitDecl()); + for (const auto AtomicChange : AtomicChanges) { + for (const auto &Replace : AtomicChange.getReplacements()) { + llvm::Error Err = FileToReplaces[Replace.getFilePath()].add(Replace); + if (Err) { + llvm::errs() << "Renaming failed in " << Replace.getFilePath() + << "! " << llvm::toString(std::move(Err)) << "\n"; + } + } + } + } + } + +private: + const std::vector<std::string> &NewNames; + const std::vector<std::vector<std::string>> &USRList; + std::map<std::string, tooling::Replacements> &FileToReplaces; +}; + std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() { return llvm::make_unique<RenamingASTConsumer>(NewNames, PrevNames, USRList, FileToReplaces, PrintLocations); } +std::unique_ptr<ASTConsumer> QualifiedRenamingAction::newASTConsumer() { + return llvm::make_unique<USRSymbolRenamer>( + NewNames, USRList, FileToReplaces); +} + } // namespace rename } // namespace clang |

