Ejemplo n.º 1
0
        private void DoTest(MethodDefinition method, params string [] userNames)
        {
            int count = 0;

            foreach (Instruction ins in method.Body.Instructions)
            {
                VariableDefinition v = ins.GetVariable(method);
                if (v != null)
                {
                    bool userName = userNames.Any(n => n == v.ToString());
                    if (userName)
                    {
                        Assert.IsFalse(v.IsGeneratedName(method.DebugInformation), "{0} was reported as a generated name", v.ToString());
                    }
                    else
                    {
                        ++count;
                        Assert.IsTrue(v.IsGeneratedName(method.DebugInformation), "{0} was not reported as a generated name", v.ToString());
                    }
                }
            }

            if (count == 0)
            {
                Assert.Fail("Didn't find any generated locals for VariableDefinitionRocksTest::{0}", method.Name);
            }
        }
Ejemplo n.º 2
0
        public RuleResult CheckMethod(MethodDefinition method)
        {
            // rule does not apply to external methods (e.g. p/invokes)
            // and generated code (by compilers or tools)
            if (!method.HasBody || method.IsGeneratedCode())
            {
                return(RuleResult.DoesNotApply);
            }

            MethodBody body = method.Body;

            if (!body.HasVariables)
            {
                return(RuleResult.Success);
            }

            var variables = body.Variables;
            int count     = variables.Count;

            if (used == null)
            {
                used = new BitArray(Math.Max(DefaultLength, count));
            }
            else if (count > used.Length)
            {
                used = new BitArray(count);
            }
            used.SetAll(false);

            foreach (Instruction ins in body.Instructions)
            {
                VariableDefinition vd = ins.GetVariable(method);
                if (vd != null)
                {
                    used [vd.Index] = true;
                }
            }

            for (int i = 0; i < count; i++)
            {
                if (!used [i])
                {
                    // sometimes the compilers generates some locals without really
                    // using them (e.g. assign only a constant). In this case we need
                    // to determine if the variable is "genuine" or a compiler
                    // (*) seen in a while (true) loop over a switch
                    VariableDefinition variable = variables [i];
                    if (variable.IsGeneratedName())
                    {
                        continue;
                    }

                    string s = String.Format(CultureInfo.InvariantCulture, "Variable '{0}' of type '{1}'",
                                             variable.Name, variable.VariableType.GetFullName());
                    Runner.Report(method, Severity.Low, Confidence.Normal, s);
                }
            }

            return(Runner.CurrentRuleResult);
        }
Ejemplo n.º 3
0
        private string Mutates(MethodDefinition method, Instruction end)
        {
            string name = null;

            Instruction ins = AvoidMethodsWithSideEffectsInConditionalCodeRule.FullTraceBack(method, end);

            if (ins != null)
            {
                Log.WriteLine(this, "checking args for call at {0:X4} starting at {1:X4}", end.Offset, ins.Offset);
                while (ins.Offset < end.Offset && name == null)
                {
                    if (ins.IsStoreArgument())
                    {
                        ParameterDefinition pd = ins.Operand as ParameterDefinition;
                        name = "argument " + pd.Name;
                    }
                    else if (ins.IsStoreLocal())
                    {
                        VariableDefinition vd = ins.GetVariable(method);
                        if (!vd.IsGeneratedName())
                        {
                            string variableName = String.Empty;                             // vd.Name is not valid since Cecil 0.10
                            name = "local " + variableName;
                        }
                    }
                    else if (ins.OpCode.Code == Code.Stfld || ins.OpCode.Code == Code.Stsfld)
                    {
                        FieldReference fr = ins.Operand as FieldReference;
                        if (!fr.IsGeneratedCode())
                        {
                            name = "field " + fr.Name;
                        }
                    }

                    ins = ins.Next;
                }
            }

            return(name);
        }