clang  15.0.0git
Transformer.cpp
Go to the documentation of this file.
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 
14 #include "llvm/Support/Error.h"
15 #include <map>
16 #include <utility>
17 #include <vector>
18 
19 namespace clang {
20 namespace tooling {
21 
22 using ::clang::ast_matchers::MatchFinder;
23 
24 namespace detail {
25 
28  if (Result.Context->getDiagnostics().hasErrorOccurred())
29  return;
30 
31  onMatchImpl(Result);
32 }
33 
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(T.Range.getBegin());
43  auto Iter = ChangesByFileID
44  .emplace(ID, AtomicChange(*Result.SourceManager,
45  T.Range.getBegin(), T.Metadata))
46  .first;
47  auto &AC = Iter->second;
48  switch (T.Kind) {
50  if (auto Err =
51  AC.replace(*Result.SourceManager, T.Range, T.Replacement)) {
52  return std::move(Err);
53  }
54  break;
56  AC.addHeader(T.Replacement);
57  break;
58  }
59  }
60 
62  Changes.reserve(ChangesByFileID.size());
63  for (auto &IDChangePair : ChangesByFileID)
64  Changes.push_back(std::move(IDChangePair.second));
65 
66  return Changes;
67 }
68 
69 } // namespace detail
70 
72  for (auto &Matcher : Impl->buildMatchers())
73  MatchFinder->addDynamicMatcher(Matcher, this);
74 }
75 
77  if (Result.Context->getDiagnostics().hasErrorOccurred())
78  return;
79 
80  Impl->onMatch(Result);
81 }
82 
83 } // namespace tooling
84 } // namespace clang
clang::tooling::detail::TransformerImpl::onMatch
void onMatch(const ast_matchers::MatchFinder::MatchResult &Result)
Definition: Transformer.cpp:26
Transformer.h
llvm::SmallVector
Definition: LLVM.h:38
clang::ast_matchers::MatchFinder
A class to allow finding matches over the Clang AST.
Definition: ASTMatchFinder.h:68
clang::tooling::AtomicChange
An atomic change is used to create and group a set of source edits, e.g.
Definition: AtomicChange.h:37
clang::transformer::EditKind::AddInclude
@ AddInclude
llvm::Expected
Definition: LLVM.h:41
ASTMatchFinder.h
clang::tooling::detail::TransformerImpl::convertToAtomicChanges
static llvm::Expected< llvm::SmallVector< AtomicChange, 1 > > convertToAtomicChanges(const llvm::SmallVectorImpl< transformer::Edit > &Edits, const ast_matchers::MatchFinder::MatchResult &Result)
Converts a set of Edit into a AtomicChange per file modified.
Definition: Transformer.cpp:35
AtomicChange.h
clang::tooling::Transformer::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *MatchFinder)
N.B.
Definition: Transformer.cpp:71
ASTMatchersInternal.h
clang::tooling::Transformer::run
void run(const ast_matchers::MatchFinder::MatchResult &Result) override
Not called directly by users – called by the framework, via base class pointer.
Definition: Transformer.cpp:76
clang::transformer::EditKind::Range
@ Range
SourceLocation.h
clang::ast_matchers::MatchFinder::MatchResult
Contains all information for a given match.
Definition: ASTMatchFinder.h:74
clang::Builtin::ID
ID
Definition: Builtins.h:52
clang
Definition: CalledOnceCheck.h:17
clang::ast_matchers::MatchFinder::addDynamicMatcher
bool addDynamicMatcher(const internal::DynTypedMatcher &NodeMatch, MatchCallback *Action)
Adds a matcher to execute when running over the AST.
Definition: ASTMatchFinder.cpp:1629
llvm::SmallVectorImpl
Definition: Randstruct.h:18