2013-09-10 12:11:42 -06:00
|
|
|
// 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 oracle
|
|
|
|
|
|
|
|
import (
|
2015-03-26 12:07:29 -06:00
|
|
|
"bytes"
|
2013-09-25 12:34:39 -06:00
|
|
|
"fmt"
|
2013-09-10 12:11:42 -06:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2015-03-26 12:07:29 -06:00
|
|
|
"io/ioutil"
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
"os"
|
2013-09-10 12:11:42 -06:00
|
|
|
"sort"
|
|
|
|
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
"golang.org/x/tools/go/loader"
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/go/types"
|
|
|
|
"golang.org/x/tools/oracle/serial"
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
"golang.org/x/tools/refactor/importgraph"
|
2013-09-10 12:11:42 -06:00
|
|
|
)
|
|
|
|
|
2015-03-26 12:07:29 -06:00
|
|
|
// TODO(adonovan): use golang.org/x/tools/refactor/importgraph to choose
|
|
|
|
// the scope automatically.
|
|
|
|
|
2013-09-10 12:11:42 -06:00
|
|
|
// Referrers reports all identifiers that resolve to the same object
|
|
|
|
// as the queried identifier, within any package in the analysis scope.
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
func referrers(q *Query) error {
|
|
|
|
lconf := loader.Config{Build: q.Build}
|
|
|
|
allowErrors(&lconf)
|
|
|
|
|
|
|
|
if err := importQueryPackage(q.Pos, &lconf); err != nil {
|
|
|
|
return err
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
var id *ast.Ident
|
|
|
|
var obj types.Object
|
|
|
|
var lprog *loader.Program
|
|
|
|
var pass2 bool
|
|
|
|
for {
|
|
|
|
// Load/parse/type-check the program.
|
|
|
|
var err error
|
|
|
|
lprog, err = lconf.Load()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
q.Fset = lprog.Fset
|
|
|
|
|
|
|
|
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)", but I think that's all.
|
|
|
|
return fmt.Errorf("no object for identifier")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the identifier is exported, we must load all packages that
|
|
|
|
// depend transitively upon the package that defines it.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): opt: skip this step if obj.Pkg() is a test or
|
|
|
|
// main package.
|
|
|
|
if pass2 || !obj.Exported() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan the workspace and build the import graph.
|
|
|
|
_, rev, errors := importgraph.Build(q.Build)
|
|
|
|
if len(errors) > 0 {
|
|
|
|
for path, err := range errors {
|
|
|
|
fmt.Fprintf(os.Stderr, "Package %q: %s.\n", path, err)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("failed to scan import graph for workspace")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-load the larger program.
|
|
|
|
// Create a new file set so that ...
|
|
|
|
// External test packages are never imported,
|
|
|
|
// so they will never appear in the graph.
|
|
|
|
// (We must reset the Config here, not just reset the Fset field.)
|
|
|
|
lconf = loader.Config{
|
|
|
|
Fset: token.NewFileSet(),
|
|
|
|
Build: q.Build,
|
|
|
|
}
|
|
|
|
allowErrors(&lconf)
|
|
|
|
for path := range rev.Search(obj.Pkg().Path()) {
|
|
|
|
lconf.Import(path)
|
|
|
|
}
|
|
|
|
pass2 = true
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
|
2014-02-27 11:21:59 -07:00
|
|
|
// Iterate over all go/types' Uses facts for the entire program.
|
2013-12-13 08:04:55 -07:00
|
|
|
var refs []*ast.Ident
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
for _, info := range lprog.AllPackages {
|
2014-02-27 11:21:59 -07:00
|
|
|
for id2, obj2 := range info.Uses {
|
2013-09-13 10:52:57 -06:00
|
|
|
if sameObj(obj, obj2) {
|
2013-12-13 08:04:55 -07:00
|
|
|
refs = append(refs, id2)
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
// TODO(adonovan): is this sort stable? Pos order depends on
|
|
|
|
// when packages are reached. Use filename order?
|
2013-12-13 08:04:55 -07:00
|
|
|
sort.Sort(byNamePos(refs))
|
2013-09-10 12:11:42 -06:00
|
|
|
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
q.result = &referrersResult{
|
|
|
|
fset: q.Fset,
|
2013-12-13 08:04:55 -07:00
|
|
|
query: id,
|
2013-09-10 12:11:42 -06:00
|
|
|
obj: obj,
|
|
|
|
refs: refs,
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
}
|
|
|
|
return nil
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
|
2013-09-13 10:52:57 -06:00
|
|
|
// same reports whether x and y are identical, or both are PkgNames
|
2014-09-02 16:12:08 -06:00
|
|
|
// that import the same Package.
|
2013-09-10 12:11:42 -06:00
|
|
|
//
|
2013-09-13 10:52:57 -06:00
|
|
|
func sameObj(x, y types.Object) bool {
|
|
|
|
if x == y {
|
|
|
|
return true
|
|
|
|
}
|
2014-09-02 16:12:08 -06:00
|
|
|
if x, ok := x.(*types.PkgName); ok {
|
|
|
|
if y, ok := y.(*types.PkgName); ok {
|
|
|
|
return x.Imported() == y.Imported()
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
}
|
2013-09-13 10:52:57 -06:00
|
|
|
return false
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
|
2013-12-13 08:04:55 -07:00
|
|
|
// -------- utils --------
|
|
|
|
|
|
|
|
type byNamePos []*ast.Ident
|
|
|
|
|
|
|
|
func (p byNamePos) Len() int { return len(p) }
|
|
|
|
func (p byNamePos) Less(i, j int) bool { return p[i].NamePos < p[j].NamePos }
|
|
|
|
func (p byNamePos) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
|
|
2013-09-10 12:11:42 -06:00
|
|
|
type referrersResult struct {
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
fset *token.FileSet
|
2014-05-02 15:38:08 -06:00
|
|
|
query *ast.Ident // identifier of query
|
2013-09-10 12:11:42 -06:00
|
|
|
obj types.Object // object it denotes
|
2013-12-13 08:04:55 -07:00
|
|
|
refs []*ast.Ident // set of all other references to it
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *referrersResult) display(printf printfFunc) {
|
2015-03-26 12:07:29 -06:00
|
|
|
printf(r.obj, "%d references to %s", len(r.refs), r.obj)
|
|
|
|
|
|
|
|
// Show referring lines, like grep.
|
|
|
|
type fileinfo struct {
|
|
|
|
refs []*ast.Ident
|
|
|
|
linenums []int // line number of refs[i]
|
|
|
|
data chan []byte // file contents
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
2015-03-26 12:07:29 -06:00
|
|
|
var fileinfos []*fileinfo
|
|
|
|
fileinfosByName := make(map[string]*fileinfo)
|
|
|
|
|
|
|
|
// First pass: start the file reads concurrently.
|
2013-09-10 12:11:42 -06:00
|
|
|
for _, ref := range r.refs {
|
oracle: several major improvements
Features:
More robust: silently ignore type errors in modes that don't need
SSA form: describe, referrers, implements, freevars, description.
This makes the tool much more robust for everyday queries.
Less configuration: don't require a scope argument for all queries.
Only queries that do pointer analysis need it.
For the rest, the initial position is enough for
importQueryPackage to deduce the scope.
It now works for queries in GoFiles, TestGoFiles, or XTestGoFiles.
(It no longer works for ad-hoc main packages like
$GOROOT/src/net/http/triv.go)
More complete: "referrers" computes the scope automatically by
scanning the import graph of the entire workspace, using gorename's
refactor/importgraph package. This requires two passes at loading.
Faster: simplified start-up logic avoids unnecessary package loading
and SSA construction (a consequence of bad abstraction) in many
cases.
"callgraph": remove it. Unlike all the other commands it isn't
related to the current selection, and we have
golang.org/x/tools/cmdcallgraph now.
Internals:
Drop support for long-running clients (i.e., Pythia), since
godoc -analysis supports all the same features except "pointsto",
and precomputes all the results so latency is much lower.
Get rid of various unhelpful abstractions introduced to support
long-running clients. Expand out the set-up logic for each
subcommand. This is simpler, easier to read, and gives us more
control, at a small cost in duplication---the familiar story of
abstractions.
Discard PTA warnings. We weren't showing them (nor should we).
Split tests into separate directories (so that importgraph works).
Change-Id: I55d46b3ab33cdf7ac22436fcc2148fe04c901237
Reviewed-on: https://go-review.googlesource.com/8243
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 09:21:48 -06:00
|
|
|
posn := r.fset.Position(ref.Pos())
|
2015-03-26 12:07:29 -06:00
|
|
|
fi := fileinfosByName[posn.Filename]
|
|
|
|
if fi == nil {
|
|
|
|
fi = &fileinfo{data: make(chan []byte)}
|
|
|
|
fileinfosByName[posn.Filename] = fi
|
|
|
|
fileinfos = append(fileinfos, fi)
|
|
|
|
|
|
|
|
// First request for this file:
|
|
|
|
// start asynchronous read.
|
|
|
|
go func() {
|
|
|
|
content, err := ioutil.ReadFile(posn.Filename)
|
|
|
|
if err != nil {
|
|
|
|
content = []byte(fmt.Sprintf("error: %v", err))
|
|
|
|
}
|
|
|
|
fi.data <- content
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
fi.refs = append(fi.refs, ref)
|
|
|
|
fi.linenums = append(fi.linenums, posn.Line)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second pass: print refs in original order.
|
|
|
|
// One line may have several refs at different columns.
|
|
|
|
for _, fi := range fileinfos {
|
|
|
|
content := <-fi.data // wait for I/O completion
|
|
|
|
lines := bytes.Split(content, []byte("\n"))
|
|
|
|
for i, ref := range fi.refs {
|
|
|
|
printf(ref, "%s", lines[fi.linenums[i]-1])
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:04:55 -07:00
|
|
|
// TODO(adonovan): encode extent, not just Pos info, in Serial form.
|
|
|
|
|
2013-09-24 13:08:14 -06:00
|
|
|
func (r *referrersResult) toSerial(res *serial.Result, fset *token.FileSet) {
|
|
|
|
referrers := &serial.Referrers{
|
2013-12-13 08:04:55 -07:00
|
|
|
Pos: fset.Position(r.query.Pos()).String(),
|
2013-09-10 12:11:42 -06:00
|
|
|
Desc: r.obj.String(),
|
|
|
|
}
|
2013-09-13 10:52:57 -06:00
|
|
|
if pos := r.obj.Pos(); pos != token.NoPos { // Package objects have no Pos()
|
2013-09-10 12:11:42 -06:00
|
|
|
referrers.ObjPos = fset.Position(pos).String()
|
|
|
|
}
|
|
|
|
for _, ref := range r.refs {
|
2013-12-13 08:04:55 -07:00
|
|
|
referrers.Refs = append(referrers.Refs, fset.Position(ref.NamePos).String())
|
2013-09-10 12:11:42 -06:00
|
|
|
}
|
|
|
|
res.Referrers = referrers
|
|
|
|
}
|