Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/ILLink.Tasks/build/Microsoft.NET.ILLink.targets
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -223,6 +223,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>
Expand Down
4 changes: 4 additions & 0 deletions src/linker/Linker/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/linker/Linker/LinkContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ internal TypeNameResolver TypeNameResolver {

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

public bool NoTrimWarn { get; set; }

public Dictionary<int, bool> WarnAsError { get; set; }

public bool GeneralWarnAsError { get; set; }
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/linker/Linker/MessageContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions test/Mono.Linker.Tests.Cases/Extensibility/CustomWarningUsage.cs
Original file line number Diff line number Diff line change
@@ -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 () { }
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}