blob: d32452f6f2939efe7b7fafa6959176d200d34b52 [file] [log] [blame]
Manuel Klimek3f001342012-05-23 16:29:20 +00001//===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//
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// Implements tools to support refactorings.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor811db4e2012-10-23 22:26:28 +000014#include "clang/Basic/DiagnosticOptions.h"
Manuel Klimek3f001342012-05-23 16:29:20 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
Manuel Klimek3f001342012-05-23 16:29:20 +000017#include "clang/Frontend/TextDiagnosticPrinter.h"
18#include "clang/Lex/Lexer.h"
Ted Kremenekcdf81492012-09-01 05:09:24 +000019#include "clang/Rewrite/Core/Rewriter.h"
Manuel Klimek3f001342012-05-23 16:29:20 +000020#include "clang/Tooling/Refactoring.h"
Ariel J. Bernal31c181b2013-10-01 14:59:00 +000021#include "llvm/Support/FileSystem.h"
22#include "llvm/Support/Path.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000023#include "llvm/Support/raw_os_ostream.h"
Manuel Klimek3f001342012-05-23 16:29:20 +000024
25namespace clang {
26namespace tooling {
27
Adrian Prantlbb165fb2015-06-20 18:53:08 +000028RefactoringTool::RefactoringTool(
29 const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
30 std::shared_ptr<PCHContainerOperations> PCHContainerOps)
31 : ClangTool(Compilations, SourcePaths, PCHContainerOps) {}
Edwin Vane5038ac02013-01-11 17:04:55 +000032
33Replacements &RefactoringTool::getReplacements() { return Replace; }
34
35int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
36 if (int Result = run(ActionFactory)) {
37 return Result;
38 }
39
40 LangOptions DefaultLangOptions;
41 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
42 TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
43 DiagnosticsEngine Diagnostics(
Dmitri Gribenkof8579502013-01-12 19:30:44 +000044 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
Edwin Vane5038ac02013-01-11 17:04:55 +000045 &*DiagOpts, &DiagnosticPrinter, false);
46 SourceManager Sources(Diagnostics, getFiles());
47 Rewriter Rewrite(Sources, DefaultLangOptions);
48
49 if (!applyAllReplacements(Rewrite)) {
50 llvm::errs() << "Skipped some replacements.\n";
51 }
52
53 return saveRewrittenFiles(Rewrite);
54}
55
56bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
57 return tooling::applyAllReplacements(Replace, Rewrite);
58}
59
60int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
Alp Tokera23d2662013-10-29 08:32:41 +000061 return Rewrite.overwriteChangedFiles() ? 1 : 0;
Manuel Klimek3f001342012-05-23 16:29:20 +000062}
63
64} // end namespace tooling
65} // end namespace clang