blob: 08944552ac49c68cb15e91e7dd198fe79618090c [file] [log] [blame]
Manuel Klimekde237262014-08-20 01:39:051//===--- 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 Klimekde237262014-08-20 01:39:0522#include "clang/Lex/Lexer.h"
Chandler Carruth3cbd71c2015-01-14 11:24:3823#include "clang/Lex/Preprocessor.h"
Manuel Klimekde237262014-08-20 01:39:0524#include "clang/Tooling/CommonOptionsParser.h"
25#include "clang/Tooling/Refactoring.h"
26#include "clang/Tooling/Tooling.h"
Manuel Klimekde237262014-08-20 01:39:0527#include <string>
28#include <vector>
29
30using namespace llvm;
31
32namespace clang {
33namespace rename {
34
35class RenamingASTConsumer : public ASTConsumer {
36public:
Eric Liu267034c2016-08-01 10:16:3937 RenamingASTConsumer(
Miklos Vajnaaaec9b62016-08-02 09:51:3138 const std::vector<std::string> &NewNames,
39 const std::vector<std::string> &PrevNames,
40 const std::vector<std::vector<std::string>> &USRList,
Eric Liu267034c2016-08-01 10:16:3941 std::map<std::string, tooling::Replacements> &FileToReplaces,
42 bool PrintLocations)
Miklos Vajnaaaec9b62016-08-02 09:51:3143 : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
Eric Liu267034c2016-08-01 10:16:3944 FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
Manuel Klimekde237262014-08-20 01:39:0545
46 void HandleTranslationUnit(ASTContext &Context) override {
Kirill Bobyrev8769778a2016-09-04 22:50:4147 for (unsigned I = 0; I < NewNames.size(); ++I)
Miklos Vajnaaaec9b62016-08-02 09:51:3148 HandleOneRename(Context, NewNames[I], PrevNames[I], USRList[I]);
Miklos Vajnaaaec9b62016-08-02 09:51:3149 }
50
51 void HandleOneRename(ASTContext &Context, const std::string &NewName,
52 const std::string &PrevName,
53 const std::vector<std::string> &USRs) {
Kirill Bobyrev6b7d8c22016-08-15 23:20:0554 const SourceManager &SourceMgr = Context.getSourceManager();
Manuel Klimekde237262014-08-20 01:39:0555 std::vector<SourceLocation> RenamingCandidates;
56 std::vector<SourceLocation> NewCandidates;
57
Kirill Bobyrev83d5d562016-07-29 10:16:4558 NewCandidates =
59 getLocationsOfUSRs(USRs, PrevName, Context.getTranslationUnitDecl());
60 RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
61 NewCandidates.end());
Manuel Klimekde237262014-08-20 01:39:0562
Kirill Bobyrev6b7d8c22016-08-15 23:20:0563 unsigned PrevNameLen = PrevName.length();
Kirill Bobyreva3432fa2016-07-22 13:41:0964 for (const auto &Loc : RenamingCandidates) {
65 if (PrintLocations) {
Manuel Klimekde237262014-08-20 01:39:0566 FullSourceLoc FullLoc(Loc, SourceMgr);
67 errs() << "clang-rename: renamed at: " << SourceMgr.getFilename(Loc)
68 << ":" << FullLoc.getSpellingLineNumber() << ":"
69 << FullLoc.getSpellingColumnNumber() << "\n";
Manuel Klimekde237262014-08-20 01:39:0570 }
Eric Liu267034c2016-08-01 10:16:3971 // FIXME: better error handling.
Kirill Bobyrev6b7d8c22016-08-15 23:20:0572 tooling::Replacement Replace(SourceMgr, Loc, PrevNameLen, NewName);
73 llvm::Error Err = FileToReplaces[Replace.getFilePath()].add(Replace);
Eric Liu267034c2016-08-01 10:16:3974 if (Err)
75 llvm::errs() << "Renaming failed in " << Replace.getFilePath() << "! "
76 << llvm::toString(std::move(Err)) << "\n";
Kirill Bobyreva3432fa2016-07-22 13:41:0977 }
Manuel Klimekde237262014-08-20 01:39:0578 }
79
80private:
Miklos Vajnaaaec9b62016-08-02 09:51:3181 const std::vector<std::string> &NewNames, &PrevNames;
82 const std::vector<std::vector<std::string>> &USRList;
Eric Liu267034c2016-08-01 10:16:3983 std::map<std::string, tooling::Replacements> &FileToReplaces;
Manuel Klimekde237262014-08-20 01:39:0584 bool PrintLocations;
85};
86
87std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
Miklos Vajnaaaec9b62016-08-02 09:51:3188 return llvm::make_unique<RenamingASTConsumer>(NewNames, PrevNames, USRList,
Eric Liu267034c2016-08-01 10:16:3989 FileToReplaces, PrintLocations);
Manuel Klimekde237262014-08-20 01:39:0590}
91
Eugene Zelenko05f7e6a2016-03-17 17:02:2592} // namespace rename
93} // namespace clang