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

Forward DefaultDllImportSearchPathsAttribute to the inner P/Invoke. #65458

Merged
merged 1 commit into from
Feb 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
});
}

private static List<AttributeSyntax> GenerateSyntaxForForwardedAttributes(AttributeData? suppressGCTransitionAttribute, AttributeData? unmanagedCallConvAttribute)
private static List<AttributeSyntax> GenerateSyntaxForForwardedAttributes(AttributeData? suppressGCTransitionAttribute, AttributeData? unmanagedCallConvAttribute, AttributeData? defaultDllImportSearchPathsAttribute)
{
const string CallConvsField = "CallConvs";
// Manually rehydrate the forwarded attributes with fully qualified types so we don't have to worry about any using directives.
Expand Down Expand Up @@ -232,6 +232,15 @@ private static List<AttributeSyntax> GenerateSyntaxForForwardedAttributes(Attrib
}
attributes.Add(unmanagedCallConvSyntax);
}
if (defaultDllImportSearchPathsAttribute is not null)
{
attributes.Add(
Attribute(ParseName(TypeNames.DefaultDllImportSearchPathsAttribute)).AddArgumentListArguments(
AttributeArgument(
CastExpression(ParseTypeName(TypeNames.DllImportSearchPath),
LiteralExpression(SyntaxKind.NumericLiteralExpression,
Literal((int)defaultDllImportSearchPathsAttribute.ConstructorArguments[0].Value!))))));
}
return attributes;
}

Expand Down Expand Up @@ -392,30 +401,36 @@ private static IncrementalStubGenerationContext CalculateStubInformation(IMethod
INamedTypeSymbol? lcidConversionAttrType = environment.Compilation.GetTypeByMetadataName(TypeNames.LCIDConversionAttribute);
INamedTypeSymbol? suppressGCTransitionAttrType = environment.Compilation.GetTypeByMetadataName(TypeNames.SuppressGCTransitionAttribute);
INamedTypeSymbol? unmanagedCallConvAttrType = environment.Compilation.GetTypeByMetadataName(TypeNames.UnmanagedCallConvAttribute);
INamedTypeSymbol? defaultDllImportSearchPathsAttrType = environment.Compilation.GetTypeByMetadataName(TypeNames.DefaultDllImportSearchPathsAttribute);
// Get any attributes of interest on the method
AttributeData? generatedDllImportAttr = null;
AttributeData? lcidConversionAttr = null;
AttributeData? suppressGCTransitionAttribute = null;
AttributeData? unmanagedCallConvAttribute = null;
AttributeData? defaultDllImportSearchPathsAttribute = null;
foreach (AttributeData attr in symbol.GetAttributes())
{
if (attr.AttributeClass is not null
&& attr.AttributeClass.ToDisplayString() == TypeNames.GeneratedDllImportAttribute)
{
generatedDllImportAttr = attr;
}
else if (lcidConversionAttrType != null && SymbolEqualityComparer.Default.Equals(attr.AttributeClass, lcidConversionAttrType))
else if (lcidConversionAttrType is not null && SymbolEqualityComparer.Default.Equals(attr.AttributeClass, lcidConversionAttrType))
{
lcidConversionAttr = attr;
}
else if (suppressGCTransitionAttrType != null && SymbolEqualityComparer.Default.Equals(attr.AttributeClass, suppressGCTransitionAttrType))
else if (suppressGCTransitionAttrType is not null && SymbolEqualityComparer.Default.Equals(attr.AttributeClass, suppressGCTransitionAttrType))
{
suppressGCTransitionAttribute = attr;
}
else if (unmanagedCallConvAttrType != null && SymbolEqualityComparer.Default.Equals(attr.AttributeClass, unmanagedCallConvAttrType))
else if (unmanagedCallConvAttrType is not null && SymbolEqualityComparer.Default.Equals(attr.AttributeClass, unmanagedCallConvAttrType))
{
unmanagedCallConvAttribute = attr;
}
else if (defaultDllImportSearchPathsAttrType is not null && SymbolEqualityComparer.Default.Equals(attr.AttributeClass, defaultDllImportSearchPathsAttrType))
{
defaultDllImportSearchPathsAttribute = attr;
}
}

Debug.Assert(generatedDllImportAttr is not null);
Expand All @@ -434,7 +449,7 @@ private static IncrementalStubGenerationContext CalculateStubInformation(IMethod
// Create the stub.
var dllImportStub = DllImportStubContext.Create(symbol, stubDllImportData, environment, generatorDiagnostics, ct);

List<AttributeSyntax> additionalAttributes = GenerateSyntaxForForwardedAttributes(suppressGCTransitionAttribute, unmanagedCallConvAttribute);
List<AttributeSyntax> additionalAttributes = GenerateSyntaxForForwardedAttributes(suppressGCTransitionAttribute, unmanagedCallConvAttribute, defaultDllImportSearchPathsAttribute);
return new IncrementalStubGenerationContext(environment, dllImportStub, additionalAttributes.ToImmutableArray(), stubDllImportData, generatorDiagnostics.Diagnostics.ToImmutableArray());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@ public static string Unsafe(InteropGenerationOptions options)
}

public const string System_Runtime_CompilerServices_DisableRuntimeMarshallingAttribute = "System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute";
public const string DefaultDllImportSearchPathsAttribute = "System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute";
public const string DllImportSearchPath = "System.Runtime.InteropServices.DllImportSearchPath";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,50 @@ public Native(S s) { }
callConvType2));
}

[ConditionalFact]
public async Task DefaultDllImportSearchPathsAttribute()
{
string source = @$"
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly:DisableRuntimeMarshalling]
partial class C
{{
[DefaultDllImportSearchPaths(DllImportSearchPath.System32 | DllImportSearchPath.UserDirectories)]
[GeneratedDllImportAttribute(""DoesNotExist"")]
public static partial S Method1();
}}

[NativeMarshalling(typeof(Native))]
struct S
{{
}}

struct Native
{{
public Native(S s) {{ }}
public S ToManaged() {{ return default; }}
}}
";
Compilation origComp = await TestUtils.CreateCompilation(source);
Compilation newComp = TestUtils.RunGenerators(origComp, out _, new Microsoft.Interop.DllImportGenerator());
Assert.Empty(newComp.GetDiagnostics());

ITypeSymbol attributeType = newComp.GetTypeByMetadataName("System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute")!;

Assert.NotNull(attributeType);

IMethodSymbol targetMethod = GetGeneratedPInvokeTargetFromCompilation(newComp);

DllImportSearchPath expected = DllImportSearchPath.System32 | DllImportSearchPath.UserDirectories;

Assert.Contains(
targetMethod.GetAttributes(),
attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, attributeType)
&& attr.ConstructorArguments.Length == 1
&& expected == (DllImportSearchPath)attr.ConstructorArguments[0].Value!);
}

[ConditionalFact]
public async Task OtherAttributeType()
{
Expand Down