Skip to content

Commit f810ca6

Browse files
committed
Generate valid C# when passing a const ref to char
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent caed19e commit f810ca6

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
557557
if (Context.Context.Options.MarshalCharAsManagedChar &&
558558
primitive == PrimitiveType.Char)
559559
{
560-
Context.Return.Write($"({typePrinter.PrintNative(pointer)}) ");
561-
Context.Return.Write(param.Name);
560+
Context.Return.Write($"({typePrinter.PrintNative(pointer)}) &{param.Name}");
562561
return true;
563562
}
564563

tests/CSharp/CSharp.Tests.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public void TestUncompilableCode()
9494
{
9595
}
9696

97-
CSharp.CSharp.ReturnCharPointer();
9897
int value = 5;
9998
IntPtr intPtr = CSharp.CSharp.RValueReferenceToPointer((void**) &value);
10099
Assert.That((int) intPtr, Is.EqualTo(value));
@@ -103,6 +102,14 @@ public void TestUncompilableCode()
103102
#pragma warning restore 0219
104103
}
105104

105+
[Test]
106+
public void TestReturnCharPointer()
107+
{
108+
Assert.That(new IntPtr(CSharp.CSharp.ReturnCharPointer()), Is.EqualTo(IntPtr.Zero));
109+
const char z = 'z';
110+
Assert.That(*CSharp.CSharp.TakeConstCharRef(z), Is.EqualTo(z));
111+
}
112+
106113
[Test]
107114
public void TestIndexer()
108115
{

tests/CSharp/CSharp.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,11 @@ char* returnCharPointer()
15661566
return 0;
15671567
}
15681568

1569+
char* takeConstCharRef(const char& c)
1570+
{
1571+
return const_cast<char*>(&c);
1572+
}
1573+
15691574
const char*& takeConstCharStarRef(const char*& c)
15701575
{
15711576
return c;

tests/CSharp/CSharp.h

+1
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,7 @@ class DLL_API HasFunctionPtrField
13121312

13131313
DLL_API void va_listFunction(va_list v);
13141314
DLL_API char* returnCharPointer();
1315+
DLL_API char* takeConstCharRef(const char& c);
13151316
DLL_API const char*& takeConstCharStarRef(const char*& c);
13161317
DLL_API const void*& rValueReferenceToPointer(void*&& v);
13171318

0 commit comments

Comments
 (0)