blob: aec075fb73af5163a93147568956e583da443b59 [file] [log] [blame]
Manuel Klimekde237262014-08-20 01:39:05 +00001//===--- 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:05 +000022#include "clang/Lex/Lexer.h"
Chandler Carruth3cbd71c2015-01-14 11:24:38 +000023#include "clang/Lex/Preprocessor.h"
Manuel Klimekde237262014-08-20 01:39:05 +000024#include "clang/Tooling/CommonOptionsParser.h"
25#include "clang/Tooling/Refactoring.h"
26#include "clang/Tooling/Tooling.h"
Manuel Klimekde237262014-08-20 01:39:05 +000027#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:39 +000037 RenamingASTConsumer(
Miklos Vajnaaaec9b62016-08-02 09:51:31 +000038 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:39 +000041 std::map<std::string, tooling::Replacements> &FileToReplaces,
42 bool PrintLocations)
Miklos Vajnaaaec9b62016-08-02 09:51:31 +000043 : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
Eric Liu267034c2016-08-01 10:16:39 +000044 FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
Manuel Klimekde237262014-08-20 01:39:05 +000045
46 void HandleTranslationUnit(ASTContext &Context) override {
Miklos Vajnaaaec9b62016-08-02 09:51:31 +000047 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 Klimekde237262014-08-20 01:39:05 +000055 const auto &SourceMgr = Context.getSourceManager();
56 std::vector<SourceLocation> RenamingCandidates;
57 std::vector<SourceLocation> NewCandidates;
58
Kirill Bobyrev83d5d562016-07-29 10:16:45 +000059 NewCandidates =
60 getLocationsOfUSRs(USRs, PrevName, Context.getTranslationUnitDecl());
61 RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
62 NewCandidates.end());
Manuel Klimekde237262014-08-20 01:39:05 +000063
Yaron Keren40178c32015-07-03 09:30:33 +000064 auto PrevNameLen = PrevName.length();
Kirill Bobyreva3432fa2016-07-22 13:41:09 +000065 for (const auto &Loc : RenamingCandidates) {
66 if (PrintLocations) {
Manuel Klimekde237262014-08-20 01:39:05 +000067 FullSourceLoc FullLoc(Loc, SourceMgr);
68 errs() << "clang-rename: renamed at: " << SourceMgr.getFilename(Loc)
69 << ":" << FullLoc.getSpellingLineNumber() << ":"
70 << FullLoc.getSpellingColumnNumber() << "\n";
Manuel Klimekde237262014-08-20 01:39:05 +000071 }
Eric Liu267034c2016-08-01 10:16:39 +000072 // 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 Bobyreva3432fa2016-07-22 13:41:09 +000078 }
Manuel Klimekde237262014-08-20 01:39:05 +000079 }
80
81private:
Miklos Vajnaaaec9b62016-08-02 09:51:31 +000082 const std::vector<std::string> &NewNames, &PrevNames;
83 const std::vector<std::vector<std::string>> &USRList;
Eric Liu267034c2016-08-01 10:16:39 +000084 std::map<std::string, tooling::Replacements> &FileToReplaces;
Manuel Klimekde237262014-08-20 01:39:05 +000085 bool PrintLocations;
86};
87
88std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
Miklos Vajnaaaec9b62016-08-02 09:51:31 +000089 return llvm::make_unique<RenamingASTConsumer>(NewNames, PrevNames, USRList,
Eric Liu267034c2016-08-01 10:16:39 +000090 FileToReplaces, PrintLocations);
Manuel Klimekde237262014-08-20 01:39:05 +000091}
92
Eugene Zelenko05f7e6a2016-03-17 17:02:25 +000093} // namespace rename
94} // namespace clang