blob: d45cd8c57f10e92bbb794fce1229f72fadf3ab51 [file] [log] [blame]
Manuel Klimek3f001342012-05-23 16:29:20 +00001//===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Manuel Klimek3f001342012-05-23 16:29:20 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Implements tools to support refactorings.
10//
11//===----------------------------------------------------------------------===//
12
Mehdi Amini9670f842016-07-18 19:02:11 +000013#include "clang/Tooling/Refactoring.h"
Douglas Gregor811db4e2012-10-23 22:26:28 +000014#include "clang/Basic/DiagnosticOptions.h"
Manuel Klimek3f001342012-05-23 16:29:20 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
Eric Liu4c1ef97a2016-03-29 16:31:53 +000017#include "clang/Format/Format.h"
Manuel Klimek3f001342012-05-23 16:29:20 +000018#include "clang/Frontend/TextDiagnosticPrinter.h"
19#include "clang/Lex/Lexer.h"
Ted Kremenekcdf81492012-09-01 05:09:24 +000020#include "clang/Rewrite/Core/Rewriter.h"
Ariel J. Bernal31c181b2013-10-01 14:59:00 +000021#include "llvm/Support/Path.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000022#include "llvm/Support/raw_os_ostream.h"
Manuel Klimek3f001342012-05-23 16:29:20 +000023
24namespace clang {
25namespace tooling {
26
Adrian Prantlbb165fb2015-06-20 18:53:08 +000027RefactoringTool::RefactoringTool(
28 const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
29 std::shared_ptr<PCHContainerOperations> PCHContainerOps)
Benjamin Kramerf6021ec2017-03-21 21:35:04 +000030 : ClangTool(Compilations, SourcePaths, std::move(PCHContainerOps)) {}
Edwin Vane5038ac02013-01-11 17:04:55 +000031
Eric Liu40ef2fb2016-08-01 10:16:37 +000032std::map<std::string, Replacements> &RefactoringTool::getReplacements() {
33 return FileToReplaces;
34}
Edwin Vane5038ac02013-01-11 17:04:55 +000035
36int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
37 if (int Result = run(ActionFactory)) {
38 return Result;
39 }
40
41 LangOptions DefaultLangOptions;
42 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
43 TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
44 DiagnosticsEngine Diagnostics(
Dmitri Gribenkof8579502013-01-12 19:30:44 +000045 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
Edwin Vane5038ac02013-01-11 17:04:55 +000046 &*DiagOpts, &DiagnosticPrinter, false);
47 SourceManager Sources(Diagnostics, getFiles());
48 Rewriter Rewrite(Sources, DefaultLangOptions);
49
50 if (!applyAllReplacements(Rewrite)) {
51 llvm::errs() << "Skipped some replacements.\n";
52 }
53
54 return saveRewrittenFiles(Rewrite);
55}
56
57bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
Eric Liu40ef2fb2016-08-01 10:16:37 +000058 bool Result = true;
Eric Liucf2913c2016-11-07 06:08:23 +000059 for (const auto &Entry : groupReplacementsByFile(
60 Rewrite.getSourceMgr().getFileManager(), FileToReplaces))
Eric Liu40ef2fb2016-08-01 10:16:37 +000061 Result = tooling::applyAllReplacements(Entry.second, Rewrite) && Result;
62 return Result;
Edwin Vane5038ac02013-01-11 17:04:55 +000063}
64
65int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
Alp Tokera23d2662013-10-29 08:32:41 +000066 return Rewrite.overwriteChangedFiles() ? 1 : 0;
Manuel Klimek3f001342012-05-23 16:29:20 +000067}
68
Eric Liu40ef2fb2016-08-01 10:16:37 +000069bool formatAndApplyAllReplacements(
Antonio Maiorano3adfb6a2017-01-17 00:12:27 +000070 const std::map<std::string, Replacements> &FileToReplaces,
71 Rewriter &Rewrite, StringRef Style) {
Eric Liu4c1ef97a2016-03-29 16:31:53 +000072 SourceManager &SM = Rewrite.getSourceMgr();
73 FileManager &Files = SM.getFileManager();
74
Eric Liu4c1ef97a2016-03-29 16:31:53 +000075 bool Result = true;
Eric Liucf2913c2016-11-07 06:08:23 +000076 for (const auto &FileAndReplaces : groupReplacementsByFile(
77 Rewrite.getSourceMgr().getFileManager(), FileToReplaces)) {
Benjamin Kramer442b9a92016-05-29 11:04:56 +000078 const std::string &FilePath = FileAndReplaces.first;
Eric Liu4c1ef97a2016-03-29 16:31:53 +000079 auto &CurReplaces = FileAndReplaces.second;
80
Harlan Haskins8d323d12019-08-01 21:31:56 +000081 const FileEntry *Entry = nullptr;
82 if (auto File = Files.getFile(FilePath))
83 Entry = *File;
84
Eric Liu4c1ef97a2016-03-29 16:31:53 +000085 FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
86 StringRef Code = SM.getBufferData(ID);
87
Antonio Maiorano3adfb6a2017-01-17 00:12:27 +000088 auto CurStyle = format::getStyle(Style, FilePath, "LLVM");
89 if (!CurStyle) {
90 llvm::errs() << llvm::toString(CurStyle.takeError()) << "\n";
91 return false;
92 }
93
Eric Liu4f8d9942016-07-11 13:53:12 +000094 auto NewReplacements =
Antonio Maiorano3adfb6a2017-01-17 00:12:27 +000095 format::formatReplacements(Code, CurReplaces, *CurStyle);
Eric Liu4f8d9942016-07-11 13:53:12 +000096 if (!NewReplacements) {
97 llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n";
98 return false;
99 }
100 Result = applyAllReplacements(*NewReplacements, Rewrite) && Result;
Eric Liu4c1ef97a2016-03-29 16:31:53 +0000101 }
102 return Result;
103}
104
Manuel Klimek3f001342012-05-23 16:29:20 +0000105} // end namespace tooling
106} // end namespace clang