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

Various refactorings to share more code with future generators #67997

Merged
merged 19 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
28 changes: 14 additions & 14 deletions docs/project/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ The diagnostic id values reserved for .NET Libraries analyzer warnings are `SYSL
| __`SYSLIB1050`__ | Invalid LibraryImportAttribute usage |
| __`SYSLIB1051`__ | Specified type is not supported by source-generated P/Invokes |
| __`SYSLIB1052`__ | Specified configuration is not supported by source-generated P/Invokes |
| __`SYSLIB1053`__ | Current target framework is not supported by source-generated P/Invokes |
| __`SYSLIB1054`__ | Specified LibraryImportAttribute arguments cannot be forwarded to DllImportAttribute |
| __`SYSLIB1055`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1056`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1057`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1058`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1059`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1060`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1061`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1062`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1063`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1064`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1065`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1066`__ | *_`SYSLIB1055`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1053`__ | Specified LibraryImportAttribute arguments cannot be forwarded to DllImportAttribute |
| __`SYSLIB1054`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1055`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1056`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1057`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1058`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1059`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1060`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1061`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1062`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1063`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1064`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1065`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
| __`SYSLIB1066`__ | *_`SYSLIB1054`-`SYSLIB1066` reserved for Microsoft.Interop.LibraryImportGenerator._* |
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.ComponentModel;

