clang-tools  4.0.0
RenamingAction.cpp
Go to the documentation of this file.
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"
22 #include "clang/Lex/Lexer.h"
23 #include "clang/Lex/Preprocessor.h"
24 #include "clang/Tooling/CommonOptionsParser.h"
25 #include "clang/Tooling/Refactoring.h"
26 #include "clang/Tooling/Tooling.h"
27 #include <string>
28 #include <vector>
29 
30 using namespace llvm;
31 
32 namespace clang {
33 namespace rename {
34 
36 public:
38  const std::vector<std::string> &NewNames,
39  const std::vector<std::string> &PrevNames,
40  const std::vector<std::vector<std::string>> &USRList,
41  std::map<std::string, tooling::Replacements> &FileToReplaces,
42  bool PrintLocations)
43  : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
44  FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
45 
46  void HandleTranslationUnit(ASTContext &Context) override {
47  for (unsigned I = 0; I < NewNames.size(); ++I)
48  HandleOneRename(Context, NewNames[I], PrevNames[I], USRList[I]);
49  }
50 
51  void HandleOneRename(ASTContext &Context, const std::string &NewName,
52  const std::string &PrevName,
53  const std::vector<std::string> &USRs) {
54  const SourceManager &SourceMgr = Context.getSourceManager();
55  std::vector<SourceLocation> RenamingCandidates;
56  std::vector<SourceLocation> NewCandidates;
57 
58  NewCandidates =
59  getLocationsOfUSRs(USRs, PrevName, Context.getTranslationUnitDecl());
60  RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
61  NewCandidates.end());
62 
63  unsigned PrevNameLen = PrevName.length();
64  for (const auto &Loc : RenamingCandidates) {
65  if (PrintLocations) {
66  FullSourceLoc FullLoc(Loc, SourceMgr);
67  errs() << "clang-rename: renamed at: " << SourceMgr.getFilename(Loc)
68  << ":" << FullLoc.getSpellingLineNumber() << ":"
69  << FullLoc.getSpellingColumnNumber() << "\n";
70  }
71  // FIXME: better error handling.
72  tooling::Replacement Replace(SourceMgr, Loc, PrevNameLen, NewName);
73  llvm::Error Err = FileToReplaces[Replace.getFilePath()].add(Replace);
74  if (Err)
75  llvm::errs() << "Renaming failed in " << Replace.getFilePath() << "! "
76  << llvm::toString(std::move(Err)) << "\n";
77  }
78  }
79 
80 private:
81  const std::vector<std::string> &NewNames, &PrevNames;
82  const std::vector<std::vector<std::string>> &USRList;
83  std::map<std::string, tooling::Replacements> &FileToReplaces;
84  bool PrintLocations;
85 };
86 
87 std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
88  return llvm::make_unique<RenamingASTConsumer>(NewNames, PrevNames, USRList,
89  FileToReplaces, PrintLocations);
90 }
91 
92 } // namespace rename
93 } // namespace clang
SourceLocation Loc
'#' location in the include directive
static cl::opt< bool > PrintLocations("pl", cl::desc("Print the locations affected by renaming to stderr."), cl::cat(ClangRenameOptions))
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
static cl::list< std::string > NewNames("new-name", cl::desc("The new name to change the symbol to."), cl::ZeroOrMore, cl::cat(ClangRenameOptions))
SourceManager SourceMgr
Definition: ClangTidy.cpp:244
void HandleTranslationUnit(ASTContext &Context) override
const std::string PrevName
Provides functionality for finding all instances of a USR in a given AST.
void HandleOneRename(ASTContext &Context, const std::string &NewName, const std::string &PrevName, const std::vector< std::string > &USRs)
ClangTidyContext & Context
Definition: ClangTidy.cpp:87
std::vector< SourceLocation > getLocationsOfUSRs(const std::vector< std::string > &USRs, StringRef PrevName, Decl *Decl)
RenamingASTConsumer(const std::vector< std::string > &NewNames, const std::vector< std::string > &PrevNames, const std::vector< std::vector< std::string >> &USRList, std::map< std::string, tooling::Replacements > &FileToReplaces, bool PrintLocations)
Provides an action to rename every symbol at a point.