clang-tools  3.9.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:
37  RenamingASTConsumer(const std::string &NewName,
38  const std::string &PrevName,
39  const std::vector<std::string> &USRs,
40  tooling::Replacements &Replaces,
41  bool PrintLocations)
42  : NewName(NewName), PrevName(PrevName), USRs(USRs), Replaces(Replaces),
43  PrintLocations(PrintLocations) {
44  }
45 
46  void HandleTranslationUnit(ASTContext &Context) override {
47  const auto &SourceMgr = Context.getSourceManager();
48  std::vector<SourceLocation> RenamingCandidates;
49  std::vector<SourceLocation> NewCandidates;
50 
51  for (const auto &USR : USRs) {
52  NewCandidates = getLocationsOfUSR(USR, PrevName,
53  Context.getTranslationUnitDecl());
54  RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
55  NewCandidates.end());
56  NewCandidates.clear();
57  }
58 
59  auto PrevNameLen = PrevName.length();
60  if (PrintLocations)
61  for (const auto &Loc : RenamingCandidates) {
62  FullSourceLoc FullLoc(Loc, SourceMgr);
63  errs() << "clang-rename: renamed at: " << SourceMgr.getFilename(Loc)
64  << ":" << FullLoc.getSpellingLineNumber() << ":"
65  << FullLoc.getSpellingColumnNumber() << "\n";
66  Replaces.insert(tooling::Replacement(SourceMgr, Loc, PrevNameLen,
67  NewName));
68  }
69  else
70  for (const auto &Loc : RenamingCandidates)
71  Replaces.insert(tooling::Replacement(SourceMgr, Loc, PrevNameLen,
72  NewName));
73  }
74 
75 private:
76  const std::string &NewName, &PrevName;
77  const std::vector<std::string> &USRs;
78  tooling::Replacements &Replaces;
79  bool PrintLocations;
80 };
81 
82 std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
83  return llvm::make_unique<RenamingASTConsumer>(NewName, PrevName, USRs,
84  Replaces, PrintLocations);
85 }
86 
87 } // namespace rename
88 } // namespace clang
SourceLocation Loc
'#' location in the include directive
RenamingASTConsumer(const std::string &NewName, const std::string &PrevName, const std::vector< std::string > &USRs, tooling::Replacements &Replaces, bool PrintLocations)
static cl::opt< bool > PrintLocations("pl", cl::desc("Print the locations affected by renaming to stderr."), cl::cat(ClangRenameCategory))
SourceManager SourceMgr
Definition: ClangTidy.cpp:193
const std::string USR
void HandleTranslationUnit(ASTContext &Context) override
static cl::opt< std::string > NewName("new-name", cl::desc("The new name to change the symbol to."), cl::cat(ClangRenameCategory))
const std::string PrevName
Provides functionality for finding all instances of a USR in a given AST.
std::vector< SourceLocation > getLocationsOfUSR(StringRef USR, StringRef PrevName, Decl *Decl)
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
Provides an action to rename every symbol at a point.