1
0
mirror of https://github.com/golang/go synced 2024-10-01 08:28:43 -06:00
Commit Graph

33 Commits

Author SHA1 Message Date
Alan Donovan
997b3545fd go/types: change {Type,Object,Selection}String to accept a Qualifier function
The optional Qualifier function determines what prefix to attach to
package-level names, enabling clients to qualify packages in different
ways, for example, using only the package name instead of its complete
path, or using the locally appropriate name for package given a set of
(possibly renaming) imports.

Prior to this change, clients wanting this behavior had to copy
hundreds of lines of complex printing logic.

Fun fact: (*types.Package).Path and (*types.Package).Name are valid
Qualifier functions.

We provide the RelativeTo helper function to create Qualifiers so that
the old behavior remains a one-liner.

Fixes golang/go#11133

Change-Id: Ibd63f639c7b3aa1738826d6165f2d810efeb8293
Reviewed-on: https://go-review.googlesource.com/11692
Reviewed-by: Robert Griesemer <gri@golang.org>
2015-06-30 19:00:00 +00:00
Alan Donovan
b28839e4bd 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 19:21:37 +00:00
Alan Donovan
264bffc00c oracle: when 'implements' is invoked on a method, show related methods, not types.
Fixes #9972

Change-Id: I25b65a64dcc4d551be3db8566783a9d23d410a2e
Reviewed-on: https://go-review.googlesource.com/5860
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-02-25 22:38:52 +00:00
Peter Collingbourne
4f8578d2c0 astutil: move to go/ast/astutil
Change-Id: I9a45bfc07613eb2210081d306d71f0a4d152eda5
Reviewed-on: https://go-review.googlesource.com/2592
Reviewed-by: Alan Donovan <adonovan@google.com>
2015-01-13 15:47:47 +00:00
Alan Donovan
b8d26f5b94 tools: minor comment fixes.
LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/173170043
2014-11-13 12:34:25 -05:00
Andrew Gerrand
5ebbcd132f go.tools: use golang.org/x/... import paths
Rewrite performed with this command:
  sed -i '' 's_code.google.com/p/go\._golang.org/x/_g' \
    $(grep -lr 'code.google.com/p/go.' *)

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/170920043
2014-11-10 08:50:40 +11:00
Alan Donovan
95bd0c4fdf go.tools/go/types: add (*PkgName).ImportedPackage method.
It returns the value formerly returned by Pkg(), i.e. the imported package.
Pkg() now returns the package enclosing the import statement,
which is consistent with all other Objects.

Fixes golang/go#8628.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/136090043
2014-09-02 18:12:08 -04:00
Alan Donovan
f2db24a319 go.tools/go/loader: use new types.TypeAndValue mode predicates.
PackageInfo:
- deleted IsType
- inlined + deleted: ValueOf, TypeCaseVar, ImportSpecPkg
- on failure, TypeOf accessor now returns nil (was: panic)

go/ssa: avoid extra map lookups by using Uses or Defs directly when safe to do so,
and keeping the TypeAndValue around in expr0().

LGTM=gri
R=gri, pcc
CC=golang-codereviews
https://golang.org/cl/107650043
2014-07-11 10:50:09 +01:00
Alan Donovan
b3970ee159 go.tools/go/oracle: show import path (not just name) when describing an import.
+ test.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/88190044
2014-04-15 15:37:44 -04:00
Alan Donovan
03ca00ddd4 go.tools/go/types/typeutil: new package for type utilities.
Contains the members formerly known as:
- ssa.IntuitiveMethodSet
- typemap.M (now: Map)

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/65670043
2014-02-19 13:32:36 -05:00
Alan Donovan
1f29e74bfa go.tools/go/types: remove Type.MethodSet() method.
Method-set caching is now performed externally using a MethodSetCache (if desired), not by the Types themselves.

This a minor deoptimization due to the extra maps, but avoids a situation in which method-sets are computed and frozen prematurely. (See b/7114)

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/61430045
2014-02-11 16:49:27 -05:00
Alan Donovan
3fc0fc1310 go.tools: rename packages.
Was:		Now:
ssa		go/ssa
importer	go/loader
pointer		go/pointer

