2012-05-24 00:29:20 +08:00
|
|
|
//===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-05-24 00:29:20 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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)
|
2017-03-22 05:35:04 +08:00
|
|
|
: ClangTool(Compilations, SourcePaths, std::move(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;
|
2016-11-07 14:08:23 +08:00
|
|
|
for (const auto &Entry : groupReplacementsByFile(
|
|
|
|
Rewrite.getSourceMgr().getFileManager(), FileToReplaces))
|
2016-08-01 18:16:37 +08:00
|
|
|
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(
|
2017-01-17 08:12:27 +08:00
|
|
|
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-11-07 14:08:23 +08:00
|
|
|
for (const auto &FileAndReplaces : groupReplacementsByFile(
|
|
|
|
Rewrite.getSourceMgr().getFileManager(), FileToReplaces)) {
|
2016-05-29 19:04:56 +08:00
|
|
|
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);
|
|
|
|
|
2017-01-17 08:12:27 +08:00
|
|
|
auto CurStyle = format::getStyle(Style, FilePath, "LLVM");
|
|
|
|
if (!CurStyle) {
|
|
|
|
llvm::errs() << llvm::toString(CurStyle.takeError()) << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-11 21:53:12 +08:00
|
|
|
auto NewReplacements =
|
2017-01-17 08:12:27 +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
|