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

117 Commits

Author SHA1 Message Date
Alan Donovan
afcda55b39 go/ssa: rename some API features (incompatible change)
Rename the following exported names:
	(*Program).BuildAll → Build
	(*Program).Method   → MethodValue
	Package.Object      → Pkg
Also:
	(*Function).pkgobj  → pkg

Change-Id: Iff7e6c240ebe6786ba759278ac0daa3d66698013
Reviewed-on: https://go-review.googlesource.com/14134
Reviewed-by: Robert Griesemer <gri@golang.org>
2015-08-31 22:20:12 +00:00
Robert Griesemer
958ab4f550 x/tools: disable tests not supported on Android
For golang/go#11811.

Change-Id: Icf16a2d47fcf2fe0d79dd825ccb851a3d63a660f
Reviewed-on: https://go-review.googlesource.com/13268
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-08-06 00:36:03 +00:00
Alan Donovan
543085f0ec go/callgraph: add "concurrent" or "deferred" prefix to (*Edge).Description
Fixes golang/go#11518

Change-Id: If6c86c1995c0b6a1226deedaa1713947baa6593a
Reviewed-on: https://go-review.googlesource.com/12085
Reviewed-by: Daniel Morsing <daniel.morsing@gmail.com>
2015-07-13 18:17:08 +00:00
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
Robert Griesemer
3f8eecd15b go/types: move MethodSetCache into package go/types/typeutil
Change-Id: Iba5d7c2df533948a5b28373b077cc0476a6745ad
Reviewed-on: https://go-review.googlesource.com/10770
Reviewed-by: Alan Donovan <adonovan@google.com>
2015-06-05 17:34:01 +00:00
Alan Donovan
3d1847243e oracle: disable parser bailout
This change sets ParserMode=AllErrors so that the parser is never
allowed to discard the AST and use a dummy one just because it saw too
many errors.

Also, change (*loader.Program).PathEnclosingInterval so that other
clients that forget to set this flag don't panic while calling
fset.File(f.Pos()).Base() on an ast.File f with no position info.

Change-Id: Ie544f169d367d2aa85426212b27063dc72e36fb1
Reviewed-on: https://go-review.googlesource.com/10290
Reviewed-by: Robert Griesemer <gri@golang.org>
2015-05-20 20:27:48 +00:00
Daniel Morsing
500e956000 oracle: attempt to deduce callees statically before building SSA
When querying for callees against a static call, the entire SSA
form for the program was built. Since we can tell if a callee is
statically dispatched after typechecking, try to do that before
building the SSA form.

This cuts 3.5 seconds off queries against static calls.

Change-Id: I22291381d3bec490e3b1d6f9c6b5a0092fd9f635
Reviewed-on: https://go-review.googlesource.com/10230
Reviewed-by: Alan Donovan <adonovan@google.com>
2015-05-20 12:10:52 +00:00
Alan Donovan
d1b42377f9 cmd/oracle: improve error messages
Also, in Emacs, make 'referrers' query not prompt for a scope.

Change-Id: I5c0f034d4fa8b653311f1b7d8ff58b699d168b79
Reviewed-on: https://go-review.googlesource.com/9927
Reviewed-by: Andrew Gerrand <adg@golang.org>
2015-05-13 15:02:11 +00:00
Alan Donovan
6a8dc4ed40 oracle: report a better error if query is outside GOROOT/GOPATH
Change-Id: I2f724b5015021be8bf1fccb518f17cc011d687e8
Reviewed-on: https://go-review.googlesource.com/9546
Reviewed-by: David Chase <drchase@google.com>
2015-04-30 18:55:42 +00:00
Alan Donovan
a9f55c4fa4 oracle: support "referrers" query on package declaration
Added test.

