1package analysis
2
3import (
4	"flag"
5	"fmt"
6	"go/ast"
7	"go/token"
8	"go/types"
9	"reflect"
10
11	"golang.org/x/tools/internal/analysisinternal"
12)
13
14// An Analyzer describes an analysis function and its options.
15type Analyzer struct {
16	// The Name of the analyzer must be a valid Go identifier
17	// as it may appear in command-line flags, URLs, and so on.
18	Name string
19
20	// Doc is the documentation for the analyzer.
21	// The part before the first "\n\n" is the title
22	// (no capital or period, max ~60 letters).
23	Doc string
24
25	// Flags defines any flags accepted by the analyzer.
26	// The manner in which these flags are exposed to the user
27	// depends on the driver which runs the analyzer.
28	Flags flag.FlagSet
29
30	// Run applies the analyzer to a package.
31	// It returns an error if the analyzer failed.
32	//
33	// On success, the Run function may return a result
34	// computed by the Analyzer; its type must match ResultType.
35	// The driver makes this result available as an input to
36	// another Analyzer that depends directly on this one (see
37	// Requires) when it analyzes the same package.
38	//
39	// To pass analysis results between packages (and thus
40	// potentially between address spaces), use Facts, which are
41	// serializable.
42	Run func(*Pass) (interface{}, error)
43
44	// RunDespiteErrors allows the driver to invoke
45	// the Run method of this analyzer even on a
46	// package that contains parse or type errors.
47	RunDespiteErrors bool
48
49	// Requires is a set of analyzers that must run successfully
50	// before this one on a given package. This analyzer may inspect
51	// the outputs produced by each analyzer in Requires.
52	// The graph over analyzers implied by Requires edges must be acyclic.
53	//
54	// Requires establishes a "horizontal" dependency between
55	// analysis passes (different analyzers, same package).
56	Requires []*Analyzer
57
58	// ResultType is the type of the optional result of the Run function.
59	ResultType reflect.Type
60
61	// FactTypes indicates that this analyzer imports and exports
62	// Facts of the specified concrete types.
63	// An analyzer that uses facts may assume that its import
64	// dependencies have been similarly analyzed before it runs.
65	// Facts must be pointers.
66	//
67	// FactTypes establishes a "vertical" dependency between
68	// analysis passes (same analyzer, different packages).
69	FactTypes []Fact
70}
71
72func (a *Analyzer) String() string { return a.Name }
73
74func init() {
75	// Set the analysisinternal functions to be able to pass type errors
76	// to the Pass type without modifying the go/analysis API.
77	analysisinternal.SetTypeErrors = func(p interface{}, errors []types.Error) {
78		p.(*Pass).typeErrors = errors
79	}
80	analysisinternal.GetTypeErrors = func(p interface{}) []types.Error {
81		return p.(*Pass).typeErrors
82	}
83}
84
85// A Pass provides information to the Run function that
86// applies a specific analyzer to a single Go package.
87//
88// It forms the interface between the analysis logic and the driver
89// program, and has both input and an output components.
90//
91// As in a compiler, one pass may depend on the result computed by another.
92//
93// The Run function should not call any of the Pass functions concurrently.
94type Pass struct {
95	Analyzer *Analyzer // the identity of the current analyzer
96
97	// syntax and type information
98	Fset       *token.FileSet // file position information
99	Files      []*ast.File    // the abstract syntax tree of each file
100	OtherFiles []string       // names of non-Go files of this package
101	Pkg        *types.Package // type information about the package
102	TypesInfo  *types.Info    // type information about the syntax trees
103	TypesSizes types.Sizes    // function for computing sizes of types
104
105	// Report reports a Diagnostic, a finding about a specific location
106	// in the analyzed source code such as a potential mistake.
107	// It may be called by the Run function.
108	Report func(Diagnostic)
109
110	// ResultOf provides the inputs to this analysis pass, which are
111	// the corresponding results of its prerequisite analyzers.
112	// The map keys are the elements of Analysis.Required,
113	// and the type of each corresponding value is the required
114	// analysis's ResultType.
115	ResultOf map[*Analyzer]interface{}
116
117	// -- facts --
118
119	// ImportObjectFact retrieves a fact associated with obj.
120	// Given a value ptr of type *T, where *T satisfies Fact,
121	// ImportObjectFact copies the value to *ptr.
122	//
123	// ImportObjectFact panics if called after the pass is complete.
124	// ImportObjectFact is not concurrency-safe.
125	ImportObjectFact func(obj types.Object, fact Fact) bool
126
127	// ImportPackageFact retrieves a fact associated with package pkg,
128	// which must be this package or one of its dependencies.
129	// See comments for ImportObjectFact.
130	ImportPackageFact func(pkg *types.Package, fact Fact) bool
131
132	// ExportObjectFact associates a fact of type *T with the obj,
133	// replacing any previous fact of that type.
134	//
135	// ExportObjectFact panics if it is called after the pass is
136	// complete, or if obj does not belong to the package being analyzed.
137	// ExportObjectFact is not concurrency-safe.
138	ExportObjectFact func(obj types.Object, fact Fact)
139
140	// ExportPackageFact associates a fact with the current package.
141	// See comments for ExportObjectFact.
142	ExportPackageFact func(fact Fact)
143
144	// AllPackageFacts returns a new slice containing all package facts of the analysis's FactTypes
145	// in unspecified order.
146	// WARNING: This is an experimental API and may change in the future.
147	AllPackageFacts func() []PackageFact
148
149	// AllObjectFacts returns a new slice containing all object facts of the analysis's FactTypes
150	// in unspecified order.
151	// WARNING: This is an experimental API and may change in the future.
152	AllObjectFacts func() []ObjectFact
153
154	// typeErrors contains types.Errors that are associated with the pkg.
155	typeErrors []types.Error
156
157	/* Further fields may be added in future. */
158	// For example, suggested or applied refactorings.
159}
160
161// PackageFact is a package together with an associated fact.
162// WARNING: This is an experimental API and may change in the future.
163type PackageFact struct {
164	Package *types.Package
165	Fact    Fact
166}
167
168// ObjectFact is an object together with an associated fact.
169// WARNING: This is an experimental API and may change in the future.
170type ObjectFact struct {
171	Object types.Object
172	Fact   Fact
173}
174
175// Reportf is a helper function that reports a Diagnostic using the
176// specified position and formatted error message.
177func (pass *Pass) Reportf(pos token.Pos, format string, args ...interface{}) {
178	msg := fmt.Sprintf(format, args...)
179	pass.Report(Diagnostic{Pos: pos, Message: msg})
180}
181
182// The Range interface provides a range. It's equivalent to and satisfied by
183// ast.Node.
184type Range interface {
185	Pos() token.Pos // position of first character belonging to the node
186	End() token.Pos // position of first character immediately after the node
187}
188
189// ReportRangef is a helper function that reports a Diagnostic using the
190// range provided. ast.Node values can be passed in as the range because
191// they satisfy the Range interface.
192func (pass *Pass) ReportRangef(rng Range, format string, args ...interface{}) {
193	msg := fmt.Sprintf(format, args...)
194	pass.Report(Diagnostic{Pos: rng.Pos(), End: rng.End(), Message: msg})
195}
196
197func (pass *Pass) String() string {
198	return fmt.Sprintf("%s@%s", pass.Analyzer.Name, pass.Pkg.Path())
199}
200
201// A Fact is an intermediate fact produced during analysis.
202//
203// Each fact is associated with a named declaration (a types.Object) or
204// with a package as a whole. A single object or package may have
205// multiple associated facts, but only one of any particular fact type.
206//
207// A Fact represents a predicate such as "never returns", but does not
208// represent the subject of the predicate such as "function F" or "package P".
209//
210// Facts may be produced in one analysis pass and consumed by another
211// analysis pass even if these are in different address spaces.
212// If package P imports Q, all facts about Q produced during
213// analysis of that package will be available during later analysis of P.
214// Facts are analogous to type export data in a build system:
215// just as export data enables separate compilation of several passes,
216// facts enable "separate analysis".
217//
218// Each pass (a, p) starts with the set of facts produced by the
219// same analyzer a applied to the packages directly imported by p.
220// The analysis may add facts to the set, and they may be exported in turn.
221// An analysis's Run function may retrieve facts by calling
222// Pass.Import{Object,Package}Fact and update them using
223// Pass.Export{Object,Package}Fact.
224//
225// A fact is logically private to its Analysis. To pass values
226// between different analyzers, use the results mechanism;
227// see Analyzer.Requires, Analyzer.ResultType, and Pass.ResultOf.
228//
229// A Fact type must be a pointer.
230// Facts are encoded and decoded using encoding/gob.
231// A Fact may implement the GobEncoder/GobDecoder interfaces
232// to customize its encoding. Fact encoding should not fail.
233//
234// A Fact should not be modified once exported.
235type Fact interface {
236	AFact() // dummy method to avoid type errors
237}
238