Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analyzer bootstrap phase #2540

Merged
merged 4 commits into from
Jan 25, 2022
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
2 changes: 1 addition & 1 deletion build.cmd
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@echo off
powershell -ExecutionPolicy ByPass -NoProfile -command "& """%~dp0eng\common\Build.ps1""" -restore -build %*"
powershell -ExecutionPolicy ByPass -NoProfile -command "& """%~dp0eng\Build.ps1""" -restore -build %*"
12 changes: 12 additions & 0 deletions eng/Analyzers.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="$(MicrosoftCodeAnalysisCSharpCodeStyleVersion)" PrivateAssets="all" />
<!-- <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.205" PrivateAssets="all" /> -->
</ItemGroup>

<Import Project="$(BootstrapBuildPath)/Microsoft.NET.ILLink.Analyzers.props" Condition="'$(BootstrapBuildPath)' != ''" />

<!-- Don't enable for Cecil, as they can't be suppressed -->
<PropertyGroup Condition="'$(BootstrapBuildPath)' != '' and '$(MSBuildProjectName)' != 'Mono.Cecil.Pdb'">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why just Mono.Cecil.Pdb and not the other cecil projects (mainly the Mono.Cecil itself)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mono.Cecil.PDB has a warning (COM) that I need to file a bug for.

We also can't disable it because it sets <NoWarn> without using $(NoWarn) inside, so it overrides the higher warnings. None of the other projects do, so we can temporarily disable warnings for them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could push a change to the project to correctly pick up NoWarn from the outside.
Once we have XML support it should be possible to get rid of the warning that way.

<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
</PropertyGroup>

<ItemGroup Condition="'$(BootstrapBuildPath)' != ''">
<Analyzer Include="$(BootstrapBuildPath)/ILLink.RoslynAnalyzer.dll" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions eng/bootstrap.proj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.DotNet.Arcade.Sdk">

<Target Name="Build">
<MSBuild Projects="../src/ILLink.RoslynAnalyzer/ILLink.RoslynAnalyzer.csproj"
Targets="Restore" />
<MSBuild Projects="../src/ILLink.RoslynAnalyzer/ILLink.RoslynAnalyzer.csproj"
Properties="OutDir=$(ArtifactsDir)/bootstrap"
Targets="Build" />
</Target>
</Project>
17 changes: 17 additions & 0 deletions eng/build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[CmdletBinding(PositionalBinding=$false)]
Param(
[switch] $integrationTest,
[Parameter(ValueFromRemainingArguments=$true)][String[]]$remaining
)

. (Join-Path $PSScriptRoot "common/tools.ps1")

$args = $remaining.Clone()

if ($integrationTest) {
dotnet build (Join-Path $PSScriptRoot bootstrap.proj)
$args += "-integrationTest"
$args += "/p:BootstrapBuildPath=$ArtifactsDir/bootstrap"
}

powershell -ExecutionPolicy ByPass -NoProfile (Join-Path $PSScriptRoot "common/build.ps1") @args
4 changes: 2 additions & 2 deletions src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ public override void Initialize (AnalysisContext context)
for (int i = 0; i < typeParams.Length; i++) {
var typeParam = typeParams[i];
var typeArg = typeArgs[i];
if (!typeParam.HasConstructorConstraint)
if (!typeParam.HasConstructorConstraint ||
typeArg is not INamedTypeSymbol { InstanceConstructors: { } typeArgCtors })
continue;

var typeArgCtors = ((INamedTypeSymbol) typeArg).InstanceConstructors;
foreach (var instanceCtor in typeArgCtors) {
if (instanceCtor.Arity > 0)
continue;
Expand Down
3 changes: 3 additions & 0 deletions src/linker/Linker.Steps/OutputStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@ private void OutputPInvokes ()

using (var fs = File.Open (Path.Combine (Context.OutputDirectory, Context.PInvokesListFile), FileMode.Create)) {
var values = Context.PInvokes.Distinct ().OrderBy (l => l);
// Ignore warning, since we're just enabling analyzer for dogfooding
#pragma warning disable IL2026
var jsonSerializer = new DataContractJsonSerializer (typeof (List<PInvokeInfo>));
jsonSerializer.WriteObject (fs, values);
#pragma warning restore IL2026
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/linker/Linker/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,11 @@ string Unquote (string arg)
if (File.Exists (assemblyPath)) {
// The CLR will return the already-loaded assembly if the same path is requested multiple times
// (or even if a different path specifies the "same" assembly, based on the MVID).

// Ignore warning, since we're just enabling analyzer for dogfooding
#pragma warning disable IL2026
return AssemblyLoadContext.Default.LoadFromAssemblyPath (assemblyPath);
#pragma warning restore IL2026
}
Context.LogError (null, DiagnosticId.AssemblyInCustomStepOptionCouldNotBeFound, arg);
} else
Expand Down Expand Up @@ -992,7 +996,10 @@ protected bool AddCustomStep (Pipeline pipeline, string arg)

Type? ResolveStepType (string type, Assembly assembly)
{
// Ignore warning, since we're just enabling analyzer for dogfooding
#pragma warning disable IL2026
Type? step = assembly != null ? assembly.GetType (type) : Type.GetType (type, false);
#pragma warning restore IL2026

if (step == null) {
Context.LogError (null, DiagnosticId.CustomStepTypeCouldNotBeFound, type);
Expand All @@ -1004,7 +1011,10 @@ protected bool AddCustomStep (Pipeline pipeline, string arg)

TStep? ResolveStep<TStep> (string type, Assembly assembly) where TStep : class
{
// Ignore warning, since we're just enabling analyzer for dogfooding
#pragma warning disable IL2026
Type? step = assembly != null ? assembly.GetType (type) : Type.GetType (type, false);
#pragma warning restore IL2026

if (step == null) {
Context.LogError (null, DiagnosticId.CustomStepTypeCouldNotBeFound, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -190,9 +192,13 @@ public static void GenericTypeWithStaticMethodViaLdftn ()

class TestType { }

static T MakeNew<T> () where T : new() => new T ();
static T MakeNew2<T> () where T : new() => MakeNew<T> ();

public static void Test ()
{
GenericTypeWithStaticMethodViaLdftn ();
MakeNew2<TestType> ();
}
}
}
Expand Down