You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
8.3 KiB

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