1//===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// Implements tools to support refactorings.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Tooling/Refactoring.h"
14#include "clang/Basic/DiagnosticOptions.h"
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Format/Format.h"
18#include "clang/Frontend/TextDiagnosticPrinter.h"
19#include "clang/Lex/Lexer.h"
20#include "clang/Rewrite/Core/Rewriter.h"
21#include "llvm/Support/Path.h"
22#include "llvm/Support/raw_os_ostream.h"
23
24namespace clang {
25namespace tooling {
26
27RefactoringTool::RefactoringTool(
28 const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
29 std::shared_ptr<PCHContainerOperations> PCHContainerOps)
30 : ClangTool(Compilations, SourcePaths, std::move(PCHContainerOps)) {}
31
32std::map<std::string, Replacements> &RefactoringTool::getReplacements() {
33 return FileToReplaces;
34}
35
36int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
37 if (int Result = run(Action: ActionFactory)) {
38 return Result;
39 }
40
41 LangOptions DefaultLangOptions;
42 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
43 TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
44 DiagnosticsEngine Diagnostics(
45 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
46 &*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 bool Result = true;
59 for (const auto &Entry : groupReplacementsByFile(
60 FileMgr&: Rewrite.getSourceMgr().getFileManager(), FileToReplaces))
61 Result = tooling::applyAllReplacements(Replaces: Entry.second, Rewrite) && Result;
62 return Result;
63}
64
65int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
66 return Rewrite.overwriteChangedFiles() ? 1 : 0;
67}
68
69bool formatAndApplyAllReplacements(
70 const std::map<std::string, Replacements> &FileToReplaces,
71 Rewriter &Rewrite, StringRef Style) {
72 SourceManager &SM = Rewrite.getSourceMgr();
73 FileManager &Files = SM.getFileManager();
74
75 bool Result = true;
76 for (const auto &FileAndReplaces : groupReplacementsByFile(
77 FileMgr&: Rewrite.getSourceMgr().getFileManager(), FileToReplaces)) {
78 const std::string &FilePath = FileAndReplaces.first;
79 auto &CurReplaces = FileAndReplaces.second;
80
81 FileEntryRef Entry = llvm::cantFail(ValOrErr: Files.getFileRef(Filename: FilePath));
82 FileID ID = SM.getOrCreateFileID(SourceFile: Entry, FileCharacter: SrcMgr::C_User);
83 StringRef Code = SM.getBufferData(FID: ID);
84
85 auto CurStyle = format::getStyle(StyleName: Style, FileName: FilePath, FallbackStyle: "LLVM");
86 if (!CurStyle) {
87 llvm::errs() << llvm::toString(E: CurStyle.takeError()) << "\n";
88 return false;
89 }
90
91 auto NewReplacements =
92 format::formatReplacements(Code, Replaces: CurReplaces, Style: *CurStyle);
93 if (!NewReplacements) {
94 llvm::errs() << llvm::toString(E: NewReplacements.takeError()) << "\n";
95 return false;
96 }
97 Result = applyAllReplacements(Replaces: *NewReplacements, Rewrite) && Result;
98 }
99 return Result;
100}
101
102} // end namespace tooling
103} // end namespace clang
104