Refactoring.cpp 3.2 KB

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