Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Rule in FxCop to only apply to methods called by particular type's method

I want to create an FxRule that applies a method, only if the method gets called from a specific class.

NOTE: I don't want to just apply a rule to a method of a particular class, i want to be able to handle methods calling other methods calling other methods which do the boxing.

I'd like to get FxCop to report problems associated with the method that does the boxing.

Below is the code i have so far:

using System;
using System.Linq;
using Microsoft.FxCop.Sdk;
using System.Collections.Generic;

class CheckUpdatableComponents : BaseIntrospectionRule
{
    private string[] MethodsToCheck = new string[] { "BeginDraw", "BeginRun", "Draw", "EndRun", "EndDraw", "Update" };

    /// <summary>Gets the base class hooked up.</summary>
    public CheckUpdatableComponents()
        : base("CheckUpdatableComponents", "FxCopRules.Rules", typeof(CheckUpdatableComponents).Assembly)
    {
    }

    public override ProblemCollection Check(string namespaceName, TypeNodeCollection types)
    {
        foreach (var type in types.Where(T => IsSubClassOf(T, "Microsoft.Xna.Framework.Game")))
        {
            foreach (var MethodToCheck in MethodsToCheck)
            {
                Method RunMethod = type.GetMethod(Identifier.For(MethodToCheck));

                if (RunMethod != null)
                {
                    Visit(RunMethod);
                }
            }
        }

        return Problems;
    }

    public override void VisitMethod(Method method)
    {
                Problems.Add(new Problem(GetResolution(), method, method.ToString())); // This problem only appears for each of the RunMethods, and doesn't seem to be recursing down the tree.


        foreach (var Instruction in method.Instructions)
        {
            if (Instruction.NodeType == NodeType.Box ||
                Instruction.NodeType == NodeType.Unbox ||
                Instruction.NodeType == NodeType.UnboxAny ||
                Instruction.OpCode == OpCode.Box ||
                Instruction.OpCode == OpCode.Unbox ||
                Instruction.OpCode == OpCode.Unbox_Any)
            {
            }
        }

        base.VisitMethod(method);
    }

    private bool IsSubClassOf(TypeNode type, string typeName)
    {
        if (type.FullName == typeName)
            return true;
        if (type.BaseType == null)
            return false;
        else
            return IsSubClassOf(type.BaseType, typeName);
    }
}

My issue with the above code, is firstly, that it doesn't appear to be recursing. Secondly, that FxCop reports the problems as being associated with the namespace (probably because i kick off the visit using the Check(namespace....) part.

My problem is that i want FxCop to report a method that has boxing as a problem, but only if it gets called by a particular method, however i have no way of walking up the call tree, i can only visit lower nodes to check for problems with my starting location.

Has anyone done this sort of thing before?

How can i find out what methods call a given method?

like image 217
George Duckett Avatar asked Oct 11 '22 17:10

George Duckett


1 Answers

EDIT: This doesn't work in the case of virtual method calls where the IL is callvirt. See my question here.

I managed to work it out, by discovering the method CallGraph.CallersFor(). I'm now looking for methods declared with a given attribute, or declared by a class with a given attribute, but the principal is the same.

using System;
using System.Linq;
using Microsoft.FxCop.Sdk;
using System.Collections.Generic;

class CheckUpdatableComponents : BaseIntrospectionRule
{
    // private string[] MethodsToCheckNames = new string[] { "BeginDraw", "BeginRun", "Draw", "EndRun", "EndDraw", "Update" };

    /// <summary>Gets the base class hooked up.</summary>
    public CheckUpdatableComponents()
        : base("CheckUpdatableComponents", "FxCopRules.Rules", typeof(CheckUpdatableComponents).Assembly)
    {
    }

    public override ProblemCollection Check(Member member)
    {

        Method method = member as Method;

        if (method != null)
        {
            if (ShouldCheckMethod(method))
            {
                foreach (var Instruction in method.Instructions)
                {
                    if (Instruction.NodeType == NodeType.Box ||
                        Instruction.NodeType == NodeType.Unbox ||
                        Instruction.NodeType == NodeType.UnboxAny ||
                        Instruction.OpCode == OpCode.Box ||
                        Instruction.OpCode == OpCode.Unbox ||
                        Instruction.OpCode == OpCode.Unbox_Any)
                    {
                        Problems.Add(new Problem(GetResolution(), Instruction, Instruction.SourceContext.StartLine.ToString()));
                    }
                }
            }
        }

        return Problems;
    }

    public bool ShouldCheckMethod(Method method)
    {
        Queue<Method> MethodsToCheck = new Queue<Method>();

        List<Method> MethodsChecked = new List<Method>();

        MethodsToCheck.Enqueue(method);

        while (MethodsToCheck.Count != 0)
        {
            Method MethodToCheck = MethodsToCheck.Dequeue();

            if (!MethodsChecked.Contains(MethodToCheck) && MethodToCheck != null)
            {
                /*if (IsSubClassOf(MethodToCheck.DeclaringType, "Microsoft.Xna.Framework.Game") &&
                    MethodsToCheckNames.Contains(MethodToCheck.Name.Name))
                {
                    return true;
                }*/

                foreach (var attribute in MethodToCheck.Attributes.Union(MethodToCheck.DeclaringType.Attributes))
                {
                    if (attribute.Type != null &&
                        attribute.Type.FullName == "GridEngine.Components.Debugging.Attributes.FxCop.PerformanceCriticalAttribute")
                    {
                        return true;
                    }
                }

                // Add methods up the class tree
                MethodsToCheck.Enqueue(MethodToCheck.OverriddenMethod);
                MethodsToCheck.Enqueue(MethodToCheck.HiddenMethod);



                // Add calling methods
                foreach (var CallingMethod in CallGraph.CallersFor(MethodToCheck))
                {
                    MethodsToCheck.Enqueue(CallingMethod);
                }
            }

            MethodsChecked.Add(MethodToCheck);
        }

        return false;
    }

    private bool IsSubClassOf(TypeNode type, string typeName)
    {
        if (type.FullName == typeName)
            return true;
        if (type.BaseType == null)
            return false;
        else
            return IsSubClassOf(type.BaseType, typeName);
    }
}
like image 114
George Duckett Avatar answered Nov 14 '22 22:11

George Duckett