예제 #1
0
 /// <summary>
 /// Method used to update statistics on how columns are accesed, by
 /// a string or integer indices.
 /// </summary>
 /// <param name="type"></param>
 private void UpdateColumnAccessesStats(ITypeReference type)
 {
     if (type.FullName() == "System.String")
     {
         ColumnStringAccesses++;
     }
     else if (type.FullName() == "System.Int32" || type.FullName() == "System.Int64")
     {
         ColumnIndexAccesses++;
     }
     else
     {
         Utils.WriteLine("WARNING: column access of type " + type.FullName());
     }
 }
예제 #2
0
        public override DifferenceType Diff(IDifferences differences, ITypeDefinition impl, ITypeDefinition contract)
        {
            if (impl == null || contract == null)
            {
                return(DifferenceType.Unknown);
            }

            if (!impl.IsEnum || !contract.IsEnum)
            {
                return(DifferenceType.Unknown);
            }

            ITypeReference implType     = impl.GetEnumType();
            ITypeReference contractType = contract.GetEnumType();

            if (!_typeComparer.Equals(implType, contractType))
            {
                differences.AddTypeMismatchDifference(this, implType, contractType,
                                                      "Enum type for '{0}' is '{1}' in implementation but '{2}' in the contract.",
                                                      impl.FullName(), implType.FullName(), contractType.FullName());
                return(DifferenceType.Changed);
            }

            return(DifferenceType.Unknown);
        }
        public override DifferenceType Diff(IDifferences differences, ITypeDefinitionMember impl, ITypeDefinitionMember contract)
        {
            if (impl == null || contract == null)
            {
                return(DifferenceType.Unknown);
            }

            if (!impl.ContainingTypeDefinition.IsEnum || !contract.ContainingTypeDefinition.IsEnum)
            {
                return(DifferenceType.Unknown);
            }

            IFieldDefinition implField     = impl as IFieldDefinition;
            IFieldDefinition contractField = contract as IFieldDefinition;

            Contract.Assert(implField != null || contractField != null);

            string implValue     = Convert.ToString(implField.Constant.Value);
            string contractValue = Convert.ToString(contractField.Constant.Value);

            // Calling the toString method to compare in since we might have the case where one Enum is type a and the other is type b, but they might still have same value.
            if (implValue != contractValue)
            {
                ITypeReference implValType     = impl.ContainingTypeDefinition.GetEnumType();
                ITypeReference contractValType = contract.ContainingTypeDefinition.GetEnumType();

                differences.AddIncompatibleDifference(this,
                                                      "Enum value '{0}' is ({1}){2} in the implementation but ({3}){4} in the contract.",
                                                      implField.FullName(), implValType.FullName(), implField.Constant.Value,
                                                      contractValType.FullName(), contractField.Constant.Value);
                return(DifferenceType.Changed);
            }

            return(DifferenceType.Unknown);
        }
        //private bool AnySecurityAttributeAdded(IDifferences differences, IReference target, IEnumerable<ISecurityAttribute> attribues1, IEnumerable<ISecurityAttribute> attributes2)
        //{
        //    return AnyAttributeAdded(differences, target, attribues1.SelectMany(a => a.Attributes), attributes2.SelectMany(a => a.Attributes));
        //}

        private void CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes)
        {
            AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings);

            attributeMapping.AddMapping(0, contractAttributes);
            attributeMapping.AddMapping(1, implAttributes);

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                case DifferenceType.Added:
                {
                    ITypeReference type       = group.Representative.Attributes.First().Type;
                    string         attribName = type.FullName();

                    if (FilterAttribute(attribName))
                    {
                        break;
                    }

                    AttributeAdded(target, attribName);

                    differences.AddIncompatibleDifference("CannotAddAttribute",
                                                          "Attribute '{0}' exists on '{1}' in the implementation but not the contract.",
                                                          attribName, target.FullName());

                    break;
                }

                case DifferenceType.Changed:

                    //TODO: Add some more logic to check the two lists of attributes which have the same type.
                    break;

                case DifferenceType.Removed:
                {
                    ITypeReference type       = group.Representative.Attributes.First().Type;
                    string         attribName = type.FullName();

                    if (s_IgnorableAttributes.Contains(attribName))
                    {
                        break;
                    }

                    differences.AddIncompatibleDifference("CannotRemoveAttribute",
                                                          "Attribute '{0}' exists on '{1}' in the contract but not the implementation.",
                                                          attribName, target.FullName());

                    break;
                }
                }
            }
        }
        private bool AnyAttributeAdded(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes)
        {
            bool added = false;

            AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings);

            attributeMapping.AddMapping(0, implAttributes);
            attributeMapping.AddMapping(1, contractAttributes);

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                case DifferenceType.Added:
                    ITypeReference type       = group.Representative.Attributes.First().Type;
                    string         attribName = type.FullName();

                    if (s_IgnorableAttributes.Contains(attribName))
                    {
                        break;
                    }

                    differences.AddIncompatibleDifference(this,
                                                          "Attribute '{0}' exists in the contract but not the implementation.",
                                                          attribName, target.FullName());

                    added = true;

                    break;

                case DifferenceType.Changed:

                    //TODO: Add some more logic to check the two lists of attributes which have the same type.
                    break;

                case DifferenceType.Removed:
                    // Removing attributes is OK
                    break;
                }
            }

            return(added);
        }
