`ReduceNestingTransform` increases nesting for long `if` chain
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 52/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- csharp
- Ambito
- devtools, reverse-engineering
Direzione di ricerca
Inizia da ICSharpCode.Decompiler/IL/Transforms/ReduceNestingTransform.cs, in particolare da ImproveILOrdering alle righe indicate, e riproduci la trasformazione usando l’esempio allegato di System.Private.CoreLib. Confronta l’ILAst prima e dopo ReduceNestingTransform; il lavoro è completato quando la lunga catena di condizioni non viene convertita nella struttura if profondamente annidata mostrata nel report.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Input code
System.Private.CoreLib from .NET 10.0.2 runtime.
Type: Microsoft.Win32.OAVariantLib
Method: ToOAVariant
Erroneous output
ILSpy produces an insane looking if chain for this method.
private static ComVariant ToOAVariant(object input)
{
if (!(input is string value))
{
if (!(input is DateTime value2))
{
if (!(input is bool value3))
{
if (!(input is decimal value4))
{
if (!(input is sbyte value5))
{
if (!(input is byte value6))
{
if (!(input is short value7))
{
if (!(input is ushort value8))
{
if (!(input is int value9))
{
if (!(input is uint value10))
{
if (!(input is long value11))
{
if (!(input is ulong value12))
{
if (!(input is float value13))
{
if (!(input is double value14))
{
if (input != null)
{
if (!(input is Missing))
{
if (input is DBNull)
{
return ComVariant.Null;
}
return Variant.GetIUnknownOrIDispatchFromObject(input);
}
throw new NotSupportedException(SR.NotSupported_ChangeType);
}
return default(ComVariant);
}
return ComVariant.Create(value14);
}
return ComVariant.Create(value13);
}
return ComVariant.Create(value12);
}
return ComVariant.Create(value11);
}
return ComVariant.Create(value10);
}
return ComVariant.Create(value9);
}
return ComVariant.Create(value8);
}
return ComVariant.Create(value7);
}
return ComVariant.Create(value6);
}
return ComVariant.Create(value5);
}
return ComVariant.Create(value4);
}
return ComVariant.Create(value3);
}
return ComVariant.Create(value2);
}
return ComVariant.Create(value);
}
The original code, which uses a switch on types that ILSpy does not support, can be found here:
A better decompialtion of this code without the switch on type would be:
private static ComVariant ToOAVariant(object input)
{
if (input is string value)
{
return ComVariant.Create(value);
}
if (input is DateTime value2)
{
return ComVariant.Create(value2);
}
if (input is bool value3)
{
return ComVariant.Create(value3);
}
if (input is decimal value4)
{
return ComVariant.Create(value4);
}
if (input is sbyte value5)
{
return ComVariant.Create(value5);
}
if (input is byte value6)
{
return ComVariant.Create(value6);
}
if (input is short value7)
{
return ComVariant.Create(value7);
}
if (input is ushort value8)
{
return ComVariant.Create(value8);
}
if (input is int value9)
{
return ComVariant.Create(value9);
}
if (input is uint value10)
{
return ComVariant.Create(value10);
}
if (input is long value11)
{
return ComVariant.Create(value11);
}
if (input is ulong value12)
{
return ComVariant.Create(value12);
}
if (input is float value13)
{
return ComVariant.Create(value13);
}
if (input is double value14)
{
return ComVariant.Create(value14);
}
if (input == null)
{
return default(ComVariant);
}
if (input is Missing)
{
throw new NotSupportedException(SR.NotSupported_ChangeType);
}
if (input is DBNull)
{
return ComVariant.Null;
}
return Variant.GetIUnknownOrIDispatchFromObject(input);
}
I've debugged this using the ILAst debug steps feature and up until ReduceNestingTransform gets ran, the ILAst looks as follows:
BlockContainer {
Block IL_0000 (incoming: 1) {
if (match.notnull.type[System.String] (V_0 = ldloc input)) Block IL_013c {
leave IL_0000 (call Create(ldloc V_0))
}
if (match.notnull.type[System.DateTime] (V_1 = ldloc input)) Block IL_014f {
leave IL_0000 (call Create(ldloc V_1))
}
if (match.notnull.type[System.Boolean] (V_2 = ldloc input)) Block IL_015c {
leave IL_0000 (call Create(ldloc V_2))
}
if (match.notnull.type[System.Decimal] (V_3 = ldloc input)) Block IL_0169 {
leave IL_0000 (call Create(ldloc V_3))
}
if (match.notnull.type[System.SByte] (V_4 = ldloc input)) Block IL_0177 {
leave IL_0000 (call Create(ldloc V_4))
}
if (match.notnull.type[System.Byte] (V_5 = ldloc input)) Block IL_0185 {
leave IL_0000 (call Create(ldloc V_5))
}
if (match.notnull.type[System.Int16] (V_6 = ldloc input)) Block IL_0193 {
leave IL_0000 (call Create(ldloc V_6))
}
if (match.notnull.type[System.UInt16] (V_7 = ldloc input)) Block IL_019e {
leave IL_0000 (call Create(ldloc V_7))
}
if (match.notnull.type[System.Int32] (V_8 = ldloc input)) Block IL_01a9 {
leave IL_0000 (call Create(ldloc V_8))
}
if (match.notnull.type[System.UInt32] (V_9 = ldloc input)) Block IL_01b4 {
leave IL_0000 (call Create(ldloc V_9))
}
if (match.notnull.type[System.Int64] (V_10 = ldloc input)) Block IL_01bf {
leave IL_0000 (call Create(ldloc V_10))
}
if (match.notnull.type[System.UInt64] (V_11 = ldloc input)) Block IL_01ca {
leave IL_0000 (call Create(ldloc V_11))
}
if (match.notnull.type[System.Single] (V_12 = ldloc input)) Block IL_01d5 {
leave IL_0000 (call Create(ldloc V_12))
}
if (match.notnull.type[System.Double] (V_13 = ldloc input)) Block IL_01e0 {
leave IL_0000 (call Create(ldloc V_13))
}
if (comp.o(ldloc input == ldnull)) Block IL_01e4 {
leave IL_0000 (default.value System.Runtime.InteropServices.Marshalling.ComVariant)
}
if (comp.o(isinst System.Reflection.Missing(ldloc input) != ldnull)) Block IL_01f2 {
throw(newobj NotSupportedException..ctor(call get_NotSupported_ChangeType()))
}
if (comp.o(isinst System.DBNull(ldloc input) != ldnull)) Block IL_01fd {
leave IL_0000 (call get_Null())
}
leave IL_0000 (call GetIUnknownOrIDispatchFromObject(ldloc input))
}
}
After ReduceNestingTransform the ILAst contains the massively nested if statements found in the final C# code.
BlockContainer {
Block IL_0000 (incoming: 1) {
if (logic.not(match.notnull.type[System.String] (V_0 = ldloc input))) Block IL_0013 {
if (logic.not(match.notnull.type[System.DateTime] (V_1 = ldloc input))) Block IL_0027 {
if (logic.not(match.notnull.type[System.Boolean] (V_2 = ldloc input))) Block IL_003b {
if (logic.not(match.notnull.type[System.Decimal] (V_3 = ldloc input))) Block IL_004f {
if (logic.not(match.notnull.type[System.SByte] (V_4 = ldloc input))) Block IL_0064 {
if (logic.not(match.notnull.type[System.Byte] (V_5 = ldloc input))) Block IL_0079 {
if (logic.not(match.notnull.type[System.Int16] (V_6 = ldloc input))) Block IL_008e {
if (logic.not(match.notnull.type[System.UInt16] (V_7 = ldloc input))) Block IL_00a3 {
if (logic.not(match.notnull.type[System.Int32] (V_8 = ldloc input))) Block IL_00b8 {
if (logic.not(match.notnull.type[System.UInt32] (V_9 = ldloc input))) Block IL_00cd {
if (logic.not(match.notnull.type[System.Int64] (V_10 = ldloc input))) Block IL_00e2 {
if (logic.not(match.notnull.type[System.UInt64] (V_11 = ldloc input))) Block IL_00f7 {
if (logic.not(match.notnull.type[System.Single] (V_12 = ldloc input))) Block IL_010c {
if (logic.not(match.notnull.type[System.Double] (V_13 = ldloc input))) Block IL_011c {
if (comp.o(ldloc input != ldnull)) Block IL_0127 {
if (comp.o(isinst System.Reflection.Missing(ldloc input) == ldnull)) Block IL_0132 {
if (comp.o(isinst System.DBNull(ldloc input) != ldnull)) Block IL_01fd {
leave IL_0000 (call get_Null())
}
leave IL_0000 (call GetIUnknownOrIDispatchFromObject(ldloc input))
}
throw(newobj NotSupportedException..ctor(call get_NotSupported_ChangeType()))
}
leave IL_0000 (default.value System.Runtime.InteropServices.Marshalling.ComVariant)
}
leave IL_0000 (call Create(ldloc V_13))
}
leave IL_0000 (call Create(ldloc V_12))
}
leave IL_0000 (call Create(ldloc V_11))
}
leave IL_0000 (call Create(ldloc V_10))
}
leave IL_0000 (call Create(ldloc V_9))
}
leave IL_0000 (call Create(ldloc V_8))
}
leave IL_0000 (call Create(ldloc V_7))
}
leave IL_0000 (call Create(ldloc V_6))
}
leave IL_0000 (call Create(ldloc V_5))
}
leave IL_0000 (call Create(ldloc V_4))
}
leave IL_0000 (call Create(ldloc V_3))
}
leave IL_0000 (call Create(ldloc V_2))
}
leave IL_0000 (call Create(ldloc V_1))
}
leave IL_0000 (call Create(ldloc V_0))
}
}
After some testing, I have pinpointed this to the ImproveILOrdering step in ReduceNestingTransform.
https://github.com/icsharpcode/ILSpy/blob/4a29de5a4e95630982730da0dd8081ced4a58eeb/ICSharpCode.Decompiler/IL/Transforms/ReduceNestingTransform.cs#L173-L202
Details
- Product in use: e.g. ILSpy commit 4a29de5a4e95630982730da0dd8081ced4a58eeb
- Lingua principale
- C#
- Stelle
- 27.1k
- Fork
- 3.8k
- Merge medio
- 1g 23h
- PR unite (30g)
- 44
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di icsharpcode/ILSpy
-
AvaloniaEdit Issues to dateApertaExternal UI
Difficoltà 1/5 1-3 ore Idoneità per principianti 75/100
icsharpcode/ILSpy#4284 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 55/100
icsharpcode/ILSpy#4357 ·
I maintainer di solito rispondono entro 1 giorno
-
Misdecompilation of a lazy initialization patternForse già presa @siegfriedpammer l’ha presa 5 giorni fa. ApertaBug Decompiler
Difficoltà 4/5 3-5 giorni Idoneità per principianti 20/100
icsharpcode/ILSpy#4349 ·
I maintainer di solito rispondono entro 1 giorno
-
Bug
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
icsharpcode/ILSpy#4343 ·
I maintainer di solito rispondono entro 1 giorno
-
AI
Difficoltà 4/5 1-2 giorni Idoneità per principianti 55/100
icsharpcode/ILSpy#4300 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di icsharpcode/ILSpy
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
umbraco/Umbraco.Automate#612 ·
I maintainer di solito rispondono entro 1 giorno
-
崩溃日志只按 CRLF 分行Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
PCL-Community/PCL-CE#3872 ·
I maintainer di solito rispondono entro 1 giorno
-
type:task ux
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
elsa-workflows/elsa-foundation#5253 ·
I maintainer di solito rispondono entro 1 giorno