Debian Codegraph @

go/ir/irutil/util.go

raw
1 package irutil
2
3 import (
4 "go/types"
5 "slices"
6 "strings"
7
8 "honnef.co/go/tools/go/ir"
9 "honnef.co/go/tools/go/types/typeutil"
10 )
11
12 func Reachable(from, to *ir.BasicBlock) bool {
13 if from == to {
14 return true
15 }
16 if from.Dominates(to) {
17 return true
18 }
19
20 found := false
21 Walk(from, func(b *ir.BasicBlock) bool {
22 if b == to {
23 found = true
24 return false
25 }
26 return true
27 })
28 return found
29 }
30
31 func Walk(b *ir.BasicBlock, fn func(*ir.BasicBlock) bool) {
32 seen := map[*ir.BasicBlock]bool{}
33 wl := []*ir.BasicBlock{b}
34 for len(wl) > 0 {
35 b := wl[len(wl)-1]
36 wl = wl[:len(wl)-1]
37 if seen[b] {
38 continue
39 }
40 seen[b] = true
41 if !fn(b) {
42 continue
43 }
44 wl = append(wl, b.Succs...)
45 }
46 }
47
48 func Vararg(x *ir.Slice) ([]ir.Value, bool) {
49 var out []ir.Value
50 alloc, ok := x.X.(*ir.Alloc)
51 if !ok {
52 return nil, false
53 }
54 for _, ref := range *alloc.Referrers() {
55 if ref == x {
56 continue
57 }
58 if ref.Block() != x.Block() {
59 return nil, false
60 }
61 switch ref := ref.(type) {
62 case *ir.IndexAddr:
63 idx := ref
64 if len(*idx.Referrers()) != 1 {
65 return nil, false
66 }
67 store, ok := (*idx.Referrers())[0].(*ir.Store)
68 if !ok {
69 return nil, false
70 }
71 out = append(out, store.Val)
72 default:
73 return nil, false
74 }
75 }
76 return out, true
77 }
78
79 func CallName(call *ir.CallCommon) string {
80 if call.IsInvoke() {
81 return ""
82 }
83 switch v := call.Value.(type) {
84 case *ir.Function:
85 fn, ok := v.Object().(*types.Func)
86 if !ok {
87 return ""
88 }
89 return typeutil.FuncName(fn)
90 case *ir.Builtin:
91 return v.Name()
92 }
93 return ""
94 }
95
96 func IsCallTo(call *ir.CallCommon, name string) bool { return CallName(call) == name }
97
98 func IsCallToAny(call *ir.CallCommon, names ...string) bool {
99 q := CallName(call)
100 return slices.Contains(names, q)
101 }
102
103 func IsExample(fn *ir.Function) bool {
104 if !strings.HasPrefix(fn.Name(), "Example") {
105 return false
106 }
107 f := fn.Prog.Fset.File(fn.Pos())
108 if f == nil {
109 return false
110 }
111 return strings.HasSuffix(f.Name(), "_test.go")
112 }
113
114 // Flatten recursively returns the underlying value of an
115 // ir.Phi node. If all edges in an ir.Phi node are the same (after
116 // flattening), the flattened edge will get returned. If flattening is
117 // not possible, nil is returned.
118 func Flatten(v ir.Value) ir.Value {
119 failed := false
120 seen := map[ir.Value]struct{}{}
121 var out ir.Value
122 var dfs func(v ir.Value)
123 dfs = func(v ir.Value) {
124 if failed {
125 return
126 }
127 if _, ok := seen[v]; ok {
128 return
129 }
130 seen[v] = struct{}{}
131
132 switch v := v.(type) {
133 case *ir.Phi:
134 for _, e := range v.Edges {
135 dfs(e)
136 }
137 default:
138 if out == nil {
139 out = v
140 } else if out != v {
141 failed = true
142 }
143 }
144 }
145 dfs(v)
146
147 if failed {
148 return nil
149 }
150 return out
151 }