Help us shape the future of Go! Take the 2026 Go Developer Survey

diff

package
v5.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const MaxCandidates = 100
View Source
const MaxFunctionBlocks = 5000

Variables

This section is empty.

Functions

func FingerprintTree

func FingerprintTree(rootDir string, fileFilter func(string) bool, policy ir.LiteralPolicy) ([]FingerprintResult, LoadMeta, error)

FingerprintTree fingerprints Go source under rootDir using a tree-mode load. If a real go.mod is found at or above rootDir, it's used directly. Otherwise a synthetic go.mod is supplied via packages.Config.Overlay (no disk write) so the loader has a canonical module path to resolve through — this fixes the qualifier-corruption case where types.Type.String() would otherwise carry a temp-dir-synthesized path.

fileFilter, if non-nil, keeps only function fingerprints whose source file satisfies the predicate. Use it to avoid fingerprinting the whole tree when the caller only cares about a subset (e.g., changed files in a diff).

GOPROXY=off is preserved via GetHardenedEnv(); files that import external modules without resolvable deps will still parse-fail by design.

KNOWN LIMITATION — same-module sub-package imports in real multi-package trees: when no real go.mod is found, the synthetic go.mod declares module "synthetic.local/anonymous" (see syntheticModulePath). For self-contained single-package trees (the synthetic-corpus shape this code was first validated against) this is fine — no imports need to resolve through the module path. For real multi-package modules whose internal files import other sub-packages of the same module (e.g., github.com/google/go-cmp's cmp/compare.go importing github.com/google/go-cmp/cmp/internal/diff), the synthetic module identity does NOT match the import paths declared in source, so the sub-package lookup fails with "cannot find module providing package <real-module-path>/<subpath>" even though the sub-package's source is present on disk in the tree.

Verified by real-corpus triage of the 3 genuine same-package-sibling commits in the pilot (go-cmp 8ebdfab3, x/text c8872a1a, x/text db455d00): in each case the failing sub-package directory EXISTS at the worktree- root-relative path that the real import declares, so a synthesized go.mod declaring the REAL module name resolves the imports correctly. Pass that name as TreeLoadOptions.ModuleNameHint via FingerprintTreeWithOptions. Affected corpus: pre-modules-era multi-package trees (modern commits carry their own go.mod and don't go through this synthesis path).

func FingerprintTreeAdvanced

func FingerprintTreeAdvanced(rootDir string, fileFilter func(string) bool, policy ir.LiteralPolicy, strictMode bool) ([]FingerprintResult, LoadMeta, error)

FingerprintTreeAdvanced is the strict-mode variant of FingerprintTree.

func FingerprintTreeWithOptions

func FingerprintTreeWithOptions(rootDir string, fileFilter func(string) bool, policy ir.LiteralPolicy, opts TreeLoadOptions) ([]FingerprintResult, LoadMeta, error)

FingerprintTreeWithOptions is the options-driven variant of FingerprintTree. Use this to configure GOPROXY (e.g. to allow downloading missing dependencies) or to supply a module name hint for pre-modules-era source trees that have no go.mod. The zero TreeLoadOptions value is identical to calling FingerprintTree.

func GetHardenedEnv

func GetHardenedEnv() []string

func IsCommutativeOp

func IsCommutativeOp(op token.Token) bool

func MatchFunctionsByTopology

func MatchFunctionsByTopology(oldResults, newResults []FingerprintResult, threshold float64) (
	matched []TopologyMatch,
	addedFuncs []FingerprintResult,
	removedFuncs []FingerprintResult,
)

func ShortFuncName

func ShortFuncName(fullName string) string

Types

type FingerprintResult

type FingerprintResult struct {
	FunctionName string
	Fingerprint  string
	CanonicalIR  string
	Pos          token.Pos
	Line         int
	Filename     string
	// contains filtered or unexported fields
}

func FingerprintPackages

func FingerprintPackages(initialPkgs []*packages.Package, policy ir.LiteralPolicy, strictMode bool) ([]FingerprintResult, error)

func FingerprintSource

func FingerprintSource(filename string, src string, policy ir.LiteralPolicy) ([]FingerprintResult, error)

func FingerprintSourceAdvanced

func FingerprintSourceAdvanced(filename string, src string, policy ir.LiteralPolicy, strictMode bool) ([]FingerprintResult, error)

func GenerateFingerprint

func GenerateFingerprint(fn *ssa.Function, policy ir.LiteralPolicy, strictMode bool) FingerprintResult

func (FingerprintResult) GetSSAFunction

func (r FingerprintResult) GetSSAFunction() *ssa.Function

type LoadMeta

type LoadMeta struct {
	HadGoMod         bool     // real go.mod found at or above rootDir
	SynthesizedGoMod bool     // loader supplied a synthetic go.mod via overlay
	ModulePath       string   // module path used for resolution (real or synthetic)
	LoadErrors       []string // per-package errors encountered during Load
}

LoadMeta records what the tree loader did for a particular FingerprintTree invocation. Carried alongside results so callers can tag analysis output with whether a real go.mod was found or whether one was synthesized.

type TopologyMatch

type TopologyMatch struct {
	OldResult   FingerprintResult
	NewResult   FingerprintResult
	OldTopology *topology.FunctionTopology
	NewTopology *topology.FunctionTopology
	Similarity  float64
	ByName      bool
}

type TreeLoadOptions

type TreeLoadOptions struct {
	// Proxy overrides the GOPROXY setting passed to the Go toolchain during
	// tree loading. If empty, the hardened default ("off") is used, which
	// prevents any external network access. Set to a proxy URL (e.g.
	// "https://proxy.golang.org,direct") when analysing source trees whose
	// declared dependencies are not yet cached in GOMODCACHE. All other
	// hardening applied by GetHardenedEnv (CGO_ENABLED=0, GOWORK=off, etc.)
	// remains in effect regardless of this field.
	Proxy string

	// ModuleNameHint is used as the module path in the synthesised go.mod
	// when a source tree has no real go.mod. If empty, the stable default
	// "synthetic.local/anonymous" is used. Setting this to the tree's actual
	// module path (e.g. "github.com/spf13/cobra") resolves same-module
	// sub-package imports correctly in pre-modules-era multi-package trees.
	ModuleNameHint string
}

TreeLoadOptions customises the tree-mode loader. The zero value is safe and matches the behaviour of FingerprintTree/FingerprintTreeAdvanced (secure hardened defaults: GOPROXY=off, synthetic module path for no-go.mod trees).

type Zipper

type Zipper struct {
	// contains filtered or unexported fields
}

Zipper implements the semantic delta analysis algorithm.

func NewZipper

func NewZipper(oldFn, newFn *ssa.Function, policy ir.LiteralPolicy) (*Zipper, error)

func (*Zipper) ComputeDiff

func (z *Zipper) ComputeDiff() (*ZipperArtifacts, error)

type ZipperArtifacts

type ZipperArtifacts struct {
	OldFunction  string
	NewFunction  string
	MatchedNodes int
	Added        []string
	Removed      []string
	Preserved    bool
}

ZipperArtifacts contains the results of the semantic delta analysis.

Jump to

Keyboard shortcuts

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