Skip to content

Commit 0c2f106

Browse files
committed
add support for formatting parameter type of by-ref pointers
1 parent e7f74fa commit 0c2f106

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,10 +1174,20 @@ m is MethodInfo methodMayBeAccessor &&
11741174
if (isAsyncStateMachine)
11751175
sb.Append("async ");
11761176

1177-
if (method is not null && method.GetParameters().Any(static p => p.ParameterType.IsPointer))
1177+
if (method is not null && method.GetParameters().Any(IsParameterUnsafe))
11781178
sb.Append("unsafe ");
11791179
}
11801180

1181+
static bool IsParameterUnsafe(ParameterInfo p)
1182+
{
1183+
if (p.ParameterType.IsPointer)
1184+
return true;
1185+
if (p.ParameterType.IsByRef && p.ParameterType.HasElementType && p.ParameterType.GetElementType()!.IsPointer)
1186+
return true;
1187+
1188+
return false;
1189+
}
1190+
11811191
if (member.IsHidingInheritedMember(nonPublic: true))
11821192
sb.Append("new ");
11831193

tests/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.MemberDeclaration.Parameters.NullabilityAnnotations.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,26 @@ class GenericTypes {
151151
}
152152
}
153153

154+
class PointerTypes {
155+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfValueType)}(int* p) {{}}")] public unsafe void PointerOfValueType(int* p) { }
156+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfNullableValueType)}(int?* p) {{}}")] public unsafe void PointerOfNullableValueType(int?* p) { }
157+
158+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfValueTupleOfValueType)}((int, int)* p) {{}}")] public unsafe void PointerOfValueTupleOfValueType((int, int)* p) { }
159+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfValueTupleOfNullableValueType)}((int, int?)* p) {{}}")] public unsafe void PointerOfValueTupleOfNullableValueType((int, int?)* p) { }
160+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfNullableValueTupleOfValueType)}((int, int)?* p) {{}}")] public unsafe void PointerOfNullableValueTupleOfValueType((int, int)?* p) { }
161+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfNullableValueTupleOfNullableValueType)}((int, int?)?* p) {{}}")] public unsafe void PointerOfNullableValueTupleOfNullableValueType((int, int?)?* p) { }
162+
163+
class ByRef {
164+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfValueType)}(out int* p) {{}}")] public unsafe void PointerOfValueType(out int* p) => throw null;
165+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfNullableValueType)}(out int?* p) {{}}")] public unsafe void PointerOfNullableValueType(out int?* p) => throw null;
166+
167+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfValueTupleOfValueType)}(out (int, int)* p) {{}}")] public unsafe void PointerOfValueTupleOfValueType(out (int, int)* p) => throw null;
168+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfValueTupleOfNullableValueType)}(out (int, int?)* p) {{}}")] public unsafe void PointerOfValueTupleOfNullableValueType(out (int, int?)* p) => throw null;
169+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfNullableValueTupleOfValueType)}(out (int, int)?* p) {{}}")] public unsafe void PointerOfNullableValueTupleOfValueType(out (int, int)?* p) => throw null;
170+
[MemberDeclarationTestCase($"public unsafe void {nameof(PointerOfNullableValueTupleOfNullableValueType)}(out (int, int?)?* p) {{}}")] public unsafe void PointerOfNullableValueTupleOfNullableValueType(out (int, int?)?* p) => throw null;
171+
}
172+
}
173+
154174
class NullabilityAnnotationOptions {
155175
[MemberDeclarationTestCase($"public void {nameof(ValueType)}(int p) {{}}", MemberEnableNullabilityAnnotations = false)]
156176
[MemberDeclarationTestCase($"public void {nameof(ValueType)}(int p) {{}}", MemberEnableNullabilityAnnotations = true)]

0 commit comments

Comments
 (0)