Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
3 changes: 1 addition & 2 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp.Interop/ClangSharp.Interop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

<PropertyGroup>
<RootNamespace>ClangSharp</RootNamespace>
<TargetFrameworks>net5.0;netcoreapp3.1;netstandard2.0</TargetFrameworks>
<LangVersion>9</LangVersion>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
20 changes: 20 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
17 changes: 17 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2246,6 +2246,7 @@ private void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr unaryExprOrT
}

var parentType = null as Type;
var parentTypeIsVariableSized = false;

if (IsPrevContextStmt<CallExpr>(out var callExpr, out _))
{
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
76 changes: 48 additions & 28 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
{
Expand Down Expand Up @@ -2413,37 +2440,30 @@ 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;

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))
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp/ClangSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="'$(ContinuousIntegrationBuild)' == 'true'">
Expand Down Expand Up @@ -38,7 +38,7 @@
<!-- Auto-detect LLVM... more or less -->
<PropertyGroup Condition="'$(LLVMIncludePath)' == ''">
<LLVMIncludePath Condition="'$(OS)' == 'Windows_NT'">C:\Program Files\LLVM\include</LLVMIncludePath>
<LLVMIncludePath Condition="'$(OS)' != 'Windows_NT'">/usr/lib/llvm-12/include</LLVMIncludePath>
<LLVMIncludePath Condition="'$(OS)' != 'Windows_NT'">/usr/lib/llvm-13/include</LLVMIncludePath>
</PropertyGroup>

</Project>
3 changes: 3 additions & 0 deletions sources/ClangSharpPInvokeGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion sources/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<Target Name="GenerateSkipLocalsInit"
BeforeTargets="BeforeCompile;CoreCompile"
DependsOnTargets="PrepareForBuild"
Condition="'$(Language)' == 'C#' And '$(TargetFramework)' == 'net5.0'"
Condition="'$(Language)' == 'C#' And '$(TargetFramework)' == 'net6.0'"
Inputs="$(MSBuildAllProjects)"
Outputs="$(GeneratedSkipLocalsInitFile)">
<WriteLinesToFile Lines="$(GeneratedSkipLocalsInitFileLines)" Overwrite="true" WriteOnlyWhenDifferent="true" File="$(GeneratedSkipLocalsInitFile)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Xunit;

namespace ClangSharp.UnitTests
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<RootNamespace>ClangSharp.UnitTests</RootNamespace>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion tests/ClangSharp.UnitTests/ClangSharp.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down