Tools or pointers for control flow analysis of OCaml code?


I’m working on a PPX rewriter that needs to reason about possible return values of a function. Roughly, I’d like to have my PPX generate a control flow graph from the parsetree representation of a function, then traverse the flowpaths of the CFG to gather information about how that function’s return values are constructed.

OCaml seems to be widely used to do static analysis of other languages, but as yet I have not found a tool for generating a control flow graph for OCaml code. Has anyone tried to implement something like this, or know of any tools to do so? Any ideas about where to start implementing this from scratch? Is there maybe an intermediate representation to which I could convert an OCaml parsetree, for which generating a CFG is already implemented somewhere?

The compiler does some kind of control flow analysis, but I think it happens much deeper than the parsetree generation phase, and I’d like to avoid relying on compiler internals that could break with a new compiler release.

1 Like