clang API Documentation

Refactoring.cpp
Go to the documentation of this file.
00001 //===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  Implements tools to support refactorings.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Basic/DiagnosticOptions.h"
00015 #include "clang/Basic/FileManager.h"
00016 #include "clang/Basic/SourceManager.h"
00017 #include "clang/Frontend/TextDiagnosticPrinter.h"
00018 #include "clang/Lex/Lexer.h"
00019 #include "clang/Rewrite/Core/Rewriter.h"
00020 #include "clang/Tooling/Refactoring.h"
00021 #include "llvm/Support/FileSystem.h"
00022 #include "llvm/Support/Path.h"
00023 #include "llvm/Support/raw_os_ostream.h"
00024 
00025 namespace clang {
00026 namespace tooling {
00027 
00028 RefactoringTool::RefactoringTool(const CompilationDatabase &Compilations,
00029                                  ArrayRef<std::string> SourcePaths)
00030   : ClangTool(Compilations, SourcePaths) {}
00031 
00032 Replacements &RefactoringTool::getReplacements() { return Replace; }
00033 
00034 int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
00035   if (int Result = run(ActionFactory)) {
00036     return Result;
00037   }
00038 
00039   LangOptions DefaultLangOptions;
00040   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
00041   TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
00042   DiagnosticsEngine Diagnostics(
00043       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
00044       &*DiagOpts, &DiagnosticPrinter, false);
00045   SourceManager Sources(Diagnostics, getFiles());
00046   Rewriter Rewrite(Sources, DefaultLangOptions);
00047 
00048   if (!applyAllReplacements(Rewrite)) {
00049     llvm::errs() << "Skipped some replacements.\n";
00050   }
00051 
00052   return saveRewrittenFiles(Rewrite);
00053 }
00054 
00055 bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
00056   return tooling::applyAllReplacements(Replace, Rewrite);
00057 }
00058 
00059 int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
00060   return Rewrite.overwriteChangedFiles() ? 1 : 0;
00061 }
00062 
00063 } // end namespace tooling
00064 } // end namespace clang