Tuesday, August 31, 2004

This post presents a new Reflector Addin that creates and diplays the graph of statement inside methods: the StatementGraph. This addin is an evolution of the IL graph. (this addin is not yet available for download).

 The statement graph is built from the Reflector CodeModel where each IStatement instance is a vertex and edges are added accordingly to the code "flow" (creating the edges is the most involved task). The rest of the job is handled by the QuickGraph library. When it makes sense, the vertices are clickable and you can jump to the invoked method, etc... The next step will be to update the Automatic Unit Test generator with the StatementGraph...

Let's see some simple methods and their corresponding graphs:

Simple

  • Original code:
    public void Simple()
    {
        Console.WriteLine("hello");
    }
    
  • Decompiled:
    public void Simple()
    {
          Console.WriteLine("hello");
    }
  • Graph:

Two statements in sequence

  • Original code:
    public void Body()
    {
        Console.WriteLine("hello");
        Console.WriteLine("world");
    }
    
  • Decompiled:
    public void Body()
    {
          Console.WriteLine("hello");
          Console.WriteLine("world");
    }
  • Graph:

If - Then - Else

  • Original code:
    public void If(int value)
    {
        if (value<0)
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
  • Decompiled:
    public void If(int value)
    {
          if (value < 0)
          {
                Console.WriteLine("true");
                return;
          }
          Console.WriteLine("false");
    }
    
  • Graph:

While

  • Original code:
    public void While()
    {
        int i = 0;
        while(i<10)
        {
            Console.Write(i++);
        }
    }
  • Decompiled:
    public void While()
    {
          int num1 = 0;
          while ((num1 < 10))
          {
                Console.Write(num1++);
          }
    }
  • Graph:

While with break and continue

  • Original code:
    public void WhileBreakContinue()
    {
        int i = 0;
        while (i < 10)
        {
            if (i == 5)
                continue;
            if (i == 7)
                break;
            Console.Write(i++);
        }
        Console.WriteLine("Finished");
    }
  • Decompiled:
    public void WhileBreakContinue()
    {
          int num1 = 0;
          while ((num1 < 10))
          {
                if (num1 == 5)
                {
                      continue;
                }
                if (num1 == 7)
                {
                      break;
                }
                Console.Write(num1++);
          }
          Console.WriteLine("Finished");
    }
    
    
    
  • Graph:

Try - Catch

  • Original code:
    public void TryCatch()
    {
        try
        {
            Console.WriteLine("hello");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Boom: {0}",ex);
        }
    }
    
  • Decompiled:
    public void TryCatch()
    {
          try
          {
                Console.WriteLine("hello");
          }
          catch (Exception exception1)
          {
                Console.WriteLine("Boom: {0}", exception1);
          }
    }
  • Graph:

posted on Wednesday, September 01, 2004 6:40:00 AM UTC  #    Comments [18]
Tracked by:
"remington 870 stock" (online) [Trackback]
"orange peel extract" (online) [Trackback]
"dan updike" (online) [Trackback]
"buspar weight gain" (online) [Trackback]
Monday, June 06, 2005 5:36:02 PM UTC
you've been HAACKED
Monday, June 06, 2005 5:36:02 PM UTC
Awesome!!! 8-)
Erin
Monday, June 06, 2005 5:36:03 PM UTC
Pictures are ok, but there is big room for improvement.
<br>
<br>1) All the block boxes '{...}' in exaples are redundant!
<br>
<br>2) The state lines are not labeled properly.
<br>Labels are centered between two lines.
<br>I would consider higligting the semantic relationship by proximity.
<br>
<br>3) Why are the break and continue both the box and a line?
<br>They are language constucts to express unconditional transfer of control same as jump or GOTO, show them as such.
<br>
<br>4) Reinventing a wheel again ?
<br>Flow charts were here for some time.
<br>
<br>5) Branching on dimond shape may be more intuitive.
<br>
<br>Start and end may be also nice. This can be substitute for the block box.
<br>
<br>(As the flow chart and state diagram are dual, stay consistent.)
JR
Monday, June 06, 2005 5:36:03 PM UTC
Fantastic! Once it's integrated into automatic test generation will be awsome.
<br>
<br>One additional report I can think off would be the paths through a method that the tests did not cover. That would be just amazing!
<br>
<br>
Mark B
Monday, June 06, 2005 5:36:03 PM UTC
Hi JR,
<br>
<br>1) False, '{...}' represent IBlockStatement in the <a title="Reflector" href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a> CodeModel. Since, all vertex represent a IStatement, IBlockStatement has his place in the graph,
<br>
<br>2)&gt;Labels are centered between two lines
<br>Go tell the people from AT&amp;T to rewrite their graphviz library.
<br>
<br>3) They should be a box only, but I have added a &quot;hint&quot; on the edge saying &quot;this edge comes from a break statement&quot;. In the <a title="Reflector" href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a> CodeModel a break is a IBreakStatement and a continue is a IContinueStatement. Hence, they are represented by vertices.
<br>
<br>4)Show me where you can find this in .Net
<br>
<br>5)I don't control the layout of the vertices. Besides, method usually don't like diamons. As soon as they become complicated, their graph becomes rather big and complex. I wan't to keep the first block box because the next step is to output the full graph of a class.
<br>
<br>
Jonathan de Halleux
Monday, June 06, 2005 5:36:03 PM UTC
Yup, the next step is test generation integration.
<br>
<br>&gt; One additional report I can think off would be the paths through a method that the tests did not cover. That would be just amazing!
<br>
<br>Good idea, one could think about loading NCover result and display uncoved path in the code... and generate tests for those path
Jonathan de Halleux
Monday, June 06, 2005 5:36:04 PM UTC
<br>Very good!
<br>
<br>&gt; 4)Show me where you can find this in .Net
<br>
<br>Borland Together will reverse engineer C# code to produce sequence diagrams, although the resulting diagram can be a mess if the code is bad.
<br>
<br>I wonder if this has refactoring applications too? Visually it might be easier to see similar code behaviour.
<br>
Peter Johnston
Monday, June 06, 2005 5:36:04 PM UTC
My graph does not have refactoring since I'm using the <a title="Reflector" href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a> API, which is a decompiler.
<br>
<br>I'm sure the Visual Studio handling the compiler could be able to produce such graphs. Of course, one could image to select a few vertices and do &quot;Extract Method&quot;, or even better do &quot;automatic&quot; clustering on the graph to automatically refactor the code.
<br>
<br>
Jonathan de Halleux
Monday, June 06, 2005 5:36:04 PM UTC
Peli's Blog
Monday, June 06, 2005 5:36:05 PM UTC
Peli's Blog
Monday, June 06, 2005 5:36:05 PM UTC
There is a product called CodeLogic which is recently released. Check out <a target="_new" href="http://www.logicexplorers.com/CodeLogicOverview.html">http://www.logicexplorers.com/CodeLogicOverview.html</a> . Well, after that I moved to CodeLogic for understanding code logic (Although It works with sources and it is not a decompiler). But it looks like that I am going to shift again after you release this addons. Thanks for giving us such cool addons.
Nauman Leghari
Monday, June 06, 2005 5:36:05 PM UTC
Interresting product :) thanks for the link...
Jonathan de Halleux
Monday, June 06, 2005 5:36:06 PM UTC
Peli's Blog
Monday, June 06, 2005 5:36:06 PM UTC
Guys, when this addon is going to be released?
<br>Is there a download link to pre-view version?
Ihor Bobak
Monday, June 06, 2005 5:36:06 PM UTC
It is already released (a while ago). Go to www.dotnetwiki.org and look for <a title="Reflector" href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a>.Addins in the download page.
<br>
<br>Add <a title="Reflector" href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a>.Graph.dll as a addin to get the statement graph.
Jonathan de Halleux
Monday, June 06, 2005 5:36:06 PM UTC
Alessandro's Blogs
Monday, June 06, 2005 5:36:07 PM UTC
Di .NET e di altre amenita'
Monday, June 06, 2005 5:36:07 PM UTC
Rengul weblogs
Comments are closed.