2014-08-20 09:39:05 +08:00
|
|
|
//===--- tools/extra/clang-rename/RenamingAction.cpp - Clang rename tool --===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief Provides an action to rename every symbol at a point.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "RenamingAction.h"
|
|
|
|
#include "USRLocFinder.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
2015-01-14 19:24:38 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2014-08-20 09:39:05 +08:00
|
|
|
#include "clang/Tooling/CommonOptionsParser.h"
|
|
|
|
#include "clang/Tooling/Refactoring.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
2015-01-14 19:24:38 +08:00
|
|
|
#include <ctype.h>
|
2014-08-20 09:39:05 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace rename {
|
|
|
|
|
|
|
|
class RenamingASTConsumer : public ASTConsumer {
|
|
|
|
public:
|
2015-07-03 17:30:33 +08:00
|
|
|
RenamingASTConsumer(const std::string &NewName,
|
|
|
|
const std::string &PrevName,
|
2014-08-20 09:39:05 +08:00
|
|
|
const std::vector<std::string> &USRs,
|
2015-07-03 17:30:33 +08:00
|
|
|
tooling::Replacements &Replaces,
|
|
|
|
bool PrintLocations)
|
2014-08-20 09:39:05 +08:00
|
|
|
: NewName(NewName), PrevName(PrevName), USRs(USRs), Replaces(Replaces),
|
2015-07-03 17:30:33 +08:00
|
|
|
PrintLocations(PrintLocations) {
|
|
|
|
}
|
2014-08-20 09:39:05 +08:00
|
|
|
|
|
|
|
void HandleTranslationUnit(ASTContext &Context) override {
|
|
|
|
const auto &SourceMgr = Context.getSourceManager();
|
|
|
|
std::vector<SourceLocation> RenamingCandidates;
|
|
|
|
std::vector<SourceLocation> NewCandidates;
|
|
|
|
|
|
|
|
for (const auto &USR : USRs) {
|
|
|
|
NewCandidates = getLocationsOfUSR(USR, Context.getTranslationUnitDecl());
|
|
|
|
RenamingCandidates.insert(RenamingCandidates.end(), NewCandidates.begin(),
|
|
|
|
NewCandidates.end());
|
|
|
|
NewCandidates.clear();
|
|
|
|
}
|
|
|
|
|
2015-07-03 17:30:33 +08:00
|
|
|
auto PrevNameLen = PrevName.length();
|
2014-08-20 09:39:05 +08:00
|
|
|
if (PrintLocations)
|
|
|
|
for (const auto &Loc : RenamingCandidates) {
|
|
|
|
FullSourceLoc FullLoc(Loc, SourceMgr);
|
|
|
|
errs() << "clang-rename: renamed at: " << SourceMgr.getFilename(Loc)
|
|
|
|
<< ":" << FullLoc.getSpellingLineNumber() << ":"
|
|
|
|
<< FullLoc.getSpellingColumnNumber() << "\n";
|
|
|
|
Replaces.insert(tooling::Replacement(SourceMgr, Loc, PrevNameLen,
|
|
|
|
NewName));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (const auto &Loc : RenamingCandidates)
|
|
|
|
Replaces.insert(tooling::Replacement(SourceMgr, Loc, PrevNameLen,
|
|
|
|
NewName));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-07-03 17:30:33 +08:00
|
|
|
const std::string &NewName, &PrevName;
|
2014-08-20 09:39:05 +08:00
|
|
|
const std::vector<std::string> &USRs;
|
|
|
|
tooling::Replacements &Replaces;
|
|
|
|
bool PrintLocations;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
|
|
|
|
return llvm::make_unique<RenamingASTConsumer>(NewName, PrevName, USRs,
|
|
|
|
Replaces, PrintLocations);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|