;
25using namespacetransformer;
28usingast_matchers::internal::DynTypedMatcher;
37 for(
const auto&
E: ASTEdits) {
40 return Range.takeError();
41std::optional<CharSourceRange> EditRange =
54 T.Range = *EditRange;
55 if(
E.Replacement) {
56 autoReplacement =
E.Replacement->eval(Result);
58 returnReplacement.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 autoMetadata =
E.Metadata(Result);
70 returnMetadata.takeError();
71 T.Metadata = std::move(*Metadata);
73Edits.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 returnstd::move(Generators[0]);
113[Gs = std::move(Generators)](
116 for(
const auto&G : Gs) {
119 returnEdits.takeError();
120AllEdits.append(Edits->begin(), Edits->end());
128 E.TargetRange = std::move(
Target);
129 E.Replacement = std::move(Replacement);
136 E.Note = std::move(
Note);
146SimpleTextGenerator(std::string S) : S(
std::move(S)) {}
148std::string *Result)
const override{
150 returnllvm::Error::success();
152std::string
toString()
const override{
153 return(llvm::Twine(
"text(\"") + S +
"\")").str();
159 returnstd::make_shared<SimpleTextGenerator>(std::move(S));
168 casetransformer::IncludeFormat::Quoted:
170 casetransformer::IncludeFormat::Angled:
171 return(
"<"+ Header +
">").str();
173llvm_unreachable(
"Unknown transformer::IncludeFormat enum");
179 E.Kind = EditKind::AddInclude;
197R.
Cases= {{std::move(M), std::move(Edits)}};
202std::initializer_list<ASTEdit> Edits) {
211template<
typenameT>
212classBindingsMatcher :
publicast_matchers::internal::MatcherInterface<T> {
214 constast_matchers::internal::Matcher<T> InnerMatcher;
218ast_matchers::internal::Matcher<T> InnerMatcher)
222 const T&
Node, ast_matchers::internal::ASTMatchFinder *Finder,
223ast_matchers::internal::BoundNodesTreeBuilder *Builder)
const override{
224ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
225 for(
const auto&N :
Nodes.getMap())
226Result.setBinding(N.first, N.second);
227 if(InnerMatcher.matches(
Node, Finder, &Result)) {
228*Builder = std::move(Result);
240template<
typenameT>
241classDynamicForEachDescendantMatcher
242:
publicast_matchers::internal::MatcherInterface<T> {
243 constDynTypedMatcher DescendantMatcher;
246 explicitDynamicForEachDescendantMatcher(DynTypedMatcher DescendantMatcher)
247: DescendantMatcher(
std::move(DescendantMatcher)) {}
250 const T&
Node, ast_matchers::internal::ASTMatchFinder *Finder,
251ast_matchers::internal::BoundNodesTreeBuilder *Builder)
const override{
252 returnFinder->matchesDescendantOf(
253 Node, this->DescendantMatcher, Builder,
254ast_matchers::internal::ASTMatchFinder::BK_All);
258template<
typenameT>
259ast_matchers::internal::Matcher<T>
262 returnast_matchers::internal::makeMatcher(
newBindingsMatcher<T>(
264ast_matchers::internal::makeMatcher(
265 newDynamicForEachDescendantMatcher<T>(std::move(M)))));
272 template<
typenameT>
276MF->
addMatcher(forEachDescendantDynamically<T>(
Nodes, Matcher),
this);
283 autoTransformations =
Rule.Cases[I].Edits(Result);
284 if(!Transformations) {
285Edits = Transformations.takeError();
288Edits->append(Transformations->begin(), Transformations->end());
298template<
typenameT>
302ApplyRuleCallback Callback(std::move(Rule));
304Callback.registerMatchers<
T>(Result.Nodes, &Finder);
305Finder.match(
Node, *Result.Context);
306 returnstd::move(Callback.Edits);
338 returnllvm::make_error<llvm::StringError>(
339llvm::errc::invalid_argument,
340 "type unsupported for recursive rewriting, Kind="+
346 return[NodeId = std::move(NodeId),
347Rule = std::move(Rule)](
const MatchResult&Result)
350Result.Nodes.getMap();
351 autoIt = NodesMap.find(NodeId);
352 if(It == NodesMap.end())
353 returnllvm::make_error<llvm::StringError>(llvm::errc::invalid_argument,
354 "ID not bound: "+ NodeId);
361 for(
auto&Case : Rule.Cases)
370 return!M.canConvertTo<
QualType>();
382std::vector<DynTypedMatcher> Matchers;
383Matchers.reserve(Cases.size());
384 for(
const auto&Case : Cases) {
385std::string Tag = (TagBase + Twine(Case.first)).str();
387DynTypedMatcher BoundMatcher(Case.second.Matcher);
388BoundMatcher.setAllowBind(
true);
389 autoM = *BoundMatcher.tryBind(Tag);
390Matchers.push_back(!M.getTraversalKind()
391? M.withTraversalKind(DefaultTraversalKind)
404 for(
auto&Rule : Rules)
405R.
Cases.append(Rule.Cases.begin(), Rule.Cases.end());
409std::vector<DynTypedMatcher>
419 for(
intI = 0, N = Cases.size(); I < N; ++I) {
421 "Matcher must be non-(Qual)Type node matcher");
422Buckets[Cases[I].Matcher.getSupportedKind()].emplace_back(I, Cases[I]);
430std::vector<DynTypedMatcher> Matchers;
431 for(
const auto&Bucket : Buckets) {
432DynTypedMatcher M = DynTypedMatcher::constructVariadic(
433DynTypedMatcher::VO_AnyOf, Bucket.first,
435M.setAllowBind(
true);
437Matchers.push_back(M.tryBind(
RootID)->withTraversalKind(
TK_AsIs));
444assert(Ms.size() == 1 &&
"Cases must have compatible matchers.");
449 auto&NodesMap = Result.Nodes.getMap();
450 autoRoot = NodesMap.find(
RootID);
451assert(Root != NodesMap.end() &&
"Transformation failed: missing root node.");
456 returnRootRange->getBegin();
459 returnResult.SourceManager->getExpansionLoc(
460Root->second.getSourceRange().getBegin());
467 if(Rule.Cases.size() == 1)
470 auto&NodesMap = Result.Nodes.getMap();
471 for(
size_ti = 0, N = Rule.Cases.size(); i < N; ++i) {
472std::string Tag = (
"Tag"+ Twine(i)).str();
473 if(NodesMap.find(Tag) != NodesMap.end())
476llvm_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.
A failable computation over nodes bound by AST matchers, with (limited) reflection via the toString m...
virtual std::string toString() const =0
Constructs a string representation of the computation, for informational purposes.
virtual llvm::Error eval(const ast_matchers::MatchFinder::MatchResult &Match, T *Result) const =0
Evaluates the computation and (potentially) updates the accumulator Result.
const Regex Rule("(.+)/(.+)\\.framework/")
bool matches(const til::SExpr *E1, const til::SExpr *E2)
llvm::Expected< SmallVector< Edit, 1 > > rewriteDescendants(const Decl &Node, RewriteRule Rule, const ast_matchers::MatchFinder::MatchResult &Result)
The following overload set is a version of rewriteDescendants that operates directly on the AST,...
RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M, EditGenerator Edits)
EditGenerator makeEditGenerator(EditGenerator Edits)
SourceLocation getRuleMatchLoc(const ast_matchers::MatchFinder::MatchResult &Result)
Gets the beginning location of the source matched by a rewrite rule.
ast_matchers::internal::DynTypedMatcher buildMatcher(const RewriteRuleBase &Rule)
Builds a single matcher for the rule, covering all of the rule's cases.
size_t findSelectedCase(const ast_matchers::MatchFinder::MatchResult &Result, const RewriteRuleBase &Rule)
Returns the index of the Case of Rule that was selected in the match result.
std::vector< ast_matchers::internal::DynTypedMatcher > buildMatchers(const RewriteRuleBase &Rule)
Builds a set of matchers that cover the rule.
EditGenerator flattenVector(SmallVector< EditGenerator, 2 > Generators)
Flattens a list of generators into a single generator whose elements are the concatenation of the res...
EditGenerator flatten(Ts &&...Edits)
ASTEdit note(RangeSelector Anchor, TextGenerator Note)
Generates a single, no-op edit with the associated note anchored at the start location of the specifi...
Generator< std::string > TextGenerator
RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M, EditsT &&Edits)
Constructs a simple RewriteRule.
EditGenerator editList(llvm::SmallVector< ASTEdit, 1 > Edits)
Lifts a list of ASTEdits into an EditGenerator.
MatchConsumer< CharSourceRange > RangeSelector
ASTEdit changeTo(RangeSelector Target, TextGenerator Replacement)
Replaces a portion of the source text with Replacement.
RewriteRuleWith< MetadataT > applyFirst(ArrayRef< RewriteRuleWith< MetadataT > > Rules)
Applies the first rule whose pattern matches; other rules are ignored.
EditGenerator noopEdit(RangeSelector Anchor)
Generates a single, no-op edit anchored at the start location of the specified range.
RangeSelector before(RangeSelector Selector)
Selects the (empty) range [B,B) when Selector selects the range [B,E).
IncludeFormat
Format of the path in an include directive â angle brackets or quotes.
ASTEdit remove(RangeSelector S)
Removes the source selected by S.
MatchConsumer< llvm::SmallVector< Edit, 1 > > EditGenerator
Maps a match result to a list of concrete edits (with possible failure).
ASTEdit addInclude(RangeSelector Target, StringRef Header, IncludeFormat Format=IncludeFormat::Quoted)
Adds an include directive for the given header to the file of Target.
EditGenerator rewriteDescendants(std::string NodeId, RewriteRule Rule)
Applies Rule to all descendants of the node bound to NodeId.
EditGenerator edit(ASTEdit E)
Generates a single (specified) edit.
ASTEdit change(RangeSelector Target, TextGenerator Replacement)
DEPRECATED: use changeTo.
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.
A concrete description of a source edit, represented by a character range in the source to be replace...
Description of a source-code transformation.
SmallVector< Case, 1 > Cases
A source-code transformation with accompanying metadata.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4