forked from ondrajz/go-callvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (146 loc) · 4.02 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// go-callvis: a tool to help visualize the call graph of a Go program.
//
package main
import (
"flag"
"fmt"
"go/build"
"log"
"os"
"strings"
"time"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/pointer"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)
var Version = "0.0.0-src"
var (
focusFlag = flag.String("focus", "main", "Focus package with name.")
limitFlag = flag.String("limit", "", "Limit package path to prefix.")
groupFlag = flag.String("group", "", "Grouping by [type, pkg].")
ignoreFlag = flag.String("ignore", "", "Ignore package paths with prefix (separated by comma).")
testFlag = flag.Bool("tests", false, "Include test code.")
debugFlag = flag.Bool("debug", false, "Enable verbose log.")
versionFlag = flag.Bool("version", false, "Show version and exit.")
)
func main() {
// Graphviz options
flag.UintVar(&minlen, "minlen", 2, "Minimum edge length (for wider output).")
flag.Float64Var(&nodesep, "nodesep", 0.35, "Minimum space between two adjacent nodes in the same rank (for taller output).")
flag.Parse()
if *versionFlag {
fmt.Fprintf(os.Stderr, "go-callvis %s\n", Version)
os.Exit(0)
}
if *debugFlag {
log.SetFlags(log.Lmicroseconds)
}
ignorePaths := []string{}
for _, p := range strings.Split(*ignoreFlag, ",") {
p = strings.TrimSpace(p)
if p != "" {
ignorePaths = append(ignorePaths, p)
}
}
groupBy := make(map[string]bool)
for _, g := range strings.Split(*groupFlag, ",") {
g := strings.TrimSpace(g)
if g == "" {
continue
}
if g != "pkg" && g != "type" {
fmt.Fprintf(os.Stderr, "go-callvis: %s\n", "invalid group option")
os.Exit(1)
}
groupBy[g] = true
}
if err := run(&build.Default, *focusFlag, *limitFlag, groupBy, ignorePaths, *testFlag, flag.Args()); err != nil {
fmt.Fprintf(os.Stderr, "go-callvis: %s\n", err)
os.Exit(1)
}
}
func run(ctxt *build.Context, focus, limitPath string, groupBy map[string]bool, ignorePaths []string, tests bool, args []string) error {
if len(args) == 0 {
return fmt.Errorf("missing arguments")
}
t0 := time.Now()
conf := loader.Config{Build: ctxt}
_, err := conf.FromArgs(args, tests)
if err != nil {
return err
}
load, err := conf.Load()
if err != nil {
return err
}
logf("loading took: %v", time.Since(t0))
logf("%d imported (%d created)", len(load.Imported), len(load.Created))
t0 = time.Now()
prog := ssautil.CreateProgram(load, 0)
prog.Build()
pkgs := prog.AllPackages()
logf("building took: %v", time.Since(t0))
var focusPkg *build.Package
if focus != "" {
focusPkg, err = conf.Build.Import(focus, "", 0)
if err != nil {
if strings.Contains(focus, "/") {
return err
}
// try to find package by name
var foundPaths []string
for _, p := range pkgs {
if p.Pkg.Name() == focus {
foundPaths = append(foundPaths, p.Pkg.Path())
}
}
if len(foundPaths) == 0 {
return err
} else if len(foundPaths) > 1 {
for _, p := range foundPaths {
fmt.Fprintf(os.Stderr, " - %s\n", p)
}
return fmt.Errorf("found %d packages with name %q, use import path not name", len(foundPaths), focus)
}
if focusPkg, err = conf.Build.Import(foundPaths[0], "", 0); err != nil {
return err
}
}
logf("focusing: %v", focusPkg.ImportPath)
}
var mains []*ssa.Package
if tests {
for _, pkg := range pkgs {
if main := prog.CreateTestMainPackage(pkg); main != nil {
mains = append(mains, main)
}
}
if mains == nil {
return fmt.Errorf("no tests")
}
} else {
mains = append(mains, ssautil.MainPackages(pkgs)...)
if len(mains) == 0 {
return fmt.Errorf("no main packages")
}
}
logf("%d packages (%d main)", len(pkgs), len(mains))
t0 = time.Now()
ptrcfg := &pointer.Config{
Mains: mains,
BuildCallGraph: true,
}
result, err := pointer.Analyze(ptrcfg)
if err != nil {
return err
}
logf("analysis took: %v", time.Since(t0))
return printOutput(mains[0].Pkg, result.CallGraph,
focusPkg, limitPath, ignorePaths, groupBy)
}
func logf(f string, a ...interface{}) {
if *debugFlag {
log.Printf(f, a...)
}
}