Exemplo n.º 1
0
        void AddSuppression(SuppressMessageInfo info, ICustomAttributeProvider provider)
        {
            if (!_suppressions.TryGetValue(provider, out var suppressions))
            {
                suppressions = new Dictionary <int, SuppressMessageInfo> ();
                _suppressions.Add(provider, suppressions);
            }

            if (suppressions.ContainsKey(info.Id))
            {
                _context.LogMessage($"Element {provider} has more than one unconditional suppression. Note that only the last one is used.");
            }

            suppressions[info.Id] = info;
        }
Exemplo n.º 2
0
        public static AttributeInfo ProcessAttributes(AssemblyDefinition assembly, LinkContext context)
        {
            if (context.Annotations.GetAction(assembly) == AssemblyAction.Skip)
            {
                return(null);
            }

            var rsc = GetEmbeddedXml(assembly, res => res.Name.Equals("ILLink.LinkAttributes.xml", StringComparison.OrdinalIgnoreCase));

            if (rsc == null)
            {
                return(null);
            }

            LinkAttributesParser parser = null;

            try {
                context.LogMessage($"Processing embedded {rsc.Name} from {assembly.Name}");
                parser = GetExternalLinkAttributesParser(context, rsc, assembly);
            } catch (XmlException ex) {
                context.LogError($"Error processing {rsc.Name} from {assembly.Name}: {ex}", 1003);
            }

            if (parser == null)
            {
                return(null);
            }

            var attributeInfo = new AttributeInfo();

            parser.Parse(attributeInfo);
            return(attributeInfo);
        }
Exemplo n.º 3
0
        public static void ProcessDescriptors(AssemblyDefinition assembly, LinkContext context)
        {
            if (context.Annotations.GetAction(assembly) == AssemblyAction.Skip)
            {
                return;
            }

            var rsc = GetEmbeddedXml(assembly, res => ShouldProcessRootDescriptorResource(assembly, context, res.Name));

            if (rsc == null)
            {
                return;
            }

            DescriptorMarker marker = null;

            try {
                context.LogMessage($"Processing embedded linker descriptor {rsc.Name} from {assembly.Name}");
                marker = GetExternalResolveStep(context, rsc, assembly);
            } catch (XmlException ex) {
                /* This could happen if some broken XML file is embedded. */
                context.LogError($"Error processing {rsc.Name}: {ex}", 1003);
            }

            if (marker != null)
            {
                marker.Mark();
            }
        }
        public void UnrecognizedReflectionAccessPattern(IMemberDefinition source, Instruction sourceInstruction, IMetadataTokenProvider accessedItem, string message)
        {
            MessageOrigin origin;
            string        location = string.Empty;
            var           method   = source as MethodDefinition;

            if (sourceInstruction != null && method != null)
            {
                origin = MessageOrigin.TryGetOrigin(method, sourceInstruction.Offset);
            }
            else
            {
                origin = new MessageOrigin(source);
            }

            if (origin.FileName == null)
            {
                if (method != null)
                {
                    location = method.DeclaringType.FullName + "::" + GetSignature(method) + ": ";
                }
                else
                {
                    location = source.DeclaringType?.FullName + "::" + source.Name;
                }
            }

            _context.LogMessage(MessageContainer.CreateWarningMessage(_context, location + message, 2006, origin, "Unrecognized reflection pattern"));
        }
Exemplo n.º 5
0
        public static SubstitutionInfo?ProcessSubstitutions(AssemblyDefinition assembly, LinkContext context)
        {
            if (context.Annotations.GetAction(assembly) == AssemblyAction.Skip)
            {
                return(null);
            }

            var rsc = GetEmbeddedXml(assembly, res => res.Name.Equals("ILLink.Substitutions.xml", StringComparison.OrdinalIgnoreCase));

            if (rsc == null)
            {
                return(null);
            }

            BodySubstitutionParser?parser = null;

            try {
                context.LogMessage($"Processing embedded substitution descriptor '{rsc.Name}' from '{assembly.Name}'.");
                parser = GetExternalSubstitutionParser(context, rsc, assembly);
            } catch (XmlException ex) {
                context.LogError(null, DiagnosticId.XmlException, rsc.Name, ex.ToString());
            }

            if (parser == null)
            {
                return(null);
            }

            var substitutionInfo = new SubstitutionInfo();

            parser.Parse(substitutionInfo);
            return(substitutionInfo);
        }
