From d5380fe89052f1be8107866a0dd778feec07fc72 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Wed, 3 Apr 2019 01:30:27 +0300 Subject: [PATCH] Fix the generated C# for const char*& Signed-off-by: Dimitar Dobrev --- src/Generator/Generators/CSharp/CSharpMarshal.cs | 11 +++++++---- src/Generator/Types/Std/Stdlib.cs | 1 + tests/CSharp/CSharp.Tests.cs | 6 ++++++ tests/CSharp/CSharp.cpp | 5 +++++ tests/CSharp/CSharp.cs | 3 ++- tests/CSharp/CSharp.h | 1 + 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 5718cbee..ceda0fb6 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -499,7 +499,7 @@ namespace CppSharp.Generators.CSharp var isRefParam = param != null && (param.IsInOut || param.IsOut); var pointee = pointer.Pointee.Desugar(); - if (pointee.IsConstCharString() && isRefParam) + if (pointee.IsConstCharString()) { if (param.IsOut) { @@ -513,13 +513,16 @@ namespace CppSharp.Generators.CSharp MarshalString(pointee); pointer.QualifiedPointee.Visit(this); Context.ArgumentPrefix.Write("&"); + return true; } - else + if (pointer.IsReference) { + Context.Return.Write($@"({typePrinter.PrintNative( + pointee.GetQualifiedPointee())}*) "); pointer.QualifiedPointee.Visit(this); - Context.Cleanup.WriteLine($"Marshal.FreeHGlobal({Context.ArgName});"); + Context.ArgumentPrefix.Write("&"); + return true; } - return true; } var finalPointee = (pointee.GetFinalPointee() ?? pointee).Desugar(); diff --git a/src/Generator/Types/Std/Stdlib.cs b/src/Generator/Types/Std/Stdlib.cs index cc10322a..b7715f21 100644 --- a/src/Generator/Types/Std/Stdlib.cs +++ b/src/Generator/Types/Std/Stdlib.cs @@ -168,6 +168,7 @@ namespace CppSharp.Types.Std public override void CSharpMarshalToNative(CSharpMarshalContext ctx) { if (ctx.Parameter.Usage == ParameterUsage.Unknown && + !ctx.Parameter.Type.IsReference() && ctx.MarshalKind != MarshalKind.NativeField && ctx.MarshalKind != MarshalKind.VTableReturnValue && ctx.MarshalKind != MarshalKind.Variable) diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index 12cea524..762e48d8 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -1255,6 +1255,12 @@ public unsafe class CSharpTests : GeneratorTestFixture } } + [Test] + public void TestConstCharStarRef() + { + Assert.That(CSharp.CSharp.TakeConstCharStarRef("Test"), Is.EqualTo("Test")); + } + public class Inter : SimpleInterface { public override int Size => s; diff --git a/tests/CSharp/CSharp.cpp b/tests/CSharp/CSharp.cpp index 686f39ba..8326d01e 100644 --- a/tests/CSharp/CSharp.cpp +++ b/tests/CSharp/CSharp.cpp @@ -1551,3 +1551,8 @@ char* returnCharPointer() { return 0; } + +const char* takeConstCharStarRef(const char*& c) +{ + return c; +} diff --git a/tests/CSharp/CSharp.cs b/tests/CSharp/CSharp.cs index ee6eca88..bfca2e7b 100644 --- a/tests/CSharp/CSharp.cs +++ b/tests/CSharp/CSharp.cs @@ -259,7 +259,8 @@ namespace CppSharp.Tests public override void CSharpMarshalToNative(CSharpMarshalContext ctx) { - ctx.Return.Write("\"test\""); + ctx.Return.Write(ctx.Parameter.Type.Desugar().IsAddress() ? + "global::System.IntPtr.Zero" : "\"test\""); } public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index 89a00e35..2fc0ea52 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -1297,3 +1297,4 @@ private: DLL_API void va_listFunction(va_list v); DLL_API char* returnCharPointer(); +DLL_API const char* takeConstCharStarRef(const char*& c);