HeadlinesBriefing favicon HeadlinesBriefing.com

Go Analysis Package: Modular Static Analysis Framework

Hacker News •
×

Package analysis defines the interface between a modular static analysis and an analysis driver program. A static analysis inspects a package of Go code and reports diagnostics, such as mistakes or suggested refactorings. Modular analyses inspect one package at a time but can save information from lower-level packages for use in higher-level packages, similar to separate compilation. For example, the printf checker records when a function delegates to fmt.Printf and checks calls across package boundaries.

The primary type is Analyzer, which statically describes an analysis function: its name, documentation, flags, dependencies, and run logic. Analyzers reside in separate packages and are collected by a driver like vet, which runs them and prints diagnostics. The Analyzer struct includes fields for flags, result types, required analyses, and fact types. Drivers can configure flags via command line, IDE, or config files.

A Pass describes a single unit of work: applying an Analyzer to a package. It provides syntax trees, type information, source positions, and results from required analyzers. The Report function emits diagnostics with optional categories; severity filtering is left to the driver. Analyzers like ctrlflow, inspect, and buildssa extend capabilities without core API dependencies.

This framework enables integration with tools such as go build, Bazel, Buck, Source Graph, and godoc, allowing checkers from diverse sources to be reused across command-line tools, editors, build systems, and code review pipelines.