Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [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 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | 3cbd71c | 2015-01-14 11:24:38 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [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 +0000 | [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: |
Eric Liu | 267034c | 2016-08-01 10:16:39 +0000 | [diff] [blame] | 37 | RenamingASTConsumer( |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame^] | 38 | const std::vector<std::string> &NewNames, |
| 39 | const std::vector<std::string> &PrevNames, |
| 40 | const std::vector<std::vector<std::string>> &USRList, |
Eric Liu | 267034c | 2016-08-01 10:16:39 +0000 | [diff] [blame] | 41 | std::map<std::string, tooling::Replacements> &FileToReplaces, |
| 42 | bool PrintLocations) |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame^] | 43 | : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList), |
Eric Liu | 267034c | 2016-08-01 10:16:39 +0000 | [diff] [blame] | 44 | FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {} |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 45 | |
| 46 | void HandleTranslationUnit(ASTContext &Context) override { |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame^] | 47 | for (unsigned I = 0; I < NewNames.size(); ++I) { |
| 48 | HandleOneRename(Context, NewNames[I], PrevNames[I], USRList[I]); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void HandleOneRename(ASTContext &Context, const std::string &NewName, |
| 53 | const std::string &PrevName, |
| 54 | const std::vector<std::string> &USRs) { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 55 | const auto &SourceMgr = Context.getSourceManager(); |
| 56 | std::vector<SourceLocation> RenamingCandidates; |
| 57 | std::vector<SourceLocation> NewCandidates; |
| 58 | |
Kirill Bobyrev | 83d5d56 | 2016-07-29 10:16:45 +0000 | [diff] [blame] | 59 | NewCandidates = |
| 60 | getLocationsOfUSRs(USRs, PrevName, Context.getTranslationUnitDecl()); |
| 61 | RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(), |
| 62 | NewCandidates.end()); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 63 | |
Yaron Keren | 40178c3 | 2015-07-03 09:30:33 +0000 | [diff] [blame] | 64 | auto PrevNameLen = PrevName.length(); |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 65 | for (const auto &Loc : RenamingCandidates) { |
| 66 | if (PrintLocations) { |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 67 | FullSourceLoc FullLoc(Loc, SourceMgr); |
| 68 | errs() << "clang-rename: renamed at: " << SourceMgr.getFilename(Loc) |
| 69 | << ":" << FullLoc.getSpellingLineNumber() << ":" |
| 70 | << FullLoc.getSpellingColumnNumber() << "\n"; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 71 | } |
Eric Liu | 267034c | 2016-08-01 10:16:39 +0000 | [diff] [blame] | 72 | // FIXME: better error handling. |
| 73 | auto Replace = tooling::Replacement(SourceMgr, Loc, PrevNameLen, NewName); |
| 74 | auto Err = FileToReplaces[Replace.getFilePath()].add(Replace); |
| 75 | if (Err) |
| 76 | llvm::errs() << "Renaming failed in " << Replace.getFilePath() << "! " |
| 77 | << llvm::toString(std::move(Err)) << "\n"; |
Kirill Bobyrev | a3432fa | 2016-07-22 13:41:09 +0000 | [diff] [blame] | 78 | } |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | private: |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame^] | 82 | const std::vector<std::string> &NewNames, &PrevNames; |
| 83 | const std::vector<std::vector<std::string>> &USRList; |
Eric Liu | 267034c | 2016-08-01 10:16:39 +0000 | [diff] [blame] | 84 | std::map<std::string, tooling::Replacements> &FileToReplaces; |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 85 | bool PrintLocations; |
| 86 | }; |
| 87 | |
| 88 | std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() { |
Miklos Vajna | aaec9b6 | 2016-08-02 09:51:31 +0000 | [diff] [blame^] | 89 | return llvm::make_unique<RenamingASTConsumer>(NewNames, PrevNames, USRList, |
Eric Liu | 267034c | 2016-08-01 10:16:39 +0000 | [diff] [blame] | 90 | FileToReplaces, PrintLocations); |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Eugene Zelenko | 05f7e6a | 2016-03-17 17:02:25 +0000 | [diff] [blame] | 93 | } // namespace rename |
| 94 | } // namespace clang |