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

Ensure compatible-codegen inserts "unsafe" where required #494

Merged
merged 1 commit into from
Nov 18, 2023
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
2 changes: 1 addition & 1 deletion sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5933,7 +5933,7 @@ private bool IsUnsafe(RecordDecl recordDecl)
{
return true;
}
else if ((decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymousStructOrUnion && IsUnsafe(nestedRecordDecl))
else if ((decl is RecordDecl nestedRecordDecl) && nestedRecordDecl.IsAnonymousStructOrUnion && (IsUnsafe(nestedRecordDecl) || Config.GenerateCompatibleCode))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ void MyFunction()
namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Explicit)]
public partial struct MyUnion
public unsafe partial struct MyUnion
{
[FieldOffset(0)]
[NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ public partial struct _w_e__Struct
public {expectedManagedType} value;
}}

public partial struct _Anonymous1_e__Struct
public unsafe partial struct _Anonymous1_e__Struct
{{
public {expectedManagedType} value1;

Expand Down Expand Up @@ -1318,7 +1318,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()

var expectedOutputContents = @"namespace ClangSharp.Test
{
public partial struct MyStruct
public unsafe partial struct MyStruct
{
public int x;

Expand Down Expand Up @@ -1375,7 +1375,7 @@ public int o0_b16_4
}
}

public partial struct _Anonymous_e__Struct
public unsafe partial struct _Anonymous_e__Struct
{
public int z;

Expand Down Expand Up @@ -1708,7 +1708,7 @@ protected override Task RemapNestedAnonymousTestImpl()

var expectedOutputContents = @"namespace ClangSharp.Test
{
public partial struct MyStruct
public unsafe partial struct MyStruct
{
public double r;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()
namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Explicit)]
public partial struct MyUnion
public unsafe partial struct MyUnion
{
[FieldOffset(0)]
public int x;
Expand Down Expand Up @@ -952,7 +952,7 @@ public int o0_b16_4
}

[StructLayout(LayoutKind.Explicit)]
public partial struct _Anonymous_e__Union
public unsafe partial struct _Anonymous_e__Union
{
[FieldOffset(0)]
public int z;
Expand Down Expand Up @@ -1283,7 +1283,7 @@ protected override Task RemapNestedAnonymousTestImpl()
namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Explicit)]
public partial struct MyUnion
public unsafe partial struct MyUnion
{
[FieldOffset(0)]
public double r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ void MyFunction()
namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Explicit)]
public partial struct MyUnion
public unsafe partial struct MyUnion
{
[FieldOffset(0)]
[NativeTypeName(""__AnonymousRecord_ClangUnsavedFile_L3_C5"")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ public partial struct _w_e__Struct
public {expectedManagedType} value;
}}

public partial struct _Anonymous1_e__Struct
public unsafe partial struct _Anonymous1_e__Struct
{{
public {expectedManagedType} value1;

Expand Down Expand Up @@ -1326,7 +1326,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()

var expectedOutputContents = @"namespace ClangSharp.Test
{
public partial struct MyStruct
public unsafe partial struct MyStruct
{
public int x;

Expand Down Expand Up @@ -1383,7 +1383,7 @@ public int o0_b16_4
}
}

public partial struct _Anonymous_e__Struct
public unsafe partial struct _Anonymous_e__Struct
{
public int z;

Expand Down Expand Up @@ -1716,7 +1716,7 @@ protected override Task RemapNestedAnonymousTestImpl()

var expectedOutputContents = @"namespace ClangSharp.Test
{
public partial struct MyStruct
public unsafe partial struct MyStruct
{
public double r;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()
namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Explicit)]
public partial struct MyUnion
public unsafe partial struct MyUnion
{
[FieldOffset(0)]
public int x;
Expand Down Expand Up @@ -959,7 +959,7 @@ public int o0_b16_4
}

[StructLayout(LayoutKind.Explicit)]
public partial struct _Anonymous_e__Union
public unsafe partial struct _Anonymous_e__Union
{
[FieldOffset(0)]
public int z;
Expand Down Expand Up @@ -1290,7 +1290,7 @@ protected override Task RemapNestedAnonymousTestImpl()
namespace ClangSharp.Test
{
[StructLayout(LayoutKind.Explicit)]
public partial struct MyUnion
public unsafe partial struct MyUnion
{
[FieldOffset(0)]
public double r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ void MyFunction()
var expectedOutputContents = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<struct name=""MyUnion"" access=""public"" layout=""Explicit"">
<struct name=""MyUnion"" access=""public"" unsafe=""true"" layout=""Explicit"">
<field name=""Anonymous"" access=""public"" offset=""0"">
<type native=""__AnonymousRecord_ClangUnsavedFile_L3_C5"">_Anonymous_e__Struct</type>
</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()
var expectedOutputContents = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<struct name=""MyStruct"" access=""public"">
<struct name=""MyStruct"" access=""public"" unsafe=""true"">
<field name=""x"" access=""public"">
<type>int</type>
</field>
Expand Down Expand Up @@ -1304,7 +1304,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()
<code>Anonymous.Anonymous.o0_b16_4 = value;</code>
</set>
</field>
<struct name=""_Anonymous_e__Struct"" access=""public"">
<struct name=""_Anonymous_e__Struct"" access=""public"" unsafe=""true"">
<field name=""z"" access=""public"">
<type>int</type>
</field>
Expand Down Expand Up @@ -1654,7 +1654,7 @@ protected override Task RemapNestedAnonymousTestImpl()
var expectedOutputContents = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<struct name=""MyStruct"" access=""public"">
<struct name=""MyStruct"" access=""public"" unsafe=""true"">
<field name=""r"" access=""public"">
<type>double</type>
</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()
var expectedOutputContents = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<struct name=""MyUnion"" access=""public"" layout=""Explicit"">
<struct name=""MyUnion"" access=""public"" unsafe=""true"" layout=""Explicit"">
<field name=""x"" access=""public"" offset=""0"">
<type>int</type>
</field>
Expand Down Expand Up @@ -892,7 +892,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()
<code>Anonymous.Anonymous.o0_b16_4 = value;</code>
</set>
</field>
<struct name=""_Anonymous_e__Union"" access=""public"" layout=""Explicit"">
<struct name=""_Anonymous_e__Union"" access=""public"" unsafe=""true"" layout=""Explicit"">
<field name=""z"" access=""public"" offset=""0"">
<type>int</type>
</field>
Expand Down Expand Up @@ -1188,7 +1188,7 @@ protected override Task RemapNestedAnonymousTestImpl()
var expectedOutputContents = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<struct name=""MyUnion"" access=""public"" layout=""Explicit"">
<struct name=""MyUnion"" access=""public"" unsafe=""true"" layout=""Explicit"">
<field name=""r"" access=""public"" offset=""0"">
<type>double</type>
</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ void MyFunction()
var expectedOutputContents = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<struct name=""MyUnion"" access=""public"" layout=""Explicit"">
<struct name=""MyUnion"" access=""public"" unsafe=""true"" layout=""Explicit"">
<field name=""Anonymous"" access=""public"" offset=""0"">
<type native=""__AnonymousRecord_ClangUnsavedFile_L3_C5"">_Anonymous_e__Struct</type>
</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()
var expectedOutputContents = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<struct name=""MyStruct"" access=""public"">
<struct name=""MyStruct"" access=""public"" unsafe=""true"">
<field name=""x"" access=""public"">
<type>int</type>
</field>
Expand Down Expand Up @@ -1316,7 +1316,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()
<code>Anonymous.Anonymous.o0_b16_4 = value;</code>
</set>
</field>
<struct name=""_Anonymous_e__Struct"" access=""public"">
<struct name=""_Anonymous_e__Struct"" access=""public"" unsafe=""true"">
<field name=""z"" access=""public"">
<type>int</type>
</field>
Expand Down Expand Up @@ -1665,7 +1665,7 @@ protected override Task RemapNestedAnonymousTestImpl()
var expectedOutputContents = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<struct name=""MyStruct"" access=""public"">
<struct name=""MyStruct"" access=""public"" unsafe=""true"">
<field name=""r"" access=""public"">
<type>double</type>
</field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()
var expectedOutputContents = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<struct name=""MyUnion"" access=""public"" layout=""Explicit"">
<struct name=""MyUnion"" access=""public"" unsafe=""true"" layout=""Explicit"">
<field name=""x"" access=""public"" offset=""0"">
<type>int</type>
</field>
Expand Down Expand Up @@ -898,7 +898,7 @@ protected override Task NestedAnonymousWithBitfieldTestImpl()
<code>Anonymous.Anonymous.o0_b16_4 = value;</code>
</set>
</field>
<struct name=""_Anonymous_e__Union"" access=""public"" layout=""Explicit"">
<struct name=""_Anonymous_e__Union"" access=""public"" unsafe=""true"" layout=""Explicit"">
<field name=""z"" access=""public"" offset=""0"">
<type>int</type>
</field>
Expand Down Expand Up @@ -1194,7 +1194,7 @@ protected override Task RemapNestedAnonymousTestImpl()
var expectedOutputContents = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<struct name=""MyUnion"" access=""public"" layout=""Explicit"">
<struct name=""MyUnion"" access=""public"" unsafe=""true"" layout=""Explicit"">
<field name=""r"" access=""public"" offset=""0"">
<type>double</type>
</field>
Expand Down