Coverage Report

Created: 2020-02-18 08:44

/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 <utility>
16
#include <vector>
17
18
using namespace clang;
19
using namespace tooling;
20
21
using ast_matchers::MatchFinder;
22
23
33
void Transformer::registerMatchers(MatchFinder *MatchFinder) {
24
33
  for (auto &Matcher : transformer::detail::buildMatchers(Rule))
25
34
    MatchFinder->addDynamicMatcher(Matcher, this);
26
33
}
27
28
42
void Transformer::run(const MatchFinder::MatchResult &Result) {
29
42
  if (Result.Context->getDiagnostics().hasErrorOccurred())
30
1
    return;
31
41
32
41
  transformer::RewriteRule::Case Case =
33
41
      transformer::detail::findSelectedCase(Result, Rule);
34
41
  auto Transformations = transformer::detail::translateEdits(Result, Case.Edits);
35
41
  if (!Transformations) {
36
3
    Consumer(Transformations.takeError());
37
3
    return;
38
3
  }
39
38
40
38
  if (Transformations->empty()) {
41
2
    // No rewrite applied (but no error encountered either).
42
2
    transformer::detail::getRuleMatchLoc(Result).print(
43
2
        llvm::errs() << "note: skipping match at loc ", *Result.SourceManager);
44
2
    llvm::errs() << "\n";
45
2
    return;
46
2
  }
47
36
48
36
  // Record the results in the AtomicChange, anchored at the location of the
49
36
  // first change.
50
36
  AtomicChange AC(*Result.SourceManager,
51
36
                  (*Transformations)[0].Range.getBegin());
52
39
  for (const auto &T : *Transformations) {
53
39
    if (auto Err = AC.replace(*Result.SourceManager, T.Range, T.Replacement)) {
54
1
      Consumer(std::move(Err));
55
1
      return;
56
1
    }
57
39
  }
58
36
59
36
  
for (const auto &I : Case.AddedIncludes)35
{
60
2
    auto &Header = I.first;
61
2
    switch (I.second) {
62
1
    case transformer::IncludeFormat::Quoted:
63
1
      AC.addHeader(Header);
64
1
      break;
65
1
    case transformer::IncludeFormat::Angled:
66
1
      AC.addHeader((llvm::Twine("<") + Header + ">").str());
67
1
      break;
68
2
    }
69
2
  }
70
35
71
35
  Consumer(std::move(AC));
72
35
}