Skip to content

Commit abc85c3

Browse files
committed
Add option to omit std packages
1 parent 2dc5fd5 commit abc85c3

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ Represents | Style
105105
Grouping functions by [pkg, type]. (separate multiple by comma)
106106
-ignore string
107107
Ignore package paths with prefix. (separate multiple by comma)
108+
-nostd
109+
Omit calls from/to std packages.
108110
-minlen uint
109111
Minimum edge length (for wider output). (default: 2)
110112
-nodesep float
@@ -125,12 +127,14 @@ Join [#go-callvis](https://gophers.slack.com/archives/go-callvis) channel at [go
125127

126128
> *Not a member yet?* [Get invitation](https://gophersinvite.herokuapp.com).
127129
128-
### How to contribute
130+
### How to help
129131

130132
###### Did you find any bugs or have some suggestions?
131-
132133
Feel free to open [new issue](https://github.com/TrueFurby/go-callvis/issues/new) or start discussion in the slack channel.
133134

135+
###### Do you want to contribute to the development?
136+
Fork the project and do a pull request. [Here](https://github.com/TrueFurby/go-callvis/projects/1) you can find the state of features.
137+
134138
### Known Issues
135139

136140
###### Each execution takes a lot of time, because currently:

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
limitFlag = flag.String("limit", "", "Limit package paths to prefix. (separate multiple by comma)")
2525
groupFlag = flag.String("group", "", "Grouping functions by [pkg, type] (separate multiple by comma).")
2626
ignoreFlag = flag.String("ignore", "", "Ignore package paths with prefix (separate multiple by comma).")
27+
nostdFlag = flag.Bool("nostd", false, "Omit calls to/from std packages.")
2728
testFlag = flag.Bool("tests", false, "Include test code.")
2829
debugFlag = flag.Bool("debug", false, "Enable verbose log.")
2930
versionFlag = flag.Bool("version", false, "Show version and exit.")
@@ -73,13 +74,13 @@ func main() {
7374
}
7475
}
7576

76-
if err := run(&build.Default, *focusFlag, groupBy, limitPaths, ignorePaths, *testFlag, flag.Args()); err != nil {
77+
if err := run(&build.Default, *focusFlag, groupBy, limitPaths, ignorePaths, *nostdFlag, *testFlag, flag.Args()); err != nil {
7778
fmt.Fprintf(os.Stderr, "go-callvis: %s\n", err)
7879
os.Exit(1)
7980
}
8081
}
8182

82-
func run(ctxt *build.Context, focus string, groupBy map[string]bool, limitPaths, ignorePaths []string, tests bool, args []string) error {
83+
func run(ctxt *build.Context, focus string, groupBy map[string]bool, limitPaths, ignorePaths []string, nostd, tests bool, args []string) error {
8384
if len(args) == 0 {
8485
return fmt.Errorf("missing arguments")
8586
}
@@ -162,7 +163,7 @@ func run(ctxt *build.Context, focus string, groupBy map[string]bool, limitPaths,
162163
logf("analysis took: %v", time.Since(t0))
163164

164165
return printOutput(mains[0].Pkg, result.CallGraph,
165-
focusPkg, limitPaths, ignorePaths, groupBy)
166+
focusPkg, limitPaths, ignorePaths, groupBy, nostd)
166167
}
167168

168169
func logf(f string, a ...interface{}) {

output.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
var output io.Writer = os.Stdout
1616

17-
func printOutput(mainPkg *types.Package, cg *callgraph.Graph, focusPkg *build.Package, limitPaths, ignorePaths []string, groupBy map[string]bool) error {
17+
func printOutput(mainPkg *types.Package, cg *callgraph.Graph, focusPkg *build.Package, limitPaths, ignorePaths []string, groupBy map[string]bool, nostd bool) error {
1818
groupType := groupBy["type"]
1919
groupPkg := groupBy["pkg"]
2020

@@ -41,31 +41,37 @@ func printOutput(mainPkg *types.Package, cg *callgraph.Graph, focusPkg *build.Pa
4141

4242
logf("%d limit prefixes: %v", len(limitPaths), limitPaths)
4343
logf("%d ignore prefixes: %v", len(ignorePaths), ignorePaths)
44+
logf("no std packages: %v", nostd)
4445

45-
var inLimit = func(from, to *callgraph.Node) bool {
46-
if len(limitPaths) == 0 {
47-
return true
48-
}
49-
var fromOk, toOk bool
50-
fromPath := from.Func.Pkg.Pkg.Path()
51-
toPath := to.Func.Pkg.Pkg.Path()
46+
var inLimit = func(node *callgraph.Node) bool {
47+
pkgPath := node.Func.Pkg.Pkg.Path()
5248
for _, p := range limitPaths {
53-
if strings.HasPrefix(fromPath, p) {
54-
fromOk = true
55-
}
56-
if strings.HasPrefix(toPath, p) {
57-
toOk = true
49+
if strings.HasPrefix(pkgPath, p) {
50+
return true
5851
}
59-
if fromOk && toOk {
60-
logf("in limit: %s -> %s", from, to)
52+
}
53+
return false
54+
}
55+
56+
var isIgnored = func(node *callgraph.Node) bool {
57+
pkgPath := node.Func.Pkg.Pkg.Path()
58+
for _, p := range ignorePaths {
59+
if strings.HasPrefix(pkgPath, p) {
6160
return true
6261
}
6362
}
64-
logf("NOT in limit: %s -> %s", from, to)
6563
return false
6664
}
6765

66+
var isStd = func(node *callgraph.Node) bool {
67+
pkg, _ := build.Import(node.Func.Pkg.Pkg.Path(), "", 0)
68+
return pkg.Goroot
69+
}
70+
71+
count := 0
6872
err := callgraph.GraphVisitEdges(cg, func(edge *callgraph.Edge) error {
73+
count++
74+
6975
caller := edge.Caller
7076
callee := edge.Callee
7177

@@ -79,21 +85,26 @@ func printOutput(mainPkg *types.Package, cg *callgraph.Graph, focusPkg *build.Pa
7985

8086
// focus specific pkg
8187
if focusPkg != nil &&
82-
!(callerPkg.Path() == focusPkg.ImportPath || calleePkg.Path() == focusPkg.ImportPath) {
88+
(callerPkg.Path() != focusPkg.ImportPath && calleePkg.Path() != focusPkg.ImportPath) {
8389
return nil
8490
}
8591

8692
// limit path prefixes
87-
if !inLimit(caller, callee) {
93+
if len(limitPaths) > 0 &&
94+
(!inLimit(caller) || !inLimit(callee)) {
95+
logf("NOT in limit: %s -> %s", caller, callee)
8896
return nil
8997
}
9098

9199
// ignore path prefixes
92-
for _, p := range ignorePaths {
93-
if strings.HasPrefix(callerPkg.Path(), p) ||
94-
strings.HasPrefix(calleePkg.Path(), p) {
95-
return nil
96-
}
100+
if len(ignorePaths) > 0 &&
101+
(isIgnored(caller) || isIgnored(callee)) {
102+
return nil
103+
}
104+
105+
// omit std
106+
if nostd && (isStd(caller) || isStd(callee)) {
107+
return nil
97108
}
98109

99110
var sprintNode = func(node *callgraph.Node) *dotNode {
@@ -261,7 +272,7 @@ func printOutput(mainPkg *types.Package, cg *callgraph.Graph, focusPkg *build.Pa
261272
return err
262273
}
263274

264-
logf("%d edges", len(edges))
275+
logf("%d/%d edges", len(edges), count)
265276

266277
dot := &dotGraph{
267278
Title: mainPkg.Path(),

0 commit comments

Comments
 (0)