当前位置: 首页>>代码示例>>C#>>正文


C# Method.TryGetBody方法代码示例

本文整理汇总了C#中Method.TryGetBody方法的典型用法代码示例。如果您正苦于以下问题:C# Method.TryGetBody方法的具体用法?C# Method.TryGetBody怎么用?C# Method.TryGetBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Method的用法示例。


在下文中一共展示了Method.TryGetBody方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TryRetrieveMethodCall

        /// <summary>
        /// Returns a method call for a PUT. This should be of certain characteristics,
        /// It should contain only one method call as generated by us
        /// </summary>
        /// <param name="putmethod"></param>
        /// <returns></returns>
        public static bool TryRetrieveMethodCall(Method putmethod, out Method assocmethod)
        {
            SafeDebug.AssumeNotNull(putmethod, "putmethod");
            assocmethod = null;

            MethodBodyEx mbodyex;
            bool bresult = putmethod.TryGetBody(out mbodyex);
            SafeDebug.Assert(bresult, "Failed to get the body");

            int offset = 0;
            Instruction instruction;

            List<Method> allCalledMethods = new List<Method>();
            while (mbodyex.TryGetInstruction(offset, out instruction))
            {
                SafeDebug.AssumeNotNull(instruction, "instruction");
                OpCode opCode = instruction.OpCode;

                if (opCode == OpCodes.Call || opCode == OpCodes.Callvirt)
                {
                    SafeDebug.Assume(opCode.OperandType == OperandType.InlineMethod,
                        "opCode.OperandType == OperandType.InlineMethod");
                    allCalledMethods.Add(instruction.Method);
                }

                offset = instruction.NextOffset;
            }

            if (allCalledMethods.Count != 1)
                return false;

            assocmethod = allCalledMethods[0];
            return true;
        }
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:40,代码来源:PUTGenerator.cs

