void SweepCollection(IList list) { for (int i = 0; i < list.Count; i++) { if (!Annotations.IsMarked((IMetadataTokenProvider)list [i])) { list.RemoveAt(i--); } } }
bool FilterDispose(MethodDefinition m) { #if DEBUG var sp = m.DebugInformation.GetSequencePoint(m.Body.Instructions [0]); if (sp != null) { string source = sp.Document.Url; if (!source.EndsWith(".g.cs", StringComparison.Ordinal)) { throw new InvalidProgramException(String.Format("Attempt at modifying non-generated code for {0} : {1}", m, source)); } } #endif bool remove_all_fields = true; var il = m.Body.Instructions; for (int i = il.Count - 1; i >= 0; i--) { Instruction ins = il [i]; if (ins.OpCode.Code != Code.Stfld) { continue; } // if the field is not marked elsewhere (since we skipped Dispose) if (!Annotations.IsMarked(ins.Operand as FieldReference)) { // remove stfld, the previous ldnull and ldarg.0 instructions ins.OpCode = OpCodes.Nop; ins.Operand = null; ins = ins.Previous; #if DEBUG if (ins.OpCode.Code != Code.Ldnull) { throw new InvalidProgramException(String.Format("Attempt at modifying wrong code pattern for {0}", m)); } #endif ins.OpCode = OpCodes.Nop; ins.Operand = null; ins = ins.Previous; #if DEBUG if (ins.OpCode.Code != Code.Ldarg_0) { throw new InvalidProgramException(String.Format("Attempt at modifying wrong code pattern for {0}", m)); } #endif ins.OpCode = OpCodes.Nop; ins.Operand = null; i -= 2; } else { remove_all_fields = false; } } return(remove_all_fields); }
void MarkProperty(TypeDefinition type, PropertyDefinition property, string[] accessors, bool required) { if (Annotations.IsMarked(property)) { Context.LogMessage(MessageContainer.CreateWarningMessage($"Duplicate preserve of '{property.FullName}' in '{_xmlDocumentLocation}'", 2025)); } Annotations.Mark(property, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation)); MarkPropertyAccessors(type, property, accessors, required); }
void ProcessMethods(Collection <MethodDefinition> methods, ModuleDefinition module) { foreach (var method in methods) { if (!Annotations.IsMarked(method)) { EnsureBypassNGenAttribute(module); method.CustomAttributes.Add(bypassNGenAttribute); } } }
void MarkMethod(MethodDefinition method) { if (Annotations.IsMarked(method)) { Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {method.FullName}"); } Annotations.Mark(method, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation)); Annotations.MarkIndirectlyCalledMethod(method); Annotations.SetAction(method, MethodAction.Parse); }
void MarkMethod(MethodDefinition method) { if (Annotations.IsMarked(method)) { Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {method.FullName}"); } Annotations.Mark(method); Tracer.AddDirectDependency(this, method); Annotations.SetAction(method, MethodAction.Parse); }
protected void SweepCollection <T> (IList <T> list) where T : IMetadataTokenProvider { for (int i = 0; i < list.Count; i++) { if (!Annotations.IsMarked(list [i])) { ElementRemoved(list [i]); list.RemoveAt(i--); } } }
protected void SweepCustomAttributes(ICustomAttributeProvider provider) { for (int i = provider.CustomAttributes.Count - 1; i >= 0; i--) { var attribute = provider.CustomAttributes [i]; if (!Annotations.IsMarked(attribute)) { CustomAttributeUsageRemoved(provider, attribute); provider.CustomAttributes.RemoveAt(i); } } }
void ResolveAllTypeReferences(AssemblyDefinition assembly) { if (resolvedTypeReferences == null) { resolvedTypeReferences = new HashSet <AssemblyDefinition> (); } if (resolvedTypeReferences.Contains(assembly)) { return; } resolvedTypeReferences.Add(assembly); var hash = new Dictionary <TypeReference, IMetadataScope> (); foreach (TypeReference tr in assembly.MainModule.GetTypeReferences()) { if (hash.ContainsKey(tr)) { continue; } var td = tr.Resolve(); IMetadataScope scope = tr.Scope; // at this stage reference might include things that can't be resolved // and if it is (resolved) it needs to be kept only if marked (#16213) if ((td != null) && Annotations.IsMarked(td)) { scope = assembly.MainModule.Import(td).Scope; } hash.Add(tr, scope); } if (assembly.MainModule.HasExportedTypes) { foreach (var et in assembly.MainModule.ExportedTypes) { var td = et.Resolve(); IMetadataScope scope = et.Scope; if ((td != null) && Annotations.IsMarked(td)) { scope = assembly.MainModule.Import(td).Scope; hash.Add(td, scope); } } } // Resolve everything first before updating scopes. // If we set the scope to null, then calling Resolve() on any of its // nested types would crash. foreach (var e in hash) { e.Key.Scope = e.Value; } }
protected void SweepInterfaces(TypeDefinition type) { for (int i = type.Interfaces.Count - 1; i >= 0; i--) { var iface = type.Interfaces [i]; if (Annotations.IsMarked(iface.InterfaceType.Resolve())) { continue; } InterfaceRemoved(type, iface); type.Interfaces.RemoveAt(i); } }
protected virtual void InitializeAssembly(AssemblyDefinition assembly) { MarkAssembly(assembly); foreach (TypeDefinition type in assembly.MainModule.Types) { if (!Annotations.IsMarked(type)) { continue; } InitializeType(type); } }
void MarkEvent(TypeDefinition type, EventDefinition @event, bool required) { if (Annotations.IsMarked(@event)) { Context.LogMessage(MessageContainer.CreateWarningMessage($"Duplicate preserve of '{@event.FullName}' in '{_xmlDocumentLocation}'", 2025)); } Annotations.Mark(@event, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation)); MarkMethod(type, @event.AddMethod, required); MarkMethod(type, @event.RemoveMethod, required); MarkMethodIfNotNull(type, @event.InvokeMethod, required); }
protected override void ProcessProperty(TypeDefinition type, PropertyDefinition property, XPathNavigator nav, object customData, bool fromSignature) { string[] accessors = fromSignature ? GetAccessors(nav) : _accessorsAll; if (Annotations.IsMarked(property)) { Context.LogWarning($"Duplicate preserve of '{property.FullName}' in '{_xmlDocumentLocation}'", 2025, _xmlDocumentLocation); } Annotations.Mark(property, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation)); ProcessPropertyAccessors(type, property, accessors, customData); }
protected override void ProcessEvent(TypeDefinition type, EventDefinition @event, XPathNavigator nav, object customData) { if (Annotations.IsMarked(@event)) { Context.LogWarning($"Duplicate preserve of '{@event.FullName}' in '{_xmlDocumentLocation}'", 2025, _xmlDocumentLocation); } Annotations.Mark(@event, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation)); ProcessMethod(type, @event.AddMethod, null, customData); ProcessMethod(type, @event.RemoveMethod, null, customData); ProcessMethodIfNotNull(type, @event.InvokeMethod, customData); }
void UpdateForwardedTypesScope(AssemblyDefinition assembly) { var changed_types = new Dictionary <TypeReference, IMetadataScope> (); foreach (TypeReference tr in assembly.MainModule.GetTypeReferences()) { if (tr.IsWindowsRuntimeProjection) { continue; } var td = tr.Resolve(); // at this stage reference might include things that can't be resolved // and if it is (resolved) it needs to be kept only if marked (#16213) if (td == null || !Annotations.IsMarked(td)) { continue; } IMetadataScope scope = assembly.MainModule.ImportReference(td).Scope; if (tr.Scope != scope) { changed_types.Add(tr, scope); } } // // Resolved everything first before updating scopes. // If we set the scope to null, then calling Resolve() on any of its // nested types would crash. // foreach (var e in changed_types) { e.Key.Scope = e.Value; } if (assembly.MainModule.HasExportedTypes) { foreach (var et in assembly.MainModule.ExportedTypes) { var td = et.Resolve(); if (td == null) { continue; } et.Scope = assembly.MainModule.ImportReference(td).Scope; } } }
protected override bool ShouldMarkInterfaceImplementation(TypeDefinition type, InterfaceImplementation iface, TypeDefinition resolvedInterfaceType) { if (Annotations.IsMarked(iface)) { return(false); } if (base.ShouldMarkInterfaceImplementation(type, iface, resolvedInterfaceType)) { return(true); } return(resolvedInterfaceType.ImplementsIJavaObject() || resolvedInterfaceType.ImplementsIJavaPeerable()); }
protected virtual void SweepAssembly(AssemblyDefinition assembly) { var types = new List <TypeDefinition> (); foreach (TypeDefinition type in assembly.MainModule.Types) { if (Annotations.IsMarked(type)) { SweepType(type); types.Add(type); continue; } // Is <Module> type. if (type.MetadataToken.RID == 1) { types.Add(type); } else { ElementRemoved(type); } } assembly.MainModule.Types.Clear(); foreach (TypeDefinition type in types) { assembly.MainModule.Types.Add(type); } SweepResources(assembly); SweepCustomAttributes(assembly); foreach (var module in assembly.Modules) { SweepCustomAttributes(module); } // // MainModule module references are used by pinvoke // if (assembly.MainModule.HasModuleReferences) { SweepCollectionMetadata(assembly.MainModule.ModuleReferences); } SweepTypeForwarders(assembly); UpdateForwardedTypesScope(assembly); }
protected virtual void ProcessType(TypeDefinition type, XPathNavigator nav) { if (IsExcluded(nav)) { return; } TypePreserve preserve = GetTypePreserve(nav); if (!IsRequired(nav)) { Annotations.SetPreserve(type, preserve); return; } if (Annotations.IsMarked(type)) { var existingLevel = Annotations.IsPreserved(type) ? Annotations.GetPreserve(type) : TypePreserve.Nothing; var duplicateLevel = preserve != TypePreserve.Nothing ? preserve : nav.HasChildren ? TypePreserve.Nothing : TypePreserve.All; Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {type.FullName} ({existingLevel}). Duplicate uses ({duplicateLevel})"); } Annotations.MarkAndPush(type); Tracer.AddDirectDependency(this, type); if (type.IsNested) { var parent = type; while (parent.IsNested) { parent = parent.DeclaringType; Annotations.Mark(parent); } } if (preserve != TypePreserve.Nothing) { Annotations.SetPreserve(type, preserve); } if (nav.HasChildren) { MarkSelectedFields(nav, type); MarkSelectedMethods(nav, type); MarkSelectedEvents(nav, type); MarkSelectedProperties(nav, type); } Tracer.Pop(); }
void SweepNestedTypes(TypeDefinition type) { for (int i = 0; i < type.NestedTypes.Count; i++) { var nested = type.NestedTypes [i]; if (Annotations.IsMarked(nested)) { SweepType(nested); } else { type.NestedTypes.RemoveAt(i--); } } }
bool AreMarked(List <OverrideInformation> list) { if (list == null) { return(false); } foreach (var m in list) { if (Annotations.IsMarked(m.Override)) { return(true); } } return(false); }
bool IsAnyMarked(List <MethodDefinition> list) { if (list == null) { return(false); } foreach (var m in list) { if (Annotations.IsMarked(m)) { return(true); } } return(false); }
void MarkField(TypeDefinition type, FieldDefinition field, string signature) { if (field != null) { if (Annotations.IsMarked(field)) { Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {field.FullName}"); } Annotations.Mark(field); } else { AddUnresolveMarker(string.Format("T: {0}; F: {1}", type, signature)); } }
void MarkMethod(TypeDefinition type, MethodDefinition method, bool required) { if (Annotations.IsMarked(method)) { Context.LogMessage(MessageContainer.CreateWarningMessage($"Duplicate preserve of '{method.FullName}' in '{_xmlDocumentLocation}'", 2025)); } Annotations.Mark(method, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation)); Annotations.MarkIndirectlyCalledMethod(method); Annotations.SetAction(method, MethodAction.Parse); if (!required) { Annotations.AddPreservedMethod(type, method); } }
bool IsAnyMarked(IEnumerable <OverrideInformation> list) { if (list == null) { return(false); } foreach (var m in list) { if (Annotations.IsMarked(m.Override)) { return(true); } } return(false); }
protected override void ProcessMethod(TypeDefinition type, MethodDefinition method, XPathNavigator nav, object customData) { if (Annotations.IsMarked(method)) { Context.LogWarning($"Duplicate preserve of '{method.GetDisplayName ()}' in '{_xmlDocumentLocation}'", 2025, _xmlDocumentLocation); } Annotations.Mark(method, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation)); Annotations.MarkIndirectlyCalledMethod(method); Annotations.SetAction(method, MethodAction.Parse); if (!(bool)customData) { Annotations.AddPreservedMethod(type, method); } }
protected override bool IsReferenced(ModuleDefinition module, string name) { if (!module.TryGetTypeReference(name, out var tr)) { return(false); } // it might be there (and not cleaned) until it's saved back to disk // but it can't resolve anymore (since it's removed from the actual assembly) var td = tr.Resolve(); if (td == null) { return(false); } // and, if it was (cache) then we can ask if it was marked (since we're post mark) return(Annotations.IsMarked(td)); }
protected IList <CustomAttribute> SweepCustomAttributes(ICustomAttributeProvider provider) { var removed = new List <CustomAttribute>(); for (int i = provider.CustomAttributes.Count - 1; i >= 0; i--) { var attribute = provider.CustomAttributes [i]; if (!Annotations.IsMarked(attribute)) { CustomAttributeUsageRemoved(provider, attribute); removed.Add(provider.CustomAttributes [i]); provider.CustomAttributes.RemoveAt(i); } } return(removed); }
bool IsLinqProvider(TypeDefinition type) { // skip unmarked types. if (!Annotations.IsMarked(type)) { return(false); } if (type.Namespace == "System.Linq") { return(false); // we are not looking for default system types. } if (!type.HasInterfaces) { return(false); } return(type.Interfaces.Any(i => i.FullName == "System.Linq.IQueryProvider") || type.Interfaces.Select(t => t.Resolve()).Any(t => t != null && IsLinqProvider(t))); }
void MarkProperty(TypeDefinition type, PropertyDefinition property, string signature, string[] accessors) { if (property != null) { if (Annotations.IsMarked(property)) { Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {property.FullName}"); } Annotations.Mark(property); MarkPropertyAccessors(type, property, accessors); } else { AddUnresolveMarker(string.Format("T: {0}; P: {1}", type, signature)); } }
protected virtual void ProcessType(TypeDefinition type, XPathNavigator nav) { #if !FEATURE_ILLINK if (IsExcluded(nav)) { return; } #endif TypePreserve preserve = GetTypePreserve(nav); if (preserve != TypePreserve.Nothing) { Annotations.SetPreserve(type, preserve); } bool required = IsRequired(nav); MarkChildren(type, nav, required); if (!required) { return; } if (Annotations.IsMarked(type)) { var existingLevel = Annotations.TryGetPreserve(type, out TypePreserve existingPreserve) ? existingPreserve : TypePreserve.Nothing; var duplicateLevel = preserve != TypePreserve.Nothing ? preserve : nav.HasChildren ? TypePreserve.Nothing : TypePreserve.All; Context.LogMessage($"Duplicate preserve in {_xmlDocumentLocation} of {type.FullName} ({existingLevel}). Duplicate uses ({duplicateLevel})"); } Annotations.Mark(type, new DependencyInfo(DependencyKind.XmlDescriptor, _xmlDocumentLocation)); if (type.IsNested) { var currentType = type; while (currentType.IsNested) { var parent = currentType.DeclaringType; Context.Annotations.Mark(parent, new DependencyInfo(DependencyKind.DeclaringType, currentType)); currentType = parent; } } }