Fixing some tests and simplify when casts are inserted for character literals

This commit is contained in:
Tanner Gooding 2021-09-23 18:56:52 -07:00
Родитель 6a55a5b496
Коммит f9c3c091e1
5 изменённых файлов: 33 добавлений и 9 удалений

Просмотреть файл

@ -188,9 +188,29 @@ namespace ClangSharp
}
else
{
var isPreviousExplicitCast = IsPrevContextStmt<ExplicitCastExpr>(out _);
var castType = "";
if (!isPreviousExplicitCast)
if (IsPrevContextStmt<ImplicitCastExpr>(out var implicitCastExpr))
{
// C# characters are effectively `ushort` while C defaults to "char" which is
// most typically `sbyte`. Due to this we need to insert a correct implicit
// cast to ensure things are correctly handled here.
var castExprTypeName = GetRemappedTypeName(implicitCastExpr, context: null, implicitCastExpr.Type, out _, skipUsing: true);
if (!IsUnsigned(castExprTypeName))
{
castType = "sbyte";
}
else if (implicitCastExpr.Type.Handle.NumBits < 16)
{
// Cast to byte if the target type is less
castType = "byte";
}
}
if (castType != "")
{
outputBuilder.Write("(sbyte)(");
}
@ -199,7 +219,7 @@ namespace ClangSharp
outputBuilder.Write(EscapeCharacter((char)characterLiteral.Value));
outputBuilder.Write('\'');
if (!isPreviousExplicitCast)
if (castType != "")
{
outputBuilder.Write(')');
}

Просмотреть файл

@ -1494,7 +1494,7 @@ namespace ClangSharp
{
if (_config.GenerateUnixTypes)
{
goto case CXTypeKind.CXType_Int;
goto case CXTypeKind.CXType_UInt;
}
else
{

Просмотреть файл

@ -50,7 +50,7 @@ namespace ClangSharp.UnitTests
[InlineData("unsigned short", "7", true, "ushort", "7")]
[InlineData("unsigned int", "8", true, "uint", "8")]
[InlineData("unsigned long long", "9", true, "ulong", "9")]
[InlineData("unsigned short", "'A'", true, "ushort", "(byte)('A')")]
[InlineData("unsigned short", "'A'", true, "ushort", "'A'")]
public abstract Task OptionalParameterTest(string nativeType, string nativeInit, bool expectedNativeTypeNameAttr, string expectedManagedType, string expectedManagedInit);
[Theory]

Просмотреть файл

@ -186,7 +186,7 @@ namespace ClangSharp.Test
public static partial class Methods
{{
[NativeTypeName(""#define MyMacro1 (long)0x80000000L"")]
public const IntPtr MyMacro1 = (IntPtr)(0x80000000);
public const IntPtr MyMacro1 = unchecked((nint)(0x80000000));
[NativeTypeName(""#define MyMacro2 (int)0x80000000"")]
public const int MyMacro2 = unchecked((int)(0x80000000));
@ -227,7 +227,7 @@ namespace ClangSharp.Test
public static partial class Methods
{{
[NativeTypeName(""#define MyMacro3 MyMacro2(3)"")]
public const int MyMacro3 = unchecked((int)(((UIntPtr)(1) << 31) | ((UIntPtr)(2) << 16) | ((UIntPtr)(3))));
public const int MyMacro3 = unchecked((int)(((nuint)(1) << 31) | ((nuint)(2) << 16) | ((nuint)(3))));
}}
}}
";

Просмотреть файл

@ -224,7 +224,11 @@ const GUID IID_IUnknown = {{ 0x00000000, 0x0000, 0x0000, {{ 0xC0, 0x00, 0x00, 0x
<constant name=""MyMacro1"" access=""public"">
<type primitive=""True"">IntPtr</type>
<value>
<code>(IntPtr)(<value>0x80000000</value>)</code>
<unchecked>
<value>
<code>(nint)(<value>0x80000000</value>)</code>
</value>
</unchecked>
</value>
</constant>
<constant name=""MyMacro2"" access=""public"">
@ -285,7 +289,7 @@ const GUID IID_IUnknown = {{ 0x00000000, 0x0000, 0x0000, {{ 0xC0, 0x00, 0x00, 0x
<type primitive=""True"">int</type>
<value>
<unchecked>
<code>((int)(((UIntPtr)(1) &lt;&lt; 31) | ((UIntPtr)(2) &lt;&lt; 16) | ((UIntPtr)(3))))</code>
<code>((int)(((nuint)(1) &lt;&lt; 31) | ((nuint)(2) &lt;&lt; 16) | ((nuint)(3))))</code>
</unchecked>
</value>
</constant>