xref: /llvm-project/clang/lib/Tooling/Refactoring.cpp (revision 9670f847b8c5f6eb42c3df74035809f95465b5fb)
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 Replacements &RefactoringTool::getReplacements() { return Replace; }
34 
35 int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
36   if (int Result = run(ActionFactory)) {
37     return Result;
38   }
39 
40   LangOptions DefaultLangOptions;
41   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
42   TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
43   DiagnosticsEngine Diagnostics(
44       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
45       &*DiagOpts, &DiagnosticPrinter, false);
46   SourceManager Sources(Diagnostics, getFiles());
47   Rewriter Rewrite(Sources, DefaultLangOptions);
48 
49   if (!applyAllReplacements(Rewrite)) {
50     llvm::errs() << "Skipped some replacements.\n";
51   }
52 
53   return saveRewrittenFiles(Rewrite);
54 }
55 
56 bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
57   return tooling::applyAllReplacements(Replace, Rewrite);
58 }
59 
60 int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
61   return Rewrite.overwriteChangedFiles() ? 1 : 0;
62 }
63 
64 bool formatAndApplyAllReplacements(const Replacements &Replaces,
65                                    Rewriter &Rewrite, StringRef Style) {
66   SourceManager &SM = Rewrite.getSourceMgr();
67   FileManager &Files = SM.getFileManager();
68 
69   auto FileToReplaces = groupReplacementsByFile(Replaces);
70 
71   bool Result = true;
72   for (const auto &FileAndReplaces : FileToReplaces) {
73     const std::string &FilePath = FileAndReplaces.first;
74     auto &CurReplaces = FileAndReplaces.second;
75 
76     const FileEntry *Entry = Files.getFile(FilePath);
77     FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
78     StringRef Code = SM.getBufferData(ID);
79 
80     format::FormatStyle CurStyle = format::getStyle(Style, FilePath, "LLVM");
81     auto NewReplacements =
82         format::formatReplacements(Code, CurReplaces, CurStyle);
83     if (!NewReplacements) {
84       llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n";
85       return false;
86     }
87     Result = applyAllReplacements(*NewReplacements, Rewrite) && Result;
88   }
89   return Result;
90 }
91 
92 } // end namespace tooling
93 } // end namespace clang
94