Click here to Skip to main content
15,894,539 members
Articles / Programming Languages / C#

Static Code Analysis

Rate me:
Please Sign up or sign in to vote.
4.97/5 (34 votes)
15 Mar 2010CPOL16 min read 87K   1.1K   63  
A static code analyzer building method call networks + sample applications
This article describes the operation of a method-based static code analyzer for .NET that constructs in-memory method call networks of compiled assemblies. You will also see a concrete application of static code analysis to generate a website providing easy insights on a sample application.
<?xml version="1.0" encoding="utf-8" ?>
<analyzer
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblies>
    <!-- Include the mscorlib.dll to have access to the NotImplementedException constructor methods -->
    <assembly file="mscorlib.dll" />
    <!-- Include the client-tier assemblies -->
    <filepatterns path="..\SampleApp\ToDoApplication\bin\Debug">
      <add value="*.exe" />
      <add value="*.dll" />
    </filepatterns>
    <!-- Include the service-tier assemblies -->
    <filepatterns path="..\SampleApp\ToDoService\bin\Debug">
      <add value="*.dll" />
    </filepatterns>
    <!-- Include the unittest assemblies -->
    <filepatterns path="..\SampleApp\ToDoUnitTests\bin\Debug">
      <add value="*.dll" />
    </filepatterns>
  </assemblies>
  <!-- Set the language in which to express method signatures to C# and
       consider the given namespaces as imported -->
  <language code="C#">
    <import namespace="System" />
    <import namespace="System.Collections" />
    <import namespace="System.Collections.Generic" />
    <import namespace="ToDoSample.Contract" />
    <import namespace="ToDoSample.Service"/>
  </language>
  <processors>
    <!-- The specialmethodsprocessor adds tags "constructor" (and others) to methods to ease
         processing by the rule based processor -->
    <specialmethodsprocessor handler="Arebis.CodeAnalysis.Static.Processors.SpecialMethodsProcessor" />
    <!-- The rule based processor with its rules -->
    <rulesprocessor handler="Arebis.CodeAnalysis.Static.Processors.RulesProcessor">
      <definitions>
        <assembly path="bin\Arebis.CodeAnalysis.Static.dll" />
      </definitions>
      <rulesets>
        <ruleset name="_skip">
          <!-- the _skip tag has special meaning for the rule based processor: methods matching this rule are not processed further -->
          <namerule like="System.Windows.Markup.IComponentConnector.Connect" target="Method" />
        </ruleset>
        <ruleset name="uioperation">
          <basetyperule type="System.Windows.Controls.Control"/>
          <tagrule name="accessor" reverse="true"/>
          <tagrule name="constructor" reverse="true"/>
        </ruleset>
        <ruleset name="serviceoperationdefinition">
          <attributerule type="System.ServiceModel.ServiceContractAttribute" target="Type" />
          <modifierrule modifiers="Interface" target="Type" />
        </ruleset>
        <ruleset name="serviceoperationimplementation">
          <attributerule type="System.ServiceModel.ServiceBehaviorAttribute" target="Type" />
          <tagrule name="constructor" reverse="true"/>
        </ruleset>
        <ruleset name="explicitimplements">
          <!-- Explicit interface implementations have methodnames containing dots -->
          <namerule match=".*\..*" target="Method"/>
        </ruleset>
        <ruleset name="componentoperation">
          <modifierrule modifiers="FamANDAssem" target="Method"/>
          <modifierrule modifiers="Static" target="Method"/>
          <namerule target="Type" like="*ComponentManager"/>
          <tagrule name="constructor" reverse="true"/>
        </ruleset>
        <ruleset name="systemexception">
          <basetyperule type="System.Exception"/>
          <namerule target="Type" like="System.*"/>
        </ruleset>
        <ruleset name="applicationexception">
          <basetyperule type="System.Exception"/>
          <tagrule name="constructor"/>
          <tagrule name="systemexception" reverse="true"/>
        </ruleset>
        <ruleset name="missingimplementationexception">
          <basetyperule type="System.NotImplementedException"/>
          <tagrule name="constructor"/>
        </ruleset>
        <ruleset name="unittest">
          <attributerule type="Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute" target="Method"/>
        </ruleset>
        <ruleset name="defaultimplementation">
          <!-- Methods with the serviceoperationimplementation tag are connected to their interface definition -->
          <tagrule name="serviceoperationimplementation"/>
        </ruleset>
        <ruleset name="leafmethod">
          <!-- Exceptions are made leaf to break the link to the supertype constructor -->
          <tagrule name="applicationexception"/>
        </ruleset>
        <ruleset name="leafmethod">
          <!-- We are not interested in the methodcalls within the System namespace -->
          <namerule target="Type" like="System.*"/>
        </ruleset>
      </rulesets>
    </rulesprocessor>
    <!-- The defaultimplementationprocessor connects methods with the defaultimplementation tag to their interface definition -->
    <defaultimplementationprocessor handler="Arebis.CodeAnalysis.Static.Processors.DefaultImplementationProcessor" />
    <!-- The defaultmethodprocessor connects methods with the defaultmethod to their types constructor (not used here) -->
    <defaultmethodprocessor handler="Arebis.CodeAnalysis.Static.Processors.DefaultMethodProcessor" />
    <!-- The virtualmethodprocessor connects virtual methods to their base definition if it has the baseimplementation tag (not used here) -->
    <virtualmethodprocessor handler="Arebis.CodeAnalysis.Static.Processors.VirtualMethodProcessor" />
    <!-- The leafmethodprocessor disconnects methods with the leafmethod tag -->
    <leafmethodprocessor handler="Arebis.CodeAnalysis.Static.Processors.LeafMethodProcessor" />
  </processors>
</analyzer>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect AREBIS
Belgium Belgium
Senior Software Architect and independent consultant.

Comments and Discussions