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
15 changes: 15 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Text;
using ClangSharp.Abstractions;

namespace ClangSharp.CSharp;

Expand Down Expand Up @@ -129,6 +130,20 @@ public void WriteLabel(string name)
WriteLine(':');
}

public void WriteNumberLiteral(ReadOnlySpan<char> value)
{
var index = value.IndexOf('\'');
while (index != -1)
{
var part = value[..index];
Write(part);
Write(['_']);
value = value[(index + 1)..];
index = value.IndexOf('\'');
}
Write(value);
}

public void WriteLine<T>(T value)
{
Write(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private void VisitCallExpr(CallExpr callExpr)
outputBuilder.AddUsingDirective("System.Runtime.CompilerServices");
outputBuilder.Write("Unsafe.CopyBlockUnaligned");
}

VisitArgs(callExpr, args);
break;
}
Expand Down Expand Up @@ -1163,7 +1163,7 @@ private void VisitExplicitCastExpr(ExplicitCastExpr explicitCastExpr)
{
var cursorName = GetCursorName(varDecl);

if (cursorName.StartsWith("ClangSharpMacro_", StringComparison.Ordinal) && _config.WithTransparentStructs.TryGetValue(typeName, out var transparentStruct))
if (cursorName.StartsWith("ClangSharpMacro_", StringComparison.Ordinal) && _config.WithTransparentStructs.TryGetValue(typeName, out var transparentStruct))
{
if (!IsPrimitiveValue(explicitCastExpr, type) || IsConstant(typeName, varDecl.Init))
{
Expand Down Expand Up @@ -1201,12 +1201,12 @@ private void VisitFloatingLiteral(FloatingLiteral floatingLiteral)
var outputBuilder = StartCSharpCode();
if (floatingLiteral.ValueString.EndsWith(".f", StringComparison.Ordinal))
{
outputBuilder.Write(floatingLiteral.ValueString.AsSpan()[..^1]);
outputBuilder.WriteNumberLiteral(floatingLiteral.ValueString.AsSpan()[..^1]);
outputBuilder.Write("0f");
}
else
{
outputBuilder.Write(floatingLiteral.ValueString);
outputBuilder.WriteNumberLiteral(floatingLiteral.ValueString);

if (floatingLiteral.ValueString.EndsWith('.'))
{
Expand Down Expand Up @@ -2060,9 +2060,9 @@ private void VisitIntegerLiteral(IntegerLiteral integerLiteral)
{
valueString = valueString[..^1];
}

var outputBuilder = StartCSharpCode();
outputBuilder.Write(valueString);
outputBuilder.WriteNumberLiteral(valueString);
outputBuilder.Write(valueSuffix);
StopCSharpCode();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests;

public abstract class DigitsSeparatorTest : PInvokeGeneratorTest
{
[TestCase("int", "1'024", "1_024")]
[TestCase("int", "1'000'001", "1_000_001")]
[TestCase("float", "1'024", "1_024")]
[TestCase("float", "1'024.0", "1_024.0")]
public Task StaticConstExprTest(string type, string nativeValue, string expectedValue) => StaticConstExprTestImpl(type, nativeValue, expectedValue);

protected abstract Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests.CSharpCompatibleUnix;

[Platform("unix")]
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
{
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
{
var inputContents = $@"class MyClass
{{
private:

static constexpr {type} x = {nativeValue};
}};
";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public partial struct MyClass
{{
[NativeTypeName(""const {type}"")]
private const {type} x = {expectedValue};
}}
}}
";

return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(
inputContents,
expectedOutputContents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests.CSharpCompatibleWindows;

[Platform("win")]
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
{
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
{
var inputContents = $@"class MyClass
{{
private:

static constexpr {type} x = {nativeValue};
}};
";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public partial struct MyClass
{{
[NativeTypeName(""const {type}"")]
private const {type} x = {expectedValue};
}}
}}
";

return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(
inputContents,
expectedOutputContents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests.CSharpDefaultUnix;

[Platform("unix")]
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
{
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
{
var inputContents = $@"class MyClass
{{
private:

static constexpr {type} x = {nativeValue};
}};
";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public partial struct MyClass
{{
[NativeTypeName(""const {type}"")]
private const {type} x = {expectedValue};
}}
}}
";

return ValidateGeneratedCSharpDefaultUnixBindingsAsync(
inputContents,
expectedOutputContents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests.CSharpDefaultWindows;

[Platform("win")]
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
{
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
{
var inputContents = $@"class MyClass
{{
private:

static constexpr {type} x = {nativeValue};
}};
";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public partial struct MyClass
{{
[NativeTypeName(""const {type}"")]
private const {type} x = {expectedValue};
}}
}}
";

return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(
inputContents,
expectedOutputContents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests.CSharpLatestUnix;

[Platform("unix")]
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
{
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
{
var inputContents = $@"class MyClass
{{
private:

static constexpr {type} x = {nativeValue};
}};
";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public partial struct MyClass
{{
[NativeTypeName(""const {type}"")]
private const {type} x = {expectedValue};
}}
}}
";

return ValidateGeneratedCSharpLatestUnixBindingsAsync(
inputContents,
expectedOutputContents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests.CSharpLatestWindows;

[Platform("win")]
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
{
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
{
var inputContents = $@"class MyClass
{{
private:

static constexpr {type} x = {nativeValue};
}};
";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public partial struct MyClass
{{
[NativeTypeName(""const {type}"")]
private const {type} x = {expectedValue};
}}
}}
";

return ValidateGeneratedCSharpLatestWindowsBindingsAsync(
inputContents,
expectedOutputContents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests.CSharpPreviewUnix;

[Platform("unix")]
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
{
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
{
var inputContents = $@"class MyClass
{{
private:

static constexpr {type} x = {nativeValue};
}};
";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public partial struct MyClass
{{
[NativeTypeName(""const {type}"")]
private const {type} x = {expectedValue};
}}
}}
";

return ValidateGeneratedCSharpPreviewUnixBindingsAsync(
inputContents,
expectedOutputContents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests.CSharpPreviewWindows;

[Platform("win")]
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
{
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
{
var inputContents = $@"class MyClass
{{
private:

static constexpr {type} x = {nativeValue};
}};
";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public partial struct MyClass
{{
[NativeTypeName(""const {type}"")]
private const {type} x = {expectedValue};
}}
}}
";

return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(
inputContents,
expectedOutputContents);
}
}
Loading