clang  5.0.0
RenamingAction.cpp
Go to the documentation of this file.
1 //===--- RenamingAction.cpp - Clang refactoring library -------------------===//
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 
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
21 #include "clang/Lex/Lexer.h"
22 #include "clang/Lex/Preprocessor.h"
26 #include "clang/Tooling/Tooling.h"
27 #include <string>
28 #include <vector>
29 
30 using namespace llvm;
31 
32 namespace clang {
33 namespace tooling {
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 
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 = tooling::getLocationsOfUSRs(
59  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 // A renamer to rename symbols which are identified by a give USRList to
88 // new name.
89 //
90 // FIXME: Merge with the above RenamingASTConsumer.
91 class USRSymbolRenamer : public ASTConsumer {
92 public:
93  USRSymbolRenamer(const std::vector<std::string> &NewNames,
94  const std::vector<std::vector<std::string>> &USRList,
95  std::map<std::string, tooling::Replacements> &FileToReplaces)
96  : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) {
97  assert(USRList.size() == NewNames.size());
98  }
99 
101  for (unsigned I = 0; I < NewNames.size(); ++I) {
102  // FIXME: Apply AtomicChanges directly once the refactoring APIs are
103  // ready.
104  auto AtomicChanges = tooling::createRenameAtomicChanges(
105  USRList[I], NewNames[I], Context.getTranslationUnitDecl());
106  for (const auto AtomicChange : AtomicChanges) {
107  for (const auto &Replace : AtomicChange.getReplacements()) {
108  llvm::Error Err = FileToReplaces[Replace.getFilePath()].add(Replace);
109  if (Err) {
110  llvm::errs() << "Renaming failed in " << Replace.getFilePath()
111  << "! " << llvm::toString(std::move(Err)) << "\n";
112  }
113  }
114  }
115  }
116  }
117 
118 private:
119  const std::vector<std::string> &NewNames;
120  const std::vector<std::vector<std::string>> &USRList;
121  std::map<std::string, tooling::Replacements> &FileToReplaces;
122 };
123 
124 std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
125  return llvm::make_unique<RenamingASTConsumer>(NewNames, PrevNames, USRList,
126  FileToReplaces, PrintLocations);
127 }
128 
129 std::unique_ptr<ASTConsumer> QualifiedRenamingAction::newASTConsumer() {
130  return llvm::make_unique<USRSymbolRenamer>(NewNames, USRList, FileToReplaces);
131 }
132 
133 } // end namespace tooling
134 } // end namespace clang
std::vector< tooling::AtomicChange > createRenameAtomicChanges(llvm::ArrayRef< std::string > USRs, llvm::StringRef NewName, Decl *TranslationUnitDecl)
Create atomic changes for renaming all symbol references which are identified by the USRs set to a gi...
Defines the clang::ASTContext interface.
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
Definition: ASTConsumer.h:34
Defines the clang::FileManager interface and associated types.
USRSymbolRenamer(const std::vector< std::string > &NewNames, const std::vector< std::vector< std::string >> &USRList, std::map< std::string, tooling::Replacements > &FileToReplaces)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:128
const Replacements & getReplacements() const
Returns a const reference to existing replacements.
Definition: AtomicChange.h:105
std::vector< SourceLocation > getLocationsOfUSRs(const std::vector< std::string > &USRs, llvm::StringRef PrevName, Decl *Decl)
detail::InMemoryDirectory::const_iterator I
const std::string PrevName
A text replacement.
Definition: Replacement.h:81
StringRef getFilename(SourceLocation SpellingLoc) const
Return the filename of the file containing a SourceLocation.
ASTContext * Context
Provides functionality for finding all instances of a USR in a given AST.
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:956
Defines the clang::Preprocessor interface.
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)
void HandleTranslationUnit(ASTContext &Context) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
StringRef getFilePath() const
Accessors.
Definition: Replacement.h:118
void HandleTranslationUnit(ASTContext &Context) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
unsigned getSpellingLineNumber(bool *Invalid=nullptr) const
SourceMgr(SourceMgr)
std::string toString(const til::SExpr *E)
void HandleOneRename(ASTContext &Context, const std::string &NewName, const std::string &PrevName, const std::vector< std::string > &USRs)
SourceManager & getSourceManager()
Definition: ASTContext.h:616
A SourceLocation and its associated SourceManager.
Defines the clang::FrontendAction interface and various convenience abstract classes (clang::ASTFront...
An atomic change is used to create and group a set of source edits, e.g.
Definition: AtomicChange.h:36
Provides an action to rename every symbol at a point.
This class handles loading and caching of source files into memory.
unsigned getSpellingColumnNumber(bool *Invalid=nullptr) const