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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<PackageValidationBaselineVersion>17.0.0</PackageValidationBaselineVersion>
<Product>ClangSharp</Product>
<RootNamespace>ClangSharp</RootNamespace>
<VersionPrefix>23.1.5.0</VersionPrefix>
<VersionPrefix>23.1.5.1</VersionPrefix>
<VersionSuffix Condition="'$(PACKAGE_PUBLISH_MODE)' != 'stable'">rc1</VersionSuffix>
<VersionSuffix Condition="'$(GITHUB_EVENT_NAME)' == 'pull_request'">pr</VersionSuffix>
</PropertyGroup>
Expand Down
13 changes: 11 additions & 2 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,17 @@ private void VisitBinaryOperator(BinaryOperator binaryOperator)
// A C enumerator has type int, but it is emitted as a member of its generated enum, which C# cannot shift by
case Expr when IsStmtAsWritten<DeclRefExpr>(rhs, out var declRefExpr, removeParens: true) && (declRefExpr.Decl is EnumConstantDecl):
{
outputBuilder.Write("(int)");
ParenthesizeStmt(binaryOperator.RHS);
// In C++ the enumerator is promoted via an implicit cast to int, which already emits the `(int)` cast
// except within an enumerator initializer, where the reference is emitted without a cast
if ((rhs is ImplicitCastExpr { Type.CanonicalType.Kind: CXType_Int }) && !IsPrevContextDecl<EnumConstantDecl>(out _, out _))
{
Visit(binaryOperator.RHS);
}
else
{
outputBuilder.Write("(int)");
ParenthesizeStmt(binaryOperator.RHS);
}
break;
}
// Already the correct type or implicitly castable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +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.IO;
using System.Threading.Tasks;
using NUnit.Framework;
Expand All @@ -9,23 +10,32 @@ namespace ClangSharp.UnitTests.Baseline;
// Shared baseline read/write core used by both the matrix-parameterized BaselineTest and the
// single-variant StandaloneBaselineTest. Given the already-generated output for a case+variant, it either
// (re)writes the checked-in baseline under UPDATE_BASELINES, or resolves the baseline via the fallback
// chain and asserts byte-for-byte equality.
// chain and asserts equality after resolving the generator version placeholder.
internal static class BaselineAssertions
{
private const string GeneratedCodeAttributePlaceholder = "GeneratedCode(\"ClangSharp\", \"{ClangSharpVersion}\")";
private static readonly string s_generatedCodeAttribute = $"GeneratedCode(\"ClangSharp\", \"{typeof(PInvokeGenerator).Assembly.GetName().Version}\")";

internal static string NormalizeGeneratedCodeVersion(string actual)
=> actual.Replace(s_generatedCodeAttribute, GeneratedCodeAttributePlaceholder, StringComparison.Ordinal);

internal static string ResolveGeneratedCodeVersion(string expected)
=> expected.Replace(GeneratedCodeAttributePlaceholder, s_generatedCodeAttribute, StringComparison.Ordinal);

public static async Task AssertOrUpdateAsync(string area, string caseName, BaselineVariant variant, string actual)
{
if (BaselineHarness.ShouldUpdateBaselines)
{
var path = BaselineHarness.MostSpecificPath(area, caseName, variant);
_ = Directory.CreateDirectory(Path.GetDirectoryName(path)!);
await File.WriteAllTextAsync(path, actual).ConfigureAwait(false);
await File.WriteAllTextAsync(path, NormalizeGeneratedCodeVersion(actual)).ConfigureAwait(false);
return;
}

var resolved = BaselineHarness.ResolveBaselinePath(area, caseName, variant);
Assert.That(resolved, Is.Not.Null, $"No baseline found for case '{caseName}' variant '{variant}'. Run with UPDATE_BASELINES=1 (Windows variants) or seed from the legacy tests (Unix variants).");

var expected = await File.ReadAllTextAsync(resolved!).ConfigureAwait(false);
Assert.That(actual, Is.EqualTo(expected));
Assert.That(actual, Is.EqualTo(ResolveGeneratedCodeVersion(expected)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 NUnit.Framework;

namespace ClangSharp.UnitTests.Baseline;

public sealed class BaselineAssertionsTest
{
[Test]
public void GeneratedCodeVersionIsResolvedAndNormalized()
{
const string baseline = "[assembly: GeneratedCode(\"ClangSharp\", \"{ClangSharpVersion}\")]";
var actual = $"[assembly: GeneratedCode(\"ClangSharp\", \"{typeof(PInvokeGenerator).Assembly.GetName().Version}\")]";

Assert.That(BaselineAssertions.ResolveGeneratedCodeVersion(baseline), Is.EqualTo(actual));
Assert.That(BaselineAssertions.NormalizeGeneratedCodeVersion(actual), Is.EqualTo(baseline));
Assert.That(BaselineAssertions.NormalizeGeneratedCodeVersion("GeneratedCode(\"ClangSharp\", \"wrong\")"), Is.EqualTo("GeneratedCode(\"ClangSharp\", \"wrong\")"));
Assert.That("[assembly: GeneratedCode(\"ClangSharp\", \"wrong\")]", Is.Not.EqualTo(BaselineAssertions.ResolveGeneratedCodeVersion(baseline)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using static ClangSharp.Test.MyEnum;

namespace ClangSharp.Test
{
public enum MyEnum
{
MyEnum_Shift = 24,
MyEnum_Value = 1 << (int)(MyEnum_Shift),
}

public static partial class Methods
{
[return: NativeTypeName("unsigned char")]
public static byte MyFunction([NativeTypeName("unsigned int")] uint x)
{
return (byte)(x >> (int)(MyEnum_Shift));
}

[return: NativeTypeName("unsigned int")]
public static uint MyFunction2([NativeTypeName("unsigned int")] uint x)
{
return x << (int)(MyEnum_Shift);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<bindings>
<namespace name="ClangSharp.Test">
<enumeration name="MyEnum" access="public">
<type>int</type>
<enumerator name="MyEnum_Shift" access="public">
<type primitive="False">int</type>
<value>
<code>24</code>
</value>
</enumerator>
<enumerator name="MyEnum_Value" access="public">
<type primitive="False">int</type>
<value>
<code>1 &lt;&lt; (int)(<value>MyEnum_Shift</value>)</code>
</value>
</enumerator>
</enumeration>
<class name="Methods" access="public" static="true">
<function name="MyFunction" access="public" static="true">
<type native="unsigned char">byte</type>
<param name="x">
<type>uint</type>
</param>
<code>return (byte)(x &gt;&gt; (int)(<value>MyEnum_Shift</value>));</code>
</function>
<function name="MyFunction2" access="public" static="true">
<type native="unsigned int">uint</type>
<param name="x">
<type>uint</type>
</param>
<code>return x &lt;&lt; (int)(MyEnum_Shift);</code>
</function>
</class>
</namespace>
</bindings>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Diagnostics;
using System.Runtime.InteropServices;

[assembly: GeneratedCode("ClangSharp", "23.1.5.0")]
[assembly: GeneratedCode("ClangSharp", "{ClangSharpVersion}")]

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

namespace ClangSharp.Test
{
[GeneratedCode("ClangSharp", "23.1.5.0")]
[GeneratedCode("ClangSharp", "{ClangSharpVersion}")]
public enum MyEnum
{
MyEnum_Value,
}

[GeneratedCode("ClangSharp", "23.1.5.0")]
[GeneratedCode("ClangSharp", "{ClangSharpVersion}")]
public partial struct MyStruct
{
public int value;
}

[GeneratedCode("ClangSharp", "23.1.5.0")]
[GeneratedCode("ClangSharp", "{ClangSharpVersion}")]
public static unsafe partial class Methods
{
[DllImport("ClangSharpPInvokeGenerator", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?MyFunction@@YAXP6AXH@Z@Z", ExactSpelling = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@

namespace ClangSharp.Test
{
[GeneratedCode("ClangSharp", "23.1.5.0")]
[GeneratedCode("ClangSharp", "{ClangSharpVersion}")]
public enum MyEnum
{
MyEnum_Value,
}

[GeneratedCode("ClangSharp", "23.1.5.0")]
[GeneratedCode("ClangSharp", "{ClangSharpVersion}")]
public partial struct MyStruct
{
public int value;
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
[GeneratedCode("ClangSharp", "23.1.5.0")]
[GeneratedCode("ClangSharp", "{ClangSharpVersion}")]
public delegate void MyCallback(int value);

[GeneratedCode("ClangSharp", "23.1.5.0")]
[GeneratedCode("ClangSharp", "{ClangSharpVersion}")]
public static partial class Methods
{
[DllImport("ClangSharpPInvokeGenerator", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?MyFunction@@YAXP6AXH@Z@Z", ExactSpelling = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

namespace ClangSharp.Test
{
[GeneratedCode("ClangSharp", "23.1.5.0")]
[GeneratedCode("ClangSharp", "{ClangSharpVersion}")]
public enum MyEnum
{
MyEnum_Value,
}

[GeneratedCode("ClangSharp", "23.1.5.0")]
[GeneratedCode("ClangSharp", "{ClangSharpVersion}")]
public partial struct MyStruct
{
public int value;
}

[GeneratedCode("ClangSharp", "23.1.5.0")]
[GeneratedCode("ClangSharp", "{ClangSharpVersion}")]
public static unsafe partial class Methods
{
[DllImport("ClangSharpPInvokeGenerator", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?MyFunction@@YAXP6AXH@Z@Z", ExactSpelling = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Diagnostics;
using System.Runtime.InteropServices;

[assembly: GeneratedCode("ClangSharp", "23.1.5.0")]
[assembly: GeneratedCode("ClangSharp", "{ClangSharpVersion}")]

namespace ClangSharp.Test
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Diagnostics;
using System.Runtime.InteropServices;

[assembly: GeneratedCode("ClangSharp", "23.1.5.0")]
[assembly: GeneratedCode("ClangSharp", "{ClangSharpVersion}")]

namespace ClangSharp.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.CodeDom.Compiler;
using System.Diagnostics;

[assembly: GeneratedCode("ClangSharp", "23.1.5.0")]
[assembly: GeneratedCode("ClangSharp", "{ClangSharpVersion}")]

namespace ClangSharp.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.CodeDom.Compiler;
using System.Diagnostics;

[assembly: GeneratedCode("ClangSharp", "23.1.5.0")]
[assembly: GeneratedCode("ClangSharp", "{ClangSharpVersion}")]

namespace ClangSharp.Test
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.CodeDom.Compiler;
using System.Diagnostics;

[assembly: GeneratedCode("ClangSharp", "23.1.5.0")]
[assembly: GeneratedCode("ClangSharp", "{ClangSharpVersion}")]

namespace ClangSharp.Test
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,29 @@ static inline int MyFunction(MyEnum x)
return ValidateAsync(nameof(CompareMultipleEnumTest), inputContents);
}

[Test]
public Task EnumeratorShiftTest()
{
var inputContents = @"enum MyEnum : int
{
MyEnum_Shift = 24,
MyEnum_Value = 1 << MyEnum_Shift,
};

static inline unsigned char MyFunction(unsigned int x)
{
return (unsigned char)(x >> MyEnum_Shift);
}

static inline unsigned int MyFunction2(unsigned int x)
{
return x << (MyEnum_Shift);
}
";

return ValidateAsync(nameof(EnumeratorShiftTest), inputContents);
}

[Test]
public Task ConditionalOperatorTest()
{
Expand Down
Loading