tags:

views:

496

answers:

3

How do I write a code analysis tool for vs2008 to prevent a specific framework call, such as GC.WaitForFullGCComplete() or Application.DoEvents()

I tried overriding the VisitMethodCall in my custom rule, but I cannot figure out what the Microsoft.FxCop.Sdk.MethodCall parameter really has in it. Could not find example on the web.

Can someone point me in the correct direction?

A: 

This is not the answer to your question: Why do you want to do that? What is the real problem you are trying to solve? The time spent of creating the FxCop rule (not particularly easy) might be best spent eleswhere?

There are legitimate reasoins for calling Application.DoEvents() (though I would be suspicious if it was 'sprinkled' through code). I can't ever seeing GC.WaitForFullGCComplete() in any code.

Mitch Wheat
Legitimate reasons can have suppression added. Wouldn't the the FXCop rule basically be telling you that the its been 'sprinkled'.
Preet Sangha
@Preet Sangha: as GC.WaitForFullGCComplete() is rarely used anyway, I doubt it.
Mitch Wheat
Those were just examples. I need a rule that can warn on a list of framework method calls.
Bill
I am building up a library of required FxCop rules for our organization. Most are easy to write; examples on the web. This particular task I could not figure out. See my comment on the question for the answer.
Bill
A: 

I remember a Debugging book that I think that had a chapter on FXCop rules and discussed how to write one. For the life of me I cannot find my copy. And I suspect you'll need reflector

Preet Sangha
A: 

An alternative to the mess of writing FxCop custom rules would be to use NDepend and its Code Query language (CQL). CQL is dedicated to write code quality rules that can be verified live in Visual Studio, or that can be verified during build process and reported in a HTML report.

If you wish to control calls to method Method1(string) declared in Class1 declared in namespaces Namespace1, you could write something like:

WARN IF Count > 0 IN SELECT METHODS WHERE IsDirectlyUsing "Namespace1.Class1.Method1(String)" AND !NameLike "regexHere"

Here are a few other samples of CQL rules (designed to be highly customizable):

Code refactored recently should be 100% covered by test:

WARN IF Count > 0 IN SELECT METHODS WHERE CodeWasChanged AND PercentageCoverage < 100

Complex methods should be commented:

WARN IF Count > 0 IN SELECT METHODS WHERE CyclomaticComplexity > 15 AND PercentageComment < 10

I don’t want that my User Interface layer to depend directly on the DataBase layer:

WARN IF Count > 0 IN SELECT NAMESPACES WHERE IsDirectlyUsing "DataLayer" AND NameIs "UILayer"

Static fields should not be named m_XXX (Custom naming conventions):

WARN IF Count > 0 IN SELECT FIELDS WHERE NameLike "^m_" AND IsStatic

Methods out of MyAssembly and MyAssembly2 should not have more than 30 lines of code:

WARN IF Count > 0 IN SELECT METHODS OUT OF ASSEMBLIES "MyAssembly1", "MyAssembly2" WHERE NbLinesOfCode > 30

Public methods should not be removed to avoid API breaking changes:

WARN IF Count > 0 IN SELECT METHODS WHERE IsPublic AND IsInOlderBuild AND WasRemoved

Types tagged with the attribute MyNamespace.FullCoveredAttribute must be thoroughly covered by tests:

WARN IF Count > 0 IN SELECT TYPES WHERE HasAttribute "MyNamespace.FullCoveredAttribute" AND PercentageCoverage < 100

Patrick Smacchia - NDepend dev