-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (68 loc) · 1.31 KB
/
main.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
76
77
78
package main
import (
"fmt"
"go/types"
"os"
"github.com/kkHAIKE/ssaviewer/gen"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)
func getAllFunction(prog *ssa.Program, pkg *ssa.Package) (ret []*ssa.Function) {
add := func(f *ssa.Function) {
if f == nil {
return
}
ret = append(ret, f)
ret = append(ret, f.AnonFuncs...)
}
addType := func(tp types.Type) {
ts := prog.MethodSets.MethodSet(tp)
for i := 0; i < ts.Len(); i++ {
add(prog.MethodValue(ts.At(i)))
}
}
for _, v := range pkg.Members {
switch vv := v.(type) {
case *ssa.Function:
add(vv)
case *ssa.Type:
addType(vv.Type())
addType(types.NewPointer(vv.Type()))
}
}
return
}
func main() {
pkgs, err := packages.Load(&packages.Config{
Mode: packages.LoadSyntax,
Fset: gen.Fset,
}, ".")
if err != nil {
panic(err)
}
prog, ssapkgs := ssautil.Packages(pkgs, 0)
pkg := ssapkgs[0]
pkg.SetDebugMode(true)
pkg.Build()
fs := getAllFunction(prog, pkg)
if len(os.Args) == 1 {
for _, v := range fs {
fmt.Println(v.RelString(pkg.Pkg))
}
return
}
var f *ssa.Function
for _, v := range fs {
if v.RelString(pkg.Pkg) == os.Args[1] {
f = v
break
}
}
if f == nil {
panic("not found")
}
w := gen.NewHTMLWriter("ssa.html", f)
w.WriteFunc()
w.Close()
}