Change-Id: Id7d061b0f74959166b5631a3fde8da4000ffb8d2
Reviewed-on: https://go-review.googlesource.com/9326
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-04-27 13:27:00 +00:00
Alan Donovan
837a81a1b2 oracle: referrers: display the selected object relative to the current package
Added test.  (Its previous output was "references to type
referrers.s", with s package-qualified.)

Change-Id: Ifd70246bf5976d5f59ed85e7bbded618286ec6bc
Reviewed-on: https://go-review.googlesource.com/9294
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-04-23 20:44:32 +00:00
Alan Donovan
9c57c19a58 go/loader: eliminate ImportFromBinary option and PackageCreated hook
The loader package now loads, parses, and type-checks a whole program
from source, and that is all.

Also:
- simplified loader logic
- ssa.Create is gone; use ssautil.CreateProgram.
- ssautil.LoadPackage renamed to BuildPackage.
  It is now independent of go/types' Import hook and the Packages map.
- ssadump: -importbin flag removed.
  The value of this flag was that it caused the tool to print IR
  for only a single package; this is now the normal behaviour.

Fixes #9955

Change-Id: I4571118258ab1a46dccece3241b7dc51401a3acc
Reviewed-on: https://go-review.googlesource.com/8953
Reviewed-by: Robert Griesemer <gri@golang.org>
2015-04-17 18:58:17 +00:00
Alan Donovan
ab1b92fc43 oracle: ignore errors during import graph construction
Such errors include files without package decl, or conflicting package
decls.  We ignore type errors, so we should ignore import errors too.

Fixes #10347

Change-Id: I9011a9099a2804281ea2d989d7263a9ce691be16
Reviewed-on: https://go-review.googlesource.com/8498
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-04-06 19:51:23 +00:00
Alan Donovan
f0d8175b3e oracle: skip cgo processing for queries that don't need SSA/PTA
This makes type-based queries faster and more robust because they
needn't invoke a C compiler, at the cost of worse results in/near .go
files that import "C".  It's particularly important for "referrers"
since a refererrs query on a name from the standard library can load
every package in the workspace.

Fixes issue 10347

Change-Id: I2f65474ce963de5c0897ba67eeb26290dd449cf1
Reviewed-on: https://go-review.googlesource.com/8493
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-04-06 16:52:07 +00:00
Alan Donovan
a18bb1d557 oracle: add TODO
Change-Id: I315af962e43a3ae9c8e5cad13766d2200484fdf9
Reviewed-on: https://go-review.googlesource.com/8386
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-04-02 18:07:00 +00:00
Alan Donovan
dce4131cda oracle: referrers: also scan *_test.go files for references.
Added test case.  This required making the result sort order
deterministic when the results are spread across several packages.

Also: implements: print type names relative to query package.
Updated tests.

Change-Id: I9f882cd358a612585a4aac9a117b89d9131a294e
Reviewed-on: https://go-review.googlesource.com/8283
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-31 16:25:39 +00:00
Alan Donovan
a27f5b3448 oracle: factor part of the initial setup for PTA queries
The check for len(argv)==0 now only applies to these modes.

Also, more consistent variable naming.

Change-Id: I9adb6bebc819eb43d54ddf63c42d952671ce9236
Reviewed-on: https://go-review.googlesource.com/8244
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 21:02:07 +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
68b5f7541d cmd/oracle: usability improvements to "describe", "referrers"
Emacs integration:
- eliminate oracle minor mode
- in go-mode, bind F5, F6 to "describe", "referrers".
  This reverts a previous policy decision but convenience matters too.
- don't insist on an analysis scope for modes that don't do PTA.
- don't hide the filename as "▶"; show the last 20 chars.
  (Especially useful for "referrers" mode.)
- output postprocessing: don't get stuck in a loop if the output
  is not as expected (e.g. when it includes a panic log).

referrers:
- show the matching lines (like grep does).
  We do the I/O in parallel.

Change-Id: I86b18c1d3a4d9fa4242984cba62b314796669d8e
Reviewed-on: https://go-review.googlesource.com/8120
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-03-30 19:04:01 +00:00
Alan Donovan
bf80246757 oracle/testdata: avoid sort.Sort so that my $GOROOT doesn't appear in output
Sorry for the oversight.

Change-Id: Ibb686dbee996b5223bd223fdd3afaab243a7a3ee
Reviewed-on: https://go-review.googlesource.com/7501
Reviewed-by: Robert Griesemer <gri@golang.org>
2015-03-12 18:36:52 +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
Alan Donovan
69db398fe0 go/loader: rename SourceImports flag to ImportFromBinary and invert sense
...since the zero value is more useful by far.

This is a breaking API change, obviously.  (One or two tests in this
CL have intentional been left using the zero value, i.e., they now
load source.)

Change-Id: I42287bfcdb1afef8ee84e5eac12534dd0a1fd5d2
Reviewed-on: https://go-review.googlesource.com/5653
Reviewed-by: Robert Griesemer <gri@golang.org>
2015-02-23 23:01:10 +00:00
Alan Donovan
0dda50d42a go/loader: improve robustness in face of input errors
Before this change, many kinds of error would cause the loader to stop.
    making it brittle when analyzing large codebases, as in "godoc -analysis".

    This change moves operations that used to occur during
    configuration---(*build.Context).Import, loading, and parsing of
    initial packages---into the Load call, and ensures that all failures
    during Loading are reported at the end so that the maximum amount of
    progress is made.

    Also: redesign the tests and add many new cases.

Change-Id: Ia8cd99416af7c5d4a5fe133908adfa83676d401f
Reviewed-on: https://go-review.googlesource.com/3626
Reviewed-by: Robert Griesemer <gri@golang.org>
2015-02-02 20:01:59 +00:00
Alan Donovan
4d45c85020 go/types: expose IsInterface predicate, eliminating 6 copies
Change-Id: I3704d7bd7a11f691c66556c1b77ef79a503d2fe9
Reviewed-on: https://go-review.googlesource.com/2173
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-01-21 18:49:27 +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
4f13714aca astutil: add Unparen utility, eliminating 7 copies.
Change-Id: I824328c275bd6198a57edd64bf72cb2cf0490a68
Reviewed-on: https://go-review.googlesource.com/2172
Reviewed-by: Chris Manghane <cmang@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
2014-12-29 22:24:57 +00:00
David Symonds
24257c8cd2 tools: add import comments.
Change-Id: Idda6e64580432cb9a731e4ebf4005ee4ceb4202d
Reviewed-on: https://go-review.googlesource.com/1244
Reviewed-by: Andrew Gerrand <adg@golang.org>
2014-12-09 22:42:16 +00:00
Daniel Morsing
25f58f97ce x/tools/oracle: add whicherrs query mode
The whicherrs query mode takes the position of an error and returns the set of constants, globals and types visible from within the scope of the error being queried.
It is meant to be used as a shortcut to find out which errors should be handled for a given functions call.

LGTM=adonovan
R=golang-codereviews, dominik.honnef, adonovan
CC=golang-codereviews
https://golang.org/cl/167420043
2014-12-05 15:51:34 +00:00
Alan Donovan
d75c6bdb8f cmd/oracle: minor fixes.
- print "oracle:" not "Error:" in error messages; remove period.
- allocate token.FileSet correctly.
- remove stale TODO (multiple test packages)
- fix typo and omission ('what') in usage message.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/178860043
2014-11-17 13:50:23 -05: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
Daniel Morsing
70f0e2472d go.tools/oracle: consider calls to close a peer operation.
This makes it possible to find corresponding closes to receives and sends.

LGTM=adonovan
R=adonovan
CC=golang-codereviews
https://golang.org/cl/155260043
2014-10-12 15:08:28 +01:00
Alan Donovan
9872f0d268 go.tools/*: replace $GOROOT/src/pkg with $GOROOT/src where appropriate.
(godoc is excluded from this CL since it will continue to use
/src/pkg in its URL namespace, making the necessary cleanup
more subtle.)

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/141770043
2014-09-08 13:24:38 -04: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
4228ee8063 go.tools/go/ssa: improve printing of anonymous functions.
Examples:
- "foo$1" becomes "pkg.foo$1"
- "init$1" (meaning the first declared "init" function) becomes "init#1",
   to distinguish it from "init$1" (meaning the first anonymous function
   within the synthetic "init" function that initializes package-level vars).

It is now an invariant that all source-level (non-synthetic)
functions have distinct names, and that all names include the
enclosing package.  Added test for this.

+ updated various clients.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/122750043
2014-07-31 17:37:41 -04:00
Alan Donovan
e5d15a9781 go.tools/go/pointer: add intrinsic for time.startTimer, which is implemented in C.
Without it, no value appears to be sent on NewTicker/NewTimer channels.

+ test

Also:
- add (callgraph.Edge).{Description,Pos} convenience methods
  to simplify client code when Site==nil.

LGTM=gri
R=gri, friestein68503
CC=golang-codereviews
https://golang.org/cl/112610043
2014-07-22 18:30:06 -04:00
Alan Donovan
f9612295cb go.tools/oracle: pointsto: if the queried expression is an lvalue, use the type of its value, not its address.
(Probable regression caused by recent changes to VarValue.)

+ regression test.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/116160044
2014-07-22 18:29:56 -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
1582053234 go.tools/oracle: remove stale item
LGTM=crawshaw
R=crawshaw
CC=golang-codereviews
https://golang.org/cl/101320043
2014-06-16 15:46:56 -04:00
Alan Donovan
142566e529 go/ssa: avoid "premature optimization" of dead branch removal.
Blocks dominated by "if false" should be retained in the
initial SSA form so they remain visible to subsequent source
code analysis tools.

In any case, true compilers already need a stronger version of
this optimization so they can simplify CFGs such as this:
	const x, y = ...
        switch x {case y:...}
where a branch is constant but the comparison of constants
does not occur within an expression.

LGTM=gri
R=gri
CC=golang-codereviews, pcc
https://golang.org/cl/101250043
2014-06-12 11:31:41 -04:00
Alan Donovan
fec252214b go.tools/ssa: create thunks for method expressions T.f.
Until now, the same Function was used to represent a method
(T)func() and the "method expression" function func(T) formed
from it. So the SSA code for this:

    var buf bytes.Buffer
    f := Buffer.Bytes
    f(buf)
    buf.Bytes()

would involve an implicit cast (ChangeType) on line 2.
However, compilers based on go/ssa may want to use different
calling conventions for them, like gccgo does (see issue
7839).  This change decouples them by using an anonymous
function called a "thunk", rather like this:

    f := func(r *bytes.Buffer) []byte { return r.Bytes() }

Thunks are similar to method wrappers; both are created by
makeWrapper.

"Interface method wrappers" were a special case of thunks for
direct calls (no indirection/fields) of interface methods.
They are now subsumed by thunks and have been deleted.  Now
that only the needed thunks are built, we don't need to
populate the concrete method sets of interface types at all,
so (*Program).Method and LookupMethod return nil for them.
This results in a slight reduction in function count (>1%) and
instruction count (<<1%).

Details:

go/ssa:
- API: ChangeType no longer supports func/method conversions.
- API: (*Program).FuncValue now returns nil for abstract
  (interface) methods.
- API: (*Function).RelString simplified.
  "$bound" is now a suffix not a prefix, and the receiver
  type is rendered package-relative.
- API: Function.Object is now defined for all wrappers too.
- API: (*Program).Method and LookupMethod return nil for
  abstract methods.
- emitConv no longer permits (non-identical)
  Signature->Signature conversions.  Added assertion.
- add and use isInterface helper
- sanity: we check packages after Build, not Create, otherwise
  cross-package refs might fail.

go/pointer:
- update tests for new function strings.
- pointer_test: don't add non-pointerlike probes to analysis.
  (The error was checked, but too late, causing a panic.)
- fixed a minor bug: if a test probe print(x) was the sole
  reference to x, no nodes were generated for x.
- (reflect.Type).MethodByName: updated due to ssa API changes.
  Also, fixed incorrect testdata/funcreflect.go expectation
  for MethodByName on interfaces.

oracle:
- fix for new FuncValue semantics.
- a "pointsto" query on an I.f thunk now returns an error.

Fixes golang/go#7839

LGTM=gri
R=gri
CC=golang-codereviews, pcc
https://golang.org/cl/93780044
2014-06-11 13:10:26 -04:00
Alan Donovan
95e5e90454 go.tools/oracle: callgraph: fix crash caused by sparse node numbering.
Revision 0ea4058a1ca3 caused the node numbering to become
sparse, violating a precondition of
(*callgraphResult).toSerial.  Now we renumber the callgraph
nodes always, not just the qpos != nil case.

Fixes golang/go#8171

LGTM=crawshaw
R=crawshaw
CC=golang-codereviews
https://golang.org/cl/103240044
2014-06-10 12:36:40 -04:00
Robert Griesemer
30b1abe2f7 go.tools: fix various typos
LGTM=adonovan
R=adonovan
CC=golang-codereviews
https://golang.org/cl/97920045
2014-05-02 14:38:08 -07:00
Alan Donovan
6ec40d9aac go.tools/oracle: don't discard /usr/bin/diff's stderr
LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/87980045
2014-04-15 15:39:38 -04: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
ccb0e9ab88 go.tools/.hgignore: don't ignore test-generated files, clean them up.
(Includes rollback of CL 83680043.)

LGTM=gri
R=gri
CC=golang-codereviews, rsc
https://golang.org/cl/86430045
2014-04-10 13:17:20 -04:00
Alan Donovan
a4491f08bf go.tools/oracle: fix bug in reduceScope on ad-hoc main packages.
LGTM=crawshaw
R=crawshaw
CC=golang-codereviews
https://golang.org/cl/77450048
2014-03-19 14:00:35 -04:00
Alan Donovan
ba9c801433 go.tools: various comments + doc tweaks.
No functional changes.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/74270043
2014-03-11 18:24:39 -04:00
Alan Donovan
d503a640d7 go.tools/go/ssa: name anon funcs by their enclosing func.
Before, they were named func@line:col which made them easy to find in the source if you know the file, but hard if you don't, and it made tests fragile.

Now, they are named outer$1, outer$2, etc, which makes them
more informative in a UI since "outer" has meaning.

LGTM=crawshaw
R=crawshaw
CC=golang-codereviews
https://golang.org/cl/65630048
2014-02-28 10:18:55 -05:00
Alan Donovan
c509cf123c go.tools/go/pointer: recover from panic in Analyse and return an error.
This is to avoid an internal error in pointer analysis from
bringing down a long-lived application such as godoc.

LGTM=crawshaw
R=crawshaw
CC=golang-codereviews
https://golang.org/cl/68930046
2014-02-27 14:13:52 -05:00