IEnumerable <(SuppressMessageInfo Info, ICustomAttributeProvider Target)> DecodeGlobalSuppressions(ModuleDefinition module, ICustomAttributeProvider provider)
        {
            var attributes = _context.CustomAttributes.GetCustomAttributes(provider).
                             Where(a => TypeRefHasUnconditionalSuppressions(a.AttributeType));

            foreach (var instance in attributes)
            {
                SuppressMessageInfo info;
                if (!TryDecodeSuppressMessageAttributeData(instance, out info))
                {
                    continue;
                }

                var scope = info.Scope?.ToLower();
                if (info.Target == null && (scope == "module" || scope == null))
                {
                    yield return(info, provider);

                    continue;
                }

                switch (scope)
                {
                case "module":
                    yield return(info, provider);

                    break;

                case "type":
                case "member":
                    if (info.Target == null)
                    {
                        break;
                    }

                    foreach (var result in DocumentationSignatureParser.GetMembersForDocumentationSignature(info.Target, module, _context))
                    {
                        yield return(info, result);
                    }

                    break;

                default:
                    _context.LogWarning($"Invalid scope '{info.Scope}' used in 'UnconditionalSuppressMessageAttribute' on module '{module.Name}' " +
                                        $"with target '{info.Target}'.",
                                        2108, _context.GetAssemblyLocation(module.Assembly));
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        static bool TryLogSingleWarning(LinkContext context, int code, MessageOrigin origin, string subcategory)
        {
            if (subcategory != MessageSubCategory.TrimAnalysis)
            {
                return(false);
            }

            // There are valid cases where we can't map the message to an assembly
            // For example if it's caused by something in an xml file passed on the command line
            // In that case, give up on single-warn collapse and just print out the warning on its own.
            var assembly = origin.Provider switch {
                AssemblyDefinition asm => asm,
                TypeDefinition type => type.Module.Assembly,
                IMemberDefinition member => member.DeclaringType.Module.Assembly,
                _ => null
            };

            if (assembly == null)
            {
                return(false);
            }

            // Any IL2026 warnings left in an assembly with an IsTrimmable attribute are considered intentional
            // and should not be collapsed, so that the user-visible RUC message gets printed.
            if (code == 2026 && context.IsTrimmable(assembly))
            {
                return(false);
            }

            var assemblyName = assembly.Name.Name;

            if (!context.IsSingleWarn(assemblyName))
            {
                return(false);
            }

            if (context.AssembliesWithGeneratedSingleWarning.Add(assemblyName))
            {
                context.LogWarning(context.GetAssemblyLocation(assembly), DiagnosticId.AssemblyProducedTrimWarnings, assemblyName);
            }

            return(true);
        }
Ejemplo n.º 3
0
        private static MessageContainer CreateWarningMessageContainer(LinkContext context, string text, int code, MessageOrigin origin, WarnVersion version, string subcategory = MessageSubCategory.None)
        {
            if (!(version >= WarnVersion.ILLink0 && version <= WarnVersion.Latest))
            {
                throw new ArgumentException($"The provided warning version '{version}' is invalid.");
            }

            if (context.IsWarningSuppressed(code, origin))
            {
                return(Empty);
            }

            if (version > context.WarnVersion)
            {
                return(Empty);
            }

            if (subcategory == MessageSubCategory.TrimAnalysis)
            {
                Debug.Assert(origin.MemberDefinition != null);
                var declaringType = origin.MemberDefinition?.DeclaringType ?? (origin.MemberDefinition as TypeDefinition);
                var assembly      = declaringType.Module.Assembly;
                var assemblyName  = assembly?.Name.Name;
                if (assemblyName != null && context.IsSingleWarn(assemblyName))
                {
                    if (context.AssembliesWithGeneratedSingleWarning.Add(assemblyName))
                    {
                        context.LogWarning($"Assembly '{assemblyName}' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries", 2104, context.GetAssemblyLocation(assembly));
                    }
                    return(Empty);
                }
            }

            if (context.IsWarningAsError(code))
            {
                return(new MessageContainer(MessageCategory.WarningAsError, text, code, subcategory, origin));
            }

            return(new MessageContainer(MessageCategory.Warning, text, code, subcategory, origin));
        }
Ejemplo n.º 4
0
        static bool TryLogSingleWarning(LinkContext context, int code, MessageOrigin origin, string subcategory)
        {
            if (subcategory != MessageSubCategory.TrimAnalysis)
            {
                return(false);
            }

            Debug.Assert(origin.Provider != null);
            var assembly = origin.Provider switch {
                AssemblyDefinition asm => asm,
                TypeDefinition type => type.Module.Assembly,
                IMemberDefinition member => member.DeclaringType.Module.Assembly,
                _ => throw new NotSupportedException()
            };

            Debug.Assert(assembly != null);
            if (assembly == null)
            {
                return(false);
            }

            // Any IL2026 warnings left in an assembly with an IsTrimmable attribute are considered intentional
            // and should not be collapsed, so that the user-visible RUC message gets printed.
            if (code == 2026 && context.IsTrimmable(assembly))
            {
                return(false);
            }

            var assemblyName = assembly.Name.Name;

            if (!context.IsSingleWarn(assemblyName))
            {
                return(false);
            }

            if (context.AssembliesWithGeneratedSingleWarning.Add(assemblyName))
            {
                context.LogWarning($"Assembly '{assemblyName}' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries", 2104, context.GetAssemblyLocation(assembly));
            }

            return(true);
        }