16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Errc.h"
18#include "llvm/Support/Error.h"
25using namespace transformer;
28using ast_matchers::internal::DynTypedMatcher;
37 for (
const auto &
E : ASTEdits) {
40 return Range.takeError();
41 std::optional<CharSourceRange> EditRange =
56 auto Replacement =
E.Replacement->eval(Result);
58 return Replacement.takeError();
59 T.Replacement = std::move(*Replacement);
62 auto Note =
E.Note->eval(Result);
64 return Note.takeError();
65 T.Note = std::move(*
Note);
68 auto Metadata =
E.Metadata(Result);
70 return Metadata.takeError();
71 T.Metadata = std::move(*Metadata);
73 Edits.push_back(std::move(
T));
79 return [Edits = std::move(Edits)](
const MatchResult &Result) {
91 return [Anchor = std::move(Anchor)](
const MatchResult &Result)
95 return Range.takeError();
102 E.Kind = EditKind::Range;
110 if (Generators.size() == 1)
111 return std::move(Generators[0]);
113 [Gs = std::move(Generators)](
116 for (
const auto &G : Gs) {
119 return Edits.takeError();
120 AllEdits.append(Edits->begin(), Edits->end());
128 E.TargetRange = std::move(
Target);
129 E.Replacement = std::move(Replacement);
136 E.Note = std::move(
Note);
146 SimpleTextGenerator(std::string S) : S(
std::move(S)) {}
148 std::string *Result)
const override {
150 return llvm::Error::success();
152 std::string
toString()
const override {
153 return (llvm::Twine(
"text(\"") + S +
"\")").str();
159 return std::make_shared<SimpleTextGenerator>(std::move(S));
168 case transformer::IncludeFormat::Quoted:
170 case transformer::IncludeFormat::Angled:
171 return (
"<" + Header +
">").str();
173 llvm_unreachable(
"Unknown transformer::IncludeFormat enum");
179 E.Kind = EditKind::AddInclude;
197 R.
Cases = {{std::move(M), std::move(Edits)}};
202 std::initializer_list<ASTEdit> Edits) {
212class BindingsMatcher :
public ast_matchers::internal::MatcherInterface<T> {
214 const ast_matchers::internal::Matcher<T> InnerMatcher;
218 ast_matchers::internal::Matcher<T> InnerMatcher)
222 const T &
Node, ast_matchers::internal::ASTMatchFinder *Finder,
223 ast_matchers::internal::BoundNodesTreeBuilder *Builder)
const override {
224 ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
225 for (
const auto &N :
Nodes.getMap())
226 Result.setBinding(N.first, N.second);
227 if (InnerMatcher.matches(
Node, Finder, &Result)) {
228 *Builder = std::move(Result);
241class DynamicForEachDescendantMatcher
242 :
public ast_matchers::internal::MatcherInterface<T> {
243 const DynTypedMatcher DescendantMatcher;
246 explicit DynamicForEachDescendantMatcher(DynTypedMatcher DescendantMatcher)
247 : DescendantMatcher(
std::move(DescendantMatcher)) {}
250 const T &
Node, ast_matchers::internal::ASTMatchFinder *Finder,
251 ast_matchers::internal::BoundNodesTreeBuilder *Builder)
const override {
252 return Finder->matchesDescendantOf(
253 Node, this->DescendantMatcher, Builder,
254 ast_matchers::internal::ASTMatchFinder::BK_All);
259ast_matchers::internal::Matcher<T>
262 return ast_matchers::internal::makeMatcher(
new BindingsMatcher<T>(
264 ast_matchers::internal::makeMatcher(
265 new DynamicForEachDescendantMatcher<T>(std::move(M)))));
272 template <
typename T>
283 auto Transformations =
Rule.Cases[I].Edits(Result);
284 if (!Transformations) {
285 Edits = Transformations.takeError();
288 Edits->append(Transformations->begin(), Transformations->end());
302 ApplyRuleCallback Callback(std::move(Rule));
304 Callback.registerMatchers<
T>(Result.Nodes, &Finder);
305 Finder.match(
Node, *Result.Context);
306 return std::move(Callback.Edits);
338 return llvm::make_error<llvm::StringError>(
339 llvm::errc::invalid_argument,
340 "type unsupported for recursive rewriting, Kind=" +
346 return [NodeId = std::move(NodeId),
350 Result.Nodes.getMap();
351 auto It = NodesMap.find(NodeId);
352 if (It == NodesMap.end())
353 return llvm::make_error<llvm::StringError>(llvm::errc::invalid_argument,
354 "ID not bound: " + NodeId);
361 for (
auto &Case : Rule.Cases)
382 std::vector<DynTypedMatcher> Matchers;
383 Matchers.reserve(Cases.size());
384 for (
const auto &Case : Cases) {
385 std::string Tag = (TagBase + Twine(Case.first)).str();
387 DynTypedMatcher BoundMatcher(Case.second.Matcher);
388 BoundMatcher.setAllowBind(
true);
389 auto M = *BoundMatcher.tryBind(Tag);
390 Matchers.push_back(!M.getTraversalKind()
391 ? M.withTraversalKind(DefaultTraversalKind)
404 for (
auto &Rule : Rules)
405 R.
Cases.append(Rule.Cases.begin(), Rule.Cases.end());
409std::vector<DynTypedMatcher>
419 for (
int I = 0, N = Cases.size(); I < N; ++I) {
421 "Matcher must be non-(Qual)Type node matcher");
422 Buckets[Cases[I].Matcher.getSupportedKind()].emplace_back(I, Cases[I]);
430 std::vector<DynTypedMatcher> Matchers;
431 for (
const auto &Bucket : Buckets) {
432 DynTypedMatcher M = DynTypedMatcher::constructVariadic(
433 DynTypedMatcher::VO_AnyOf, Bucket.first,
435 M.setAllowBind(
true);
437 Matchers.push_back(M.tryBind(
RootID)->withTraversalKind(
TK_AsIs));
444 assert(Ms.size() == 1 &&
"Cases must have compatible matchers.");
449 auto &NodesMap = Result.Nodes.getMap();
450 auto Root = NodesMap.find(
RootID);
451 assert(Root != NodesMap.end() &&
"Transformation failed: missing root node.");
456 return RootRange->getBegin();
459 return Result.SourceManager->getExpansionLoc(
460 Root->second.getSourceRange().getBegin());
467 if (Rule.Cases.size() == 1)
470 auto &NodesMap = Result.Nodes.getMap();
471 for (
size_t i = 0, N = Rule.Cases.size(); i < N; ++i) {
472 std::string Tag = (
"Tag" + Twine(i)).str();
473 if (NodesMap.find(Tag) != NodesMap.end())
476 llvm_unreachable(
"No tag found for this rule.");
BoundNodesTreeBuilder Nodes
llvm::MachO::Target Target
static TextGenerator makeText(std::string S)
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.