2012-05-24 00:29:20 +08:00
|
|
|
//===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implements tools to support refactorings.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Tooling/Refactoring.h"
|
2012-10-24 06:26:28 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2012-05-24 00:29:20 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2016-03-30 00:31:53 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2012-05-24 00:29:20 +08:00
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
2012-09-01 13:09:24 +08:00
|
|
|
#include "clang/Rewrite/Core/Rewriter.h"
|
2013-10-01 22:59:00 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "llvm/Support/raw_os_ostream.h"
|
2012-05-24 00:29:20 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tooling {
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
RefactoringTool::RefactoringTool(
|
|
|
|
const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps)
|
|
|
|
: ClangTool(Compilations, SourcePaths, PCHContainerOps) {}
|
2012-05-24 00:29:20 +08:00
|
|
|
|
2016-08-01 18:16:37 +08:00
|
|
|
std::map<std::string, Replacements> &RefactoringTool::getReplacements() {
|
|
|
|
return FileToReplaces;
|
|
|
|
}
|
2012-05-24 00:29:20 +08:00
|
|
|
|
2013-01-12 01:04:55 +08:00
|
|
|
int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
|
|
|
|
if (int Result = run(ActionFactory)) {
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2012-05-24 00:29:20 +08:00
|
|
|
LangOptions DefaultLangOptions;
|
2012-10-24 06:26:28 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
|
|
|
|
TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
|
2012-05-24 00:29:20 +08:00
|
|
|
DiagnosticsEngine Diagnostics(
|
2013-01-13 03:30:44 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
|
2012-10-24 06:26:28 +08:00
|
|
|
&*DiagOpts, &DiagnosticPrinter, false);
|
2013-01-12 01:04:55 +08:00
|
|
|
SourceManager Sources(Diagnostics, getFiles());
|
2012-05-24 00:29:20 +08:00
|
|
|
Rewriter Rewrite(Sources, DefaultLangOptions);
|
2013-01-12 01:04:55 +08:00
|
|
|
|
|
|
|
if (!applyAllReplacements(Rewrite)) {
|
2012-05-24 00:29:20 +08:00
|
|
|
llvm::errs() << "Skipped some replacements.\n";
|
|
|
|
}
|
2013-01-12 01:04:55 +08:00
|
|
|
|
|
|
|
return saveRewrittenFiles(Rewrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
|
2016-08-01 18:16:37 +08:00
|
|
|
bool Result = true;
|
|
|
|
for (const auto &Entry : FileToReplaces)
|
|
|
|
Result = tooling::applyAllReplacements(Entry.second, Rewrite) && Result;
|
|
|
|
return Result;
|
2013-01-12 01:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
|
2013-10-29 16:32:41 +08:00
|
|
|
return Rewrite.overwriteChangedFiles() ? 1 : 0;
|
2012-05-24 00:29:20 +08:00
|
|
|
}
|
|
|
|
|
2016-08-01 18:16:37 +08:00
|
|
|
bool formatAndApplyAllReplacements(
|
|
|
|
const std::map<std::string, Replacements> &FileToReplaces, Rewriter &Rewrite,
|
|
|
|
StringRef Style) {
|
2016-03-30 00:31:53 +08:00
|
|
|
SourceManager &SM = Rewrite.getSourceMgr();
|
|
|
|
FileManager &Files = SM.getFileManager();
|
|
|
|
|
|
|
|
bool Result = true;
|
2016-05-29 19:04:56 +08:00
|
|
|
for (const auto &FileAndReplaces : FileToReplaces) {
|
|
|
|
const std::string &FilePath = FileAndReplaces.first;
|
2016-03-30 00:31:53 +08:00
|
|
|
auto &CurReplaces = FileAndReplaces.second;
|
|
|
|
|
|
|
|
const FileEntry *Entry = Files.getFile(FilePath);
|
|
|
|
FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
|
|
|
|
StringRef Code = SM.getBufferData(ID);
|
|
|
|
|
|
|
|
format::FormatStyle CurStyle = format::getStyle(Style, FilePath, "LLVM");
|
2016-07-11 21:53:12 +08:00
|
|
|
auto NewReplacements =
|
2016-03-30 00:31:53 +08:00
|
|
|
format::formatReplacements(Code, CurReplaces, CurStyle);
|
2016-07-11 21:53:12 +08:00
|
|
|
if (!NewReplacements) {
|
|
|
|
llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Result = applyAllReplacements(*NewReplacements, Rewrite) && Result;
|
2016-03-30 00:31:53 +08:00
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2012-05-24 00:29:20 +08:00
|
|
|
} // end namespace tooling
|
|
|
|
} // end namespace clang
|