Skip to content

Commit

Permalink
Suppress illink roslyn analyzer warnings
Browse files Browse the repository at this point in the history
Using the same property that suppresses the linker warnings. Includes a
manual dependency update to unblock dotnet#15955.
  • Loading branch information
sbomer committed Feb 23, 2021
1 parent 6e946fe commit 6661105
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
4 changes: 2 additions & 2 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@
</Dependency>
<Dependency Name="Microsoft.NET.ILLink.Tasks" Version="6.0.0-alpha.1.21116.1">
<Uri>https://github.com/mono/linker</Uri>
<Sha>2cb420dbabb4ec86d41a1b431696e58f45f031cd</Sha>
<Sha>ed016c44d716a4179ba6d0f532af0af5ebb04541</Sha>
</Dependency>
<Dependency Name="Microsoft.NET.ILLink.Analyzers" Version="6.0.0-alpha.1.21116.1">
<Uri>https://github.com/mono/linker</Uri>
<Sha>2cb420dbabb4ec86d41a1b431696e58f45f031cd</Sha>
<Sha>ed016c44d716a4179ba6d0f532af0af5ebb04541</Sha>
</Dependency>
<Dependency Name="System.CodeDom" Version="6.0.0-preview.2.21115.2">
<Uri>https://github.com/dotnet/runtime</Uri>
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
</PropertyGroup>
<PropertyGroup>
<!-- Dependencies from https://github.com/mono/linker -->
<MicrosoftNETILLinkTasksPackageVersion>6.0.0-alpha.1.21116.1</MicrosoftNETILLinkTasksPackageVersion>
<MicrosoftNETILLinkTasksPackageVersion>6.0.100-preview.2.21122.1</MicrosoftNETILLinkTasksPackageVersion>
<MicrosoftNETILLinkAnalyzerPackageVersion>$(MicrosoftNETILLinkTasksPackageVersion)</MicrosoftNETILLinkAnalyzerPackageVersion>
</PropertyGroup>
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ Copyright (c) .NET Foundation. All rights reserved.
<CustomResourceTypesSupport>false</CustomResourceTypesSupport>
</PropertyGroup>

<PropertyGroup Condition=" '$(PublishTrimmed)' == 'true' ">
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' ">true</SuppressTrimAnalysisWarnings>
</PropertyGroup>

<!-- Suppress warnings produced by the linker or by the ILLink Roslyn analyzer. Warnings produced
exclusively by the linker should be set in PrepareForILLink, to avoid polluting the global NoWarn list. -->
<PropertyGroup Condition=" '$(PublishTrimmed)' == 'true' and '$(SuppressTrimAnalysisWarnings)' == 'true' ">
<!-- RequiresUnreferenceCodeAttribute method called -->
<NoWarn>$(NoWarn);IL2026</NoWarn>
</PropertyGroup>

<!--
============================================================
ILLink
Expand Down Expand Up @@ -166,14 +177,13 @@ Copyright (c) .NET Foundation. All rights reserved.
$([MSBuild]::VersionGreaterThanOrEquals($(EffectiveAnalysisLevel), '5.0')) ">5</ILLinkWarningLevel>
<ILLinkWarningLevel Condition=" '$(ILLinkWarningLevel)' == '' ">0</ILLinkWarningLevel>
<ILLinkTreatWarningsAsErrors Condition=" '$(ILLinkTreatWarningsAsErrors)' == '' ">$(TreatWarningsAsErrors)</ILLinkTreatWarningsAsErrors>
<SuppressTrimAnalysisWarnings Condition=" '$(SuppressTrimAnalysisWarnings)' == '' ">true</SuppressTrimAnalysisWarnings>
<_ExtraTrimmerArgs>--skip-unresolved true $(_ExtraTrimmerArgs)</_ExtraTrimmerArgs>
<TrimMode Condition=" '$(TrimMode)' == '' ">copyused</TrimMode>
</PropertyGroup>

<!-- Suppress warnings produced by the linker. Any warnings shared with the analyzer should be
moved to the global PropertyGroup above. -->
<PropertyGroup Condition=" '$(SuppressTrimAnalysisWarnings)' == 'true' ">
<!-- RequiresUnreferenceCodeAttribute method called -->
<NoWarn>$(NoWarn);IL2026</NoWarn>
<!-- Invalid use of DynamicallyAccessedMembersAttribute -->
<NoWarn>$(NoWarn);IL2041;IL2042;IL2043;IL2056</NoWarn>
<!-- Reference to removed attribute type -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,27 @@ public void ILLink_analyzer_warnings_are_produced(string targetFramework)
var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
publishCommand
.Execute(RuntimeIdentifier)
.Should()
.HaveStdOutContaining("(8,13): warning IL3000")
.And.HaveStdOutContaining("(9,13): warning IL3001");
.Should().Pass()
.And.HaveStdOutContaining("(9,13): warning IL3000")
.And.HaveStdOutContaining("(10,13): warning IL3001");
}

