Example #1
0
func ExampleRead() {
	g, err := dot.Read([]byte(`digraph G {Hello->World}`))
	if err != nil {
		panic(err)
	}
	s := g.String()
	fmt.Println(s)
}
Example #2
0
// restructure attempts to recover the control flow primitives of a given
// control flow graph. It does so by repeatedly locating and merging structured
// subgraphs (graph representations of control flow primitives) into single
// nodes until the entire graph is reduced into a single node or no structured
// subgraphs may be located. The list of primitives is ordered in the same
// sequence as they were located.
func restructure(dotPath string, subs []*graphs.SubGraph) (prims []*primitive.Primitive, err error) {
	// Parse the unstructured CFG.
	var graph *dot.Graph
	switch dotPath {
	case "-":
		// Read from stdin.
		buf, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			return nil, errutil.Err(err)
		}
		graph, err = dot.Read(buf)
		if err != nil {
			return nil, errutil.Err(err)
		}
	default:
		// Read from FILE.
		graph, err = dot.ParseFile(dotPath)
		if err != nil {
			return nil, errutil.Err(err)
		}
	}
	if len(graph.Nodes.Nodes) == 0 {
		return nil, errutil.Newf("unable to restructure empty graph %q", dotPath)
	}

	// Locate control flow primitives.
	for step := 1; len(graph.Nodes.Nodes) > 1; step++ {
		prim, err := findPrim(graph, subs, step)
		if err != nil {
			return nil, errutil.Err(err)
		}
		prims = append(prims, prim)
	}

	return prims, nil
}