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
19namespace clang {
20namespace tooling {
21
22using ::clang::ast_matchers::MatchFinder;
23
24namespace detail {
25
26void TransformerImpl::onMatch(
27 const ast_matchers::MatchFinder::MatchResult &Result) {
28 if (Result.Context->getDiagnostics().hasErrorOccurred())
29 return;
30
31 onMatchImpl(Result);
32}
33
34llvm::Expected<llvm::SmallVector<AtomicChange, 1>>
35TransformerImpl::convertToAtomicChanges(
36 const llvm::SmallVectorImpl<transformer::Edit> &Edits,
37 const MatchFinder::MatchResult &Result) {
38 // Group the transformations, by file, into AtomicChanges, each anchored by
39 // the location of the first change in that file.
40 std::map<FileID, AtomicChange> ChangesByFileID;
41 for (const auto &T : Edits) {
42 auto ID = Result.SourceManager->getFileID(SpellingLoc: T.Range.getBegin());
43 auto Iter = ChangesByFileID
44 .emplace(args&: ID, args: AtomicChange(*Result.SourceManager,
45 T.Range.getBegin(), T.Metadata))
46 .first;
47 auto &AC = Iter->second;
48 switch (T.Kind) {
49 case transformer::EditKind::Range:
50 if (auto Err =
51 AC.replace(SM: *Result.SourceManager, Range: T.Range, ReplacementText: T.Replacement)) {
52 return std::move(Err);
53 }
54 break;
55 case transformer::EditKind::AddInclude:
56 AC.addHeader(Header: T.Replacement);
57 break;
58 }
59 }
60
61 llvm::SmallVector<AtomicChange, 1> Changes;
62 Changes.reserve(N: ChangesByFileID.size());
63 for (auto &IDChangePair : ChangesByFileID)
64 Changes.push_back(Elt: std::move(IDChangePair.second));
65
66 return Changes;
67}
68
69} // namespace detail
70
71void Transformer::registerMatchers(MatchFinder *MatchFinder) {
72 for (auto &Matcher : Impl->buildMatchers())
73 MatchFinder->addDynamicMatcher(NodeMatch: Matcher, Action: this);
74}
75
76void Transformer::run(const MatchFinder::MatchResult &Result) {
77 if (Result.Context->getDiagnostics().hasErrorOccurred())
78 return;
79
80 Impl->onMatch(Result);
81}
82
83} // namespace tooling
84} // namespace clang
85

source code of clang/lib/Tooling/Transformer/Transformer.cpp