Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 1 | //===--- tools/extra/clang-rename/RenamingAction.cpp - Clang rename tool --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief Provides an action to rename every symbol at a point. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "RenamingAction.h" |
| 16 | #include "USRLocFinder.h" |
| 17 | #include "clang/AST/ASTConsumer.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Frontend/CompilerInstance.h" |
| 21 | #include "clang/Frontend/FrontendAction.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | 3cbd71c | 2015-01-14 11:24:38 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 24 | #include "clang/Tooling/CommonOptionsParser.h" |
| 25 | #include "clang/Tooling/Refactoring.h" |
| 26 | #include "clang/Tooling/Tooling.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 27 | #include <string> |
| 28 | #include <vector> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace clang { |
| 33 | namespace rename { |
| 34 | |
| 35 | class RenamingASTConsumer : public ASTConsumer { |
| 36 | public: |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 | [diff] [blame^] | 37 | RenamingASTConsumer(const std::string &NewName, const std::string &PrevName, |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 38 | const std::vector<std::string> &USRs, |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 | [diff] [blame^] | 39 | tooling::Replacements &Replaces, bool PrintLocations) |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 40 | : NewName(NewName), PrevName(PrevName), USRs(USRs), Replaces(Replaces), |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 | [diff] [blame^] | 41 | PrintLocations(PrintLocations) {} |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 42 | |
| 43 | void HandleTranslationUnit(ASTContext &Context) override { |
| 44 | const auto &SourceMgr = Context.getSourceManager(); |
| 45 | std::vector<SourceLocation> RenamingCandidates; |
| 46 | std::vector<SourceLocation> NewCandidates; |
| 47 | |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 | [diff] [blame^] | 48 | NewCandidates = |
| 49 | getLocationsOfUSRs(USRs, PrevName, Context.getTranslationUnitDecl()); |
| 50 | RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(), |
| 51 | NewCandidates.end()); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 52 | |
Yaron Keren | 40178c3 | 2015-07-03 09:30:33 | [diff] [blame] | 53 | auto PrevNameLen = PrevName.length(); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 | [diff] [blame] | 54 | for (const auto &Loc : RenamingCandidates) { |
| 55 | if (PrintLocations) { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 56 | FullSourceLoc FullLoc(Loc, SourceMgr); |
| 57 | errs() << "clang-rename: renamed at: " << SourceMgr.getFilename(Loc) |
| 58 | << ":" << FullLoc.getSpellingLineNumber() << ":" |
| 59 | << FullLoc.getSpellingColumnNumber() << "\n"; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 60 | } |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 | [diff] [blame^] | 61 | Replaces.insert( |
| 62 | tooling::Replacement(SourceMgr, Loc, PrevNameLen, NewName)); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 | [diff] [blame] | 63 | } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | private: |
Yaron Keren | 40178c3 | 2015-07-03 09:30:33 | [diff] [blame] | 67 | const std::string &NewName, &PrevName; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 68 | const std::vector<std::string> &USRs; |
| 69 | tooling::Replacements &Replaces; |
| 70 | bool PrintLocations; |
| 71 | }; |
| 72 | |
| 73 | std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() { |
| 74 | return llvm::make_unique<RenamingASTConsumer>(NewName, PrevName, USRs, |
| 75 | Replaces, PrintLocations); |
| 76 | } |
| 77 | |
Eugene Zelenko | 05f7e6a | 2016-03-17 17:02:25 | [diff] [blame] | 78 | } // namespace rename |
| 79 | } // namespace clang |