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
|
Analyzer *Analyzer // the identity of the current analyzer
|
||||||
|
|
||||||
// syntax and type information
|
// syntax and type information
|
||||||
Fset *token.FileSet // file position information
|
Fset *token.FileSet // file position information
|
||||||
Syntax []*ast.File // the abstract syntax tree of each file
|
Files []*ast.File // the abstract syntax tree of each file
|
||||||
Pkg *types.Package // type information about the package
|
Pkg *types.Package // type information about the package
|
||||||
Info *types.Info // type information about the syntax trees
|
TypesInfo *types.Info // type information about the syntax trees
|
||||||
|
|
||||||
// ResultOf provides the inputs to this analysis pass, which are
|
// ResultOf provides the inputs to this analysis pass, which are
|
||||||
// the corresponding results of its prerequisite analyzers.
|
// the corresponding results of its prerequisite analyzers.
|
||||||
|
@ -119,10 +119,10 @@ type Pass struct {
|
||||||
|
|
||||||
// -- outputs --
|
// -- outputs --
|
||||||
|
|
||||||
// Findings is a list of findings about specific locations
|
// Report reports a Diagnostic, a finding about a specific location
|
||||||
// in the analyzed source code, such as potential mistakes.
|
// in the analyzed source code such as a potential mistake.
|
||||||
// It is populated by the Run function.
|
// It may be called by the Run function.
|
||||||
Findings []*Finding
|
Report func(Diagnostic)
|
||||||
|
|
||||||
// ExportObjectFact associates a fact of type *T with the obj,
|
// ExportObjectFact associates a fact of type *T with the obj,
|
||||||
// replacing any previous fact of that type.
|
// replacing any previous fact of that type.
|
||||||
|
@ -140,14 +140,11 @@ type Pass struct {
|
||||||
// For example, suggested or applied refactorings.
|
// For example, suggested or applied refactorings.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Findingf is a helper function that creates a new Finding using the
|
// Reportf is a helper function that reports a Diagnostic using the
|
||||||
// specified position and formatted error message, appends it to
|
// specified position and formatted error message.
|
||||||
// pass.Findings, and returns it.
|
func (pass *Pass) Reportf(pos token.Pos, format string, args ...interface{}) {
|
||||||
func (pass *Pass) Findingf(pos token.Pos, format string, args ...interface{}) *Finding {
|
|
||||||
msg := fmt.Sprintf(format, args...)
|
msg := fmt.Sprintf(format, args...)
|
||||||
f := &Finding{Pos: pos, Message: msg}
|
pass.Report(Diagnostic{Pos: pos, Message: msg})
|
||||||
pass.Findings = append(pass.Findings, f)
|
|
||||||
return f
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pass *Pass) String() string {
|
func (pass *Pass) String() string {
|
||||||
|
@ -192,12 +189,12 @@ type Fact interface {
|
||||||
AFact() // dummy method to avoid type errors
|
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.
|
// which should be a constant, may be used to classify them.
|
||||||
// It is primarily intended to make it easy to look up documentation.
|
// It is primarily intended to make it easy to look up documentation.
|
||||||
type Finding struct {
|
type Diagnostic struct {
|
||||||
Pos token.Pos
|
Pos token.Pos
|
||||||
Category string // optional
|
Category string // optional
|
||||||
Message string
|
Message string
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// The findcall package is a trivial example and test of an analyzer of
|
// 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.
|
// method of the name specified by its --name flag.
|
||||||
package findcall
|
package findcall
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func findcall(pass *analysis.Pass) (interface{}, error) {
|
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 {
|
ast.Inspect(f, func(n ast.Node) bool {
|
||||||
if call, ok := n.(*ast.CallExpr); ok {
|
if call, ok := n.(*ast.CallExpr); ok {
|
||||||
var id *ast.Ident
|
var id *ast.Ident
|
||||||
|
@ -33,8 +33,8 @@ func findcall(pass *analysis.Pass) (interface{}, error) {
|
||||||
case *ast.SelectorExpr:
|
case *ast.SelectorExpr:
|
||||||
id = fun.Sel
|
id = fun.Sel
|
||||||
}
|
}
|
||||||
if id != nil && !pass.Info.Types[id].IsType() && id.Name == name {
|
if id != nil && !pass.TypesInfo.Types[id].IsType() && id.Name == name {
|
||||||
pass.Findingf(call.Lparen, "call of %s(...)", id.Name)
|
pass.Reportf(call.Lparen, "call of %s(...)", id.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
//
|
//
|
||||||
// {"greeting": "hello", "audience": "world"}.
|
// {"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.
|
// showing which key/value pairs it contributes.
|
||||||
package pkgfact
|
package pkgfact
|
||||||
|
|
||||||
|
@ -54,14 +54,14 @@ func run(pass *analysis.Pass) (interface{}, error) {
|
||||||
// package and accumulate its information into the result.
|
// package and accumulate its information into the result.
|
||||||
// (Warning: accumulation leads to quadratic growth of work.)
|
// (Warning: accumulation leads to quadratic growth of work.)
|
||||||
doImport := func(spec *ast.ImportSpec) {
|
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
|
var fact pairsFact
|
||||||
if pass.ImportPackageFact(pkg, &fact) {
|
if pass.ImportPackageFact(pkg, &fact) {
|
||||||
for _, pair := range fact {
|
for _, pair := range fact {
|
||||||
eq := strings.IndexByte(pair, '=')
|
eq := strings.IndexByte(pair, '=')
|
||||||
result[pair[:eq]] = pair[1+eq:]
|
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
|
name := spec.Names[i].Name
|
||||||
if strings.HasPrefix(name, "_") {
|
if strings.HasPrefix(name, "_") {
|
||||||
key := name[1:]
|
key := name[1:]
|
||||||
value := pass.Info.Types[spec.Values[i]].Value.String()
|
value := pass.TypesInfo.Types[spec.Values[i]].Value.String()
|
||||||
result[key] = value
|
result[key] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, f := range pass.Syntax {
|
for _, f := range pass.Files {
|
||||||
for _, decl := range f.Decls {
|
for _, decl := range f.Decls {
|
||||||
if decl, ok := decl.(*ast.GenDecl); ok {
|
if decl, ok := decl.(*ast.GenDecl); ok {
|
||||||
for _, spec := range decl.Specs {
|
for _, spec := range decl.Specs {
|
||||||
|
|
Loading…
Reference in New Issue