loader

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package loader loads a Go module once via go/packages and builds SSA for it. Nothing else in the program touches go/packages: every later phase works off the single *Program produced here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FuncPackage

func FuncPackage(fn *ssa.Function) string

FuncPackage returns the package path of fn, following Origin for generic instantiations and Parent for anonymous functions.

Types

type FileClass

type FileClass uint8

FileClass classifies a source file for pre-analysis exclusion. Generated and vendored files cannot be dropped at load time without breaking type-checking (a .pb.go file may define types that handwritten code uses), so they are classified immediately after load and excluded from every user-visible phase instead: entrypoint discovery, flow output, and stats.

const (
	Normal FileClass = iota
	Generated
	Vendor
)

func (FileClass) String

func (c FileClass) String() string

type Program

type Program struct {
	Prog     *ssa.Program
	Pkgs     []*packages.Package
	Fset     *token.FileSet
	Module   string // module path of the analyzed module
	Dir      string // absolute directory the module was loaded from
	Warnings []string
	// contains filtered or unexported fields
}

Program is the fully loaded and SSA-built analysis subject.

func Load

func Load(dir string) (*Program, error)

Load loads the module rooted at dir and builds SSA with generic instantiation. Test files never load (Tests: false), so they cannot inflate any later phase.

func (*Program) FuncClass

func (p *Program) FuncClass(fn *ssa.Function) FileClass

FuncClass reports the FileClass of the file declaring fn. Synthetic functions without a position (wrappers, bound methods) are Normal so they never mask real code they forward to.

func (*Program) GeneratedStats

func (p *Program) GeneratedStats() (generated, total int)

GeneratedStats counts module-local source functions by class, feeding the "most of your code is generated" honesty warning.

func (*Program) IfCondition

func (p *Program) IfCondition(pos token.Pos) (text string, stmtPos token.Pos, ok bool)

IfCondition renders the source text of the if-statement enclosing pos (typically an ssa.If condition's position) and returns the statement's own position for deduplication: `&&`/`||` conditions lower to several SSA branches that all belong to one source `if`. Text is verbatim (including non-ASCII), single-line, capped for card display.

func (*Program) IfConditionOfBody

func (p *Program) IfConditionOfBody(pos token.Pos) (text string, stmtPos token.Pos, ok bool)

IfConditionOfBody finds the If statement whose BODY (or else-branch) contains pos and renders its condition. Used for gates: their condition value (a phi, a stored bool) often carries no position inside the if, so the anchor comes from the first instruction of a branch instead. Walking outward and requiring pos inside Body/Else skips a nested if whose condition happens to open the branch.

func (*Program) InModule

func (p *Program) InModule(fn *ssa.Function) bool

InModule reports whether fn belongs to a package of the analyzed module.

func (*Program) PkgInModule

func (p *Program) PkgInModule(pkg string) bool

PkgInModule reports whether a package path belongs to the analyzed module.

func (*Program) Position

func (p *Program) Position(pos token.Pos) string

Position formats fn's declaration position relative to the module root, e.g. "service/pull.go:44". Unknown positions return "".

Jump to

Keyboard shortcuts

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