Coverage Report

Created: 2020-09-15 12:33

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/Transformer/Transformer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Transformer.cpp - Transformer library implementation ---*- C++ -*-===//
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
#include "clang/Tooling/Transformer/Transformer.h"
10
#include "clang/ASTMatchers/ASTMatchFinder.h"
11
#include "clang/ASTMatchers/ASTMatchersInternal.h"
12
#include "clang/Basic/SourceLocation.h"
13
#include "clang/Tooling/Refactoring/AtomicChange.h"
14
#include "llvm/Support/Error.h"
15
#include <map>
16
#include <utility>
17
#include <vector>
18
19
using namespace clang;
20
using namespace tooling;
21
22
using ast_matchers::MatchFinder;
23
24
58
void Transformer::registerMatchers(MatchFinder *MatchFinder) {
25
58
  for (auto &Matcher : transformer::detail::buildMatchers(Rule))
26
59
    MatchFinder->addDynamicMatcher(Matcher, this);
27
58
}
28
29
69
void Transformer::run(const MatchFinder::MatchResult &Result) {
30
69
  if (Result.Context->getDiagnostics().hasErrorOccurred())
31
1
    return;
32
68
33
68
  transformer::RewriteRule::Case Case =
34
68
      transformer::detail::findSelectedCase(Result, Rule);
35
68
  auto Transformations = Case.Edits(Result);
36
68
  if (!Transformations) {
37
5
    Consumer(Transformations.takeError());
38
5
    return;
39
5
  }
40
63
41
63
  if (Transformations->empty())
42
3
    return;
43
60
44
  // Group the transformations, by file, into AtomicChanges, each anchored by
45
  // the location of the first change in that file.
46
60
  std::map<FileID, AtomicChange> ChangesByFileID;
47
97
  for (const auto &T : *Transformations) {
48
97
    auto ID = Result.SourceManager->getFileID(T.Range.getBegin());
49
97
    auto Iter = ChangesByFileID
50
97
                    .emplace(ID, AtomicChange(*Result.SourceManager,
51
97
                                              T.Range.getBegin(), T.Metadata))
52
97
                    .first;
53
97
    auto &AC = Iter->second;
54
97
    switch (T.Kind) {
55
92
    case transformer::EditKind::Range:
56
92
      if (auto Err =
57
1
              AC.replace(*Result.SourceManager, T.Range, T.Replacement)) {
58
1
        Consumer(std::move(Err));
59
1
        return;
60
1
      }
61
91
      break;
62
5
    case transformer::EditKind::AddInclude:
63
5
      AC.addHeader(T.Replacement);
64
5
      break;
65
97
    }
66
97
  }
67
60
68
59
  for (auto &IDChangePair : ChangesByFileID)
69
59
    Consumer(std::move(IDChangePair.second));
70
59
}