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

詹石岩 via cfe-dev cfe-dev at lists.llvm.org
Tue Jan 3 02:23:46 PST 2017


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170103/f94a54bd/attachment.html>


More information about the cfe-dev mailing list