示例2: TryComputeMethodEffects

        /// <summary>
        /// Computes method effects statically. All written fields of a method.
        /// Can be imprecise and conservative
        /// </summary>
        /// <param name="declaringType"></param>
        /// <param name="method"></param>
        /// <param name="effects"></param>
        /// <returns></returns>
        public static bool TryComputeMethodEffects(IPexComponent host, TypeEx declaringType, Method method,
            SafeSet<Method> visitedMethods, out MethodEffects effects)
        {
            SafeDebug.AssumeNotNull(declaringType, "declaringType");
            SafeDebug.AssumeNotNull(method, "method");

            try
            {
                if (visitedMethods == null)
                    visitedMethods = new SafeSet<Method>();

                if (visitedMethods.Contains(method))
                {
                    effects = null;
                    return false;
                }

                visitedMethods.Add(method);

                //Check whether this has been computed before
                var psd = host.GetService<IPexMeStaticDatabase>() as PexMeStaticDatabase;
                if (psd.MethodEffectsDic.TryGetValue(method.GlobalIndex, out effects))
                    return true;

                var res = new SafeSet<string>();
                var directSetFields = new SafeSet<string>();
                var directCalledMethods = new SafeSet<Method>();
                var returnFields = new SafeSet<Field>();
                var modificationTypeDic = new SafeDictionary<string, FieldModificationType>();
                var parameters = method.Parameters;

                MethodBodyEx body;
                if (!method.TryGetBody(out body) || !body.HasInstructions)
                {
                    effects = null;
                    return false;
                }

                int callDepth = 0;
                int offset = 0;
                Instruction instruction;
                OpCode prevOpcode = OpCodes.Nop;

                //Stack for load instructions
                Field lastAccessedArrayField = null;
                Field lastAccessedField = null;

                while (body.TryGetInstruction(offset, out instruction))
                {
                    SafeDebug.AssumeNotNull(instruction, "instruction");
                    OpCode opCode = instruction.OpCode;
                    if (LdcOpCodes.Contains(opCode))
                    {
                        //topIsConstant = true;
                    }
                    else if (ConvOpCodes.Contains(opCode))
                    {
                        // do not change topIsConstant
                    }
                    else
                    {
                        if (opCode == OpCodes.Stfld)
                        {
                            SafeDebug.Assume(opCode.OperandType == OperandType.InlineField, "opCode.OperandType == OperandType.InlineField");
                            Field field = instruction.Field;
                            AddFieldToMethodEffects(host, declaringType, res, directSetFields, modificationTypeDic, prevOpcode, field, field.Type);
                        }
                        else if (opCode == OpCodes.Ldfld || opCode == OpCodes.Ldflda)
                        {
                            SafeDebug.Assume(opCode.OperandType == OperandType.InlineField, "opCode.OperandType == OperandType.InlineField");
                            Field accessedField = instruction.Field;

                            if (accessedField.Type.Spec == TypeSpec.SzArray)
                            {
                                lastAccessedArrayField = accessedField;
                            }
                            else
                                lastAccessedField = accessedField;
                        }
                        else if (StElemOpCodes.Contains(opCode))
                        {
                            if (lastAccessedArrayField != null)
                            {
                                //Indicates that there is n array type modified
                                AddFieldToMethodEffects(host, declaringType, res, directSetFields, modificationTypeDic, prevOpcode, lastAccessedArrayField, lastAccessedArrayField.Type);
                                lastAccessedArrayField = null;
                            }
                        }
                        else if (opCode == OpCodes.Call || opCode == OpCodes.Callvirt)
                        {
                            SafeDebug.Assume(opCode.OperandType == OperandType.InlineMethod, "opCode.OperandType == OperandType.InlineMethod");
                            Method methodinner = instruction.Method;
//.........这里部分代码省略.........
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:101,代码来源:FieldAnalyzer.cs

示例3: PopulateVertices

        /// <summary>
        /// Populates all vertices
        /// </summary>
        /// <param name="method"></param>
        private void PopulateVertices(Method method)
        {
            MethodBodyEx body;
            if (!method.TryGetBody(out body) || !body.HasInstructions)
            {
                return;
            }

            int offset = 0;
            Instruction instruction;
            while (body.TryGetInstruction(offset, out instruction))
            {
                SafeDebug.AssumeNotNull(instruction, "instruction");
                OpCode opCode = instruction.OpCode;

                InstructionVertex iv = this.AddVertex(instruction);
                this.vertices[offset] = iv;
                offset = instruction.NextOffset;
            }
        }
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:24,代码来源:InstructionGraph.cs

示例4: PopulateEdges

        /// <summary>
        /// Adds edges to the graph
        /// </summary>
        /// <param name="method"></param>
        private void PopulateEdges(Method method)
        {
            MethodBodyEx body;
            if (!method.TryGetBody(out body) || !body.HasInstructions)
            {
                return;
            }

            int offset = 0;
            Instruction instruction;
            InstructionVertex cv = null;

            //make the graph
            while (body.TryGetInstruction(offset, out instruction))
            {
                SafeDebug.AssumeNotNull(instruction, "instruction");
                OpCode opCode = instruction.OpCode;

                InstructionVertex iv = this.vertices[offset];
                if (cv != null)
                {
                    this.AddEdge(cv, iv);
                }

                if (MethodOrFieldAnalyzer.BranchOpCodes.Contains(opCode))
                {
                    InstructionVertex alternatev = this.vertices[instruction.BrTargetOffset];
                    this.AddEdge(iv, alternatev);
                    cv = iv;
                }
                else if (opCode == OpCodes.Switch)
                {
                    foreach (var switchoff in instruction.SwitchOffsets)
                    {
                        InstructionVertex alternatev = this.vertices[switchoff];
                        this.AddEdge(iv, alternatev);
                    }
                    cv = iv;
                }
                else if (opCode == OpCodes.Br || opCode == OpCodes.Br_S)
                {
                    InstructionVertex alternatev = this.vertices[instruction.BrTargetOffset];
                    this.AddEdge(iv, alternatev);
                    cv = null;
                }
                else if (opCode == OpCodes.Break)
                {
                    InstructionVertex alternatev = this.vertices[instruction.BrTargetOffset];
                    this.AddEdge(iv, alternatev);
                    cv = null;
                }
                else if (opCode == OpCodes.Ret || opCode == OpCodes.Throw)
                {
                    cv = null;
                }
                else
                {
                    cv = iv;
                }
                offset = instruction.NextOffset;
            }
        }
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:66,代码来源:InstructionGraph.cs

示例5: AreAllOffsetsCoveredInMethod

        /// <summary>
        /// Checks whether all blocks within the method are covered
        /// </summary>
        /// <param name="methoddef"></param>
        /// <returns></returns>
        public bool AreAllOffsetsCoveredInMethod(Method method)
        {
            var methoddef = method.Definition;
            MethodDefinitionBodyInstrumentationInfo info;
            if (!methoddef.TryGetBodyInstrumentationInfo(out info))
                return false;

            CoverageDomain domain;
            int[] hits;
            if (!this.coverageBuilderMaxAggregator.TryGetMethodHits(methoddef, out domain, out hits))
                return false;

            MethodBodyEx body;
            if (!method.TryGetBody(out body) || !body.HasInstructions)
                return false;

            int offset = 0;
            Instruction instruction;
            while (body.TryGetInstruction(offset, out instruction))
            {
                //For a branching instruction, check whether both sides are covered
                if (MethodOrFieldAnalyzer.BranchOpCodes.Contains(instruction.OpCode))
                {
                    if (NumTimesOffsetCovered(offset, info, hits) <= 1)
                        return false;
                }
                offset = instruction.NextOffset;
            }
            return true;
        }
开发者ID:spati2,项目名称:ICSE-2011-Covana,代码行数:35,代码来源:PexMeDynamicDatabase.cs


注:本文中的Method.TryGetBody方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。