File: dot.go

package info (click to toggle)
golang-github-traefik-yaegi 0.16.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 24,608 kB
  • sloc: sh: 457; makefile: 39
file content (94 lines) | stat: -rw-r--r-- 2,326 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package interp

import (
	"fmt"
	"io"
	"log"
	"os/exec"
	"path/filepath"
	"strings"
)

// astDot displays an AST in graphviz dot(1) format using dotty(1) co-process.
func (n *node) astDot(out io.Writer, name string) {
	fmt.Fprintf(out, "digraph ast {\n")
	fmt.Fprintf(out, "labelloc=\"t\"\n")
	fmt.Fprintf(out, "label=\"%s\"\n", name)
	n.Walk(func(n *node) bool {
		var label string
		switch n.kind {
		case basicLit, identExpr:
			label = strings.ReplaceAll(n.ident, "\"", "\\\"")
		default:
			if n.action != aNop {
				label = n.action.String()
			} else {
				label = n.kind.String()
			}
		}
		fmt.Fprintf(out, "%d [label=\"%d: %s\"]\n", n.index, n.index, label)
		if n.anc != nil {
			fmt.Fprintf(out, "%d -> %d\n", n.anc.index, n.index)
		}
		return true
	}, nil)
	fmt.Fprintf(out, "}\n")
}

// cfgDot displays a CFG in graphviz dot(1) format using dotty(1) co-process.
func (n *node) cfgDot(out io.Writer) {
	fmt.Fprintf(out, "digraph cfg {\n")
	n.Walk(nil, func(n *node) {
		if n.kind == basicLit || n.tnext == nil {
			return
		}
		var label string
		if n.action == aNop {
			label = "nop: end_" + n.kind.String()
		} else {
			label = n.action.String()
		}
		fmt.Fprintf(out, "%d [label=\"%d: %v %d\"]\n", n.index, n.index, label, n.findex)
		if n.fnext != nil {
			fmt.Fprintf(out, "%d -> %d [color=green]\n", n.index, n.tnext.index)
			fmt.Fprintf(out, "%d -> %d [color=red]\n", n.index, n.fnext.index)
		} else if n.tnext != nil {
			fmt.Fprintf(out, "%d -> %d\n", n.index, n.tnext.index)
		}
	})
	fmt.Fprintf(out, "}\n")
}

type nopCloser struct {
	io.Writer
}

func (nopCloser) Close() error { return nil }

// dotWriter returns an output stream to a dot(1) co-process where to write data in .dot format.
func dotWriter(dotCmd string) io.WriteCloser {
	if dotCmd == "" {
		return nopCloser{io.Discard}
	}
	fields := strings.Fields(dotCmd)
	cmd := exec.Command(fields[0], fields[1:]...)
	dotin, err := cmd.StdinPipe()
	if err != nil {
		log.Fatal(err)
	}
	if err = cmd.Start(); err != nil {
		log.Fatal(err)
	}
	return dotin
}

func defaultDotCmd(filePath, prefix string) string {
	dir, fileName := filepath.Split(filePath)
	ext := filepath.Ext(fileName)
	if ext == "" {
		fileName += ".dot"
	} else {
		fileName = strings.Replace(fileName, ext, ".dot", 1)
	}
	return "dot -Tdot -o" + dir + prefix + fileName
}