2013-12-13 08:04:55 -07: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 (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/build"
|
|
|
|
"go/token"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2015-01-08 19:32:51 -07:00
|
|
|
"golang.org/x/tools/go/ast/astutil"
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/oracle/serial"
|
2013-12-13 08:04:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// what reports all the information about the query selection that can be
|
|
|
|
// obtained from parsing only its containing source file.
|
|
|
|
// It is intended to be a very low-latency query callable from GUI
|
|
|
|
// tools, e.g. to populate a menu of options of slower queries about
|
|
|
|
// the selected location.
|
|
|
|
//
|
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 what(q *Query) error {
|
|
|
|
qpos, err := fastQueryPos(q.Pos)
|
2013-12-13 08:04:55 -07:00
|
|
|
if err != nil {
|
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 err
|
2013-12-13 08:04:55 -07: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.Fset = qpos.fset
|
2013-12-13 08:04:55 -07:00
|
|
|
|
|
|
|
// (ignore errors)
|
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
|
|
|
srcdir, importPath, _ := guessImportPath(q.Fset.File(qpos.start).Name(), q.Build)
|
2013-12-13 08:04:55 -07:00
|
|
|
|
|
|
|
// Determine which query modes are applicable to the selection.
|
|
|
|
enable := map[string]bool{
|
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
|
|
|
"describe": true, // any syntax; always enabled
|
2013-12-13 08:04:55 -07:00
|
|
|
}
|
2013-12-13 16:00:55 -07:00
|
|
|
|
|
|
|
if qpos.end > qpos.start {
|
|
|
|
enable["freevars"] = true // nonempty selection?
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:04:55 -07:00
|
|
|
for _, n := range qpos.path {
|
|
|
|
switch n := n.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
enable["definition"] = true
|
|
|
|
enable["referrers"] = true
|
2013-12-13 16:00:55 -07:00
|
|
|
enable["implements"] = true
|
2013-12-13 08:04:55 -07:00
|
|
|
case *ast.CallExpr:
|
|
|
|
enable["callees"] = true
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
enable["callers"] = true
|
|
|
|
enable["callstack"] = true
|
|
|
|
case *ast.SendStmt:
|
|
|
|
enable["peers"] = true
|
|
|
|
case *ast.UnaryExpr:
|
|
|
|
if n.Op == token.ARROW {
|
|
|
|
enable["peers"] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 16:00:55 -07:00
|
|
|
// For implements, we approximate findInterestingNode.
|
|
|
|
if _, ok := enable["implements"]; !ok {
|
|
|
|
switch n.(type) {
|
|
|
|
case *ast.ArrayType,
|
|
|
|
*ast.StructType,
|
|
|
|
*ast.FuncType,
|
|
|
|
*ast.InterfaceType,
|
|
|
|
*ast.MapType,
|
|
|
|
*ast.ChanType:
|
|
|
|
enable["implements"] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:04:55 -07:00
|
|
|
// For pointsto, we approximate findInterestingNode.
|
|
|
|
if _, ok := enable["pointsto"]; !ok {
|
|
|
|
switch n.(type) {
|
|
|
|
case ast.Stmt,
|
|
|
|
*ast.ArrayType,
|
|
|
|
*ast.StructType,
|
|
|
|
*ast.FuncType,
|
|
|
|
*ast.InterfaceType,
|
|
|
|
*ast.MapType,
|
|
|
|
*ast.ChanType:
|
|
|
|
enable["pointsto"] = false // not an expr
|
|
|
|
|
|
|
|
case ast.Expr, ast.Decl, *ast.ValueSpec:
|
|
|
|
enable["pointsto"] = true // an expr, maybe
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Comment, Field, KeyValueExpr, etc: ascend.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have an exact selection, disable modes that need one.
|
|
|
|
if !qpos.exact {
|
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
|
|
|
enable["callees"] = false
|
|
|
|
enable["pointsto"] = false
|
|
|
|
enable["whicherrs"] = false
|
|
|
|
enable["describe"] = false
|
2013-12-13 08:04:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var modes []string
|
|
|
|
for mode := range enable {
|
|
|
|
modes = append(modes, mode)
|
|
|
|
}
|
|
|
|
sort.Strings(modes)
|
|
|
|
|
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 = &whatResult{
|
|
|
|
path: qpos.path,
|
|
|
|
srcdir: srcdir,
|
|
|
|
importPath: importPath,
|
|
|
|
modes: modes,
|
|
|
|
}
|
|
|
|
return nil
|
2013-12-13 08:04:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// guessImportPath finds the package containing filename, and returns
|
|
|
|
// its source directory (an element of $GOPATH) and its import path
|
|
|
|
// relative to it.
|
|
|
|
//
|
|
|
|
// TODO(adonovan): what about _test.go files that are not part of the
|
|
|
|
// package?
|
|
|
|
//
|
|
|
|
func guessImportPath(filename string, buildContext *build.Context) (srcdir, importPath string, err error) {
|
|
|
|
absFile, err := filepath.Abs(filename)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("can't form absolute path of %s", filename)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
absFileDir := segments(filepath.Dir(absFile))
|
|
|
|
|
|
|
|
// Find the innermost directory in $GOPATH that encloses filename.
|
|
|
|
minD := 1024
|
|
|
|
for _, gopathDir := range buildContext.SrcDirs() {
|
|
|
|
absDir, err := filepath.Abs(gopathDir)
|
|
|
|
if err != nil {
|
|
|
|
continue // e.g. non-existent dir on $GOPATH
|
|
|
|
}
|
|
|
|
d := prefixLen(segments(absDir), absFileDir)
|
|
|
|
// If there are multiple matches,
|
|
|
|
// prefer the innermost enclosing directory
|
|
|
|
// (smallest d).
|
|
|
|
if d >= 0 && d < minD {
|
|
|
|
minD = d
|
|
|
|
srcdir = gopathDir
|
|
|
|
importPath = strings.Join(absFileDir[len(absFileDir)-minD:], string(os.PathSeparator))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if srcdir == "" {
|
|
|
|
err = fmt.Errorf("can't find package for file %s", filename)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func segments(path string) []string {
|
|
|
|
return strings.Split(path, string(os.PathSeparator))
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefixLen returns the length of the remainder of y if x is a prefix
|
|
|
|
// of y, a negative number otherwise.
|
|
|
|
func prefixLen(x, y []string) int {
|
|
|
|
d := len(y) - len(x)
|
|
|
|
if d >= 0 {
|
|
|
|
for i := range x {
|
|
|
|
if y[i] != x[i] {
|
|
|
|
return -1 // not a prefix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
type whatResult struct {
|
|
|
|
path []ast.Node
|
|
|
|
modes []string
|
|
|
|
srcdir string
|
|
|
|
importPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *whatResult) display(printf printfFunc) {
|
|
|
|
for _, n := range r.path {
|
|
|
|
printf(n, "%s", astutil.NodeDescription(n))
|
|
|
|
}
|
|
|
|
printf(nil, "modes: %s", r.modes)
|
|
|
|
printf(nil, "srcdir: %s", r.srcdir)
|
|
|
|
printf(nil, "import path: %s", r.importPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *whatResult) toSerial(res *serial.Result, fset *token.FileSet) {
|
|
|
|
var enclosing []serial.SyntaxNode
|
|
|
|
for _, n := range r.path {
|
|
|
|
enclosing = append(enclosing, serial.SyntaxNode{
|
|
|
|
Description: astutil.NodeDescription(n),
|
|
|
|
Start: fset.Position(n.Pos()).Offset,
|
|
|
|
End: fset.Position(n.End()).Offset,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
res.What = &serial.What{
|
|
|
|
Modes: r.modes,
|
|
|
|
SrcDir: r.srcdir,
|
|
|
|
ImportPath: r.importPath,
|
|
|
|
Enclosing: enclosing,
|
|
|
|
}
|
|
|
|
}
|