roslynroslyn-code-analysiscontrol-flow-graphmicrosoft.codeanalysis

Roslyn: Reconstruct code from BasicBlocks in a CFG


In Roslyn, a control flow graph has the list of BasicBlocks which constitute the program. Is it possible to get code statements from each block to reconstruct the entire code?


Solution

  • This is not possible using Roslyn's in-built methods in my knowledge. I achieved a similar result by constructing a dictionary of blocks and sets of line numbers corresponding to those blocks using string matching. Additional filtering done to the same in order to improve accuracy in case of blocks that include locks etc. The result is not 100 percent accurate, but it comes pretty close in most cases.