From f2ee1c55f95d30938800e71959db1399aeaabfa6 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Wed, 11 Oct 2017 22:11:59 +0200 Subject: [PATCH] Add test case for switch without default + remove empty default sections in StatementBuilder. --- .../TestCases/Pretty/Switch.cs | 34 +++++ .../TestCases/Pretty/Switch.il | 129 +++++++++++++++--- .../TestCases/Pretty/Switch.opt.il | 112 ++++++++++++--- .../TestCases/Pretty/Switch.opt.roslyn.il | 78 ++++++++++- .../TestCases/Pretty/Switch.roslyn.il | 106 +++++++++++++- .../CSharp/StatementBuilder.cs | 6 + 6 files changed, 427 insertions(+), 38 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs index fcf01f6d9..47cae5066 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.cs @@ -160,6 +160,40 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty } } + public static void SwitchOverInt(int i) + { + switch (i) { + case 0: { + Console.WriteLine("zero"); + break; + } + case 5: { + Console.WriteLine("five"); + break; + } + case 10: { + Console.WriteLine("ten"); + break; + } + case 15: { + Console.WriteLine("fifteen"); + break; + } + case 20: { + Console.WriteLine("twenty"); + break; + } + case 25: { + Console.WriteLine("twenty-five"); + break; + } + case 30: { + Console.WriteLine("thirty"); + break; + } + } + } + public static string ShortSwitchOverString(string text) { Console.WriteLine("ShortSwitchOverString: " + text); diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.il index c6af28ac4..f5e28683c 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.il @@ -10,7 +10,7 @@ .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. .ver 4:0:0:0 } -.assembly nyniamwr +.assembly cgwywrw0 { .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 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. @@ -20,15 +20,15 @@ .hash algorithm 0x00008004 .ver 0:0:0:0 } -.module nyniamwr.dll -// MVID: {95C99B41-CBA3-42E4-A4DE-27E535671AB2} +.module cgwywrw0.dll +// MVID: {12C66A56-696F-4026-B79B-EFB40F4CD81E} .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: 0x03080000 +// Image base: 0x00B50000 // =============== CLASS MEMBERS DECLARATION =================== @@ -476,6 +476,101 @@ IL_006b: ret } // end of method Switch::SwitchOverNullableIntNoNullCaseShifted + .method public hidebysig static void SwitchOverInt(int32 i) cil managed + { + // Code size 151 (0x97) + .maxstack 2 + .locals init (int32 V_0) + IL_0000: nop + IL_0001: ldarg.0 + IL_0002: stloc.0 + IL_0003: ldloc.0 + IL_0004: ldc.i4.s 10 + IL_0006: bgt.s IL_0017 + + IL_0008: ldloc.0 + IL_0009: ldc.i4.0 + IL_000a: beq.s IL_0034 + + IL_000c: ldloc.0 + IL_000d: ldc.i4.5 + IL_000e: beq.s IL_0042 + + IL_0010: ldloc.0 + IL_0011: ldc.i4.s 10 + IL_0013: beq.s IL_0050 + + IL_0015: br.s IL_0096 + + IL_0017: ldloc.0 + IL_0018: ldc.i4.s 20 + IL_001a: bgt.s IL_0028 + + IL_001c: ldloc.0 + IL_001d: ldc.i4.s 15 + IL_001f: beq.s IL_005e + + IL_0021: ldloc.0 + IL_0022: ldc.i4.s 20 + IL_0024: beq.s IL_006c + + IL_0026: br.s IL_0096 + + IL_0028: ldloc.0 + IL_0029: ldc.i4.s 25 + IL_002b: beq.s IL_007a + + IL_002d: ldloc.0 + IL_002e: ldc.i4.s 30 + IL_0030: beq.s IL_0088 + + IL_0032: br.s IL_0096 + + IL_0034: nop + IL_0035: ldstr "zero" + IL_003a: call void [mscorlib]System.Console::WriteLine(string) + IL_003f: nop + IL_0040: br.s IL_0096 + + IL_0042: nop + IL_0043: ldstr "five" + IL_0048: call void [mscorlib]System.Console::WriteLine(string) + IL_004d: nop + IL_004e: br.s IL_0096 + + IL_0050: nop + IL_0051: ldstr "ten" + IL_0056: call void [mscorlib]System.Console::WriteLine(string) + IL_005b: nop + IL_005c: br.s IL_0096 + + IL_005e: nop + IL_005f: ldstr "fifteen" + IL_0064: call void [mscorlib]System.Console::WriteLine(string) + IL_0069: nop + IL_006a: br.s IL_0096 + + IL_006c: nop + IL_006d: ldstr "twenty" + IL_0072: call void [mscorlib]System.Console::WriteLine(string) + IL_0077: nop + IL_0078: br.s IL_0096 + + IL_007a: nop + IL_007b: ldstr "twenty-five" + IL_0080: call void [mscorlib]System.Console::WriteLine(string) + IL_0085: nop + IL_0086: br.s IL_0096 + + IL_0088: nop + IL_0089: ldstr "thirty" + IL_008e: call void [mscorlib]System.Console::WriteLine(string) + IL_0093: nop + IL_0094: br.s IL_0096 + + IL_0096: ret + } // end of method Switch::SwitchOverInt + .method public hidebysig static string ShortSwitchOverString(string text) cil managed { @@ -560,7 +655,7 @@ IL_0015: brfalse IL_00ef IL_001a: volatile. - IL_001c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{95C99B41-CBA3-42E4-A4DE-27E535671AB2}'::'$$method0x6000007-1' + IL_001c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{12C66A56-696F-4026-B79B-EFB40F4CD81E}'::'$$method0x6000008-1' IL_0021: brtrue.s IL_0084 IL_0023: ldc.i4.7 @@ -601,9 +696,9 @@ IL_0078: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_007d: volatile. - IL_007f: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{95C99B41-CBA3-42E4-A4DE-27E535671AB2}'::'$$method0x6000007-1' + IL_007f: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{12C66A56-696F-4026-B79B-EFB40F4CD81E}'::'$$method0x6000008-1' IL_0084: volatile. - IL_0086: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{95C99B41-CBA3-42E4-A4DE-27E535671AB2}'::'$$method0x6000007-1' + IL_0086: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{12C66A56-696F-4026-B79B-EFB40F4CD81E}'::'$$method0x6000008-1' IL_008b: ldloc.1 IL_008c: ldloca.s V_2 IL_008e: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2::TryGetValue(!0, @@ -686,7 +781,7 @@ IL_0015: brfalse IL_0165 IL_001a: volatile. - IL_001c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{95C99B41-CBA3-42E4-A4DE-27E535671AB2}'::'$$method0x6000008-1' + IL_001c: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{12C66A56-696F-4026-B79B-EFB40F4CD81E}'::'$$method0x6000009-1' IL_0021: brtrue IL_00ba IL_0026: ldc.i4.s 11 @@ -747,9 +842,9 @@ IL_00ae: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_00b3: volatile. - IL_00b5: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{95C99B41-CBA3-42E4-A4DE-27E535671AB2}'::'$$method0x6000008-1' + IL_00b5: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{12C66A56-696F-4026-B79B-EFB40F4CD81E}'::'$$method0x6000009-1' IL_00ba: volatile. - IL_00bc: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{95C99B41-CBA3-42E4-A4DE-27E535671AB2}'::'$$method0x6000008-1' + IL_00bc: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{12C66A56-696F-4026-B79B-EFB40F4CD81E}'::'$$method0x6000009-1' IL_00c1: ldloc.2 IL_00c2: ldloca.s V_3 IL_00c4: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2::TryGetValue(!0, @@ -1064,7 +1159,7 @@ IL_0034: brfalse IL_012d IL_0039: volatile. - IL_003b: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{95C99B41-CBA3-42E4-A4DE-27E535671AB2}'::'$$method0x600000d-1' + IL_003b: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{12C66A56-696F-4026-B79B-EFB40F4CD81E}'::'$$method0x600000e-1' IL_0040: brtrue.s IL_0097 IL_0042: ldc.i4.6 @@ -1100,9 +1195,9 @@ IL_008b: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_0090: volatile. - IL_0092: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{95C99B41-CBA3-42E4-A4DE-27E535671AB2}'::'$$method0x600000d-1' + IL_0092: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{12C66A56-696F-4026-B79B-EFB40F4CD81E}'::'$$method0x600000e-1' IL_0097: volatile. - IL_0099: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{95C99B41-CBA3-42E4-A4DE-27E535671AB2}'::'$$method0x600000d-1' + IL_0099: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{12C66A56-696F-4026-B79B-EFB40F4CD81E}'::'$$method0x600000e-1' IL_009e: ldloc.s V_6 IL_00a0: ldloca.s V_7 IL_00a2: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2::TryGetValue(!0, @@ -1196,14 +1291,14 @@ } // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Switch -.class private auto ansi '{95C99B41-CBA3-42E4-A4DE-27E535671AB2}' +.class private auto ansi '{12C66A56-696F-4026-B79B-EFB40F4CD81E}' extends [mscorlib]System.Object { .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) - .field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2 '$$method0x6000007-1' .field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2 '$$method0x6000008-1' - .field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2 '$$method0x600000d-1' -} // end of class '{95C99B41-CBA3-42E4-A4DE-27E535671AB2}' + .field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2 '$$method0x6000009-1' + .field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2 '$$method0x600000e-1' +} // end of class '{12C66A56-696F-4026-B79B-EFB40F4CD81E}' // ============================================================= diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.il index e09dd767c..bff2ea6b4 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.il @@ -10,7 +10,7 @@ .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. .ver 4:0:0:0 } -.assembly f3gworj3 +.assembly '2bmtzwrf' { .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 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. @@ -20,15 +20,15 @@ .hash algorithm 0x00008004 .ver 0:0:0:0 } -.module f3gworj3.dll -// MVID: {6837A40E-7A00-4F01-B2D7-DE0001F70EDF} +.module '2bmtzwrf.dll' +// MVID: {F90E3F03-F070-45F0-AC05-0914EF70B327} .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: 0x00780000 +// Image base: 0x02E10000 // =============== CLASS MEMBERS DECLARATION =================== @@ -382,6 +382,84 @@ IL_005b: ret } // end of method Switch::SwitchOverNullableIntNoNullCaseShifted + .method public hidebysig static void SwitchOverInt(int32 i) cil managed + { + // Code size 125 (0x7d) + .maxstack 2 + .locals init (int32 V_0) + IL_0000: ldarg.0 + IL_0001: stloc.0 + IL_0002: ldloc.0 + IL_0003: ldc.i4.s 10 + IL_0005: bgt.s IL_0015 + + IL_0007: ldloc.0 + IL_0008: ldc.i4.0 + IL_0009: beq.s IL_0030 + + IL_000b: ldloc.0 + IL_000c: ldc.i4.5 + IL_000d: beq.s IL_003b + + IL_000f: ldloc.0 + IL_0010: ldc.i4.s 10 + IL_0012: beq.s IL_0046 + + IL_0014: ret + + IL_0015: ldloc.0 + IL_0016: ldc.i4.s 20 + IL_0018: bgt.s IL_0025 + + IL_001a: ldloc.0 + IL_001b: ldc.i4.s 15 + IL_001d: beq.s IL_0051 + + IL_001f: ldloc.0 + IL_0020: ldc.i4.s 20 + IL_0022: beq.s IL_005c + + IL_0024: ret + + IL_0025: ldloc.0 + IL_0026: ldc.i4.s 25 + IL_0028: beq.s IL_0067 + + IL_002a: ldloc.0 + IL_002b: ldc.i4.s 30 + IL_002d: beq.s IL_0072 + + IL_002f: ret + + IL_0030: ldstr "zero" + IL_0035: call void [mscorlib]System.Console::WriteLine(string) + IL_003a: ret + + IL_003b: ldstr "five" + IL_0040: call void [mscorlib]System.Console::WriteLine(string) + IL_0045: ret + + IL_0046: ldstr "ten" + IL_004b: call void [mscorlib]System.Console::WriteLine(string) + IL_0050: ret + + IL_0051: ldstr "fifteen" + IL_0056: call void [mscorlib]System.Console::WriteLine(string) + IL_005b: ret + + IL_005c: ldstr "twenty" + IL_0061: call void [mscorlib]System.Console::WriteLine(string) + IL_0066: ret + + IL_0067: ldstr "twenty-five" + IL_006c: call void [mscorlib]System.Console::WriteLine(string) + IL_0071: ret + + IL_0072: ldstr "thirty" + IL_0077: call void [mscorlib]System.Console::WriteLine(string) + IL_007c: ret + } // end of method Switch::SwitchOverInt + .method public hidebysig static string ShortSwitchOverString(string text) cil managed { @@ -449,7 +527,7 @@ IL_0013: brfalse IL_00db IL_0018: volatile. - IL_001a: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{6837A40E-7A00-4F01-B2D7-DE0001F70EDF}'::'$$method0x6000007-1' + IL_001a: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{F90E3F03-F070-45F0-AC05-0914EF70B327}'::'$$method0x6000008-1' IL_001f: brtrue.s IL_0082 IL_0021: ldc.i4.7 @@ -490,9 +568,9 @@ IL_0076: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_007b: volatile. - IL_007d: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{6837A40E-7A00-4F01-B2D7-DE0001F70EDF}'::'$$method0x6000007-1' + IL_007d: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{F90E3F03-F070-45F0-AC05-0914EF70B327}'::'$$method0x6000008-1' IL_0082: volatile. - IL_0084: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{6837A40E-7A00-4F01-B2D7-DE0001F70EDF}'::'$$method0x6000007-1' + IL_0084: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{F90E3F03-F070-45F0-AC05-0914EF70B327}'::'$$method0x6000008-1' IL_0089: ldloc.0 IL_008a: ldloca.s V_1 IL_008c: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2::TryGetValue(!0, @@ -553,7 +631,7 @@ IL_0013: brfalse IL_013f IL_0018: volatile. - IL_001a: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{6837A40E-7A00-4F01-B2D7-DE0001F70EDF}'::'$$method0x6000008-1' + IL_001a: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{F90E3F03-F070-45F0-AC05-0914EF70B327}'::'$$method0x6000009-1' IL_001f: brtrue IL_00b8 IL_0024: ldc.i4.s 11 @@ -614,9 +692,9 @@ IL_00ac: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_00b1: volatile. - IL_00b3: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{6837A40E-7A00-4F01-B2D7-DE0001F70EDF}'::'$$method0x6000008-1' + IL_00b3: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{F90E3F03-F070-45F0-AC05-0914EF70B327}'::'$$method0x6000009-1' IL_00b8: volatile. - IL_00ba: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{6837A40E-7A00-4F01-B2D7-DE0001F70EDF}'::'$$method0x6000008-1' + IL_00ba: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{F90E3F03-F070-45F0-AC05-0914EF70B327}'::'$$method0x6000009-1' IL_00bf: ldloc.1 IL_00c0: ldloca.s V_2 IL_00c2: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2::TryGetValue(!0, @@ -845,7 +923,7 @@ IL_0031: brfalse IL_0119 IL_0036: volatile. - IL_0038: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{6837A40E-7A00-4F01-B2D7-DE0001F70EDF}'::'$$method0x600000d-1' + IL_0038: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{F90E3F03-F070-45F0-AC05-0914EF70B327}'::'$$method0x600000e-1' IL_003d: brtrue.s IL_0094 IL_003f: ldc.i4.6 @@ -881,9 +959,9 @@ IL_0088: call instance void class [mscorlib]System.Collections.Generic.Dictionary`2::Add(!0, !1) IL_008d: volatile. - IL_008f: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{6837A40E-7A00-4F01-B2D7-DE0001F70EDF}'::'$$method0x600000d-1' + IL_008f: stsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{F90E3F03-F070-45F0-AC05-0914EF70B327}'::'$$method0x600000e-1' IL_0094: volatile. - IL_0096: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{6837A40E-7A00-4F01-B2D7-DE0001F70EDF}'::'$$method0x600000d-1' + IL_0096: ldsfld class [mscorlib]System.Collections.Generic.Dictionary`2 '{F90E3F03-F070-45F0-AC05-0914EF70B327}'::'$$method0x600000e-1' IL_009b: ldloc.s V_6 IL_009d: ldloca.s V_7 IL_009f: call instance bool class [mscorlib]System.Collections.Generic.Dictionary`2::TryGetValue(!0, @@ -955,14 +1033,14 @@ } // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Switch -.class private auto ansi '{6837A40E-7A00-4F01-B2D7-DE0001F70EDF}' +.class private auto ansi '{F90E3F03-F070-45F0-AC05-0914EF70B327}' extends [mscorlib]System.Object { .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) - .field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2 '$$method0x6000007-1' .field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2 '$$method0x6000008-1' - .field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2 '$$method0x600000d-1' -} // end of class '{6837A40E-7A00-4F01-B2D7-DE0001F70EDF}' + .field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2 '$$method0x6000009-1' + .field static assembly class [mscorlib]System.Collections.Generic.Dictionary`2 '$$method0x600000e-1' +} // end of class '{F90E3F03-F070-45F0-AC05-0914EF70B327}' // ============================================================= diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.roslyn.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.roslyn.il index bd0488bb3..50e230ad0 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.roslyn.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.opt.roslyn.il @@ -25,14 +25,14 @@ .ver 0:0:0:0 } .module Switch.dll -// MVID: {04DBCBA6-8175-41CD-8917-9428C9765986} +// MVID: {C83C2FCD-FA35-4E74-9418-9B91F2C0748A} .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: 0x00830000 +// Image base: 0x01020000 // =============== CLASS MEMBERS DECLARATION =================== @@ -393,6 +393,80 @@ IL_005b: ret } // end of method Switch::SwitchOverNullableIntNoNullCaseShifted + .method public hidebysig static void SwitchOverInt(int32 i) cil managed + { + // Code size 122 (0x7a) + .maxstack 2 + IL_0000: ldarg.0 + IL_0001: ldc.i4.s 10 + IL_0003: bgt.s IL_0012 + + IL_0005: ldarg.0 + IL_0006: brfalse.s IL_002d + + IL_0008: ldarg.0 + IL_0009: ldc.i4.5 + IL_000a: beq.s IL_0038 + + IL_000c: ldarg.0 + IL_000d: ldc.i4.s 10 + IL_000f: beq.s IL_0043 + + IL_0011: ret + + IL_0012: ldarg.0 + IL_0013: ldc.i4.s 20 + IL_0015: bgt.s IL_0022 + + IL_0017: ldarg.0 + IL_0018: ldc.i4.s 15 + IL_001a: beq.s IL_004e + + IL_001c: ldarg.0 + IL_001d: ldc.i4.s 20 + IL_001f: beq.s IL_0059 + + IL_0021: ret + + IL_0022: ldarg.0 + IL_0023: ldc.i4.s 25 + IL_0025: beq.s IL_0064 + + IL_0027: ldarg.0 + IL_0028: ldc.i4.s 30 + IL_002a: beq.s IL_006f + + IL_002c: ret + + IL_002d: ldstr "zero" + IL_0032: call void [mscorlib]System.Console::WriteLine(string) + IL_0037: ret + + IL_0038: ldstr "five" + IL_003d: call void [mscorlib]System.Console::WriteLine(string) + IL_0042: ret + + IL_0043: ldstr "ten" + IL_0048: call void [mscorlib]System.Console::WriteLine(string) + IL_004d: ret + + IL_004e: ldstr "fifteen" + IL_0053: call void [mscorlib]System.Console::WriteLine(string) + IL_0058: ret + + IL_0059: ldstr "twenty" + IL_005e: call void [mscorlib]System.Console::WriteLine(string) + IL_0063: ret + + IL_0064: ldstr "twenty-five" + IL_0069: call void [mscorlib]System.Console::WriteLine(string) + IL_006e: ret + + IL_006f: ldstr "thirty" + IL_0074: call void [mscorlib]System.Console::WriteLine(string) + IL_0079: ret + } // end of method Switch::SwitchOverInt + .method public hidebysig static string ShortSwitchOverString(string text) cil managed { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.roslyn.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.roslyn.il index 38331d470..eb57faa5d 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.roslyn.il +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/Switch.roslyn.il @@ -25,14 +25,14 @@ .ver 0:0:0:0 } .module Switch.dll -// MVID: {87DEBC09-DFAD-437D-9221-109E2117A07A} +// MVID: {B5801C4A-C979-4EC9-B5B2-295587FEEF30} .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: 0x02960000 +// Image base: 0x005D0000 // =============== CLASS MEMBERS DECLARATION =================== @@ -521,6 +521,108 @@ IL_0079: ret } // end of method Switch::SwitchOverNullableIntNoNullCaseShifted + .method public hidebysig static void SwitchOverInt(int32 i) cil managed + { + // Code size 161 (0xa1) + .maxstack 2 + .locals init (int32 V_0) + IL_0000: nop + IL_0001: ldarg.0 + IL_0002: stloc.0 + IL_0003: ldloc.0 + IL_0004: ldc.i4.s 10 + IL_0006: bgt.s IL_001d + + IL_0008: ldloc.0 + IL_0009: brfalse.s IL_003e + + IL_000b: br.s IL_000d + + IL_000d: ldloc.0 + IL_000e: ldc.i4.5 + IL_000f: beq.s IL_004c + + IL_0011: br.s IL_0013 + + IL_0013: ldloc.0 + IL_0014: ldc.i4.s 10 + IL_0016: beq.s IL_005a + + IL_0018: br IL_00a0 + + IL_001d: ldloc.0 + IL_001e: ldc.i4.s 20 + IL_0020: bgt.s IL_0030 + + IL_0022: ldloc.0 + IL_0023: ldc.i4.s 15 + IL_0025: beq.s IL_0068 + + IL_0027: br.s IL_0029 + + IL_0029: ldloc.0 + IL_002a: ldc.i4.s 20 + IL_002c: beq.s IL_0076 + + IL_002e: br.s IL_00a0 + + IL_0030: ldloc.0 + IL_0031: ldc.i4.s 25 + IL_0033: beq.s IL_0084 + + IL_0035: br.s IL_0037 + + IL_0037: ldloc.0 + IL_0038: ldc.i4.s 30 + IL_003a: beq.s IL_0092 + + IL_003c: br.s IL_00a0 + + IL_003e: nop + IL_003f: ldstr "zero" + IL_0044: call void [mscorlib]System.Console::WriteLine(string) + IL_0049: nop + IL_004a: br.s IL_00a0 + + IL_004c: nop + IL_004d: ldstr "five" + IL_0052: call void [mscorlib]System.Console::WriteLine(string) + IL_0057: nop + IL_0058: br.s IL_00a0 + + IL_005a: nop + IL_005b: ldstr "ten" + IL_0060: call void [mscorlib]System.Console::WriteLine(string) + IL_0065: nop + IL_0066: br.s IL_00a0 + + IL_0068: nop + IL_0069: ldstr "fifteen" + IL_006e: call void [mscorlib]System.Console::WriteLine(string) + IL_0073: nop + IL_0074: br.s IL_00a0 + + IL_0076: nop + IL_0077: ldstr "twenty" + IL_007c: call void [mscorlib]System.Console::WriteLine(string) + IL_0081: nop + IL_0082: br.s IL_00a0 + + IL_0084: nop + IL_0085: ldstr "twenty-five" + IL_008a: call void [mscorlib]System.Console::WriteLine(string) + IL_008f: nop + IL_0090: br.s IL_00a0 + + IL_0092: nop + IL_0093: ldstr "thirty" + IL_0098: call void [mscorlib]System.Console::WriteLine(string) + IL_009d: nop + IL_009e: br.s IL_00a0 + + IL_00a0: ret + } // end of method Switch::SwitchOverInt + .method public hidebysig static string ShortSwitchOverString(string text) cil managed { diff --git a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs index 8be341e76..909339d6d 100644 --- a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs @@ -175,6 +175,12 @@ namespace ICSharpCode.Decompiler.CSharp else ConvertSwitchSectionBody(astSection, section.Body); break; + case Leave leave: + if (astSection.CaseLabels.Count == 1 && astSection.CaseLabels.First().Expression.IsNull && leave.TargetContainer == switchContainer) { + stmt.SwitchSections.Remove(astSection); + break; + } + goto default; default: ConvertSwitchSectionBody(astSection, section.Body); break;