Errors relating to a particular package (as opposed to the Load
operation as a whole) are now only recorded in the Errors
field of the corresponding Package.
Clients are responsible for printing or otherwise handling errors.
This is a breaking API change.
The PrintErrors function prints all accumulated errors,
dependencies first, and is provided for convenience.
PrintErrors is based on Visit, another helper function, which
visits each Package in an import graph.
Also:
- add an Example documenting typical use.
- update clients to handle errors explicitly.
Depends on https://go-review.googlesource.com/c/tools/+/130576
Change-Id: I39407ab7f46dae2f0dd0fdde21080e172e1258aa
Reviewed-on: https://go-review.googlesource.com/131015
Reviewed-by: Michael Matloob <matloob@golang.org>
This saves 35% of wall time in a command such as
$ gopackages -mode=syntax golang.org/x/tools/go/packages/gopackages fmt
that would otherwise typecheck every function in each package that
depends on fmt. It also applies in -mode=types when source was loaded
as a fall back for missing export data due to an error in a lower package.
Also, remove Config.TypeChecker field now that not a single of its
subfields is passed through go/packages to the type checker.
(The Sizes function is logically a result, not an input, of loading,
though we have yet to implement it properly.)
Change-Id: I472d21b34fc5e2832f7353e82992a67a06e4e4cc
Reviewed-on: https://go-review.googlesource.com/129497
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Unlike go/loader's FromArgs, go/packages doesn't return the
non-package arguments, so we add a new repeated flag -arg=x -arg=y to
specify them.
Because we cannot add packages to a packages.Load query, we emit an
error if -run is specified and "runtime" is not among the
dependencies.
There is no easy way to distinguish synthetic test main packages
from regular main packages, so we no longer try.
This makes -test and -run orthogonal.
Change-Id: Ibd69b993c05de62df01dde52218a9e266cd63a71
Reviewed-on: https://go-review.googlesource.com/128837
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Details:
- remove go1.5 "default version" labels on most files
- remove go1.6 labels on a few files
- go/loader: use conf.Cwd not "." in a couple places; update faulty
test expectations. (Not sure why this wasn't caught sooner.)
- go/ssa/interp: add 'mono' result to time.now intrinsic
- go/gcimporter15/bimport.go: make consistent with the version in gc
- go/ssa/interp: update test error message
- go/ssa: update a comment
The go/gcimporter15/bexport.go logic is stale and needs to be brought
up to date. Needs a separate CL since it's tricky.
Tested on go1.6, go1.7, go1.8.
Change-Id: I841189d30e131b7c49a4e8690ea7c40b55041bae
Reviewed-on: https://go-review.googlesource.com/36540
Reviewed-by: Robert Griesemer <gri@golang.org>
Supporting user-defined TestMain functions requires that we generate a
"testmain" package for each testable package, rather than a single one
for the entire program. This entails these API changes:
1. (*ssa.Program).{CreateTestMainPackage,FindTests} both now
accept only a single package. Existing clients that pass them
multiple packages must call them from a loop.
2. (*ssa.Program).FindTests returns an additional result, the the
optional TestMain *ssa.Function. Existing clients may discard it.
Also:
- Test the new logic using the SSA interpreter
- add ssautil.MainPackages helper
- callgraph: allow multiple main packages, and analyze them all
- ssadump -run: allow multiple main/test packages, and run each in a new interpreter
- minor simplifications to some callers (e.g. guru)
Fixesgolang/go#9553
Change-Id: Ia7de9bd27448fb08b8d172ba5cdbcf37a762b7a0
Reviewed-on: https://go-review.googlesource.com/25102
Reviewed-by: Robert Griesemer <gri@golang.org>
Per https://groups.google.com/forum/#!topic/golang-announce/qu_rAphYdxY
this change deletes the packages
go/exact
go/gccgoimporter
go/gcimporter
go/importer
go/types
cmd/vet
from the x/tools repo and any files depending on those packages
building against Go 1.4.
x/tools packages depending on any of these libraries must use the
respective versions from the std lib or use vendored versions if
building against 1.4.
Remaining packages may or may not build against Go 1.4 anymore
and will not be supported against 1.4.
Change-Id: I1c655fc30aee49b6c7326ebd4eb1bb0836ac97e0
Reviewed-on: https://go-review.googlesource.com/20810
Reviewed-by: Alan Donovan <adonovan@google.com>
The BuilderModeFlag function does not work with flag packages (such as
Google's) that define their own FlagSet. This change exposes a type
that satisifies flag.Value insted of a function. That type
is *BuilderMode; there's no need for a separate wrapper type.
Change-Id: I8095b80de499e3c52a29a5c1996d1b1fe3799358
Reviewed-on: https://go-review.googlesource.com/20330
Reviewed-by: Michael Matloob <matloob@golang.org>
A few files have been forked and tagged "go1.5,!go1.6" to work around
minor API changes between the two types packages:
- constant.Value.String() in oracle/describe.go and its tests;
- constant.ToInt must now be called before constant.Int64Val.
- types.Config{Importer: importer.Default()} in a number of places
- go/types/typeutil/import_test.go uses lowercase names to avoid 'import "C"'.
Files in go/types/typesutil, missing from my previous CL, have been
tagged !go1.5; these files will be deleted in February.
All affected packages were tested using 1.4.1, 1.5, and ~1.6 (tip).
Change-Id: Iec7fd370e1434508149b378438fb37f65b8d2ba8
Reviewed-on: https://go-review.googlesource.com/18207
Reviewed-by: Robert Griesemer <gri@golang.org>
This change will ensure that the tree continues to work with go1.4.1.
All files continue to depend on golang.org/x/tools/go/types, but in a
follow-up change, I will switch the primary files to depend on the
standard go/types package. Another (smaller) set of files will be
forked and tagged, this time !1.6, due to API differences between the
two packages.
All tests pass using 1.4.1, 1.5, and ~1.6 (tip).
Change-Id: Ifd75a6330e120957d646be91693daaba1ce0e8c9
Reviewed-on: https://go-review.googlesource.com/18333
Reviewed-by: Robert Griesemer <gri@golang.org>
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>
...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>
ssadump's -build=G option is now spelled -import; it was never related to ssa.
Change-Id: Ic21cd8b6990c0ffd25651c17a842a63bfa5019cf
Reviewed-on: https://go-review.googlesource.com/5172
Reviewed-by: David Crawshaw <crawshaw@golang.org>
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>
Such messages are more informative when the error occurs deep within a script.
Also: add tool name to digraph's usage messages.
LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/173380043
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
And serialize the printing of each item with a mutex.
It is the formatted output of this tool, after all.
Also: minor doc tweaks.
LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/114620044
Bare init functions omit calls to dependent init functions and the
use of an init guard. They are useful in cases where the client uses
a different calling convention for init functions, or cases where
it is easier for a client to analyze bare init functions.
LGTM=adonovan
R=adonovan
CC=golang-codereviews, iant
https://golang.org/cl/78780043
Previously, each word could be a package import path or a
comma-separated list of *.go file names. Now, if the
first word ends with ".go", all words are assumed to be
Go source files. This makes it impossible to specify
two ad-hoc packages from source files, but no-one needs that.
FromArgs also takes a boolean indicating whether tests
are wanted or not.
Also: ssadump: add -test flag to set that boolean.
For the oracle it's always true.
LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/61470047
The Importer type has been replaced with Config and Program.
Clients populate a Config, directly or more usually via
convenience functions. They then call its Load() method to do
all of the typechecking and transitive-closure computation.
ssa.NewProgram and ssa.CreatePackages have been fused into
ssa.Create, which now cannot fail, since (*Config).Load()
reports all type errors.
Also:
- The addition of an ssa.GlobalDebug builder mode flag
eliminates a loop-over-packages repeated in many clients.
- PackageInfo.Err flag unexported. Clients never see bad infos now.
- cmd/ssadump: now only looks for func "main" in package "main".
- importsOf deleted, was dead code.
STILL TODO:
- ParseFile seems like API creep (though it's convenient)
and CreateFromFiles is dangerous (w.r.t. FileSet identity).
Need to think more...
- the need for clients to rely on elementwise correspondence
of Config.CreatePkgs and Program.Created is a little sad.
- The command-line interface has not changed.
That will happen in a follow-up.
r recommends using a repeated flag: -package p -package q ...
R=gri
CC=axwalk, frederik.zipp, golang-codereviews
https://golang.org/cl/49530047
Packages were not being created for all types.Packages,
specifically, indirectly imported packages were missing.
(*Program).CreatePackages now iterates over the type-checker's
package map too.
Also: removed all concurrency from importer. I think it was
incorrect (and hard to fix).
Also: change LoadInitialPackages so that all named packages
are loaded from source. This happens regardless of whether
GCImporter is used to satisfy imports.
Details:
- importer.Config.SourceImports flag determines whether to
load all packages from *.go source.
(Before, this was indicated by Config.Build != nil.)
- importer.Config.Build field effectively defaults to
&go/build.Default. A zero importer.Config is now usable.
- importer.Importer.Config field is now exported.
- LoadPackage renamed to ImportPackage since the resulting
packages may come from GCImporter (and be incomplete).
- doImport and ImportPackage fused.
Fixesgolang/go#7028
R=gri, axwalk
CC=golang-codereviews
https://golang.org/cl/48770043
Also report an error for "cross-interpretation": not supported
due to the interpreter's assumption that host and target
{int,uint,uintptr} are the same. (Too tedious and messy to fix.)
Tested manually:
% cat d.go
package main
const m = ^uintptr(0)
const w = m>>8&1 + m>>16&1 + m>>32&1
func main() { println(m, w) }
% ./ssadump -build=P -run d.go
package main:
const m m = 18446744073709551615:uintptr
const w w = 3:uintptr
18446744073709551615 3
% GOARCH=386 ./ssadump -build=P -run d.go
package main:
const m m = 4294967295:uintptr
const w w = 2:uintptr
Error: Cross-interpretation is not yet supported (target has GOARCH 386, interpreter has amd64).
Fixesgolang/go#7080
R=gri
CC=golang-codereviews
https://golang.org/cl/49070043
This allows us to run/analyze multiple tests.
Also it causes the production code packages to be properly initialized.
Also:
- cmd/ssadump: improved usage message (add example;
incorporate LoadInitialPackages usage; explain how -run
finds main).
- pointer, oracle, ssa/interp: use CreateTestMainPackage.
- ssa/builder.go: remove 'rundefers' instruction from package init,
which no longer uses 'defer'.
R=gri
CC=golang-dev
https://golang.org/cl/15920047
A function such as this:
func one() (x int) {
defer func() { recover() }()
x = 1
panic("return")
}
that combines named return parameters (NRPs) with deferred calls
that call recover, may return non-zero values despite the
fact it doesn't even contain a return statement. (!)
This requires a change to the SSA API: all functions'
control-flow graphs now have a second entry point, called
Recover, which is the block at which control flow resumes
after a recovered panic. The Recover block simply loads the
NRPs and returns them.
As an optimization, most functions don't need a Recover block,
so it is omitted. In fact it is only needed for functions that
have NRPs and defer a call to another function that _may_ call
recover.
Dataflow analysis of SSA now requires extra work, since every
may-panic instruction has an implicit control-flow edge to
the Recover block. The only dataflow analysis so far implemented
is SSA renaming, for which we make the following simplifying
assumption: the Recover block only loads the NRPs and returns.
This means we don't really need to analyze it, we can just
skip the "lifting" of such NRPs. We also special-case the Recover
block in the dominance computation.
Rejected alternative approaches:
- Specifying a Recover block for every defer instruction (like a
traditional exception handler).
This seemed like excessive generality, since Go programs
only need the same degenerate form of Recover block.
- Adding an instruction to set the Recover block immediately
after the named return values are set up, so that dominance
can be computed without special-casing.
This didn't seem worth the effort.
Interpreter:
- This CL completely reimplements the panic/recover/
defer logic in the interpreter. It's clearer and simpler
and closer to the model in the spec.
- Some runtime panic messages have been changed to be closer
to gc's, since tests depend on it.
- The interpreter now requires that the runtime.runtimeError
type be part of the SSA program. This requires that clients
import this package prior to invoking the interpreter.
This in turn requires (Importer).ImportPackage(path string),
which this CL adds.
- All $GOROOT/test/recover{,1,2,3}.go tests are now passing.
NB, the bug described in coverage.go (defer/recover in a concatenated
init function) remains. Will be fixed in a follow-up.
Fixesgolang/go#6381
R=gri
CC=crawshaw, golang-dev
https://golang.org/cl/13844043
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
1. ParseFiles (in util.go) parses each file in its own goroutine.
2. (*Importer).LoadPackage asynchronously prefetches the
import graph by scanning the imports of each loaded package
and calling LoadPackage on each one.
LoadPackage is now thread-safe and idempotent: it uses a
condition variable per package; the first goroutine to
request a package becomes responsible for loading it and
broadcasts to the others (waiting) when it becomes ready.
ssadump runs 34% faster when loading the oracle.
Also, refactorings:
- delete SourceLoader mechanism; just expose go/build.Context directly.
- CreateSourcePackage now also returns an error directly,
rather than via PackageInfo.Err, since every client wants that.
R=crawshaw
CC=golang-dev
https://golang.org/cl/13509045
(Its former location was based on a misunderstanding of 'go build'.)
Also: set GOMAXPROCS to NumCPU by default.
R=crawshaw
CC=golang-dev
https://golang.org/cl/13354043