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 <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Tim King <taking@google.com>
diff --git a/go/cfg/cfg.go b/go/cfg/cfg.go
index 0166835..fad4530 100644
--- a/go/cfg/cfg.go
+++ b/go/cfg/cfg.go
@@ -211,9 +211,13 @@
return buf.String()
}
-// digraph emits AT&T GraphViz (dot) syntax for the CFG.
-// TODO(adonovan): publish; needs a proposal.
-func (g *CFG) digraph(fset *token.FileSet) string {
+// Dot returns the control-flow graph in the [Dot graph description language].
+// Use a command such as 'dot -Tsvg' to render it in a form viewable in a browser.
+// This method is provided as a debugging aid; the details of the
+// output are unspecified and may change.
+//
+// [Dot graph description language]: https://en.wikipedia.org/wiki/DOT_(graph_description_language)
+func (g *CFG) Dot(fset *token.FileSet) string {
var buf bytes.Buffer
buf.WriteString("digraph CFG {\n")
buf.WriteString(" node [shape=box];\n")
@@ -235,11 +239,6 @@
return buf.String()
}
-// exposed to main.go
-func digraph(g *CFG, fset *token.FileSet) string {
- return g.digraph(fset)
-}
-
func formatNode(fset *token.FileSet, n ast.Node) string {
var buf bytes.Buffer
format.Node(&buf, fset, n)
diff --git a/go/cfg/main.go b/go/cfg/main.go
index e25b368..3f1b361 100644
--- a/go/cfg/main.go
+++ b/go/cfg/main.go
@@ -19,10 +19,8 @@
"flag"
"fmt"
"go/ast"
- "go/token"
"log"
"os"
- _ "unsafe" // for linkname
"golang.org/x/tools/go/cfg"
"golang.org/x/tools/go/packages"
@@ -47,7 +45,7 @@
if decl, ok := decl.(*ast.FuncDecl); ok {
if decl.Name.Name == funcname {
g := cfg.New(decl.Body, mayReturn)
- fmt.Println(digraph(g, pkg.Fset))
+ fmt.Println(g.Dot(pkg.Fset))
os.Exit(0)
}
}
@@ -67,6 +65,3 @@
}
return true
}
-
-//go:linkname digraph golang.org/x/tools/go/cfg.digraph
-func digraph(g *cfg.CFG, fset *token.FileSet) string