Browse Source

Add support for "definitely assigned if true/false" to data flow analysis.

pull/1612/head
Daniel Grunwald 6 years ago
parent
commit
18ee984ade
  1. 1
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 6
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  3. 45
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/OutVariables.cs
  4. 50
      ICSharpCode.Decompiler/FlowAnalysis/DataFlowVisitor.cs

1
ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

@ -77,6 +77,7 @@ @@ -77,6 +77,7 @@
<ItemGroup>
<Compile Include="DisassemblerPrettyTestRunner.cs" />
<Compile Include="TestCases\ILPretty\ConstantBlobs.cs" />
<Compile Include="TestCases\Pretty\OutVariables.cs" />
<None Include="TestCases\ILPretty\DirectCallToExplicitInterfaceImpl.cs" />
<Compile Include="TestCases\ILPretty\Issue1389.cs" />
<Compile Include="TestCases\ILPretty\Issue1454.cs" />

6
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -239,6 +239,12 @@ namespace ICSharpCode.Decompiler.Tests @@ -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)
{

45
ICSharpCode.Decompiler.Tests/TestCases/Pretty/OutVariables.cs

@ -0,0 +1,45 @@ @@ -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<int, string> d)
{
if (d.Count > 2 && d.TryGetValue(42, out string value)) {
Console.WriteLine(value);
}
}
public static Action CapturedOutVarInShortCircuit(Dictionary<int, string> 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;
}
}
}

50
ICSharpCode.Decompiler/FlowAnalysis/DataFlowVisitor.cs

@ -603,16 +603,60 @@ namespace ICSharpCode.Decompiler.FlowAnalysis @@ -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);
}
/// <summary>
/// Evaluates the condition of an if.
/// </summary>
/// <returns>
/// A pair of:
/// * The state after the condition evaluates to true
/// * The state after the condition evaluates to false
/// </returns>
/// <remarks>
/// <c>this.state</c> is invalid after this function was called, and must be overwritten
/// with one of the return values.
/// </remarks>
(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);

Loading…
Cancel
Save