go-callgraph

command module
v0.0.0-...-7781ba2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2025 License: MIT Imports: 6 Imported by: 0

README

go-callgraph

A static call graph analysis tool for Go programs. This tool parses Go source code and generates call graphs showing the relationships between functions and methods. The output can be exported in DOT (Graphviz) or JSON formats for visualization and further analysis.

Features

  • Multiple Analysis Algorithms: Supports three call graph construction algorithms:

    • static: Conservative static analysis (includes all possible calls)
    • cha: Class Hierarchy Analysis (fast, moderately precise)
    • rta: Rapid Type Analysis (more precise, requires main packages)
  • Multiple Output Formats:

    • DOT: Graphviz format for visualization with tools like dot, graphviz
    • JSON: Structured format for programmatic analysis
  • Extensible Design: Clean architecture with separate analyzer and exporter packages for easy extension to other languages or output formats

  • Research-Ready: Designed for correctness and extensibility, suitable for program analysis research

Installation

go install github.com/BaseMax/go-callgraph@latest

Or build from source:

git clone https://github.com/BaseMax/go-callgraph.git
cd go-callgraph
go build -o go-callgraph .

Usage

Basic usage:

go-callgraph [options] <package-path>
Options
  • -format <format>: Output format (dot or json). Default: dot
  • -output <file>: Output file path. If not specified, prints to stdout
  • -algo <algorithm>: Call graph algorithm (static, cha, or rta). Default: cha
Examples

Generate a DOT format call graph for a package:

go-callgraph -format=dot -output=graph.dot ./...

Generate a JSON format call graph using RTA algorithm:

go-callgraph -format=json -algo=rta . > callgraph.json

Analyze a specific package and visualize with Graphviz:

go-callgraph -format=dot ./mypackage | dot -Tpng -o callgraph.png

Analyze the example calculator program:

go-callgraph -format=dot ./examples

Output Formats

DOT Format

The DOT format output can be visualized using Graphviz tools:

# Generate PNG image
go-callgraph -format=dot ./... | dot -Tpng -o graph.png

# Generate SVG
go-callgraph -format=dot ./... | dot -Tsvg -o graph.svg

# Generate PDF
go-callgraph -format=dot ./... | dot -Tpdf -o graph.pdf
JSON Format

The JSON format includes detailed information about nodes and edges:

{
  "nodes": [
    {
      "id": "node_0",
      "package": "example.com/myapp",
      "receiver": "MyType",
      "function": "MyMethod",
      "fullName": "example.com/myapp.(MyType).MyMethod"
    }
  ],
  "edges": [
    {
      "from": "node_0",
      "to": "node_1",
      "site": "/path/to/file.go:10:5"
    }
  ]
}

Algorithms

Static Analysis (-algo=static)

The most conservative approach that includes all syntactically possible calls. This may include calls that never actually execute but provides complete coverage.

Use when: You need comprehensive coverage and don't mind false positives.

Class Hierarchy Analysis (-algo=cha)

Moderately precise analysis based on the class hierarchy. Faster than RTA and works on any Go code.

Use when: You need a balance between precision and performance (default choice).

Rapid Type Analysis (-algo=rta)

More precise analysis that considers reachable types from main packages. Requires main packages or entry points.

Use when: You need higher precision and have a complete program with main packages.

Example

The repository includes an example calculator program in examples/calculator.go:

package main

import "fmt"

type Calculator struct {
    name string
}

func NewCalculator(name string) *Calculator {
    return &Calculator{name: name}
}

func (c *Calculator) Add(a, b int) int {
    return c.compute(a, b, add)
}

func main() {
    calc := NewCalculator("MyCalc")
    sum := calc.Add(5, 3)
    fmt.Printf("Sum: %d\n", sum)
}

Analyze it:

go-callgraph -format=dot -output=examples/graph.dot ./examples

Architecture

The tool is organized into three main components:

  • main.go: CLI interface and command-line argument parsing
  • pkg/analyzer: Call graph construction using Go's SSA and callgraph packages
  • pkg/exporter: Export functionality for DOT and JSON formats

This modular design allows easy extension:

  • Add new analysis algorithms in the analyzer package
  • Add new export formats in the exporter package
  • Extend to other languages by creating new analyzer implementations

Use Cases

  • Code Understanding: Visualize program structure and function relationships
  • Impact Analysis: Understand which functions are affected by changes
  • Dead Code Detection: Identify unreachable functions
  • Testing: Determine test coverage requirements
  • Research: Program analysis and software engineering research
  • Documentation: Generate architectural diagrams

Requirements

  • Go 1.18 or later (for generics support in dependencies)

Dependencies

  • golang.org/x/tools/go/packages: Package loading
  • golang.org/x/tools/go/ssa: SSA construction
  • golang.org/x/tools/go/callgraph: Call graph algorithms

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

License

See LICENSE file for details.

Author

BaseMax

Roadmap

  • Support for additional languages (Python, JavaScript, etc.)
  • Interactive web-based visualization
  • Call graph filtering and querying
  • Performance optimizations for large codebases
  • Integration with IDE plugins

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL