Skip to content

Ensure a typedef to a typedef union Name works with C #390

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

Merged
merged 1 commit into from
Sep 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
4 changes: 4 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3094,6 +3094,10 @@ private string GetTypeName(Cursor? cursor, Cursor? context, Type rootType, Type
{
result.typeName = result.typeName[7..];
}
else if (result.typeName.StartsWith("union "))
{
result.typeName = result.typeName[6..];
}
}

if (result.typeName.Contains("::"))
Expand Down
75 changes: 75 additions & 0 deletions tests/ClangSharp.PInvokeGenerator.UnitTests/CTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,79 @@ public partial struct MyStruct

return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, commandlineArgs: DefaultCClangCommandLineArgs);
}

[Test]
public Task StructTest()
{
var inputContents = @"typedef struct _MyStruct
{
int _field;
} MyStruct;

typedef struct _MyOtherStruct
{
MyStruct _field1;
MyStruct* _field2;
} MyOtherStruct;
";
var expectedOutputContents = @"namespace ClangSharp.Test
{
public partial struct _MyStruct
{
public int _field;
}

public unsafe partial struct _MyOtherStruct
{
[NativeTypeName(""MyStruct"")]
public _MyStruct _field1;

[NativeTypeName(""MyStruct *"")]
public _MyStruct* _field2;
}
}
";
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, commandlineArgs: DefaultCClangCommandLineArgs);
}

[Test]
public Task UnionTest()
{
var inputContents = @"typedef union _MyUnion
{
int _field;
} MyUnion;

typedef union _MyOtherUnion
{
MyUnion _field1;
MyUnion* _field2;
} MyOtherUnion;
";
var expectedOutputContents = @"using System.Runtime.InteropServices;

namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Explicit)]
public partial struct _MyUnion
{
[FieldOffset(0)]
public int _field;
}

[StructLayout(LayoutKind.Explicit)]
public unsafe partial struct _MyOtherUnion
{
[FieldOffset(0)]
[NativeTypeName(""MyUnion"")]
public _MyUnion _field1;

[FieldOffset(0)]
[NativeTypeName(""MyUnion *"")]
public _MyUnion* _field2;
}
}
";
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, commandlineArgs: DefaultCClangCommandLineArgs);
}
}