@@ -14,7 +14,7 @@ import (
1414
1515var 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