[cfe-dev] clang 3.8, libtooling cannot visit some BinaryOperator

mats petersson via cfe-dev cfe-dev at lists.llvm.org
Tue Jan 3 04:20:55 PST 2017


Are you SURE that the VisitBinaryOperator function is not being called? I
find that highly unlikely (considering that these visitors are quite
commonly used for all sorts of different purposes, and checking for
assignments isn't unusual by any means).

I would expect that the conditions INSIDE your function is what is causing
problems. Try adding some printout (or set a breakpoint in a debugger) at
the beginning of the function. I suspect the function is being called, but
the operands are not the kind you think they are in this case, so your
printout or "do stuff" isn't happening.

--
Mats

On 3 January 2017 at 10:23, 詹石岩 via cfe-dev <cfe-dev at lists.llvm.org> wrote:

> Hi guys,
> I'm using libtooling to develop a tool to instrument source code. I need
> to record the left values of assignment statements.
> I tried the `VisitBinaryOperator` function in `RecursiveASTVisitor`. The
> code looked like this:
>
> ```cpp
>
> bool VisitBinaryOperator(BinaryOperator *b){
>     if(b->isAssignmentOp()){
>         Stmt *lhs = binaryOperator->getLHS();
>
>         if (const DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr>(lhs))
>
>         {
>             // It's a reference to a declaration...
>             if (const VarDecl *VD = dyn_cast<VarDecl>(declRefExpr->getDecl()))
>             {
>                 // It's a reference to a variable (a local, function parameter, global, or static data member).
>                 string s = VD->getType().getAsString();
>
>                 if (s == "int" || s == "float" || s == "double")
>
>                     //do something
>
>             }
>
>         }
>
>         if (const MemberExpr *memberRefExpr = dyn_cast<MemberExpr>(lhs))
>         {
>             // It's a reference to a declaration...
>             if (ValueDecl *VD = memberRefExpr->getMemberDecl())
>             {
>                 // It's a reference to a variable (a local, function parameter, global, or static data member).
>                 string s = VD->getType().getAsString();
>                 llvm::outs()<<"type"<<s<<'\n';
>                 if (s == "int" || s == "float" || s == "double")
>                     //do something
>
>             }
>
>         }
>
> }
> return true;
> }
>
> ```
>
> Then I tested this code on a .c file, the content includes this:
> ```c
> void setForceMethod(NbodyModel *theModel,int force_method) {
>     theModel->force_method=force_method;
> }
> void setTreeRangeCoefficient(NbodyModel *theModel,double coefficient) {
>     theModel->treeRangeCoefficient=coefficient;
> }
> void setIntMethod(NbodyModel *theModel,int int_method) {
>     theModel->int_method=int_method;
> }
> ```
>
> The result is the code didn't catch the assignment inside these functions
> meanwhile other
>
> The run command is `test nbody.c test.h null -DHAS_X11 -D_REENTRANT
> -D_USE_PTHREADS -DHAS_MPI -DHAS_FFTW3 -DUSE_PPPM` and
> the `-DHAS_X11 -D_REENTRANT -D_USE_PTHREADS -DHAS_MPI -DHAS_FFTW3
> -DUSE_PPPM` was passed to CompilerInvocation::CreateFromArgs
>
> Any ideas why I can't visit these things?
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170103/ec66a0d7/attachment.html>


More information about the cfe-dev mailing list