views:

581

answers:

6

Hi,

I recently wrote a post here on Stackoverflow asking for some C# libraries that calculate metrics, mainly CC...unfortunately with no success. So I'm going to write it myself. I did a search on the web of what could be the best approach, but before starting I'd like to ask you on how you'd do it.

I'm currently between two kind of approaches

  • Given a source code directory, to parse the source code with regex expressions or similar for identifying the constructs like methods, conditional statements etc. for being able to calculate CC
  • Given an assembly, loading it and analyzing it (using CodeDom?)

I'm more for the 2nd approach, since parsing the source code directly doesn't seem to be a good approach to me. I've read about CodeDom which is integrated in the .Net framework. I know it is used for dynamic code generation. I guess I could also use it for analyzing the code structure, can't I? Does anybody of you have some good starting point of using CodeDom, some hints, good tutorials where to start?

Thanks

Edit: Or possibly some other utility that allows to parse source code easily (DOM like structure).

A: 

The problem with using CodeDom is that it is one way - there are APIs for generating code, but none for parsing code. I seem to recall hearing about some unsupported/hidden APIs that parse code into CodeDom structures, but I'm not sure.

Also, how would you get the code from the assembly itself? Reflection doesn't go down to the IL, but only to members of classes.

Andy
I cannot use Reflection, you're right. But isn't it possible to load a code represented as string into CodeDom and then to traverse the CodeDom structure? This tool seems to use such an approach: http://www.anticipatingminds.com/Content/Products/devMetrics/devMetricsSharedCode.aspx
Juri
I haven't looked into CodeDom for a long time, but there weren't any parsers at the time. Are you sure that devMetrics works off of assemblies, and not the code itself?
Andy
+1  A: 

Have a look at the Common Compiler Infrastructure (CCI) from Microsoft Research.

Bryan Watts
A: 

Have a look at this CodeProject article. It seems to be the beginning of what you are trying to do, but would need some additions; since the example code in the article does not parse members, only types and namespaces.

There exist no complete parsers for CodeDOM that I know of, which is also mentioned on the BCL Team Blog.

driis
+2  A: 

Gendarme does some code metrics (sort of) with the help of Mono.Cecil, perhaps it would help with what you are trying to accomplish?

chyne
A: 

See C# Metrics tool that computes CC and a wide variety of other metrics.

Ira Baxter
thx, I'll take a look at it
Juri
A: 

Since you're concerned with .NET have a look at using .Net Reflector

There are a variety of Plugins one of which calculates some basic metrics including CC.

If you don't find what you're looking for why not write your own. .NET Reflector has a plugin API providing a type of CodeDom model that you can analyse easily (also see at bottom of page)

Tom Carter