Get number of unique operators and operands for calculation of Halstead metrics

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Get number of unique operators and operands for calculation of Halstead metrics

manavender reddy
Hi all,

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.
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 ? 


Thanks
Manavender

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Get number of unique operators and operands for calculation of Halstead metrics

Douglas Gregor

On Aug 15, 2010, at 1:27 PM, manavender reddy wrote:

> Hi all,
>
> 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.
> 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 ?

You can gather this information by walking the abstract syntax tree. Look for StmtVisitor in the Clang code-base; it will help you walk the AST so you can count operators and operands.

        - Doug


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Get number of unique operators and operands for calculation of Halstead metrics

Ted Kremenek
In reply to this post by manavender reddy
Hi Manavender,

I don't know if your metrics actually require a CFG, or just can be done with a walk on the AST.

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:

  if (CallExpr *CE = dyn_cast<CallExpr>(S))
  ...

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.

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.

Ted

On Aug 15, 2010, at 1:27 PM, manavender reddy wrote:

> Hi all,
>
> 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.
> 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 ?
>
>
> Thanks
> Manavender
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Get number of unique operators and operands for calculation of Halstead metrics

manavender reddy
Thanks guys :) 

On Mon, Aug 16, 2010 at 9:23 AM, Ted Kremenek <[hidden email]> wrote:
Hi Manavender,

I don't know if your metrics actually require a CFG, or just can be done with a walk on the AST.

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:

 if (CallExpr *CE = dyn_cast<CallExpr>(S))
 ...

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.

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.

Ted

On Aug 15, 2010, at 1:27 PM, manavender reddy wrote:

> Hi all,
>
> 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.
> 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 ?
>
>
> Thanks
> Manavender
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev