mirror of
https://github.com/golang/go
synced 2024-11-18 14:54:40 -07:00
2da0720e4f
Visible changes: - "referrers" queries now emit a stream of results, so they start appearing quickly even in large queries. We no longer report the total number of matches. - packageReferrers now also uses AfterTypeCheck hook and streaming. - XML support has been dropped. - The -format flag has been replaced by -json. JSON protocol changes: - The enclosing Result struct has been removed. - Likewise the 'mode' field (since the caller knows it already) - "freevars" and "referrers" now emit a stream of objects In the case of referrers, the first object has a different from the rest. - The "referrers" results include the text of the matching line (parity with -json=false) Implementation details: - the concurrency-safe q.Output function can be called many times, each with a queryResult to print. - fset is no longer saved in Query (cleaner) - queryResult methods renamed PrintPlain, JSON Change-Id: I41a4e3f57f266fcf043ece4045bca82c6f6a356f Reviewed-on: https://go-review.googlesource.com/21397 Reviewed-by: Michael Matloob <matloob@golang.org>
100 lines
2.2 KiB
Go
100 lines
2.2 KiB
Go
// Copyright 2013 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"go/ast"
|
|
"go/token"
|
|
|
|
"golang.org/x/tools/cmd/guru/serial"
|
|
"golang.org/x/tools/go/loader"
|
|
)
|
|
|
|
// definition reports the location of the definition of an identifier.
|
|
func definition(q *Query) error {
|
|
// First try the simple resolution done by parser.
|
|
// It only works for intra-file references but it is very fast.
|
|
// (Extending this approach to all the files of the package,
|
|
// resolved using ast.NewPackage, was not worth the effort.)
|
|
{
|
|
qpos, err := fastQueryPos(q.Build, q.Pos)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
id, _ := qpos.path[0].(*ast.Ident)
|
|
if id == nil {
|
|
return fmt.Errorf("no identifier here")
|
|
}
|
|
|
|
if obj := id.Obj; obj != nil && obj.Pos().IsValid() {
|
|
q.Output(qpos.fset, &definitionResult{
|
|
pos: obj.Pos(),
|
|
descr: fmt.Sprintf("%s %s", obj.Kind, obj.Name),
|
|
})
|
|
return nil // success
|
|
}
|
|
}
|
|
|
|
// Run the type checker.
|
|
lconf := loader.Config{Build: q.Build}
|
|
allowErrors(&lconf)
|
|
|
|
if _, err := importQueryPackage(q.Pos, &lconf); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Load/parse/type-check the program.
|
|
lprog, err := lconf.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
qpos, err := parseQueryPos(lprog, q.Pos, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
id, _ := qpos.path[0].(*ast.Ident)
|
|
if id == nil {
|
|
return fmt.Errorf("no identifier here")
|
|
}
|
|
|
|
obj := qpos.info.ObjectOf(id)
|
|
if obj == nil {
|
|
// Happens for y in "switch y := x.(type)",
|
|
// and the package declaration,
|
|
// but I think that's all.
|
|
return fmt.Errorf("no object for identifier")
|
|
}
|
|
|
|
if !obj.Pos().IsValid() {
|
|
return fmt.Errorf("%s is built in", obj.Name())
|
|
}
|
|
|
|
q.Output(lprog.Fset, &definitionResult{
|
|
pos: obj.Pos(),
|
|
descr: qpos.objectString(obj),
|
|
})
|
|
return nil
|
|
}
|
|
|
|
type definitionResult struct {
|
|
pos token.Pos // (nonzero) location of definition
|
|
descr string // description of object it denotes
|
|
}
|
|
|
|
func (r *definitionResult) PrintPlain(printf printfFunc) {
|
|
printf(r.pos, "defined here as %s", r.descr)
|
|
}
|
|
|
|
func (r *definitionResult) JSON(fset *token.FileSet) []byte {
|
|
return toJSON(&serial.Definition{
|
|
Desc: r.descr,
|
|
ObjPos: fset.Position(r.pos).String(),
|
|
})
|
|
}
|