Browse Source

Fix #949: Unsafe modifier missing in some cases

pull/960/head
Siegfried Pammer 8 years ago
parent
commit
20f772172c
  1. 25
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs
  2. 109
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.il
  3. 103
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.opt.il
  4. 91
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.opt.roslyn.il
  5. 97
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.roslyn.il
  6. 12
      ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUnsafeModifier.cs

25
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs

@ -38,12 +38,27 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -38,12 +38,27 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public unsafe fixed byte Old[1];
}
public unsafe delegate void UnsafeDelegate(byte* ptr);
private UnsafeDelegate unsafeDelegate;
private static UnsafeDelegate staticUnsafeDelegate;
public unsafe int* NullPointer {
get {
return null;
}
}
unsafe static UnsafeCode()
{
UnsafeCode.staticUnsafeDelegate = UnsafeCode.UnsafeStaticMethod;
}
public unsafe UnsafeCode()
{
this.unsafeDelegate = this.UnsafeMethod;
}
public unsafe int SizeOf()
{
return sizeof(SimpleStruct);
@ -53,6 +68,16 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -53,6 +68,16 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
}
private unsafe void UnsafeMethod(byte* ptr)
{
}
private unsafe static void UnsafeStaticMethod(byte* ptr)
{
}
public unsafe void PointerComparison(int* a, double* b)
{
UnsafeCode.UseBool(a == b);

109
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.il

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly '5ph04scr'
.assembly ob0irbmp
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
@ -20,20 +20,20 @@ @@ -20,20 +20,20 @@
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module '5ph04scr.dll'
// MVID: {979A8D45-AB93-4029-8820-2753F73AD209}
.module ob0irbmp.dll
// MVID: {72C21E04-2476-45C2-824E-E862E52B73A5}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x01AF0000
// Image base: 0x02DA0000
// =============== CLASS MEMBERS DECLARATION ===================
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
.class public auto ansi ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
extends [mscorlib]System.Object
{
.class sequential ansi sealed nested public beforefieldinit SimpleStruct
@ -51,8 +51,8 @@ @@ -51,8 +51,8 @@
{
.pack 0
.size 400
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.UnsafeValueTypeAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public int32 FixedElementField
} // end of class '<Integers>e__FixedBuffer0'
@ -71,8 +71,8 @@ @@ -71,8 +71,8 @@
{
.pack 0
.size 1
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.UnsafeValueTypeAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public uint8 FixedElementField
} // end of class '<Old>e__FixedBuffer2'
@ -96,6 +96,8 @@ @@ -96,6 +96,8 @@
61 35 63 35 36 31 39 33 34 65 30 38 39 C8 00 00 // a5c561934e089...
00 00 00 )
.field public valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/StructWithFixedSizeMembers/'<Old>e__FixedBuffer2' Old
.custom instance void [mscorlib]System.ObsoleteAttribute::.ctor(string) = ( 01 00 11 61 6E 6F 74 68 65 72 20 61 74 74 72 69 // ...another attri
62 75 74 65 00 00 ) // bute..
.custom instance void [mscorlib]System.Runtime.CompilerServices.FixedBufferAttribute::.ctor(class [mscorlib]System.Type,
int32) = ( 01 00 58 53 79 73 74 65 6D 2E 42 79 74 65 2C 20 // ..XSystem.Byte,
6D 73 63 6F 72 6C 69 62 2C 20 56 65 72 73 69 6F // mscorlib, Versio
@ -104,10 +106,39 @@ @@ -104,10 +106,39 @@
69 63 4B 65 79 54 6F 6B 65 6E 3D 62 37 37 61 35 // icKeyToken=b77a5
63 35 36 31 39 33 34 65 30 38 39 01 00 00 00 00 // c561934e089.....
00 )
.custom instance void [mscorlib]System.ObsoleteAttribute::.ctor(string) = ( 01 00 11 61 6E 6F 74 68 65 72 20 61 74 74 72 69 // ...another attri
62 75 74 65 00 00 ) // bute..
} // end of class StructWithFixedSizeMembers
.class auto ansi sealed nested public UnsafeDelegate
extends [mscorlib]System.MulticastDelegate
{
.method public hidebysig specialname rtspecialname
instance void .ctor(object 'object',
native int 'method') runtime managed
{
} // end of method UnsafeDelegate::.ctor
.method public hidebysig newslot virtual
instance void Invoke(uint8* ptr) runtime managed
{
} // end of method UnsafeDelegate::Invoke
.method public hidebysig newslot virtual
instance class [mscorlib]System.IAsyncResult
BeginInvoke(uint8* ptr,
class [mscorlib]System.AsyncCallback callback,
object 'object') runtime managed
{
} // end of method UnsafeDelegate::BeginInvoke
.method public hidebysig newslot virtual
instance void EndInvoke(class [mscorlib]System.IAsyncResult result) runtime managed
{
} // end of method UnsafeDelegate::EndInvoke
} // end of class UnsafeDelegate
.field private class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate unsafeDelegate
.field private static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate staticUnsafeDelegate
.method public hidebysig specialname instance int32*
get_NullPointer() cil managed
{
@ -124,6 +155,39 @@ @@ -124,6 +155,39 @@
IL_0007: ret
} // end of method UnsafeCode::get_NullPointer
.method private hidebysig specialname rtspecialname static
void .cctor() cil managed
{
// Code size 19 (0x13)
.maxstack 8
IL_0000: nop
IL_0001: ldnull
IL_0002: ldftn void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::UnsafeStaticMethod(uint8*)
IL_0008: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate::.ctor(object,
native int)
IL_000d: stsfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::staticUnsafeDelegate
IL_0012: ret
} // end of method UnsafeCode::.cctor
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 28 (0x1c)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: nop
IL_0008: ldarg.0
IL_0009: ldarg.0
IL_000a: ldftn instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::UnsafeMethod(uint8*)
IL_0010: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate::.ctor(object,
native int)
IL_0015: stfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::unsafeDelegate
IL_001a: nop
IL_001b: ret
} // end of method UnsafeCode::.ctor
.method public hidebysig instance int32
SizeOf() cil managed
{
@ -147,6 +211,23 @@ @@ -147,6 +211,23 @@
IL_0001: ret
} // end of method UnsafeCode::UseBool
.method private hidebysig instance void
UnsafeMethod(uint8* ptr) cil managed
{
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method UnsafeCode::UnsafeMethod
.method private hidebysig static void UnsafeStaticMethod(uint8* ptr) cil managed
{
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method UnsafeCode::UnsafeStaticMethod
.method public hidebysig instance void
PointerComparison(int32* a,
float64* b) cil managed
@ -1126,16 +1207,6 @@ @@ -1126,16 +1207,6 @@
IL_001a: ret
} // end of method UnsafeCode::Finalize
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method UnsafeCode::.ctor
.property instance int32* NullPointer()
{
.get instance int32* ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::get_NullPointer()

103
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.opt.il

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly zuwavv1x
.assembly gqg3tz14
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
@ -20,20 +20,20 @@ @@ -20,20 +20,20 @@
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module zuwavv1x.dll
// MVID: {4CC9FC6C-21CA-408A-ABC9-544A07D1E512}
.module gqg3tz14.dll
// MVID: {F3C49182-FFB5-41E4-933E-7530556E64D8}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x01600000
// Image base: 0x00960000
// =============== CLASS MEMBERS DECLARATION ===================
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
.class public auto ansi ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
extends [mscorlib]System.Object
{
.class sequential ansi sealed nested public beforefieldinit SimpleStruct
@ -51,8 +51,8 @@ @@ -51,8 +51,8 @@
{
.pack 0
.size 400
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.UnsafeValueTypeAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public int32 FixedElementField
} // end of class '<Integers>e__FixedBuffer0'
@ -71,8 +71,8 @@ @@ -71,8 +71,8 @@
{
.pack 0
.size 1
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.UnsafeValueTypeAttribute::.ctor() = ( 01 00 00 00 )
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
.field public uint8 FixedElementField
} // end of class '<Old>e__FixedBuffer2'
@ -96,6 +96,8 @@ @@ -96,6 +96,8 @@
61 35 63 35 36 31 39 33 34 65 30 38 39 C8 00 00 // a5c561934e089...
00 00 00 )
.field public valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/StructWithFixedSizeMembers/'<Old>e__FixedBuffer2' Old
.custom instance void [mscorlib]System.ObsoleteAttribute::.ctor(string) = ( 01 00 11 61 6E 6F 74 68 65 72 20 61 74 74 72 69 // ...another attri
62 75 74 65 00 00 ) // bute..
.custom instance void [mscorlib]System.Runtime.CompilerServices.FixedBufferAttribute::.ctor(class [mscorlib]System.Type,
int32) = ( 01 00 58 53 79 73 74 65 6D 2E 42 79 74 65 2C 20 // ..XSystem.Byte,
6D 73 63 6F 72 6C 69 62 2C 20 56 65 72 73 69 6F // mscorlib, Versio
@ -104,10 +106,39 @@ @@ -104,10 +106,39 @@
69 63 4B 65 79 54 6F 6B 65 6E 3D 62 37 37 61 35 // icKeyToken=b77a5
63 35 36 31 39 33 34 65 30 38 39 01 00 00 00 00 // c561934e089.....
00 )
.custom instance void [mscorlib]System.ObsoleteAttribute::.ctor(string) = ( 01 00 11 61 6E 6F 74 68 65 72 20 61 74 74 72 69 // ...another attri
62 75 74 65 00 00 ) // bute..
} // end of class StructWithFixedSizeMembers
.class auto ansi sealed nested public UnsafeDelegate
extends [mscorlib]System.MulticastDelegate
{
.method public hidebysig specialname rtspecialname
instance void .ctor(object 'object',
native int 'method') runtime managed
{
} // end of method UnsafeDelegate::.ctor
.method public hidebysig newslot virtual
instance void Invoke(uint8* ptr) runtime managed
{
} // end of method UnsafeDelegate::Invoke
.method public hidebysig newslot virtual
instance class [mscorlib]System.IAsyncResult
BeginInvoke(uint8* ptr,
class [mscorlib]System.AsyncCallback callback,
object 'object') runtime managed
{
} // end of method UnsafeDelegate::BeginInvoke
.method public hidebysig newslot virtual
instance void EndInvoke(class [mscorlib]System.IAsyncResult result) runtime managed
{
} // end of method UnsafeDelegate::EndInvoke
} // end of class UnsafeDelegate
.field private class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate unsafeDelegate
.field private static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate staticUnsafeDelegate
.method public hidebysig specialname instance int32*
get_NullPointer() cil managed
{
@ -118,6 +149,35 @@ @@ -118,6 +149,35 @@
IL_0002: ret
} // end of method UnsafeCode::get_NullPointer
.method private hidebysig specialname rtspecialname static
void .cctor() cil managed
{
// Code size 18 (0x12)
.maxstack 8
IL_0000: ldnull
IL_0001: ldftn void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::UnsafeStaticMethod(uint8*)
IL_0007: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate::.ctor(object,
native int)
IL_000c: stsfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::staticUnsafeDelegate
IL_0011: ret
} // end of method UnsafeCode::.cctor
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 25 (0x19)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ldarg.0
IL_0007: ldarg.0
IL_0008: ldftn instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::UnsafeMethod(uint8*)
IL_000e: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate::.ctor(object,
native int)
IL_0013: stfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::unsafeDelegate
IL_0018: ret
} // end of method UnsafeCode::.ctor
.method public hidebysig instance int32
SizeOf() cil managed
{
@ -134,6 +194,21 @@ @@ -134,6 +194,21 @@
IL_0000: ret
} // end of method UnsafeCode::UseBool
.method private hidebysig instance void
UnsafeMethod(uint8* ptr) cil managed
{
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
} // end of method UnsafeCode::UnsafeMethod
.method private hidebysig static void UnsafeStaticMethod(uint8* ptr) cil managed
{
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
} // end of method UnsafeCode::UnsafeStaticMethod
.method public hidebysig instance void
PointerComparison(int32* a,
float64* b) cil managed
@ -881,16 +956,6 @@ @@ -881,16 +956,6 @@
IL_0015: ret
} // end of method UnsafeCode::Finalize
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method UnsafeCode::.ctor
.property instance int32* NullPointer()
{
.get instance int32* ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::get_NullPointer()

91
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.opt.roslyn.il

@ -25,19 +25,19 @@ @@ -25,19 +25,19 @@
.ver 0:0:0:0
}
.module UnsafeCode.dll
// MVID: {958D637E-F39D-447B-A248-B73AECEC847A}
// MVID: {CBE9626D-F9BD-4008-A325-BABE72F089C1}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x007C0000
// Image base: 0x01600000
// =============== CLASS MEMBERS DECLARATION ===================
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
.class public auto ansi ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
extends [mscorlib]System.Object
{
.class sequential ansi sealed nested public beforefieldinit SimpleStruct
@ -112,6 +112,37 @@ @@ -112,6 +112,37 @@
62 75 74 65 00 00 ) // bute..
} // end of class StructWithFixedSizeMembers
.class auto ansi sealed nested public UnsafeDelegate
extends [mscorlib]System.MulticastDelegate
{
.method public hidebysig specialname rtspecialname
instance void .ctor(object 'object',
native int 'method') runtime managed
{
} // end of method UnsafeDelegate::.ctor
.method public hidebysig newslot virtual
instance void Invoke(uint8* ptr) runtime managed
{
} // end of method UnsafeDelegate::Invoke
.method public hidebysig newslot virtual
instance class [mscorlib]System.IAsyncResult
BeginInvoke(uint8* ptr,
class [mscorlib]System.AsyncCallback callback,
object 'object') runtime managed
{
} // end of method UnsafeDelegate::BeginInvoke
.method public hidebysig newslot virtual
instance void EndInvoke(class [mscorlib]System.IAsyncResult result) runtime managed
{
} // end of method UnsafeDelegate::EndInvoke
} // end of class UnsafeDelegate
.field private class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate unsafeDelegate
.field private static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate staticUnsafeDelegate
.method public hidebysig specialname instance int32*
get_NullPointer() cil managed
{
@ -122,6 +153,35 @@ @@ -122,6 +153,35 @@
IL_0002: ret
} // end of method UnsafeCode::get_NullPointer
.method private hidebysig specialname rtspecialname static
void .cctor() cil managed
{
// Code size 18 (0x12)
.maxstack 8
IL_0000: ldnull
IL_0001: ldftn void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::UnsafeStaticMethod(uint8*)
IL_0007: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate::.ctor(object,
native int)
IL_000c: stsfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::staticUnsafeDelegate
IL_0011: ret
} // end of method UnsafeCode::.cctor
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 25 (0x19)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ldarg.0
IL_0007: ldarg.0
IL_0008: ldftn instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::UnsafeMethod(uint8*)
IL_000e: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate::.ctor(object,
native int)
IL_0013: stfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::unsafeDelegate
IL_0018: ret
} // end of method UnsafeCode::.ctor
.method public hidebysig instance int32
SizeOf() cil managed
{
@ -138,6 +198,21 @@ @@ -138,6 +198,21 @@
IL_0000: ret
} // end of method UnsafeCode::UseBool
.method private hidebysig instance void
UnsafeMethod(uint8* ptr) cil managed
{
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
} // end of method UnsafeCode::UnsafeMethod
.method private hidebysig static void UnsafeStaticMethod(uint8* ptr) cil managed
{
// Code size 1 (0x1)
.maxstack 8
IL_0000: ret
} // end of method UnsafeCode::UnsafeStaticMethod
.method public hidebysig instance void
PointerComparison(int32* a,
float64* b) cil managed
@ -888,16 +963,6 @@ @@ -888,16 +963,6 @@
IL_0015: ret
} // end of method UnsafeCode::Finalize
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method UnsafeCode::.ctor
.property instance int32* NullPointer()
{
.get instance int32* ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::get_NullPointer()

97
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.roslyn.il

@ -25,19 +25,19 @@ @@ -25,19 +25,19 @@
.ver 0:0:0:0
}
.module UnsafeCode.dll
// MVID: {A3E96E65-04A9-4270-81A4-F0FB3CB8D517}
// MVID: {DD8EC7AC-2A00-4D24-83E2-D39C3E04AE41}
.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x01310000
// Image base: 0x01090000
// =============== CLASS MEMBERS DECLARATION ===================
.class public auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
.class public auto ansi ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode
extends [mscorlib]System.Object
{
.class sequential ansi sealed nested public beforefieldinit SimpleStruct
@ -112,6 +112,37 @@ @@ -112,6 +112,37 @@
62 75 74 65 00 00 ) // bute..
} // end of class StructWithFixedSizeMembers
.class auto ansi sealed nested public UnsafeDelegate
extends [mscorlib]System.MulticastDelegate
{
.method public hidebysig specialname rtspecialname
instance void .ctor(object 'object',
native int 'method') runtime managed
{
} // end of method UnsafeDelegate::.ctor
.method public hidebysig newslot virtual
instance void Invoke(uint8* ptr) runtime managed
{
} // end of method UnsafeDelegate::Invoke
.method public hidebysig newslot virtual
instance class [mscorlib]System.IAsyncResult
BeginInvoke(uint8* ptr,
class [mscorlib]System.AsyncCallback callback,
object 'object') runtime managed
{
} // end of method UnsafeDelegate::BeginInvoke
.method public hidebysig newslot virtual
instance void EndInvoke(class [mscorlib]System.IAsyncResult result) runtime managed
{
} // end of method UnsafeDelegate::EndInvoke
} // end of class UnsafeDelegate
.field private class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate unsafeDelegate
.field private static class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate staticUnsafeDelegate
.method public hidebysig specialname instance int32*
get_NullPointer() cil managed
{
@ -128,6 +159,38 @@ @@ -128,6 +159,38 @@
IL_0007: ret
} // end of method UnsafeCode::get_NullPointer
.method private hidebysig specialname rtspecialname static
void .cctor() cil managed
{
// Code size 19 (0x13)
.maxstack 8
IL_0000: nop
IL_0001: ldnull
IL_0002: ldftn void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::UnsafeStaticMethod(uint8*)
IL_0008: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate::.ctor(object,
native int)
IL_000d: stsfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::staticUnsafeDelegate
IL_0012: ret
} // end of method UnsafeCode::.cctor
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 27 (0x1b)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: nop
IL_0008: ldarg.0
IL_0009: ldarg.0
IL_000a: ldftn instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::UnsafeMethod(uint8*)
IL_0010: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate::.ctor(object,
native int)
IL_0015: stfld class ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode/UnsafeDelegate ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::unsafeDelegate
IL_001a: ret
} // end of method UnsafeCode::.ctor
.method public hidebysig instance int32
SizeOf() cil managed
{
@ -151,6 +214,23 @@ @@ -151,6 +214,23 @@
IL_0001: ret
} // end of method UnsafeCode::UseBool
.method private hidebysig instance void
UnsafeMethod(uint8* ptr) cil managed
{
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method UnsafeCode::UnsafeMethod
.method private hidebysig static void UnsafeStaticMethod(uint8* ptr) cil managed
{
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method UnsafeCode::UnsafeStaticMethod
.method public hidebysig instance void
PointerComparison(int32* a,
float64* b) cil managed
@ -1132,17 +1212,6 @@ @@ -1132,17 +1212,6 @@
IL_0019: ret
} // end of method UnsafeCode::Finalize
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // end of method UnsafeCode::.ctor
.property instance int32* NullPointer()
{
.get instance int32* ICSharpCode.Decompiler.Tests.TestCases.Pretty.UnsafeCode::get_NullPointer()

12
ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUnsafeModifier.cs

@ -108,8 +108,16 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -108,8 +108,16 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
memberReferenceExpression.ReplaceWith(pre);
}
var rr = memberReferenceExpression.GetResolveResult();
if (rr != null && rr.Type is PointerType)
return true;
if (rr != null) {
if (rr.Type is PointerType)
return true;
if (rr is MemberResolveResult mrr && mrr.Member.ReturnType.Kind == TypeKind.Delegate) {
var method = mrr.Member.ReturnType.GetDefinition()?.GetDelegateInvokeMethod();
if (method != null && (method.ReturnType is PointerType || method.Parameters.Any(p => p.Type is PointerType)))
return true;
}
}
return result;
}

Loading…
Cancel
Save