Ejemplo n.º 1
0
        /// <summary>
        /// Display a warning message to the end user.
        /// This API is used for warnings defined in the linker, not by custom steps. Warning
        /// versions are inferred from the code, and every warning that we define is versioned.
        /// </summary>
        /// <param name="text">Humanly readable message describing the warning</param>
        /// <param name="code">Unique warning ID. Please see https://github.com/mono/linker/blob/main/docs/error-codes.md for the list of warnings and possibly add a new one</param>
        /// <param name="origin">Filename or member where the warning is coming from</param>
        /// <param name="subcategory">Optionally, further categorize this warning</param>
        /// <returns>New MessageContainer of 'Warning' category</returns>
        public void LogWarning(string text, int code, MessageOrigin origin, string subcategory = MessageSubCategory.None)
        {
            WarnVersion      version = GetWarningVersion();
            MessageContainer warning = MessageContainer.CreateWarningMessage(this, text, code, origin, version, subcategory);

            _cachedWarningMessageContainers.Add(warning);
        }
Ejemplo n.º 2
0
        public void TestWarn(string warnArg, WarnVersion expectedVersion)
        {
            var task = new MockTask()
            {
                Warn = warnArg
            };

            using (var driver = task.CreateDriver()) {
                Assert.Equal(expectedVersion, driver.Context.WarnVersion);
            }
        }
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
        /// <summary>
        /// Create a custom warning message.
        /// </summary>
        /// <param name="context">Context with the relevant warning suppression info.</param>
        /// <param name="text">Humanly readable message describing the warning</param>
        /// <param name="code">A custom warning ID. This code should be greater than or equal to 6001
        /// to avoid any collisions with existing and future linker warnings</param>
        /// <param name="origin">Filename or member where the warning is coming from</param>
        /// <param name="version">Optional warning version number. Versioned warnings can be controlled with the
        /// warning wave option --warn VERSION. Unversioned warnings are unaffected by this option</param>
        /// <param name="subcategory"></param>
        /// <returns>Custom MessageContainer of 'Warning' category</returns>
        public static MessageContainer CreateCustomWarningMessage(LinkContext context, string text, int code, MessageOrigin origin, WarnVersion version, string subcategory = MessageSubCategory.None)
        {
#if DEBUG
            Debug.Assert(Assembly.GetCallingAssembly() != typeof(MessageContainer).Assembly,
                         "'CreateCustomWarningMessage' is intended to be used by external assemblies only. Use 'CreateWarningMessage' instead.");
#endif
            if (code <= 6000)
            {
                throw new ArgumentOutOfRangeException(nameof(code), $"The provided code '{code}' does not fall into the permitted range for external warnings. To avoid possible collisions " +
                                                      $"with existing and future {Constants.ILLink} warnings, external messages should use codes starting from 6001.");
            }

            return(CreateWarningMessageContainer(context, text, code, origin, version, subcategory));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Create a warning message.
        /// </summary>
        /// <param name="context">Context with the relevant warning suppression info.</param>
        /// <param name="text">Humanly readable message describing the warning</param>
        /// <param name="code">Unique warning ID. Please see https://github.com/mono/linker/blob/main/docs/error-codes.md
        /// for the list of warnings and possibly add a new one</param>
        /// /// <param name="origin">Filename or member where the warning is coming from</param>
        /// <param name="subcategory">Optionally, further categorize this warning</param>
        /// <param name="version">Optional warning version number. Versioned warnings can be controlled with the
        /// warning wave option --warn VERSION. Unversioned warnings are unaffected by this option. </param>
        /// <returns>New MessageContainer of 'Warning' category</returns>
        internal static MessageContainer CreateWarningMessage(LinkContext context, string text, int code, MessageOrigin origin, WarnVersion version, string subcategory = MessageSubCategory.None)
        {
            if (!(code > 2000 && code <= 6000))
            {
                throw new ArgumentOutOfRangeException(nameof(code), $"The provided code '{code}' does not fall into the warning category, which is in the range of 2001 to 6000 (inclusive).");
            }

            return(CreateWarningMessageContainer(context, text, code, origin, version, subcategory));
        }
Ejemplo n.º 6
0
 public static MessageContainer CreateCustomWarningMessage(LinkContext context, string text, int code, MessageOrigin origin, WarnVersion version, string subcategory = "")
 {
     throw null;
 }
Ejemplo n.º 7
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 (TryLogSingleWarning(context, code, origin, subcategory))
            {
                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.º 8
0
        private static MessageContainer CreateWarningMessageContainer(LinkContext context, MessageOrigin origin, DiagnosticId id, WarnVersion version, string subcategory, params string[] args)
        {
            if (!(version >= WarnVersion.ILLink0 && version <= WarnVersion.Latest))
            {
                throw new ArgumentException($"The provided warning version '{version}' is invalid.");
            }

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

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

            if (TryLogSingleWarning(context, (int)id, origin, subcategory))
            {
                return(Empty);
            }

            if (context.IsWarningAsError((int)id))
            {
                return(new MessageContainer(MessageCategory.WarningAsError, id, subcategory, origin, args));
            }

            return(new MessageContainer(MessageCategory.Warning, id, subcategory, origin, args));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Create a warning message.
        /// </summary>
        /// <param name="context">Context with the relevant warning suppression info.</param>
        /// <param name="origin">Filename or member where the warning is coming from</param>
        /// <param name="id">Unique warning ID. Please see https://github.com/dotnet/linker/blob/main/docs/error-codes.md
        /// for the list of warnings and possibly add a new one</param>
        /// <param name="version">Optional warning version number. Versioned warnings can be controlled with the
        /// warning wave option --warn VERSION. Unversioned warnings are unaffected by this option. </param>
        /// <param name="args">Additional arguments to form a humanly readable message describing the warning</param>
        /// <returns>New MessageContainer of 'Warning' category</returns>
        internal static MessageContainer CreateWarningMessage(LinkContext context, MessageOrigin origin, DiagnosticId id, WarnVersion version, params string[] args)
        {
            if (!((int)id > 2000 && (int)id <= 6000))
            {
                throw new ArgumentOutOfRangeException(nameof(id), $"The provided code '{(int) id}' does not fall into the warning category, which is in the range of 2001 to 6000 (inclusive).");
            }

            return(CreateWarningMessageContainer(context, origin, id, version, id.GetDiagnosticSubcategory(), args));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Create a warning message.
        /// </summary>
        /// <param name="context">Context with the relevant warning suppression info.</param>
        /// <param name="text">Humanly readable message describing the warning</param>
        /// <param name="code">Unique warning ID. Please see https://github.com/mono/linker/blob/master/doc/error-codes.md
        /// for the list of warnings and possibly add a new one</param>
        /// /// <param name="origin">Filename or member where the warning is coming from</param>
        /// <param name="subcategory">Optionally, further categorize this warning</param>
        /// <param name="version">Optional warning version number. Versioned warnings can be controlled with the
        /// warning wave option --warn VERSION. Unversioned warnings are unaffected by this option. </param>
        /// <returns>New MessageContainer of 'Warning' category</returns>
        public static MessageContainer CreateWarningMessage(LinkContext context, string text, int code, MessageOrigin origin, WarnVersion version, string subcategory = MessageSubCategory.None)
        {
            if (!(code > 2000 && code <= 6000))
            {
                throw new ArgumentException($"The provided code '{code}' does not fall into the warning category, which is in the range of 2001 to 6000 (inclusive).");
            }

            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 (context.IsWarningAsError(code))
            {
                return(new MessageContainer(MessageCategory.WarningAsError, text, code, subcategory, origin));
            }

            return(new MessageContainer(MessageCategory.Warning, text, code, subcategory, origin));
        }