Thanks guys :) <br><br><div class="gmail_quote">On Mon, Aug 16, 2010 at 9:23 AM, Ted Kremenek <span dir="ltr"><<a href="mailto:kremenek@apple.com">kremenek@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi Manavender,<br>
<br>
I don't know if your metrics actually require a CFG, or just can be done with a walk on the AST.<br>
<br>
Assuming you are starting with a FunctionDecl*, it is possible to query the FunctionDecl for its body (getBody()).  That value will be a Stmt*.  After that, you can walk the AST using a variety of schemes.  To "pattern match" a Stmt* against a particular AST kind (i.e., a subclass of Stmt), do:<br>

<br>
  if (CallExpr *CE = dyn_cast<CallExpr>(S))<br>
  ...<br>
<br>
There are plenty of examples like this in the Clang codebase.  Specific Stmt/Expr classes will have accessors to their subexpressions, but there is also a generic iterator interface in Stmt (child_begin(), child_end()) which allows you to recursively walk the AST.  We also have various visitor classes that you can use, e.g. StmtVisitor, RecursiveASTVisitor, which also have example uses in the codebase.<br>

<br>
Using the CFG is a bit tricker.  The CFG consists of basic blocks, which you can iterate through.  Each CFGBlock contains an ordered set of statements, and those statements can be traversed just like you would traverse an AST.  The main catch is that CFGBlocks have a notion of "block-level expression", which are the Stmt* that appear in the ordered set (not all subexpressions appear in the set).  Those block-level expressions may appear as subexpressions of later block-level expressions, but they are ordered in the CFGBlock to represent control-flow dependencies.<br>

<font color="#888888"><br>
Ted<br>
</font><div class="im"><br>
On Aug 15, 2010, at 1:27 PM, manavender reddy wrote:<br>
<br>
</div><div><div></div><div class="h5">> Hi all,<br>
><br>
> Can anyone help me with this. How can i find number of unique operators and operands in a function. Here's what i am doing.<br>
> I first got an CFG of the function and then i am going through each block. I am stuck after that, i dont know how to parse each Stmt/Expr in a block to get operators and operands ?<br>
><br>
><br>
> Thanks<br>
> Manavender<br>
</div></div><div><div></div><div class="h5">> _______________________________________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br>
</div></div></blockquote></div><br>