]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Tooling/Refactoring.cpp
Merge clang trunk r300422 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Tooling / Refactoring.cpp
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, std::move(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 : groupReplacementsByFile(
61            Rewrite.getSourceMgr().getFileManager(), FileToReplaces))
62     Result = tooling::applyAllReplacements(Entry.second, Rewrite) && Result;
63   return Result;
64 }
65
66 int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
67   return Rewrite.overwriteChangedFiles() ? 1 : 0;
68 }
69
70 bool formatAndApplyAllReplacements(
71     const std::map<std::string, Replacements> &FileToReplaces,
72     Rewriter &Rewrite, StringRef Style) {
73   SourceManager &SM = Rewrite.getSourceMgr();
74   FileManager &Files = SM.getFileManager();
75
76   bool Result = true;
77   for (const auto &FileAndReplaces : groupReplacementsByFile(
78            Rewrite.getSourceMgr().getFileManager(), FileToReplaces)) {
79     const std::string &FilePath = FileAndReplaces.first;
80     auto &CurReplaces = FileAndReplaces.second;
81
82     const FileEntry *Entry = Files.getFile(FilePath);
83     FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
84     StringRef Code = SM.getBufferData(ID);
85
86     auto CurStyle = format::getStyle(Style, FilePath, "LLVM");
87     if (!CurStyle) {
88       llvm::errs() << llvm::toString(CurStyle.takeError()) << "\n";
89       return false;
90     }
91
92     auto NewReplacements =
93         format::formatReplacements(Code, CurReplaces, *CurStyle);
94     if (!NewReplacements) {
95       llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n";
96       return false;
97     }
98     Result = applyAllReplacements(*NewReplacements, Rewrite) && Result;
99   }
100   return Result;
101 }
102
103 } // end namespace tooling
104 } // end namespace clang