Debian Codegraph @

internal/graph/graphfmt/dot.go

raw symbols SCIP
1 // Copyright 2026 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package graphfmt serializes graphs to external representations.
6 package graphfmtscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/
7
8 import (
9 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
10 "ioscip-go gomod github.com/golang/go/src go1.25.0 io/"
11 "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/"
12 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
13
14 "golang.org/x/tools/internal/graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/"
15 )
16
17 // Dot contains options for generating a Graphviz Dot graph from a
18 // [graph.Graph].
19 type Dotscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#[NodeID comparable] struct {
20 // Name is the name given to the graph. Usually this can be
21 // left blank.
22 Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#Name. string
23
24 // Label returns the string to use as a label for the given
25 // node. If nil, nodes are labeled with their node numbers.
26 Labelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#Label. func(node NodeID) string
27
28 // NodeAttrs, if non-nil, returns a set of attributes for a
29 // node. If this includes a "label" attribute, it overrides
30 // the label returned by Label.
31 NodeAttrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#NodeAttrs. func(node NodeID) []DotAttrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#
32
33 // EdgeAttrs, if non-nil, returns a set of attributes for an
34 // edge.
35 EdgeAttrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#EdgeAttrs. func(from, to NodeID) []DotAttrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#
36
37 // ClusterOf, if non-nil, returns the cluster of a node, or nil if the node
38 // should not be in a cluster. Multiple nodes with the same cluster will be
39 // arranged together and enclosed in a box.
40 ClusterOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#ClusterOf. func(node NodeID) *DotClusterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotCluster#
41 }
42
43 // DotAttr is an attribute for a Dot node or edge.
44 type DotAttrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr# struct {
45 Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#Name. string
46
47 // Val is the value of this attribute. It may be a string
48 // (which will be escaped), bool, int, uint, float64 or
49 // DotLiteral.
50 Valscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#Val. any
51 }
52
53 // DotLiteral is a string literal that should be passed to dot
54 // unescaped.
55 type DotLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotLiteral# string
56
57 // DotCluster represents a cluster of nodes arranged together.
58 type DotClusterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotCluster# struct {
59 Labelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotCluster#Label. string
60 Attrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotCluster#Attrs. []DotAttrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#
61 }
62
63 func defaultLabelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/defaultLabel().[NodeID comparable](node NodeID) string {
64 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(node)
65 }
66
67 // Sprint returns the Dot form of g as a string.
68 func (d Dotscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#[NodeID]) Sprintscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#Sprint().(g graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/.Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#[NodeID]) string {
69 w := new(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#)
70
71 nodeLabel := d.Labelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#Label.
72 if nodeLabel == nil {
73 nodeLabel = defaultLabelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/defaultLabel().
74 }
75
76 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "digraph %s {\n", dotStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/dotString().(d.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#Name.))
77
78 // Collect nodes by cluster.
79 type cluster struct {
80 c *DotClusterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotCluster#
81 nodes []NodeID
82 }
83 var clusters []cluster
84 clusterIDs := make(map[*DotClusterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotCluster#]int)
85 nodeNums := make(map[NodeID]int)
86 for nid := range g.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#Nodes.() {
87 var c *DotClusterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotCluster#
88 if d.ClusterOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#ClusterOf. != nil {
89 c = d.ClusterOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#ClusterOf.(nid)
90 }
91 id, ok := clusterIDs[c]
92 if !ok {
93 id = len(clusters)
94 clusterIDs[c] = id
95 clusters = append(clusters, cluster{c: c})
96 }
97 clusters[id].nodes = append(clusters[id].nodes, nid)
98 nodeNums[nid] = len(nodeNums)
99 }
100
101 // Emit each cluster.
102 for cid, c := range clusters {
103 if c.c != nil {
104 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "subgraph cluster_%d {\n", cid)
105 if attrs := formatAttrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/formatAttrs().(c.c.Attrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotCluster#Attrs., c.c.Labelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotCluster#Label.); attrs != "" {
106 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "graph %s;", attrs)
107 }
108 }
109
110 // Emit nodes. We don't emit edges yet because an edge may have a
111 // forward-reference, which could define the target node in the wrong
112 // cluster.
113 for _, nid := range c.nodes {
114 // Define node.
115 var attrList []DotAttrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#
116 var label string
117 if d.NodeAttrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#NodeAttrs. != nil {
118 attrList = d.NodeAttrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#NodeAttrs.(nid)
119 }
120 if nodeLabel != nil {
121 label = nodeLabel(nid)
122 }
123 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "n%d%s;\n", nodeNums[nid], formatAttrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/formatAttrs().(attrList, label))
124 }
125
126 if c.c != nil {
127 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "}\n")
128 }
129 }
130
131 // Emit edges.
132 for nid := range g.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#Nodes.() {
133 for succ := range g.Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#Out.(nid) {
134 var attrs string
135 if d.EdgeAttrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#EdgeAttrs. != nil {
136 attrs = formatAttrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/formatAttrs().(d.EdgeAttrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#EdgeAttrs.(nid, succ), "")
137 }
138 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "n%d -> n%d%s;\n", nodeNums[nid], nodeNums[succ], attrs)
139 }
140 }
141
142 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(w, "}\n")
143
144 return w.Stringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#String().()
145 }
146
147 // SVG attempts to render g to an SVG.
148 func (d Dotscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#[NodeID]) SVGscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#SVG().(w ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Writerscip-go gomod github.com/golang/go/src go1.25.0 io/Writer#, g graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/.Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#[NodeID]) error {
149 // Check that we can run Dot at all.
150 dot := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().("dot", "-V")
151 if err := dot.Runscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Run().(); err != nil {
152 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("cannot run dot: %s", err)
153 }
154
155 // Convert to SVG.
156 //
157 // TODO: Consider lifting nice graph viewer from go.dev/cl/192706
158 dot = execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().("dot", "-Tsvg", "-")
159 in, err := dot.StdinPipescip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#StdinPipe().()
160 if err != nil {
161 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("running dot: %s", err)
162 }
163 dot.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stdout. = w
164 if err := dot.Startscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Start().(); err != nil {
165 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("running dot: %s", err)
166 }
167 _, err = in.Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().([]byte(d.Sprintscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/Dot#Sprint().(g)))
168 in.Closescip-go gomod github.com/golang/go/src go1.25.0 io/Closer#Close().()
169 if err != nil {
170 // Let Dot exit, but ignore any errors from it.
171 dot.Waitscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Wait().()
172 return err
173 }
174 if err := dot.Waitscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Wait().(); err != nil {
175 if err, ok := err.(*execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.ExitErrorscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/ExitError#); ok && len(err.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/ExitError#Stderr.) > 0 {
176 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("running dot: %s\n%s", err, err.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/ExitError#Stderr.)
177 }
178 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("running dot: %s", err)
179 }
180 return nil
181 }
182
183 var dotStringerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/dotStringer. = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.NewReplacerscip-go gomod github.com/golang/go/src go1.25.0 strings/NewReplacer().(
184 "\n", `\n`,
185 `\`, `\\`,
186 `"`, `\"`,
187 `{`, `\{`,
188 `}`, `\}`,
189 `<`, `\<`,
190 `>`, `\>`,
191 `|`, `\|`,
192 )
193
194 // dotString returns s as a quoted dot string.
195 func dotStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/dotString().(s string) string {
196 var buf stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#
197 buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteByte().('"')
198 dotStringerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/dotStringer..WriteStringscip-go gomod github.com/golang/go/src go1.25.0 strings/Replacer#WriteString().(&buf, s)
199 buf.WriteBytescip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteByte().('"')
200 return buf.Stringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#String().()
201 }
202
203 // formatAttrs formats attrs as a dot attribute set, including the surrounding
204 // brackets. If "label" is not present in attrs and the label argument is not
205 // "", it adds the given label attribute. If attrs is empty and label is "", it
206 // returns an empty string.
207 func formatAttrsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/formatAttrs().(attrs []DotAttrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#, label string) string {
208 if len(attrs) == 0 && label == "" {
209 return ""
210 }
211 var buf stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#
212 buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteString().(" [")
213 haveLabel := false
214 for i, attr := range attrs {
215 if i > 0 {
216 buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteString().(",")
217 }
218 formatAttrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/formatAttr().(&buf, attr)
219 if attr.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#Name. == "label" {
220 haveLabel = true
221 }
222 }
223 if !haveLabel && label != "" {
224 if len(attrs) > 0 {
225 buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteString().(",")
226 }
227 formatAttrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/formatAttr().(&buf, DotAttrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#{"label", label})
228 }
229 buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteString().("]")
230 return buf.Stringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#String().()
231 }
232
233 func formatAttrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/formatAttr().(buf *stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#, attr DotAttrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#) {
234 buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteString().(attr.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#Name.)
235 buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteString().("=")
236 switch val := attr.Valscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#Val..(type) {
237 case string:
238 buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteString().(dotStringscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/dotString().(val))
239 case bool, int, uint, float64:
240 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(buf, "%v", val)
241 case DotLiteralscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotLiteral#:
242 buf.WriteStringscip-go gomod github.com/golang/go/src go1.25.0 strings/Builder#WriteString().(string(val))
243 default:
244 panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("dot attribute %s had unknown type %T", attr.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#Name., attr.Valscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph/graphfmt`/DotAttr#Val.))
245 }
246 }