Skip to content

Commit 29d17a0

Browse files
adonovangopherbot
authored andcommitted
go/cfg: publish (*CFG).Dot method
The new method formats the graph in the syntax used by the 'dot' command from https://graphviz.org/. Fixes golang/go#65754 Change-Id: Icf696419070e19b2f2236ac1e4aaa09323c565de Reviewed-on: https://go-review.googlesource.com/c/tools/+/572016 Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Tim King <[email protected]>
1 parent 03f7b7b commit 29d17a0

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

go/cfg/cfg.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,13 @@ func (g *CFG) Format(fset *token.FileSet) string {
211211
return buf.String()
212212
}
213213

214-
// digraph emits AT&T GraphViz (dot) syntax for the CFG.
215-
// TODO(adonovan): publish; needs a proposal.
216-
func (g *CFG) digraph(fset *token.FileSet) string {
214+
// Dot returns the control-flow graph in the [Dot graph description language].
215+
// Use a command such as 'dot -Tsvg' to render it in a form viewable in a browser.
216+
// This method is provided as a debugging aid; the details of the
217+
// output are unspecified and may change.
218+
//
219+
// [Dot graph description language]: ​​https://en.wikipedia.org/wiki/DOT_(graph_description_language)
220+
func (g *CFG) Dot(fset *token.FileSet) string {
217221
var buf bytes.Buffer
218222
buf.WriteString("digraph CFG {\n")
219223
buf.WriteString(" node [shape=box];\n")
@@ -235,11 +239,6 @@ func (g *CFG) digraph(fset *token.FileSet) string {
235239
return buf.String()
236240
}
237241

238-
// exposed to main.go
239-
func digraph(g *CFG, fset *token.FileSet) string {
240-
return g.digraph(fset)
241-
}
242-
243242
func formatNode(fset *token.FileSet, n ast.Node) string {
244243
var buf bytes.Buffer
245244
format.Node(&buf, fset, n)

go/cfg/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ import (
1919
"flag"
2020
"fmt"
2121
"go/ast"
22-
"go/token"
2322
"log"
2423
"os"
25-
_ "unsafe" // for linkname
2624

2725
"golang.org/x/tools/go/cfg"
2826
"golang.org/x/tools/go/packages"
@@ -47,7 +45,7 @@ func main() {
4745
if decl, ok := decl.(*ast.FuncDecl); ok {
4846
if decl.Name.Name == funcname {
4947
g := cfg.New(decl.Body, mayReturn)
50-
fmt.Println(digraph(g, pkg.Fset))
48+
fmt.Println(g.Dot(pkg.Fset))
5149
os.Exit(0)
5250
}
5351
}
@@ -67,6 +65,3 @@ func mayReturn(call *ast.CallExpr) bool {
6765
}
6866
return true
6967
}
70-
71-
//go:linkname digraph golang.org/x/tools/go/cfg.digraph
72-
func digraph(g *cfg.CFG, fset *token.FileSet) string

0 commit comments

Comments
 (0)