mirror of
https://github.com/golang/go
synced 2024-10-01 01:28:32 -06:00
go/analysis: more API renamings:
Info -> TypesInfo Syntax -> Files Diagnostic -> Finding Also, diagnostics are now reported by calling Pass.Report (or Reportf). Change-Id: Id5e0745fca914699a6a64be3554049ffc02e822b Reviewed-on: https://go-review.googlesource.com/137395 Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
parent
0b24b358f4
commit
31d48d9a8c
@ -92,10 +92,10 @@ type Pass struct {
|
||||
Analyzer *Analyzer // the identity of the current analyzer
|
||||
|
||||
// syntax and type information
|
||||
Fset *token.FileSet // file position information
|
||||
Syntax []*ast.File // the abstract syntax tree of each file
|
||||
Pkg *types.Package // type information about the package
|
||||
Info *types.Info // type information about the syntax trees
|
||||
Fset *token.FileSet // file position information
|
||||
Files []*ast.File // the abstract syntax tree of each file
|
||||
Pkg *types.Package // type information about the package
|
||||
TypesInfo *types.Info // type information about the syntax trees
|
||||
|
||||
// ResultOf provides the inputs to this analysis pass, which are
|
||||
// the corresponding results of its prerequisite analyzers.
|
||||
@ -119,10 +119,10 @@ type Pass struct {
|
||||
|
||||
// -- outputs --
|
||||
|
||||
// Findings is a list of findings about specific locations
|
||||
// in the analyzed source code, such as potential mistakes.
|
||||
// It is populated by the Run function.
|
||||
Findings []*Finding
|
||||
// Report reports a Diagnostic, a finding about a specific location
|
||||
// in the analyzed source code such as a potential mistake.
|
||||
// It may be called by the Run function.
|
||||
Report func(Diagnostic)
|
||||
|
||||
// ExportObjectFact associates a fact of type *T with the obj,
|
||||
// replacing any previous fact of that type.
|
||||
@ -140,14 +140,11 @@ type Pass struct {
|
||||
// For example, suggested or applied refactorings.
|
||||
}
|
||||
|
||||
// Findingf is a helper function that creates a new Finding using the
|
||||
// specified position and formatted error message, appends it to
|
||||
// pass.Findings, and returns it.
|
||||
func (pass *Pass) Findingf(pos token.Pos, format string, args ...interface{}) *Finding {
|
||||
// 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{}) {
|
||||
msg := fmt.Sprintf(format, args...)
|
||||
f := &Finding{Pos: pos, Message: msg}
|
||||
pass.Findings = append(pass.Findings, f)
|
||||
return f
|
||||
pass.Report(Diagnostic{Pos: pos, Message: msg})
|
||||
}
|
||||
|
||||
func (pass *Pass) String() string {
|
||||
@ -192,12 +189,12 @@ type Fact interface {
|
||||
AFact() // dummy method to avoid type errors
|
||||
}
|
||||
|
||||
// A Finding is a message associated with a source location.
|
||||
// A Diagnostic is a message associated with a source location.
|
||||
//
|
||||
// An Analyzer may return a variety of findings; the optional Category,
|
||||
// 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.
|
||||
type Finding struct {
|
||||
type Diagnostic struct {
|
||||
Pos token.Pos
|
||||
Category string // optional
|
||||
Message string
|
||||
|
@ -1,5 +1,5 @@
|
||||
// The findcall package is a trivial example and test of an analyzer of
|
||||
// Go source code. It reports a finding for every call to a function or
|
||||
// Go source code. It reports a diagnostic for every call to a function or
|
||||
// method of the name specified by its --name flag.
|
||||
package findcall
|
||||
|
||||
@ -23,7 +23,7 @@ func init() {
|
||||
}
|
||||
|
||||
func findcall(pass *analysis.Pass) (interface{}, error) {
|
||||
for _, f := range pass.Syntax {
|
||||
for _, f := range pass.Files {
|
||||
ast.Inspect(f, func(n ast.Node) bool {
|
||||
if call, ok := n.(*ast.CallExpr); ok {
|
||||
var id *ast.Ident
|
||||
@ -33,8 +33,8 @@ func findcall(pass *analysis.Pass) (interface{}, error) {
|
||||
case *ast.SelectorExpr:
|
||||
id = fun.Sel
|
||||
}
|
||||
if id != nil && !pass.Info.Types[id].IsType() && id.Name == name {
|
||||
pass.Findingf(call.Lparen, "call of %s(...)", id.Name)
|
||||
if id != nil && !pass.TypesInfo.Types[id].IsType() && id.Name == name {
|
||||
pass.Reportf(call.Lparen, "call of %s(...)", id.Name)
|
||||
}
|
||||
}
|
||||
return true
|
||||
|
@ -15,7 +15,7 @@
|
||||
//
|
||||
// {"greeting": "hello", "audience": "world"}.
|
||||
//
|
||||
// In addition, the analysis reports a finding at each import
|
||||
// In addition, the analysis reports a diagnostic at each import
|
||||
// showing which key/value pairs it contributes.
|
||||
package pkgfact
|
||||
|
||||
@ -54,14 +54,14 @@ func run(pass *analysis.Pass) (interface{}, error) {
|
||||
// package and accumulate its information into the result.
|
||||
// (Warning: accumulation leads to quadratic growth of work.)
|
||||
doImport := func(spec *ast.ImportSpec) {
|
||||
pkg := pass.Info.Defs[spec.Name].(*types.PkgName).Imported()
|
||||
pkg := pass.TypesInfo.Defs[spec.Name].(*types.PkgName).Imported()
|
||||
var fact pairsFact
|
||||
if pass.ImportPackageFact(pkg, &fact) {
|
||||
for _, pair := range fact {
|
||||
eq := strings.IndexByte(pair, '=')
|
||||
result[pair[:eq]] = pair[1+eq:]
|
||||
}
|
||||
pass.Findingf(spec.Pos(), "%s", strings.Join(fact, " "))
|
||||
pass.Reportf(spec.Pos(), "%s", strings.Join(fact, " "))
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,14 +72,14 @@ func run(pass *analysis.Pass) (interface{}, error) {
|
||||
name := spec.Names[i].Name
|
||||
if strings.HasPrefix(name, "_") {
|
||||
key := name[1:]
|
||||
value := pass.Info.Types[spec.Values[i]].Value.String()
|
||||
value := pass.TypesInfo.Types[spec.Values[i]].Value.String()
|
||||
result[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, f := range pass.Syntax {
|
||||
for _, f := range pass.Files {
|
||||
for _, decl := range f.Decls {
|
||||
if decl, ok := decl.(*ast.GenDecl); ok {
|
||||
for _, spec := range decl.Specs {
|
||||
|
Loading…
Reference in New Issue
Block a user