<div dir="ltr"><div>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).<br><br>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.<br><br>--<br></div>Mats<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 3 January 2017 at 10:23, 詹石岩 via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi guys,<div>I'm using libtooling to develop a tool to instrument source code. I need to record the left values of assignment statements.</div><div>I tried the `VisitBinaryOperator` function in `RecursiveASTVisitor`. The code looked like this:</div><div><br></div><div>```cpp</div><div><pre style="font-size:9pt"><span style="color:#000080;font-weight:bold">bool </span>VisitBinaryOperator(<span style="color:#008080">BinaryOper<wbr>ator </span>*b){<br>    <span style="color:#000080;font-weight:bold">if</span>(b->isAssignmentOp()){<br>        Stmt <span style="font-size:9pt">*lhs = binaryOperator->getLHS();</span></pre><pre style="font-size:9pt">        if <span style="font-size:9pt">(</span><span style="font-size:9pt;color:rgb(0,0,128);font-weight:bold">const </span><span style="font-size:9pt;color:rgb(0,128,128)">DeclRefExpr </span><span style="font-size:9pt">*declRefExpr = dyn_cast<</span><span style="font-size:9pt;color:rgb(0,128,128)">DeclRefExpr</span><span style="font-size:9pt">>(lhs))</span><pre style="font-size:9pt">        {<br>            <span style="color:#808080;font-style:italic">// It's a reference to a declaration...<br></span><span style="color:#808080;font-style:italic">            </span><span style="color:#000080;font-weight:bold">if </span>(<span style="color:#000080;font-weight:bold">const </span><span style="color:#008080">VarDecl </span>*VD = dyn_cast<<span style="color:#008080">VarDecl</span>>(declRefExpr-<wbr>>getDecl()))<br>            {<br>                <span style="color:#808080;font-style:italic">// It's a reference to a variable (a local, function parameter, global, or static data member).<br></span><span style="color:#808080;font-style:italic">                </span><span style="color:#371f80">string </span>s = VD->getType().getAsString();</pre><pre style="font-size:9pt">                if <span style="font-size:9pt">(s </span><span style="font-size:9pt;color:rgb(0,128,128)">== </span><span style="font-size:9pt;color:rgb(0,128,0);font-weight:bold">"int" </span><span style="font-size:9pt">|| s </span><span style="font-size:9pt;color:rgb(0,128,128)">== </span><span style="font-size:9pt;color:rgb(0,128,0);font-weight:bold">"float" </span><span style="font-size:9pt">|| s </span><span style="font-size:9pt;color:rgb(0,128,128)">== </span><span style="font-size:9pt;color:rgb(0,128,0);font-weight:bold">"double"</span><span style="font-size:9pt">)</span></pre><pre style="font-size:9pt"><span style="font-size:9pt">                    //do something</span></pre><pre style="font-size:9pt"><span style="font-size:9pt">            }</span></pre><pre style="font-size:9pt"><span style="font-size:9pt">        }</span></pre><pre style="font-size:9pt"><span style="color:#000080;font-weight:bold">        if </span>(<span style="color:#000080;font-weight:bold">const </span><span style="color:#008080">MemberExpr </span>*memberRefExpr = dyn_cast<<span style="color:#008080">MemberExpr</span>>(lhs))<br>        {<br>            <span style="color:#808080;font-style:italic">// It's a reference to a declaration...<br></span><span style="color:#808080;font-style:italic">            </span><span style="color:#000080;font-weight:bold">if </span>(<span style="color:#008080">ValueDecl </span>*VD = memberRefExpr->getMemberDecl()<wbr>)<br>            {<br>                <span style="color:#808080;font-style:italic">// It's a reference to a variable (a local, function parameter, global, or static data member).<br></span><span style="color:#808080;font-style:italic">                </span><span style="color:#371f80">string </span>s = VD->getType().getAsString();<br>                <span style="color:#008080">llvm</span>::outs()<span style="color:#008080"><<</span><span style="color:#008000;font-weight:bold">"type"</span><span style="color:#008080"><<</span>s<span style="color:#008080"><<</span><span style="color:#008000;font-weight:bold">'</span><span style="color:#000080;font-weight:bold">\n</span><span style="color:#008000;font-weight:bold">'</span>;<br>                <span style="color:#000080;font-weight:bold">if </span>(s <span style="color:#008080">== </span><span style="color:#008000;font-weight:bold">"int" </span>|| s <span style="color:#008080">== </span><span style="color:#008000;font-weight:bold">"float" </span>|| s <span style="color:#008080">== </span><span style="color:#008000;font-weight:bold">"double"</span>)<br>                    //do something</pre><pre style="font-size:9pt">            }</pre><pre style="font-size:9pt">        }</pre>    }<br>    <span style="color:#000080;font-weight:bold">return true</span>;<br>}</pre></div><div>```</div><div><br></div><div>Then I tested this code on a .c file, the content includes this:</div><div>```c</div><div><div>void setForceMethod(NbodyModel *theModel,int force_method) {</div><div>    theModel->force_method=force_<wbr>method;</div><div>}</div><div>void setTreeRangeCoefficient(<wbr>NbodyModel *theModel,double coefficient) {</div><div>    theModel-><wbr>treeRangeCoefficient=<wbr>coefficient;</div><div>}</div><div>void setIntMethod(NbodyModel *theModel,int int_method) {</div><div>    theModel->int_method=int_<wbr>method;</div><div>}</div></div><div>```</div><div><br></div><div>The result is the code didn't catch the assignment inside these functions meanwhile other </div><div><br></div><div>The run command is `test nbody.c test.h null -DHAS_X11 -D_REENTRANT -D_USE_PTHREADS -DHAS_MPI -DHAS_FFTW3 -DUSE_PPPM` and</div><div>the `-DHAS_X11 -D_REENTRANT -D_USE_PTHREADS -DHAS_MPI -DHAS_FFTW3 -DUSE_PPPM` was passed to <span style="font-size:9pt;white-space:pre-wrap;color:rgb(0,128,128)">CompilerInvocation</span><span style="font-size:9pt;white-space:pre-wrap">::</span><span style="font-size:9pt;white-space:pre-wrap;background-color:rgb(228,228,255)">CreateFrom<wbr>Args</span></div><div><span style="font-size:9pt;white-space:pre-wrap;background-color:rgb(228,228,255)"><br></span></div><div>Any ideas why I can't visit these things?</div><br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">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/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>