Exemple #1
0
        private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
        {
            AnalyzerTreeNode newNode = null;

            try {
                if (!TypesHierarchyHelpers.IsBaseType(analyzedMethod.DeclaringType, type, resolveTypeArguments: false))
                {
                    yield break;
                }

                foreach (MethodDefinition method in type.Methods)
                {
                    if (TypesHierarchyHelpers.IsBaseMethod(analyzedMethod, method))
                    {
                        bool hidesParent = !method.IsVirtual ^ method.IsNewSlot;
                        newNode = new AnalyzedMethodTreeNode(method, hidesParent ? "(hides) " : "");
                    }
                }
            }
            catch (ReferenceResolvingException) {
                // ignore this type definition. maybe add a notification about such cases.
            }

            if (newNode != null)
            {
                newNode.Language = this.Language;
                yield return(newNode);
            }
        }
Exemple #2
0
        private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
        {
            if (!type.HasInterfaces)
            {
                yield break;
            }
            TypeReference implementedInterfaceRef = type.Interfaces.FirstOrDefault(i => i.InterfaceType.Resolve() == analyzedMethod.DeclaringType)?.InterfaceType;

            if (implementedInterfaceRef == null)
            {
                yield break;
            }

            foreach (MethodDefinition method in type.Methods.Where(m => m.Name == analyzedMethod.Name))
            {
                if (TypesHierarchyHelpers.MatchInterfaceMethod(method, analyzedMethod, implementedInterfaceRef))
                {
                    var node = new AnalyzedMethodTreeNode(method);
                    node.Language = this.Language;
                    yield return(node);
                }
                yield break;
            }

            foreach (MethodDefinition method in type.Methods)
            {
                if (method.HasOverrides && method.Overrides.Any(m => m.Resolve() == analyzedMethod))
                {
                    var node = new AnalyzedMethodTreeNode(method);
                    node.Language = this.Language;
                    yield return(node);
                }
            }
        }
Exemple #3
0
        private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
        {
            string name = analyzedMethod.Name;

            foreach (MethodDefinition method in type.Methods)
            {
                bool   found  = false;
                string prefix = string.Empty;
                if (!method.HasBody)
                {
                    continue;
                }
                foreach (Instruction instr in method.Body.Instructions)
                {
                    MethodReference mr = instr.Operand as MethodReference;
                    if (mr != null && mr.Name == name)
                    {
                        // explicit call to the requested method
                        if (instr.OpCode.Code == Code.Call &&
                            Helpers.IsReferencedBy(analyzedMethod.DeclaringType, mr.DeclaringType) &&
                            mr.Resolve() == analyzedMethod)
                        {
                            found  = true;
                            prefix = "(as base) ";
                            break;
                        }
                        // virtual call to base method
                        if (instr.OpCode.Code == Code.Callvirt)
                        {
                            MethodDefinition md = mr.Resolve();
                            if (md == null)
                            {
                                // cannot resolve the operand, so ignore this method
                                break;
                            }
                            if (md == baseMethod)
                            {
                                found = true;
                                break;
                            }
                        }
                    }
                }

                method.Body = null;

                if (found)
                {
                    MethodDefinition codeLocation = this.Language.GetOriginalCodeLocation(method) as MethodDefinition;
                    if (codeLocation != null && !HasAlreadyBeenFound(codeLocation))
                    {
                        var node = new AnalyzedMethodTreeNode(codeLocation);
                        node.Language = this.Language;
                        yield return(node);
                    }
                }
            }
        }
Exemple #4
0
        private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
        {
            if (analyzedType.IsEnum && type == analyzedType)
            {
                yield break;
            }

            if (!this.Language.ShowMember(type))
            {
                yield break;
            }

            foreach (FieldDefinition field in type.Fields)
            {
                if (TypeIsExposedBy(field))
                {
                    var node = new AnalyzedFieldTreeNode(field);
                    node.Language = this.Language;
                    yield return(node);
                }
            }

            foreach (PropertyDefinition property in type.Properties)
            {
                if (TypeIsExposedBy(property))
                {
                    var node = new AnalyzedPropertyTreeNode(property);
                    node.Language = this.Language;
                    yield return(node);
                }
            }

            foreach (EventDefinition eventDef in type.Events)
            {
                if (TypeIsExposedBy(eventDef))
                {
                    var node = new AnalyzedEventTreeNode(eventDef);
                    node.Language = this.Language;
                    yield return(node);
                }
            }

            foreach (MethodDefinition method in type.Methods)
            {
                if (TypeIsExposedBy(method))
                {
                    var node = new AnalyzedMethodTreeNode(method);
                    node.Language = this.Language;
                    yield return(node);
                }
            }
        }
