Skip to content

Commit 05d6ef2

Browse files
Add the struct marshalling attributes to Ancillary.Interop and add an analyzer that validates manual usage. (#61)
Co-authored-by: Elinor Fung <[email protected]>
1 parent e50c8c2 commit 05d6ef2

14 files changed

+2104
-19
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#nullable enable
2+
3+
namespace System.Runtime.InteropServices
4+
{
5+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
6+
class GeneratedMarshallingAttribute : Attribute
7+
{
8+
}
9+
10+
[AttributeUsage(AttributeTargets.Struct)]
11+
public class BlittableTypeAttribute : Attribute
12+
{
13+
}
14+
15+
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class)]
16+
public class NativeMarshallingAttribute : Attribute
17+
{
18+
public NativeMarshallingAttribute(Type nativeType)
19+
{
20+
NativeType = nativeType;
21+
}
22+
23+
public Type NativeType { get; }
24+
}
25+
26+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.Field)]
27+
public class MarshalUsingAttribute : Attribute
28+
{
29+
public MarshalUsingAttribute(Type nativeType)
30+
{
31+
NativeType = nativeType;
32+
}
33+
34+
public Type NativeType { get; }
35+
}
36+
}

DllImportGenerator/DllImportGenerator.Test/CompileFails.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34
using Xunit;
45

56
namespace DllImportGenerator.Test
@@ -13,9 +14,9 @@ public static IEnumerable<object[]> CodeSnippetsToCompile()
1314

1415
[Theory]
1516
[MemberData(nameof(CodeSnippetsToCompile))]
16-
public void ValidateSnippets(string source, int failCount)
17+
public async Task ValidateSnippets(string source, int failCount)
1718
{
18-
Compilation comp = TestUtils.CreateCompilation(source);
19+
Compilation comp = await TestUtils.CreateCompilation(source);
1920
TestUtils.AssertPreSourceGeneratorCompilation(comp);
2021

2122
var newComp = TestUtils.RunGenerators(comp, out var generatorDiags, new Microsoft.Interop.DllImportGenerator());

DllImportGenerator/DllImportGenerator.Test/Compiles.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34
using Xunit;
45

56
namespace DllImportGenerator.Test
@@ -23,9 +24,9 @@ public static IEnumerable<object[]> CodeSnippetsToCompile()
2324

2425
[Theory]
2526
[MemberData(nameof(CodeSnippetsToCompile))]
26-
public void ValidateSnippets(string source)
27+
public async Task ValidateSnippets(string source)
2728
{
28-
Compilation comp = TestUtils.CreateCompilation(source);
29+
Compilation comp = await TestUtils.CreateCompilation(source);
2930
TestUtils.AssertPreSourceGeneratorCompilation(comp);
3031

3132
var newComp = TestUtils.RunGenerators(comp, out var generatorDiags, new Microsoft.Interop.DllImportGenerator());

DllImportGenerator/DllImportGenerator.Test/DllImportGenerator.Test.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<ItemGroup>
1010
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.7.0-3.final" PrivateAssets="all" />
1111
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.0-beta1.final" PrivateAssets="all" />
12+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.0.1-beta1.20418.1" PrivateAssets="all" />
1213
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="3.7.0-3.final">
1314
<PrivateAssets>all</PrivateAssets>
1415
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -29,5 +30,8 @@
2930
<ProjectReference Include="..\Ancillary.Interop\Ancillary.Interop.csproj" />
3031
<ProjectReference Include="..\DllImportGenerator\DllImportGenerator.csproj" />
3132
</ItemGroup>
32-
33+
34+
<PropertyGroup>
35+
<RestoreAdditionalProjectSources>https://dotnet.myget.org/F/roslyn-analyzers/api/v3/index.json ;$(RestoreAdditionalProjectSources)</RestoreAdditionalProjectSources>
36+
</PropertyGroup>
3337
</Project>

0 commit comments

Comments
 (0)