コード例 #1
0
ファイル: CallGraph.cs プロジェクト: zhuyue1314/CodeContracts
        /// <summary>
        /// Adds the methods of the given type to the method order as well
        /// as recursively calling AddType on nested types within the given type.
        /// Call this with every top-level type to be analyzed.
        /// Internally called with nested types as well.
        /// </summary>
        public void AddType(Type type)
        {
            //TODO:0 HACK HACK HACK for the TechFest demo
            // If it is a compiler generated class, we skip it
            foreach (Attribute a in this.md.GetAttributes(type))
            {
                string fullname = md.FullName(md.AttributeType(a));
                if (fullname == "System.CodeDom.Compiler.GeneratedCodeAttribute")
                {
                    return;
                }
                if (fullname == "System.Diagnostics.Contracts.ContractClassForAttribute")
                {
                    return;
                }
            }


            foreach (Method method in md.Methods(type))
            {
                if (md.Name(method) == "ObjectInvariant")
                {
                    // ignore contract helper method
                    continue;
                }
                this.AddMethod(method);
            }

            foreach (Type nested in md.NestedTypes(type))
            {
                this.AddType(nested);
            }
        }
コード例 #2
0
        public static bool TryGetMethodHashAttribute <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(
            this IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> metaDataDecoder,
            Method method,
            out MethodHashAttribute mhAttr)
        {
            Contract.Requires(metaDataDecoder != null);
            Contract.Ensures(!Contract.Result <bool>() || Contract.ValueAtReturn(out mhAttr) != null);

#if false                                          // dead code???
            if (false && method.Equals(cache.One)) // problem: successive analyses are run in the same worker
            {
                mhAttr = cache.Two;
                return(mhAttr != null);
            }
#endif
            mhAttr = null;
            foreach (var attr in metaDataDecoder.GetAttributes(method))
            {
                var attrType = metaDataDecoder.AttributeType(attr);
                if (metaDataDecoder.FullName(attrType) == NameFor.MethodHashAttribute)
                {
                    mhAttr = MethodHashAttribute.FromDecoder(metaDataDecoder.PositionalArguments(attr));
                    if (mhAttr != null)
                    {
                        break;
                    }
                }
            }

            cache = Pair.For <object, MethodHashAttribute>(method, mhAttr);

            return(mhAttr != null);
        }
コード例 #3
0
            private static bool TryAddAttribute(Set <String> mask, Attribute attrib,
                                                IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder,
                                                out string attribName)
            {
                Contract.Requires(mask != null);
                Contract.Requires(mdDecoder != null);

                attribName = null;

                var attribType = mdDecoder.AttributeType(attrib);

                if (mdDecoder.Name(attribType) == "SuppressMessageAttribute")
                {
                    var args = mdDecoder.PositionalArguments(attrib);

                    Contract.Assume(args != null);

                    if (args.Count < 2)
                    {
                        return(false);
                    }

                    var name = (string)args[0];

                    if (name == "Microsoft.Contracts")
                    {
                        attribName = (string)args[1];
                        mask.Add(attribName);

                        return(true);
                    }
                }

                return(false);
            }
コード例 #4
0
        public static MethodRegressionOutcomes GetOutcomes <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Method method, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder)
        {
            if (mdDecoder.GetMethodHashAttributeFlags(method).HasFlag(MethodHashAttributeFlags.IgnoreRegression))
            {
                return(new IgnoreRegressionOutcomes());
            }

            var outcomes = new Set <string>();

            foreach (Attribute attr in mdDecoder.GetAttributes(method))
            {
                var attrType = mdDecoder.AttributeType(attr);
                if (mdDecoder.Name(attrType) != "RegressionOutcomeAttribute")
                {
                    continue;
                }
                var posArgs = mdDecoder.PositionalArguments(attr);
                if (posArgs.Count == 0)
                {
                    var outcome = (ProofOutcome)(byte)GetNamedArgOrDefault <int, Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(mdDecoder, "Outcome", attr);

                    // at some places we have "this.x" instead of "x", both should be treated equal.
                    string msg           = ((string)mdDecoder.NamedArgument("Message", attr)).Replace("this.", string.Empty);
                    int    primaryOffset = GetNamedArgOrDefault <int, Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(mdDecoder, "PrimaryILOffset", attr);
                    int    methodOffset  = GetNamedArgOrDefault <int, Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(mdDecoder, "MethodILOffset", attr);

                    outcomes.Add(CanonicalFormat(outcome, msg, primaryOffset, methodOffset));
                }
                else
                {
                    outcomes.Add((string)posArgs[0]);
                }
            }
            return(new MethodRegressionOutcomes(outcomes));
        }
