From ab1598c5475dd945b3e165658988c78fedc16911 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Tue, 10 Oct 2017 00:50:23 +0200 Subject: [PATCH] Fix bug in TransformRoslynPostIncDecOperatorOnAddress; Add DelegateConstruction tests --- ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs | 2 +- .../IL/Transforms/DelegateConstruction.cs | 8 ++++++++ .../IL/Transforms/TransformAssignment.cs | 4 +++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs index b0afed891..483e1f3b0 100644 --- a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs @@ -97,7 +97,7 @@ namespace ICSharpCode.Decompiler.Tests Run(cscOptions: cscOptions); } - [Test, Ignore] + [Test] public void DelegateConstruction([ValueSource("defaultOptions")] CompilerOptions cscOptions) { Run(cscOptions: cscOptions); diff --git a/ICSharpCode.Decompiler/IL/Transforms/DelegateConstruction.cs b/ICSharpCode.Decompiler/IL/Transforms/DelegateConstruction.cs index 6b0150216..da1a91382 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/DelegateConstruction.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/DelegateConstruction.cs @@ -319,6 +319,14 @@ namespace ICSharpCode.Decompiler.IL.Transforms throw new NotImplementedException(); } } + + protected internal override void VisitCompoundAssignmentInstruction(CompoundAssignmentInstruction inst) + { + base.VisitCompoundAssignmentInstruction(inst); + if (inst.Target.MatchLdLoc(out var v)) { + inst.ReplaceWith(new StLoc(v, new BinaryNumericInstruction(inst.Operator, inst.Target, inst.Value, inst.CheckForOverflow, inst.Sign))); + } + } } #endregion } diff --git a/ICSharpCode.Decompiler/IL/Transforms/TransformAssignment.cs b/ICSharpCode.Decompiler/IL/Transforms/TransformAssignment.cs index 7a2f2e7e9..a1de1bf3e 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/TransformAssignment.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/TransformAssignment.cs @@ -338,6 +338,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms var stobj = block.Instructions.ElementAtOrDefault(i + 1) as StObj; if (inst == null || stobj == null) return false; + if (!inst.Variable.IsSingleDefinition || inst.Variable.LoadCount != 1) + return false; if (!inst.Value.MatchLdObj(out var loadTarget, out var loadType) || !loadTarget.MatchLdFlda(out var fieldTarget, out var field)) return false; if (!stobj.Target.MatchLdFlda(out var fieldTarget2, out var field2)) @@ -348,7 +350,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms if (binary == null || !binary.Left.MatchLdLoc(inst.Variable) || !binary.Right.MatchLdcI4(1) || (binary.Operator != BinaryNumericOperator.Add && binary.Operator != BinaryNumericOperator.Sub)) return false; - context.Step($"TransformRoslynPostIncDecOperator", inst); + context.Step("TransformRoslynPostIncDecOperator", inst); stobj.ReplaceWith(new CompoundAssignmentInstruction(binary, inst.Value, binary.Right, loadType, CompoundAssignmentType.EvaluatesToOldValue)); block.Instructions.RemoveAt(i); return true;