blob: 910f4fdaafdeb7c024ec2801149a365e3c5846c3 [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:
Kirill Bobyrev83d5d562016-07-29 10:16:4537 RenamingASTConsumer(const std::string &NewName, const std::string &PrevName,
Manuel Klimekde237262014-08-20 01:39:0538 const std::vector<std::string> &USRs,
Kirill Bobyrev83d5d562016-07-29 10:16:4539 tooling::Replacements &Replaces, bool PrintLocations)
Manuel Klimekde237262014-08-20 01:39:0540 : NewName(NewName), PrevName(PrevName), USRs(USRs), Replaces(Replaces),
Kirill Bobyrev83d5d562016-07-29 10:16:4541 PrintLocations(PrintLocations) {}
Manuel Klimekde237262014-08-20 01:39:0542
43 void HandleTranslationUnit(ASTContext &Context) override {
44 const auto &SourceMgr = Context.getSourceManager();
45 std::vector<SourceLocation> RenamingCandidates;
46 std::vector<SourceLocation> NewCandidates;
47
Kirill Bobyrev83d5d562016-07-29 10:16:4548 NewCandidates =
49 getLocationsOfUSRs(USRs, PrevName, Context.getTranslationUnitDecl());
50 RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
51 NewCandidates.end());
Manuel Klimekde237262014-08-20 01:39:0552
Yaron Keren40178c32015-07-03 09:30:3353 auto PrevNameLen = PrevName.length();
Kirill Bobyreva3432fa2016-07-22 13:41:0954 for (const auto &Loc : RenamingCandidates) {
55 if (PrintLocations) {
Manuel Klimekde237262014-08-20 01:39:0556 FullSourceLoc FullLoc(Loc, SourceMgr);
57 errs() << "clang-rename: renamed at: " << SourceMgr.getFilename(Loc)
58 << ":" << FullLoc.getSpellingLineNumber() << ":"
59 << FullLoc.getSpellingColumnNumber() << "\n";
Manuel Klimekde237262014-08-20 01:39:0560 }
Kirill Bobyrev83d5d562016-07-29 10:16:4561 Replaces.insert(
62 tooling::Replacement(SourceMgr, Loc, PrevNameLen, NewName));
Kirill Bobyreva3432fa2016-07-22 13:41:0963 }
Manuel Klimekde237262014-08-20 01:39:0564 }
65
66private:
Yaron Keren40178c32015-07-03 09:30:3367 const std::string &NewName, &PrevName;
Manuel Klimekde237262014-08-20 01:39:0568 const std::vector<std::string> &USRs;
69 tooling::Replacements &Replaces;
70 bool PrintLocations;
71};
72
73std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
74 return llvm::make_unique<RenamingASTConsumer>(NewName, PrevName, USRs,
75 Replaces, PrintLocations);
76}
77
Eugene Zelenko05f7e6a2016-03-17 17:02:2578} // namespace rename
79} // namespace clang