Skip to content

Commit d5380fe

Browse files
committed
Fix the generated C# for const char*&
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 75bd505 commit d5380fe

File tree

6 files changed

+22
-5
lines changed

6 files changed

+22
-5
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
499499
var isRefParam = param != null && (param.IsInOut || param.IsOut);
500500

501501
var pointee = pointer.Pointee.Desugar();
502-
if (pointee.IsConstCharString() && isRefParam)
502+
if (pointee.IsConstCharString())
503503
{
504504
if (param.IsOut)
505505
{
@@ -513,13 +513,16 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
513513
MarshalString(pointee);
514514
pointer.QualifiedPointee.Visit(this);
515515
Context.ArgumentPrefix.Write("&");
516+
return true;
516517
}
517-
else
518+
if (pointer.IsReference)
518519
{
520+
Context.Return.Write($@"({typePrinter.PrintNative(
521+
pointee.GetQualifiedPointee())}*) ");
519522
pointer.QualifiedPointee.Visit(this);
520-
Context.Cleanup.WriteLine($"Marshal.FreeHGlobal({Context.ArgName});");
523+
Context.ArgumentPrefix.Write("&");
524+
return true;
521525
}
522-
return true;
523526
}
524527

525528
var finalPointee = (pointee.GetFinalPointee() ?? pointee).Desugar();

src/Generator/Types/Std/Stdlib.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ public override Type CSharpSignatureType(TypePrinterContext ctx)
168168
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
169169
{
170170
if (ctx.Parameter.Usage == ParameterUsage.Unknown &&
171+
!ctx.Parameter.Type.IsReference() &&
171172
ctx.MarshalKind != MarshalKind.NativeField &&
172173
ctx.MarshalKind != MarshalKind.VTableReturnValue &&
173174
ctx.MarshalKind != MarshalKind.Variable)

tests/CSharp/CSharp.Tests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,12 @@ public void TestVirtualIndirectCallInNative()
12551255
}
12561256
}
12571257

1258+
[Test]
1259+
public void TestConstCharStarRef()
1260+
{
1261+
Assert.That(CSharp.CSharp.TakeConstCharStarRef("Test"), Is.EqualTo("Test"));
1262+
}
1263+
12581264
public class Inter : SimpleInterface
12591265
{
12601266
public override int Size => s;

tests/CSharp/CSharp.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,3 +1551,8 @@ char* returnCharPointer()
15511551
{
15521552
return 0;
15531553
}
1554+
1555+
const char* takeConstCharStarRef(const char*& c)
1556+
{
1557+
return c;
1558+
}

tests/CSharp/CSharp.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ public override Type CSharpSignatureType(TypePrinterContext ctx)
259259

260260
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
261261
{
262-
ctx.Return.Write("\"test\"");
262+
ctx.Return.Write(ctx.Parameter.Type.Desugar().IsAddress() ?
263+
"global::System.IntPtr.Zero" : "\"test\"");
263264
}
264265

265266
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)

tests/CSharp/CSharp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,3 +1297,4 @@ class DLL_API InterfaceTester
12971297

12981298
DLL_API void va_listFunction(va_list v);
12991299
DLL_API char* returnCharPointer();
1300+
DLL_API const char* takeConstCharStarRef(const char*& c);

0 commit comments

Comments
 (0)