Skip to content

Commit f9c3c09

Browse files
committed
Fixing some tests and simplify when casts are inserted for character literals
1 parent 6a55a5b commit f9c3c09

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,29 @@ private void VisitCharacterLiteral(CharacterLiteral characterLiteral)
188188
}
189189
else
190190
{
191-
var isPreviousExplicitCast = IsPrevContextStmt<ExplicitCastExpr>(out _);
191+
var castType = "";
192192

193-
if (!isPreviousExplicitCast)
193+
if (IsPrevContextStmt<ImplicitCastExpr>(out var implicitCastExpr))
194+
{
195+
// C# characters are effectively `ushort` while C defaults to "char" which is
196+
// most typically `sbyte`. Due to this we need to insert a correct implicit
197+
// cast to ensure things are correctly handled here.
198+
199+
var castExprTypeName = GetRemappedTypeName(implicitCastExpr, context: null, implicitCastExpr.Type, out _, skipUsing: true);
200+
201+
if (!IsUnsigned(castExprTypeName))
202+
{
203+
castType = "sbyte";
204+
}
205+
else if (implicitCastExpr.Type.Handle.NumBits < 16)
206+
{
207+
// Cast to byte if the target type is less
208+
209+
castType = "byte";
210+
}
211+
}
212+
213+
if (castType != "")
194214
{
195215
outputBuilder.Write("(sbyte)(");
196216
}
@@ -199,7 +219,7 @@ private void VisitCharacterLiteral(CharacterLiteral characterLiteral)
199219
outputBuilder.Write(EscapeCharacter((char)characterLiteral.Value));
200220
outputBuilder.Write('\'');
201221

202-
if (!isPreviousExplicitCast)
222+
if (castType != "")
203223
{
204224
outputBuilder.Write(')');
205225
}

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ private string GetTypeName(Cursor cursor, Cursor context, Type rootType, Type ty
14941494
{
14951495
if (_config.GenerateUnixTypes)
14961496
{
1497-
goto case CXTypeKind.CXType_Int;
1497+
goto case CXTypeKind.CXType_UInt;
14981498
}
14991499
else
15001500
{

tests/ClangSharp.PInvokeGenerator.UnitTests/Base/FunctionDeclarationDllImportTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public abstract class FunctionDeclarationDllImportTest : PInvokeGeneratorTest
5050
[InlineData("unsigned short", "7", true, "ushort", "7")]
5151
[InlineData("unsigned int", "8", true, "uint", "8")]
5252
[InlineData("unsigned long long", "9", true, "ulong", "9")]
53-
[InlineData("unsigned short", "'A'", true, "ushort", "(byte)('A')")]
53+
[InlineData("unsigned short", "'A'", true, "ushort", "'A'")]
5454
public abstract Task OptionalParameterTest(string nativeType, string nativeInit, bool expectedNativeTypeNameAttr, string expectedManagedType, string expectedManagedInit);
5555

5656
[Theory]

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/VarDeclarationTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ namespace ClangSharp.Test
186186
public static partial class Methods
187187
{{
188188
[NativeTypeName(""#define MyMacro1 (long)0x80000000L"")]
189-
public const IntPtr MyMacro1 = (IntPtr)(0x80000000);
189+
public const IntPtr MyMacro1 = unchecked((nint)(0x80000000));
190190
191191
[NativeTypeName(""#define MyMacro2 (int)0x80000000"")]
192192
public const int MyMacro2 = unchecked((int)(0x80000000));
@@ -227,7 +227,7 @@ namespace ClangSharp.Test
227227
public static partial class Methods
228228
{{
229229
[NativeTypeName(""#define MyMacro3 MyMacro2(3)"")]
230-
public const int MyMacro3 = unchecked((int)(((UIntPtr)(1) << 31) | ((UIntPtr)(2) << 16) | ((UIntPtr)(3))));
230+
public const int MyMacro3 = unchecked((int)(((nuint)(1) << 31) | ((nuint)(2) << 16) | ((nuint)(3))));
231231
}}
232232
}}
233233
";

tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/VarDeclarationTest.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,11 @@ public override Task UncheckedConversionMacroTest()
224224
<constant name=""MyMacro1"" access=""public"">
225225
<type primitive=""True"">IntPtr</type>
226226
<value>
227-
<code>(IntPtr)(<value>0x80000000</value>)</code>
227+
<unchecked>
228+
<value>
229+
<code>(nint)(<value>0x80000000</value>)</code>
230+
</value>
231+
</unchecked>
228232
</value>
229233
</constant>
230234
<constant name=""MyMacro2"" access=""public"">
@@ -285,7 +289,7 @@ public override Task UncheckedConversionMacroTest2()
285289
<type primitive=""True"">int</type>
286290
<value>
287291
<unchecked>
288-
<code>((int)(((UIntPtr)(1) &lt;&lt; 31) | ((UIntPtr)(2) &lt;&lt; 16) | ((UIntPtr)(3))))</code>
292+
<code>((int)(((nuint)(1) &lt;&lt; 31) | ((nuint)(2) &lt;&lt; 16) | ((nuint)(3))))</code>
289293
</unchecked>
290294
</value>
291295
</constant>

0 commit comments

Comments
 (0)