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" |
Chandler Carruth | 3cbd71c | 2015-01-14 11:24:38 | [diff] [blame^] | 27 | #include <ctype.h> |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
Manuel Klimek | de23726 | 2014-08-20 01:39:05 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <vector> |
| 32 | |
| 33 | using namespace llvm; |
| 34 | |
| 35 | namespace clang { |
| 36 | namespace rename { |
| 37 | |
| 38 | class RenamingASTConsumer : public ASTConsumer { |
| 39 | public: |
| 40 | RenamingASTConsumer(const std::string &NewName, |
| 41 | const std::string &PrevName, |
| 42 | const std::vector<std::string> &USRs, |
| 43 | tooling::Replacements &Replaces, |
| 44 | bool PrintLocations) |
| 45 | : NewName(NewName), PrevName(PrevName), USRs(USRs), Replaces(Replaces), |
| 46 | PrintLocations(PrintLocations) { |
| 47 | } |
| 48 | |
| 49 | void HandleTranslationUnit(ASTContext &Context) override { |
| 50 | const auto &SourceMgr = Context.getSourceManager(); |
| 51 | std::vector<SourceLocation> RenamingCandidates; |
| 52 | std::vector<SourceLocation> NewCandidates; |
| 53 | |
| 54 | for (const auto &USR : USRs) { |
| 55 | NewCandidates = getLocationsOfUSR(USR, Context.getTranslationUnitDecl()); |
| 56 | RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(), |
| 57 | NewCandidates.end()); |
| 58 | NewCandidates.clear(); |
| 59 | } |
| 60 | |
| 61 | auto PrevNameLen = PrevName.length(); |
| 62 | if (PrintLocations) |
| 63 | for (const auto &Loc : RenamingCandidates) { |
| 64 | FullSourceLoc FullLoc(Loc, SourceMgr); |
| 65 | errs() << "clang-rename: renamed at: " << SourceMgr.getFilename(Loc) |
| 66 | << ":" << FullLoc.getSpellingLineNumber() << ":" |
| 67 | << FullLoc.getSpellingColumnNumber() << "\n"; |
| 68 | Replaces.insert(tooling::Replacement(SourceMgr, Loc, PrevNameLen, |
| 69 | NewName)); |
| 70 | } |
| 71 | else |
| 72 | for (const auto &Loc : RenamingCandidates) |
| 73 | Replaces.insert(tooling::Replacement(SourceMgr, Loc, PrevNameLen, |
| 74 | NewName)); |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | const std::string &NewName, &PrevName; |
| 79 | const std::vector<std::string> &USRs; |
| 80 | tooling::Replacements &Replaces; |
| 81 | bool PrintLocations; |
| 82 | }; |
| 83 | |
| 84 | std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() { |
| 85 | return llvm::make_unique<RenamingASTConsumer>(NewName, PrevName, USRs, |
| 86 | Replaces, PrintLocations); |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | } |