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

diff

package
v6.0.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const LoaderEnvVar = "SFW_LOADER"

LoaderEnvVar selects the source loader. Set to "packages" it makes every source load a plain go/packages LoadAllSyntax load, which type-checks every dependency's function bodies; any other value selects the faster loader (see loadSource).

View Source
const MaxCandidates = 100
View Source
const MaxFunctionBlocks = 5000
View Source
const TypeCacheEnvVar = "SFW_TYPE_CACHE"

TypeCacheEnvVar configures the persistent type cache, which keeps the declarations of shareable dependencies (see dependencyKey) across processes: "rw:DIR" reads and writes entries in the absolute directory DIR, "ro:DIR" only reads them, and any other value turns the cache off.

An entry is keyed by everything that determines the types: the running executable's contents, the package path and name, the type sizes, the Go language version, GODEBUG, each compiled file's path and content hash, and the keys of its imports. A hit therefore means the same source bytes checked by the same program. Only packages without errors are stored; an entry that fails its checksum or does not decode is a miss.

Variables

This section is empty.

Functions

func GetHardenedEnv

func GetHardenedEnv() []string

GetHardenedEnv returns the environment for loading untrusted source: no cgo, no network (GOPROXY=off), a read-only module graph, no workspace (see HardenedEnvFor) and the local toolchain.

func HardenedEnvFor

func HardenedEnvFor(dir string) []string

HardenedEnvFor is GetHardenedEnv for loading packages in dir: it uses the workspace that governs dir (see WorkspaceFile), as the go command builds it, and none when there is none, and the vendor directory the go command builds dir from (see VendorDir), if any.

func LoadFiles

func LoadFiles(dir string, files map[string][]byte) ([]*packages.Package, error)

LoadFiles loads, with one go/packages call run from dir, the packages that compile the given files: absolute paths mapped to their contents, which are used as overlays. It uses the same configuration as the single-file loader behind FingerprintSource, so a package loads the same way either way. Unlike FingerprintSource it does not retry a file the default build leaves out (a _test.go file, a GOOS suffix, a //go:build line): such a file is in none of the returned packages, and PackagesContain reports it.

func MatchFunctionsByTopology

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

func PackageFunctions

func PackageFunctions(prog *ssa.Program, pkg *ssa.Package) []*ssa.Function

PackageFunctions lists every function with a body that a package contributes: package-level functions, methods of its named types, and, recursively, the closures they contain -- including the synthetic bodies of range-over-func loops. Ordered by name for deterministic output.

func PackagesContain

func PackagesContain(pkgs []*packages.Package, absPath string) bool

PackagesContain reports whether any of pkgs compiles the file at absPath.

func ShortFuncName

func ShortFuncName(fullName string) string

func VendorDir

func VendorDir(dir string) string

VendorDir returns the vendor directory the go command builds dir from, or "" when it builds from the module cache. As in the go command, that is the workspace's vendor directory when a go.work governs dir (see WorkspaceFile) and the go.work's go line is 1.22 or later, and otherwise the vendor directory of the module whose go.mod is nearest above dir when the go.mod's go line is 1.14 or later (a missing go line counts as 1.16, as the go command counts it). The directory must exist; whether it is consistent with the go.mod is the go command's to report.

func WorkspaceFile

func WorkspaceFile(dir string) string

WorkspaceFile returns the go.work file that governs builds in dir as the go command finds it, the first in dir or a parent, or "" when there is none. The search stops at the repository's root, the first directory holding a .git entry: a tree checked out inside another must not take the outer tree's workspace.

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 FingerprintPackagesWhere

func FingerprintPackagesWhere(initialPkgs []*packages.Package, policy ir.LiteralPolicy, strictMode bool, keep func(filename string) bool) ([]FingerprintResult, error)

FingerprintPackagesWhere is FingerprintPackages for the functions whose file, as FingerprintResult.Filename names it, keep accepts, and those without a file; a nil keep accepts every file. The other functions are not canonicalized: a diff of a few files of a large package reads only their functions.

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 FingerprintSourceWhere

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

FingerprintSourceWhere is FingerprintSourceAdvanced restricted as FingerprintPackagesWhere restricts FingerprintPackages.

func GenerateFingerprint

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

func (FingerprintResult) GetSSAFunction

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

type TopologyMatch

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

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.

Directories

Path Synopsis
internal
purge
Package purge strips the bodies of a Go source file so that only its declarations remain to be parsed.
Package purge strips the bodies of a Go source file so that only its declarations remain to be parsed.

Jump to

Keyboard shortcuts

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