#if MICROSOFT_INTEROP_SOURCEGENERATION
namespace Microsoft.Interop
#else
namespace System.Runtime.InteropServices
#endif
{
/// <summary>
/// A direction of marshalling data into or out of the managed environment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;

#if MICROSOFT_INTEROP_SOURCEGENERATION
namespace Microsoft.Interop
#else
namespace System.Runtime.InteropServices
#endif
{
/// <summary>
/// Optional features supported by custom type marshallers.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if MICROSOFT_INTEROP_SOURCEGENERATION
namespace Microsoft.Interop
#else
namespace System.Runtime.InteropServices
#endif
{
/// <summary>
/// The shape of a custom type marshaller for usage in source-generated interop scenarios.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
//
// Types in this file are used for generated p/invokes
//
#if MICROSOFT_INTEROP_SOURCEGENERATION
namespace Microsoft.Interop
#else
namespace System.Runtime.InteropServices
#endif
{
/// <summary>
/// Specifies how strings should be marshalled for generated p/invokes
/// </summary>
#if SYSTEM_PRIVATE_CORELIB
#if SYSTEM_PRIVATE_CORELIB || MICROSOFT_INTEROP_SOURCEGENERATION
public
#else
internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,10 @@ namespace Microsoft.Interop
{
internal static class Comparers
{
/// <summary>
/// Comparer for the set of all of the generated stubs and diagnostics generated for each of them.
/// </summary>
public static readonly IEqualityComparer<ImmutableArray<(string, ImmutableArray<Diagnostic>)>> GeneratedSourceSet = new ImmutableArraySequenceEqualComparer<(string, ImmutableArray<Diagnostic>)>(new CustomValueTupleElementComparer<string, ImmutableArray<Diagnostic>>(EqualityComparer<string>.Default, new ImmutableArraySequenceEqualComparer<Diagnostic>(EqualityComparer<Diagnostic>.Default)));

/// <summary>
/// Comparer for an individual generated stub source as a string and the generated diagnostics for the stub.
/// </summary>
public static readonly IEqualityComparer<(string, ImmutableArray<Diagnostic>)> GeneratedSource = new CustomValueTupleElementComparer<string, ImmutableArray<Diagnostic>>(EqualityComparer<string>.Default, new ImmutableArraySequenceEqualComparer<Diagnostic>(EqualityComparer<Diagnostic>.Default));

/// <summary>
/// Comparer for an individual generated stub source as a syntax tree and the generated diagnostics for the stub.
/// </summary>
public static readonly IEqualityComparer<(MemberDeclarationSyntax Syntax, ImmutableArray<Diagnostic> Diagnostics)> GeneratedSyntax = new CustomValueTupleElementComparer<MemberDeclarationSyntax, ImmutableArray<Diagnostic>>(SyntaxEquivalentComparer.Instance, new ImmutableArraySequenceEqualComparer<Diagnostic>(EqualityComparer<Diagnostic>.Default));

/// <summary>
/// Comparer for the context used to generate a stub and the original user-provided syntax that triggered stub creation.
/// </summary>
public static readonly IEqualityComparer<(MethodDeclarationSyntax Syntax, LibraryImportGenerator.IncrementalStubGenerationContext StubContext)> CalculatedContextWithSyntax = new CustomValueTupleElementComparer<MethodDeclarationSyntax, LibraryImportGenerator.IncrementalStubGenerationContext>(SyntaxEquivalentComparer.Instance, EqualityComparer<LibraryImportGenerator.IncrementalStubGenerationContext>.Default);
}

/// <summary>
Expand Down Expand Up @@ -61,23 +46,6 @@ public int GetHashCode(ImmutableArray<T> obj)
}
}

internal sealed class SyntaxEquivalentComparer : IEqualityComparer<SyntaxNode>
{
public static readonly SyntaxEquivalentComparer Instance = new();

private SyntaxEquivalentComparer() { }

public bool Equals(SyntaxNode x, SyntaxNode y)
{
return x.IsEquivalentTo(y);
}

public int GetHashCode(SyntaxNode obj)
{
throw new UnreachableException();
}
}

internal sealed class CustomValueTupleElementComparer<T, U> : IEqualityComparer<(T, U)>
{
private readonly IEqualityComparer<T> _item1Comparer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public class Ids
public const string InvalidLibraryImportAttributeUsage = Prefix + "1050";
public const string TypeNotSupported = Prefix + "1051";
public const string ConfigurationNotSupported = Prefix + "1052";
public const string TargetFrameworkNotSupported = Prefix + "1053";
public const string CannotForwardToDllImport = Prefix + "1054";
public const string CannotForwardToDllImport = Prefix + "1053";
}

private const string Category = "LibraryImportGenerator";
Expand Down Expand Up @@ -148,16 +147,6 @@ public class Ids
isEnabledByDefault: true,
description: GetResourceString(nameof(SR.ConfigurationNotSupportedDescription)));

public static readonly DiagnosticDescriptor TargetFrameworkNotSupported =
new DiagnosticDescriptor(
Ids.TargetFrameworkNotSupported,
GetResourceString(nameof(SR.TargetFrameworkNotSupportedTitle)),
GetResourceString(nameof(SR.TargetFrameworkNotSupportedMessage)),
Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: GetResourceString(nameof(SR.TargetFrameworkNotSupportedDescription)));

public static readonly DiagnosticDescriptor CannotForwardToDllImport =
new DiagnosticDescriptor(
Ids.CannotForwardToDllImport,
Expand Down Expand Up @@ -221,11 +210,11 @@ public void ReportConfigurationNotSupported(
/// <summary>
/// Report diagnostic for marshalling of a parameter/return that is not supported
/// </summary>
/// <param name="method">Method with the parameter/return</param>
/// <param name="diagnosticLocations">Method with the parameter/return</param>
/// <param name="info">Type info for the parameter/return</param>
/// <param name="notSupportedDetails">[Optional] Specific reason for lack of support</param>
public void ReportMarshallingNotSupported(
MethodDeclarationSyntax method,
MethodSignatureDiagnosticLocations diagnosticLocations,
TypePositionInfo info,
string? notSupportedDetails,
ImmutableDictionary<string, string> diagnosticProperties)
Expand All @@ -235,15 +224,14 @@ public void ReportMarshallingNotSupported(

if (info.IsManagedReturnPosition)
{
diagnosticLocation = Location.Create(method.SyntaxTree, method.Identifier.Span);
elementName = method.Identifier.ValueText;
diagnosticLocation = diagnosticLocations.FallbackLocation;
elementName = diagnosticLocations.MethodIdentifier;
}
else
{
Debug.Assert(info.ManagedIndex <= method.ParameterList.Parameters.Count);
ParameterSyntax param = method.ParameterList.Parameters[info.ManagedIndex];
diagnosticLocation = Location.Create(param.SyntaxTree, param.Identifier.Span);
elementName = param.Identifier.ValueText;
Debug.Assert(info.ManagedIndex <= diagnosticLocations.ManagedParameterLocations.Length);
diagnosticLocation = diagnosticLocations.ManagedParameterLocations[info.ManagedIndex];
elementName = info.InstanceIdentifier;
}

if (!string.IsNullOrEmpty(notSupportedDetails))
Expand Down Expand Up @@ -327,31 +315,18 @@ public void ReportInvalidMarshallingAttributeInfo(
new LocalizableResourceString(reasonResourceName, SR.ResourceManager, typeof(FxResources.Microsoft.Interop.LibraryImportGenerator.SR), reasonArgs)));
}

/// <summary>
/// Report diagnostic for targeting a framework that is not supported
/// </summary>
/// <param name="minimumSupportedVersion">Minimum supported version of .NET</param>
public void ReportTargetFrameworkNotSupported(Version minimumSupportedVersion)
{
_diagnostics.Add(
Diagnostic.Create(
TargetFrameworkNotSupported,
Location.None,
minimumSupportedVersion.ToString(2)));
}

/// <summary>
/// Report diagnostic for configuration that cannot be forwarded to <see cref="DllImportAttribute" />
/// </summary>
/// <param name="method">Method with the configuration that cannot be forwarded</param>
/// <param name="name">Configuration name</param>
/// <param name="value">Configuration value</param>
public void ReportCannotForwardToDllImport(MethodDeclarationSyntax method, string name, string? value = null)
public void ReportCannotForwardToDllImport(MethodSignatureDiagnosticLocations method, string name, string? value = null)
{
_diagnostics.Add(
Diagnostic.Create(
CannotForwardToDllImport,
Location.Create(method.SyntaxTree, method.Identifier.Span),
method.FallbackLocation,
value is null ? name : $"{name}={value}"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,11 @@

namespace Microsoft.Interop
{
/// <summary>
/// Flags used to indicate members on LibraryImport attribute.
/// </summary>
[Flags]
public enum LibraryImportMember
{
None = 0,
EntryPoint = 1 << 0,
SetLastError = 1 << 1,
StringMarshalling = 1 << 2,
StringMarshallingCustomType = 1 << 3,
All = ~None
}

/// <summary>
/// LibraryImportAttribute data
/// </summary>
internal sealed record LibraryImportData(string ModuleName)
internal sealed record LibraryImportData(string ModuleName) : InteropAttributeData
{
/// <summary>
/// Value set by the user on the original declaration.
/// </summary>
public LibraryImportMember IsUserDefined { get; init; }
public string? EntryPoint { get; init; }
public bool SetLastError { get; init; }
public StringMarshalling StringMarshalling { get; init; }
public INamedTypeSymbol? StringMarshallingCustomType { get; init; }
public string EntryPoint { get; init; }
}
}
Loading