Skip to content

Instantly share code, notes, and snippets.

@JoshVarty
Created March 24, 2015 19:48
Show Gist options
  • Save JoshVarty/d641a4d18606ce1233d1 to your computer and use it in GitHub Desktop.
Save JoshVarty/d641a4d18606ce1233d1 to your computer and use it in GitHub Desktop.
var tree = CSharpSyntaxTree.ParseText(@"
class C
{
void M()
{
for (int i = 0; i < 10; i++)
{
if (i == 3)
continue;
if (i == 8)
break;
}
}
}
");
var Mscorlib = PortableExecutableReference.CreateFromAssembly(typeof(object).Assembly);
var compilation = CSharpCompilation.Create("MyCompilation",
syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
var model = compilation.GetSemanticModel(tree);
var firstFor = tree.GetRoot().DescendantNodes().OfType<ForStatementSyntax>().Single();
ControlFlowAnalysis result = model.AnalyzeControlFlow(firstFor.Statement);
Console.WriteLine(result.Succeeded); //True
Console.WriteLine(result.ExitPoints.Count()); //2 - continue, and break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment