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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//===--- Transformer.h - Transformer class ----------------------*- 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
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLING_TRANSFORMER_TRANSFORMER_H_
#define LLVM_CLANG_TOOLING_TRANSFORMER_TRANSFORMER_H_
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/Refactoring/AtomicChange.h"
#include "clang/Tooling/Transformer/RewriteRule.h"
#include "llvm/Support/Error.h"
#include <functional>
#include <utility>
namespace clang {
namespace tooling {
namespace detail {
/// Implementation details of \c Transformer with type erasure around
/// \c RewriteRule<T> as well as the corresponding consumers.
class TransformerImpl {
public:
virtual ~TransformerImpl() = default;
void onMatch(const ast_matchers::MatchFinder::MatchResult &Result);
virtual std::vector<ast_matchers::internal::DynTypedMatcher>
buildMatchers() const = 0;
protected:
/// Converts a set of \c Edit into a \c AtomicChange per file modified.
/// Returns an error if the edits fail to compose, e.g. overlapping edits.
static llvm::Expected<llvm::SmallVector<AtomicChange, 1>>
convertToAtomicChanges(const llvm::SmallVectorImpl<transformer::Edit> &Edits,
const ast_matchers::MatchFinder::MatchResult &Result);
private:
virtual void
onMatchImpl(const ast_matchers::MatchFinder::MatchResult &Result) = 0;
};
// FIXME: Use std::type_identity or backport when available.
template <class T> struct type_identity {
using type = T;
};
} // namespace detail
template <typename T> struct TransformerResult {
llvm::MutableArrayRef<AtomicChange> Changes;
T Metadata;
};
template <> struct TransformerResult<void> {
llvm::MutableArrayRef<AtomicChange> Changes;
};
/// Handles the matcher and callback registration for a single `RewriteRule`, as
/// defined by the arguments of the constructor.
class Transformer : public ast_matchers::MatchFinder::MatchCallback {
public:
/// Provides the set of changes to the consumer. The callback is free to move
/// or destructively consume the changes as needed.
///
/// We use \c MutableArrayRef as an abstraction to provide decoupling, and we
/// expect the majority of consumers to copy or move the individual values
/// into a separate data structure.
using ChangeSetConsumer = std::function<void(
Expected<llvm::MutableArrayRef<AtomicChange>> Changes)>;
/// \param Consumer receives all rewrites for a single match, or an error.
/// Will not necessarily be called for each match; for example, if the rule
/// generates no edits but does not fail. Note that clients are responsible
/// for handling the case that independent \c AtomicChanges conflict with each
/// other.
explicit Transformer(transformer::RewriteRuleWith<void> Rule,
ChangeSetConsumer Consumer)
: Transformer(std::move(Rule),
[Consumer = std::move(Consumer)](
llvm::Expected<TransformerResult<void>> Result) {
if (Result)
Consumer(Result->Changes);
else
Consumer(Result.takeError());
}) {}
/// \param Consumer receives all rewrites and the associated metadata for a
/// single match, or an error. Will always be called for each match, even if
/// the rule generates no edits. Note that clients are responsible for
/// handling the case that independent \c AtomicChanges conflict with each
/// other.
template <typename MetadataT>
explicit Transformer(
transformer::RewriteRuleWith<MetadataT> Rule,
std::function<void(llvm::Expected<TransformerResult<
typename detail::type_identity<MetadataT>::type>>)>
Consumer);
/// N.B. Passes `this` pointer to `MatchFinder`. So, this object should not
/// be moved after this call.
void registerMatchers(ast_matchers::MatchFinder *MatchFinder);
/// Not called directly by users -- called by the framework, via base class
/// pointer.
void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
std::unique_ptr<detail::TransformerImpl> Impl;
};
namespace detail {
/// Runs the metadata generator on \c Rule and stuffs it into \c Result.
/// @{
template <typename T>
llvm::Error
populateMetadata(const transformer::RewriteRuleWith<T> &Rule,
size_t SelectedCase,
const ast_matchers::MatchFinder::MatchResult &Match,
TransformerResult<T> &Result) {
// Silence a false positive GCC -Wunused-but-set-parameter warning in constexpr
// cases, by marking SelectedCase as used. See
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85827 for details. The issue is
// fixed in GCC 10.
(void)SelectedCase;
if constexpr (!std::is_void_v<T>) {
auto Metadata = Rule.Metadata[SelectedCase]->eval(Match);
if (!Metadata)
return Metadata.takeError();
Result.Metadata = std::move(*Metadata);
}
return llvm::Error::success();
}
/// @}
/// Implementation when metadata is generated as a part of the rewrite. This
/// happens when we have a \c RewriteRuleWith<T>.
template <typename T> class WithMetadataImpl final : public TransformerImpl {
transformer::RewriteRuleWith<T> Rule;
std::function<void(llvm::Expected<TransformerResult<T>>)> Consumer;
public:
explicit WithMetadataImpl(
transformer::RewriteRuleWith<T> R,
std::function<void(llvm::Expected<TransformerResult<T>>)> Consumer)
: Rule(std::move(R)), Consumer(std::move(Consumer)) {
assert(llvm::all_of(Rule.Cases,
[](const transformer::RewriteRuleBase::Case &Case)
-> bool { return !!Case.Edits; }) &&
"edit generator must be provided for each rule");
if constexpr (!std::is_void_v<T>)
assert(llvm::all_of(Rule.Metadata,
[](const typename transformer::Generator<T> &Metadata)
-> bool { return !!Metadata; }) &&
"metadata generator must be provided for each rule");
}
private:
void onMatchImpl(const ast_matchers::MatchFinder::MatchResult &Result) final {
size_t I = transformer::detail::findSelectedCase(Result, Rule);
auto Transformations = Rule.Cases[I].Edits(Result);
if (!Transformations) {
Consumer(Transformations.takeError());
return;
}
llvm::SmallVector<AtomicChange, 1> Changes;
if (!Transformations->empty()) {
auto C = convertToAtomicChanges(*Transformations, Result);
if (C) {
Changes = std::move(*C);
} else {
Consumer(C.takeError());
return;
}
} else if (std::is_void<T>::value) {
// If we don't have metadata and we don't have any edits, skip.
return;
}
TransformerResult<T> RewriteResult;
if (auto E = populateMetadata(Rule, I, Result, RewriteResult)) {
Consumer(std::move(E));
return;
}
RewriteResult.Changes = llvm::MutableArrayRef<AtomicChange>(Changes);
Consumer(std::move(RewriteResult));
}
std::vector<ast_matchers::internal::DynTypedMatcher>
buildMatchers() const final {
return transformer::detail::buildMatchers(Rule);
}
};
} // namespace detail
template <typename MetadataT>
Transformer::Transformer(
transformer::RewriteRuleWith<MetadataT> Rule,
std::function<void(llvm::Expected<TransformerResult<
typename detail::type_identity<MetadataT>::type>>)>
Consumer)
: Impl(std::make_unique<detail::WithMetadataImpl<MetadataT>>(
std::move(Rule), std::move(Consumer))) {}
} // namespace tooling
} // namespace clang
#endif // LLVM_CLANG_TOOLING_TRANSFORMER_TRANSFORMER_H_
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
|