<div dir="ltr">You can easily modify the code snippet I provided to keep a stack of parents --- starting from the callExpr you found. Every time you encounter a parent that you are interested in (ifStmt, whileStmt) you push it on stack. <div>Once you reach a functionDecl stmt, you are done populating the parents, and then can analyze the stack to find out the exact nesting. Am I missing something? </div><div class="gmail_extra"><br clear="all"><div><div data-smartmail="gmail_signature">--<div>Himanshu</div></div></div>
<br><div class="gmail_quote">On Wed, Jul 13, 2016 at 10:16 PM, Farzad Sadeghi <span dir="ltr"><<a href="mailto:thabogre@gmail.com" target="_blank">thabogre@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">ok the fault is probably mine for not explaining more. my situation is<br>
I'm looking for callEXprs that are inside functionCall. that part is<br>
simple enough. but I need to know whether the callExpr is inside an<br>
ifStmt, whileStmt, forStmt or SwitchStmt or inside a combination of<br>
those fourand then i have to decide and deal differently for each<br>
case. that's why I'm looking for the ancestors or descendants of a<br>
matched node.<br>
<div><div><br>
On 7/14/16, Himanshu <<a href="mailto:himanshu@utexas.edu" target="_blank">himanshu@utexas.edu</a>> wrote:<br>
> I believe by all the ancestors you mean: if a callExpr is encountered, then<br>
> you want all the functions that calls the callExpr, as well as each<br>
> function that calls the wrapping function in which the call was found?<br>
><br>
> In that case, you may need to do multiple passes using a loop such as:<br>
> while new calling functions were found, explore those functions).<br>
><br>
> --<br>
> Himanshu<br>
><br>
> On Wed, Jul 13, 2016 at 3:45 PM, Farzad Sadeghi <<a href="mailto:thabogre@gmail.com" target="_blank">thabogre@gmail.com</a>> wrote:<br>
><br>
>> Hi Himanshu,<br>
>> thank you for the answer but what i need is exactly what i asked for.<br>
>> i need to exactly know all the ancestors or descendants of a matched<br>
>> node.<br>
>><br>
>> On 7/13/16, Himanshu <<a href="mailto:himanshu@utexas.edu" target="_blank">himanshu@utexas.edu</a>> wrote:<br>
>> > Hi Farzad,<br>
>> ><br>
>> > If all you are looking for is finding a functionDecl wrapping a<br>
>> > callExpr,<br>
>> > then following is what you want.<br>
>> ><br>
>> > const clang::FunctionDecl* getParentFunctionDecl(ASTContext<br>
>> *context,const<br>
>> > clang::Stmt& stmt){<br>
>> > auto it = context->getParents(stmt).begin();<br>
>> > if(it == context->getParents(stmt).end())<br>
>> > return nullptr;<br>
>> ><br>
>> > const clang::FunctionDecl *fDecl =<br>
>> it->get<clang::FunctionDecl>();<br>
>> ><br>
>> > if(fDecl)<br>
>> > return fDecl;<br>
>> ><br>
>> > const clang::Stmt *aStmt = it->get<clang::Stmt>();<br>
>> ><br>
>> > if(aStmt)<br>
>> > return getParentFunctionDecl(context,*aStmt);<br>
>> ><br>
>> > //nothing worked<br>
>> > return nullptr;<br>
>> > }<br>
>> ><br>
>> > --<br>
>> > Himanshu<br>
>> ><br>
>> > On Wed, Jul 13, 2016 at 6:34 AM, Farzad Sadeghi via cfe-dev <<br>
>> > <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
>> ><br>
>> >> Hi,<br>
>> >> I need to travel through all the ancestors or descendants of a matched<br>
>> >> AST node to later use that info to moodify parts of the input source<br>
>> >> code. I tried to look for ways to do that. I looked at the getParents<br>
>> >> member function of the ASTContext class. I could use that to just go<br>
>> >> up the AST hierarchy to visit all the ancestor nodes of my<br>
>> >> currently-matched node. but the problem with that is, when i get the<br>
>> >> parent node, i no longer have the context for that node to try and get<br>
>> >> its parent. I could try to rebuild the ASTContext for the new node but<br>
>> >> that seems to be another big task on its own, if possible. the lowest<br>
>> >> NodeKind (lowest in the C hierarchy) I'm looking for is a callExpr and<br>
>> >> the highest I'm looking for is a functionDecl. How can I obtain all<br>
>> >> the ancestors or descendants of a matched AST node after the match<br>
>> >> returns the control to run in MatchCallback?<br>
>> >><br>
>> >><br>
>> >> --<br>
>> >> Farzad Sadeghi<br>
>> >> _______________________________________________<br>
>> >> cfe-dev mailing list<br>
>> >> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
>> >> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
>> >><br>
>> ><br>
>><br>
>><br>
>> --<br>
>> Farzad Sadeghi<br>
>><br>
><br>
<br>
<br>
</div></div><span><font color="#888888">--<br>
Farzad Sadeghi<br>
</font></span></blockquote></div><br></div></div>