Custom Code Analysis Rules


We were recently asked to extend the Code Analysis rules for Visual Studio.  Here is an example I prepared for a demo which I have to show how an organization can add their own custom rules to FxCop.  The assembly can then be deployed to your developers and can also be deployed to your Visual Studio Team Foundation Build server so your rules can be checked during any server builds you are doing.

The following example class simply scans the code to enforce a naming convention and provide a warning (or error) to the developer that it is not compliant with your companies coding standards.  This makes the manual code review more efficient.  If you are looking for a tool to do Code Reviews with your developers I would recommend you check out Team Foundation Sidekicks 2008 by Attrice Corporation.

The Custom Rule

I created a new empty Windows Class Library project in Visual Studio 2008 named RuleTest.

I added the following references: FxCopCommon, FxCopSdk and Microsoft.Cci.  These are located in the following directory: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Team Tools\Static Analysis Tools\FxCop.  NOTE: On a 32-bit system the "Program Files" directory is simply named "Program Files", I built this sample on Vista x64 so my "Program Files" directory for Visual Studio is located under "Program Files (x86)".

I created a new class named NamingRules.cs and inherited the class from BaseIntrospectionRule

In my constructor I call the base class constructor and initialize it with information about my Custom Rule:

public NamingRules()
            : base("NamingRules", "RuleTest.NamingRules", typeof(NamingRules).Assembly)
        {
            tagger = new Regex("(g_|m_)?");
        }

I then override the following two methods from the base class:

public override void BeforeAnalysis()
{
  ...               
}
public override ProblemCollection Check(Member member)
{
  ...
  return this.Problems;
}

You must also provide an XML file which contains all the text used by the rule.  This file has its Build Action set to "Embedded Resource" so it is embedded into the assembly. Here is what it looks like:

image

When you compile your Class Library and get your assembly you must place it in the following directory so Visual Studio will use it.  NOTE: You will have to close Visual Studio to get it to load your new rule.

C:\Program Files (x86)\Microsoft Visual Studio 9.0\Team Tools\Static Analysis Tools\FxCop\Rules

Then you can right click your project and select "Run Code Analysis"

Results from Code Analysis Run 

Microsoft FxCop

You can also test your custom rule assembly using the FxCop GUI which you can get from the FxCop 1.36 Beta 2 which is available on the Microsoft download site.  This is a stand-alone interface which will allow you to test your custom rule before you deploy it to Visual Studio.  Here is a screen capture of the FxCop GUI:

Microsoft FxCop Rules Interface

If you right click on the FxCop project on the "Rules" tab you can add your custom rule assembly, then on the "Targets" tab add your assembly you want to run your rule on and click the "Analyze" toolbar button.  Here is what the output looks like:

Microsoft FxCop Analyze Target

The code for this custom rule demo is posted here on my SkyDrive.

About wesmacdonald

Wes MacDonald is a Visual Studio ALM MVP, PSD, MCT and a Principal Consultant for LIKE 10 INC., a SharePoint Server, Visual Studio and Team Foundation Server Consulting Firm providing premium support and guidance services.

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: