| 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 excfg |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "fmt" |
| 10 | "go/ast" |
| 11 | "go/format" |
| 12 | "strings" |
| 13 | |
| 14 | "golang.org/x/tools/go/ast/astutil" |
| 15 | internalastutil "golang.org/x/tools/internal/astutil" |
| 16 | ) |
| 17 | |
| 18 | // String formats the control-flow graph for ease of debugging. |
| 19 | func (cfg *CFG) String() string { |
| 20 | var buf strings.Builder |
| 21 | for _, b := range cfg.Blocks { |
| 22 | fmt.Fprintf(&buf, "%s: %s, CFG block .%d\n", b, b.Kind, b.CFGBlock) |
| 23 | fmt.Fprintf(&buf, "\t%s\n", formatBlockNode(cfg, b)) |
| 24 | if len(b.Succs) > 0 { |
| 25 | fmt.Fprintf(&buf, "\tsuccs:") |
| 26 | for _, succ := range b.Succs { |
| 27 | fmt.Fprintf(&buf, " %s", succ) |
| 28 | } |
| 29 | buf.WriteByte('\n') |
| 30 | } |
| 31 | buf.WriteByte('\n') |
| 32 | } |
| 33 | return buf.String() |
| 34 | } |
| 35 | |
| 36 | func (b *Block) String() string { |
| 37 | return fmt.Sprintf("B%d", b.Index) |
| 38 | } |
| 39 | |
| 40 | func formatBlockNode(cfg *CFG, b *Block) string { |
| 41 | // Replace sub-expressions with their block names. |
| 42 | subs := cfg.Subexprs(b) |
| 43 | subMap := make(map[ast.Node]*Block) |
| 44 | for _, sub := range subs { |
| 45 | subMap[sub.Node] = sub |
| 46 | } |
| 47 | |
| 48 | // We can't necessarily modify the AST in place, so we defensively clone it. |
| 49 | n := internalastutil.CloneNode(b.Node) |
| 50 | // The tricky part is that subMap is indexed by pre, but we need to modify |
| 51 | // post. To solve this, we number the nodes in a first pass over the |
| 52 | // pre-clone, and then use the same numbering to modify the post-clone. |
| 53 | apply := func(n ast.Node, f func(i int, c *astutil.Cursor) bool) { |
| 54 | i := 0 |
| 55 | astutil.Apply(n, func(c *astutil.Cursor) bool { |
| 56 | visit := f(i, c) |
| 57 | i++ |
| 58 | return visit |
| 59 | }, nil) |
| 60 | } |
| 61 | // First pass: number nodes and collect numbered rewrites. |
| 62 | type rewrite struct { |
| 63 | i int |
| 64 | b *Block |
| 65 | } |
| 66 | var rewrites []rewrite |
| 67 | apply(b.Node, func(i int, c *astutil.Cursor) bool { |
| 68 | node := c.Node() |
| 69 | if sub, ok := subMap[node]; ok { |
| 70 | rewrites = append(rewrites, rewrite{i, sub}) |
| 71 | return false |
| 72 | } |
| 73 | return true |
| 74 | }) |
| 75 | // Second pass: apply numbered rewrites. |
| 76 | apply(n, func(i int, c *astutil.Cursor) bool { |
| 77 | if len(rewrites) > 0 && rewrites[0].i == i { |
| 78 | id := &ast.Ident{Name: rewrites[0].b.String()} |
| 79 | c.Replace(id) |
| 80 | rewrites = rewrites[1:] |
| 81 | return false |
| 82 | } |
| 83 | return true |
| 84 | }) |
| 85 | |
| 86 | // Format. |
| 87 | var buf bytes.Buffer |
| 88 | format.Node(&buf, cfg.Fset, n) |
| 89 | return string(bytes.Replace(buf.Bytes(), []byte("\n"), []byte("\n\t"), -1)) |
| 90 | } |