Next CL: call -> go/callgraph (requires more care)

R=gri, crawshaw
CC=golang-codereviews
https://golang.org/cl/52960043
2014-01-16 09:33:58 -05:00
Alan Donovan
f119874203 go.tools/oracle: improvements to command set and performance.
Command set:
- what: an extremely fast query that parses a single
  file and returns the AST stack, package name and the
  set of query modes that apply to the current selection.
  Intended for GUI tools that need to grey out UI elements.
- definition: shows the definition of an identifier.
- pointsto: the PTA features of 'describe' have been split
  out into their own command.
- describe: with PTA stripped out, the cost is now bounded by
  type checking.

Performance:
- The importer.Config.TypeCheckFuncBodies predicate supports
  setting the 'IgnoreFuncBodies' typechecker flag on a
  per-package basis.  This means we can load dependencies from
  source more quickly if we only need exported types.
  (We avoid gcimport data because it may be absent or stale.)
  This also means we can run type-based queries on packages
  that aren't part of the pointer analysis scope. (Yay.)
- Modes that require only type analysis of the query package
  run a "what" query first, and restrict their analysis scope
  to just that package and its dependencies (sans func
  bodies), making them much faster.
- We call newOracle not oracle.New in Query, so that the
  'needs' bitset isn't ignored (oops!).  This makes the
  non-PTA queries faster.

Also:
- removed vestigial timers junk.
- pos.go: existing position utilties split out into own file.
  Added parsePosFlag utility.
- numerous cosmetic tweaks.

+ very basic tests.

To do in follow-ups:
- sophisticated editor integration of "what".
- better tests.
- refactoring of control flow as described in comment.
- changes to "implements", "describe" commands.
- update design doc + user manual.

R=crawshaw, dominik.honnef
CC=golang-dev, gri
https://golang.org/cl/40630043
2013-12-13 10:04:55 -05:00
Alan Donovan
fb3d862cae go.tools/importer: move PathEnclosingInterval to package astutil.
R=crawshaw
CC=golang-dev
https://golang.org/cl/38650044
2013-12-09 09:36:29 -05:00
Alan Donovan
7f8168b1d4 go.tools/pointer: allow clients to request both pts(v) and pts(*v) in the same analysis.
Also: add (ptset).String().

R=crawshaw
CC=golang-dev
https://golang.org/cl/36800044
2013-12-05 22:30:42 -05:00
Alan Donovan
2e33158b60 go.tools/oracle: show size and alignment for structs and named types.
Users should be familiar with the sizes of all other types.

Currently we assume amd64 (as do other parts of the oracle,
e.g. go/build tags).  Will parameterize later.

R=crawshaw
CC=golang-dev, gri
https://golang.org/cl/29710043
2013-11-20 16:00:23 -05:00
Alan Donovan
9c112540f6 go.tools/oracle: use SelectionString when printing methods.
R=gri, crawshaw, gri
CC=golang-dev
https://golang.org/cl/26900043
2013-11-15 12:35:11 -05:00
Alan Donovan
f488a2c4f5 go.tools/oracle: use TypeString, ObjectString to print relative (unqualified) names when a package is implied by the context.
+ a couple more tests.

R=gri, crawshaw
CC=golang-dev
https://golang.org/cl/26370046
2013-11-15 09:22:16 -05:00
Robert Griesemer
27563ff576 go.tools/go/types: move gcimporter to its own package
- fixed a couple of TODOs
- various cleanups along the way
- adjusted clients

Once submitted, clients of go/types that don't explicitly
specify Config.Import will need to add the extra import:

import _ "code.google.com/p/go.tools/go/gcimporter"

to install the default (gc) importer in go/types.

R=adonovan, gri
CC=golang-dev
https://golang.org/cl/26390043
2013-11-14 14:11:43 -08:00
Alan Donovan
9f640c2abb go.tools/ssa: record lvalue/rvalue distinction precisely in DebugRef.
A DebugRef associates a source expression E with an ssa.Value
V, but until now did not record whether V was the value or the
address of E.  So, we would guess from the "pointerness" of
the Value, leading to confusion in some cases, e.g.

   type N *N
   var n N
   n = &n  // lvalue and rvalue are both pointers