Exemplo n.º 6
0
        public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            AssemblyDefinition asm = null;

            if (!_assemblies.TryGetValue(name.Name, out asm) && (_unresolvedAssemblies == null || !_unresolvedAssemblies.Contains(name.Name)))
            {
                try {
                    asm = base.Resolve(name, parameters);
                    _assemblies [name.Name] = asm;
                } catch (AssemblyResolutionException) {
                    if (!_ignoreUnresolved)
                    {
                        throw;
                    }

                    _context.LogMessage($"warning: unresolved assembly {name.Name}");
                    if (_unresolvedAssemblies == null)
                    {
                        _unresolvedAssemblies = new HashSet <string> ();
                    }
                    _unresolvedAssemblies.Add(name.Name);
                }
            }

            return(asm);
        }
        static Attribute ProcessRequiresUnreferencedCodeAttribute(LinkContext context, MethodDefinition method, CustomAttribute customAttribute)
        {
            if (customAttribute.HasConstructorArguments)
            {
                string message = (string)customAttribute.ConstructorArguments[0].Value;
                string url     = null;
                foreach (var prop in customAttribute.Properties)
                {
                    if (prop.Name == "Url")
                    {
                        url = (string)prop.Argument.Value;
                    }
                }

                return(new RequiresUnreferencedCodeAttribute(message)
                {
                    Url = url
                });
            }

            context.LogMessage(MessageContainer.CreateWarningMessage(context,
                                                                     $"Attribute '{typeof (RequiresUnreferencedCodeAttribute).FullName}' on '{method}' doesn't have a required constructor argument.",
                                                                     2028, MessageOrigin.TryGetOrigin(method, 0)));

            return(null);
        }
Exemplo n.º 8
0
        public bool Run(ILogger customLogger = null)
        {
            int setupStatus = SetupContext(customLogger);

            if (setupStatus > 0)
            {
                return(true);
            }
            if (setupStatus < 0)
            {
                return(false);
            }

            Pipeline p = context.Pipeline;

            PreProcessPipeline(p);

            try {
                p.Process(context);
            } catch (Exception ex) {
                if (ex is LinkerFatalErrorException lex)
                {
                    context.LogMessage(lex.MessageContainer);
                    Console.Error.WriteLine(ex.ToString());
                }
                else
                {
                    context.LogMessage(MessageContainer.CreateErrorMessage($"IL Linker has encountered an unexpected error. Please report the issue at https://github.com/mono/linker/issues \n{ex}", 1012));
                }

                return(false);
            } finally {
                context.Tracer.Finish();
            }

            return(true);
        }
        void AddSuppression(SuppressMessageInfo info, ICustomAttributeProvider provider)
        {
            if (!_suppressions.TryGetValue(provider, out var suppressions))
            {
                suppressions = new Dictionary <int, SuppressMessageInfo> ();
                _suppressions.Add(provider, suppressions);
            }
            else if (suppressions.ContainsKey(info.Id))
            {
                string?elementName = provider is MemberReference memberRef?memberRef.GetDisplayName() : provider.ToString();

                _context.LogMessage($"Element '{elementName}' has more than one unconditional suppression. Note that only the last one is used.");
            }

            suppressions[info.Id] = info;
        }
Exemplo n.º 10
0
        public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            // Validate arguments, similarly to how the base class does it.
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            AssemblyDefinition asm = null;

            if (!_assemblies.TryGetValue(name.Name, out asm) && (_unresolvedAssemblies == null || !_unresolvedAssemblies.Contains(name.Name)))
            {
                try {
                    // Any full path explicit reference takes precedence over other look up logic
                    asm = ResolveFromReferences(name, _references, parameters);

                    // Fall back to the base class resolution logic
                    if (asm == null)
                    {
                        asm = base.Resolve(name, parameters);
                    }

                    _assemblies [name.Name] = asm;
                } catch (AssemblyResolutionException) {
                    if (!_ignoreUnresolved)
                    {
                        throw;
                    }

                    _context.LogMessage($"warning: unresolved assembly {name.Name}");
                    if (_unresolvedAssemblies == null)
                    {
                        _unresolvedAssemblies = new HashSet <string> ();
                    }
                    _unresolvedAssemblies.Add(name.Name);
                }
            }

            return(asm);
        }
        public void AddLocalSuppression(CustomAttribute ca, ICustomAttributeProvider provider)
        {
            SuppressMessageInfo info;

            if (!TryDecodeSuppressMessageAttributeData(ca, out info))
            {
                return;
            }

            if (!_localSuppressions.TryGetValue(provider, out var suppressions))
            {
                suppressions = new Dictionary <int, SuppressMessageInfo> ();
                _localSuppressions.Add(provider, suppressions);
            }

            if (suppressions.ContainsKey(info.Id))
            {
                _context.LogMessage($"Element {provider} has more than one unconditional suppression. Note that only the last one is used.");
            }

            suppressions[info.Id] = info;
        }
Exemplo n.º 12
0
 public void UnrecognizedReflectionAccessPattern(MethodDefinition sourceMethod, MethodDefinition reflectionMethod, string message)
 {
     _context.LogMessage(MessageImportance.Low, message);
 }
 public void UnrecognizedReflectionAccessPattern(MethodDefinition sourceMethod, Instruction reflectionMethodCall, IMetadataTokenProvider accessedItem, string message)
 {
     _context.LogMessage(MessageContainer.CreateWarningMessage(message, 2006, "Unrecognized reflection pattern",
                                                               reflectionMethodCall != null ? MessageOrigin.TryGetOrigin(sourceMethod, reflectionMethodCall.Offset) : null));
 }