Skip to content

Commit 6fd9078

Browse files
committed
Fix the regressed C# marshalling of char*
Fixes #1258. Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent e0b8b58 commit 6fd9078

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

Diff for: src/Generator/Generators/CSharp/CSharpMarshal.cs

+9-8
Original file line numberDiff line numberDiff line change
@@ -566,24 +566,25 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
566566
return true;
567567
}
568568

569+
bool isConst = quals.IsConst || pointer.QualifiedPointee.Qualifiers.IsConst ||
570+
pointer.GetFinalQualifiedPointee().Qualifiers.IsConst;
571+
569572
if (Context.Context.Options.MarshalCharAsManagedChar &&
570573
primitive == PrimitiveType.Char)
571574
{
572-
Context.Return.Write($"({typePrinter.PrintNative(pointer)}) &{param.Name}");
575+
Context.Return.Write($"({typePrinter.PrintNative(pointer)})");
576+
if (isConst)
577+
Context.Return.Write("&");
578+
Context.Return.Write(param.Name);
573579
return true;
574580
}
575581

576582
pointer.QualifiedPointee.Visit(this);
577583

578584
if (Context.Parameter.IsIndirect)
579585
Context.ArgumentPrefix.Write("&");
580-
581-
bool isVoid = primitive == PrimitiveType.Void &&
582-
pointee.IsAddress() && pointer.IsReference() &&
583-
(quals.IsConst || pointer.QualifiedPointee.Qualifiers.IsConst ||
584-
pointer.GetFinalQualifiedPointee().Qualifiers.IsConst);
585-
if (pointer.Pointee.Desugar(false) is TemplateParameterSubstitutionType ||
586-
isVoid)
586+
bool isVoid = primitive == PrimitiveType.Void && pointer.IsReference() && isConst;
587+
if (pointer.Pointee.Desugar(false) is TemplateParameterSubstitutionType || isVoid)
587588
{
588589
var local = Generator.GeneratedIdentifier($@"{
589590
param.Name}{Context.ParameterIndex}");

Diff for: tests/CSharp/CSharp.Tests.cs

+7
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ public void TestReturnCharPointer()
110110
Assert.That(*CSharp.CSharp.TakeConstCharRef(z), Is.EqualTo(z));
111111
}
112112

113+
[Test]
114+
public void TestTakeCharPointer()
115+
{
116+
char c = 'c';
117+
Assert.That(*CSharp.CSharp.TakeCharPointer(&c), Is.EqualTo(c));
118+
}
119+
113120
[Test]
114121
public void TestIndexer()
115122
{

Diff for: tests/CSharp/CSharp.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,11 @@ char* returnCharPointer()
16451645
return 0;
16461646
}
16471647

1648+
char* takeCharPointer(char* c)
1649+
{
1650+
return c;
1651+
}
1652+
16481653
char* takeConstCharRef(const char& c)
16491654
{
16501655
return const_cast<char*>(&c);

Diff for: tests/CSharp/CSharp.h

+1
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,7 @@ class DLL_API HasFunctionPtrField
13621362

13631363
DLL_API void va_listFunction(va_list v);
13641364
DLL_API char* returnCharPointer();
1365+
DLL_API char* takeCharPointer(char* c);
13651366
DLL_API char* takeConstCharRef(const char& c);
13661367
DLL_API const char*& takeConstCharStarRef(const char*& c);
13671368
DLL_API const void*& rValueReferenceToPointer(void*&& v);

0 commit comments

Comments
 (0)