From 18ee984ade1b08ebac063004351409f6100c585f Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 27 Jul 2019 19:31:13 +0200 Subject: [PATCH] Add support for "definitely assigned if true/false" to data flow analysis. --- .../ICSharpCode.Decompiler.Tests.csproj | 1 + .../PrettyTestRunner.cs | 6 +++ .../TestCases/Pretty/OutVariables.cs | 45 +++++++++++++++++ .../FlowAnalysis/DataFlowVisitor.cs | 50 +++++++++++++++++-- 4 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/Pretty/OutVariables.cs diff --git a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj index c6d570204..0d0fac14d 100644 --- a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj @@ -77,6 +77,7 @@ + diff --git a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs index fa1bb5dd8..0f932c117 100644 --- a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs @@ -239,6 +239,12 @@ namespace ICSharpCode.Decompiler.Tests RunForLibrary(cscOptions: cscOptions, asmOptions: AssemblerOptions.UseOwnDisassembler); } + [Test] + public void OutVariables([ValueSource(nameof(roslynOnlyOptions))] CompilerOptions cscOptions) + { + RunForLibrary(cscOptions: cscOptions); + } + [Test] public void InitializerTests([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions) { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OutVariables.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OutVariables.cs new file mode 100644 index 000000000..5a6916b8e --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OutVariables.cs @@ -0,0 +1,45 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this +// software and associated documentation files (the "Software"), to deal in the Software +// without restriction, including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +// to whom the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System; +using System.Collections.Generic; + +namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty +{ + public class PatternMatching + { + public static void OutVarInShortCircuit(Dictionary d) + { + if (d.Count > 2 && d.TryGetValue(42, out string value)) { + Console.WriteLine(value); + } + } + + public static Action CapturedOutVarInShortCircuit(Dictionary d) + { + // Note: needs reasoning about "definitely assigned if true" + // to ensure that the value is initialized when the delegate is declared. + if (d.Count > 2 && d.TryGetValue(42, out string value)) { + return delegate { + Console.WriteLine(value); + }; + } + return null; + } + } +} diff --git a/ICSharpCode.Decompiler/FlowAnalysis/DataFlowVisitor.cs b/ICSharpCode.Decompiler/FlowAnalysis/DataFlowVisitor.cs index a283fcad0..a53fcfb79 100644 --- a/ICSharpCode.Decompiler/FlowAnalysis/DataFlowVisitor.cs +++ b/ICSharpCode.Decompiler/FlowAnalysis/DataFlowVisitor.cs @@ -603,16 +603,60 @@ namespace ICSharpCode.Decompiler.FlowAnalysis protected internal override void VisitIfInstruction(IfInstruction inst) { DebugStartPoint(inst); - inst.Condition.AcceptVisitor(this); - State branchState = state.Clone(); + var (beforeThen, beforeElse) = EvaluateCondition(inst.Condition); + state = beforeThen; inst.TrueInst.AcceptVisitor(this); State afterTrueState = state; - state = branchState; + state = beforeElse; inst.FalseInst.AcceptVisitor(this); state.JoinWith(afterTrueState); DebugEndPoint(inst); } + /// + /// Evaluates the condition of an if. + /// + /// + /// A pair of: + /// * The state after the condition evaluates to true + /// * The state after the condition evaluates to false + /// + /// + /// this.state is invalid after this function was called, and must be overwritten + /// with one of the return values. + /// + (State OnTrue, State OnFalse) EvaluateCondition(ILInstruction inst) + { + if (inst is IfInstruction ifInst) { + // 'if (a?b:c)' or similar. + // This also includes conditions that are logic.not, logic.and, logic.or. + DebugStartPoint(ifInst); + var (beforeThen, beforeElse) = EvaluateCondition(ifInst.Condition); + state = beforeThen; + var (afterThenTrue, afterThenFalse) = EvaluateCondition(ifInst.TrueInst); + state = beforeElse; + var (afterElseTrue, afterElseFalse) = EvaluateCondition(ifInst.FalseInst); + + var onTrue = afterThenTrue; + onTrue.JoinWith(afterElseTrue); + var onFalse = afterThenFalse; + onFalse.JoinWith(afterElseFalse); + + DebugEndPoint(ifInst); + return (onTrue, onFalse); + } else if (inst is LdcI4 constant) { + if (constant.Value == 0) { + return (bottomState.Clone(), state); + } else { + return (state, bottomState.Clone()); + } + } else { + // other kind of condition + inst.AcceptVisitor(this); + return (state, state.Clone()); + } + } + protected internal override void VisitNullCoalescingInstruction(NullCoalescingInstruction inst) { HandleBinaryWithOptionalEvaluation(inst, inst.ValueInst, inst.FallbackInst);