Skip to content

Commit

Permalink
Use standard golang.org/x/tools/go/callgraph package (#24)
Browse files Browse the repository at this point in the history
* Delete `callgraph` package in favor of standard one
* Add `callgraphutil` package
* Add comments to test other algorithms
  • Loading branch information
picatz authored Jan 4, 2024
1 parent 97994d7 commit 8a457f3
Show file tree
Hide file tree
Showing 14 changed files with 483 additions and 327 deletions.
3 changes: 0 additions & 3 deletions callgraph/doc.go

This file was deleted.

9 changes: 9 additions & 0 deletions callgraphutil/aliases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package callgraphutil

import "golang.org/x/tools/go/callgraph"

// Nodes is a handy alias for a slice of callgraph.Nodes.
type Nodes = []*callgraph.Node

// Edges is a handy alias for a slice of callgraph.Edges.
type Edges = []*callgraph.Edge
35 changes: 35 additions & 0 deletions callgraphutil/calls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package callgraphutil

import "golang.org/x/tools/go/callgraph"

// CalleesOf returns nodes that are called by the caller node.
func CalleesOf(caller *callgraph.Node) Nodes {
calleesMap := make(map[*callgraph.Node]bool)
for _, e := range caller.Out {
calleesMap[e.Callee] = true
}

// Convert map to slice.
calleesSlice := make([]*callgraph.Node, 0, len(calleesMap))
for callee := range calleesMap {
calleesSlice = append(calleesSlice, callee)
}

return calleesSlice
}

// CallersOf returns nodes that call the callee node.
func CallersOf(callee *callgraph.Node) Nodes {
uniqCallers := make(map[*callgraph.Node]bool)
for _, e := range callee.In {
uniqCallers[e.Caller] = true
}

// Convert map to slice.
callersSlice := make(Nodes, 0, len(uniqCallers))
for caller := range uniqCallers {
callersSlice = append(callersSlice, caller)
}

return callersSlice
}
3 changes: 3 additions & 0 deletions callgraphutil/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package callgraphutil implements utilities for golang.org/x/tools/go/callgraph
// including path searching, graph construction, printing, and more.
package callgraphutil
Loading

0 comments on commit 8a457f3

Please sign in to comment.