blob: c8173060d8c11c30d369ca784cd58fb563f8a764 [file] [log] [blame]
Manuel Klimekf9d4cbd2012-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 Gregor02c23eb2012-10-23 22:26:28 +000014#include "clang/Basic/DiagnosticOptions.h"
Manuel Klimekf9d4cbd2012-05-23 16:29:20 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
Manuel Klimekf9d4cbd2012-05-23 16:29:20 +000017#include "clang/Frontend/TextDiagnosticPrinter.h"
18#include "clang/Lex/Lexer.h"
Ted Kremenek305c6132012-09-01 05:09:24 +000019#include "clang/Rewrite/Core/Rewriter.h"
Manuel Klimekf9d4cbd2012-05-23 16:29:20 +000020#include "clang/Tooling/Refactoring.h"
Ariel J. Bernald11344a2013-10-01 14:59:00 +000021#include "llvm/Support/FileSystem.h"
22#include "llvm/Support/Path.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070023#include "llvm/Support/raw_os_ostream.h"
Manuel Klimekf9d4cbd2012-05-23 16:29:20 +000024
25namespace clang {
26namespace tooling {
27
Edwin Vaned088a5f2013-01-11 17:04:55 +000028RefactoringTool::RefactoringTool(const CompilationDatabase &Compilations,
29 ArrayRef<std::string> SourcePaths)
30 : ClangTool(Compilations, SourcePaths) {}
31
32Replacements &RefactoringTool::getReplacements() { return Replace; }
33
34int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
35 if (int Result = run(ActionFactory)) {
36 return Result;
37 }
38
39 LangOptions DefaultLangOptions;
40 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
41 TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
42 DiagnosticsEngine Diagnostics(
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000043 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
Edwin Vaned088a5f2013-01-11 17:04:55 +000044 &*DiagOpts, &DiagnosticPrinter, false);
45 SourceManager Sources(Diagnostics, getFiles());
46 Rewriter Rewrite(Sources, DefaultLangOptions);
47
48 if (!applyAllReplacements(Rewrite)) {
49 llvm::errs() << "Skipped some replacements.\n";
50 }
51
52 return saveRewrittenFiles(Rewrite);
53}
54
55bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
56 return tooling::applyAllReplacements(Replace, Rewrite);
57}
58
59int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
Alp Toker6bf97fb2013-10-29 08:32:41 +000060 return Rewrite.overwriteChangedFiles() ? 1 : 0;
Manuel Klimekf9d4cbd2012-05-23 16:29:20 +000061}
62
63} // end namespace tooling
64} // end namespace clang