<div dir="ltr">I believe by all the ancestors you mean: if a callExpr is encountered, then you want all the functions that calls the callExpr, as well as each function that calls the wrapping function in which the call was found? <div><br></div><div>In that case, you may need to do multiple passes using a loop such as: while new calling functions were found, explore those functions). </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 3:45 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">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>
<div><div><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 callExpr,<br>
> then following is what you want.<br>
><br>
> const clang::FunctionDecl* getParentFunctionDecl(ASTContext *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 = 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>
</div></div><span><font color="#888888">--<br>
Farzad Sadeghi<br>
</font></span></blockquote></div><br></div></div>