Now we explicitly record 'IsAddress bool' in DebugRef, and
plumb this everywhere: through (*Function).ValueForExpr and
(*Program).VarValue, all the way to forming the pointer
analysis query.

Also:
- VarValue now treats each reference to a global distinctly,
  just like it does for other vars.  So:
    var g int
    func f() {
   	g = 1     // VarValue(g) == Const(1:int), !isAddress
        print(g)  // VarValue(g) == Global(g), isAddress
    }
- DebugRefs are not emitted for references to predeclared
  identifiers (nil, built-in).
- DebugRefs no longer prevent lifting of an Alloc var into a
  register; now we update or discard the debug info.
- TestValueForExpr: improve coverage of ssa.EnclosingFunction
  by putting expectations in methods and init funcs, not just
  normal funcs.
- oracle: fix golden file broken by recent
  (*types.Var).IsField change.

R=gri
CC=golang-dev
https://golang.org/cl/16610045
2013-10-24 18:31:50 -04:00
Alan Donovan
785cfaa938 go.tools/pointer: use new callgraph API.
Also: pointer.Analyze now returns a pointer.Result object,
containing the callgraph and the results of ssa.Value queries.

The oracle has been updated to use the new call and pointer APIs.

R=crawshaw, gri
CC=golang-dev
https://golang.org/cl/13915043
2013-09-25 17:17:42 -04:00
Alan Donovan
3a4c0462d8 go.tools/oracle: change -mode argument into subcommand.
e.g. "oracle callgraph <package>"

Also: simplified error handling.
Eliminated oracle.errorf because it prepends "file:line:col: "
to the error message so the main function can't safely prepend "Error: ".
The position wasn't interesting though: it was just -pos, more or less.