コード例 #5
0
        public static AssemblyRegressionOutcomes GetOutcomes <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Assembly assembly, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder)
        {
            Set <string> outcomes = new Set <string>();

            foreach (Attribute attr in mdDecoder.GetAttributes(assembly))
            {
                Type attrType = mdDecoder.AttributeType(attr);
                if (mdDecoder.Name(attrType) != "RegressionOutcomeAttribute")
                {
                    continue;
                }
                string expectedString = (string)mdDecoder.PositionalArguments(attr)[0];
                outcomes.Add(expectedString);
            }
            return(new AssemblyRegressionOutcomes(outcomes));
        }
コード例 #6
0
        /// <summary>
        /// Adds the methods of the given type to the method order as well
        /// as recursively calling AddType on nested types within the given type (if necessary).
        /// Call this with every top-level type to be analyzed.
        /// Internally called with nested types as well.
        /// </summary>
        public virtual void AddType(Type type)
        {
            foreach (var a in this.md.GetAttributes(type))
            {
                var fullname = this.md.FullName(md.AttributeType(a));

                if (fullname == "System.Diagnostics.Contracts.ContractClassForAttribute")
                {
                    return;
                }
            }

            foreach (var method in this.md.Methods(type).OrderBy(m => md.FullName(m)))
            {
                if (this.IsObjectInvariantMethod(method))
                {
                    continue; // ignore contract helper method
                }
                if (this.md.IsConstructor(method))
                {
                    continue; // added separately
                }
                this.AddMethod(method);
            }

            // add constructors so they have a chance to be analyzed first
            foreach (var method in this.md.Methods(type))
            {
                if (this.md.IsConstructor(method) && !this.md.IsMethodHashAttributeConstructor(method))
                {
                    this.AddMethod(method);
                }
            }

            foreach (var nested in this.md.NestedTypes(type))
            {
                this.AddType(nested);
            }
        }
コード例 #7
0
        public static int GetReanalyisCountIfAny <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Method method, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder)
        {
            if (mdDecoder.GetMethodHashAttributeFlags(method).HasFlag(MethodHashAttributeFlags.IgnoreRegression))
            {
                return(0);
            }

            foreach (Attribute attr in mdDecoder.GetAttributes(method))
            {
                Type attrType = mdDecoder.AttributeType(attr);
                if (mdDecoder.Name(attrType) != "RegressionReanalysisCountAttribute")
                {
                    continue;
                }
                var posArgs = mdDecoder.PositionalArguments(attr);
                if (posArgs.Count != 0)
                {
                    return((int)posArgs[0]);
                }
            }

            return(0);
        }
コード例 #8
0
            internal static bool IsMaskedInType(Type t,
                                                IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder,
                                                string maskingString)
            {
                Contract.Requires(mdDecoder != null);
                Contract.Ensures(!Contract.Result <bool>() || maskingString != null);

                if (maskingString == null)
                {
                    return(false);
                }

                foreach (var attrib in mdDecoder.GetAttributes(t))
                {
                    var attribType = mdDecoder.AttributeType(attrib);
                    if (mdDecoder.Name(attribType) == "SuppressMessageAttribute")
                    {
                        var args = mdDecoder.PositionalArguments(attrib);

                        Contract.Assume(args != null);

                        if (args.Count < 2)
                        {
                            return(false);
                        }

                        var name = (string)args[0];

                        if (name == "Microsoft.Contracts" && ((args[1] as string) == maskingString))
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
コード例 #9
0
        public static bool IsObjectInvariantMethod <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(
            this IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> metaDataDecoder,
            Method method)
        {
            Contract.Requires(metaDataDecoder != null);

            if (metaDataDecoder.Name(method) == NameFor.ObjectInvariantMethodName)
            {
                return(true);
            }

            return(metaDataDecoder.GetAttributes(method).Any(attr => metaDataDecoder.Name(metaDataDecoder.AttributeType(attr)) == NameFor.ObjectInvariantMethodAttribute));
        }