blob: db34c952d794527cff85302573a641c4510314b0 [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)
Benjamin Kramerf6021ec2017-03-21 21:35:04 +000031 : ClangTool(Compilations, SourcePaths, std::move(PCHContainerOps)) {}
Edwin Vane5038ac02013-01-11 17:04:55 +000032
Eric Liu40ef2fb2016-08-01 10:16:37 +000033std::map<std::string, Replacements> &RefactoringTool::getReplacements() {
34 return FileToReplaces;
35}
Edwin Vane5038ac02013-01-11 17:04:55 +000036
37int 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(
Dmitri Gribenkof8579502013-01-12 19:30:44 +000046 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
Edwin Vane5038ac02013-01-11 17:04:55 +000047 &*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
58bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
Eric Liu40ef2fb2016-08-01 10:16:37 +000059 bool Result = true;
Eric Liucf2913c2016-11-07 06:08:23 +000060 for (const auto &Entry : groupReplacementsByFile(
61 Rewrite.getSourceMgr().getFileManager(), FileToReplaces))
Eric Liu40ef2fb2016-08-01 10:16:37 +000062 Result = tooling::applyAllReplacements(Entry.second, Rewrite) && Result;
63 return Result;
Edwin Vane5038ac02013-01-11 17:04:55 +000064}
65
66int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
Alp Tokera23d2662013-10-29 08:32:41 +000067 return Rewrite.overwriteChangedFiles() ? 1 : 0;
Manuel Klimek3f001342012-05-23 16:29:20 +000068}
69
Eric Liu40ef2fb2016-08-01 10:16:37 +000070bool formatAndApplyAllReplacements(
Antonio Maiorano3adfb6a2017-01-17 00:12:27 +000071 const std::map<std::string, Replacements> &FileToReplaces,
72 Rewriter &Rewrite, StringRef Style) {
Eric Liu4c1ef97a2016-03-29 16:31:53 +000073 SourceManager &SM = Rewrite.getSourceMgr();
74 FileManager &Files = SM.getFileManager();
75
Eric Liu4c1ef97a2016-03-29 16:31:53 +000076 bool Result = true;
Eric Liucf2913c2016-11-07 06:08:23 +000077 for (const auto &FileAndReplaces : groupReplacementsByFile(
78 Rewrite.getSourceMgr().getFileManager(), FileToReplaces)) {
Benjamin Kramer442b9a92016-05-29 11:04:56 +000079 const std::string &FilePath = FileAndReplaces.first;
Eric Liu4c1ef97a2016-03-29 16:31:53 +000080 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
Antonio Maiorano3adfb6a2017-01-17 00:12:27 +000086 auto CurStyle = format::getStyle(Style, FilePath, "LLVM");
87 if (!CurStyle) {
88 llvm::errs() << llvm::toString(CurStyle.takeError()) << "\n";
89 return false;
90 }
91
Eric Liu4f8d9942016-07-11 13:53:12 +000092 auto NewReplacements =
Antonio Maiorano3adfb6a2017-01-17 00:12:27 +000093 format::formatReplacements(Code, CurReplaces, *CurStyle);
Eric Liu4f8d9942016-07-11 13:53:12 +000094 if (!NewReplacements) {
95 llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n";
96 return false;
97 }
98 Result = applyAllReplacements(*NewReplacements, Rewrite) && Result;
Eric Liu4c1ef97a2016-03-29 16:31:53 +000099 }
100 return Result;
101}
102
Manuel Klimek3f001342012-05-23 16:29:20 +0000103} // end namespace tooling
104} // end namespace clang