From 6fd90784455423c4f5a208097e9f9f407be45f6e Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Wed, 15 Apr 2020 18:32:26 +0300 Subject: [PATCH] Fix the regressed C# marshalling of char* Fixes https://github.com/mono/CppSharp/issues/1258. Signed-off-by: Dimitar Dobrev --- .../Generators/CSharp/CSharpMarshal.cs | 17 +++++++++-------- tests/CSharp/CSharp.Tests.cs | 7 +++++++ tests/CSharp/CSharp.cpp | 5 +++++ tests/CSharp/CSharp.h | 1 + 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 6b05cb77..920adb8c 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -566,10 +566,16 @@ namespace CppSharp.Generators.CSharp return true; } + bool isConst = quals.IsConst || pointer.QualifiedPointee.Qualifiers.IsConst || + pointer.GetFinalQualifiedPointee().Qualifiers.IsConst; + if (Context.Context.Options.MarshalCharAsManagedChar && primitive == PrimitiveType.Char) { - Context.Return.Write($"({typePrinter.PrintNative(pointer)}) &{param.Name}"); + Context.Return.Write($"({typePrinter.PrintNative(pointer)})"); + if (isConst) + Context.Return.Write("&"); + Context.Return.Write(param.Name); return true; } @@ -577,13 +583,8 @@ namespace CppSharp.Generators.CSharp if (Context.Parameter.IsIndirect) Context.ArgumentPrefix.Write("&"); - - bool isVoid = primitive == PrimitiveType.Void && - pointee.IsAddress() && pointer.IsReference() && - (quals.IsConst || pointer.QualifiedPointee.Qualifiers.IsConst || - pointer.GetFinalQualifiedPointee().Qualifiers.IsConst); - if (pointer.Pointee.Desugar(false) is TemplateParameterSubstitutionType || - isVoid) + bool isVoid = primitive == PrimitiveType.Void && pointer.IsReference() && isConst; + if (pointer.Pointee.Desugar(false) is TemplateParameterSubstitutionType || isVoid) { var local = Generator.GeneratedIdentifier($@"{ param.Name}{Context.ParameterIndex}"); diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index 106f994f..ff438d49 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -110,6 +110,13 @@ public unsafe class CSharpTests : GeneratorTestFixture Assert.That(*CSharp.CSharp.TakeConstCharRef(z), Is.EqualTo(z)); } + [Test] + public void TestTakeCharPointer() + { + char c = 'c'; + Assert.That(*CSharp.CSharp.TakeCharPointer(&c), Is.EqualTo(c)); + } + [Test] public void TestIndexer() { diff --git a/tests/CSharp/CSharp.cpp b/tests/CSharp/CSharp.cpp index 4bb5f20b..024c9833 100644 --- a/tests/CSharp/CSharp.cpp +++ b/tests/CSharp/CSharp.cpp @@ -1645,6 +1645,11 @@ char* returnCharPointer() return 0; } +char* takeCharPointer(char* c) +{ + return c; +} + char* takeConstCharRef(const char& c) { return const_cast(&c); diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index 8d705c1c..500d8fcd 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -1362,6 +1362,7 @@ public: DLL_API void va_listFunction(va_list v); DLL_API char* returnCharPointer(); +DLL_API char* takeCharPointer(char* c); DLL_API char* takeConstCharRef(const char& c); DLL_API const char*& takeConstCharStarRef(const char*& c); DLL_API const void*& rValueReferenceToPointer(void*&& v);