[cfe-dev] acquiring all the ancestors or descendants of a matched node
Himanshu via cfe-dev
cfe-dev at lists.llvm.org
Thu Jul 14 09:48:56 PDT 2016
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.
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?
--
Himanshu
On Wed, Jul 13, 2016 at 10:16 PM, Farzad Sadeghi <thabogre at gmail.com> wrote:
> ok the fault is probably mine for not explaining more. my situation is
> I'm looking for callEXprs that are inside functionCall. that part is
> simple enough. but I need to know whether the callExpr is inside an
> ifStmt, whileStmt, forStmt or SwitchStmt or inside a combination of
> those fourand then i have to decide and deal differently for each
> case. that's why I'm looking for the ancestors or descendants of a
> matched node.
>
> On 7/14/16, Himanshu <himanshu at utexas.edu> wrote:
> > 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?
> >
> > In that case, you may need to do multiple passes using a loop such as:
> > while new calling functions were found, explore those functions).
> >
> > --
> > Himanshu
> >
> > On Wed, Jul 13, 2016 at 3:45 PM, Farzad Sadeghi <thabogre at gmail.com>
> wrote:
> >
> >> Hi Himanshu,
> >> thank you for the answer but what i need is exactly what i asked for.
> >> i need to exactly know all the ancestors or descendants of a matched
> >> node.
> >>
> >> On 7/13/16, Himanshu <himanshu at utexas.edu> wrote:
> >> > Hi Farzad,
> >> >
> >> > If all you are looking for is finding a functionDecl wrapping a
> >> > callExpr,
> >> > then following is what you want.
> >> >
> >> > const clang::FunctionDecl* getParentFunctionDecl(ASTContext
> >> *context,const
> >> > clang::Stmt& stmt){
> >> > auto it = context->getParents(stmt).begin();
> >> > if(it == context->getParents(stmt).end())
> >> > return nullptr;
> >> >
> >> > const clang::FunctionDecl *fDecl =
> >> it->get<clang::FunctionDecl>();
> >> >
> >> > if(fDecl)
> >> > return fDecl;
> >> >
> >> > const clang::Stmt *aStmt = it->get<clang::Stmt>();
> >> >
> >> > if(aStmt)
> >> > return getParentFunctionDecl(context,*aStmt);
> >> >
> >> > //nothing worked
> >> > return nullptr;
> >> > }
> >> >
> >> > --
> >> > Himanshu
> >> >
> >> > On Wed, Jul 13, 2016 at 6:34 AM, 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. 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
> >>
> >
>
>
> --
> Farzad Sadeghi
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160714/a7e0013f/attachment.html>
More information about the cfe-dev
mailing list