[cfe-dev] acquiring all the ancestors or descendants of a matched node

Farzad Sadeghi via cfe-dev cfe-dev at lists.llvm.org
Thu Jul 14 03:31:23 PDT 2016


Dear Manuel,
thank you for the answer.
well the ASTContext being independent of the node will most probably
solve my problem regarding the second answer,  using `hasAcestor` or
`hasDescendant` will not suffice for my current application. for
example when i run into  an `ifStmt` nsted with a `forStmt`, the
matcher will let me know there is a forStmt and an ifStmt nested
together but I wont be able to know the order and the order is
signifacant for my application and basically i would have to go
through the ancestors and descendants again which is the main problem
in the first place. Also, writing a matcher that considers all
permutations possible of ifStmts, whenStms, forStmts and switchStmts
being nested together is currently not possible using ASTMatchers.



On 7/14/16, Manuel Klimek <klimek at google.com> wrote:
> On Wed, Jul 13, 2016 at 3:34 PM Farzad Sadeghi via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
>
>> Hi,
>> I need to travel through all the ancestors or descendants of a matched
>> AST node to later use that info to moodify parts of the input source
>> code. I tried to look for ways to do that. I looked at the getParents
>> member function of the ASTContext class. I could use that to just go
>> up the AST hierarchy to visit all the ancestor nodes of my
>> currently-matched node. but the problem with that is, when i get the
>> parent node, i no longer have the context for that node to try and get
>> its parent.
>
>
> The ASTContext is independent of the node - you can still use the same
> ASTContext.
>
> Note that you also have hasAncestor AST matchers, and you can run AST
> matchers directly on a node inside a MatchCallback.
>
>
>> I could try to rebuild the ASTContext for the new node but
>> that seems to be another big task on its own, if possible. the lowest
>> NodeKind (lowest in the C hierarchy) I'm looking for is a callExpr and
>> the highest I'm looking for is a functionDecl. How can I obtain all
>> the ancestors or descendants of a matched AST node after the match
>> returns the control to run in MatchCallback?
>>
>>
>> --
>> Farzad Sadeghi
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>>
>


-- 
Farzad Sadeghi



More information about the cfe-dev mailing list