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
22namespace clang {
23namespace tooling {
24
25RefactoringTool::RefactoringTool(
26 const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
27 std::shared_ptr<PCHContainerOperations> PCHContainerOps)
28 : ClangTool(Compilations, SourcePaths, std::move(PCHContainerOps)) {}
29
30std::map<std::string, Replacements> &RefactoringTool::getReplacements() {
31 return FileToReplaces;
32}
33
34int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
35 if (int Result = run(Action: ActionFactory)) {
36 return Result;
37 }
38
39 LangOptions DefaultLangOptions;
40 DiagnosticOptions DiagOpts;
41 TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), DiagOpts);
42 DiagnosticsEngine Diagnostics(DiagnosticIDs::create(), DiagOpts,
43 &DiagnosticPrinter, false);
44 SourceManager Sources(Diagnostics, getFiles());
45 Rewriter Rewrite(Sources, DefaultLangOptions);
46
47 if (!applyAllReplacements(Rewrite)) {
48 llvm::errs() << "Skipped some replacements.\n";
49 }
50
51 return saveRewrittenFiles(Rewrite);
52}
53
54bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
55 bool Result = true;
56 for (const auto &Entry : groupReplacementsByFile(
57 FileMgr&: Rewrite.getSourceMgr().getFileManager(), FileToReplaces))
58 Result = tooling::applyAllReplacements(Replaces: Entry.second, Rewrite) && Result;
59 return Result;
60}
61
62int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
63 return Rewrite.overwriteChangedFiles() ? 1 : 0;
64}
65
66bool formatAndApplyAllReplacements(
67 const std::map<std::string, Replacements> &FileToReplaces,
68 Rewriter &Rewrite, StringRef Style) {
69 SourceManager &SM = Rewrite.getSourceMgr();
70 FileManager &Files = SM.getFileManager();
71
72 bool Result = true;
73 for (const auto &FileAndReplaces : groupReplacementsByFile(
74 FileMgr&: Rewrite.getSourceMgr().getFileManager(), FileToReplaces)) {
75 const std::string &FilePath = FileAndReplaces.first;
76 auto &CurReplaces = FileAndReplaces.second;
77
78 FileEntryRef Entry = llvm::cantFail(ValOrErr: Files.getFileRef(Filename: FilePath));
79 FileID ID = SM.getOrCreateFileID(SourceFile: Entry, FileCharacter: SrcMgr::C_User);
80 StringRef Code = SM.getBufferData(FID: ID);
81
82 auto CurStyle = format::getStyle(StyleName: Style, FileName: FilePath, FallbackStyle: "LLVM");
83 if (!CurStyle) {
84 llvm::errs() << llvm::toString(E: CurStyle.takeError()) << "\n";
85 return false;
86 }
87
88 auto NewReplacements =
89 format::formatReplacements(Code, Replaces: CurReplaces, Style: *CurStyle);
90 if (!NewReplacements) {
91 llvm::errs() << llvm::toString(E: NewReplacements.takeError()) << "\n";
92 return false;
93 }
94 Result = applyAllReplacements(Replaces: *NewReplacements, Rewrite) && Result;
95 }
96 return Result;
97}
98
99} // end namespace tooling
100} // end namespace clang
101