Skip to content

Commit 458293e

Browse files
committed
Generate valid C# for returned const void pointers
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 9e21bcd commit 458293e

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

+7
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,14 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
182182
}
183183

184184
if (new QualifiedType(pointer, quals).IsConstRefToPrimitive())
185+
{
186+
if (finalPointee.IsPrimitiveType(PrimitiveType.Void))
187+
{
188+
Context.Return.Write($"new {typePrinter.IntPtrType}(*{Context.ReturnVarName})");
189+
return true;
190+
}
185191
Context.Return.Write("*");
192+
}
186193

187194
Context.Return.Write(Context.ReturnVarName);
188195
return true;

tests/CSharp/CSharp.Tests.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ public void TestUncompilableCode()
9595
}
9696

9797
CSharp.CSharp.ReturnCharPointer();
98-
CSharp.CSharp.RValueReferenceToPointer(null);
98+
int value = 5;
99+
IntPtr intPtr = CSharp.CSharp.RValueReferenceToPointer((void**) &value);
100+
Assert.That((int) intPtr, Is.EqualTo(value));
99101

100102
#pragma warning restore 0168
101103
#pragma warning restore 0219

tests/CSharp/CSharp.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1565,8 +1565,9 @@ const char*& takeConstCharStarRef(const char*& c)
15651565
return c;
15661566
}
15671567

1568-
void rValueReferenceToPointer(void*&& v)
1568+
const void*& rValueReferenceToPointer(void*&& v)
15691569
{
1570+
return (const void*&) v;
15701571
}
15711572

15721573
boolean_t takeTypemapTypedefParam(boolean_t b)

tests/CSharp/CSharp.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ class DLL_API HasFunctionPtrField
13071307
DLL_API void va_listFunction(va_list v);
13081308
DLL_API char* returnCharPointer();
13091309
DLL_API const char*& takeConstCharStarRef(const char*& c);
1310-
DLL_API void rValueReferenceToPointer(void*&& v);
1310+
DLL_API const void*& rValueReferenceToPointer(void*&& v);
13111311

13121312
struct {
13131313
struct {

0 commit comments

Comments
 (0)