github.com/bir3/gocompiler@v0.9.2202/src/xvendor/golang.org/x/tools/go/analysis/analysis.go (about)

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