blob: 3a1527558584790872176b84b4d769064703b968 [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
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"
Manuel Klimek3f001342012-05-23 16:29:20 +000021#include "clang/Tooling/Refactoring.h"
Ariel J. Bernal31c181b2013-10-01 14:59:00 +000022#include "llvm/Support/FileSystem.h"
23#include "llvm/Support/Path.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000024#include "llvm/Support/raw_os_ostream.h"
Manuel Klimek3f001342012-05-23 16:29:20 +000025
26namespace clang {
27namespace tooling {
28
Adrian Prantlbb165fb2015-06-20 18:53:08 +000029RefactoringTool::RefactoringTool(
30 const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
31 std::shared_ptr<PCHContainerOperations> PCHContainerOps)
32 : ClangTool(Compilations, SourcePaths, PCHContainerOps) {}
Edwin Vane5038ac02013-01-11 17:04:55 +000033
34Replacements &RefactoringTool::getReplacements() { return Replace; }
35
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) {
58 return tooling::applyAllReplacements(Replace, Rewrite);
59}
60
61int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
Alp Tokera23d2662013-10-29 08:32:41 +000062 return Rewrite.overwriteChangedFiles() ? 1 : 0;
Manuel Klimek3f001342012-05-23 16:29:20 +000063}
64
Eric Liu4c1ef97a2016-03-29 16:31:53 +000065bool formatAndApplyAllReplacements(const Replacements &Replaces,
66 Rewriter &Rewrite, StringRef Style) {
67 SourceManager &SM = Rewrite.getSourceMgr();
68 FileManager &Files = SM.getFileManager();
69
70 auto FileToReplaces = groupReplacementsByFile(Replaces);
71
72 bool Result = true;
73 for (auto &FileAndReplaces : FileToReplaces) {
74 const std::string FilePath = FileAndReplaces.first;
75 auto &CurReplaces = FileAndReplaces.second;
76
77 const FileEntry *Entry = Files.getFile(FilePath);
78 FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
79 StringRef Code = SM.getBufferData(ID);
80
81 format::FormatStyle CurStyle = format::getStyle(Style, FilePath, "LLVM");
82 Replacements NewReplacements =
83 format::formatReplacements(Code, CurReplaces, CurStyle);
84 Result = applyAllReplacements(NewReplacements, Rewrite) && Result;
85 }
86 return Result;
87}
88
Manuel Klimek3f001342012-05-23 16:29:20 +000089} // end namespace tooling
90} // end namespace clang