Home | History | Annotate | Download | only in Tooling
      1 //===--- 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 
     14 #include "clang/Basic/DiagnosticOptions.h"
     15 #include "clang/Basic/FileManager.h"
     16 #include "clang/Basic/SourceManager.h"
     17 #include "clang/Format/Format.h"
     18 #include "clang/Frontend/TextDiagnosticPrinter.h"
     19 #include "clang/Lex/Lexer.h"
     20 #include "clang/Rewrite/Core/Rewriter.h"
     21 #include "clang/Tooling/Refactoring.h"
     22 #include "llvm/Support/FileSystem.h"
     23 #include "llvm/Support/Path.h"
     24 #include "llvm/Support/raw_os_ostream.h"
     25 
     26 namespace clang {
     27 namespace tooling {
     28 
     29 RefactoringTool::RefactoringTool(
     30     const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
     31     std::shared_ptr<PCHContainerOperations> PCHContainerOps)
     32     : ClangTool(Compilations, SourcePaths, PCHContainerOps) {}
     33 
     34 Replacements &RefactoringTool::getReplacements() { return Replace; }
     35 
     36 int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
     37   if (int Result = run(ActionFactory)) {
     38     return Result;
     39   }
     40 
     41   LangOptions DefaultLangOptions;
     42   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
     43   TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
     44   DiagnosticsEngine Diagnostics(
     45       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
     46       &*DiagOpts, &DiagnosticPrinter, false);
     47   SourceManager Sources(Diagnostics, getFiles());
     48   Rewriter Rewrite(Sources, DefaultLangOptions);
     49 
     50   if (!applyAllReplacements(Rewrite)) {
     51     llvm::errs() << "Skipped some replacements.\n";
     52   }
     53 
     54   return saveRewrittenFiles(Rewrite);
     55 }
     56 
     57 bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
     58   return tooling::applyAllReplacements(Replace, Rewrite);
     59 }
     60 
     61 int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
     62   return Rewrite.overwriteChangedFiles() ? 1 : 0;
     63 }
     64 
     65 bool formatAndApplyAllReplacements(const Replacements &Replaces,
     66                                    Rewriter &Rewrite, StringRef Style) {
     67   SourceManager &SM = Rewrite.getSourceMgr();
     68   FileManager &Files = SM.getFileManager();
     69 
     70   auto FileToReplaces = groupReplacementsByFile(Replaces);
     71 
     72   bool Result = true;
     73   for (const auto &FileAndReplaces : FileToReplaces) {
     74     const std::string &FilePath = FileAndReplaces.first;
     75     auto &CurReplaces = FileAndReplaces.second;
     76 
     77     const FileEntry *Entry = Files.getFile(FilePath);
     78     FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
     79     StringRef Code = SM.getBufferData(ID);
     80 
     81     format::FormatStyle CurStyle = format::getStyle(Style, FilePath, "LLVM");
     82     auto NewReplacements =
     83         format::formatReplacements(Code, CurReplaces, CurStyle);
     84     if (!NewReplacements) {
     85       llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n";
     86       return false;
     87     }
     88     Result = applyAllReplacements(*NewReplacements, Rewrite) && Result;
     89   }
     90   return Result;
     91 }
     92 
     93 } // end namespace tooling
     94 } // end namespace clang
     95