analysisflags

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

Package analysisflags defines helpers for processing flags (-help, -json, -fix, -diff, etc) common to unitchecker and {single,multi}checker. It is not intended for broader use.

Index

Constants

This section is empty.

Variables

View Source
var (
	JSON    = false // -json
	Context = -1    // -c=N: if N>0, display offending line plus N lines of context
	Fix     bool    // -fix

)

flags common to all {single,multi,unit}checkers.

Functions

func ApplyFixes added in v0.37.0

func ApplyFixes(actions []FixAction, verbose bool) error

ApplyFixes attempts to apply the first suggested fix associated with each diagnostic reported by the specified actions. All fixes must have been validated by analysisinternal.ValidateFixes.

Each fix is treated as an independent change; fixes are merged in an arbitrary deterministic order as if by a three-way diff tool such as the UNIX diff3 command or 'git merge'. Any fix that cannot be cleanly merged is discarded, in which case the final summary tells the user to re-run the tool. TODO(adonovan): make the checker tool re-run the analysis itself.

When the same file is analyzed as a member of both a primary package "p" and a test-augmented package "p [p.test]", there may be duplicate diagnostics and fixes. One set of fixes will be applied and the other will be discarded; but re-running the tool may then show zero fixes, which may cause the confused user to wonder what happened to the other ones. TODO(adonovan): consider pre-filtering completely identical fixes.

A common reason for overlapping fixes is duplicate additions of the same import. The merge algorithm may often cleanly resolve such fixes, coalescing identical edits, but the merge may sometimes be confused by nearby changes.

Even when merging succeeds, there is no guarantee that the composition of the two fixes is semantically correct. Coalescing identical edits is appropriate for imports, but not for, say, increments to a counter variable; the correct resolution in that case might be to increment it twice. Or consider two fixes that each delete the penultimate reference to an import or local variable: each fix is sound individually, and they may be textually distant from each other, but when both are applied, the program is no longer valid because it has an unreferenced import or local variable. TODO(adonovan): investigate replacing the final "gofmt" step with a formatter that applies the unused-import deletion logic of "goimports".

Merging depends on both the order of fixes and they order of edits within them. For example, if three fixes add import "a" twice and import "b" once, the two imports of "a" may be combined if they appear in order [a, a, b], or not if they appear as [a, b, a]. TODO(adonovan): investigate an algebraic approach to imports; that is, for fixes to Go source files, convert changes within the import(...) portion of the file into semantic edits, compose those edits algebraically, then convert the result back to edits.

applyFixes returns success if all fixes are valid, could be cleanly merged, and the corresponding files were successfully updated.

If the -diff flag was set, instead of updating the files it display the final patch composed of all the cleanly merged fixes.

TODO(adonovan): handle file-system level aliases such as symbolic links using robustio.FileID.

func Help

func Help(progname string, analyzers []*analysis.Analyzer, args []string)

Help implements the help subcommand for a multichecker or unitchecker style command. The optional args specify the analyzers to describe. Help calls log.Fatal if no such analyzer exists.

func Parse

func Parse(analyzers []*analysis.Analyzer, multi bool) []*analysis.Analyzer

Parse creates a flag for each of the analyzer's flags, including (in multi mode) a flag named after the analyzer, parses the flags, then filters and returns the list of analyzers enabled by flags.

The result is intended to be passed to unitchecker.Run or checker.Run. Use in unitchecker.Run will gob.Register all fact types for the returned graph of analyzers but of course not the ones only reachable from dropped analyzers. To avoid inconsistency about which gob types are registered from run to run, Parse itself gob.Registers all the facts only reachable from dropped analyzers. This is not a particularly elegant API, but this is an internal package.

func PrintPlain

func PrintPlain(out io.Writer, fset *token.FileSet, contextLines int, diag analysis.Diagnostic)

PrintPlain prints a diagnostic in plain text form. If contextLines is nonnegative, it also prints the offending line plus this many lines of context.

func ResolveURL added in v0.9.0

func ResolveURL(a *analysis.Analyzer, d analysis.Diagnostic) (string, error)

ResolveURL resolves the URL field for a Diagnostic from an Analyzer and returns the URL. See Diagnostic.URL for details.

Types

type FixAction added in v0.37.0

type FixAction struct {
	Name         string // e.g. "analyzer@package"
	FileSet      *token.FileSet
	ReadFileFunc analysisinternal.ReadFileFunc
	Diagnostics  []analysis.Diagnostic
}

FixAction abstracts a checker action (running one analyzer on one package) for the purposes of applying its diagnostics' fixes.

type JSONDiagnostic added in v0.2.0

type JSONDiagnostic struct {
	Category       string                   `json:"category,omitempty"`
	Posn           string                   `json:"posn"` // e.g. "file.go:line:column"
	Message        string                   `json:"message"`
	SuggestedFixes []JSONSuggestedFix       `json:"suggested_fixes,omitempty"`
	Related        []JSONRelatedInformation `json:"related,omitempty"`
}

A JSONDiagnostic describes the JSON schema of an analysis.Diagnostic.

TODO(matloob): include End position if present.

type JSONRelatedInformation added in v0.18.0

type JSONRelatedInformation struct {
	Posn    string `json:"posn"` // e.g. "file.go:line:column"
	Message string `json:"message"`
}

A JSONRelated describes a secondary position and message related to a primary diagnostic.

TODO(adonovan): include End position if present.

type JSONSuggestedFix added in v0.2.0

type JSONSuggestedFix struct {
	Message string         `json:"message"`
	Edits   []JSONTextEdit `json:"edits"`
}

A JSONSuggestedFix describes an edit that should be applied as a whole or not at all. It might contain multiple TextEdits/text_edits if the SuggestedFix consists of multiple non-contiguous edits.

type JSONTextEdit added in v0.2.0

type JSONTextEdit struct {
	Filename string `json:"filename"`
	Start    int    `json:"start"`
	End      int    `json:"end"`
	New      string `json:"new"`
}

A TextEdit describes the replacement of a portion of a file. Start and End are zero-based half-open indices into the original byte sequence of the file, and New is the new text.

type JSONTree

type JSONTree map[string]map[string]any

A JSONTree is a mapping from package ID to analysis name to result. Each result is either a jsonError or a list of JSONDiagnostic.

func (JSONTree) Add

func (tree JSONTree) Add(fset *token.FileSet, id, name string, diags []analysis.Diagnostic, err error)

Add adds the result of analysis 'name' on package 'id'. The result is either a list of diagnostics or an error.

func (JSONTree) Print

func (tree JSONTree) Print(out io.Writer) error

Jump to

Keyboard shortcuts

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