[RequiresMSBuildVersionTheory("16.8.0")]
[InlineData("net6.0")]
public void ILLink_linker_analyzer_warnings_are_not_produced(string targetFramework)
{
var projectName = "ILLinkAnalyzerWarningsApp";
var testProject = CreateTestProjectWithAnalyzerWarnings(targetFramework, projectName, true);
// Inactive linker settings should have no effect on the linker analyzer,
// unless PublishTrimmed is also set.
testProject.AdditionalProperties["SuppressTrimAnalysisWarnings"] = "false";
var testAsset = _testAssetsManager.CreateTestProject(testProject);

var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
publishCommand
.Execute(RuntimeIdentifier)
.Should().Pass()
.And.NotHaveStdOutContaining("IL2026");
}

private TestProject CreateTestProjectWithAnalyzerWarnings(string targetFramework, string projectName, bool isExecutable)
Expand All @@ -526,13 +544,20 @@ private TestProject CreateTestProjectWithAnalyzerWarnings(string targetFramework

testProject.SourceFiles[$"{projectName}.cs"] = @"
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
class C
{
static void Main()
{
var a = Assembly.LoadFrom(""/some/path/not/in/bundle"");
_ = a.Location;
_ = a.GetFiles();
ProduceLinkerAnalysisWarning();
}
[RequiresUnreferencedCode(""Linker analysis warning"")]
static void ProduceLinkerAnalysisWarning()
{
}
}";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@ public void ILLink_analysis_warnings_are_disabled_by_default(string targetFramew
var rid = EnvironmentInfo.GetCompatibleRid(targetFramework);

var testProject = CreateTestProjectWithAnalysisWarnings(targetFramework, projectName);
testProject.AdditionalProperties["PublishTrimmed"] = "true";
var testAsset = _testAssetsManager.CreateTestProject(testProject);

var publishCommand = new PublishCommand(testAsset);
publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true", "/p:PublishTrimmed=true")
publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true")
.Should().Pass()
// trim analysis warnings are disabled
.And.NotHaveStdOutContaining("warning IL2075")
Expand All @@ -238,10 +239,12 @@ public void ILLink_accepts_option_to_enable_analysis_warnings(string targetFrame
var rid = EnvironmentInfo.GetCompatibleRid(targetFramework);

var testProject = CreateTestProjectWithAnalysisWarnings(targetFramework, projectName);
testProject.AdditionalProperties["PublishTrimmed"] = "true";
testProject.AdditionalProperties["SuppressTrimAnalysisWarnings"] = "false";
var testAsset = _testAssetsManager.CreateTestProject(testProject);

var publishCommand = new PublishCommand(testAsset);
publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true", "/p:PublishTrimmed=true", "/p:SuppressTrimAnalysisWarnings=false")
publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true")
.Should().Pass()
.And.HaveStdOutMatching("warning IL2075.*Program.IL_2075")
.And.HaveStdOutMatching("warning IL2026.*Program.IL_2026.*Testing analysis warning IL2026")
Expand Down Expand Up @@ -776,9 +779,11 @@ public void ILLink_can_treat_warnings_not_as_errors(string targetFramework)

var publishCommand = new PublishCommand(testAsset);
publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true", "/p:PublishTrimmed=true", "/p:SuppressTrimAnalysisWarnings=false",
"/p:TreatWarningsAsErrors=true", "/p:WarningsNotAsErrors=IL2075")
"/p:TreatWarningsAsErrors=true", "/p:WarningsNotAsErrors=\"IL2075;IL2026\"")
.Should().Fail()
.And.HaveStdOutContaining("error IL2026")
// This warning is produced by both the analyzer and the linker. Don't make it an error for the test.
.And.HaveStdOutContaining("warning IL2026")
.And.HaveStdOutContaining("error IL2043")
.And.HaveStdOutContaining("warning IL2075");
}

Expand Down Expand Up @@ -847,8 +852,10 @@ public void ILLink_can_treat_warnings_as_errors_independently(string targetFrame

var publishCommand = new PublishCommand(testAsset);
publishCommand.Execute($"/p:RuntimeIdentifier={rid}", $"/p:SelfContained=true", "/p:PublishTrimmed=true", "/p:SuppressTrimAnalysisWarnings=false",
"/p:TreatWarningsAsErrors=true", "/p:ILLinkTreatWarningsAsErrors=false")
"/p:TreatWarningsAsErrors=true", "/p:ILLinkTreatWarningsAsErrors=false", "/p:NoWarn=IL2026")
.Should().Pass()
// This warning is produced by both the analyzer and the linker. Ignore it for this test.
.And.NotHaveStdOutContaining("IL2026")
.And.HaveStdOutContaining("warning IL2075");
}

Expand Down

0 comments on commit 6661105

Please sign in to comment.