internal override bool IsCoherent()
            {
                var isCoherent = true;

                if (_attributeTargets.HasFlag(AttributeTargets.Assembly) &&
                    FoundOnAssemblyAttributes == null)
                {
                    R2API.Logger.LogError(
                        $"[ScanRequest] Given attribute {SearchedTypeFullName} " +
                        "will be checked against assemblies attributes but no corresponding callback was given");
                    isCoherent = false;
                }
                if (_attributeTargets.HasFlag(AttributeTargets.Class) &&
                    FoundOnAssemblyTypes == null)
                {
                    R2API.Logger.LogError(
                        $"[ScanRequest] Given attribute {SearchedTypeFullName} " +
                        "will be checked against assemblies class types but no corresponding callback was given");
                    isCoherent = false;
                }

                return(isCoherent);
            }
        public IEnumerable <ICustomAttributeUsage> QueryCustomAttributeUsages(AssemblyDefinition targetAssemly, AttributeTargets filter)
        {
            IEnumerable <ICustomAttributeUsage> attributeQueryResult = new List <ICustomAttributeUsage>();

            var hasAllFlags = filter.HasFlag(AttributeTargets.All);

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Assembly))
            {
                attributeQueryResult = attributeQueryResult.Concat(targetAssemly.CustomAttributes.Select(a => new CustomAttributeUsage <AssemblyDefinition> {
                    Attribute = a, DeclaringAttributeProvider = targetAssemly
                }));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Module))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryModules(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Class))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryClasses(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Struct))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryStructs(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Enum))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryEnums(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Constructor))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryConstructors(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Method))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryMethods(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Property))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryProperties(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Field))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryFields(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Event))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryEvents(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Interface))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryInterfaces(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Parameter))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryParameters(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.Delegate))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryDelegateTypes(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.ReturnValue))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryReturnTypes(targetAssemly)));
            }

            if (hasAllFlags || filter.HasFlag(AttributeTargets.GenericParameter))
            {
                attributeQueryResult = attributeQueryResult.Concat(GetAttributeUsages(QueryGenericParameters(targetAssemly)));
            }

            return(attributeQueryResult);
        }
