From 079de21a37d5bdfcd4bfb1d0a2ad3ff709256122 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Thu, 28 Oct 2021 19:38:30 -0700 Subject: [PATCH 1/2] Moving forward to net6.0 and fixing a few issues around -m64 --- .../ClangSharp.Interop.csproj | 2 +- .../ClangSharp.PInvokeGenerator.csproj | 3 +- .../PInvokeGenerator.VisitDecl.cs | 20 +++++ .../PInvokeGenerator.VisitStmt.cs | 17 +++++ .../PInvokeGenerator.cs | 76 ++++++++++++------- sources/ClangSharp/ClangSharp.csproj | 2 +- .../ClangSharpPInvokeGenerator.csproj | 4 +- sources/ClangSharpPInvokeGenerator/Program.cs | 3 + sources/Directory.Build.targets | 2 +- .../StructDeclarationTest.cs | 1 - ...angSharp.PInvokeGenerator.UnitTests.csproj | 2 +- .../ClangSharp.UnitTests.csproj | 2 +- 12 files changed, 96 insertions(+), 38 deletions(-) diff --git a/sources/ClangSharp.Interop/ClangSharp.Interop.csproj b/sources/ClangSharp.Interop/ClangSharp.Interop.csproj index 8ce46c21..55335147 100644 --- a/sources/ClangSharp.Interop/ClangSharp.Interop.csproj +++ b/sources/ClangSharp.Interop/ClangSharp.Interop.csproj @@ -2,7 +2,7 @@ - net5.0;netcoreapp3.1;netstandard2.0 + net6.0;netstandard2.0 diff --git a/sources/ClangSharp.PInvokeGenerator/ClangSharp.PInvokeGenerator.csproj b/sources/ClangSharp.PInvokeGenerator/ClangSharp.PInvokeGenerator.csproj index f61f748d..08687502 100644 --- a/sources/ClangSharp.PInvokeGenerator/ClangSharp.PInvokeGenerator.csproj +++ b/sources/ClangSharp.PInvokeGenerator/ClangSharp.PInvokeGenerator.csproj @@ -3,8 +3,7 @@ ClangSharp - net5.0;netcoreapp3.1;netstandard2.0 - 9 + net6.0;netstandard2.0 diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index 2c94a6c0..a3b55398 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -1851,6 +1851,11 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl, case CXTypeKind.CXType_ULongLong: { + if (typeNameBacking == "nuint") + { + goto case CXTypeKind.CXType_UInt; + } + bitwidthHexStringBacking += "UL"; break; } @@ -1875,6 +1880,11 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl, case CXTypeKind.CXType_LongLong: { + if (typeNameBacking == "nint") + { + goto case CXTypeKind.CXType_Int; + } + bitwidthHexStringBacking += "L"; break; } @@ -1920,6 +1930,11 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl, case CXTypeKind.CXType_ULongLong: { + if (typeNameBacking == "nuint") + { + goto case CXTypeKind.CXType_UInt; + } + bitwidthHexString += "UL"; break; } @@ -1944,6 +1959,11 @@ void VisitBitfieldDecl(FieldDecl fieldDecl, Type[] types, RecordDecl recordDecl, case CXTypeKind.CXType_LongLong: { + if (typeNameBacking == "nint") + { + goto case CXTypeKind.CXType_Int; + } + bitwidthHexString += "L"; break; } diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs index e424f91a..213bbbfa 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs @@ -2246,6 +2246,7 @@ private void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr unaryExprOrT } var parentType = null as Type; + var parentTypeIsVariableSized = false; if (IsPrevContextStmt(out var callExpr, out _)) { @@ -2276,6 +2277,21 @@ private void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr unaryExprOrT } else if (calleeDecl is FunctionDecl functionDecl) { + switch (functionDecl.Name) + { + case "memcpy": + { + parentTypeIsVariableSized = true; + break; + } + + case "memset": + { + parentTypeIsVariableSized = true; + break; + } + } + parentType = functionDecl.Parameters[index].Type.CanonicalType; } else @@ -2302,6 +2318,7 @@ private void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr unaryExprOrT needsCast |= parentType.Kind == CXTypeKind.CXType_ULong; needsCast &= !IsSupportedFixedSizedBufferType(typeName); needsCast &= argumentType.CanonicalType.Kind != CXTypeKind.CXType_Enum; + needsCast |= parentTypeIsVariableSized; } if (needsCast) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index 6d9f9f73..841c470c 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -1650,9 +1650,17 @@ private string GetTargetTypeName(Cursor cursor, out string nativeTypeName) else { var type = varDecl.Type; + var cursorName = GetCursorName(varDecl); - if (GetCursorName(varDecl).StartsWith("ClangSharpMacro_")) + if (cursorName.StartsWith("ClangSharpMacro_")) { + cursorName = cursorName["ClangSharpMacro_".Length..]; + + if (_config.WithTypes.TryGetValue(cursorName, out targetTypeName)) + { + return targetTypeName; + } + type = varDecl.Init.Type; } @@ -2262,6 +2270,11 @@ private void GetTypeSize(Cursor cursor, Type type, ref long alignment32, ref lon size32 = 4; size64 = 8; + if (alignment32 == -1) + { + alignment32 = 4; + } + if (alignment64 == -1) { alignment64 = 8; @@ -2281,6 +2294,11 @@ private void GetTypeSize(Cursor cursor, Type type, ref long alignment32, ref lon size32 = 8; size64 = 8; + if (alignment32 == -1) + { + alignment32 = 8; + } + if (alignment64 == -1) { alignment64 = 8; @@ -2342,6 +2360,10 @@ private void GetTypeSize(Cursor cursor, Type type, ref long alignment32, ref lon if (alignment32 == -1) { alignment32 = recordTypeAlignOf; + } + + if (alignment64 == -1) + { alignment64 = recordTypeAlignOf; } @@ -2358,16 +2380,21 @@ private void GetTypeSize(Cursor cursor, Type type, ref long alignment32, ref lon size32 += 4; size64 += 8; - if (alignment64 == 4) + if (alignment32 < 4) { - alignment64 = 8; + alignment32 = Math.Max(Math.Min(alignment32, 4), 1); } - maxFieldSize32 = 4; - maxFieldSize64 = 8; + if (alignment64 < 4) + { + alignment64 = Math.Max(Math.Min(alignment32, 8), 1); + } - maxFieldAlignment32 = alignment32; - maxFieldAlignment64 = alignment64; + maxFieldSize32 = Math.Max(maxFieldSize32, 4); + maxFieldSize64 = Math.Max(maxFieldSize64, 8); + + maxFieldAlignment32 = Math.Max(maxFieldSize32, 4); + maxFieldAlignment64 = Math.Max(maxFieldSize64, 8); } else { @@ -2413,7 +2440,7 @@ private void GetTypeSize(Cursor cursor, Type type, ref long alignment32, ref lon var bitfieldPreviousSize = 0L; var bitfieldRemainingBits = 0L; - foreach (var declaration in recordType.Decl.Decls) + foreach (var fieldDecl in recordType.Decl.Fields) { long fieldSize32; long fieldSize64; @@ -2421,29 +2448,22 @@ private void GetTypeSize(Cursor cursor, Type type, ref long alignment32, ref lon long fieldAlignment32 = -1; long fieldAlignment64 = -1; - if (declaration is FieldDecl fieldDecl) - { - GetTypeSize(fieldDecl, fieldDecl.Type, ref fieldAlignment32, ref fieldAlignment64, out fieldSize32, out fieldSize64); + GetTypeSize(fieldDecl, fieldDecl.Type, ref fieldAlignment32, ref fieldAlignment64, out fieldSize32, out fieldSize64); - if (fieldDecl.IsBitField) + if (fieldDecl.IsBitField) + { + if ((fieldSize32 != bitfieldPreviousSize) || (fieldDecl.BitWidthValue > bitfieldRemainingBits)) { - if ((fieldSize32 != bitfieldPreviousSize) || (fieldDecl.BitWidthValue > bitfieldRemainingBits)) - { - bitfieldRemainingBits = fieldSize32 * 8; - bitfieldPreviousSize = fieldSize32; - bitfieldRemainingBits -= fieldDecl.BitWidthValue; - } - else - { - bitfieldPreviousSize = fieldSize32; - bitfieldRemainingBits -= fieldDecl.BitWidthValue; - continue; - } + bitfieldRemainingBits = fieldSize32 * 8; + bitfieldPreviousSize = fieldSize32; + bitfieldRemainingBits -= fieldDecl.BitWidthValue; + } + else + { + bitfieldPreviousSize = fieldSize32; + bitfieldRemainingBits -= fieldDecl.BitWidthValue; + continue; } - } - else - { - continue; } if ((fieldAlignment32 == -1) || (alignment32 < 4)) diff --git a/sources/ClangSharp/ClangSharp.csproj b/sources/ClangSharp/ClangSharp.csproj index f74af481..9a713f17 100644 --- a/sources/ClangSharp/ClangSharp.csproj +++ b/sources/ClangSharp/ClangSharp.csproj @@ -2,7 +2,7 @@ - net5.0;netcoreapp3.1;netstandard2.0 + net6.0;netstandard2.0 diff --git a/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj b/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj index cba10d66..68946adf 100644 --- a/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj +++ b/sources/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj @@ -3,7 +3,7 @@ Exe - net5.0;netcoreapp3.1 + net6.0 @@ -38,7 +38,7 @@ C:\Program Files\LLVM\include - /usr/lib/llvm-12/include + /usr/lib/llvm-13/include diff --git a/sources/ClangSharpPInvokeGenerator/Program.cs b/sources/ClangSharpPInvokeGenerator/Program.cs index acc8d852..96d00b73 100644 --- a/sources/ClangSharpPInvokeGenerator/Program.cs +++ b/sources/ClangSharpPInvokeGenerator/Program.cs @@ -454,6 +454,9 @@ public static int Run(InvocationContext context) if (errorList.Any()) { + context.Console.Error.Write($"Error in args for '{files.FirstOrDefault()}'"); + context.Console.Error.Write(Environment.NewLine); + foreach (var error in errorList) { context.Console.Error.Write(error); diff --git a/sources/Directory.Build.targets b/sources/Directory.Build.targets index 7589a80e..15a9ad2e 100644 --- a/sources/Directory.Build.targets +++ b/sources/Directory.Build.targets @@ -39,7 +39,7 @@ diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs index c47c5ae5..3bb3be83 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/StructDeclarationTest.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.Threading.Tasks; -using Xunit; namespace ClangSharp.UnitTests { diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/ClangSharp.PInvokeGenerator.UnitTests.csproj b/tests/ClangSharp.PInvokeGenerator.UnitTests/ClangSharp.PInvokeGenerator.UnitTests.csproj index 8621476d..056a520e 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/ClangSharp.PInvokeGenerator.UnitTests.csproj +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/ClangSharp.PInvokeGenerator.UnitTests.csproj @@ -3,7 +3,7 @@ ClangSharp.UnitTests - net5.0;netcoreapp3.1 + net6.0 diff --git a/tests/ClangSharp.UnitTests/ClangSharp.UnitTests.csproj b/tests/ClangSharp.UnitTests/ClangSharp.UnitTests.csproj index 271eda2f..13148c9d 100644 --- a/tests/ClangSharp.UnitTests/ClangSharp.UnitTests.csproj +++ b/tests/ClangSharp.UnitTests/ClangSharp.UnitTests.csproj @@ -2,7 +2,7 @@ - net5.0;netcoreapp3.1 + net6.0 From fa40fde8992062c3f92e4be1f4f5c8b62789a9d4 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Thu, 28 Oct 2021 19:41:48 -0700 Subject: [PATCH 2/2] Ensure the .NET 6 SDK is installed by CI --- scripts/build.ps1 | 3 +-- scripts/build.sh | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 254ca1a1..54849eae 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -169,8 +169,7 @@ try { $DotNetInstallDirectory = Join-Path -Path $ArtifactsDir -ChildPath "dotnet" Create-Directory -Path $DotNetInstallDirectory - & $DotNetInstallScript -Channel 5.0 -Version latest -InstallDir $DotNetInstallDirectory -Architecture $architecture - & $DotNetInstallScript -Channel 3.1 -Version latest -InstallDir $DotNetInstallDirectory -Architecture $architecture -Runtime dotnet + & $DotNetInstallScript -Channel 6.0 -Version latest -InstallDir $DotNetInstallDirectory -Architecture $architecture $env:PATH="$DotNetInstallDirectory;$env:PATH" } diff --git a/scripts/build.sh b/scripts/build.sh index 1dedd6e2..cd3e9262 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -204,8 +204,7 @@ if [[ ! -z "$architecture" ]]; then DotNetInstallDirectory="$ArtifactsDir/dotnet" CreateDirectory "$DotNetInstallDirectory" - . "$DotNetInstallScript" --channel 5.0 --version latest --install-dir "$DotNetInstallDirectory" --architecture "$architecture" - . "$DotNetInstallScript" --channel 3.1 --version latest --install-dir "$DotNetInstallDirectory" --architecture "$architecture" --runtime dotnet + . "$DotNetInstallScript" --channel 6.0 --version latest --install-dir "$DotNetInstallDirectory" --architecture "$architecture" PATH="$DotNetInstallDirectory:$PATH:" fi