R=crawshaw, dominik.honnef, r
CC=golang-dev
https://golang.org/cl/13864044
2013-09-25 14:34:39 -04:00
Alan Donovan
37f76edde8 go.tools/oracle: support -format=xml (for Eclipse)
This CL is mostly a renaming s/json/serial/, abstracting the
oracle package away from any particular data syntax.  (The
encoding/* machinery is very clean; clearly I should have
structured it this way from the outset.)

Supporting XML then becomes a one-liner in cmd/oracle/main.go.

Also: call MarshalIndent(), not Marshall() then Indent().

R=crawshaw
CC=golang-dev
https://golang.org/cl/13858046
2013-09-24 15:08:14 -04:00
Alan Donovan
25a0cc4bfd go.tools/oracle: refactor Oracle API to allow repeated queries on same scope.
The existing standalone Query function builds an importer, ssa.Program, oracle,
and query position, executes the query and returns the result.
For clients (such as Frederik Zipp's web-based github.com/fzipp/pythia tool)
that wish to load the program once and make several queries, we now expose
these as separate operations too.  Here's a client, in pseudocode:

        o := oracle.New(...)
        for ... {
                qpos := o.ParseQueryPos(...)
                res := o.Query(mode, qpos)
                print result
        }

NB: this is a slight deoptimisation in the one-shot case since we have to
build the entire SSA program with debug info, not just the query package,
since we now don't know the query package at that time.

The 'exact' param to ParseQueryPos needs more thought since its
ideal value is a function of the query mode.  This will do for now.

Details:
- expose Oracle type, New() func and Query() method.
- expose QueryPos type and ParseQueryPos func.
- improved package doc comment.
- un-exposed the "needs" bits.
- added test.

R=crawshaw
CC=frederik.zipp, golang-dev
https://golang.org/cl/13810043
2013-09-23 15:02:18 -04:00
Alan Donovan
3b5de067a1 go.tools/pointer: reflection, part 1: maps, and some core features.
Core:
        reflect.TypeOf
        reflect.ValueOf
        reflect.Zero
        reflect.Value.Interface
Maps:
        (reflect.Value).MapIndex
        (reflect.Value).MapKeys
        (reflect.Value).SetMapIndex
        (*reflect.rtype).Elem
        (*reflect.rtype).Key

+ tests:
  pointer/testdata/mapreflect.go.
  oracle/testdata/src/main/reflection.go.

Interface objects (T, V...) have been renamed "tagged objects".

Abstraction: we model reflect.Value similar to
interface{}---as a pointer that points only to tagged
objects---but a reflect.Value may also point to an "indirect
tagged object", one in which the payload V is of type *T not T.
These are required because reflect.Values can hold lvalues,
e.g. when derived via Field() or Elem(), though we won't use
them till we get to structs and pointers.

Solving: each reflection intrinsic defines a new constraint
and resolution rule.  Because of the nature of reflection,
generalizing across types, the resolution rules dynamically
create additional complex constraints during solving, where
previously only simple (copy) constraints were created.
This requires some solver changes:

  The work done before the main solver loop (to attach new
  constraints to the graph) is now done before each iteration,
  in processNewConstraints.

  Its loop over constraints is broken into two passes:
  the first handles base (addr-of) constraints,
  the second handles simple and complex constraints.

  constraint.init() has been inlined.  The only behaviour that
  varies across constraints is ptr()

Sadly this will pessimize presolver optimisations, when we get
there; such is the price of reflection.

Objects: reflection intrinsics create objects (i.e. cause
memory allocations) with no SSA operation.  We will represent
them as the cgnode of the instrinsic (e.g. reflect.New), so we
extend Labels and node.data to represent objects as a product
(not sum) of ssa.Value and cgnode and pull this out into its
own type, struct object.  This simplifies a number of
invariants and saves space.  The ntObject flag is now
represented by obj!=nil; the other flags are moved into
object.

cgnodes are now always recorded in objects/Labels for which it
is appropriate (all but those for globals, constants and the
shared contours for functions).

Also:
- Prepopulate the flattenMemo cache to consider reflect.Value
  a fake pointer, not a struct.
- Improve accessors and documentation on type Label.
- @conctypes assertions renamed @types (since dyn. types needn't be concrete).
- add oracle 'describe' test on an interface (missing, an oversight).

R=crawshaw
CC=golang-dev
https://golang.org/cl/13418048
2013-09-16 09:49:10 -04:00
Robert Griesemer
1928c01286 go.tools/go/types: separate package descriptor from package object
Includes changes by adonovan to make oracle work again
(former CL 13395050).

R=adonovan
CC=golang-dev
https://golang.org/cl/13672044
2013-09-13 09:52:57 -07:00
Alan Donovan
c18d759e6b go.tools/oracle: describe package: simplify to use only types.Package, not ssa.Package.
R=crawshaw
CC=golang-dev
https://golang.org/cl/13396050
2013-09-10 14:19:11 -04:00
Alan Donovan
927e0f9da6 go.tools/oracle: describe: query content of lvalues, not their address.
Background: some ssa.Values represent lvalues, e.g.
      var g = new(string)
the *ssa.Global g is a **string, the address of what users
think of as the global g.

Querying pts(g) returns a singleton containing the object g, a
*string.  What users really want to see is what that in turn
points to, i.e. the label for the call to new().

This change now lets users make "indirect" pointer queries,
i.e. for pts(*v) where v is an ssa.Value.  The oracle makes an
indirect query if the type of the ssa.Value differs from the
source expression type by a pointer, i.e. it's an lvalue.

In other words, we're hiding the fact that compilers (e.g. ssa) internally represent globals by their address.

+ Tests.

This serendipitously fixed an outstanding bug mentioned in the
describe.go

R=crawshaw
CC=golang-dev
https://golang.org/cl/13532043
2013-09-09 21:06:25 -04:00
Alan Donovan
3f2f9a7e70 go.tools/importer: generalize command-line syntax.
Motivation: pointer analysis tools (like the oracle) want the
user to specify a set of initial packages, like 'go test'.
This change enables the user to specify a set of packages on
the command line using importer.LoadInitialPackages(args).

Each argument is interpreted as either:
- a comma-separated list of *.go source files together
  comprising one non-importable ad-hoc package.
  e.g. "src/pkg/net/http/triv.go" gives us [main].
- an import path, denoting both the imported package
  and its non-importable external test package, if any.
  e.g. "fmt" gives us [fmt, fmt_test].

Current type-checker limitations mean that only the first
import path may contribute tests: multiple packages augmented
by *_test.go files could create import cycles, which 'go test'
avoids by building a separate executable for each one.
That approach is less attractive for static analysis.

Details:  (many files touched, but importer.go is the crux)

importer:
- PackageInfo.Importable boolean indicates whether
  package is importable.
- un-expose Importer.Packages; expose AllPackages() instead.
- CreatePackageFromArgs has become LoadInitialPackages.
- imports() moved to util.go, renamed importsOf().
- InitialPackagesUsage usage message exported to clients.
- the package name for ad-hoc packages now comes from the
  'package' decl, not "main".

ssa.Program:
- added CreatePackages() method
- PackagesByPath un-exposed, renamed 'imported'.
- expose AllPackages and ImportedPackage accessors.

oracle:
- describe: explain and workaround a go/types bug.

Misc:
- Removed various unnecessary error.Error() calls in Printf args.

R=crawshaw
CC=golang-dev
https://golang.org/cl/13579043
2013-09-06 18:13:57 -04:00
Alan Donovan
a1dade8bdc go.oracle: describe: disambiguate 'func' object kinds.
The typechecker uses *types.Func for functions, concrete
methods and interface methods; and *types.Var for variables
and struct fields.  This change makes clear which kind of
function we're describing.  (We can't do it for vars since
go/types doesn't expose enough information, yet.)

Also: add "omitempty" to one JSON field.

R=crawshaw
CC=golang-dev
https://golang.org/cl/13527044
2013-09-04 16:15:41 -04:00
Alan Donovan
d2cdbefbfc go.tools/oracle: add option to output results in JSON syntax.
See json.go for interface specification.

Example usage:
% oracle -format=json -mode=callgraph code.google.com/p/go.tools/cmd/oracle

+ Tests, based on (small) golden files.

Overview:
  Each <query>Result structure has been "lowered" so that all
  but the most trivial logic in each display() function has
  been moved to the main query.

  Each one now has a toJSON method that populates a json.Result
  struct.  Though the <query>Result structs are similar to the
  correponding JSON protocol, they're not close enough to be
  used directly; for example, the former contain richer
  semantic entities (token.Pos, ast.Expr, ssa.Value,
  pointer.Pointer, etc) whereas JSON contains only their
  printed forms using Go basic types.

  The choices of what levels of abstractions the two sets of
  structs should have is somewhat arbitrary.  We may want
  richer information in the JSON output in future.

Details:
- oracle.Main has been split into oracle.Query() and the
  printing of the oracle.Result.
- the display() method no longer needs an *oracle param, only
  a print function.
- callees: sort the result for determinism.
- callees: compute the union across all contexts.
- callers: sort the results for determinism.
- describe(package): fixed a bug in the predicate for method
  accessibility: an unexported method defined in pkg A may
  belong to a type defined in package B (via
  embedding/promotion) and may thus be accessible to A.  New
  accessibleMethods() utility fixes this.
- describe(type): filter methods by accessibility.
- added tests of 'callgraph'.
- pointer: eliminated the 'caller CallGraphNode' parameter from
  pointer.Context.Call callback since it was redundant w.r.t
  site.Caller().
- added warning if CGO_ENABLED is unset.

R=crawshaw
CC=golang-dev
https://golang.org/cl/13270045
2013-09-03 15:29:02 -04:00
Alan Donovan
713699d8ad go.tools: add copyright messages to source files.
R=r
CC=golang-dev
https://golang.org/cl/13305043
2013-08-27 18:49:13 -04:00
Alan Donovan
e08d89f3ed go.tools/oracle: an oracle that answers questions about Go source code.
+ Tests.
+ Emacs integration.
+ Emacs integration test.
+ very rudimentary Vim integration.  Needs some love from a Vim user.

TODO (in follow-ups):
- More tests would be good.
  We'll need to make the output order deterministic in more places.
- Documentation.

R=gri, crawshaw, dominik.honnef
CC=golang-dev
https://golang.org/cl/9502043
2013-08-27 17:58:26 -04:00