<div dir="ltr">Hi all,<div><br></div><div>I'm using clang AST to do some static analysis.</div><div>In my case, I want to find all assignment statements for pointer variables.</div><div>For example, here is the source code snippet:</div><div><b><u>int *a, int **b;</u></b></div><div><b><u>**b = *a;</u></b></div><div>I want to know there is an assignment from "*a" to "**b".</div><div><b><u><br></u></b></div><div>I use RecursiveASTVistor to traverse AST. I rewrite VistitStmt() to check if AST Node is an assignment statement.</div><div>







<p class=""><span class="">bool VisitStmt(Stmt *s) {</span></p>
<p class=""><span class=""> </span><span class="">if(BinaryOperator *binaryOperator = dyn_cast<BinaryOperator>(s)) {</span></p>
<p class=""><span class="">      </span><span class="">if(binaryOperator->isAssignmentOp()) { do something}</span></p><p class=""><span class="">}</span></p><p class="">Here is the AST for the source code snippet above: </p><p class=""><span class=""><b>BinaryOperator</b></span><span class=""> 0x9532d48</span><span class=""> <</span><span class="">line:66:2</span><span class="">, </span><span class="">col:14</span><span class="">> </span><span class="">'int'</span><span class=""> '='</span></p><p class=""><span class="">    | |-</span><span class="">UnaryOperator</span><span class=""> 0x9532cb0</span><span class=""> <</span><span class="">col:2</span><span class="">, </span><span class="">col:4</span><span class="">> </span><span class="">'int'</span><span class=""> lvalue</span><span class=""> prefix '*'</span></p><p class=""><span class="">    | | `-</span><span class="">ImplicitCastExpr</span><span class=""> 0x9532c98</span><span class=""> <</span><span class="">col:3</span><span class="">, </span><span class="">col:4</span><span class="">> </span><span class="">'int *'</span><span class=""> <</span><span class="">LValueToRValue</span><span class="">></span></p><p class=""><span class="">    | |   `-</span><span class="">UnaryOperator</span><span class=""> 0x9532c78</span><span class=""> <</span><span class="">col:3</span><span class="">, </span><span class="">col:4</span><span class="">> </span><span class="">'int *'</span><span class=""> lvalue</span><span class=""> prefix '*'</span></p><p class=""><span class="">    | |     `-</span><span class="">ImplicitCastExpr</span><span class=""> 0x9532c60</span><span class=""> <</span><span class="">col:4</span><span class="">> </span><span class="">'int **'</span><span class=""> <</span><span class="">LValueToRValue</span><span class="">></span></p><p class=""><span class="">    | |       `-</span><span class="">DeclRefExpr</span><span class=""> 0x9532c38</span><span class=""> <</span><span class="">col:4</span><span class="">> </span><span class="">'int **'</span><span class=""> lvalue</span><span class=""> </span><span class=""><b>Var</b></span><span class=""> 0x9532b00</span><span class=""><b> 'b'</b></span><span class=""> </span><span class="">'int **'</span></p><p class=""><span class="">    | `-</span><span class="">ImplicitCastExpr</span><span class=""> 0x9532d30</span><span class=""> <</span><span class="">col:13</span><span class="">, </span><span class="">col:14</span><span class="">> </span><span class="">'int'</span><span class=""> <</span><span class="">LValueToRValue</span><span class="">></span></p><p class=""><span class="">    |   `-</span><span class="">UnaryOperator</span><span class=""> 0x9532d10</span><span class=""> <</span><span class="">col:13</span><span class="">, </span><span class="">col:14</span><span class="">> </span><span class="">'int'</span><span class=""> lvalue</span><span class=""> prefix '*'</span></p><p class=""><span class="">    |     `-</span><span class="">ImplicitCastExpr</span><span class=""> 0x9532cf8</span><span class=""> <</span><span class="">col:14</span><span class="">> </span><span class="">'int *'</span><span class=""> <</span><span class="">LValueToRValue</span><span class="">></span></p><p class="">
















</p><p class=""><span class="">    |       `-</span><span class="">DeclRefExpr</span><span class=""> 0x9532cd0</span><span class=""> <</span><span class="">col:14</span><span class="">> </span><span class="">'int *'</span><span class=""> lvalue</span><span class=""> </span><span class=""><b>Var</b></span><span class=""> 0x9532a88</span><span class=""><b> 'a'</b></span><span class=""> </span><span class="">'int *'</span></p><div><br></div><div>In VisitStmt() function, at the point when this <b>BinaryOperator</b> is visited, I only have the information of the current AST Node.</div><div>How can I traverse the <b>subtree</b> of this <b>BinaryOperator </b>so that I can get all the info, such as variable name <b>a</b>, <b>b</b>, prefix <b>* ,** </b>?</div><div><br></div><div>Thank you.</div>-- <br><div class="gmail_signature"><div dir="ltr">Regards,<div>Kai</div></div></div>
</div></div>