예제 #6
0
 public static bool AreEquivalent(this ITypeReference type, string typeName)
 {
     return(type.FullName() == typeName);
 }
예제 #7
0
        private DifferenceType CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes)
        {
            DifferenceType difference = DifferenceType.Unchanged;
            AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings);
            AttributeComparer attributeComparer = new AttributeComparer();

            attributeMapping.AddMapping(0, contractAttributes.OrderBy(a => a, attributeComparer));
            attributeMapping.AddMapping(1, implAttributes.OrderBy(a => a, attributeComparer));

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                case DifferenceType.Added:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    // Allow for additions
                    differences.Add(new Difference("AddedAttribute",
                                                   $"Attribute '{type.FullName()}' exists on '{target.FullName()}' in the {Implementation} but not the {Contract}."));

                    break;
                }

                case DifferenceType.Changed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    string contractKey       = attributeComparer.GetKey(group[0].Attributes.First());
                    string implementationKey = attributeComparer.GetKey(group[1].Attributes.First());

                    differences.AddIncompatibleDifference("CannotChangeAttribute",
                                                          $"Attribute '{type.FullName()}' on '{target.FullName()}' changed from '{contractKey}' in the {Contract} to '{implementationKey}' in the {Implementation}.");

                    difference = DifferenceType.Changed;
                    break;
                }

                case DifferenceType.Removed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    differences.AddIncompatibleDifference("CannotRemoveAttribute",
                                                          $"Attribute '{type.FullName()}' exists on '{target.FullName()}' in the {Contract} but not the {Implementation}.");


                    // removals of an attribute are considered a "change" of the type
                    difference = DifferenceType.Changed;
                    break;
                }
                }
            }
            return(difference);
        }
예제 #8
0
        private bool CheckAttributeDifferences(IDifferences differences, IReference target, IEnumerable <ICustomAttribute> implAttributes, IEnumerable <ICustomAttribute> contractAttributes, IReference member = null)
        {
            AttributesMapping <IEnumerable <ICustomAttribute> > attributeMapping = new AttributesMapping <IEnumerable <ICustomAttribute> >(_settings);
            AttributeComparer attributeComparer = new AttributeComparer();

            attributeMapping.AddMapping(0, contractAttributes.OrderBy(a => a, attributeComparer));
            attributeMapping.AddMapping(1, implAttributes.OrderBy(a => a, attributeComparer));

            string errString = $"'{target.FullName()}'";

            if (target is IParameterDefinition || target is IGenericParameter)
            {
                errString  = target is IGenericParameter ? "generic param" : "parameter";
                errString += $" '{target.FullName()}' on member '{member?.FullName()}'";
            }

            bool changed = false;

            foreach (var group in attributeMapping.Attributes)
            {
                switch (group.Difference)
                {
                case DifferenceType.Added:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    // Allow for additions
                    differences.Add(new Difference("AddedAttribute",
                                                   $"Attribute '{type.FullName()}' exists on {errString} in the {Implementation} but not the {Contract}."));

                    changed = true;
                    break;
                }

                case DifferenceType.Changed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    string contractKey       = attributeComparer.GetKey(group[0].Attributes.First());
                    string implementationKey = attributeComparer.GetKey(group[1].Attributes.First());

                    differences.AddIncompatibleDifference("CannotChangeAttribute",
                                                          $"Attribute '{type.FullName()}' on {errString} changed from '{contractKey}' in the {Contract} to '{implementationKey}' in the {Implementation}.");

                    changed = true;
                    break;
                }

                case DifferenceType.Removed:
                {
                    ITypeReference type = group.Representative.Attributes.First().Type;

                    if (AttributeFilter.ShouldExclude(type.DocId()))
                    {
                        break;
                    }

                    differences.AddIncompatibleDifference("CannotRemoveAttribute",
                                                          $"Attribute '{type.FullName()}' exists on {errString} in the {Contract} but not the {Implementation}.");


                    // removals of an attribute are considered a "change" of the type
                    changed = true;
                    break;
                }
                }
            }
            return(changed);
        }
예제 #9
0
        private bool ReturnTypesMatch(IDifferences differences, IMethodDefinition implMethod, IMethodDefinition contractMethod)
        {
            ITypeReference implReturnType     = implMethod.GetReturnType();
            ITypeReference contractReturnType = contractMethod.GetReturnType();

            if (implReturnType == null || contractReturnType == null)
            {
                return(true);
            }

            if (!_typeComparer.Equals(implReturnType, contractReturnType))
            {
                differences.AddTypeMismatchDifference("DelegateReturnTypesMustMatch", implReturnType, contractReturnType,
                                                      $"Return type on delegate '{implMethod.ContainingType.FullName()}' is '{implReturnType.FullName()}' in the {Implementation} but '{contractReturnType.FullName()}' in the {Contract}.");
                return(false);
            }

            return(true);
        }
예제 #10
0
 public override string ToString()
 {
     return(type.FullName());
 }