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

Update analyzer to correctly handle collection marshallers #1299

Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -615,6 +615,197 @@ await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(NativeTypeMustHaveRequiredShapeRule).WithLocation(0).WithArguments("Native", "S"));
}

[Fact]
public async Task CollectionNativeTypeWithNoMarshallingMethods_ReportsDiagnostic()
{
string source = @"
using System;
using System.Runtime.InteropServices;

[NativeMarshalling(typeof(Native))]
class S
{
public byte c;
}

[GenericContiguousCollectionMarshaller]
struct {|#0:Native|}
{
}";

await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(CollectionNativeTypeMustHaveRequiredShapeRule).WithLocation(0).WithArguments("Native", "S"));
}

[Fact]
public async Task CollectionNativeTypeWithWrongConstructor_ReportsDiagnostic()
{
string source = @"
using System;
using System.Runtime.InteropServices;

[NativeMarshalling(typeof(Native))]
class S
{
public byte c;
}

[GenericContiguousCollectionMarshaller]
ref struct {|#0:Native|}
{
public Native(S s) : this() {}

public Span<int> ManagedValues { get; set; }
public Span<byte> NativeValueStorage { get; set; }

public IntPtr Value { get; }
}";

await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(CollectionNativeTypeMustHaveRequiredShapeRule).WithLocation(0).WithArguments("Native", "S"));
}

[Fact]
public async Task CollectionNativeTypeWithCorrectConstructor_DoesNotReportDiagnostic()
{
string source = @"
using System;
using System.Runtime.InteropServices;

[NativeMarshalling(typeof(Native))]
class S
{
public byte c;
}

[GenericContiguousCollectionMarshaller]
ref struct Native
{
public Native(S s, int nativeElementSize) : this() {}

public Span<int> ManagedValues { get; set; }
public Span<byte> NativeValueStorage { get; set; }

public IntPtr Value { get; }
}";

await VerifyCS.VerifyAnalyzerAsync(source);
}

[Fact]
public async Task CollectionNativeTypeWithIncorrectStackallocConstructor_ReportsDiagnostic()
{
string source = @"
using System;
using System.Runtime.InteropServices;

[NativeMarshalling(typeof(Native))]
class S
{
public byte c;
}

[GenericContiguousCollectionMarshaller]
ref struct {|#0:Native|}
{
public Native(S s, Span<byte> stackSpace) : this() {}

public const int StackBufferSize = 1;

public Span<int> ManagedValues { get; set; }
public Span<byte> NativeValueStorage { get; set; }

public IntPtr Value { get; }
}";

await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(CollectionNativeTypeMustHaveRequiredShapeRule).WithLocation(0).WithArguments("Native", "S"));
}

[Fact]
public async Task CollectionNativeTypeWithOnlyStackallocConstructor_ReportsDiagnostic()
{
string source = @"
using System;
using System.Runtime.InteropServices;

[NativeMarshalling(typeof(Native))]
class S
{
public byte c;
}

[GenericContiguousCollectionMarshaller]
ref struct {|#0:Native|}
{
public Native(S s, Span<byte> stackSpace, int nativeElementSize) : this() {}

public const int StackBufferSize = 1;

public Span<int> ManagedValues { get; set; }
public Span<byte> NativeValueStorage { get; set; }

public IntPtr Value { get; }
}";

await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(StackallocMarshallingShouldSupportAllocatingMarshallingFallbackRule).WithLocation(0).WithArguments("Native", "S"));
}

[Fact]
public async Task CollectionNativeTypeWithMissingManagedValuesProperty_ReportsDiagnostic()
{
string source = @"
using System;
using System.Runtime.InteropServices;

[NativeMarshalling(typeof(Native))]
class S
{
public byte c;
}

[GenericContiguousCollectionMarshaller]
ref struct {|#0:Native|}
{
public Native(S s, int nativeElementSize) : this() {}

public Span<byte> NativeValueStorage { get; set; }

public IntPtr Value { get; }
}";

await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(CollectionNativeTypeMustHaveRequiredShapeRule).WithLocation(0).WithArguments("Native", "S"));
}

[Fact]
public async Task CollectionNativeTypeWithMissingNativeValueStorageProperty_ReportsDiagnostic()
{
string source = @"
using System;
using System.Runtime.InteropServices;

[NativeMarshalling(typeof(Native))]
class S
{
public byte c;
}

[GenericContiguousCollectionMarshaller]
ref struct {|#0:Native|}
{
public Native(S s, int nativeElementSize) : this() {}

public Span<int> ManagedValues { get; set; }

public IntPtr Value { get; }
}";

await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(CollectionNativeTypeMustHaveRequiredShapeRule).WithLocation(0).WithArguments("Native", "S"));
}

[Fact]
public async Task NativeTypeWithOnlyConstructor_DoesNotReportDiagnostic()
{
Expand Down Expand Up @@ -1005,7 +1196,7 @@ public Native(S s)
}

[Fact]
public async Task UninstantiatedGenericNativeType_ReportsDiagnostic()
public async Task UninstantiatedGenericNativeTypeOnNonGeneric_ReportsDiagnostic()
{

string source = @"
Expand All @@ -1029,7 +1220,61 @@ public Native(S s)

public T Value { get; set; }
}";
await VerifyCS.VerifyAnalyzerAsync(source, VerifyCS.Diagnostic(NativeGenericTypeMustBeClosedRule).WithLocation(0).WithArguments("Native<>", "S"));
await VerifyCS.VerifyAnalyzerAsync(source, VerifyCS.Diagnostic(NativeGenericTypeMustBeClosedOrMatchArityRule).WithLocation(0).WithArguments("Native<>", "S"));
}

[Fact]
public async Task UninstantiatedGenericNativeTypeOnGenericWithArityMismatch_ReportsDiagnostic()
{
string source = @"
using System.Runtime.InteropServices;

[{|#0:NativeMarshalling(typeof(Native<,>))|}]
struct S<T>
{
public string s;
}

struct Native<T, U>
where T : new()
{
public Native(S<T> s)
{
Value = 0;
}

public S<T> ToManaged() => new S<T>();

public int Value { get; set; }
}";
await VerifyCS.VerifyAnalyzerAsync(source, VerifyCS.Diagnostic(NativeGenericTypeMustBeClosedOrMatchArityRule).WithLocation(0).WithArguments("Native<,>", "S<T>"));
}

[Fact]
public async Task UninstantiatedGenericNativeTypeOnGenericWithArityMatch_DoesNotReportDiagnostic()
{
string source = @"
using System.Runtime.InteropServices;

[NativeMarshalling(typeof(Native<>))]
struct S<T>
{
public T t;
}

struct Native<T>
where T : new()
{
public Native(S<T> s)
{
Value = 0;
}

public S<T> ToManaged() => new S<T>();

public int Value { get; set; }
}";
await VerifyCS.VerifyAnalyzerAsync(source);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static class Ids
public const string StackallocMarshallingShouldSupportAllocatingMarshallingFallback = Prefix + "011";
public const string StackallocConstructorMustHaveStackBufferSizeConstant = Prefix + "012";
public const string RefValuePropertyUnsupported = Prefix + "014";
public const string NativeGenericTypeMustBeClosed = Prefix + "016";
public const string NativeGenericTypeMustBeClosedOrMatchArity = Prefix + "016";

// GeneratedDllImport
public const string GeneratedDllImportMissingRequiredModifiers = Prefix + "013";
Expand Down
Loading