diff options
author | Sven Boemer <sbomer@gmail.com> | 2022-08-26 01:47:48 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-26 01:47:48 +0300 |
commit | b23be78205b79d60c1ea2cab5306f530a0faade2 (patch) | |
tree | 51212c13e94c1987ccaaa2b4314cd894889101d6 | |
parent | 1a134989d42c995a9bbbc909084141ccfa754acd (diff) |
Don't suppress all warnings with SuppressTrimAnalysisWarnings (#3003)
This replaces
https://github.com/dotnet/linker/commit/82c6dc6f82aeb90cfbe509ae07cf539eecb75550
with a different approach. Now SuppressTrimAnalysisWarnings only
suppresses those warnings defined to be part of the "trim analysis"
category, using a separate command-line argument. An exception is 5.0
apps, where the setting continues to suppress specific warnings for
compatibility.
-rw-r--r-- | src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets | 13 | ||||
-rw-r--r-- | src/linker/Linker/Driver.cs | 4 | ||||
-rw-r--r-- | src/linker/Linker/LinkContext.cs | 7 | ||||
-rw-r--r-- | src/linker/Linker/MessageContainer.cs | 4 | ||||
-rw-r--r-- | test/Mono.Linker.Tests.Cases/Extensibility/CustomWarningUsage.cs | 32 | ||||
-rw-r--r-- | test/Mono.Linker.Tests.Cases/Extensibility/Dependencies/CustomWarning.cs | 22 |
6 files changed, 78 insertions, 4 deletions
diff --git a/src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets b/src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets index e69f49b54..84430b768 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. <!-- Suppress warnings produced by the linker or by the ILLink Roslyn analyzer. --> <PropertyGroup Condition="'$(SuppressTrimAnalysisWarnings)' == 'true'"> - <ILLinkWarningLevel Condition="'$(ILLinkWarningLevel)' == ''">0</ILLinkWarningLevel> + <_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) --notrimwarn</_ExtraTrimmerArgs> <EnableTrimAnalyzer Condition="'$(EnableTrimAnalyzer)' == ''">false</EnableTrimAnalyzer> </PropertyGroup> @@ -232,6 +232,17 @@ Copyright (c) .NET Foundation. All rights reserved. <TrimmerSingleWarn Condition=" '$(TrimmerSingleWarn)' == '' ">true</TrimmerSingleWarn> </PropertyGroup> + <!-- Suppressions to work around issues in previous versions of the framework. See https://github.com/dotnet/runtime/issues/40336 --> + <PropertyGroup Condition="'$(SuppressTrimAnalysisWarnings)' == 'true' And $([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '6.0'))"> + <!-- Framework embedded XML descriptors reference windows-only members. --> + <NoWarn Condition=" !$(RuntimeIdentifier.StartsWith('win')) ">$(NoWarn);IL2008</NoWarn> <!-- Unresolved type referenced in XML. --> + <NoWarn Condition=" !$(RuntimeIdentifier.StartsWith('win')) ">$(NoWarn);IL2009</NoWarn> <!-- Unresolved member on type referenced in XML. --> + <!-- Framework has DynamicDependencyAttributes that reference windows-only members. --> + <NoWarn Condition=" !$(RuntimeIdentifier.StartsWith('win')) ">$(NoWarn);IL2037</NoWarn> <!-- Unresolved member for DynamicDependencyAttribute --> + <!-- Framework embedded XML descriptors reference 32-bit-only members. --> + <NoWarn Condition=" '$(PlatformTarget)' != 'x64' AND '$(PlatformTarget)' != 'arm64'">$(NoWarn);IL2009;IL2012</NoWarn> <!-- Unresolved field referenced in XML --> + </PropertyGroup> + <!-- Enable serialization discovery for compat in < 7.0 --> <PropertyGroup Condition="$([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '7.0'))"> <_ExtraTrimmerArgs>--enable-serialization-discovery $(_ExtraTrimmerArgs)</_ExtraTrimmerArgs> diff --git a/src/linker/Linker/Driver.cs b/src/linker/Linker/Driver.cs index c4e2cc65f..77f3b4ddb 100644 --- a/src/linker/Linker/Driver.cs +++ b/src/linker/Linker/Driver.cs @@ -505,6 +505,10 @@ namespace Mono.Linker 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 f909c7fff..a10a7ac3e 100644 --- a/src/linker/Linker/LinkContext.cs +++ b/src/linker/Linker/LinkContext.cs @@ -163,6 +163,8 @@ namespace Mono.Linker public HashSet<int> NoWarn { get; set; } + public bool NoTrimWarn { get; set; } + public Dictionary<int, bool> WarnAsError { get; set; } public bool GeneralWarnAsError { get; set; } @@ -700,8 +702,11 @@ namespace Mono.Linker _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 328763d24..f7ed7bd04 100644 --- a/src/linker/Linker/MessageContainer.cs +++ b/src/linker/Linker/MessageContainer.cs @@ -162,7 +162,7 @@ namespace Mono.Linker 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 @@ namespace Mono.Linker 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 000000000..a488f8b82 --- /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 000000000..9b35ce0d4 --- /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 |