Skip to content

Commit 48900c6

Browse files
Merge pull request #639 from davidelettieri/feat/support-digits-separator
feat: support single quote digits separator
2 parents 92e8c55 + db2c90e commit 48900c6

File tree

19 files changed

+636
-6
lines changed

19 files changed

+636
-6
lines changed

sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.Globalization;
77
using System.Text;
8+
using ClangSharp.Abstractions;
89

910
namespace ClangSharp.CSharp;
1011

@@ -129,6 +130,20 @@ public void WriteLabel(string name)
129130
WriteLine(':');
130131
}
131132

133+
public void WriteNumberLiteral(ReadOnlySpan<char> value)
134+
{
135+
var index = value.IndexOf('\'');
136+
while (index != -1)
137+
{
138+
var part = value[..index];
139+
Write(part);
140+
Write(['_']);
141+
value = value[(index + 1)..];
142+
index = value.IndexOf('\'');
143+
}
144+
Write(value);
145+
}
146+
132147
public void WriteLine<T>(T value)
133148
{
134149
Write(value);

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private void VisitCallExpr(CallExpr callExpr)
265265
outputBuilder.AddUsingDirective("System.Runtime.CompilerServices");
266266
outputBuilder.Write("Unsafe.CopyBlockUnaligned");
267267
}
268-
268+
269269
VisitArgs(callExpr, args);
270270
break;
271271
}
@@ -1163,7 +1163,7 @@ private void VisitExplicitCastExpr(ExplicitCastExpr explicitCastExpr)
11631163
{
11641164
var cursorName = GetCursorName(varDecl);
11651165

1166-
if (cursorName.StartsWith("ClangSharpMacro_", StringComparison.Ordinal) && _config.WithTransparentStructs.TryGetValue(typeName, out var transparentStruct))
1166+
if (cursorName.StartsWith("ClangSharpMacro_", StringComparison.Ordinal) && _config.WithTransparentStructs.TryGetValue(typeName, out var transparentStruct))
11671167
{
11681168
if (!IsPrimitiveValue(explicitCastExpr, type) || IsConstant(typeName, varDecl.Init))
11691169
{
@@ -1201,12 +1201,12 @@ private void VisitFloatingLiteral(FloatingLiteral floatingLiteral)
12011201
var outputBuilder = StartCSharpCode();
12021202
if (floatingLiteral.ValueString.EndsWith(".f", StringComparison.Ordinal))
12031203
{
1204-
outputBuilder.Write(floatingLiteral.ValueString.AsSpan()[..^1]);
1204+
outputBuilder.WriteNumberLiteral(floatingLiteral.ValueString.AsSpan()[..^1]);
12051205
outputBuilder.Write("0f");
12061206
}
12071207
else
12081208
{
1209-
outputBuilder.Write(floatingLiteral.ValueString);
1209+
outputBuilder.WriteNumberLiteral(floatingLiteral.ValueString);
12101210

12111211
if (floatingLiteral.ValueString.EndsWith('.'))
12121212
{
@@ -2060,9 +2060,9 @@ private void VisitIntegerLiteral(IntegerLiteral integerLiteral)
20602060
{
20612061
valueString = valueString[..^1];
20622062
}
2063-
2063+
20642064
var outputBuilder = StartCSharpCode();
2065-
outputBuilder.Write(valueString);
2065+
outputBuilder.WriteNumberLiteral(valueString);
20662066
outputBuilder.Write(valueSuffix);
20672067
StopCSharpCode();
20682068
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
2+
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
6+
namespace ClangSharp.UnitTests;
7+
8+
public abstract class DigitsSeparatorTest : PInvokeGeneratorTest
9+
{
10+
[TestCase("int", "1'024", "1_024")]
11+
[TestCase("int", "1'000'001", "1_000_001")]
12+
[TestCase("float", "1'024", "1_024")]
13+
[TestCase("float", "1'024.0", "1_024.0")]
14+
public Task StaticConstExprTest(string type, string nativeValue, string expectedValue) => StaticConstExprTestImpl(type, nativeValue, expectedValue);
15+
16+
protected abstract Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue);
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
2+
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
6+
namespace ClangSharp.UnitTests.CSharpCompatibleUnix;
7+
8+
[Platform("unix")]
9+
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
10+
{
11+
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
12+
{
13+
var inputContents = $@"class MyClass
14+
{{
15+
private:
16+
17+
static constexpr {type} x = {nativeValue};
18+
}};
19+
";
20+
21+
var expectedOutputContents = $@"namespace ClangSharp.Test
22+
{{
23+
public partial struct MyClass
24+
{{
25+
[NativeTypeName(""const {type}"")]
26+
private const {type} x = {expectedValue};
27+
}}
28+
}}
29+
";
30+
31+
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(
32+
inputContents,
33+
expectedOutputContents);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
2+
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
6+
namespace ClangSharp.UnitTests.CSharpCompatibleWindows;
7+
8+
[Platform("win")]
9+
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
10+
{
11+
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
12+
{
13+
var inputContents = $@"class MyClass
14+
{{
15+
private:
16+
17+
static constexpr {type} x = {nativeValue};
18+
}};
19+
";
20+
21+
var expectedOutputContents = $@"namespace ClangSharp.Test
22+
{{
23+
public partial struct MyClass
24+
{{
25+
[NativeTypeName(""const {type}"")]
26+
private const {type} x = {expectedValue};
27+
}}
28+
}}
29+
";
30+
31+
return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(
32+
inputContents,
33+
expectedOutputContents);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
2+
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
6+
namespace ClangSharp.UnitTests.CSharpDefaultUnix;
7+
8+
[Platform("unix")]
9+
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
10+
{
11+
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
12+
{
13+
var inputContents = $@"class MyClass
14+
{{
15+
private:
16+
17+
static constexpr {type} x = {nativeValue};
18+
}};
19+
";
20+
21+
var expectedOutputContents = $@"namespace ClangSharp.Test
22+
{{
23+
public partial struct MyClass
24+
{{
25+
[NativeTypeName(""const {type}"")]
26+
private const {type} x = {expectedValue};
27+
}}
28+
}}
29+
";
30+
31+
return ValidateGeneratedCSharpDefaultUnixBindingsAsync(
32+
inputContents,
33+
expectedOutputContents);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
2+
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
6+
namespace ClangSharp.UnitTests.CSharpDefaultWindows;
7+
8+
[Platform("win")]
9+
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
10+
{
11+
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
12+
{
13+
var inputContents = $@"class MyClass
14+
{{
15+
private:
16+
17+
static constexpr {type} x = {nativeValue};
18+
}};
19+
";
20+
21+
var expectedOutputContents = $@"namespace ClangSharp.Test
22+
{{
23+
public partial struct MyClass
24+
{{
25+
[NativeTypeName(""const {type}"")]
26+
private const {type} x = {expectedValue};
27+
}}
28+
}}
29+
";
30+
31+
return ValidateGeneratedCSharpDefaultWindowsBindingsAsync(
32+
inputContents,
33+
expectedOutputContents);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
2+
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
6+
namespace ClangSharp.UnitTests.CSharpLatestUnix;
7+
8+
[Platform("unix")]
9+
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
10+
{
11+
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
12+
{
13+
var inputContents = $@"class MyClass
14+
{{
15+
private:
16+
17+
static constexpr {type} x = {nativeValue};
18+
}};
19+
";
20+
21+
var expectedOutputContents = $@"namespace ClangSharp.Test
22+
{{
23+
public partial struct MyClass
24+
{{
25+
[NativeTypeName(""const {type}"")]
26+
private const {type} x = {expectedValue};
27+
}}
28+
}}
29+
";
30+
31+
return ValidateGeneratedCSharpLatestUnixBindingsAsync(
32+
inputContents,
33+
expectedOutputContents);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
2+
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
6+
namespace ClangSharp.UnitTests.CSharpLatestWindows;
7+
8+
[Platform("win")]
9+
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
10+
{
11+
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
12+
{
13+
var inputContents = $@"class MyClass
14+
{{
15+
private:
16+
17+
static constexpr {type} x = {nativeValue};
18+
}};
19+
";
20+
21+
var expectedOutputContents = $@"namespace ClangSharp.Test
22+
{{
23+
public partial struct MyClass
24+
{{
25+
[NativeTypeName(""const {type}"")]
26+
private const {type} x = {expectedValue};
27+
}}
28+
}}
29+
";
30+
31+
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(
32+
inputContents,
33+
expectedOutputContents);
34+
}
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
2+
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
6+
namespace ClangSharp.UnitTests.CSharpPreviewUnix;
7+
8+
[Platform("unix")]
9+
public sealed class DigitsSeparatorTest : UnitTests.DigitsSeparatorTest
10+
{
11+
protected override Task StaticConstExprTestImpl(string type, string nativeValue, string expectedValue)
12+
{
13+
var inputContents = $@"class MyClass
14+
{{
15+
private:
16+
17+
static constexpr {type} x = {nativeValue};
18+
}};
19+
";
20+
21+
var expectedOutputContents = $@"namespace ClangSharp.Test
22+
{{
23+
public partial struct MyClass
24+
{{
25+
[NativeTypeName(""const {type}"")]
26+
private const {type} x = {expectedValue};
27+
}}
28+
}}
29+
";
30+
31+
return ValidateGeneratedCSharpPreviewUnixBindingsAsync(
32+
inputContents,
33+
expectedOutputContents);
34+
}
35+
}

0 commit comments

Comments
 (0)