aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/lib/Tooling/Transformer/Transformer.cpp
blob: f95f2ab7d954d88cc2dc804c1960da3a31ac2db4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//===--- Transformer.cpp - Transformer library implementation ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/Tooling/Transformer/Transformer.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchersInternal.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Tooling/Refactoring/AtomicChange.h"
#include "llvm/Support/Error.h"
#include <map>
#include <utility>
#include <vector>

namespace clang {
namespace tooling {

using ::clang::ast_matchers::MatchFinder;

namespace detail {

void TransformerImpl::onMatch(
    const ast_matchers::MatchFinder::MatchResult &Result) {
  if (Result.Context->getDiagnostics().hasErrorOccurred())
    return;

  onMatchImpl(Result);
}

llvm::Expected<llvm::SmallVector<AtomicChange, 1>>
TransformerImpl::convertToAtomicChanges(
    const llvm::SmallVectorImpl<transformer::Edit> &Edits,
    const MatchFinder::MatchResult &Result) {
  // Group the transformations, by file, into AtomicChanges, each anchored by
  // the location of the first change in that file.
  std::map<FileID, AtomicChange> ChangesByFileID;
  for (const auto &T : Edits) {
    auto ID = Result.SourceManager->getFileID(T.Range.getBegin());
    auto Iter = ChangesByFileID
                    .emplace(ID, AtomicChange(*Result.SourceManager,
                                              T.Range.getBegin(), T.Metadata))
                    .first;
    auto &AC = Iter->second;
    switch (T.Kind) {
    case transformer::EditKind::Range:
      if (auto Err =
              AC.replace(*Result.SourceManager, T.Range, T.Replacement)) {
        return std::move(Err);
      }
      break;
    case transformer::EditKind::AddInclude:
      AC.addHeader(T.Replacement);
      break;
    }
  }

  llvm::SmallVector<AtomicChange, 1> Changes;
  Changes.reserve(ChangesByFileID.size());
  for (auto &IDChangePair : ChangesByFileID)
    Changes.push_back(std::move(IDChangePair.second));

  return Changes;
}

} // namespace detail

void Transformer::registerMatchers(MatchFinder *MatchFinder) {
  for (auto &Matcher : Impl->buildMatchers())
    MatchFinder->addDynamicMatcher(Matcher, this);
}

void Transformer::run(const MatchFinder::MatchResult &Result) {
  if (Result.Context->getDiagnostics().hasErrorOccurred())
    return;

  Impl->onMatch(Result);
}

} // namespace tooling
} // namespace clang