Coverage Report

Created: 2020-09-22 08:39

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/Refactoring.cpp
Line
Count
Source (jump to first uncovered line)
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
24
namespace clang {
25
namespace tooling {
26
27
RefactoringTool::RefactoringTool(
28
    const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
29
    std::shared_ptr<PCHContainerOperations> PCHContainerOps)
30
62
    : ClangTool(Compilations, SourcePaths, std::move(PCHContainerOps)) {}
31
32
61
std::map<std::string, Replacements> &RefactoringTool::getReplacements() {
33
61
  return FileToReplaces;
34
61
}
35
36
0
int RefactoringTool::runAndSave(FrontendActionFactory *ActionFactory) {
37
0
  if (int Result = run(ActionFactory)) {
38
0
    return Result;
39
0
  }
40
41
0
  LangOptions DefaultLangOptions;
42
0
  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
43
0
  TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
44
0
  DiagnosticsEngine Diagnostics(
45
0
      IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()),
46
0
      &*DiagOpts, &DiagnosticPrinter, false);
47
0
  SourceManager Sources(Diagnostics, getFiles());
48
0
  Rewriter Rewrite(Sources, DefaultLangOptions);
49
50
0
  if (!applyAllReplacements(Rewrite)) {
51
0
    llvm::errs() << "Skipped some replacements.\n";
52
0
  }
53
54
0
  return saveRewrittenFiles(Rewrite);
55
0
}
56
57
61
bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) {
58
61
  bool Result = true;
59
61
  for (const auto &Entry : groupReplacementsByFile(
60
61
           Rewrite.getSourceMgr().getFileManager(), FileToReplaces))
61
60
    Result = tooling::applyAllReplacements(Entry.second, Rewrite) && Result;
62
61
  return Result;
63
61
}
64
65
0
int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
66
0
  return Rewrite.overwriteChangedFiles() ? 1 : 0;
67
0
}
68
69
bool formatAndApplyAllReplacements(
70
    const std::map<std::string, Replacements> &FileToReplaces,
71
260
    Rewriter &Rewrite, StringRef Style) {
72
260
  SourceManager &SM = Rewrite.getSourceMgr();
73
260
  FileManager &Files = SM.getFileManager();
74
75
260
  bool Result = true;
76
260
  for (const auto &FileAndReplaces : groupReplacementsByFile(
77
481
           Rewrite.getSourceMgr().getFileManager(), FileToReplaces)) {
78
481
    const std::string &FilePath = FileAndReplaces.first;
79
481
    auto &CurReplaces = FileAndReplaces.second;
80
81
481
    const FileEntry *Entry = nullptr;
82
481
    if (auto File = Files.getFile(FilePath))
83
481
      Entry = *File;
84
85
481
    FileID ID = SM.getOrCreateFileID(Entry, SrcMgr::C_User);
86
481
    StringRef Code = SM.getBufferData(ID);
87
88
481
    auto CurStyle = format::getStyle(Style, FilePath, "LLVM");
89
481
    if (!CurStyle) {
90
0
      llvm::errs() << llvm::toString(CurStyle.takeError()) << "\n";
91
0
      return false;
92
0
    }
93
94
481
    auto NewReplacements =
95
481
        format::formatReplacements(Code, CurReplaces, *CurStyle);
96
481
    if (!NewReplacements) {
97
0
      llvm::errs() << llvm::toString(NewReplacements.takeError()) << "\n";
98
0
      return false;
99
0
    }
100
481
    Result = applyAllReplacements(*NewReplacements, Rewrite) && Result;
101
481
  }
102
260
  return Result;
103
260
}
104
105
} // end namespace tooling
106
} // end namespace clang