This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tree = CSharpSyntaxTree.ParseText(@" | |
class C | |
{ | |
void M(int x) | |
{ | |
return; | |
if(x == 0) //-+ Start is unreachable | |
System.Console.WriteLine(""Hello""); // | | |
L1: //-+ End is unreachable | |
} | |
} | |
"); | |
var Mscorlib = PortableExecutableReference.CreateFromAssembly(typeof(object).Assembly); | |
var compilation = CSharpCompilation.Create("MyCompilation", | |
syntaxTrees: new[] { tree }, references: new[] { Mscorlib }); | |
var model = compilation.GetSemanticModel(tree); | |
//Choose first and last statements | |
var firstIf = tree.GetRoot().DescendantNodes().OfType<IfStatementSyntax>().Single(); | |
var label1 = tree.GetRoot().DescendantNodes().OfType<LabeledStatementSyntax>().Single(); | |
ControlFlowAnalysis result = model.AnalyzeControlFlow(firstIf, label1); | |
Console.WriteLine(result.StartPointIsReachable); //False | |
Console.WriteLine(result.EndPointIsReachable); //False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment