Class CFGBuilder

java.lang.Object
org.checkerframework.dataflow.cfg.builder.CFGBuilder
Direct Known Subclasses:
CFCFGBuilder

public abstract class CFGBuilder extends Object
Builds the control flow graph of some Java code (either a method, or an arbitrary statement).

The translation of the AST to the CFG is split into three phases:

  1. Phase one. In the first phase, the AST is translated into a sequence of ExtendedNodes. An extended node can either be a Node, or one of several meta elements such as a conditional or unconditional jump or a node with additional information about exceptions. Some of the extended nodes contain labels (e.g., for the jump target), and phase one additionally creates a mapping from labels to extended nodes. Finally, the list of leaders is computed: A leader is an extended node which will give rise to a basic block in phase two.
  2. Phase two. In this phase, the sequence of extended nodes is translated to a graph of control flow blocks that contain nodes. The meta elements from phase one are translated into the correct edges.
  3. Phase three. The control flow graph generated in phase two can contain degenerate basic blocks such as empty regular basic blocks or conditional basic blocks that have the same block as both 'then' and 'else' successor. This phase removes these cases while preserving the control flow structure.
  • Constructor Details

    • CFGBuilder

      protected CFGBuilder()
      This class should never be instantiated. Protected to still allow subclasses.
  • Method Details

    • build

      public static ControlFlowGraph build(CompilationUnitTree root, UnderlyingAST underlyingAST, boolean assumeAssertionsEnabled, boolean assumeAssertionsDisabled, ProcessingEnvironment env)
      Build the control flow graph of some code.
      Parameters:
      root - the compilation unit
      underlyingAST - the AST that underlies the control frow graph
      assumeAssertionsDisabled - can assertions be assumed to be disabled?
      assumeAssertionsEnabled - can assertions be assumed to be enabled?
      env - annotation processing environment containing type utilities
      Returns:
      a control flow graph
    • build

      public static ControlFlowGraph build(TreePath bodyPath, UnderlyingAST underlyingAST, boolean assumeAssertionsEnabled, boolean assumeAssertionsDisabled, ProcessingEnvironment env)
      Build the control flow graph of some code (method, initializer block, ...). bodyPath is the TreePath to the body of that code.
    • build

      public static ControlFlowGraph build(CompilationUnitTree root, UnderlyingAST underlyingAST, ProcessingEnvironment env)
      Build the control flow graph of some code.
    • build

      public static ControlFlowGraph build(CompilationUnitTree root, MethodTree tree, ClassTree classTree, ProcessingEnvironment env)
      Build the control flow graph of a method.
    • extendedNodeCollectionToStringDebug

      public static String extendedNodeCollectionToStringDebug(Collection<? extends ExtendedNode> nodes)
      Return a printed representation of a collection of extended nodes.
      Parameters:
      nodes - a collection of extended nodes to format
      Returns:
      a printed representation of the given collection
    • printBlocks

      protected static void printBlocks(Set<Block> blocks)
      Print a set of Blocks and the edges between them. This is useful for examining the results of phase two.
      Parameters:
      blocks - the blocks to print