[cfe-dev] Traversing a function body for call expressions. How ?

Richard Smith richard at metafoo.co.uk
Fri Jan 2 05:18:51 PST 2015


On Fri, Dec 12, 2014 at 2:39 AM, Manasij Mukherjee <manasij7479 at gmail.com>
wrote:

> Hi,
> Here is my code, sorry for the delay.
>
> class FunctionBodyVisitor : public clang::StmtVisitor<FunctionBodyVisitor>
> {
> public:
>   void VisitCallExpr(clang::CallExpr* Expr) {
>     llvm::outs() << Expr->getCalleeDecl()->getAsFunction()->getName() <<
> "\n";
>     //this is never called
>   }
> };
>
> class DummyVisitor : public clang::RecursiveASTVisitor<DummyVisitor> {
> public:
>   bool VisitFunctionDecl(clang::FunctionDecl* Decl) {
>     llvm::outs() << Decl->getName() << "\n"; // this part visibly works
>     stmtVisitor.Visit(Decl->getBody());
>     return true;
>   }
> private:
>   FunctionBodyVisitor stmtVisitor;
> };
>
> The code on which this is called is:
> void foo()
> {
>
> }
> int main()
> {
>     foo(); // I want to get to this call expression
> }


Note that StmtVisitor is not recursive; the statement you're visiting is a
CompoundStmt, not a CallExpr. A RecursiveASTVisitor that just implements
VisitCallExpr may do what you want here.


>
> Manasij
>
>
> On Sat, Dec 6, 2014 at 8:18 AM, Gabor Kozar <kozargabor at gmail.com> wrote:
> > Hi,
> >
> > It should work. Define an StmtVisitor subclass, provide a VisitCallExpr.
> > Then in your Visitor::VisitFunctionDecl, construct an StmtVisitor object,
> > then call its Visit method with the function's body.
> >
> > In the future, please also provide source code, to allow us to spot any
> bugs
> > and problems in your code (e.g. you may believe that the code does what
> you
> > say it does, but you might have a bug, etc.).
> >
> > ---
> > Best regards,
> >
> > Gábor 'ShdNx' Kozár
> > http://gaborkozar.me
> >
> >
> >
> > On Wed, Dec 3, 2014, at 07:11, Manasij Mukherjee wrote:
> >
> > Hi,
> > In a RecursiveASTVisitor implementation, I implement VisitFunctionDecl.
> > Now, from the FunctionDecl I have, I can call getBody and get a Stmt.
> >
> > How can I visit all the functions this function calls ?
> > I have tried creating a StmtVisitor and ..err..defining VisitCallExpr !
> > That didn't work.
> >
> > Can anyone point me to what I should be doing ?
> >
> > Manasij Mukherjee
> > _______________________________________________
> > cfe-dev mailing list
> > cfe-dev at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
> >
> >
> >
> > _______________________________________________
> > cfe-dev mailing list
> > cfe-dev at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
> >
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150102/310aebad/attachment.html>


More information about the cfe-dev mailing list