Пример #1
0
  def MaybeAddSingleExitBlock(self, g: nx.MultiDiGraph) -> None:
    """Add a magic exit block to unite the exit statements of a CFG.

    Args:
      g: The graph to add the exit block to.
    """
    exit_blocks = list(nx_utils.ExitBlockIterator(g))
    if not exit_blocks:
      raise ValueError("No exit blocks found in graph!")

    if (
      self.only_add_entry_and_exit_blocks_if_required and len(exit_blocks) == 1
    ):
      exit_block = exit_blocks[0][0]
    else:
      exit_block = f"{g.name}_exit"
      g.add_node(
        exit_block, name=exit_block, type="magic", x=self.dictionary["!MAGIC"]
      )
      # Connect exit blocks.
      for node, data in exit_blocks:
        g.add_edge(node, exit_block, flow="control")
      # Add a dataflow edge out, if there is one.
      for src, dst, data in nx_utils.DataFlowEdgeIterator(g):
        if dst == node:
          g.add_edge(node, exit_block, flow="data")
          break
    g.exit_block = exit_block
Пример #2
0
    def MaybeAddSingleExitBlock(self, g: nx.MultiDiGraph) -> None:
        """Add a magic exit block to unite the exit statements of a CFG.

    Args:
      g: The graph to add the exit block to.
    """
        exit_blocks = list(nx_utils.ExitBlockIterator(g))
        if not exit_blocks:
            raise ValueError("No exit blocks found in graph!")

        if len(exit_blocks) == 1:
            exit_block = exit_blocks[0][0]
        else:
            exit_block = f"{g.name}_exit"
            g.add_node(
                exit_block,
                name=exit_block,
                type=programl_pb2.Node.STATEMENT,
                text="",
                preprocessed_text="",
                function=None,
                x=[self.node_encoder.dictionary["!MAGIC"]],
                y=[],
            )
            # Connect exit blocks.
            for node, data in exit_blocks:
                g.add_edge(node,
                           exit_block,
                           flow=programl_pb2.Edge.CONTROL,
                           position=0)
            # Add a dataflow edge out, if there is one.
            for src, dst, data in nx_utils.DataFlowEdgeIterator(g):
                if dst == node:
                    g.add_edge(node,
                               exit_block,
                               flow=programl_pb2.Edge.DATA,
                               position=0)
                    break
        g.graph["exit_block"] = exit_block
Пример #3
0
def test_DataFlowEdgeIterator(graph):
    assert len(list(nx_utils.DataFlowEdgeIterator(graph))) == 2