Skip to content

Commit

Permalink
Extract debug-only helper
Browse files Browse the repository at this point in the history
  • Loading branch information
alrz committed Sep 10, 2021
1 parent 18fcbdb commit 7c13399
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Xunit;
using Roslyn.Utilities;
using ReferenceEqualityComparer = Roslyn.Utilities.ReferenceEqualityComparer;
using System.Diagnostics;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{
Expand Down Expand Up @@ -439,6 +440,26 @@ protected static void AssertEmpty(SymbolInfo info)
Assert.Null(info.Symbol);
Assert.Equal(CandidateReason.None, info.CandidateReason);
}

protected static void VerifyDecisionDagDump<T>(Compilation comp, string expectedDecisionDag)
where T : CSharpSyntaxNode
{
#if DEBUG
var tree = comp.SyntaxTrees.First();
var node = tree.GetRoot().DescendantNodes().OfType<T>().First();
var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
var binder = model.GetEnclosingBinder(node.SpanStart);
var decisionDag = node switch
{
SwitchStatementSyntax n => ((BoundSwitchStatement)binder.BindStatement(n, BindingDiagnosticBag.Discarded)).DecisionDag,
SwitchExpressionSyntax n => ((BoundSwitchExpression)binder.BindExpression(n, BindingDiagnosticBag.Discarded)).DecisionDag,
IsPatternExpressionSyntax n => ((BoundIsPatternExpression)binder.BindExpression(n, BindingDiagnosticBag.Discarded)).DecisionDag,
var v => throw ExceptionUtilities.UnexpectedValue(v)
};

AssertEx.Equal(expectedDecisionDag, decisionDag.Dump());
#endif
}
#endregion helpers
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2156,12 +2156,7 @@ void Test(int[] a)
Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "[42]").WithLocation(9, 18)
);

var tree = comp.SyntaxTrees.First();
var @switch = tree.GetRoot().DescendantNodes().OfType<SwitchStatementSyntax>().Single();
var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
var binder = model.GetEnclosingBinder(@switch.SpanStart);
var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded);
AssertEx.Equal(
VerifyDecisionDagDump<SwitchStatementSyntax>(comp,
@"[0]: t0 != null ? [1] : [9]
[1]: t1 = t0.Length; [2]
[2]: t1 >= 1 ? [3] : [9]
Expand All @@ -2177,7 +2172,7 @@ void Test(int[] a)
case [42]:
break;
}`
", boundSwitch.DecisionDag.Dump());
");
}

[Fact]
Expand All @@ -2202,12 +2197,7 @@ void Test(int[] a, int[] b)
// case ([42], [43]):
Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "([42], [43])").WithLocation(9, 18));

var tree = comp.SyntaxTrees.First();
var @switch = tree.GetRoot().DescendantNodes().OfType<SwitchStatementSyntax>().Single();
var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
var binder = model.GetEnclosingBinder(@switch.SpanStart);
var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded);
AssertEx.Equal(
VerifyDecisionDagDump<SwitchStatementSyntax>(comp,
@"[0]: t1 = t0.a; [1]
[1]: t1 != null ? [2] : [22]
[2]: t2 = t1.Length; [3]
Expand Down Expand Up @@ -2236,7 +2226,7 @@ void Test(int[] a, int[] b)
case ([42], [43]):
break;
}`
", boundSwitch.DecisionDag.Dump());
");
}

[Fact]
Expand Down Expand Up @@ -2295,12 +2285,7 @@ void Test(int[] a)
Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "[1, 2, 3]").WithLocation(9, 18)
);

var tree = comp.SyntaxTrees.First();
var @switch = tree.GetRoot().DescendantNodes().OfType<SwitchStatementSyntax>().Single();
var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
var binder = model.GetEnclosingBinder(@switch.SpanStart);
var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded);
AssertEx.Equal(
VerifyDecisionDagDump<SwitchStatementSyntax>(comp,
@"[0]: t0 != null ? [1] : [13]
[1]: t1 = t0.Length; [2]
[2]: t1 >= 2 ? [3] : [13]
Expand All @@ -2320,7 +2305,7 @@ void Test(int[] a)
case [1, 2, 3]:
break;
}`
", boundSwitch.DecisionDag.Dump());
");
}

[Fact]
Expand Down Expand Up @@ -2355,12 +2340,7 @@ static void Main()
comp.VerifyEmitDiagnostics();
CompileAndVerify(comp, expectedOutput: expectedOutput);

var tree = comp.SyntaxTrees.First();
var @switch = tree.GetRoot().DescendantNodes().OfType<SwitchStatementSyntax>().Single();
var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
var binder = model.GetEnclosingBinder(@switch.SpanStart);
var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded);
AssertEx.Equal(
VerifyDecisionDagDump<SwitchStatementSyntax>(comp,
@"[0]: t0 != null ? [1] : [18]
[1]: t1 = t0.Length; [2]
[2]: t1 == 3 ? [3] : [12]
Expand All @@ -2380,7 +2360,7 @@ static void Main()
[16]: t5 == 3 ? [17] : [18]
[17]: leaf `case [1, .., 3]:`
[18]: leaf `default`
", boundSwitch.DecisionDag.Dump());
");
}

[Fact]
Expand Down Expand Up @@ -2415,12 +2395,7 @@ static void Main()
comp.VerifyEmitDiagnostics();
CompileAndVerify(comp, expectedOutput: expectedOutput);

var tree = comp.SyntaxTrees.First();
var @switch = tree.GetRoot().DescendantNodes().OfType<SwitchStatementSyntax>().Single();
var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
var binder = model.GetEnclosingBinder(@switch.SpanStart);
var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded);
AssertEx.Equal(
VerifyDecisionDagDump<SwitchStatementSyntax>(comp,
@"[0]: t0 != null ? [1] : [12]
[1]: t1 = t0.Length; [2]
[2]: t1 == 1 ? [3] : [8]
Expand All @@ -2434,7 +2409,7 @@ static void Main()
[10]: t3 == 42 ? [11] : [12]
[11]: leaf `case [..,42]:`
[12]: leaf `default`
", boundSwitch.DecisionDag.Dump());
");
}

[Fact]
Expand Down Expand Up @@ -2494,12 +2469,7 @@ void Test(object[] a)
// case [var unreachable]:
Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "[var unreachable]").WithLocation(17, 18));

var tree = comp.SyntaxTrees.First();
var @switch = tree.GetRoot().DescendantNodes().OfType<SwitchStatementSyntax>().First();
var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
var binder = model.GetEnclosingBinder(@switch.SpanStart);
var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded);
AssertEx.Equal(
VerifyDecisionDagDump<SwitchStatementSyntax>(comp,
@"[0]: t0 != null ? [1] : [10]
[1]: t1 = t0.Length; [2]
[2]: t1 >= 1 ? [3] : [10]
Expand All @@ -2518,7 +2488,7 @@ void Test(object[] a)
break;
}`
[11]: leaf `case [.., not null]:`
", boundSwitch.DecisionDag.Dump());
");
}

