From 99bbed1ceaf28335db6ac5f76622170588822e0f Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Sat, 24 Sep 2022 10:08:39 -0700 Subject: [PATCH 01/14] Ensure System.CommandLine is included in the tool package --- Directory.Build.props | 2 +- .../ClangSharpPInvokeGenerator.csproj | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 1974167d..2200f771 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -45,7 +45,7 @@ $(BaseArtifactsPath)pkg/$(Configuration)/ ClangSharp ClangSharp - 15.0.0 + 15.0.1 rc1 pr diff --git a/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj b/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj index 891e1551..33d53764 100644 --- a/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj +++ b/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj @@ -3,12 +3,9 @@ Exe - net6.0;net7.0 - - - true + net6.0;net7.0 @@ -22,7 +19,7 @@ - + From 78cc6f4f75e77e1ab9861c10438a186d7c27303c Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 28 Sep 2022 20:54:37 -0700 Subject: [PATCH 02/14] Fixing the resolved libclang version --- sources/ClangSharp.Interop/Extensions/clang.ResolveLibrary.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sources/ClangSharp.Interop/Extensions/clang.ResolveLibrary.cs b/sources/ClangSharp.Interop/Extensions/clang.ResolveLibrary.cs index 47e5269e..10726b9b 100644 --- a/sources/ClangSharp.Interop/Extensions/clang.ResolveLibrary.cs +++ b/sources/ClangSharp.Interop/Extensions/clang.ResolveLibrary.cs @@ -29,8 +29,8 @@ private static IntPtr OnDllImport(string libraryName, Assembly assembly, DllImpo private static bool TryResolveClang(Assembly assembly, DllImportSearchPath? searchPath, out IntPtr nativeLibrary) { - return (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && NativeLibrary.TryLoad("libclang.so.13", assembly, searchPath, out nativeLibrary)) - || (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && NativeLibrary.TryLoad("libclang-13", assembly, searchPath, out nativeLibrary)) + return (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && NativeLibrary.TryLoad("libclang.so.15", assembly, searchPath, out nativeLibrary)) + || (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && NativeLibrary.TryLoad("libclang-15", assembly, searchPath, out nativeLibrary)) || (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && NativeLibrary.TryLoad("libclang.so.1", assembly, searchPath, out nativeLibrary)) || NativeLibrary.TryLoad("libclang", assembly, searchPath, out nativeLibrary); } From 61c08787e339cff6f8f86bc5edba2ced1751e7c0 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 28 Sep 2022 21:25:52 -0700 Subject: [PATCH 03/14] Ensure .NET Foundation and Contributors on the FreeBSD nuspecs --- .../libClangSharp.runtime.freebsd.12-x64.nuspec | 6 +++--- .../libClangSharp.runtime.freebsd.13-x64.nuspec | 6 +++--- .../libclang.runtime.freebsd.12-x64.nuspec | 4 ++-- .../libclang.runtime.freebsd.13-x64.nuspec | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/libClangSharp/libClangSharp.runtime.freebsd.12-x64/libClangSharp.runtime.freebsd.12-x64.nuspec b/packages/libClangSharp/libClangSharp.runtime.freebsd.12-x64/libClangSharp.runtime.freebsd.12-x64.nuspec index 340b699a..56317dd8 100644 --- a/packages/libClangSharp/libClangSharp.runtime.freebsd.12-x64/libClangSharp.runtime.freebsd.12-x64.nuspec +++ b/packages/libClangSharp/libClangSharp.runtime.freebsd.12-x64/libClangSharp.runtime.freebsd.12-x64.nuspec @@ -3,13 +3,13 @@ libClangSharp.runtime.freebsd.12-x64 15.0.0 - Microsoft and Contributors - Microsoft and Contributors + .NET Foundation and Contributors + .NET Foundation and Contributors true NCSA https://github.com/dotnet/clangsharp freebsd 12 x64 native library for libClangSharp. - Copyright © Microsoft and Contributors + Copyright © .NET Foundation and Contributors diff --git a/packages/libClangSharp/libClangSharp.runtime.freebsd.13-x64/libClangSharp.runtime.freebsd.13-x64.nuspec b/packages/libClangSharp/libClangSharp.runtime.freebsd.13-x64/libClangSharp.runtime.freebsd.13-x64.nuspec index def1c760..e21ab9cc 100644 --- a/packages/libClangSharp/libClangSharp.runtime.freebsd.13-x64/libClangSharp.runtime.freebsd.13-x64.nuspec +++ b/packages/libClangSharp/libClangSharp.runtime.freebsd.13-x64/libClangSharp.runtime.freebsd.13-x64.nuspec @@ -3,13 +3,13 @@ libClangSharp.runtime.freebsd.13-x64 15.0.0 - Microsoft and Contributors - Microsoft and Contributors + .NET Foundation and Contributors + .NET Foundation and Contributors true NCSA https://github.com/dotnet/clangsharp freebsd 13 x64 native library for libClangSharp. - Copyright © Microsoft and Contributors + Copyright © .NET Foundation and Contributors diff --git a/packages/libclang/libclang.runtime.freebsd.12-x64/libclang.runtime.freebsd.12-x64.nuspec b/packages/libclang/libclang.runtime.freebsd.12-x64/libclang.runtime.freebsd.12-x64.nuspec index 64fa87db..77d40cf7 100644 --- a/packages/libclang/libclang.runtime.freebsd.12-x64/libclang.runtime.freebsd.12-x64.nuspec +++ b/packages/libclang/libclang.runtime.freebsd.12-x64/libclang.runtime.freebsd.12-x64.nuspec @@ -3,8 +3,8 @@ libclang.runtime.freebsd.12-x64 15.0.0 - Microsoft and Contributors - Microsoft and Contributors + .NET Foundation and Contributors + .NET Foundation and Contributors true Apache-2.0 WITH LLVM-exception https://github.com/dotnet/clangsharp diff --git a/packages/libclang/libclang.runtime.freebsd.13-x64/libclang.runtime.freebsd.13-x64.nuspec b/packages/libclang/libclang.runtime.freebsd.13-x64/libclang.runtime.freebsd.13-x64.nuspec index d5d6e516..627ad28e 100644 --- a/packages/libclang/libclang.runtime.freebsd.13-x64/libclang.runtime.freebsd.13-x64.nuspec +++ b/packages/libclang/libclang.runtime.freebsd.13-x64/libclang.runtime.freebsd.13-x64.nuspec @@ -3,8 +3,8 @@ libclang.runtime.freebsd.13-x64 15.0.0 - Microsoft and Contributors - Microsoft and Contributors + .NET Foundation and Contributors + .NET Foundation and Contributors true Apache-2.0 WITH LLVM-exception https://github.com/dotnet/clangsharp From 33060572b06ed2671840ca829fa4968bb07d113c Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Thu, 29 Sep 2022 14:45:06 -0700 Subject: [PATCH 04/14] Updating version to 15.0.2 --- Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 2200f771..9ca0e4ac 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -45,7 +45,7 @@ $(BaseArtifactsPath)pkg/$(Configuration)/ ClangSharp ClangSharp - 15.0.1 + 15.0.2 rc1 pr From e5b6a90c3a51b04984c3eb3e4549f48d5f5ecea0 Mon Sep 17 00:00:00 2001 From: reflectronic Date: Wed, 12 Oct 2022 03:42:29 -0400 Subject: [PATCH 05/14] Add anonymous enum tests --- .../Base/EnumDeclarationTest.cs | 11 +++ .../EnumDeclarationTest.cs | 57 ++++++++++++++ .../EnumDeclarationTest.cs | 58 ++++++++++++++ .../CSharpLatestUnix/EnumDeclarationTest.cs | 58 ++++++++++++++ .../EnumDeclarationTest.cs | 57 ++++++++++++++ .../CSharpPreviewUnix/EnumDeclarationTest.cs | 60 ++++++++++++++- .../EnumDeclarationTest.cs | 58 ++++++++++++++ .../XmlCompatibleUnix/EnumDeclarationTest.cs | 75 +++++++++++++++++++ .../EnumDeclarationTest.cs | 75 +++++++++++++++++++ .../XmlLatestUnix/EnumDeclarationTest.cs | 75 +++++++++++++++++++ .../XmlLatestWindows/EnumDeclarationTest.cs | 75 +++++++++++++++++++ .../XmlPreviewUnix/EnumDeclarationTest.cs | 75 +++++++++++++++++++ .../XmlPreviewWindows/EnumDeclarationTest.cs | 75 +++++++++++++++++++ 13 files changed, 808 insertions(+), 1 deletion(-) diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/EnumDeclarationTest.cs index e0676c10..00ff4fac 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/EnumDeclarationTest.cs @@ -63,6 +63,12 @@ public abstract class EnumDeclarationTest : PInvokeGeneratorTest [Test] public Task WithTypeStarOverrideTest() => WithTypeStarOverrideTestImpl(); + [Test] + public Task WithAnonymousEnumTest() => WithAnonymousEnumTestImpl(); + + [Test] + public Task WithReferenceToAnonymousEnumEnumeratorTest() => WithReferenceToAnonymousEnumEnumeratorTestImpl(); + protected abstract Task BasicTestImpl(); protected abstract Task BasicValueTestImpl(); @@ -73,6 +79,7 @@ public abstract class EnumDeclarationTest : PInvokeGeneratorTest protected abstract Task ExplicitTypedWithNativeTypeNameTestImpl(string nativeType, string expectedManagedType); + protected abstract Task RemapTestImpl(); protected abstract Task WithAttributeTestImpl(); @@ -96,4 +103,8 @@ public abstract class EnumDeclarationTest : PInvokeGeneratorTest protected abstract Task WithTypeStarTestImpl(); protected abstract Task WithTypeStarOverrideTestImpl(); + + protected abstract Task WithAnonymousEnumTestImpl(); + + protected abstract Task WithReferenceToAnonymousEnumEnumeratorTestImpl(); } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/EnumDeclarationTest.cs index 35f7554e..60f6421a 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/EnumDeclarationTest.cs @@ -550,4 +550,61 @@ public enum MyEnum2 : uint }; return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @"using static ClangSharp.Test.Methods; + +namespace ClangSharp.Test +{ + public enum MyEnum2 + { + MyEnum2_Value1 = MyEnum1_Value1, + } + + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + } +} +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @"namespace ClangSharp.Test +{ + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + + [NativeTypeName(""const int"")] + public const int MyEnum2_Value1 = (int)(MyEnum1_Value1) + 1; + } +} +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/EnumDeclarationTest.cs index fcb6938a..7d07fefd 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/EnumDeclarationTest.cs @@ -550,4 +550,62 @@ public enum MyEnum2 : uint }; return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @"using static ClangSharp.Test.Methods; + +namespace ClangSharp.Test +{ + public enum MyEnum2 + { + MyEnum2_Value1 = MyEnum1_Value1, + } + + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + } +} +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @"namespace ClangSharp.Test +{ + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + + [NativeTypeName(""const int"")] + public const int MyEnum2_Value1 = (int)(MyEnum1_Value1) + 1; + } +} +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/EnumDeclarationTest.cs index 4aa0b602..ada29394 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/EnumDeclarationTest.cs @@ -550,4 +550,62 @@ public enum MyEnum2 : uint }; return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @"using static ClangSharp.Test.Methods; + +namespace ClangSharp.Test +{ + public enum MyEnum2 + { + MyEnum2_Value1 = MyEnum1_Value1, + } + + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + } +} +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @"namespace ClangSharp.Test +{ + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + + [NativeTypeName(""const int"")] + public const int MyEnum2_Value1 = (int)(MyEnum1_Value1) + 1; + } +} +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/EnumDeclarationTest.cs index c2f9afe1..abbb6b17 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/EnumDeclarationTest.cs @@ -550,4 +550,61 @@ public enum MyEnum2 : uint }; return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @"using static ClangSharp.Test.Methods; + +namespace ClangSharp.Test +{ + public enum MyEnum2 + { + MyEnum2_Value1 = MyEnum1_Value1, + } + + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + } +} +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @"namespace ClangSharp.Test +{ + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + + [NativeTypeName(""const int"")] + public const int MyEnum2_Value1 = (int)(MyEnum1_Value1) + 1; + } +} +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/EnumDeclarationTest.cs index de68287b..f83cbbe6 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/EnumDeclarationTest.cs @@ -224,7 +224,7 @@ public enum MyEnum2 "; var withAttributes = new Dictionary> - { + { ["MyEnum1"] = new List() { "Flags" } }; return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents, withAttributes: withAttributes); @@ -550,4 +550,62 @@ public enum MyEnum2 : uint }; return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @"using static ClangSharp.Test.Methods; + +namespace ClangSharp.Test +{ + public enum MyEnum2 + { + MyEnum2_Value1 = MyEnum1_Value1, + } + + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + } +} +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @"namespace ClangSharp.Test +{ + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + + [NativeTypeName(""const int"")] + public const int MyEnum2_Value1 = (int)(MyEnum1_Value1) + 1; + } +} +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/EnumDeclarationTest.cs index baf2a46d..d6fee03f 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/EnumDeclarationTest.cs @@ -550,4 +550,62 @@ public enum MyEnum2 : uint }; return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @"using static ClangSharp.Test.Methods; + +namespace ClangSharp.Test +{ + public enum MyEnum2 + { + MyEnum2_Value1 = MyEnum1_Value1, + } + + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + } +} +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @"namespace ClangSharp.Test +{ + public static partial class Methods + { + public const int MyEnum1_Value1 = 1; + + [NativeTypeName(""const int"")] + public const int MyEnum2_Value1 = (int)(MyEnum1_Value1) + 1; + } +} +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/EnumDeclarationTest.cs index b7e01d3b..e407bb54 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/EnumDeclarationTest.cs @@ -652,4 +652,79 @@ enum MyEnum2 : int }; return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @" + + + + int + + int + + MyEnum1_Value1 + + + + + + int + + 1 + + + + + +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @" + + + + + int + + 1 + + + + int + + (int)(MyEnum1_Value1) + 1 + + + + + +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/EnumDeclarationTest.cs index 48b4cb82..7dcc7ac9 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/EnumDeclarationTest.cs @@ -652,4 +652,79 @@ enum MyEnum2 : int }; return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @" + + + + int + + int + + MyEnum1_Value1 + + + + + + int + + 1 + + + + + +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @" + + + + + int + + 1 + + + + int + + (int)(MyEnum1_Value1) + 1 + + + + + +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/EnumDeclarationTest.cs index c1cdf5e8..cfb68edf 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/EnumDeclarationTest.cs @@ -652,4 +652,79 @@ enum MyEnum2 : int }; return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @" + + + + int + + int + + MyEnum1_Value1 + + + + + + int + + 1 + + + + + +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @" + + + + + int + + 1 + + + + int + + (int)(MyEnum1_Value1) + 1 + + + + + +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/EnumDeclarationTest.cs index 7759d613..d7a426b1 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/EnumDeclarationTest.cs @@ -652,4 +652,79 @@ enum MyEnum2 : int }; return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @" + + + + int + + int + + MyEnum1_Value1 + + + + + + int + + 1 + + + + + +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @" + + + + + int + + 1 + + + + int + + (int)(MyEnum1_Value1) + 1 + + + + + +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/EnumDeclarationTest.cs index c98a649e..9d6ce193 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/EnumDeclarationTest.cs @@ -652,4 +652,79 @@ enum MyEnum2 : int }; return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @" + + + + int + + int + + MyEnum1_Value1 + + + + + + int + + 1 + + + + + +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @" + + + + + int + + 1 + + + + int + + (int)(MyEnum1_Value1) + 1 + + + + + +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/EnumDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/EnumDeclarationTest.cs index a35b008f..4334e19e 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/EnumDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/EnumDeclarationTest.cs @@ -652,4 +652,79 @@ enum MyEnum2 : int }; return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes); } + + protected override Task WithAnonymousEnumTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +enum MyEnum2 : int +{ + MyEnum2_Value1 = MyEnum1_Value1, +}; +"; + + var expectedOutputContents = @" + + + + int + + int + + MyEnum1_Value1 + + + + + + int + + 1 + + + + + +"; + + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } + + protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl() + { + var inputContents = @"enum +{ + MyEnum1_Value1 = 1, +}; + +const int MyEnum2_Value1 = MyEnum1_Value1 + 1; +"; + + var expectedOutputContents = @" + + + + + int + + 1 + + + + int + + (int)(MyEnum1_Value1) + 1 + + + + + +"; + var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") }; + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics); + } } From 0a72628f221b60c903c1630bb181b953346f6d69 Mon Sep 17 00:00:00 2001 From: Benjamin Moir Date: Fri, 28 Oct 2022 23:15:34 +1000 Subject: [PATCH 06/14] Adding support for --with-pack-override --- .../Abstractions/StructDesc.cs | 1 + .../CSharp/CSharpOutputBuilder.VisitDecl.cs | 11 ++++++++--- .../PInvokeGenerator.VisitDecl.cs | 1 + .../PInvokeGeneratorConfiguration.cs | 16 ++++++++++++++++ sources/ClangSharpPInvokeGenerator/Program.cs | 19 ++++++++++++++++++- .../PInvokeGeneratorTest.cs | 1 + 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs b/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs index eaf8606d..6366210c 100644 --- a/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs +++ b/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs @@ -111,6 +111,7 @@ public struct LayoutDesc public long Size32 { get; set; } public long Size64 { get; set; } public long Pack { get; set; } + public string? PackOverride { get; set; } public long MaxFieldAlignment { get; set; } public LayoutKind Kind { get; set; } } diff --git a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs index 1dd7df55..14a104fe 100644 --- a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs @@ -740,13 +740,18 @@ public void BeginStruct(in StructDesc desc) WriteLine("\"]/*' />"); } - if (desc.LayoutAttribute is not null) + if (desc.LayoutAttribute is not null || desc.Layout.PackOverride is not null) { AddUsingDirective("System.Runtime.InteropServices"); WriteIndented("[StructLayout(LayoutKind."); - Write(desc.LayoutAttribute.Value); + Write(desc.LayoutAttribute?.Value ?? LayoutKind.Sequential); - if (desc.LayoutAttribute.Pack != 0) + if (desc.Layout.PackOverride is { } packOverride) + { + Write(", Pack = "); + Write(packOverride); + } + else if (desc.LayoutAttribute!.Pack != 0) { Write(", Pack = "); Write(desc.LayoutAttribute.Pack); diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index e2c75b5c..3e32b2fe 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -1479,6 +1479,7 @@ private void VisitRecordDecl(RecordDecl recordDecl) Size32 = size32, Size64 = size64, Pack = alignment < maxAlignm ? alignment : 0, + PackOverride = TryGetRemappedValue(recordDecl, _config.WithPackOverrides, out var packOverride) ? packOverride : null, MaxFieldAlignment = maxAlignm, Kind = layoutKind }, diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs index 6d7fc4c7..4eb14a9a 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs @@ -45,6 +45,7 @@ public sealed class PInvokeGeneratorConfiguration private readonly SortedDictionary _withTransparentStructs; private readonly SortedDictionary _withTypes; private readonly SortedDictionary> _withUsings; + private readonly SortedDictionary _withPackOverrides; private PInvokeGeneratorConfigurationOptions _options; @@ -94,6 +95,7 @@ public PInvokeGeneratorConfiguration(string defaultNamespace, string outputLocat _withTransparentStructs = new SortedDictionary(); _withTypes = new SortedDictionary(); _withUsings = new SortedDictionary>(); + _withPackOverrides = new SortedDictionary(); if ((outputMode == PInvokeGeneratorOutputMode.Xml) && !options.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateMultipleFiles) && (options.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateTestsNUnit) || options.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateTestsXUnit))) { @@ -528,6 +530,20 @@ public IReadOnlyDictionary> WithUsings } } + [AllowNull] + public IReadOnlyDictionary WithPackOverrides + { + get + { + return _withPackOverrides; + } + + init + { + AddRange(_withPackOverrides, value); + } + } + public static AccessSpecifier ConvertStringToAccessSpecifier(string input) { if (input.Equals("internal", StringComparison.OrdinalIgnoreCase)) diff --git a/sources/ClangSharpPInvokeGenerator/Program.cs b/sources/ClangSharpPInvokeGenerator/Program.cs index f708bf16..21a55298 100644 --- a/sources/ClangSharpPInvokeGenerator/Program.cs +++ b/sources/ClangSharpPInvokeGenerator/Program.cs @@ -57,6 +57,7 @@ public class Program private static readonly Option s_withTransparentStructNameValuePairs; private static readonly Option s_withTypeNameValuePairs; private static readonly Option s_withUsingNameValuePairs; + private static readonly Option s_withPackOverrideNameValuePairs; private static readonly TwoColumnHelpRow[] s_configOptions = new TwoColumnHelpRow[] @@ -161,6 +162,7 @@ static Program() s_withTransparentStructNameValuePairs = GetWithTransparentStructOption(); s_withTypeNameValuePairs = GetWithTypeOption(); s_withUsingNameValuePairs = GetWithUsingOption(); + s_withPackOverrideNameValuePairs = GetWithPackOverrideOption(); s_rootCommand = new RootCommand("ClangSharp P/Invoke Binding Generator") { @@ -198,7 +200,8 @@ static Program() s_withSuppressGCTransitions, s_withTransparentStructNameValuePairs, s_withTypeNameValuePairs, - s_withUsingNameValuePairs + s_withUsingNameValuePairs, + s_withPackOverrideNameValuePairs }; Handler.SetHandler(s_rootCommand, (Action)Run); } @@ -255,6 +258,7 @@ public static void Run(InvocationContext context) var withTransparentStructNameValuePairs = context.ParseResult.GetValueForOption(s_withTransparentStructNameValuePairs) ?? Array.Empty(); var withTypeNameValuePairs = context.ParseResult.GetValueForOption(s_withTypeNameValuePairs) ?? Array.Empty(); var withUsingNameValuePairs = context.ParseResult.GetValueForOption(s_withUsingNameValuePairs) ?? Array.Empty(); + var withPackOverrideNameValuePairs = context.ParseResult.GetValueForOption(s_withPackOverrideNameValuePairs) ?? Array.Empty(); var versionResult = context.ParseResult.FindResultFor(s_versionOption); @@ -295,6 +299,7 @@ public static void Run(InvocationContext context) ParseKeyValuePairs(withTransparentStructNameValuePairs, errorList, out Dictionary withTransparentStructs); ParseKeyValuePairs(withTypeNameValuePairs, errorList, out Dictionary withTypes); ParseKeyValuePairs(withUsingNameValuePairs, errorList, out Dictionary> withUsings); + ParseKeyValuePairs(withPackOverrideNameValuePairs, errorList, out Dictionary withPackOverrides); foreach (var key in withTransparentStructs.Keys) { @@ -681,6 +686,7 @@ public static void Run(InvocationContext context) WithTransparentStructs = withTransparentStructs, WithTypes = withTypes, WithUsings = withUsings, + WithPackOverrides = withPackOverrides, }; if (config.GenerateMacroBindings) @@ -1288,4 +1294,15 @@ private static Option GetWithUsingOption() AllowMultipleArgumentsPerToken = true }; } + + private static Option GetWithPackOverrideOption() + { + return new Option( + aliases: new string[] { "--with-pack-override", "-wpo" }, + description: "Overrides the StructLayoutAttribute.Pack property for the given type.", + getDefaultValue: Array.Empty + ) { + AllowMultipleArgumentsPerToken = true + }; + } } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs index fab038cf..c53dbdf7 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs @@ -106,6 +106,7 @@ private static async Task ValidateGeneratedBindingsAsync(string inputContents, s WithTransparentStructs = withTransparentStructs, WithTypes = withTypes, WithUsings = withUsings, + WithPackOverrides = null, }; using (var pinvokeGenerator = new PInvokeGenerator(config, (path) => outputStream)) From 02d0a9c40a21556692cfcc1ce1c85db1d28e1848 Mon Sep 17 00:00:00 2001 From: Benjamin Moir Date: Sat, 29 Oct 2022 04:35:19 +1000 Subject: [PATCH 07/14] Addressing feedback for packing overrides --- .../Abstractions/StructDesc.cs | 2 +- .../CSharp/CSharpOutputBuilder.VisitDecl.cs | 6 +++--- .../PInvokeGenerator.VisitDecl.cs | 3 ++- .../PInvokeGeneratorConfiguration.cs | 10 +++++----- sources/ClangSharpPInvokeGenerator/Program.cs | 16 ++++++++-------- .../PInvokeGeneratorTest.cs | 2 +- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs b/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs index 6366210c..9fe20e9a 100644 --- a/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs +++ b/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs @@ -93,7 +93,7 @@ public StructLayoutAttribute? LayoutAttribute return attribute; } - if (layout.Pack != 0) + if (layout.Pack != 0 || layout.PackOverride is not null) { return new StructLayoutAttribute(layout.Kind) { Pack = (int)layout.Pack diff --git a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs index 14a104fe..463285b7 100644 --- a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs @@ -740,18 +740,18 @@ public void BeginStruct(in StructDesc desc) WriteLine("\"]/*' />"); } - if (desc.LayoutAttribute is not null || desc.Layout.PackOverride is not null) + if (desc.LayoutAttribute is not null) { AddUsingDirective("System.Runtime.InteropServices"); WriteIndented("[StructLayout(LayoutKind."); - Write(desc.LayoutAttribute?.Value ?? LayoutKind.Sequential); + Write(desc.LayoutAttribute.Value); if (desc.Layout.PackOverride is { } packOverride) { Write(", Pack = "); Write(packOverride); } - else if (desc.LayoutAttribute!.Pack != 0) + else if (desc.LayoutAttribute.Pack != 0) { Write(", Pack = "); Write(desc.LayoutAttribute.Pack); diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index 3e32b2fe..75b1aa5a 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -1479,7 +1479,7 @@ private void VisitRecordDecl(RecordDecl recordDecl) Size32 = size32, Size64 = size64, Pack = alignment < maxAlignm ? alignment : 0, - PackOverride = TryGetRemappedValue(recordDecl, _config.WithPackOverrides, out var packOverride) ? packOverride : null, + PackOverride = TryGetRemappedValue(recordDecl, _config.WithPackings, out var packOverride) ? packOverride : null, MaxFieldAlignment = maxAlignm, Kind = layoutKind }, @@ -2828,6 +2828,7 @@ void VisitConstantOrIncompleteArrayFieldDecl(RecordDecl recordDecl, FieldDecl co Size32 = size32, Size64 = size64, Pack = alignment < maxAlignm ? alignment : 0, + PackOverride = TryGetRemappedValue(recordDecl, _config.WithPackings, out var packOverride) ? packOverride : null, MaxFieldAlignment = maxAlignm, Kind = LayoutKind.Sequential }, diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs index 4eb14a9a..cb11991b 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs @@ -45,7 +45,7 @@ public sealed class PInvokeGeneratorConfiguration private readonly SortedDictionary _withTransparentStructs; private readonly SortedDictionary _withTypes; private readonly SortedDictionary> _withUsings; - private readonly SortedDictionary _withPackOverrides; + private readonly SortedDictionary _withPackings; private PInvokeGeneratorConfigurationOptions _options; @@ -95,7 +95,7 @@ public PInvokeGeneratorConfiguration(string defaultNamespace, string outputLocat _withTransparentStructs = new SortedDictionary(); _withTypes = new SortedDictionary(); _withUsings = new SortedDictionary>(); - _withPackOverrides = new SortedDictionary(); + _withPackings = new SortedDictionary(); if ((outputMode == PInvokeGeneratorOutputMode.Xml) && !options.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateMultipleFiles) && (options.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateTestsNUnit) || options.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateTestsXUnit))) { @@ -531,16 +531,16 @@ public IReadOnlyDictionary> WithUsings } [AllowNull] - public IReadOnlyDictionary WithPackOverrides + public IReadOnlyDictionary WithPackings { get { - return _withPackOverrides; + return _withPackings; } init { - AddRange(_withPackOverrides, value); + AddRange(_withPackings, value); } } diff --git a/sources/ClangSharpPInvokeGenerator/Program.cs b/sources/ClangSharpPInvokeGenerator/Program.cs index 21a55298..9303d0d4 100644 --- a/sources/ClangSharpPInvokeGenerator/Program.cs +++ b/sources/ClangSharpPInvokeGenerator/Program.cs @@ -57,7 +57,7 @@ public class Program private static readonly Option s_withTransparentStructNameValuePairs; private static readonly Option s_withTypeNameValuePairs; private static readonly Option s_withUsingNameValuePairs; - private static readonly Option s_withPackOverrideNameValuePairs; + private static readonly Option s_withPackingNameValuePairs; private static readonly TwoColumnHelpRow[] s_configOptions = new TwoColumnHelpRow[] @@ -162,7 +162,7 @@ static Program() s_withTransparentStructNameValuePairs = GetWithTransparentStructOption(); s_withTypeNameValuePairs = GetWithTypeOption(); s_withUsingNameValuePairs = GetWithUsingOption(); - s_withPackOverrideNameValuePairs = GetWithPackOverrideOption(); + s_withPackingNameValuePairs = GetWithPackingOption(); s_rootCommand = new RootCommand("ClangSharp P/Invoke Binding Generator") { @@ -201,7 +201,7 @@ static Program() s_withTransparentStructNameValuePairs, s_withTypeNameValuePairs, s_withUsingNameValuePairs, - s_withPackOverrideNameValuePairs + s_withPackingNameValuePairs }; Handler.SetHandler(s_rootCommand, (Action)Run); } @@ -258,7 +258,7 @@ public static void Run(InvocationContext context) var withTransparentStructNameValuePairs = context.ParseResult.GetValueForOption(s_withTransparentStructNameValuePairs) ?? Array.Empty(); var withTypeNameValuePairs = context.ParseResult.GetValueForOption(s_withTypeNameValuePairs) ?? Array.Empty(); var withUsingNameValuePairs = context.ParseResult.GetValueForOption(s_withUsingNameValuePairs) ?? Array.Empty(); - var withPackOverrideNameValuePairs = context.ParseResult.GetValueForOption(s_withPackOverrideNameValuePairs) ?? Array.Empty(); + var withPackingNameValuePairs = context.ParseResult.GetValueForOption(s_withPackingNameValuePairs) ?? Array.Empty(); var versionResult = context.ParseResult.FindResultFor(s_versionOption); @@ -299,7 +299,7 @@ public static void Run(InvocationContext context) ParseKeyValuePairs(withTransparentStructNameValuePairs, errorList, out Dictionary withTransparentStructs); ParseKeyValuePairs(withTypeNameValuePairs, errorList, out Dictionary withTypes); ParseKeyValuePairs(withUsingNameValuePairs, errorList, out Dictionary> withUsings); - ParseKeyValuePairs(withPackOverrideNameValuePairs, errorList, out Dictionary withPackOverrides); + ParseKeyValuePairs(withPackingNameValuePairs, errorList, out Dictionary withPackings); foreach (var key in withTransparentStructs.Keys) { @@ -686,7 +686,7 @@ public static void Run(InvocationContext context) WithTransparentStructs = withTransparentStructs, WithTypes = withTypes, WithUsings = withUsings, - WithPackOverrides = withPackOverrides, + WithPackings = withPackings, }; if (config.GenerateMacroBindings) @@ -1295,10 +1295,10 @@ private static Option GetWithUsingOption() }; } - private static Option GetWithPackOverrideOption() + private static Option GetWithPackingOption() { return new Option( - aliases: new string[] { "--with-pack-override", "-wpo" }, + aliases: new string[] { "--with-packing", "-wp" }, description: "Overrides the StructLayoutAttribute.Pack property for the given type.", getDefaultValue: Array.Empty ) { diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs index c53dbdf7..a85ec5c4 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs @@ -106,7 +106,7 @@ private static async Task ValidateGeneratedBindingsAsync(string inputContents, s WithTransparentStructs = withTransparentStructs, WithTypes = withTypes, WithUsings = withUsings, - WithPackOverrides = null, + WithPackings = null, }; using (var pinvokeGenerator = new PInvokeGenerator(config, (path) => outputStream)) From cacd5bbab337e03d32b0e7bb8c2eca00c477c09e Mon Sep 17 00:00:00 2001 From: Benjamin Moir Date: Sat, 29 Oct 2022 04:51:26 +1000 Subject: [PATCH 08/14] Changing LayoutDesc.Pack to string and addressing some missing changes --- .../Abstractions/StructDesc.cs | 18 ++++-------------- .../CSharp/CSharpOutputBuilder.VisitDecl.cs | 9 ++------- .../PInvokeGenerator.VisitDecl.cs | 18 +++++++++++++----- .../XML/XmlOutputBuilder.VisitDecl.cs | 4 ++-- 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs b/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs index 9fe20e9a..a63b3c65 100644 --- a/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs +++ b/sources/ClangSharp.PInvokeGenerator/Abstractions/StructDesc.cs @@ -83,21 +83,12 @@ public StructLayoutAttribute? LayoutAttribute { Debug.Assert(layout.Kind == LayoutKind.Explicit); - var attribute = new StructLayoutAttribute(layout.Kind); - - if (layout.Pack != 0) - { - attribute.Pack = (int)layout.Pack; - } - - return attribute; + return new StructLayoutAttribute(layout.Kind); } - if (layout.Pack != 0 || layout.PackOverride is not null) + if (layout.Pack is not null) { - return new StructLayoutAttribute(layout.Kind) { - Pack = (int)layout.Pack - }; + return new StructLayoutAttribute(layout.Kind); } return null; @@ -110,8 +101,7 @@ public struct LayoutDesc public long Alignment64 { get; set; } public long Size32 { get; set; } public long Size64 { get; set; } - public long Pack { get; set; } - public string? PackOverride { get; set; } + public string? Pack { get; set; } public long MaxFieldAlignment { get; set; } public LayoutKind Kind { get; set; } } diff --git a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs index 463285b7..33768afa 100644 --- a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs @@ -746,15 +746,10 @@ public void BeginStruct(in StructDesc desc) WriteIndented("[StructLayout(LayoutKind."); Write(desc.LayoutAttribute.Value); - if (desc.Layout.PackOverride is { } packOverride) + if (desc.Layout.Pack != null) { Write(", Pack = "); - Write(packOverride); - } - else if (desc.LayoutAttribute.Pack != 0) - { - Write(", Pack = "); - Write(desc.LayoutAttribute.Pack); + Write(desc.Layout.Pack); } WriteLine(")]"); diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index 75b1aa5a..fa3fcad3 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -1467,6 +1467,11 @@ private void VisitRecordDecl(RecordDecl recordDecl) baseTypeNames = baseTypeNamesBuilder.ToArray(); } + if (!TryGetRemappedValue(recordDecl, _config.WithPackings, out var pack)) + { + pack = alignment < maxAlignm ? alignment.ToString(CultureInfo.InvariantCulture) : null; + } + var desc = new StructDesc { AccessSpecifier = GetAccessSpecifier(recordDecl, matchStar: true), EscapedName = escapedName, @@ -1478,8 +1483,7 @@ private void VisitRecordDecl(RecordDecl recordDecl) Alignment64 = alignment64, Size32 = size32, Size64 = size64, - Pack = alignment < maxAlignm ? alignment : 0, - PackOverride = TryGetRemappedValue(recordDecl, _config.WithPackings, out var packOverride) ? packOverride : null, + Pack = pack, MaxFieldAlignment = maxAlignm, Kind = layoutKind }, @@ -1516,7 +1520,7 @@ private void VisitRecordDecl(RecordDecl recordDecl) if (desc.LayoutAttribute is not null) { - withAttributes.Add($"StructLayout(LayoutKind.{desc.LayoutAttribute.Value}{((desc.LayoutAttribute.Pack != 0) ? $", Pack = {desc.LayoutAttribute.Pack}" : "")})"); + withAttributes.Add($"StructLayout(LayoutKind.{desc.LayoutAttribute.Value}{((desc.Layout.Pack != null) ? $", Pack = {desc.Layout.Pack}" : "")})"); _ = withUsings.Add("System.Runtime.InteropServices"); } @@ -2818,6 +2822,11 @@ void VisitConstantOrIncompleteArrayFieldDecl(RecordDecl recordDecl, FieldDecl co AddDiagnostic(DiagnosticLevel.Info, $"{escapedName} (constant array field) has a size of 0", constantOrIncompleteArray); } + if (!TryGetRemappedValue(recordDecl, _config.WithPackings, out var pack)) + { + pack = alignment < maxAlignm ? alignment.ToString(CultureInfo.InvariantCulture) : null; + } + var desc = new StructDesc { AccessSpecifier = accessSpecifier, EscapedName = escapedName, @@ -2827,8 +2836,7 @@ void VisitConstantOrIncompleteArrayFieldDecl(RecordDecl recordDecl, FieldDecl co Alignment64 = alignment64, Size32 = size32, Size64 = size64, - Pack = alignment < maxAlignm ? alignment : 0, - PackOverride = TryGetRemappedValue(recordDecl, _config.WithPackings, out var packOverride) ? packOverride : null, + Pack = pack, MaxFieldAlignment = maxAlignm, Kind = LayoutKind.Sequential }, diff --git a/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs index 2c321370..9868d43f 100644 --- a/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs @@ -290,10 +290,10 @@ public void BeginStruct(in StructDesc info) _ = _sb.Append(info.LayoutAttribute.Value); _ = _sb.Append('"'); - if (info.LayoutAttribute.Pack != 0) + if (info.Layout.Pack != null) { _ = _sb.Append(" pack=\""); - _ = _sb.Append(info.LayoutAttribute.Pack); + _ = _sb.Append(info.Layout.Pack); _ = _sb.Append('"'); } } From 35ca8a3181b9188797b7479b54d7841085174a75 Mon Sep 17 00:00:00 2001 From: Benjamin Moir Date: Sat, 29 Oct 2022 14:16:24 +1000 Subject: [PATCH 09/14] Changing instances of `Pack != null` to `Pack is not null` for consistency with other code --- .../CSharp/CSharpOutputBuilder.VisitDecl.cs | 2 +- .../ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs | 2 +- .../XML/XmlOutputBuilder.VisitDecl.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs index 33768afa..5d87fc1f 100644 --- a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs @@ -746,7 +746,7 @@ public void BeginStruct(in StructDesc desc) WriteIndented("[StructLayout(LayoutKind."); Write(desc.LayoutAttribute.Value); - if (desc.Layout.Pack != null) + if (desc.Layout.Pack is not null) { Write(", Pack = "); Write(desc.Layout.Pack); diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index fa3fcad3..a7143c80 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -1520,7 +1520,7 @@ private void VisitRecordDecl(RecordDecl recordDecl) if (desc.LayoutAttribute is not null) { - withAttributes.Add($"StructLayout(LayoutKind.{desc.LayoutAttribute.Value}{((desc.Layout.Pack != null) ? $", Pack = {desc.Layout.Pack}" : "")})"); + withAttributes.Add($"StructLayout(LayoutKind.{desc.LayoutAttribute.Value}{((desc.Layout.Pack is not null) ? $", Pack = {desc.Layout.Pack}" : "")})"); _ = withUsings.Add("System.Runtime.InteropServices"); } diff --git a/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs index 9868d43f..e3e7b12d 100644 --- a/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs @@ -290,7 +290,7 @@ public void BeginStruct(in StructDesc info) _ = _sb.Append(info.LayoutAttribute.Value); _ = _sb.Append('"'); - if (info.Layout.Pack != null) + if (info.Layout.Pack is not null) { _ = _sb.Append(" pack=\""); _ = _sb.Append(info.Layout.Pack); From 261f236934f252f5165953ea43cbd0ba9dfbfcff Mon Sep 17 00:00:00 2001 From: Benjamin Moir Date: Wed, 2 Nov 2022 03:56:36 +1000 Subject: [PATCH 10/14] Adding tests for --with-packing output --- .../Base/StructDeclarationTest.cs | 5 ++ .../StructDeclarationTest.cs | 21 ++++++++ .../StructDeclarationTest.cs | 21 ++++++++ .../CSharpLatestUnix/StructDeclarationTest.cs | 21 ++++++++ .../StructDeclarationTest.cs | 21 ++++++++ .../StructDeclarationTest.cs | 21 ++++++++ .../StructDeclarationTest.cs | 21 ++++++++ .../PInvokeGeneratorTest.cs | 52 +++++++++---------- .../StructDeclarationTest.cs | 18 +++++++ .../StructDeclarationTest.cs | 18 +++++++ .../XmlLatestUnix/StructDeclarationTest.cs | 18 +++++++ .../XmlLatestWindows/StructDeclarationTest.cs | 18 +++++++ .../XmlPreviewUnix/StructDeclarationTest.cs | 18 +++++++ .../StructDeclarationTest.cs | 18 +++++++ 14 files changed, 265 insertions(+), 26 deletions(-) diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/StructDeclarationTest.cs index 46318f5d..eb5a0dd0 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/StructDeclarationTest.cs @@ -206,6 +206,9 @@ public abstract class StructDeclarationTest : PInvokeGeneratorTest [Test] public Task WithAccessSpecifierTest() => WithAccessSpecifierTestImpl(); + [Test] + public Task WithPackingTest() => WithPackingTestImpl(); + [Test] public Task SourceLocationAttributeTest() => SourceLocationAttributeTestImpl(); @@ -279,5 +282,7 @@ public abstract class StructDeclarationTest : PInvokeGeneratorTest protected abstract Task WithAccessSpecifierTestImpl(); + protected abstract Task WithPackingTestImpl(); + protected abstract Task SourceLocationAttributeTestImpl(); } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs index db712217..732fa268 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs @@ -1724,6 +1724,27 @@ public partial struct MyStruct3 return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] + public partial struct MyStruct + { + } +} +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs index 285e1e53..b7ca46ad 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs @@ -1728,6 +1728,27 @@ public partial struct MyStruct3 return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] + public partial struct MyStruct + { + } +} +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs index ffdee379..590ee8eb 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs @@ -1702,6 +1702,27 @@ public partial struct MyStruct3 return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] + public partial struct MyStruct + { + } +} +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedCSharpLatestUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs index 337ca60a..c4c9fb35 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs @@ -1706,6 +1706,27 @@ public partial struct MyStruct3 return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] + public partial struct MyStruct + { + } +} +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs index b3038461..2dfcdcba 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs @@ -1728,6 +1728,27 @@ public partial struct MyStruct3 return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] + public partial struct MyStruct + { + } +} +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs index 5d568d8f..52168b50 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs @@ -1732,6 +1732,27 @@ public partial struct MyStruct3 return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] + public partial struct MyStruct + { + } +} +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs index a85ec5c4..9ccabb9f 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/PInvokeGeneratorTest.cs @@ -38,43 +38,43 @@ public abstract class PInvokeGeneratorTest protected static string EscapeXml(string value) => new XText(value).ToString(); - protected static Task ValidateGeneratedCSharpPreviewWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.GeneratePreviewCode | PInvokeGeneratorConfigurationOptions.None | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs); + protected static Task ValidateGeneratedCSharpPreviewWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.GeneratePreviewCode | PInvokeGeneratorConfigurationOptions.None | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs); - protected static Task ValidateGeneratedCSharpPreviewUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.GeneratePreviewCode | PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs); + protected static Task ValidateGeneratedCSharpPreviewUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.GeneratePreviewCode | PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs); - protected static Task ValidateGeneratedCSharpLatestWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.None | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs); + protected static Task ValidateGeneratedCSharpLatestWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.None | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs); - protected static Task ValidateGeneratedCSharpLatestUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs); + protected static Task ValidateGeneratedCSharpLatestUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs); - protected static Task ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs); + protected static Task ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs); - protected static Task ValidateGeneratedCSharpCompatibleUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode | PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs); + protected static Task ValidateGeneratedCSharpCompatibleUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.CSharp, PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode | PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs); - protected static Task ValidateGeneratedXmlPreviewWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null, [CallerFilePath] string filePath = "") - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.GeneratePreviewCode | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs, filePath); + protected static Task ValidateGeneratedXmlPreviewWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null, [CallerFilePath] string filePath = "") + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.GeneratePreviewCode | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs, filePath); - protected static Task ValidateGeneratedXmlPreviewUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.GeneratePreviewCode | PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs); + protected static Task ValidateGeneratedXmlPreviewUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.GeneratePreviewCode | PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs); - protected static Task ValidateGeneratedXmlLatestWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null, [CallerFilePath] string filePath = "") - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.None | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs, filePath); + protected static Task ValidateGeneratedXmlLatestWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null, [CallerFilePath] string filePath = "") + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.None | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs, filePath); - protected static Task ValidateGeneratedXmlLatestUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs); + protected static Task ValidateGeneratedXmlLatestUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs); - protected static Task ValidateGeneratedXmlCompatibleWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs); + protected static Task ValidateGeneratedXmlCompatibleWindowsBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs); - protected static Task ValidateGeneratedXmlCompatibleUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) - => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode | PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, expectedDiagnostics, libraryPath, commandlineArgs); + protected static Task ValidateGeneratedXmlCompatibleUnixBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorConfigurationOptions additionalConfigOptions = PInvokeGeneratorConfigurationOptions.None, string[]? excludedNames = null, IReadOnlyDictionary? remappedNames = null, IReadOnlyDictionary? withAccessSpecifiers = null, IReadOnlyDictionary>? withAttributes = null, IReadOnlyDictionary? withCallConvs = null, IReadOnlyDictionary? withClasses = null, IReadOnlyDictionary? withLibraryPaths = null, IReadOnlyDictionary? withNamespaces = null, string[]? withSetLastErrors = null, IReadOnlyDictionary? withTransparentStructs = null, IReadOnlyDictionary? withTypes = null, IReadOnlyDictionary>? withUsings = null, IReadOnlyDictionary? withPackings = null, IEnumerable? expectedDiagnostics = null, string libraryPath = DefaultLibraryPath, string[]? commandlineArgs = null) + => ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorOutputMode.Xml, PInvokeGeneratorConfigurationOptions.GenerateCompatibleCode | PInvokeGeneratorConfigurationOptions.GenerateUnixTypes | additionalConfigOptions, excludedNames, remappedNames, withAccessSpecifiers, withAttributes, withCallConvs, withClasses, withLibraryPaths, withNamespaces, withSetLastErrors, withTransparentStructs, withTypes, withUsings, withPackings, expectedDiagnostics, libraryPath, commandlineArgs); - private static async Task ValidateGeneratedBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorOutputMode outputMode, PInvokeGeneratorConfigurationOptions configOptions, string[]? excludedNames, IReadOnlyDictionary? remappedNames, IReadOnlyDictionary? withAccessSpecifiers, IReadOnlyDictionary>? withAttributes, IReadOnlyDictionary? withCallConvs, IReadOnlyDictionary? withClasses, IReadOnlyDictionary? withLibraryPaths, IReadOnlyDictionary? withNamespaces, string[]? withSetLastErrors, IReadOnlyDictionary? withTransparentStructs, IReadOnlyDictionary? withTypes, IReadOnlyDictionary>? withUsings, IEnumerable? expectedDiagnostics, string libraryPath, string[]? commandlineArgs, [CallerFilePath] string filePath = "") + private static async Task ValidateGeneratedBindingsAsync(string inputContents, string expectedOutputContents, PInvokeGeneratorOutputMode outputMode, PInvokeGeneratorConfigurationOptions configOptions, string[]? excludedNames, IReadOnlyDictionary? remappedNames, IReadOnlyDictionary? withAccessSpecifiers, IReadOnlyDictionary>? withAttributes, IReadOnlyDictionary? withCallConvs, IReadOnlyDictionary? withClasses, IReadOnlyDictionary? withLibraryPaths, IReadOnlyDictionary? withNamespaces, string[]? withSetLastErrors, IReadOnlyDictionary? withTransparentStructs, IReadOnlyDictionary? withTypes, IReadOnlyDictionary>? withUsings, IReadOnlyDictionary? withPackings, IEnumerable? expectedDiagnostics, string libraryPath, string[]? commandlineArgs, [CallerFilePath] string filePath = "") { Assert.True(File.Exists(DefaultInputFileName)); commandlineArgs ??= DefaultCppClangCommandLineArgs; @@ -106,7 +106,7 @@ private static async Task ValidateGeneratedBindingsAsync(string inputContents, s WithTransparentStructs = withTransparentStructs, WithTypes = withTypes, WithUsings = withUsings, - WithPackings = null, + WithPackings = withPackings, }; using (var pinvokeGenerator = new PInvokeGenerator(config, (path) => outputStream)) diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs index d0b2bc6a..1fa3bcdd 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs @@ -1725,6 +1725,24 @@ struct MyStruct3 return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @" + + + + + +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs index c05bca08..de251716 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs @@ -1730,6 +1730,24 @@ struct MyStruct3 return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @" + + + + + +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs index 91b5cbf3..df646724 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs @@ -1705,6 +1705,24 @@ struct MyStruct3 return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @" + + + + + +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedXmlLatestUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs index 18f28897..9bb841af 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs @@ -1711,6 +1711,24 @@ struct MyStruct3 return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @" + + + + + +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedXmlLatestWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs index 511a68e3..ee446f11 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs @@ -1705,6 +1705,24 @@ struct MyStruct3 return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @" + + + + + +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedXmlPreviewUnixBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs index 6cad4903..a9a284f4 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs @@ -1711,6 +1711,24 @@ struct MyStruct3 return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents, withAccessSpecifiers: withAccessSpecifiers); } + protected override Task WithPackingTestImpl() + { + const string InputContents = @"struct MyStruct {};"; + + const string ExpectedOutputContents = @" + + + + + +"; + + var withPackings = new Dictionary { + ["MyStruct"] = "CustomPackValue" + }; + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(InputContents, ExpectedOutputContents, withPackings: withPackings); + } + protected override Task SourceLocationAttributeTestImpl() { const string InputContents = @"struct MyStruct From 3edcfa50dc18acc43d350d2278db3f31391befb5 Mon Sep 17 00:00:00 2001 From: Benjamin Moir Date: Sat, 5 Nov 2022 19:07:05 +1000 Subject: [PATCH 11/14] Don't apply --with-packing overrides to fixed buffer types --- .../PInvokeGenerator.VisitDecl.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index a7143c80..b2dacb15 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -2822,11 +2822,6 @@ void VisitConstantOrIncompleteArrayFieldDecl(RecordDecl recordDecl, FieldDecl co AddDiagnostic(DiagnosticLevel.Info, $"{escapedName} (constant array field) has a size of 0", constantOrIncompleteArray); } - if (!TryGetRemappedValue(recordDecl, _config.WithPackings, out var pack)) - { - pack = alignment < maxAlignm ? alignment.ToString(CultureInfo.InvariantCulture) : null; - } - var desc = new StructDesc { AccessSpecifier = accessSpecifier, EscapedName = escapedName, @@ -2836,7 +2831,7 @@ void VisitConstantOrIncompleteArrayFieldDecl(RecordDecl recordDecl, FieldDecl co Alignment64 = alignment64, Size32 = size32, Size64 = size64, - Pack = pack, + Pack = alignment < maxAlignm ? alignment.ToString(CultureInfo.InvariantCulture) : null, MaxFieldAlignment = maxAlignm, Kind = LayoutKind.Sequential }, From e162c165ae4d088c4a9569dad16d88c7b7c249f9 Mon Sep 17 00:00:00 2001 From: Benjamin Moir Date: Sat, 5 Nov 2022 19:30:14 +1000 Subject: [PATCH 12/14] Changing --with-packing tests to account for nested fixed buffer types --- .../StructDeclarationTest.cs | 27 +++++++++++++-- .../StructDeclarationTest.cs | 27 +++++++++++++-- .../CSharpLatestUnix/StructDeclarationTest.cs | 26 +++++++++++++-- .../StructDeclarationTest.cs | 26 +++++++++++++-- .../StructDeclarationTest.cs | 29 ++++++++++++++-- .../StructDeclarationTest.cs | 29 ++++++++++++++-- .../StructDeclarationTest.cs | 29 ++++++++++++++-- .../StructDeclarationTest.cs | 29 ++++++++++++++-- .../XmlLatestUnix/StructDeclarationTest.cs | 33 +++++++++++++++++-- .../XmlLatestWindows/StructDeclarationTest.cs | 33 +++++++++++++++++-- .../XmlPreviewUnix/StructDeclarationTest.cs | 33 +++++++++++++++++-- .../StructDeclarationTest.cs | 33 +++++++++++++++++-- 12 files changed, 330 insertions(+), 24 deletions(-) diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs index 732fa268..602a295a 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/StructDeclarationTest.cs @@ -1726,15 +1726,38 @@ public partial struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; - const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + const string ExpectedOutputContents = @"using System; +using System.Runtime.InteropServices; namespace ClangSharp.Test { [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { + [NativeTypeName(""size_t[1]"")] + public _FixedBuffer_e__FixedBuffer FixedBuffer; + + public partial struct _FixedBuffer_e__FixedBuffer + { + public UIntPtr e0; + + public unsafe ref UIntPtr this[int index] + { + get + { + fixed (UIntPtr* pThis = &e0) + { + return ref pThis[index]; + } + } + } + } } } "; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs index b7ca46ad..e5fdaa52 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs @@ -1730,15 +1730,38 @@ public partial struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; - const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + const string ExpectedOutputContents = @"using System; +using System.Runtime.InteropServices; namespace ClangSharp.Test { [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { + [NativeTypeName(""size_t[1]"")] + public _FixedBuffer_e__FixedBuffer FixedBuffer; + + public partial struct _FixedBuffer_e__FixedBuffer + { + public UIntPtr e0; + + public unsafe ref UIntPtr this[int index] + { + get + { + fixed (UIntPtr* pThis = &e0) + { + return ref pThis[index]; + } + } + } + } } } "; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs index 590ee8eb..fde9bf5f 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/StructDeclarationTest.cs @@ -1704,15 +1704,37 @@ public partial struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; - const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + const string ExpectedOutputContents = @"using System; +using System.Runtime.InteropServices; namespace ClangSharp.Test { [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { + [NativeTypeName(""size_t[1]"")] + public _FixedBuffer_e__FixedBuffer FixedBuffer; + + public partial struct _FixedBuffer_e__FixedBuffer + { + public nuint e0; + + public ref nuint this[int index] + { + get + { + return ref AsSpan(int.MaxValue)[index]; + } + } + + public Span AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length); + } } } "; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs index c4c9fb35..1c7b1948 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/StructDeclarationTest.cs @@ -1708,15 +1708,37 @@ public partial struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; - const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + const string ExpectedOutputContents = @"using System; +using System.Runtime.InteropServices; namespace ClangSharp.Test { [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { + [NativeTypeName(""size_t[1]"")] + public _FixedBuffer_e__FixedBuffer FixedBuffer; + + public partial struct _FixedBuffer_e__FixedBuffer + { + public nuint e0; + + public ref nuint this[int index] + { + get + { + return ref AsSpan(int.MaxValue)[index]; + } + } + + public Span AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length); + } } } "; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs index 2dfcdcba..067b2237 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/StructDeclarationTest.cs @@ -1730,15 +1730,40 @@ public partial struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; - const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + const string ExpectedOutputContents = @"using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.InteropServices; namespace ClangSharp.Test { [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { + [NativeTypeName(""size_t[1]"")] + public _FixedBuffer_e__FixedBuffer FixedBuffer; + + public partial struct _FixedBuffer_e__FixedBuffer + { + public nuint e0; + + [UnscopedRef] + public ref nuint this[int index] + { + get + { + return ref AsSpan(int.MaxValue)[index]; + } + } + + [UnscopedRef] + public Span AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length); + } } } "; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs index 52168b50..bf2fc373 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/StructDeclarationTest.cs @@ -1734,15 +1734,40 @@ public partial struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; - const string ExpectedOutputContents = @"using System.Runtime.InteropServices; + const string ExpectedOutputContents = @"using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.InteropServices; namespace ClangSharp.Test { [StructLayout(LayoutKind.Sequential, Pack = CustomPackValue)] public partial struct MyStruct { + [NativeTypeName(""size_t[1]"")] + public _FixedBuffer_e__FixedBuffer FixedBuffer; + + public partial struct _FixedBuffer_e__FixedBuffer + { + public nuint e0; + + [UnscopedRef] + public ref nuint this[int index] + { + get + { + return ref AsSpan(int.MaxValue)[index]; + } + } + + [UnscopedRef] + public Span AsSpan(int length) => MemoryMarshal.CreateSpan(ref e0, length); + } } } "; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs index 1fa3bcdd..2338d380 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/StructDeclarationTest.cs @@ -1727,12 +1727,37 @@ struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; const string ExpectedOutputContents = @" - + + + UIntPtr + + + + UIntPtr + + + ref UIntPtr + + int + + + fixed (UIntPtr* pThis = &e0) + { + return ref pThis[index]; + } + + + + "; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs index de251716..c29a9497 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/StructDeclarationTest.cs @@ -1732,12 +1732,37 @@ struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; const string ExpectedOutputContents = @" - + + + UIntPtr + + + + UIntPtr + + + ref UIntPtr + + int + + + fixed (UIntPtr* pThis = &e0) + { + return ref pThis[index]; + } + + + + "; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs index df646724..6778c6a6 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/StructDeclarationTest.cs @@ -1707,12 +1707,41 @@ struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; const string ExpectedOutputContents = @" - + + + nuint + + + + nuint + + + ref nuint + + int + + + return ref AsSpan(int.MaxValue)[index]; + + + + Span<nuint> + + int + + MemoryMarshal.CreateSpan(ref e0, length); + + + "; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs index 9bb841af..205e1ed0 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/StructDeclarationTest.cs @@ -1713,12 +1713,41 @@ struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; const string ExpectedOutputContents = @" - + + + nuint + + + + nuint + + + ref nuint + + int + + + return ref AsSpan(int.MaxValue)[index]; + + + + Span<nuint> + + int + + MemoryMarshal.CreateSpan(ref e0, length); + + + "; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs index ee446f11..e5e77598 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/StructDeclarationTest.cs @@ -1707,12 +1707,41 @@ struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; const string ExpectedOutputContents = @" - + + + nuint + + + + nuint + + + ref nuint + + int + + + return ref AsSpan(int.MaxValue)[index]; + + + + Span<nuint> + + int + + MemoryMarshal.CreateSpan(ref e0, length); + + + "; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs index a9a284f4..e51ecb81 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/StructDeclarationTest.cs @@ -1713,12 +1713,41 @@ struct MyStruct3 protected override Task WithPackingTestImpl() { - const string InputContents = @"struct MyStruct {};"; + const string InputContents = @"struct MyStruct +{ + size_t FixedBuffer[1]; +}; +"; const string ExpectedOutputContents = @" - + + + nuint + + + + nuint + + + ref nuint + + int + + + return ref AsSpan(int.MaxValue)[index]; + + + + Span<nuint> + + int + + MemoryMarshal.CreateSpan(ref e0, length); + + + "; From ad8ab7b7bda5703a42fe8c224230a9d8726a04ef Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Mon, 7 Nov 2022 07:50:20 -0800 Subject: [PATCH 13/14] Force the tool to be win-x64 only --- .../ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj b/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj index 33d53764..12f0bdde 100644 --- a/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj +++ b/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj @@ -4,6 +4,7 @@ Exe true + win-x64 net6.0;net7.0 From f93ceba8ca371e0147551c11ab7b587ae82ce170 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Mon, 7 Nov 2022 08:00:12 -0800 Subject: [PATCH 14/14] Add a small error message in the case of failure to resolve libClang and libClangSharp --- .../Extensions/clang.ResolveLibrary.cs | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/sources/ClangSharp.Interop/Extensions/clang.ResolveLibrary.cs b/sources/ClangSharp.Interop/Extensions/clang.ResolveLibrary.cs index 10726b9b..98a03d72 100644 --- a/sources/ClangSharp.Interop/Extensions/clang.ResolveLibrary.cs +++ b/sources/ClangSharp.Interop/Extensions/clang.ResolveLibrary.cs @@ -18,13 +18,17 @@ static @clang() private static IntPtr OnDllImport(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) { - return TryResolveLibrary(libraryName, assembly, searchPath, out var nativeLibrary) - ? nativeLibrary - : libraryName.Equals("libclang") && TryResolveClang(assembly, searchPath, out nativeLibrary) - ? nativeLibrary - : libraryName.Equals("libClangSharp") && TryResolveClangSharp(assembly, searchPath, out nativeLibrary) - ? nativeLibrary - : IntPtr.Zero; + var result = TryResolveLibrary(libraryName, assembly, searchPath, out var nativeLibrary) ? nativeLibrary + : libraryName.Equals("libclang") && TryResolveClang(assembly, searchPath, out nativeLibrary) ? nativeLibrary + : libraryName.Equals("libClangSharp") && TryResolveClangSharp(assembly, searchPath, out nativeLibrary) ? nativeLibrary + : IntPtr.Zero; + + if (result == IntPtr.Zero) + { + Console.WriteLine("Failed to resolve libClang or libClangSharp."); + Console.WriteLine("If you are running as a dotnet tool, you may need to manually copy the appropriate DLLs from NuGet due to limitations in the dotnet tool support."); + } + return result; } private static bool TryResolveClang(Assembly assembly, DllImportSearchPath? searchPath, out IntPtr nativeLibrary) @@ -47,7 +51,14 @@ private static bool TryResolveLibrary(string libraryName, Assembly assembly, Dll foreach (DllImportResolver resolver in resolvers.Cast()) { - nativeLibrary = resolver(libraryName, assembly, searchPath); + try + { + nativeLibrary = resolver(libraryName, assembly, searchPath); + } + catch + { + nativeLibrary = IntPtr.Zero; + } if (nativeLibrary != IntPtr.Zero) {