blob: d48713c69f0b5d32a19aaffef6e5ef8d983afbaf [file] [log] [blame]
Manuel Klimek3f001342012-05-23 16:29:20 +00001//===--- 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
Mehdi Amini9670f842016-07-18 19:02:11 +000014#include "clang/Tooling/Refactoring.h"
Douglas Gregor811db4e2012-10-23 22:26:28 +000015#include "clang/Basic/DiagnosticOptions.h"
Manuel Klimek3f001342012-05-23 16:29:20 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/SourceManager.h"
Eric Liu4c1ef97a2016-03-29 16:31:53 +000018#include "clang/Format/Format.h"
Manuel Klimek3f001342012-05-23 16:29:20 +000019#include "clang/Frontend/TextDiagnosticPrinter.h"
20#include "clang/Lex/Lexer.h"
Ted Kremenekcdf81492012-09-01 05:09:24 +000021#include "clang/Rewrite/Core/Rewriter.h"
Ariel J. Bernal31c181b2013-10-01 14:59:00 +000022#include "llvm/Support/Path.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000023#include "llvm/Support/raw_os_ostream.h"
Manuel Klimek3f001342012-05-23 16:29:20 +000024
25namespace clang {
26namespace tooling {
27
Adrian Prantlbb165fb2015-06-20 18:53:08 +000028RefactoringTool::RefactoringTool(
29 const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
30 std::shared_ptr<PCHContainerOperations> PCHContainerOps)
31 : ClangTool(Compilations, SourcePaths, PCHContainerOps) {}
Edwin Vane5038ac02013-01-11 17:04:55 +000032
33Replacements &RefactoringTool::getReplacements() { return Replace; }
34
35int 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(
Dmitri Gribenkof8579502013-01-12 19:30:44 +000044 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
Edwin Vane5038ac02013-01-11 17:04:55 +000045 &*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
56bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
57 return tooling::applyAllReplacements(Replace, Rewrite);
58}
59
60int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
Alp Tokera23d2662013-10-29 08:32:41 +000061 return Rewrite.overwriteChangedFiles() ? 1 : 0;
Manuel Klimek3f001342012-05-23 16:29:20 +000062}
63
Eric Liu4c1ef97a2016-03-29 16:31:53 +000064bool 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;
Benjamin Kramer442b9a92016-05-29 11:04:56 +000072 for (const auto &FileAndReplaces : FileToReplaces) {
73 const std::string &FilePath = FileAndReplaces.first;
Eric Liu4c1ef97a2016-03-29 16:31:53 +000074 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");
Eric Liu4f8d9942016-07-11 13:53:12 +000081 auto NewReplacements =
Eric Liu4c1ef97a2016-03-29 16:31:53 +000082 format::formatReplacements(Code, CurReplaces, CurStyle);
Eric Liu4f8d9942016-07-11 13:53:12 +000083 if (!NewReplacements) {
84 llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n";
85 return false;
86 }
87 Result = applyAllReplacements(*NewReplacements, Rewrite) && Result;
Eric Liu4c1ef97a2016-03-29 16:31:53 +000088 }
89 return Result;
90}
91
Manuel Klimek3f001342012-05-23 16:29:20 +000092} // end namespace tooling
93} // end namespace clang