/// <summary> /// Tries the read expected exception. /// </summary> /// <param name="target"> /// The method. /// </param> /// <param name="exceptionType"> /// Type of the exception. /// </param> /// <returns> /// The <see cref="bool"/>. /// </returns> public override bool TryReadExpectedException(ICustomAttributeProviderEx target, out TypeEx exceptionType) { var attribute = AttributeHelper.GetAttribute(target, ExpectedExceptionAttribute); if (attribute != null) { var attributeType = attribute.GetType(); // read exception type using reflection. var field = attributeType.GetField("expectedException", BindingFlags.NonPublic | BindingFlags.Instance); if (field != null) { var t = field.GetValue(attribute) as Type; bool isClass; if (t != null && ReflectionHelper.TryGetIsClass(t, out isClass) && isClass && !ReflectionHelper.ContainsGenericParameters(t)) { exceptionType = MetadataFromReflection.GetType(t); return(true); } } } exceptionType = null; return(false); }
/// <summary> /// Gets the type definition of a field /// </summary> /// <param name="host"></param> /// <param name="field"></param> /// <param name="ftex"></param> /// <returns></returns> public static bool TryGetDeclaringTypeDefinition(IPexComponent host, Field field, out TypeEx ftex) { var fdefinition = field.Definition; TypeDefinition td; if (!fdefinition.TryGetDeclaringType(out td)) { host.Log.LogError(WikiTopics.MissingWikiTopic, "fieldanalyzer", "Failed to retrieve the declaring type of the field " + field.FullName); ftex = null; return(false); } try { ftex = td.Instantiate(new TypeEx[0]); } catch (ArgumentException argEx) { ftex = td.Instantiate(new TypeEx[] { MetadataFromReflection.GetType(typeof(object)) }); } catch (Exception ex) { ftex = null; Log.AppendLine("Instantiate typeEx fail: " + ex); } return(true); }
public override Term VisitSymbol(TVoid parameter, Term term, ISymbolId key) { // ISymbolIdFromParameter fromParameter = key as ISymbolIdFromParameter; // if (fromParameter != null && // fromParameter.Parameter != null) // { // Parameter p = fromParameter.Parameter; // Method m = p.DeclaringMember as Method; // if (m != null) // { // this.method = m; // this.parameters.Add(p); // } // } Log.AppendLine("In Result track condition extrator: "); Log.AppendLine("ISymbolId is " + key.Description + " type: " + key.GetType()); if (key.GetType().FullName.IndexOf("SymbolId") != -1) { foundSymbol = true; } // Log.AppendLine("parameter is " + parameter.GetType()); if (MetadataFromReflection.GetType(key.GetType()) == MetadataFromReflection.GetType(typeof(PexTrackedResultId))) { var resultId = key as PexTrackedResultId; method = resultId.CallerMethod; location = resultId.CallerLocation; callerOffset = resultId.CallerOffset; signature = resultId.MethodSignature; foundSymbol = true; } if (MetadataFromReflection.GetType(key.GetType()) == MetadataFromReflection.GetType(typeof(PexTrackedParameterId))) { var resultId = key as PexTrackedParameterId; Log.AppendLine("Parameter is " + resultId.Parameter + " description: " + resultId.Description); foundSymbol = true; } return(base.VisitSymbol(parameter, term, key)); }
/// <summary> /// Given a method definition, this function gets the generic parameters /// </summary> /// <param name="type"></param> /// <returns></returns> public static TypeEx[] GetGenericMethodParameters(IPexComponent host, MethodDefinition mdef) { TypeEx[] typErr = new TypeEx[mdef.GenericMethodParametersCount]; TypeEx inttype = MetadataFromReflection.GetType(typeof(int)); int count = 0; foreach (var genp in mdef.GenericMethodParameters) { TypeEx predefTypeEx; if (PreDefinedGenericClasses.TryGetInstantiatedClass(host, genp.Name, null, out predefTypeEx)) { typErr[count] = predefTypeEx; } else { typErr[count] = inttype; } count++; } return(typErr); }
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); }
IIndexable <TypeEx> ICommandLineDestination.GetEnvironmentSettingTypes() { return(Indexable.Array <TypeEx>( MetadataFromReflection.GetType(typeof(ExtendedReflectionEnvironmentSettings)) )); }
static Method MakePexAssertInconclusive() { return(MetadataFromReflection.GetType(typeof(PexAssert)).GetMethod("Inconclusive", SystemTypes.String)); }