Exemplo n.º 3
0
        private IEnumerable <AnalyzerTreeNode> FindReferencesWithinInType(TypeDef type, ITypeDefOrRef attrTypeRef)
        {
            bool searchRequired = (type.IsClass && usage.HasFlag(AttributeTargets.Class)) ||
                                  (type.IsEnum && usage.HasFlag(AttributeTargets.Enum)) ||
                                  (type.IsInterface && usage.HasFlag(AttributeTargets.Interface)) ||
                                  (Decompiler.DnlibExtensions.IsValueType(type) && usage.HasFlag(AttributeTargets.Struct));

            if (searchRequired)
            {
                if (type.HasCustomAttributes)
                {
                    foreach (var attribute in type.CustomAttributes)
                    {
                        if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                        {
                            var node = new AnalyzedTypeTreeNode(type);
                            node.Language = this.Language;
                            yield return(node);

                            break;
                        }
                    }
                }
            }

            if ((this.usage & AttributeTargets.GenericParameter) != 0 && type.HasGenericParameters)
            {
                foreach (var parameter in type.GenericParameters)
                {
                    if (parameter.HasCustomAttributes)
                    {
                        foreach (var attribute in parameter.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                var node = new AnalyzedTypeTreeNode(type);
                                node.Language = this.Language;
                                yield return(node);

                                break;
                            }
                        }
                    }
                }
            }

            if ((this.usage & AttributeTargets.Field) != 0 && type.HasFields)
            {
                foreach (var field in type.Fields)
                {
                    if (field.HasCustomAttributes)
                    {
                        foreach (var attribute in field.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                var node = new AnalyzedFieldTreeNode(field);
                                node.Language = this.Language;
                                yield return(node);

                                break;
                            }
                        }
                    }
                }
            }

            if (((usage & AttributeTargets.Property) != 0) && type.HasProperties)
            {
                foreach (var property in type.Properties)
                {
                    if (property.HasCustomAttributes)
                    {
                        foreach (var attribute in property.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                var node = new AnalyzedPropertyTreeNode(property);
                                node.Language = this.Language;
                                yield return(node);

                                break;
                            }
                        }
                    }
                }
            }
            if (((usage & AttributeTargets.Event) != 0) && type.HasEvents)
            {
                foreach (var _event in type.Events)
                {
                    if (_event.HasCustomAttributes)
                    {
                        foreach (var attribute in _event.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                var node = new AnalyzedEventTreeNode(_event);
                                node.Language = this.Language;
                                yield return(node);

                                break;
                            }
                        }
                    }
                }
            }

            if (type.HasMethods)
            {
                foreach (var method in type.Methods)
                {
                    bool found = false;
                    if ((usage & (AttributeTargets.Method | AttributeTargets.Constructor)) != 0)
                    {
                        if (method.HasCustomAttributes)
                        {
                            foreach (var attribute in method.CustomAttributes)
                            {
                                if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!found &&
                        ((usage & AttributeTargets.ReturnValue) != 0) &&
                        method.Parameters.ReturnParameter.HasParamDef &&
                        method.Parameters.ReturnParameter.ParamDef.HasCustomAttributes)
                    {
                        foreach (var attribute in method.Parameters.ReturnParameter.ParamDef.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                    if (!found &&
                        ((usage & AttributeTargets.Parameter) != 0) &&
                        method.Parameters.Count > 0)
                    {
                        foreach (var parameter in method.Parameters.Where(param => param.HasParamDef))
                        {
                            if (parameter.IsHiddenThisParameter)
                            {
                                continue;
                            }
                            foreach (var attribute in parameter.ParamDef.CustomAttributes)
                            {
                                if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (found)
                    {
                        MethodDef codeLocation = this.Language.GetOriginalCodeLocation(method) as MethodDef;
                        if (codeLocation != null && !HasAlreadyBeenFound(codeLocation))
                        {
                            var node = new AnalyzedMethodTreeNode(codeLocation);
                            node.Language = this.Language;
                            yield return(node);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        IEnumerable <AnalyzerTreeNodeData> FindReferencesWithinInType(TypeDef type, ITypeDefOrRef attrTypeRef)
        {
            bool searchRequired = (type.IsClass && usage.HasFlag(AttributeTargets.Class)) ||
                                  (type.IsEnum && usage.HasFlag(AttributeTargets.Enum)) ||
                                  (type.IsInterface && usage.HasFlag(AttributeTargets.Interface)) ||
                                  (type.IsValueType && usage.HasFlag(AttributeTargets.Struct));

            if (searchRequired)
            {
                if (type.HasCustomAttributes)
                {
                    foreach (var attribute in type.CustomAttributes)
                    {
                        if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                        {
                            yield return(new TypeNode(type)
                            {
                                Context = Context
                            });

                            break;
                        }
                    }
                }
            }

            if ((usage & AttributeTargets.GenericParameter) != 0 && type.HasGenericParameters)
            {
                foreach (var parameter in type.GenericParameters)
                {
                    if (parameter.HasCustomAttributes)
                    {
                        foreach (var attribute in parameter.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                yield return(new TypeNode(type)
                                {
                                    Context = Context
                                });

                                break;
                            }
                        }
                    }
                }
            }

            if ((usage & AttributeTargets.Field) != 0 && type.HasFields)
            {
                foreach (var field in type.Fields)
                {
                    if (field.HasCustomAttributes)
                    {
                        foreach (var attribute in field.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                yield return(new FieldNode(field)
                                {
                                    Context = Context
                                });

                                break;
                            }
                        }
                    }
                }
            }

            if (((usage & AttributeTargets.Property) != 0) && type.HasProperties)
            {
                foreach (var property in type.Properties)
                {
                    if (property.HasCustomAttributes)
                    {
                        foreach (var attribute in property.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                yield return(new PropertyNode(property)
                                {
                                    Context = Context
                                });

                                break;
                            }
                        }
                    }
                }
            }
            if (((usage & AttributeTargets.Event) != 0) && type.HasEvents)
            {
                foreach (var _event in type.Events)
                {
                    if (_event.HasCustomAttributes)
                    {
                        foreach (var attribute in _event.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                yield return(new EventNode(_event)
                                {
                                    Context = Context
                                });

                                break;
                            }
                        }
                    }
                }
            }

            if (type.HasMethods)
            {
                foreach (var method in type.Methods)
                {
                    bool found = false;
                    if ((usage & (AttributeTargets.Method | AttributeTargets.Constructor)) != 0)
                    {
                        if (method.HasCustomAttributes)
                        {
                            foreach (var attribute in method.CustomAttributes)
                            {
                                if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!found &&
                        ((usage & AttributeTargets.ReturnValue) != 0) &&
                        method.Parameters.ReturnParameter.HasParamDef &&
                        method.Parameters.ReturnParameter.ParamDef.HasCustomAttributes)
                    {
                        foreach (var attribute in method.Parameters.ReturnParameter.ParamDef.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                    if (!found &&
                        ((usage & AttributeTargets.Parameter) != 0) &&
                        method.Parameters.Count > 0)
                    {
                        foreach (var parameter in method.Parameters.Where(param => param.HasParamDef))
                        {
                            if (parameter.IsHiddenThisParameter)
                            {
                                continue;
                            }
                            foreach (var attribute in parameter.ParamDef.CustomAttributes)
                            {
                                if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (found)
                    {
                        if (GetOriginalCodeLocation(method) is MethodDef codeLocation && !HasAlreadyBeenFound(codeLocation))
                        {
                            yield return(new MethodNode(codeLocation)
                            {
                                Context = Context
                            });
                        }
                    }
                }
            }
        }
        public static void Attribute_is_ignored(string attribute, AttributeTargets targets)
        {
            if (targets.HasFlag(AttributeTargets.Class))
            {
                Assert.That(attribute + " public static class Class { }", HasContract(
                                "public static class Class",
                                "{",
                                "}"));
            }

            if (targets.HasFlag(AttributeTargets.Constructor))
            {
                Assert.That("public class Class { " + attribute + " public Class() { } }", HasContract(
                                "public class Class",
                                "{",
                                "    public Class();",
                                "}"));
            }

            if (targets.HasFlag(AttributeTargets.Delegate))
            {
                Assert.That(attribute + " public delegate void Delegate();", HasContract(
                                "public delegate void Delegate();"));
            }

            if (targets.HasFlag(AttributeTargets.Enum))
            {
                Assert.That(attribute + " public enum Enum { }", HasContract(
                                "public enum Enum : int",
                                "{",
                                "}"));
            }

            if (targets.HasFlag(AttributeTargets.Event))
            {
                Assert.That("public static class Class { " + attribute + " public static event System.EventHandler Event; }", HasContract(
                                "public static class Class",
                                "{",
                                "    public static event System.EventHandler Event;",
                                "}"));
            }

            if (targets.HasFlag(AttributeTargets.Field))
            {
                Assert.That("public static class Class { " + attribute + " public static int Field; }", HasContract(
                                "public static class Class",
                                "{",
                                "    public static int Field;",
                                "}"));
            }

            if (targets.HasFlag(AttributeTargets.GenericParameter))
            {
                Assert.That("public static class Class<" + attribute + " T> { }", HasContract(
                                "public static class Class<T>",
                                "{",
                                "}"));
            }

            if (targets.HasFlag(AttributeTargets.Interface))
            {
                Assert.That(attribute + " public interface IInterface { }", HasContract(
                                "public interface IInterface",
                                "{",
                                "}"));
            }

            if (targets.HasFlag(AttributeTargets.Method))
            {
                Assert.That("public static class Class { " + attribute + " public static void Method() { } }", HasContract(
                                "public static class Class",
                                "{",
                                "    public static void Method();",
                                "}"));
            }

            if (targets.HasFlag(AttributeTargets.Module))
            {
                Assert.That("[module: " + attribute.Slice(1), HasContract());
            }

            if (targets.HasFlag(AttributeTargets.Parameter))
            {
                Assert.That("public delegate void Delegate(" + attribute + " int parameter);", HasContract(
                                "public delegate void Delegate(int parameter);"));
            }

            if (targets.HasFlag(AttributeTargets.Property))
            {
                Assert.That("public static class Class { " + attribute + " public static int Property { get; } }", HasContract(
                                "public static class Class",
                                "{",
                                "    public static int Property { get; }",
                                "}"));
            }

            if (targets.HasFlag(AttributeTargets.ReturnValue))
            {
                Assert.That("[return: " + attribute.Slice(1) + " public delegate int Delegate();", HasContract(
                                "public delegate int Delegate();"));
            }

            if (targets.HasFlag(AttributeTargets.Struct))
            {
                Assert.That(attribute + " public struct Struct { }", HasContract(
                                "public struct Struct",
                                "{",
                                "}"));
            }
        }