Reputation: 7666
I'm trying to write a program that will allow me to give it a collection of C# code files and analyze them for testability problems. I'm starting out with just the 4 provided in the preceding link.
The end goal is to create a report of all the files, any testability problems they might have, and specific code annotations on where these problems may be observed (if applicable). What I would like help with is choosing some tools to make the job of parsing the code easier; I can read everything in as a string but making order out of the code to analyze it is very difficult in that situation.
I have evaluated the following things so far:
As far as what I'm looking for:
I would at least like to have detection of basic object structures and accessor objects to navigate around in (e.g., a File object has a Namespace property with a collection of Classes, each Class has collections of Members, Constructors, and Methods, etc). Obviously if there are tools that can get more and more elaborate and detailed that would be amazing, but just having these things available to allow me to process small, focused strings would be a complete god-send.
Thanks in advance.
Upvotes: 2
Views: 439
Reputation: 16464
NRefactory 5 offers roughly the same analysis features for C# as Roslyn for C#, "only" the ability to compile to IL is missing.
Not sure what's going on with the GTK-based demo; so I've fixed the WinForms-based demo and added that back to the solution.
For parsing C# code, use new CSharpParser().Parse(sourceCode, "filename.cs")
. This will give you the full syntax tree for the file; i.e. a CompilationUnit
root node with NamespaceDeclaration
s as children. Use the demo application to see what the tree looks like.
If you want to detect certain code patterns (your testability issues), you can use NRefactory's pattern matching feature. (see doc/Pattern Matching.html
for details)
I think NRefactory with pattern matching might work better than Roslyn for your usecase.
Upvotes: 1
Reputation: 49320
If you can use prerelease code, you might want to check out roslyn, i.e. "compiler as a service":
Traditionally, compilers are black boxes – source code goes in one end and object files or assemblies come out the other end. The Roslyn [project] changes that model by opening up the Visual Basic and C# compilers as APIs. These APIs allow tools and end-users to share in the wealth of information the compilers have about code.
Mind you, however, that interpreting what you get (a syntax tree) might still be a lot of work.
Upvotes: 3