diff --git a/src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets b/src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets index 76cf4728756d..0fe32893e8dd 100644 --- a/src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets +++ b/src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets @@ -59,7 +59,7 @@ Copyright (c) .NET Foundation. All rights reserved. - 0 + <_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) --notrimwarn false @@ -223,6 +223,17 @@ Copyright (c) .NET Foundation. All rights reserved. true + + + + $(NoWarn);IL2008 + $(NoWarn);IL2009 + + $(NoWarn);IL2037 + + $(NoWarn);IL2009;IL2012 + + <_ExtraTrimmerArgs>--enable-serialization-discovery $(_ExtraTrimmerArgs) diff --git a/src/linker/Linker/Driver.cs b/src/linker/Linker/Driver.cs index c4e2cc65fbc7..77f3b4ddb2f4 100644 --- a/src/linker/Linker/Driver.cs +++ b/src/linker/Linker/Driver.cs @@ -505,6 +505,10 @@ protected int SetupContext (ILogger? customLogger = null) context.WarningSuppressionWriter = new WarningSuppressionWriter (context, fileOutputKind); continue; + case "--notrimwarn": + context.NoTrimWarn = true; + continue; + case "--nowarn": if (!GetStringParam (token, out string? noWarnArgument)) return -1; diff --git a/src/linker/Linker/LinkContext.cs b/src/linker/Linker/LinkContext.cs index f909c7ffffc1..a10a7ac3e4f0 100644 --- a/src/linker/Linker/LinkContext.cs +++ b/src/linker/Linker/LinkContext.cs @@ -163,6 +163,8 @@ internal TypeNameResolver TypeNameResolver { public HashSet NoWarn { get; set; } + public bool NoTrimWarn { get; set; } + public Dictionary WarnAsError { get; set; } public bool GeneralWarnAsError { get; set; } @@ -700,8 +702,11 @@ public void FlushCachedWarnings () _cachedWarningMessageContainers.Clear (); } - public bool IsWarningSuppressed (int warningCode, MessageOrigin origin) + public bool IsWarningSuppressed (int warningCode, string subcategory, MessageOrigin origin) { + if (subcategory == MessageSubCategory.TrimAnalysis && NoTrimWarn) + return true; + // This warning was turned off by --nowarn. if (NoWarn.Contains (warningCode)) return true; diff --git a/src/linker/Linker/MessageContainer.cs b/src/linker/Linker/MessageContainer.cs index 328763d2459f..f7ed7bd044f1 100644 --- a/src/linker/Linker/MessageContainer.cs +++ b/src/linker/Linker/MessageContainer.cs @@ -162,7 +162,7 @@ private static MessageContainer CreateWarningMessageContainer (LinkContext conte if (!(version >= WarnVersion.ILLink0 && version <= WarnVersion.Latest)) throw new ArgumentException ($"The provided warning version '{version}' is invalid."); - if (context.IsWarningSuppressed (code, origin)) + if (context.IsWarningSuppressed (code, subcategory, origin)) return Empty; if (version > context.WarnVersion) @@ -182,7 +182,7 @@ private static MessageContainer CreateWarningMessageContainer (LinkContext conte if (!(version >= WarnVersion.ILLink0 && version <= WarnVersion.Latest)) throw new ArgumentException ($"The provided warning version '{version}' is invalid."); - if (context.IsWarningSuppressed ((int) id, origin)) + if (context.IsWarningSuppressed ((int) id, subcategory, origin)) return Empty; if (version > context.WarnVersion) diff --git a/test/Mono.Linker.Tests.Cases/Extensibility/CustomWarningUsage.cs b/test/Mono.Linker.Tests.Cases/Extensibility/CustomWarningUsage.cs new file mode 100644 index 000000000000..a488f8b82eaf --- /dev/null +++ b/test/Mono.Linker.Tests.Cases/Extensibility/CustomWarningUsage.cs @@ -0,0 +1,32 @@ +using System.Diagnostics.CodeAnalysis; +using Mono.Linker.Tests.Cases.Expectations.Assertions; +using Mono.Linker.Tests.Cases.Expectations.Metadata; + +namespace Mono.Linker.Tests.Cases.Extensibility +{ + [SetupCompileBefore ("CustomWarning.dll", new[] { "Dependencies/CustomWarning.cs" }, new[] { "illink.dll", "Mono.Cecil.dll", "netstandard.dll" })] + [SetupLinkerArgument ("--custom-step", "CustomWarning,CustomWarning.dll")] + [SetupLinkerArgument ("--notrimwarn")] + [ExpectedNoWarnings] + public class CustomWarningUsage + { + [ExpectedWarning ("IL2026", "--RUCMethod--", ProducedBy = ProducedBy.Analyzer)] + public static void Main () + { + new KnownTypeThatShouldWarn (); + RUCMethod (); // Warning suppressed by --notrimwarn + } + + [ExpectedWarning ("IL6200", "custom warning on type")] + [Kept] + [KeptMember (".ctor()")] + public class KnownTypeThatShouldWarn + { + } + + [Kept] + [KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))] + [RequiresUnreferencedCode ("--RUCMethod--")] + static void RUCMethod () { } + } +} \ No newline at end of file diff --git a/test/Mono.Linker.Tests.Cases/Extensibility/Dependencies/CustomWarning.cs b/test/Mono.Linker.Tests.Cases/Extensibility/Dependencies/CustomWarning.cs new file mode 100644 index 000000000000..9b35ce0d4f6a --- /dev/null +++ b/test/Mono.Linker.Tests.Cases/Extensibility/Dependencies/CustomWarning.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using Mono.Cecil; +using Mono.Linker; +using Mono.Linker.Steps; + +public class CustomWarning : IMarkHandler +{ + LinkContext _context; + + public void Initialize (LinkContext context, MarkContext markContext) + { + _context = context; + markContext.RegisterMarkTypeAction (type => WarnOnKnownType (type)); + } + + void WarnOnKnownType (TypeDefinition type ) + { + if (type.Name == "KnownTypeThatShouldWarn") + _context.LogMessage (MessageContainer.CreateCustomWarningMessage (_context, "custom warning on type", 6200, new MessageOrigin (type), WarnVersion.ILLink5)); + } +} \ No newline at end of file