From 8b85feb8d54171fc2466bcd6ee4a86e69781bee6 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 25 Jun 2016 18:52:15 +0200 Subject: [PATCH] Fix missing checked/unchecked annotations --- .../CSharp/ExpressionBuilder.cs | 21 ++++++++++++++++++- .../Tests/RoundtripAssembly.cs | 8 ++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index 68256d0ab..ee3a5710a 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -499,6 +499,8 @@ namespace ICSharpCode.Decompiler.CSharp var resultExpr = new BinaryOperatorExpression(left.Expression, op, right.Expression) .WithILInstruction(inst) .WithRR(rr); + if (BinaryOperatorMightCheckForOverflow(op)) + resultExpr.Expression.AddAnnotation(inst.CheckForOverflow ? AddCheckedBlocks.CheckedAnnotation : AddCheckedBlocks.UncheckedAnnotation); return resultExpr.ConvertTo(compilation.FindType(inst.ResultType.ToKnownTypeCode()), this); } else { rr = resolverWithOverflowCheck.ResolveBinaryOperator(op, left.ResolveResult, right.ResolveResult); @@ -511,12 +513,29 @@ namespace ICSharpCode.Decompiler.CSharp right = right.ConvertTo(targetType, this); rr = resolverWithOverflowCheck.ResolveBinaryOperator(op, left.ResolveResult, right.ResolveResult); } - return new BinaryOperatorExpression(left.Expression, op, right.Expression) + var resultExpr = new BinaryOperatorExpression(left.Expression, op, right.Expression) .WithILInstruction(inst) .WithRR(rr); + if (BinaryOperatorMightCheckForOverflow(op)) + resultExpr.Expression.AddAnnotation(inst.CheckForOverflow ? AddCheckedBlocks.CheckedAnnotation : AddCheckedBlocks.UncheckedAnnotation); + return resultExpr; } } + static bool BinaryOperatorMightCheckForOverflow(BinaryOperatorType op) + { + switch (op) { + case BinaryOperatorType.BitwiseAnd: + case BinaryOperatorType.BitwiseOr: + case BinaryOperatorType.ExclusiveOr: + case BinaryOperatorType.ShiftLeft: + case BinaryOperatorType.ShiftRight: + return false; + default: + return true; + } + } + /// /// Gets whether has the specified . /// If is None, always returns true. diff --git a/ICSharpCode.Decompiler/Tests/RoundtripAssembly.cs b/ICSharpCode.Decompiler/Tests/RoundtripAssembly.cs index 6a90ab795..718facca4 100644 --- a/ICSharpCode.Decompiler/Tests/RoundtripAssembly.cs +++ b/ICSharpCode.Decompiler/Tests/RoundtripAssembly.cs @@ -37,11 +37,7 @@ namespace ICSharpCode.Decompiler.Tests [Test] public void Cecil_net45() { - try { - RunWithTest("Mono.Cecil-net45", "Mono.Cecil.dll", "Mono.Cecil.Tests.dll"); - } catch (CompilationFailedException ex) { - Assert.Ignore(ex.Message); - } + RunWithTest("Mono.Cecil-net45", "Mono.Cecil.dll", "Mono.Cecil.Tests.dll"); } [Test] @@ -49,7 +45,7 @@ namespace ICSharpCode.Decompiler.Tests { try { RunWithOutput("Random Tests\\TestCases", "TestCase-1.exe"); - } catch (CompilationFailedException ex) { + } catch (AssertionException ex) { Assert.Ignore(ex.Message); } }