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