analysis

Imports

Imports #

"flag"
"fmt"
"go/ast"
"go/token"
"go/types"
"reflect"
"go/token"
"fmt"
"reflect"
"strings"
"unicode"

Interfaces

Fact interface #

A Fact is an intermediate fact produced during analysis. Each fact is associated with a named declaration (a types.Object) or with a package as a whole. A single object or package may have multiple associated facts, but only one of any particular fact type. A Fact represents a predicate such as "never returns", but does not represent the subject of the predicate such as "function F" or "package P". Facts may be produced in one analysis pass and consumed by another analysis pass even if these are in different address spaces. If package P imports Q, all facts about Q produced during analysis of that package will be available during later analysis of P. Facts are analogous to type export data in a build system: just as export data enables separate compilation of several passes, facts enable "separate analysis". Each pass (a, p) starts with the set of facts produced by the same analyzer a applied to the packages directly imported by p. The analysis may add facts to the set, and they may be exported in turn. An analysis's Run function may retrieve facts by calling Pass.Import{Object,Package}Fact and update them using Pass.Export{Object,Package}Fact. A fact is logically private to its Analysis. To pass values between different analyzers, use the results mechanism; see Analyzer.Requires, Analyzer.ResultType, and Pass.ResultOf. A Fact type must be a pointer. Facts are encoded and decoded using encoding/gob. A Fact may implement the GobEncoder/GobDecoder interfaces to customize its encoding. Fact encoding should not fail. A Fact should not be modified once exported.

type Fact interface {
AFact()
}

Range interface #

The Range interface provides a range. It's equivalent to and satisfied by ast.Node.

type Range interface {
Pos() token.Pos
End() token.Pos
}

Structs

Analyzer struct #

An Analyzer describes an analysis function and its options.

type Analyzer struct {
Name string
Doc string
URL string
Flags flag.FlagSet
Run func(*Pass) (interface{}, error)
RunDespiteErrors bool
Requires []*Analyzer
ResultType reflect.Type
FactTypes []Fact
}

CycleInRequiresGraphError struct #

type CycleInRequiresGraphError struct {
AnalyzerNames map[string]bool
}

Diagnostic struct #

A Diagnostic is a message associated with a source location or range. An Analyzer may return a variety of diagnostics; the optional Category, which should be a constant, may be used to classify them. It is primarily intended to make it easy to look up documentation. All Pos values are interpreted relative to Pass.Fset. If End is provided, the diagnostic is specified to apply to the range between Pos and End.

type Diagnostic struct {
Pos token.Pos
End token.Pos
Category string
Message string
URL string
SuggestedFixes []SuggestedFix
Related []RelatedInformation
}

Module struct #

A Module describes the module to which a package belongs.

type Module struct {
Path string
Version string
GoVersion string
}

ObjectFact struct #

ObjectFact is an object together with an associated fact.

type ObjectFact struct {
Object types.Object
Fact Fact
}

PackageFact struct #

PackageFact is a package together with an associated fact.

type PackageFact struct {
Package *types.Package
Fact Fact
}

Pass struct #

A Pass provides information to the Run function that applies a specific analyzer to a single Go package. It forms the interface between the analysis logic and the driver program, and has both input and an output components. As in a compiler, one pass may depend on the result computed by another. The Run function should not call any of the Pass functions concurrently.

type Pass struct {
Analyzer *Analyzer
Fset *token.FileSet
Files []*ast.File
OtherFiles []string
IgnoredFiles []string
Pkg *types.Package
TypesInfo *types.Info
TypesSizes types.Sizes
TypeErrors []types.Error
Module *Module
Report func(Diagnostic)
ResultOf map[*Analyzer]interface{}
ReadFile func(filename string) ([]byte, error)
ImportObjectFact func(obj types.Object, fact Fact) bool
ImportPackageFact func(pkg *types.Package, fact Fact) bool
ExportObjectFact func(obj types.Object, fact Fact)
ExportPackageFact func(fact Fact)
AllPackageFacts func() []PackageFact
AllObjectFacts func() []ObjectFact
}

RelatedInformation struct #

RelatedInformation contains information related to a diagnostic. For example, a diagnostic that flags duplicated declarations of a variable may include one RelatedInformation per existing declaration.

type RelatedInformation struct {
Pos token.Pos
End token.Pos
Message string
}

SuggestedFix struct #

A SuggestedFix is a code change associated with a Diagnostic that a user can choose to apply to their code. Usually the SuggestedFix is meant to fix the issue flagged by the diagnostic. The TextEdits must not overlap, nor contain edits for other packages.

type SuggestedFix struct {
Message string
TextEdits []TextEdit
}

TextEdit struct #

A TextEdit represents the replacement of the code between Pos and End with the new text. Each TextEdit should apply to a single file. End should not be earlier in the file than Pos.

type TextEdit struct {
Pos token.Pos
End token.Pos
NewText []byte
}

Functions

Error method #

func (e *CycleInRequiresGraphError) Error() string

ReportRangef method #

ReportRangef is a helper function that reports a Diagnostic using the range provided. ast.Node values can be passed in as the range because they satisfy the Range interface.

func (pass *Pass) ReportRangef(rng Range, format string, args ...interface{})

Reportf method #

Reportf is a helper function that reports a Diagnostic using the specified position and formatted error message.

func (pass *Pass) Reportf(pos token.Pos, format string, args ...interface{})

String method #

func (a *Analyzer) String() string

String method #

func (pass *Pass) String() string

Validate function #

Validate reports an error if any of the analyzers are misconfigured. Checks include: that the name is a valid identifier; that the Doc is not empty; that the Run is non-nil; that the Requires graph is acyclic; that analyzer fact types are unique; that each fact type is a pointer. Analyzer names need not be unique, though this may be confusing.

func Validate(analyzers []*Analyzer) error

validIdent function #

func validIdent(name string) bool

Generated with Arrow