private static bool TryAddRegressionAttributeViaPex <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Method method, string message, IOutput output, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder, System.Type attributeType)
        {
            IFeedbackManager feedbackManager = GetFeedbackConnection();
            bool             success         = false;

            if (feedbackManager != null)
            {
                ConstructorInfo attrCtor = attributeType.GetConstructor(new System.Type[1] {
                    typeof(string)
                });
                MethodDefinitionName target = Translate(method, output, mdDecoder);

                string           group = Guid.NewGuid().ToString();
                ICustomAttribute ca    = new CustomAttributeBuilder(MetadataFromReflection.GetMethod(attrCtor), MetadataExpression.String(message));

                CodeUpdate update = CodeUpdate.AddAttribute("Regression", target, ca);

                CodeFix fix = CodeFix.FromUpdate("ClousotRegression", "missing regression attribute", group, update, 100, CodeFixImage.Message);
                try
                {
                    feedbackManager.AddFix(fix);
                    success = true;
                }
                catch { }
            }
            return(success);
        }
        private static bool TryAddRegressionAttributeViaPex <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Method method, Enum outcome, string message, int primaryILOffset, int methodILOffset, IOutput output, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder, System.Type attributeType)
        {
            IFeedbackManager feedbackManager = GetFeedbackConnection();
            bool             success         = false;

            if (feedbackManager != null)
            {
                PropertyInfo    outcomeProp   = attributeType.GetProperty("Outcome");
                PropertyInfo    messageProp   = attributeType.GetProperty("Message");
                PropertyInfo    primaryILProp = attributeType.GetProperty("PrimaryILOffset");
                PropertyInfo    methodILProp  = attributeType.GetProperty("MethodILOffset");
                ConstructorInfo attrCtor      = attributeType.GetConstructor(new System.Type[0]);

                MethodDefinitionName target = Translate(method, output, mdDecoder);

                MethodName attributeCtorName = MetadataFromReflection.GetMethod(attrCtor).SerializableName;

#if DEBUG_PEX_BY_XML
                SafeSimpleXmlWriter writer = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + ".xml"), true);
                target.WriteXml(writer, "method");
                writer.Close();

                SafeSimpleXmlWriter writer2 = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + "2.xml"), true);
                attributeCtorName2.WriteXml(writer2, "method");
                writer2.Close();
#endif
                string group = Guid.NewGuid().ToString();

                var outcomeArg = AttributeArgument.Named(MetadataFromReflection.GetProperty(outcomeProp), MetadataExpression.EnumValue(outcome));
#if DEBUG_PEX_BY_XML
                SafeSimpleXmlWriter writer3 = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + "3.xml"), true);
                outcomeArg.WriteXml(writer3, "method");
                writer3.Close();
#endif
                CodeUpdate update =
                    CodeUpdate.AddAttribute("Regression", target, attributeCtorName,
                                            outcomeArg,
                                            AttributeArgument.Named(MetadataFromReflection.GetProperty(messageProp), MetadataExpression.String(message)),
                                            AttributeArgument.Named(MetadataFromReflection.GetProperty(primaryILProp), MetadataExpression.I4(MetadataFromReflection.GetType(typeof(int)), primaryILOffset)),
                                            AttributeArgument.Named(MetadataFromReflection.GetProperty(methodILProp), MetadataExpression.I4(MetadataFromReflection.GetType(typeof(int)), methodILOffset))
                                            );

                CodeFix fix = CodeFix.FromUpdate("ClousotRegression", "missing regression attribute", group, update, 100, CodeFixImage.Message);
                try
                {
                    feedbackManager.AddFix(fix);
                    success = true;
                }
                catch { }
            }
            return(success);
        }
Example #3
0
        public override string GetFullyQualifiedTopProcedure()
        {
            StackTrace st = new StackTrace(false);

            for (int i = 0; i < st.FrameCount; i++)
            {
                System.Diagnostics.StackFrame sf = st.GetFrame(i);
                string assembly_name             = sf.GetMethod().Module.Assembly.GetName().ToString();

                // this is fragile
                if (assembly_name.Contains(".ManagedChess") || assembly_name.Contains(".ExtendedReflection"))
                {
                    continue;
                }

                var erMethod = MetadataFromReflection.GetMethod(sf.GetMethod());

                return(erMethod.FullName);
            }
            return("");
        }
        private static bool TryAddRegressionAttributeViaPex <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Assembly assembly, string message, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder, System.Type attributeType)
        {
            IFeedbackManager feedbackManager = GetFeedbackConnection();
            bool             success         = true;

            if (feedbackManager != null)
            {
                ConstructorInfo attrCtor = attributeType.GetConstructor(new System.Type[1] {
                    typeof(string)
                });

#if DEBUG_PEX_BY_XML
                SafeSimpleXmlWriter writer = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + ".xml"), true);
                target.WriteXml(writer, "method");
                writer.Close();

                SafeSimpleXmlWriter writer2 = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + "2.xml"), true);
                attributeCtorName2.WriteXml(writer2, "method");
                writer2.Close();
#endif
                string           group = Guid.NewGuid().ToString();
                ICustomAttribute ca    = new CustomAttributeBuilder(MetadataFromReflection.GetMethod(attrCtor), MetadataExpression.String(message));

                CodeUpdate update =
                    CodeUpdate.AddAttribute("Regression",
                                            new Microsoft.ExtendedReflection.Metadata.Names.ShortAssemblyName(mdDecoder.Name(assembly), null),
                                            ca);

                CodeFix fix = CodeFix.FromUpdate("ClousotRegression", "missing regression attribute", group, update, 100, CodeFixImage.Message);
                try
                {
                    feedbackManager.AddFix(fix);
                    success = true;
                }
                catch { }
            }
            return(success);
        }