goanalyzer

package
v0.0.0-...-ebf9fc4 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package goanalyzer extracts a function call graph from real Go source and projects it onto the dependency-free graph model in internal/graph.

The pipeline is four stages:

packages.Load        parse + type-check the requested packages
ssautil.AllPackages  lower them (and their deps) to SSA
cha.CallGraph        build a whole-program call graph via CHA
project              copy the relevant edges into internal/graph

Why CHA: Class Hierarchy Analysis is a deliberately approximate whole-program analysis. For a static call it records the exact callee. For a dynamic (interface / method-value) call it does NO flow analysis: it links the caller to *every* method in the program whose type could satisfy the interface. That over-approximation is acceptable here — for review ordering we would rather see a superset of the real dependencies than silently miss one — and it is cheap and dependency-free. The golden tests pin the approximation down so it is visible and documented rather than hidden.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FuncInfo

type FuncInfo struct {
	File     string
	Start    int
	End      int
	DocStart int
	// contains filtered or unexported fields
}

FuncInfo carries the source metadata for one function node: where it is declared and (privately) the AST node used to render its source on demand.

File is an absolute path ("" if unknown). Start and End are 1-based line numbers spanning the declaration — from the `func` keyword through the closing brace — taken from AST positions, not guessed from line text; both are 0 if unknown. DocStart is Start, or the doc comment's line when the declaration has one; it is the start used for change detection so that editing only the doc comment still flags the function.

type Result

type Result struct {
	// Graph is the call graph restricted to user code: a node is a function
	// identifier and an edge A->B means "function A calls B".
	Graph *graph.Graph

	// Funcs maps each node id to its source metadata (declaring file and line
	// range). Kept here rather than in internal/graph so that package stays a
	// pure, import-free data structure.
	Funcs map[string]FuncInfo

	// Fset is the shared position table for the analysed program; it backs both
	// the positions in Funcs and the source slicing in Source.
	Fset *token.FileSet
	// contains filtered or unexported fields
}

Result is the outcome of analysing a set of packages.

func Analyze

func Analyze(dir string, patterns ...string) (*Result, error)

Analyze loads the Go packages matched by patterns (resolved relative to dir), builds their call graph with CHA, and returns it projected onto our graph model, keeping only functions declared in the matched packages.

func (*Result) Source

func (r *Result) Source(id string) (string, bool)

Source returns the original Go source of the function identified by id and true, or ("", false) if id is unknown or has no associated syntax (e.g. a compiler-synthesised function).

The text is sliced verbatim from the source file, from the declaration's start — its doc comment if present, otherwise the `func` keyword — through its closing brace, using byte offsets from the AST (not a line heuristic). Slicing the original bytes (rather than reprinting the AST) preserves everything exactly: comments inside the body, blank lines, and the original formatting. The declaring file is read from disk at most once and cached.

Jump to

Keyboard shortcuts

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