-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwalk.go
75 lines (65 loc) · 1.4 KB
/
walk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package wrapmsg
import (
"context"
"github.com/Warashi/ssautil"
"golang.org/x/tools/go/ssa"
)
type walker struct {
stack []interface{}
}
func (w *walker) push(n interface{}) {
w.stack = append(w.stack, n)
}
func (w *walker) pop() {
w.stack = w.stack[:len(w.stack)-1]
}
func (w *walker) contains(n interface{}) bool {
for _, s := range w.stack {
if s == n {
return true
}
}
return false
}
func (w *walker) walkRefs(ctx context.Context, v ssautil.Referrerer) ([]string, bool) {
for _, v := range ssautil.Referrers(v) {
if r, ok := w.walk(ctx, v); ok {
return r, true
}
}
return nil, false
}
func (w *walker) walkOperands(ctx context.Context, v ssautil.Operander) ([]string, bool) {
for _, v := range ssautil.Operands(v) {
if r, ok := w.walk(ctx, v); ok {
return r, true
}
}
return nil, false
}
func (w *walker) walk(ctx context.Context, v ssautil.Poser) ([]string, bool) {
if w.contains(v) {
return nil, false
}
w.push(v)
defer w.pop()
switch v := v.(type) {
case *ssa.Slice:
return w.walkOperands(ctx, v)
case *ssa.Alloc:
return w.walkRefs(ctx, v)
case *ssa.IndexAddr:
return w.walkRefs(ctx, v)
case *ssa.Store:
return w.walkOperands(ctx, v)
case *ssa.ChangeInterface:
return w.walkOperands(ctx, v)
case *ssa.Extract:
return w.walkOperands(ctx, v)
case *ssa.UnOp:
return w.walkRefs(ctx, v.X)
case *ssa.Call:
return formatCall(ctx, v)
}
return nil, false
}