Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for DisableRuntimeMarshalling and CallConvMemberFunction #480

Merged
merged 9 commits into from
Oct 16, 2023
23 changes: 22 additions & 1 deletion sources/ClangSharp.Interop/ClangSharp.Interop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<NoWarn>$(NoWarn);CA1069</NoWarn>
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
<!-- CA1003: Use generic event handler instances -->
<!-- CA1008: Enums should have zero value -->
<!-- CA1027: Mark enums with FlagsAttribute -->
<!-- CA1034: Nested types should not be visible -->
<!-- CA1051: Do not declare visible instance fields -->
<!-- CA1069: Enums should not have duplicate values -->
<!-- CA1305: Specify IFormatProvider -->
<!-- CA1508: Avoid dead conditional code -->
<!-- CA1707: Identifiers should not contain underscores -->
<!-- CA1708: Identifiers should differ by more than case -->
<!-- CA1710: Identifiers should have correct suffix -->
<!-- CA1711: Identifiers should not have incorrect suffix -->
<!-- CA1712: Do not prefix enum values with type name -->
<!-- CA1720: Identifiers should not contain type names -->
<!-- CA1721: Property names should not match get methods -->
<!-- CA1724: Type names should not match namespaces -->
<!-- CA1815: Override equals and operator equals on value types -->
<!-- CA2225: Operator overloads have named alternates -->
<NoWarn>$(NoWarn);CA1003;CA1008;CA1027;CA1034;CA1051;CA1069;CA1305;CA1508;CA1707;CA1708;CA1710;CA1711;CA1712;CA1720;CA1721;CA1724;CA1815;CA2225</NoWarn>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Remove="clang.cs" />
<InternalsVisibleTo Include="ClangSharp" Key="$(AssemblyOriginatorPublicKey)" />
Expand Down
5 changes: 4 additions & 1 deletion sources/ClangSharp.Interop/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// 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;
using System.Runtime.InteropServices;

[assembly: DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]

namespace ClangSharp.Interop;

Expand All @@ -27,4 +30,4 @@ private static bool GetAppContextData(string name, bool defaultValue)
return defaultValue;
}
}
}
}
9 changes: 2 additions & 7 deletions sources/ClangSharp.Interop/Extensions/CXClientData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@

namespace ClangSharp.Interop;

