Documentation
¶
Overview ¶
Package callgraph builds SSA-based call graphs from Go source code and provides query methods for caller/callee lookups, test discovery, and edge iteration.
Algorithms ¶
Two algorithms are supported:
- CHA (Class Hierarchy Analysis) — conservative. Includes all possible dispatch targets for interface calls. Fast, may over-approximate.
- RTA (Rapid Type Analysis) — precise. Tracks which concrete types are actually instantiated and reachable. Slower, fewer false edges.
RTA is the default for most use cases. Use CHA when you need faster builds and can tolerate some extra edges.
Quick start ¶
g, err := callgraph.Build([]string{"/path/to/module"}, callgraph.RTA)
if err != nil { ... }
refs := g.FindFunctions("MyFunc")
callers := g.DirectCallers(refs)
for _, c := range callers {
fmt.Println(c.Name, c.File, c.Line)
}
Integration with persistence layers ¶
If you already load packages yourself (e.g. with custom config or exclusion filters), use BuildFromPackages to skip the load step. Combine with Graph.ForEachEdgeInPackages and QualifiedID to persist edges to a store:
g, _ := callgraph.BuildFromPackages(pkgs, callgraph.RTA)
g.ForEachEdgeInPackages(myPkgSet, func(e callgraph.EdgeInfo) bool {
store.Insert(callgraph.QualifiedID(e.Caller), callgraph.QualifiedID(e.Callee))
return true
})
Index ¶
- Constants
- func FuncPkgPath(fn *ssa.Function) string
- func LoadPackages(dir string, patterns ...string) ([]*packages.Package, error)
- func QualifiedID(fn *ssa.Function) string
- func QualifiedObjID(pkgPath string, obj types.Object) string
- type EdgeInfo
- type FuncInfo
- type FuncRef
- type Graph
- func (g *Graph) CallersToTests(refs []FuncRef) []FuncInfo
- func (g *Graph) DirectCallees(refs []FuncRef) []FuncInfo
- func (g *Graph) DirectCallerTests(refs []FuncRef) []FuncInfo
- func (g *Graph) DirectCallers(refs []FuncRef) []FuncInfo
- func (g *Graph) FindFunctions(symbol string) []FuncRef
- func (g *Graph) FindTestsByName(names []string) []FuncInfo
- func (g *Graph) ForEachEdge(fn func(EdgeInfo) bool)
- func (g *Graph) ForEachEdgeInPackages(pkgPaths map[string]bool, fn func(EdgeInfo) bool)
- func (g *Graph) FuncInfos(refs []FuncRef) []FuncInfo
- func (g *Graph) Method() Method
- func (g *Graph) Modules() []*ModuleGraph
- func (g *Graph) SymbolCount() int
- func (g *Graph) TestsCovering(affectedDirs map[string]bool, changedFiles []string, symbolNames []string) map[string][]string
- type Method
- type ModuleGraph
Constants ¶
const LoadMode = packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo
LoadMode is the packages.Config.Mode required for SSA construction. Exported so consumers can compose it with additional flags if needed.
Variables ¶
This section is empty.
Functions ¶
func FuncPkgPath ¶
FuncPkgPath returns the package path of an SSA function, or "".
func LoadPackages ¶
LoadPackages loads Go packages from dir with the standard mode flags required for SSA call graph construction. Tests are always included. If no patterns are given, "./..." is used.
func QualifiedID ¶
QualifiedID returns a stable fully-qualified ID for an SSA function, in the form "pkg/path.FuncName" or "pkg/path.Type.MethodName". Returns "" for synthetic functions (wrappers, thunks) or builtins.
func QualifiedObjID ¶
QualifiedObjID builds a stable fully-qualified ID from a package path and types.Object. For methods, the format is "pkg/path.TypeName.MethodName". For functions, "pkg/path.FuncName". This can be used for both SSA functions and AST-level symbol indexing.
Types ¶
type FuncRef ¶
type FuncRef struct {
Fn *ssa.Function
Node *gocg.Node
MG *ModuleGraph
}
FuncRef is a reference to a function in the graph. Pass to DirectCallers, DirectCallees, CallersToTests, etc.
type Graph ¶
type Graph struct {
// contains filtered or unexported fields
}
Graph wraps one or more call graphs (one per module) and provides query methods for test discovery and caller/callee lookups.
func Build ¶
Build loads packages from each module root, builds SSA, and runs the selected call graph algorithm. Each root is analysed independently.
func BuildFromPackages ¶
BuildFromPackages builds SSA and a call graph from pre-loaded packages. Use this when you have already called packages.Load yourself (e.g. with a custom Config). Returns a single-module Graph.
func (*Graph) CallersToTests ¶
CallersToTests walks callers upward from the given functions through the call graph and returns all Test/Benchmark functions reached.
func (*Graph) DirectCallees ¶
DirectCallees returns functions that any of the given functions directly call.
func (*Graph) DirectCallerTests ¶
DirectCallerTests returns Test/Benchmark functions that directly call any of the given functions (one level up only, no transitive walk).
func (*Graph) DirectCallers ¶
DirectCallers returns functions that directly call any of the given functions.
func (*Graph) FindFunctions ¶
FindFunctions returns references to SSA functions whose name matches symbol.
func (*Graph) FindTestsByName ¶
FindTestsByName returns Test/Benchmark functions whose name contains any of the given substrings. For example, "GetLocation" matches "TestGetLocation", "Test_GetLocation", "TestGetLocation_NotFound".
func (*Graph) ForEachEdge ¶
ForEachEdge iterates all call edges in the graph. The callback receives each caller-callee pair. Return false to stop iteration.
func (*Graph) ForEachEdgeInPackages ¶
ForEachEdgeInPackages iterates call edges where the caller belongs to one of the given package paths. This is the filtered variant for consumers that only want edges originating from their own code.
func (*Graph) Modules ¶
func (g *Graph) Modules() []*ModuleGraph
Modules returns the underlying module graphs for direct access.
func (*Graph) SymbolCount ¶
SymbolCount returns the total number of user-defined (non-synthetic) functions with valid source positions in the graph.
func (*Graph) TestsCovering ¶
func (g *Graph) TestsCovering(affectedDirs map[string]bool, changedFiles []string, symbolNames []string) map[string][]string
TestsCovering finds functions defined in changedFiles whose name matches one of symbolNames, then walks callers upward through the call graph and collects Test/Benchmark functions that are in one of the affectedDirs. Returns dir → []testName.
type ModuleGraph ¶
type ModuleGraph struct {
CG *gocg.Graph
Prog *ssa.Program
ByFile map[string][]*ssa.Function // source file → SSA functions defined there
}
ModuleGraph holds the call graph and SSA program for a single module. Fields are exported so consumers can access the underlying data for custom traversal beyond what the query methods provide.