clang 20.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
19namespace clang {
20namespace tooling {
21
22using ::clang::ast_matchers::MatchFinder;
23
24namespace detail {
25
28 if (Result.Context->getDiagnostics().hasErrorOccurred())
29 return;
30
31 onMatchImpl(Result);
32}
33
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
static char ID
Definition: Arena.cpp:183
unsigned Iter
Definition: HTMLLogger.cpp:154
Defines the clang::SourceLocation class and associated facilities.
A class to allow finding matches over the Clang AST.
bool addDynamicMatcher(const internal::DynTypedMatcher &NodeMatch, MatchCallback *Action)
Adds a matcher to execute when running over the AST.
An atomic change is used to create and group a set of source edits, e.g.
Definition: AtomicChange.h:37
void registerMatchers(ast_matchers::MatchFinder *MatchFinder)
N.B.
Definition: Transformer.cpp:71
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
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
void onMatch(const ast_matchers::MatchFinder::MatchResult &Result)
Definition: Transformer.cpp:26
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
const FunctionProtoType * T
Contains all information for a given match.