16#include "llvm/Support/Errc.h"
17#include "llvm/Support/Error.h"
24using namespace transformer;
27using ast_matchers::internal::DynTypedMatcher;
36 for (
const auto &
E : ASTEdits) {
39 return Range.takeError();
40 std::optional<CharSourceRange> EditRange =
55 auto Replacement =
E.Replacement->eval(Result);
57 return Replacement.takeError();
58 T.Replacement = std::move(*Replacement);
61 auto Note =
E.Note->eval(Result);
63 return Note.takeError();
64 T.Note = std::move(*
Note);
67 auto Metadata =
E.Metadata(Result);
69 return Metadata.takeError();
70 T.Metadata = std::move(*Metadata);
72 Edits.push_back(std::move(
T));
78 return [Edits = std::move(Edits)](
const MatchResult &Result) {
90 return [Anchor = std::move(Anchor)](
const MatchResult &Result)
94 return Range.takeError();
101 E.Kind = EditKind::Range;
109 if (Generators.size() == 1)
110 return std::move(Generators[0]);
112 [Gs = std::move(Generators)](
115 for (
const auto &G : Gs) {
118 return Edits.takeError();
119 AllEdits.append(Edits->begin(), Edits->end());
127 E.TargetRange = std::move(
Target);
128 E.Replacement = std::move(Replacement);
135 E.Note = std::move(
Note);
145 SimpleTextGenerator(std::string S) : S(
std::move(S)) {}
147 std::string *Result)
const override {
149 return llvm::Error::success();
151 std::string
toString()
const override {
152 return (llvm::Twine(
"text(\"") + S +
"\")").str();
158 return std::make_shared<SimpleTextGenerator>(std::move(S));
167 case transformer::IncludeFormat::Quoted:
169 case transformer::IncludeFormat::Angled:
170 return (
"<" + Header +
">").str();
172 llvm_unreachable(
"Unknown transformer::IncludeFormat enum");
178 E.Kind = EditKind::AddInclude;
196 R.
Cases = {{std::move(M), std::move(Edits)}};
201 std::initializer_list<ASTEdit> Edits) {
211class BindingsMatcher :
public ast_matchers::internal::MatcherInterface<T> {
213 const ast_matchers::internal::Matcher<T> InnerMatcher;
217 ast_matchers::internal::Matcher<T> InnerMatcher)
221 const T &
Node, ast_matchers::internal::ASTMatchFinder *Finder,
222 ast_matchers::internal::BoundNodesTreeBuilder *Builder)
const override {
223 ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
224 for (
const auto &N :
Nodes.getMap())
225 Result.setBinding(N.first, N.second);
226 if (InnerMatcher.matches(
Node, Finder, &Result)) {
227 *Builder = std::move(Result);
240class DynamicForEachDescendantMatcher
241 :
public ast_matchers::internal::MatcherInterface<T> {
242 const DynTypedMatcher DescendantMatcher;
245 explicit DynamicForEachDescendantMatcher(DynTypedMatcher DescendantMatcher)
246 : DescendantMatcher(
std::move(DescendantMatcher)) {}
249 const T &
Node, ast_matchers::internal::ASTMatchFinder *Finder,
250 ast_matchers::internal::BoundNodesTreeBuilder *Builder)
const override {
251 return Finder->matchesDescendantOf(
252 Node, this->DescendantMatcher, Builder,
253 ast_matchers::internal::ASTMatchFinder::BK_All);
258ast_matchers::internal::Matcher<T>
261 return ast_matchers::internal::Matcher(
new BindingsMatcher<T>(
263 ast_matchers::internal::Matcher(
264 new DynamicForEachDescendantMatcher<T>(std::move(M)))));
271 template <
typename T>
282 auto Transformations =
Rule.Cases[I].Edits(Result);
283 if (!Transformations) {
284 Edits = Transformations.takeError();
287 Edits->append(Transformations->begin(), Transformations->end());
301 ApplyRuleCallback Callback(std::move(Rule));
303 Callback.registerMatchers<
T>(Result.Nodes, &Finder);
304 Finder.match(
Node, *Result.Context);
305 return std::move(Callback.Edits);
337 return llvm::make_error<llvm::StringError>(
338 llvm::errc::invalid_argument,
339 "type unsupported for recursive rewriting, Kind=" +
345 return [NodeId = std::move(NodeId),
349 Result.Nodes.getMap();
350 auto It = NodesMap.find(NodeId);
351 if (It == NodesMap.end())
352 return llvm::make_error<llvm::StringError>(llvm::errc::invalid_argument,
353 "ID not bound: " + NodeId);
360 for (
auto &Case : Rule.Cases)
381 std::vector<DynTypedMatcher> Matchers;
382 Matchers.reserve(Cases.size());
383 for (
const auto &Case : Cases) {
384 std::string Tag = (TagBase + Twine(Case.first)).str();
386 DynTypedMatcher BoundMatcher(Case.second.Matcher);
387 BoundMatcher.setAllowBind(
true);
388 auto M = *BoundMatcher.tryBind(Tag);
389 Matchers.push_back(!M.getTraversalKind()
390 ? M.withTraversalKind(DefaultTraversalKind)
403 for (
auto &Rule : Rules)
404 R.
Cases.append(Rule.Cases.begin(), Rule.Cases.end());
408std::vector<DynTypedMatcher>
418 for (
int I = 0, N = Cases.size(); I < N; ++I) {
420 "Matcher must be non-(Qual)Type node matcher");
421 Buckets[Cases[I].Matcher.getSupportedKind()].emplace_back(I, Cases[I]);
429 std::vector<DynTypedMatcher> Matchers;
430 for (
const auto &Bucket : Buckets) {
431 DynTypedMatcher M = DynTypedMatcher::constructVariadic(
432 DynTypedMatcher::VO_AnyOf, Bucket.first,
434 M.setAllowBind(
true);
436 Matchers.push_back(M.tryBind(
RootID)->withTraversalKind(
TK_AsIs));
443 assert(Ms.size() == 1 &&
"Cases must have compatible matchers.");
448 auto &NodesMap = Result.Nodes.getMap();
449 auto Root = NodesMap.find(
RootID);
450 assert(Root != NodesMap.end() &&
"Transformation failed: missing root node.");
455 return RootRange->getBegin();
458 return Result.SourceManager->getExpansionLoc(
459 Root->second.getSourceRange().getBegin());
466 if (Rule.Cases.size() == 1)
469 auto &NodesMap = Result.Nodes.getMap();
470 for (
size_t i = 0, N = Rule.Cases.size(); i < N; ++i) {
471 std::string Tag = (
"Tag" + Twine(i)).str();
472 if (NodesMap.find(Tag) != NodesMap.end())
475 llvm_unreachable(
"No tag found for this rule.");
BoundNodesTreeBuilder Nodes
llvm::MachO::Target Target
static TextGenerator makeText(std::string S)
MatchFinder::MatchResult MatchResult
llvm::Expected< SmallVector< clang::transformer::Edit, 1 > > rewriteDescendantsImpl(const T &Node, RewriteRule Rule, const MatchResult &Result)
static std::string formatHeaderPath(StringRef Header, IncludeFormat Format)
static Expected< SmallVector< transformer::Edit, 1 > > translateEdits(const MatchResult &Result, ArrayRef< ASTEdit > ASTEdits)
static std::vector< DynTypedMatcher > taggedMatchers(StringRef TagBase, const SmallVectorImpl< std::pair< size_t, RewriteRule::Case > > &Cases, TraversalKind DefaultTraversalKind)
static bool hasValidKind(const DynTypedMatcher &M)
Defines the RewriteRule class and related functions for creating, modifying and interpreting RewriteR...
Defines the clang::SourceLocation class and associated facilities.
StringRef asStringRef() const
String representation of the kind.
static CharSourceRange getCharRange(SourceRange R)
static CharSourceRange getTokenRange(SourceRange R)
Decl - This represents one declaration (or definition), e.g.
A dynamically typed AST node container.
ASTNodeKind getNodeKind() const
const T * get() const
Retrieve the stored node as type T.
A (possibly-)qualified type.
Encodes a location in the source.
SourceLocation getBegin() const
Stmt - This represents one statement.
Base wrapper for a particular "section" of type source info.
Maps string IDs to AST nodes matched by parts of a matcher.
internal::BoundNodesMap::IDToNodeMap IDToNodeMap
Type of mapping from binding identifiers to bound nodes.
Called when the Match registered for it was successfully found in the AST.
virtual void run(const MatchResult &Result)=0
Called on every match by the MatchFinder.
A class to allow finding matches over the Clang AST.
void addMatcher(const DeclarationMatcher &NodeMatch, MatchCallback *Action)
Adds a matcher to execute when running over the AST.
const Regex Rule("(.+)/(.+)\\.framework/")
bool matches(const til::SExpr *E1, const til::SExpr *E2)
The JSON file list parser is used to communicate input to InstallAPI.
TraversalKind
Defines how we descend a level in the AST when we pass through expressions.
@ TK_AsIs
Will traverse all child nodes.
const FunctionProtoType * T
Contains all information for a given match.