[Fact]
Expand Down Expand Up @@ -2549,12 +2519,7 @@ public static void Test(int[] a)
comp.VerifyEmitDiagnostics();
CompileAndVerify(comp, expectedOutput: "2");

var tree = comp.SyntaxTrees.First();
var @switch = tree.GetRoot().DescendantNodes().OfType<SwitchStatementSyntax>().Single();
var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
var binder = model.GetEnclosingBinder(@switch.SpanStart);
var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded);
AssertEx.Equal(
VerifyDecisionDagDump<SwitchStatementSyntax>(comp,
@"[0]: t0 != null ? [1] : [11]
[1]: t1 = t0.Length; [2]
[2]: t1 >= 2 ? [3] : [11]
Expand All @@ -2567,7 +2532,7 @@ public static void Test(int[] a)
[9]: t3 <= 0 ? [10] : [11]
[10]: leaf `case [.., <= 0, _]:`
[11]: leaf `default`
", boundSwitch.DecisionDag.Dump());
");
}

[Fact]
Expand Down Expand Up @@ -2672,12 +2637,8 @@ void Test(int[] a)
// [_] => 2, // unreachable
Diagnostic(ErrorCode.ERR_SwitchArmSubsumed, "[_]").WithLocation(11, 13));

var tree = comp.SyntaxTrees.First();
var @switch = tree.GetRoot().DescendantNodes().OfType<SwitchExpressionSyntax>().First();
var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
var binder = model.GetEnclosingBinder(@switch.SpanStart);
var boundSwitch = (BoundSwitchExpression)binder.BindExpression(@switch, BindingDiagnosticBag.Discarded);
AssertEx.Equal(
VerifyDecisionDagDump<SwitchExpressionSyntax>(comp,

@"[0]: t0 != null ? [1] : [13]
[1]: t1 = t0.Length; [2]
[2]: t1 == 1 ? [3] : [12]
Expand All @@ -2698,7 +2659,7 @@ void Test(int[] a)
[..[>= 0]] or [..null] => 1,
[_] => 2, // unreachable
}`
", boundSwitch.DecisionDag.Dump());
");
}

[Fact]
Expand All @@ -2725,12 +2686,7 @@ void Test(int[] a)
// [var unreachable] => 5,
Diagnostic(ErrorCode.ERR_SwitchArmSubsumed, "[var unreachable]").WithLocation(12, 13));

var tree = comp.SyntaxTrees.First();
var @switch = tree.GetRoot().DescendantNodes().OfType<SwitchExpressionSyntax>().Single();
var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
var binder = model.GetEnclosingBinder(@switch.SpanStart);
var boundSwitch = (BoundSwitchExpression)binder.BindExpression(@switch, BindingDiagnosticBag.Discarded);
AssertEx.Equal(
VerifyDecisionDagDump<SwitchExpressionSyntax>(comp,
@"[0]: t0 != null ? [1] : [15]
[1]: t1 = t0.Length; [2]
[2]: t1 >= 1 ? [3] : [14]
Expand All @@ -2754,7 +2710,7 @@ void Test(int[] a)
{ Length: not 1 } => 4,
[var unreachable] => 5,
}`
", boundSwitch.DecisionDag.Dump());
");
}

[Fact]
Expand Down Expand Up @@ -2807,12 +2763,7 @@ void Test(int[][] a)
// case [[42]]:
Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "[[42]]").WithLocation(9, 18));

var tree = comp.SyntaxTrees.First();
var @switch = tree.GetRoot().DescendantNodes().OfType<SwitchStatementSyntax>().Single();
var model = (CSharpSemanticModel)comp.GetSemanticModel(tree);
var binder = model.GetEnclosingBinder(@switch.SpanStart);
var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded);
AssertEx.Equal(
VerifyDecisionDagDump<SwitchStatementSyntax>(comp,
@"[0]: t0 != null ? [1] : [27]
[1]: t1 = t0.Length; [2]
[2]: t1 >= 1 ? [3] : [27]
Expand Down Expand Up @@ -2846,7 +2797,7 @@ void Test(int[][] a)
case [[42]]:
break;
}`
", boundSwitch.DecisionDag.Dump());
");
}

[Fact]
Expand Down

0 comments on commit 7c13399

Please sign in to comment.