Determine SLOC and complexity of C# and C++ from .NET
Question
I have been using SourceMonitor on my project for a couple of years to keep records of source-code complexity and basic SLOC (including comments) for C# and C++ components. These are used for external reporting to our customer, so I'm not in a position to argue their merits or lack of.
I've been working on a repository analysis tool which is able to give me a snap-shot view of the project at any date/time. The next stage I want to add is caching of the metrics for a specified file and revision.
I know SourceMonitor can be scripted to allow me to supply the files to be tested and grab the metrics out of the result file CSV or XML.
Is there a native library in .NET that I could use to do the same thing -- e.g. avoid spawning an external process and parsing the results.
I only really need the following metrics:
- SLOC
- Number of comment lines
- Complexity of most complex method
- Name of most complex method
I need to run this on C# code and normal C++ code.
Edit: since I already have tool which provides the GUI and reports I want, the metrics need to be scripted or generated using a library/API without manual steps. Ideally I want to get metrics for a specified file/revision (rather than a whole project) which my utility will drag from version-control automatically.
NOTE: I created a bounty for this and was on holiday when it expired... the NDepends answer does NOT satisfy me as it doesn't look at source-code but the assembly itself!!!
Solution 5
Whilst I never did find a .NET product that can equally parse C# and C++, I did manage to find an easy-to-use product, CODECOUNT that supports those languages and many more.
It has a simple command line, unlike SourceMonitor that was being used on my project up until CODECOUNT replaced it.
OTHER TIPS
You can find an open source code for C# SLOC and comments here: http://code.google.com/p/projectpilot/source/browse/#svn/trunk/ProjectPilot.Framework/Metrics
A reliable command line based tool for calculating SLOC is Cloc. It supports many languages including C# and C++. Supported output formats are xml, csv and sql.
This won't give you function complexity and it's not scriptable (that I know of), but the SlickEdit Gadgets for VS has a great SLOC report tool and you can use from the solution explorer and will give you a detailed report at the file, project or solution level.
You can get it here: http://www.slickedit.com/content/view/441