public unsafe partial struct CXClientData : IEquatable<CXClientData>
public unsafe partial struct CXClientData(IntPtr handle) : IEquatable<CXClientData>
{
public CXClientData(IntPtr handle)
{
Handle = handle;
}

public IntPtr Handle { get; set; }
public IntPtr Handle { get; set; } = handle;

public static explicit operator CXClientData(void* value) => new CXClientData((IntPtr)value);

Expand Down
9 changes: 2 additions & 7 deletions sources/ClangSharp.Interop/Extensions/CXCompileCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@

namespace ClangSharp.Interop;

public unsafe partial struct CXCompileCommands : IDisposable, IEquatable<CXCompileCommands>, IReadOnlyCollection<CXCompileCommand>
public unsafe partial struct CXCompileCommands(IntPtr handle) : IDisposable, IEquatable<CXCompileCommands>, IReadOnlyCollection<CXCompileCommand>
{
public CXCompileCommands(IntPtr handle)
{
Handle = handle;
}

public readonly CXCompileCommand this[uint index] => GetCommand(index);

public readonly int Count => (int)Size;

public IntPtr Handle { get; set; }
public IntPtr Handle { get; set; } = handle;

public readonly uint Size => clang.CompileCommands_getSize(this);

Expand Down
11 changes: 3 additions & 8 deletions sources/ClangSharp.Interop/Extensions/CXDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@

namespace ClangSharp.Interop;

public unsafe partial struct CXDiagnostic : IDisposable, IEquatable<CXDiagnostic>
public unsafe partial struct CXDiagnostic(IntPtr handle) : IDisposable, IEquatable<CXDiagnostic>
{
public CXDiagnostic(IntPtr handle)
{
Handle = handle;
}

public static CXDiagnosticDisplayOptions DefaultDisplayOptions => (CXDiagnosticDisplayOptions)clang.defaultDiagnosticDisplayOptions();

public readonly uint Category => clang.getDiagnosticCategory(this);
Expand All @@ -19,7 +14,7 @@ public CXDiagnostic(IntPtr handle)

public readonly CXDiagnosticSet ChildDiagnostics => (CXDiagnosticSet)clang.getChildDiagnostics(this);

public IntPtr Handle { get; set; }
public IntPtr Handle { get; set; } = handle;

public readonly CXSourceLocation Location => clang.getDiagnosticLocation(this);

Expand Down Expand Up @@ -54,7 +49,7 @@ public void Dispose()

public readonly CXString Format(CXDiagnosticDisplayOptions options) => clang.formatDiagnostic(this, (uint)options);

[Obsolete("Use " + nameof(CategoryText) + " instead.")]
[Obsolete($"Use {nameof(CategoryText)} instead.")]
public static CXString GetCategoryName(uint category) => clang.getDiagnosticCategoryName(category);

public readonly CXString GetFixIt(uint fixIt, out CXSourceRange replacementRange)
Expand Down
2 changes: 1 addition & 1 deletion sources/ClangSharp.Interop/Internals/MarshaledString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public MarshaledString(string? input)
}
else
{
var valueBytes = (input.Length != 0) ? Encoding.UTF8.GetBytes(input) : Array.Empty<byte>();
var valueBytes = (input.Length != 0) ? Encoding.UTF8.GetBytes(input) : [];
length = valueBytes.Length;
value = Marshal.AllocHGlobal(length + 1);
Marshal.Copy(valueBytes, 0, value, length);
Expand Down
18 changes: 3 additions & 15 deletions sources/ClangSharp.Interop/Shims/MemberNotNullWhenAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,9 @@
namespace System.Diagnostics.CodeAnalysis;

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
internal sealed class MemberNotNullWhenAttribute : Attribute
internal sealed class MemberNotNullWhenAttribute(bool returnValue, params string[] members) : Attribute
{
public MemberNotNullWhenAttribute(bool returnValue, string member)
{
ReturnValue = returnValue;
Members = new[] { member };
}
public bool ReturnValue { get; } = returnValue;

public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
{
ReturnValue = returnValue;
Members = members;
}

public bool ReturnValue { get; }

public string[] Members { get; }
public string[] Members { get; } = members;
}
2 changes: 1 addition & 1 deletion sources/ClangSharp.Interop/clang.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static IntPtr OnDllImport(string libraryName, Assembly assembly, DllImpo
return nativeLibrary;
}

if (libraryName.Equals("libclang") && TryResolveClang(assembly, searchPath, out nativeLibrary))
if (libraryName.Equals("libclang", StringComparison.Ordinal) && TryResolveClang(assembly, searchPath, out nativeLibrary))
{
return nativeLibrary;
}
Expand Down
11 changes: 1 addition & 10 deletions sources/ClangSharp.PInvokeGenerator/Abstractions/BitfieldDesc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@

namespace ClangSharp.Abstractions;

public struct BitfieldDesc
internal struct BitfieldDesc
{
public Type TypeBacking { get; set; }

public List<BitfieldRegion> Regions { get; set; }
}

public struct BitfieldRegion
{
public string Name { get; set; }

public long Offset { get; set; }

public long Length { get; set; }
}
12 changes: 12 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/Abstractions/BitfieldRegion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

namespace ClangSharp.Abstractions;

internal struct BitfieldRegion
{
public string Name { get; set; }

public long Offset { get; set; }

public long Length { get; set; }
}
13 changes: 13 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/Abstractions/CallConv.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

namespace ClangSharp.Abstractions;

internal enum CallConv
{
Winapi = 1,
Cdecl = 2,
StdCall = 3,
ThisCall = 4,
FastCall = 5,
MemberFunction = 6,
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace ClangSharp.Abstractions;

public struct EnumDesc
internal struct EnumDesc
{
public AccessSpecifier AccessSpecifier { get; set; }
public string TypeName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace ClangSharp.Abstractions;

[Flags]
public enum EnumFlags
internal enum EnumFlags
{
None = 0,
Nested = 1 << 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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;
using System.Runtime.InteropServices;
using ClangSharp.Interop;

namespace ClangSharp.Abstractions;
Expand All @@ -15,7 +14,7 @@ internal struct FunctionOrDelegateDesc
public string ParentName { get; set; }
public string? LibraryPath { get; set; }
public string ReturnType { get; set; }
public CallingConvention CallingConvention { get; set; }
public CallConv CallingConvention { get; set; }
public FunctionOrDelegateFlags Flags { get; set; }
public long? VtblIndex { get; set; }
public CXSourceLocation? Location { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace ClangSharp.Abstractions;

[Flags]
public enum StructFlags
internal enum StructFlags
{
None = 0,
Unsafe = 1 << 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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.

namespace ClangSharp.Abstractions;
namespace ClangSharp;

public enum AccessSpecifier : byte
public enum AccessSpecifier
{
None,
Public,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
// 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;
using System.Globalization;
using System.Runtime.CompilerServices;

namespace ClangSharp.CSharp;

internal partial class CSharpOutputBuilder
{
private bool _customAttrIsForParameter = false;
private bool _customAttrIsForParameter;

public void WriteCustomAttribute(string attribute, Action? callback = null)
{
if (attribute.Equals("Flags") || attribute.Equals("Obsolete"))
if (attribute.Equals("Flags", StringComparison.Ordinal) || attribute.Equals("Obsolete", StringComparison.Ordinal))
{
AddUsingDirective("System");
}
else if (attribute.Equals("EditorBrowsable") || attribute.StartsWith("EditorBrowsable("))
else if (attribute.Equals("EditorBrowsable", StringComparison.Ordinal) || attribute.StartsWith("EditorBrowsable(", StringComparison.Ordinal))
{
AddUsingDirective("System.ComponentModel");
}
else if (attribute.Equals("UnscopedRef"))
else if (attribute.Equals("UnscopedRef", StringComparison.Ordinal))
{
AddUsingDirective("System.Diagnostics.CodeAnalysis");
}
else if (attribute.StartsWith("InlineArray("))
else if (attribute.StartsWith("InlineArray(", StringComparison.Ordinal))
{
AddUsingDirective("System.Runtime.CompilerServices");
}
else if (attribute.StartsWith("Guid(")|| attribute.Equals("Optional") || attribute.StartsWith("Optional, DefaultParameterValue("))
else if (attribute.StartsWith("Guid(", StringComparison.Ordinal) || attribute.Equals("Optional", StringComparison.Ordinal) || attribute.StartsWith("Optional, DefaultParameterValue(", StringComparison.Ordinal))
{
AddUsingDirective("System.Runtime.InteropServices");
}
else if (attribute.StartsWith("SupportedOSPlatform("))
else if (attribute.StartsWith("SupportedOSPlatform(", StringComparison.Ordinal))
{
AddUsingDirective("System.Runtime.Versioning");
}
Expand Down Expand Up @@ -85,7 +87,17 @@ public void WriteIid(string name, Guid value)
WriteIndentedLine("get");
WriteBlockStart();

WriteIndentedLine("ReadOnlySpan<byte> data = new byte[] {");
WriteIndented("ReadOnlySpan<byte> data = ");

if (_config.GeneratePreviewCode)
{
WriteLine('[');
}
else
{
WriteLine("new byte[] {");
}

IncreaseIndentation();
WriteIndentation();

Expand All @@ -111,7 +123,17 @@ public void WriteIid(string name, Guid value)

WriteNewline();
DecreaseIndentation();
WriteIndentedLine("};");

if (_config.GeneratePreviewCode)
{
WriteIndented(']');
}
else
{
WriteIndented('}');
}

WriteLine(';');

NeedsNewline = true;

Expand All @@ -123,7 +145,12 @@ public void WriteIid(string name, Guid value)
}
else
{
var valueString = value.ToString("X").ToUpperInvariant().Replace("{", "").Replace("}", "").Replace('X', 'x').Replace(",", ", ");
var valueString = value.ToString("X", CultureInfo.InvariantCulture)
.ToUpperInvariant()
.Replace("{", "", StringComparison.Ordinal)
.Replace("}", "", StringComparison.Ordinal)
.Replace('X', 'x')
.Replace(",", ", ", StringComparison.Ordinal);
WriteIid(name, valueString);
}
}
Expand Down
Loading