xref: /minix3/external/bsd/llvm/dist/clang/lib/Tooling/Refactoring.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc //  Implements tools to support refactorings.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "clang/Basic/DiagnosticOptions.h"
15f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
16f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
17f4a2713aSLionel Sambuc #include "clang/Frontend/TextDiagnosticPrinter.h"
18f4a2713aSLionel Sambuc #include "clang/Lex/Lexer.h"
19f4a2713aSLionel Sambuc #include "clang/Rewrite/Core/Rewriter.h"
20f4a2713aSLionel Sambuc #include "clang/Tooling/Refactoring.h"
21f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/Support/raw_os_ostream.h"
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc namespace clang {
26f4a2713aSLionel Sambuc namespace tooling {
27f4a2713aSLionel Sambuc 
RefactoringTool(const CompilationDatabase & Compilations,ArrayRef<std::string> SourcePaths)28f4a2713aSLionel Sambuc RefactoringTool::RefactoringTool(const CompilationDatabase &Compilations,
29f4a2713aSLionel Sambuc                                  ArrayRef<std::string> SourcePaths)
30f4a2713aSLionel Sambuc   : ClangTool(Compilations, SourcePaths) {}
31f4a2713aSLionel Sambuc 
getReplacements()32f4a2713aSLionel Sambuc Replacements &RefactoringTool::getReplacements() { return Replace; }
33f4a2713aSLionel Sambuc 
runAndSave(FrontendActionFactory * ActionFactory)34f4a2713aSLionel Sambuc int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
35f4a2713aSLionel Sambuc   if (int Result = run(ActionFactory)) {
36f4a2713aSLionel Sambuc     return Result;
37f4a2713aSLionel Sambuc   }
38f4a2713aSLionel Sambuc 
39f4a2713aSLionel Sambuc   LangOptions DefaultLangOptions;
40f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
41f4a2713aSLionel Sambuc   TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
42f4a2713aSLionel Sambuc   DiagnosticsEngine Diagnostics(
43f4a2713aSLionel Sambuc       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
44f4a2713aSLionel Sambuc       &*DiagOpts, &DiagnosticPrinter, false);
45f4a2713aSLionel Sambuc   SourceManager Sources(Diagnostics, getFiles());
46f4a2713aSLionel Sambuc   Rewriter Rewrite(Sources, DefaultLangOptions);
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc   if (!applyAllReplacements(Rewrite)) {
49f4a2713aSLionel Sambuc     llvm::errs() << "Skipped some replacements.\n";
50f4a2713aSLionel Sambuc   }
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc   return saveRewrittenFiles(Rewrite);
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc 
applyAllReplacements(Rewriter & Rewrite)55f4a2713aSLionel Sambuc bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
56f4a2713aSLionel Sambuc   return tooling::applyAllReplacements(Replace, Rewrite);
57f4a2713aSLionel Sambuc }
58f4a2713aSLionel Sambuc 
saveRewrittenFiles(Rewriter & Rewrite)59f4a2713aSLionel Sambuc int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
60f4a2713aSLionel Sambuc   return Rewrite.overwriteChangedFiles() ? 1 : 0;
61f4a2713aSLionel Sambuc }
62f4a2713aSLionel Sambuc 
63f4a2713aSLionel Sambuc } // end namespace tooling
64f4a2713aSLionel Sambuc } // end namespace clang
65