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); }
static MethodDefinitionName MakeAssertNotNull() { return(MethodDefinitionName.FromTypeMethod( 0, Type_Assert.Definition, true, "NotNull", SafeArray.Empty <string>(), SystemTypes.Void.SerializableName, new ParameterDefinitionName(SystemTypes.Object.SerializableName, "condition", 0, false, ParameterDirection.ByValueOrRef) )); }
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); }
static MethodDefinitionName Translate <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Method method, IOutput output, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder) { Type declaringType = mdDecoder.DeclaringType(method); TypeDefinitionName tdefName = Translate(declaringType, output, mdDecoder); TypeName resultTdefName = TranslateToTypeName(mdDecoder.ReturnType(method), output, mdDecoder); ParameterDefinitionName[] argTdefNames = Translate(mdDecoder.Parameters(method), method, output, mdDecoder); var name = mdDecoder.Name(method); if (name == "Finalize") { name = "~" + mdDecoder.Name(declaringType).Split(new char[] { '`' }, 2)[0]; } return(MethodDefinitionName.FromTypeMethod(0, tdefName, mdDecoder.IsStatic(method), name, null, resultTdefName, argTdefNames)); }
static MethodDefinitionName MakeAssertNotEqual() { var firstGenericParameter = TypeName.MakeGenericMethodParameter(0); return(MethodDefinitionName.FromTypeMethod( 0, Type_Assert.Definition, true, "Equal", new string[] { "T" }, SystemTypes.Void.SerializableName, new ParameterDefinitionName(firstGenericParameter, "expected", 0, false, ParameterDirection.ByValueOrRef), new ParameterDefinitionName(firstGenericParameter, "actual", 1, false, ParameterDirection.ByValueOrRef) )); }
private static void SuggestPostConditionViaPex <Local, Parameter, Method, Field, Property, Event, Type, Expression, Variable, Attribute, Assembly>(IExpressionContext <Local, Parameter, Method, Field, Type, Expression, Variable> context, int rank, string symptom, IOutput output, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder, string post) where Type : IEquatable <Type> { #if INCLUDE_PEXINTEGRATION IFeedbackManager feedbackManager = GetFeedbackConnection(); if (feedbackManager != null) { MethodDefinitionName target = Translate(context.MethodContext.CurrentMethod, output, mdDecoder); string group = Guid.NewGuid().ToString(); var microsoftContractsAssembly = ShortAssemblyName.FromName("Microsoft.Contracts"); CodeUpdate update = CodeUpdate.InsertCheck("Ensures", target, MakePostconditionString(post), new[] { "System.Diagnostics.Contracts" }, new[] { microsoftContractsAssembly }, "Clousot"); CodeFix fix = CodeFix.FromUpdate("Clousot", symptom, group, update, rank, CodeFixImage.Message); try { feedbackManager.AddFix(fix); } catch { } } #endif }
static XunitTestFrameworkMetadata() { Assembly_XunitAssert = ShortAssemblyName.FromName("xunit.assert"); Assembly_XunitCore = ShortAssemblyName.FromName("xunit.core"); Type_Assert = MakeTypeName("Assert"); Type_AssertException = MakeTypeName("XunitException", Namespace_XunitSdk, Assembly_XunitAssert); Type_FactAttribute = MakeTypeName("FactAttribute"); Type_TheoryAttribute = MakeTypeName("TheoryAttribute"); Type_TraitAttribute = MakeTypeName("TraitAttribute"); Ctor_FactAttribute = MetadataBuilderHelper.Attributes.ConstructorName(Type_FactAttribute.Definition); Ctor_TraitAttribute = MetadataBuilderHelper.Attributes.ConstructorName(Type_TraitAttribute.Definition); Method_PexAssertInconclusive = new Lazy<Method>(() => MakePexAssertInconclusive()); MethodDefinition_AssertEqual = MakeAssertEqual(); MethodDefinition_AssertNotEqual = MakeAssertNotEqual(); MethodDefinition_AssertNotNull = MakeAssertNotNull(); MethodDefinition_AssertNull = MakeAssertNull(); MethodDefinition_AssertTrue = MakeAssertTrue(); Property_Skip = new PropertyDefinitionName(Assembly_XunitCore, -1, Type_FactAttribute.Definition, "Skip", SystemTypes.String.SerializableName).SelfInstantiation; }
static XunitTestFrameworkMetadata() { Assembly_XunitAssert = ShortAssemblyName.FromName("xunit.assert"); Assembly_XunitCore = ShortAssemblyName.FromName("xunit.core"); Type_Assert = MakeTypeName("Assert"); Type_AssertException = MakeTypeName("XunitException", Namespace_XunitSdk, Assembly_XunitAssert); Type_FactAttribute = MakeTypeName("FactAttribute"); Type_TheoryAttribute = MakeTypeName("TheoryAttribute"); Type_TraitAttribute = MakeTypeName("TraitAttribute"); Ctor_FactAttribute = MetadataBuilderHelper.Attributes.ConstructorName(Type_FactAttribute.Definition); Ctor_TraitAttribute = MetadataBuilderHelper.Attributes.ConstructorName(Type_TraitAttribute.Definition); Method_PexAssertInconclusive = new Lazy <Method>(() => MakePexAssertInconclusive()); MethodDefinition_AssertEqual = MakeAssertEqual(); MethodDefinition_AssertNotEqual = MakeAssertNotEqual(); MethodDefinition_AssertNotNull = MakeAssertNotNull(); MethodDefinition_AssertNull = MakeAssertNull(); MethodDefinition_AssertTrue = MakeAssertTrue(); Property_Skip = new PropertyDefinitionName(Assembly_XunitCore, -1, Type_FactAttribute.Definition, "Skip", SystemTypes.String.SerializableName).SelfInstantiation; }