From 59e2989a2618a9ea6d94f2f16eedf12da9ae98e6 Mon Sep 17 00:00:00 2001 From: William Chen Date: Wed, 26 Aug 2026 12:36:00 -0400 Subject: [PATCH 1/4] Add RuntimeSizeofMacroTest --- .../SizeOfMacroTest.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/SizeOfMacroTest.cs diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/SizeOfMacroTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/SizeOfMacroTest.cs new file mode 100644 index 00000000..2140dc77 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/SizeOfMacroTest.cs @@ -0,0 +1,26 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; +using ClangSharp.UnitTests.Baseline; +using NUnit.Framework; + +namespace ClangSharp.UnitTests; + +public sealed class SizeOfMacroTestTest : StandaloneBaselineTest +{ + protected override string Area => "SizeOfMacroTestTest"; + + [Test] + public Task RuntimeSizeofMacroTest() + { + var inputContents = """ + typedef struct XrEventDataBuffer { + const void* next; + } XrEventDataBuffer; + + #define XR_MAX_EVENT_DATA_SIZE sizeof(XrEventDataBuffer) + """; + + return ValidateGeneratedCSharpLatestWindowsBaselineAsync(inputContents); + } +} From 48f01e77cf5e9cff7b0f57b9012989527ad7ec0c Mon Sep 17 00:00:00 2001 From: William Chen Date: Wed, 26 Aug 2026 12:54:35 -0400 Subject: [PATCH 2/4] Add current snapshot for RuntimeSizeofMacroTest --- ...RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs new file mode 100644 index 00000000..a536cc21 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs @@ -0,0 +1,14 @@ +namespace ClangSharp.Test +{ + public unsafe partial struct XrEventDataBuffer + { + [NativeTypeName("const void *")] + public void* next; + } + + public static unsafe partial class Methods + { + [NativeTypeName("#define XR_MAX_EVENT_DATA_SIZE sizeof(XrEventDataBuffer)")] + public static readonly ulong XR_MAX_EVENT_DATA_SIZE = sizeof(XrEventDataBuffer); + } +} From 1eb2401e6b4340e439328e0fa5dc4de204d7f21f Mon Sep 17 00:00:00 2001 From: William Chen Date: Wed, 26 Aug 2026 13:12:58 -0400 Subject: [PATCH 3/4] Add ULongLong to list of types that needs a cast for sizeof --- .../ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs | 2 +- .../RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs index 95e0284e..af4c622b 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs @@ -3266,7 +3266,7 @@ private void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr unaryExprOrT } needsCast = IsType(unaryExprOrTypeTraitExpr, parentType, out var builtinType) && - ((builtinType.Kind == CXType_UInt) || (builtinType.Kind == CXType_ULong)); + (builtinType.Kind is CXType_UInt or CXType_ULong or CXType_ULongLong); needsCast &= !IsSupportedFixedSizedBufferType(typeName); needsCast &= !IsType(unaryExprOrTypeTraitExpr, argumentType); needsCast |= parentTypeIsVariableSized; diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs index a536cc21..8c8c4f6f 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs @@ -9,6 +9,6 @@ public unsafe partial struct XrEventDataBuffer public static unsafe partial class Methods { [NativeTypeName("#define XR_MAX_EVENT_DATA_SIZE sizeof(XrEventDataBuffer)")] - public static readonly ulong XR_MAX_EVENT_DATA_SIZE = sizeof(XrEventDataBuffer); + public static readonly ulong XR_MAX_EVENT_DATA_SIZE = (uint)(sizeof(XrEventDataBuffer)); } } From 409d45f10051310fdae615b365b04d0009b13f31 Mon Sep 17 00:00:00 2001 From: William Chen Date: Wed, 26 Aug 2026 13:34:58 -0400 Subject: [PATCH 4/4] Split RuntimeSizeofMacroTest into Unix and Windows variants --- ...meSizeofMacroTestUnix.CSharp.Latest.Unix.cs | 14 ++++++++++++++ ...fMacroTestWindows.CSharp.Latest.Windows.cs} | 0 .../SizeOfMacroTest.cs | 18 +++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTestUnix.CSharp.Latest.Unix.cs rename tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/{RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs => RuntimeSizeofMacroTestWindows.CSharp.Latest.Windows.cs} (100%) diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTestUnix.CSharp.Latest.Unix.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTestUnix.CSharp.Latest.Unix.cs new file mode 100644 index 00000000..7eeb9749 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTestUnix.CSharp.Latest.Unix.cs @@ -0,0 +1,14 @@ +namespace ClangSharp.Test +{ + public unsafe partial struct XrEventDataBuffer + { + [NativeTypeName("const void *")] + public void* next; + } + + public static unsafe partial class Methods + { + [NativeTypeName("#define XR_MAX_EVENT_DATA_SIZE sizeof(XrEventDataBuffer)")] + public static readonly nuint XR_MAX_EVENT_DATA_SIZE = unchecked((nuint)((uint)(sizeof(XrEventDataBuffer)))); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTestWindows.CSharp.Latest.Windows.cs similarity index 100% rename from tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTest.CSharp.Latest.Windows.cs rename to tests/ClangSharp.PInvokeGenerator.UnitTests/Baseline/Baselines/SizeOfMacroTestTest/RuntimeSizeofMacroTestWindows.CSharp.Latest.Windows.cs diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/SizeOfMacroTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/SizeOfMacroTest.cs index 2140dc77..ff889968 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/SizeOfMacroTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/SizeOfMacroTest.cs @@ -11,7 +11,8 @@ public sealed class SizeOfMacroTestTest : StandaloneBaselineTest protected override string Area => "SizeOfMacroTestTest"; [Test] - public Task RuntimeSizeofMacroTest() + [Platform("win")] + public Task RuntimeSizeofMacroTestWindows() { var inputContents = """ typedef struct XrEventDataBuffer { @@ -23,4 +24,19 @@ typedef struct XrEventDataBuffer { return ValidateGeneratedCSharpLatestWindowsBaselineAsync(inputContents); } + + [Test] + [Platform("unix")] + public Task RuntimeSizeofMacroTestUnix() + { + var inputContents = """ + typedef struct XrEventDataBuffer { + const void* next; + } XrEventDataBuffer; + + #define XR_MAX_EVENT_DATA_SIZE sizeof(XrEventDataBuffer) + """; + + return ValidateGeneratedCSharpLatestUnixBaselineAsync(inputContents); + } }