7

I am looking for a way to programmatically inspect a .NET (C#, VB.NET,..) source code to perform static code analysis.

I'd like to perform queries on the code such as: - list classes whose name begin by x - list all the subclasses of x - list methods which instanciate an object of class x - determine if method x contains a variable named y - list methods calling the method y - ...

What I am looking for is an API or something else allowing me to write programs able to examine a source code.

8 Answers 8

9

NDepend gives a SQL-like query language for querying .NET code structure.

Sign up to request clarification or add additional context in comments.

4 Comments

Brilliant tool. +1 for NDepend recommendation.
Where's the programmatic interface in NDepend? In other words how can it be used from my own C# code?
I believe NDepend has some sort of programming/query language... however that's the limit of my knowledge. I guess you'd have to contact the NDepend people directly.
Have a look at my updated answer, I describe more details concerning the NDepend programming/query language.
5

You can use System.Reflection, that should do the trick nicely for some of the things you want. As far as getting into the IL itself, check out Mono's Cecil.

3 Comments

Unless, @monkeyget is looking at the source code. Then they'll probably want a parser.
Or a compiler :). Yea I uh missed that he said "source code". I guess NDepend and some of the IDE plugins might be of assistance then. Sorry!
System.Reflection is indeed an option which i'll look into but it seems limited and i'm sure there must exist more powerful and simple tools for what I want to do. I mentioned source code in the question but a tool working on IL would be ok too.
1

Why not use FxCop for static code analysis?

1 Comment

Because he wants to WRITE software that will allow him to do what FxCop does.
1

See the DMS Software Reengineering Toolkit.

DMS provides parsers that automatically build ASTs for many languages (C, C++, Java, C# [1.2, 2.0, 3.0 and 4.0], COBOL, ECMAScript, PHP, Verilog, ..) as well as symbol tables and control and data flow analysis for several of these.

DMS's pattern language can be used to match surface-syntax patterns, and combined with procedural analysis to ties code elements together with symbol table entries and various data flow relations. It has been used to implement a wide variety of program analysis tools, and is designed to be a foundation for you to build you own tool, without wasting a vast amount of time building basic program analysis infrastructure.

Comments

0

What about using the code model in Reflector? With the code model view add-in you should be able to get the idea of how to interrogate the structure of the code.

Comments

0

What about StyleCop? http://code.msdn.microsoft.com/sourceanalysis. But it doesn't support APIs.

Comments

0

To complete the stusmith's answer, NDepend seems to be what you are looking for. NDepend lets write Code Queries and Rules over LINQ Queries, (what we call CQLinq). Disclaimer: I am one of the developers of the tool

For example here are some CQLinq queries:

-> list classes whose name begin by x

from t in Application.Types.WithNameLike("^x")
where t.IsClass select t

-> list all the subclasses of x

from t in Application.Types
where t.DeriveFrom("MyNamespace.MyTypeX")
select t

-> list methods which instanciate an object of class x

from m in Application.Methods
where m.CreateA("MyNamespace.MyTypeX")
select m

-> list methods calling the method y - ...

from m in Application.Methods
where m.IsUsing("MyNamespace.MyType.MyMethodY()")
select m

More than 200 code rules are proposed by default. Customizing existing rules or creating your own rules is straightforward thanks to the well-known C# LINQ syntax.

CQLinq queries can be edited live in VisualStudio, and offer instant result, with browsing facilities:

enter image description here

Actually, CQLinq is based on NDepend.API, and more specifically on types in the namespace NDepend.CodeModel. With NDepend.API you can write programs to do things more tricky tghan just CQLinq queries, for example we wrote a Code Duplicate Finder tool with NDepend.API.

Rules can be verified live in Visual Studio and at Build Process time, in a generated HTML+javascript report.

Comments

0

I would recommend using Roslyn for this.

1 Comment

Because the OP said 'programmatically inspect .. source code' and that is what Roslyn is all about. The idea of using the framework as C# itself will use is very appealing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.