Skip to content

Commit 7b70e92

Browse files
Merge pull request #643 from ds5678/conversions-to-pointer
Improve method naming for conversions to pointers
2 parents 53dbe11 + c59edc7 commit 7b70e92

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6581,9 +6581,20 @@ private bool TryRemapOperatorName(ref string name, FunctionDecl functionDecl)
65816581
if (functionDecl is CXXConversionDecl)
65826582
{
65836583
var returnType = functionDecl.ReturnType;
6584+
var pointerIndirectionLevel = 0;
6585+
while (returnType is PointerType pointerType)
6586+
{
6587+
pointerIndirectionLevel++;
6588+
returnType = pointerType.PointeeType;
6589+
}
65846590
var returnTypeName = GetRemappedTypeName(cursor: null, context: null, returnType, out _, skipUsing: true);
65856591

6586-
name = $"To{returnTypeName}";
6592+
name = pointerIndirectionLevel switch {
6593+
0 => $"To{returnTypeName}",
6594+
1 => $"To{returnTypeName}Pointer",
6595+
2 => $"To{returnTypeName}PointerPointer",
6596+
_ => $"To{returnTypeName}{string.Concat(Enumerable.Repeat("Pointer", pointerIndirectionLevel))}"
6597+
};
65876598
return true;
65886599
}
65896600

tests/ClangSharp.PInvokeGenerator.UnitTests/Base/CXXMethodDeclarationCSharpTest.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,59 @@ protected override Task ConversionTestImpl()
9696
var inputContents = @"struct MyStruct
9797
{
9898
int value;
99-
99+
int* pointer;
100+
int** pointer2;
101+
int*** pointer3;
100102
operator int()
101103
{
102104
return value;
103105
}
106+
operator int*()
107+
{
108+
return pointer;
109+
}
110+
operator int**()
111+
{
112+
return pointer2;
113+
}
114+
operator int***()
115+
{
116+
return pointer3;
117+
}
104118
};
105119
";
106120

107121
var expectedOutputContents = @"namespace ClangSharp.Test
108122
{
109-
public partial struct MyStruct
123+
public unsafe partial struct MyStruct
110124
{
111125
public int value;
112126
127+
public int* pointer;
128+
129+
public int** pointer2;
130+
131+
public int*** pointer3;
132+
113133
public int ToInt32()
114134
{
115135
return value;
116136
}
137+
138+
public int* ToInt32Pointer()
139+
{
140+
return pointer;
141+
}
142+
143+
public int** ToInt32PointerPointer()
144+
{
145+
return pointer2;
146+
}
147+
148+
public int*** ToInt32PointerPointerPointer()
149+
{
150+
return pointer3;
151+
}
117152
}
118153
}
119154
";

0 commit comments

Comments
 (0)