Exemplo n.º 1
0
        IEnumerable <AnalyzerTreeNodeData> FindReferencesWithinInType(TypeDef type, ITypeDefOrRef attrTypeRef)
        {
            var  attrTypeRefScopeType = attrTypeRef.GetScopeType();
            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)
            {
                foreach (var attribute in type.GetCustomAttributes())
                {
                    if (new SigComparer().Equals(attribute.AttributeType?.GetScopeType(), attrTypeRefScopeType))
                    {
                        yield return(new TypeNode(type)
                        {
                            Context = Context
                        });

                        break;
                    }
                }
            }

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

                            break;
                        }
                    }
                }
            }

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

                            break;
                        }
                    }
                }
            }

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

                            break;
                        }
                    }
                }
            }
            if (((usage & AttributeTargets.Event) != 0) && type.HasEvents)
            {
                foreach (var @event in type.Events)
                {
                    foreach (var attribute in @event.GetCustomAttributes())
                    {
                        if (new SigComparer().Equals(attribute.AttributeType?.GetScopeType(), attrTypeRefScopeType))
                        {
                            yield return(new EventNode(@event)
                            {
                                Context = Context
                            });

                            break;
                        }
                    }
                }
            }

            if ((usage & (AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.ReturnValue | AttributeTargets.Parameter)) != 0 && type.HasMethods)
            {
                foreach (var method in type.Methods)
                {
                    bool found = false;
                    if ((usage & (AttributeTargets.Method | AttributeTargets.Constructor)) != 0)
                    {
                        foreach (var attribute in method.GetCustomAttributes())
                        {
                            if (new SigComparer().Equals(attribute.AttributeType?.GetScopeType(), attrTypeRefScopeType))
                            {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found &&
                        ((usage & AttributeTargets.ReturnValue) != 0) &&
                        method.Parameters.ReturnParameter.ParamDef is ParamDef retParamDef)
                    {
                        foreach (var attribute in retParamDef.GetCustomAttributes())
                        {
                            if (new SigComparer().Equals(attribute.AttributeType?.GetScopeType(), attrTypeRefScopeType))
                            {
                                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.GetCustomAttributes())
                            {
                                if (new SigComparer().Equals(attribute.AttributeType?.GetScopeType(), attrTypeRefScopeType))
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (found)
                    {
                        if (GetOriginalCodeLocation(method) is MethodDef codeLocation && !HasAlreadyBeenFound(codeLocation))
                        {
                            yield return(new MethodNode(codeLocation)
                            {
                                Context = Context
                            });
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        IEnumerable <AnalyzerTreeNodeData> FindReferencesInModule(IEnumerable <ModuleDef> modules, ITypeDefOrRef tr, CancellationToken ct)
        {
            var trScopeType = tr.GetScopeType();
            var checkedAsms = new HashSet <AssemblyDef>();

            foreach (var module in modules)
            {
                if ((usage & AttributeTargets.Assembly) != 0)
                {
                    AssemblyDef asm = module.Assembly;
                    if (!(asm is null) && checkedAsms.Add(asm))
                    {
                        foreach (var attribute in asm.GetCustomAttributes())
                        {
                            if (new SigComparer().Equals(attribute.AttributeType?.GetScopeType(), trScopeType))
                            {
                                yield return(new AssemblyNode(asm)
                                {
                                    Context = Context
                                });

                                break;
                            }
                        }
                    }
                }

                ct.ThrowIfCancellationRequested();

                if ((usage & AttributeTargets.Module) != 0)
                {
                    foreach (var attribute in module.GetCustomAttributes())
                    {
                        if (new SigComparer().Equals(attribute.AttributeType?.GetScopeType(), trScopeType))
                        {
                            yield return(new ModuleNode(module)
                            {
                                Context = Context
                            });

                            break;
                        }
                    }
                }

                ct.ThrowIfCancellationRequested();

                if ((usage & (AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.GenericParameter | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.ReturnValue | AttributeTargets.Parameter)) != 0)
                {
                    foreach (TypeDef type in TreeTraversal.PreOrder(module.Types, t => t.NestedTypes))
                    {
                        ct.ThrowIfCancellationRequested();
                        foreach (var result in FindReferencesWithinInType(type, tr))
                        {
                            ct.ThrowIfCancellationRequested();
                            yield return(result);
                        }
                    }
                }
            }
        }