1 //===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements tools to support refactorings.
12 //===----------------------------------------------------------------------===//
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"
29 RefactoringTool::RefactoringTool(
30 const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
31 std::shared_ptr<PCHContainerOperations> PCHContainerOps)
32 : ClangTool(Compilations, SourcePaths, PCHContainerOps) {}
34 Replacements &RefactoringTool::getReplacements() { return Replace; }
36 int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
37 if (int Result = run(ActionFactory)) {
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);
50 if (!applyAllReplacements(Rewrite)) {
51 llvm::errs() << "Skipped some replacements.\n";
54 return saveRewrittenFiles(Rewrite);
57 bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
58 return tooling::applyAllReplacements(Replace, Rewrite);
61 int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
62 return Rewrite.overwriteChangedFiles() ? 1 : 0;
65 bool formatAndApplyAllReplacements(const Replacements &Replaces,
66 Rewriter &Rewrite, StringRef Style) {
67 SourceManager &SM = Rewrite.getSourceMgr();
68 FileManager &Files = SM.getFileManager();
70 auto FileToReplaces = groupReplacementsByFile(Replaces);
73 for (const auto &FileAndReplaces : FileToReplaces) {
74 const std::string &FilePath = FileAndReplaces.first;
75 auto &CurReplaces = FileAndReplaces.second;
77 const FileEntry *Entry = Files.getFile(FilePath);
78 FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
79 StringRef Code = SM.getBufferData(ID);
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";
88 Result = applyAllReplacements(*NewReplacements, Rewrite) && Result;
93 } // end namespace tooling
94 } // end namespace clang