|
| 1 | +package callgraphutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/csv" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "golang.org/x/tools/go/callgraph" |
| 11 | +) |
| 12 | + |
| 13 | +// WriteCSV writes the given callgraph.Graph to the given io.Writer in CSV |
| 14 | +// format. This format can be used to generate a visual representation of the |
| 15 | +// call graph using many different tools. |
| 16 | +func WriteCSV(w io.Writer, g *callgraph.Graph) error { |
| 17 | + cw := csv.NewWriter(w) |
| 18 | + cw.Comma = ',' |
| 19 | + defer cw.Flush() |
| 20 | + |
| 21 | + // Write header. |
| 22 | + if err := cw.Write([]string{ |
| 23 | + "source_pkg", |
| 24 | + "source_pkg_go_version", |
| 25 | + "source_pkg_origin", |
| 26 | + "source_func", |
| 27 | + "source_func_name", |
| 28 | + "source_func_signature", |
| 29 | + "target_pkg", |
| 30 | + "target_pkg_go_version", |
| 31 | + "target_pkg_origin", |
| 32 | + "target_func", |
| 33 | + "target_func_name", |
| 34 | + "target_func_signature", |
| 35 | + }); err != nil { |
| 36 | + return fmt.Errorf("failed to write header: %w", err) |
| 37 | + } |
| 38 | + |
| 39 | + // Write edges. |
| 40 | + for _, n := range g.Nodes { |
| 41 | + source, err := getNodeInfo(n) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("failed to get node info: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + for _, e := range n.Out { |
| 47 | + target, err := getNodeInfo(e.Callee) |
| 48 | + if err != nil { |
| 49 | + return fmt.Errorf("failed to get node info: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + record := []string{} |
| 53 | + record = append(record, source.CSV()...) |
| 54 | + record = append(record, target.CSV()...) |
| 55 | + |
| 56 | + // Write edge. |
| 57 | + if err := cw.Write(record); err != nil { |
| 58 | + return fmt.Errorf("failed to write edge: %w", err) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +// nodeInfo is a struct that contains information about a callgraph.Node used |
| 67 | +// to generate CSV output. |
| 68 | +type nodeInfo struct { |
| 69 | + pkgPath string |
| 70 | + pkgGoVersion string |
| 71 | + pkgOrigin string |
| 72 | + pkgFunc string |
| 73 | + pkgFuncName string |
| 74 | + pkgFuncSignature string |
| 75 | +} |
| 76 | + |
| 77 | +// CSV returns single record for the node. |
| 78 | +func (n *nodeInfo) CSV() []string { |
| 79 | + return []string{ |
| 80 | + n.pkgPath, |
| 81 | + n.pkgGoVersion, |
| 82 | + n.pkgOrigin, |
| 83 | + n.pkgFunc, |
| 84 | + n.pkgFuncName, |
| 85 | + n.pkgFuncSignature, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// getNodeInfo returns a nodeInfo struct for the given callgraph.Node. |
| 90 | +func getNodeInfo(n *callgraph.Node) (*nodeInfo, error) { |
| 91 | + info := &nodeInfo{ |
| 92 | + pkgPath: "unknown", |
| 93 | + pkgGoVersion: runtime.Version(), |
| 94 | + pkgOrigin: "unknown", |
| 95 | + pkgFunc: n.Func.String(), |
| 96 | + pkgFuncName: n.Func.Name(), |
| 97 | + pkgFuncSignature: n.Func.Signature.String(), |
| 98 | + } |
| 99 | + |
| 100 | + if n.Func.Pkg != nil { |
| 101 | + info.pkgPath = n.Func.Pkg.Pkg.Path() |
| 102 | + |
| 103 | + if goVersion := n.Func.Pkg.Pkg.GoVersion(); goVersion != "" { |
| 104 | + info.pkgGoVersion = goVersion |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if strings.Contains(info.pkgPath, ".") { |
| 109 | + info.pkgOrigin = strings.Split(info.pkgPath, "/")[0] |
| 110 | + } else { |
| 111 | + // If the package path doesn't contain a dot, then it's |
| 112 | + // probably a standard library package? This is a pattern |
| 113 | + // I've used and seen elsewhere. |
| 114 | + info.pkgOrigin = "stdlib" |
| 115 | + } |
| 116 | + |
| 117 | + return info, nil |
| 118 | +} |
0 commit comments