diff --git a/go/analysis/analysis.go b/go/analysis/analysis.go index b1befed7..127f1d3f 100644 --- a/go/analysis/analysis.go +++ b/go/analysis/analysis.go @@ -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 diff --git a/go/analysis/passes/findcall/findcall.go b/go/analysis/passes/findcall/findcall.go index 5955498e..28c4c62e 100644 --- a/go/analysis/passes/findcall/findcall.go +++ b/go/analysis/passes/findcall/findcall.go @@ -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 diff --git a/go/analysis/passes/pkgfact/pkgfact.go b/go/analysis/passes/pkgfact/pkgfact.go index 092b4cc6..4c98cb7a 100644 --- a/go/analysis/passes/pkgfact/pkgfact.go +++ b/go/analysis/passes/pkgfact/pkgfact.go @@ -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 {