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