Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use standard golang.org/x/tools/go/callgraph package #24

Merged
merged 5 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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