private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDef type) { foreach (MethodDef method in type.Methods) { bool found = false; if (!method.HasBody) continue; foreach (Instruction instr in method.Body.Instructions) { if (CanBeReference(instr.OpCode.Code)) { IField fr = instr.Operand as IField; if (fr != null && new SigComparer(SigComparerOptions.CompareDeclaringTypes | SigComparerOptions.PrivateScopeIsComparable).Equals(fr, analyzedField) && Helpers.IsReferencedBy(analyzedField.DeclaringType, fr.DeclaringType)) { found = true; break; } } } Helpers.FreeMethodBody(method); 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; } } } }
private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDefinition type) { string name = analyzedMethod.Name; foreach (MethodDefinition method in type.Methods) { bool found = false; if (!method.HasBody) continue; foreach (Instruction instr in method.Body.Instructions) { MethodReference mr = instr.Operand as MethodReference; if (mr != null && mr.Name == name && Helpers.IsReferencedBy(analyzedMethod.DeclaringType, mr.DeclaringType) && mr.Resolve() == analyzedMethod) { 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; } } } }
private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDef type) { if (!type.HasInterfaces) yield break; var iff = type.Interfaces.FirstOrDefault(i => new SigComparer().Equals(i.Interface, analyzedMethod.DeclaringType)); ITypeDefOrRef implementedInterfaceRef = iff == null ? null : iff.Interface; if (implementedInterfaceRef == null) yield break; //TODO: Can we compare method sigs too? foreach (MethodDef 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 (MethodDef method in type.Methods) { if (method.HasOverrides && method.Overrides.Any(m => new SigComparer(SigComparerOptions.CompareDeclaringTypes | SigComparerOptions.PrivateScopeIsComparable).Equals(m.MethodDeclaration, analyzedMethod))) { var node = new AnalyzedMethodTreeNode(method); node.Language = this.Language; yield return node; } } }
private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDefinition type) { if (!type.HasInterfaces) { yield break; } TypeReference implementedInterfaceRef = type.Interfaces.FirstOrDefault(i => i.Resolve() == analyzedMethod.DeclaringType); 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); } } }
private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDefinition type) { if (!type.HasInterfaces) yield break; TypeReference implementedInterfaceRef = type.Interfaces.FirstOrDefault(i => i.Resolve() == analyzedMethod.DeclaringType); 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; } } }
private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDef type) { AnalyzerTreeNode newNode = null; try { if (!TypesHierarchyHelpers.IsBaseType(analyzedMethod.DeclaringType, type, resolveTypeArguments: false)) { yield break; } foreach (MethodDef method in type.Methods) { if (TypesHierarchyHelpers.IsBaseMethod(analyzedMethod, method)) { bool hidesParent = !method.IsVirtual ^ method.IsNewSlot; newNode = new AnalyzedMethodTreeNode(method, hidesParent); } } } catch (ResolveException) { // ignore this type definition. maybe add a notification about such cases. } if (newNode != null) { newNode.Language = this.Language; yield return(newNode); } }
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; } } }
private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDef type) { string name = analyzedMethod.Name; foreach (MethodDef method in type.Methods) { bool found = false; string prefix = string.Empty; if (!method.HasBody) { continue; } foreach (Instruction instr in method.Body.Instructions) { IMethod mr = instr.Operand as IMethod; if (mr != null && !mr.IsField && mr.Name == name) { // explicit call to the requested method if (instr.OpCode.Code == Code.Call && Helpers.IsReferencedBy(analyzedMethod.DeclaringType, mr.DeclaringType) && Decompiler.DnlibExtensions.Resolve(mr) == analyzedMethod) { found = true; prefix = "(as base) "; break; } // virtual call to base method if (instr.OpCode.Code == Code.Callvirt) { MethodDef md = Decompiler.DnlibExtensions.Resolve(mr); if (md == null) { // cannot resolve the operand, so ignore this method break; } if (md == baseMethod) { found = true; break; } } } } Helpers.FreeMethodBody(method); 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); } } } }
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; } }
private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDef type) { if (analyzedType.IsEnum && type == analyzedType) { yield break; } if (!this.Language.ShowMember(type)) { yield break; } foreach (FieldDef field in type.Fields) { if (TypeIsExposedBy(field)) { var node = new AnalyzedFieldTreeNode(field); node.Language = this.Language; yield return(node); } } foreach (PropertyDef property in type.Properties) { if (TypeIsExposedBy(property)) { var node = new AnalyzedPropertyTreeNode(property); node.Language = this.Language; yield return(node); } } foreach (EventDef eventDef in type.Events) { if (TypeIsExposedBy(eventDef)) { var node = new AnalyzedEventTreeNode(eventDef); node.Language = this.Language; yield return(node); } } foreach (MethodDef method in type.Methods) { if (TypeIsExposedBy(method)) { var node = new AnalyzedMethodTreeNode(method); node.Language = this.Language; yield return(node); } } }
private IEnumerable<AnalyzerTreeNode> GetChildren() { 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; } }
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); } } } }
private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDef type) { if (!HasExtensionAttribute(type)) yield break; foreach (MethodDef method in type.Methods) { if (method.IsStatic && HasExtensionAttribute(method)) { int skip = Decompiler.DnlibExtensions.GetParametersSkip(method.Parameters); if (method.Parameters.Count > skip && new SigComparer().Equals(analyzedType, method.Parameters[skip].Type)) { var node = new AnalyzedMethodTreeNode(method); node.Language = this.Language; yield return node; } } } }
private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDef 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 (MethodDef 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) { IField fr = instr.Operand as IField; if (new SigComparer(SigComparerOptions.CompareDeclaringTypes | SigComparerOptions.PrivateScopeIsComparable).Equals(fr, eventBackingField)) { readBackingField = true; } } if (readBackingField && (code == Code.Callvirt || code == Code.Call)) { IMethod mr = instr.Operand as IMethod; if (mr != null && eventFiringMethod != null && mr.Name == eventFiringMethod.Name && Decompiler.DnlibExtensions.Resolve(mr) == eventFiringMethod) { found = true; break; } } } Helpers.FreeMethodBody(method); 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); } } } }
private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDef type) { if (!HasExtensionAttribute(type)) yield break; foreach (MethodDef method in type.Methods) { if (method.IsStatic && HasExtensionAttribute(method)) { if (method.Parameters.Count > 0 && Decompiler.DnlibExtensions.Resolve(method.Parameters[0].Type) == analyzedType) { var node = new AnalyzedMethodTreeNode(method); node.Language = this.Language; yield return node; } } } }
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; } } } }
private IEnumerable <AnalyzerTreeNode> GetChildren() { 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); } }
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); } }
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); } } } }
private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDef type) { if (!HasExtensionAttribute(type)) { yield break; } foreach (MethodDef method in type.Methods) { if (method.IsStatic && HasExtensionAttribute(method)) { if (method.Parameters.Count > 0 && Decompiler.DnlibExtensions.Resolve(method.Parameters[0].Type) == analyzedType) { var node = new AnalyzedMethodTreeNode(method); node.Language = this.Language; yield return(node); } } } }
private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDef type) { if (!HasExtensionAttribute(type)) { yield break; } foreach (MethodDef method in type.Methods) { if (method.IsStatic && HasExtensionAttribute(method)) { int skip = Decompiler.DnlibExtensions.GetParametersSkip(method.Parameters); if (method.Parameters.Count > skip && new SigComparer().Equals(analyzedType, method.Parameters[skip].Type)) { var node = new AnalyzedMethodTreeNode(method); node.Language = this.Language; yield return(node); } } } }
private IEnumerable <SharpTreeNode> FindReferencesInType(TypeDefinition type) { string name = analyzedField.Name; string declTypeName = analyzedField.DeclaringType.FullName; 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); } } } }
private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDef type) { foreach (MethodDef 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) { IMethod mr = instr.Operand as IMethod; if (mr != null && !mr.IsField && mr.Name == ".ctor") { if (Helpers.IsReferencedBy(analyzedType, mr.DeclaringType)) { found = true; break; } } } Helpers.FreeMethodBody(method); if (found) { var node = new AnalyzedMethodTreeNode(method); node.Language = this.Language; yield return(node); } } }
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; } } }
private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDef type) { AnalyzerTreeNode newNode = null; try { if (!TypesHierarchyHelpers.IsBaseType(analyzedMethod.DeclaringType, type, resolveTypeArguments: false)) yield break; foreach (MethodDef method in type.Methods) { if (TypesHierarchyHelpers.IsBaseMethod(analyzedMethod, method)) { bool hidesParent = !method.IsVirtual ^ method.IsNewSlot; newNode = new AnalyzedMethodTreeNode(method, hidesParent); } } } catch (ResolveException) { // ignore this type definition. maybe add a notification about such cases. } if (newNode != null) { newNode.Language = this.Language; yield return newNode; } }
private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDef type) { foreach (MethodDef method in type.Methods) { bool found = false; if (!method.HasBody) { continue; } foreach (Instruction instr in method.Body.Instructions) { if (CanBeReference(instr.OpCode.Code)) { IField fr = instr.Operand as IField; if (fr != null && new SigComparer(SigComparerOptions.CompareDeclaringTypes | SigComparerOptions.PrivateScopeIsComparable).Equals(fr, analyzedField) && Helpers.IsReferencedBy(analyzedField.DeclaringType, fr.DeclaringType)) { found = true; break; } } } Helpers.FreeMethodBody(method); 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); } } } }
private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDef type) { string name = analyzedMethod.Name; foreach (MethodDef method in type.Methods) { bool found = false; if (!method.HasBody) { continue; } foreach (Instruction instr in method.Body.Instructions) { IMethod mr = instr.Operand as IMethod; if (mr != null && !mr.IsField && mr.Name == name && Helpers.IsReferencedBy(analyzedMethod.DeclaringType, mr.DeclaringType) && Decompiler.DnlibExtensions.Resolve(mr) == analyzedMethod) { found = true; break; } } Helpers.FreeMethodBody(method); 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); } } } }
private IEnumerable <SharpTreeNode> FindReferences(LoadedAssembly asm, CancellationToken ct) { string asmName = asm.AssemblyDefinition.Name.Name; string name = analyzedMethod.Name; string declTypeName = analyzedMethod.DeclaringType.FullName; foreach (TypeDefinition type in TreeTraversal.PreOrder(asm.AssemblyDefinition.MainModule.Types, t => t.NestedTypes)) { ct.ThrowIfCancellationRequested(); SharpTreeNode newNode = null; try { if (!TypesHierarchyHelpers.IsBaseType(analyzedMethod.DeclaringType, type, resolveTypeArguments: false)) { continue; } foreach (MethodDefinition method in type.Methods) { ct.ThrowIfCancellationRequested(); 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) { yield return(newNode); } } }
private IEnumerable <AnalyzerTreeNode> FindReferencesInType(TypeDef type) { if (!type.HasInterfaces) { yield break; } var iff = type.Interfaces.FirstOrDefault(i => new SigComparer().Equals(i.Interface, analyzedMethod.DeclaringType)); ITypeDefOrRef implementedInterfaceRef = iff == null ? null : iff.Interface; if (implementedInterfaceRef == null) { yield break; } //TODO: Can we compare method sigs too? foreach (MethodDef 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 (MethodDef method in type.Methods) { if (method.HasOverrides && method.Overrides.Any(m => new SigComparer(SigComparerOptions.CompareDeclaringTypes | SigComparerOptions.PrivateScopeIsComparable).Equals(m.MethodDeclaration, analyzedMethod))) { var node = new AnalyzedMethodTreeNode(method); node.Language = this.Language; yield return(node); } } }
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); } } } } }
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; } } } }
private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDef 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 (MethodDef 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) { IField fr = instr.Operand as IField; if (new SigComparer(SigComparerOptions.CompareDeclaringTypes | SigComparerOptions.PrivateScopeIsComparable).Equals(fr, eventBackingField)) { readBackingField = true; } } if (readBackingField && (code == Code.Callvirt || code == Code.Call)) { IMethod mr = instr.Operand as IMethod; if (mr != null && eventFiringMethod != null && mr.Name == eventFiringMethod.Name && Decompiler.DnlibExtensions.Resolve(mr) == eventFiringMethod) { found = true; break; } } } Helpers.FreeMethodBody(method); 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; } } } }
private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDef type) { string name = analyzedField.Name; foreach (MethodDef method in type.Methods) { bool found = false; if (!method.HasBody) continue; foreach (Instruction instr in method.Body.Instructions) { if (CanBeReference(instr.OpCode.Code)) { IField fr = instr.Operand as IField; if (fr != null && fr.Name == name && Helpers.IsReferencedBy(analyzedField.DeclaringType, fr.DeclaringType) && Decompiler.DnlibExtensions.Resolve(fr) == analyzedField) { found = true; break; } } } method.Body = null; 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; } } } }
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; } } } } }
private IEnumerable<SharpTreeNode> 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; } } } }
private IEnumerable<SharpTreeNode> FindReferences(LoadedAssembly asm, CancellationToken ct) { string asmName = asm.AssemblyDefinition.Name.Name; string name = analyzedMethod.Name; string declTypeName = analyzedMethod.DeclaringType.FullName; foreach (TypeDefinition type in TreeTraversal.PreOrder(asm.AssemblyDefinition.MainModule.Types, t => t.NestedTypes)) { ct.ThrowIfCancellationRequested(); SharpTreeNode newNode = null; try { if (!TypesHierarchyHelpers.IsBaseType(analyzedMethod.DeclaringType, type, resolveTypeArguments: false)) continue; foreach (MethodDefinition method in type.Methods) { ct.ThrowIfCancellationRequested(); 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) yield return newNode; } }