Exemple #5
0
        private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
        {
            // HACK: in lieu of proper flow analysis, I'm going to use a simple heuristic
            // If the method accesses the event's backing field, and calls invoke on a delegate
            // with the same signature, then it is (most likely) raise the given event.

            foreach (MethodDefinition method in type.Methods)
            {
                bool readBackingField = false;
                bool found            = false;
                if (!method.HasBody)
                {
                    continue;
                }
                foreach (Instruction instr in method.Body.Instructions)
                {
                    Code code = instr.OpCode.Code;
                    if (code == Code.Ldfld || code == Code.Ldflda)
                    {
                        FieldReference fr = instr.Operand as FieldReference;
                        if (fr != null && fr.Name == eventBackingField.Name && fr == eventBackingField)
                        {
                            readBackingField = true;
                        }
                    }
                    if (readBackingField && (code == Code.Callvirt || code == Code.Call))
                    {
                        MethodReference mr = instr.Operand as MethodReference;
                        if (mr != null && mr.Name == eventFiringMethod.Name && mr.Resolve() == eventFiringMethod)
                        {
                            found = true;
                            break;
                        }
                    }
                }

                method.Body = null;

                if (found)
                {
                    MethodDefinition codeLocation = this.Language.GetOriginalCodeLocation(method) as MethodDefinition;
                    if (codeLocation != null && !HasAlreadyBeenFound(codeLocation))
                    {
                        var node = new AnalyzedMethodTreeNode(codeLocation);
                        node.Language = this.Language;
                        yield return(node);
                    }
                }
            }
        }
Exemple #6
0
 protected override IEnumerable <AnalyzerTreeNode> FetchChildren(CancellationToken ct)
 {
     foreach (var f in GetUsedFields().Distinct())
     {
         var node = new AnalyzedFieldTreeNode(f);
         node.Language = this.Language;
         yield return(node);
     }
     foreach (var m in GetUsedMethods().Distinct())
     {
         var node = new AnalyzedMethodTreeNode(m);
         node.Language = this.Language;
         yield return(node);
     }
 }
Exemple #7
0
 private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
 {
     if (!HasExtensionAttribute(type))
     {
         yield break;
     }
     foreach (MethodDefinition method in type.Methods)
     {
         if (method.IsStatic && HasExtensionAttribute(method))
         {
             if (method.HasParameters && method.Parameters[0].ParameterType.Resolve() == analyzedType)
             {
                 var node = new AnalyzedMethodTreeNode(method);
                 node.Language = this.Language;
                 yield return(node);
             }
         }
     }
 }
Exemple #8
0
        private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
        {
            foreach (MethodDefinition method in type.Methods)
            {
                bool found = false;
                if (!method.HasBody)
                {
                    continue;
                }

                // ignore chained constructors
                // (since object is the root of everything, we can short circuit the test in this case)
                if (method.Name == ".ctor" &&
                    (isSystemObject || analyzedType == type || TypesHierarchyHelpers.IsBaseType(analyzedType, type, false)))
                {
                    continue;
                }

                foreach (Instruction instr in method.Body.Instructions)
                {
                    MethodReference mr = instr.Operand as MethodReference;
                    if (mr != null && mr.Name == ".ctor")
                    {
                        if (Helpers.IsReferencedBy(analyzedType, mr.DeclaringType))
                        {
                            found = true;
                            break;
                        }
                    }
                }

                method.Body = null;

                if (found)
                {
                    var node = new AnalyzedMethodTreeNode(method);
                    node.Language = this.Language;
                    yield return(node);
                }
            }
        }
Exemple #9
0
        private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
        {
            string name = analyzedField.Name;

            foreach (MethodDefinition method in type.Methods)
            {
                bool found = false;
                if (!method.HasBody)
                {
                    continue;
                }
                foreach (Instruction instr in method.Body.Instructions)
                {
                    if (CanBeReference(instr.OpCode.Code))
                    {
                        FieldReference fr = instr.Operand as FieldReference;
                        if (fr != null && fr.Name == name &&
                            Helpers.IsReferencedBy(analyzedField.DeclaringType, fr.DeclaringType) &&
                            fr.Resolve() == analyzedField)
                        {
                            found = true;
                            break;
                        }
                    }
                }

                method.Body = null;

                if (found)
                {
                    MethodDefinition codeLocation = this.Language.GetOriginalCodeLocation(method) as MethodDefinition;
                    if (codeLocation != null && !HasAlreadyBeenFound(codeLocation))
                    {
                        var node = new AnalyzedMethodTreeNode(codeLocation);
                        node.Language = this.Language;
                        yield return(node);
                    }
                }
            }
        }
Exemple #10
0
        private IEnumerable <AnalyzerTreeNode> FindReferencesWithinInType(TypeDefinition type, TypeReference 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 (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 (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 (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 (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 (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 (attribute.AttributeType == attrTypeRef)
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!found &&
                        ((usage & AttributeTargets.ReturnValue) != 0) &&
                        method.MethodReturnType.HasCustomAttributes)
                    {
                        foreach (var attribute in method.MethodReturnType.CustomAttributes)
                        {
                            if (attribute.AttributeType == attrTypeRef)
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                    if (!found &&
                        ((usage & AttributeTargets.Parameter) != 0) &&
                        method.HasParameters)
                    {
                        foreach (var parameter in method.Parameters)
                        {
                            foreach (var attribute in parameter.CustomAttributes)
                            {
                                if (attribute.AttributeType == attrTypeRef)
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (found)
                    {
                        MethodDefinition codeLocation = this.Language.GetOriginalCodeLocation(method) as MethodDefinition;
                        if (codeLocation != null && !HasAlreadyBeenFound(codeLocation))
                        {
                            var node = new AnalyzedMethodTreeNode(codeLocation);
                            node.Language = this.Language;
                            yield return(node);
                        }
                    }
                }
            }
        }