Skip to content

Commit b23be78

Browse files
authored
Don't suppress all warnings with SuppressTrimAnalysisWarnings (#3003)
This replaces 82c6dc6 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.
1 parent 1a13498 commit b23be78

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Copyright (c) .NET Foundation. All rights reserved.
5959

6060
<!-- Suppress warnings produced by the linker or by the ILLink Roslyn analyzer. -->
6161
<PropertyGroup Condition="'$(SuppressTrimAnalysisWarnings)' == 'true'">
62-
<ILLinkWarningLevel Condition="'$(ILLinkWarningLevel)' == ''">0</ILLinkWarningLevel>
62+
<_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) --notrimwarn</_ExtraTrimmerArgs>
6363
<EnableTrimAnalyzer Condition="'$(EnableTrimAnalyzer)' == ''">false</EnableTrimAnalyzer>
6464
</PropertyGroup>
6565

@@ -232,6 +232,17 @@ Copyright (c) .NET Foundation. All rights reserved.
232232
<TrimmerSingleWarn Condition=" '$(TrimmerSingleWarn)' == '' ">true</TrimmerSingleWarn>
233233
</PropertyGroup>
234234

235+
<!-- Suppressions to work around issues in previous versions of the framework. See https://github.com/dotnet/runtime/issues/40336 -->
236+
<PropertyGroup Condition="'$(SuppressTrimAnalysisWarnings)' == 'true' And $([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '6.0'))">
237+
<!-- Framework embedded XML descriptors reference windows-only members. -->
238+
<NoWarn Condition=" !$(RuntimeIdentifier.StartsWith('win')) ">$(NoWarn);IL2008</NoWarn> <!-- Unresolved type referenced in XML. -->
239+
<NoWarn Condition=" !$(RuntimeIdentifier.StartsWith('win')) ">$(NoWarn);IL2009</NoWarn> <!-- Unresolved member on type referenced in XML. -->
240+
<!-- Framework has DynamicDependencyAttributes that reference windows-only members. -->
241+
<NoWarn Condition=" !$(RuntimeIdentifier.StartsWith('win')) ">$(NoWarn);IL2037</NoWarn> <!-- Unresolved member for DynamicDependencyAttribute -->
242+
<!-- Framework embedded XML descriptors reference 32-bit-only members. -->
243+
<NoWarn Condition=" '$(PlatformTarget)' != 'x64' AND '$(PlatformTarget)' != 'arm64'">$(NoWarn);IL2009;IL2012</NoWarn> <!-- Unresolved field referenced in XML -->
244+
</PropertyGroup>
245+
235246
<!-- Enable serialization discovery for compat in < 7.0 -->
236247
<PropertyGroup Condition="$([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '7.0'))">
237248
<_ExtraTrimmerArgs>--enable-serialization-discovery $(_ExtraTrimmerArgs)</_ExtraTrimmerArgs>

src/linker/Linker/Driver.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ protected int SetupContext (ILogger? customLogger = null)
505505
context.WarningSuppressionWriter = new WarningSuppressionWriter (context, fileOutputKind);
506506
continue;
507507

508+
case "--notrimwarn":
509+
context.NoTrimWarn = true;
510+
continue;
511+
508512
case "--nowarn":
509513
if (!GetStringParam (token, out string? noWarnArgument))
510514
return -1;

src/linker/Linker/LinkContext.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ internal TypeNameResolver TypeNameResolver {
163163

164164
public HashSet<int> NoWarn { get; set; }
165165

166+
public bool NoTrimWarn { get; set; }
167+
166168
public Dictionary<int, bool> WarnAsError { get; set; }
167169

168170
public bool GeneralWarnAsError { get; set; }
@@ -700,8 +702,11 @@ public void FlushCachedWarnings ()
700702
_cachedWarningMessageContainers.Clear ();
701703
}
702704

703-
public bool IsWarningSuppressed (int warningCode, MessageOrigin origin)
705+
public bool IsWarningSuppressed (int warningCode, string subcategory, MessageOrigin origin)
704706
{
707+
if (subcategory == MessageSubCategory.TrimAnalysis && NoTrimWarn)
708+
return true;
709+
705710
// This warning was turned off by --nowarn.
706711
if (NoWarn.Contains (warningCode))
707712
return true;

src/linker/Linker/MessageContainer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private static MessageContainer CreateWarningMessageContainer (LinkContext conte
162162
if (!(version >= WarnVersion.ILLink0 && version <= WarnVersion.Latest))
163163
throw new ArgumentException ($"The provided warning version '{version}' is invalid.");
164164

165-
if (context.IsWarningSuppressed (code, origin))
165+
if (context.IsWarningSuppressed (code, subcategory, origin))
166166
return Empty;
167167

168168
if (version > context.WarnVersion)
@@ -182,7 +182,7 @@ private static MessageContainer CreateWarningMessageContainer (LinkContext conte
182182
if (!(version >= WarnVersion.ILLink0 && version <= WarnVersion.Latest))
183183
throw new ArgumentException ($"The provided warning version '{version}' is invalid.");
184184

185-
if (context.IsWarningSuppressed ((int) id, origin))
185+
if (context.IsWarningSuppressed ((int) id, subcategory, origin))
186186
return Empty;
187187

188188
if (version > context.WarnVersion)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Mono.Linker.Tests.Cases.Expectations.Assertions;
3+
using Mono.Linker.Tests.Cases.Expectations.Metadata;
4+
5+
namespace Mono.Linker.Tests.Cases.Extensibility
6+
{
7+
[SetupCompileBefore ("CustomWarning.dll", new[] { "Dependencies/CustomWarning.cs" }, new[] { "illink.dll", "Mono.Cecil.dll", "netstandard.dll" })]
8+
[SetupLinkerArgument ("--custom-step", "CustomWarning,CustomWarning.dll")]
9+
[SetupLinkerArgument ("--notrimwarn")]
10+
[ExpectedNoWarnings]
11+
public class CustomWarningUsage
12+
{
13+
[ExpectedWarning ("IL2026", "--RUCMethod--", ProducedBy = ProducedBy.Analyzer)]
14+
public static void Main ()
15+
{
16+
new KnownTypeThatShouldWarn ();
17+
RUCMethod (); // Warning suppressed by --notrimwarn
18+
}
19+
20+
[ExpectedWarning ("IL6200", "custom warning on type")]
21+
[Kept]
22+
[KeptMember (".ctor()")]
23+
public class KnownTypeThatShouldWarn
24+
{
25+
}
26+
27+
[Kept]
28+
[KeptAttributeAttribute (typeof (RequiresUnreferencedCodeAttribute))]
29+
[RequiresUnreferencedCode ("--RUCMethod--")]
30+
static void RUCMethod () { }
31+
}
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Mono.Cecil;
4+
using Mono.Linker;
5+
using Mono.Linker.Steps;
6+
7+
public class CustomWarning : IMarkHandler
8+
{
9+
LinkContext _context;
10+
11+
public void Initialize (LinkContext context, MarkContext markContext)
12+
{
13+
_context = context;
14+
markContext.RegisterMarkTypeAction (type => WarnOnKnownType (type));
15+
}
16+
17+
void WarnOnKnownType (TypeDefinition type )
18+
{
19+
if (type.Name == "KnownTypeThatShouldWarn")
20+
_context.LogMessage (MessageContainer.CreateCustomWarningMessage (_context, "custom warning on type", 6200, new MessageOrigin (type), WarnVersion.ILLink5));
21+
}
22+
}

0 commit comments

Comments
 (0)