mirror of
https://github.com/golang/go
synced 2024-11-19 04:34:39 -07:00
3f2f9a7e70
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
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
// 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 ssa
|
|
|
|
// This file defines utilities for visiting the SSA representation of
|
|
// a Program.
|
|
//
|
|
// TODO(adonovan): improve the API:
|
|
// - permit client to supply a callback for each function,
|
|
// instruction, type with methods, etc?
|
|
// - return graph information about the traversal?
|
|
// - test coverage.
|
|
|
|
import "code.google.com/p/go.tools/go/types"
|
|
|
|
// AllFunctions returns the set of all functions (including anonymous
|
|
// functions and synthetic wrappers) in program prog.
|
|
//
|
|
// Precondition: all packages are built.
|
|
//
|
|
func AllFunctions(prog *Program) map[*Function]bool {
|
|
visit := visitor{
|
|
prog: prog,
|
|
seen: make(map[*Function]bool),
|
|
}
|
|
visit.program()
|
|
return visit.seen
|
|
}
|
|
|
|
type visitor struct {
|
|
prog *Program
|
|
seen map[*Function]bool
|
|
}
|
|
|
|
func (visit *visitor) program() {
|
|
for _, pkg := range visit.prog.AllPackages() {
|
|
for _, mem := range pkg.Members {
|
|
switch mem := mem.(type) {
|
|
case *Function:
|
|
visit.function(mem)
|
|
case *Type:
|
|
visit.methodSet(mem.Type())
|
|
visit.methodSet(types.NewPointer(mem.Type()))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (visit *visitor) methodSet(typ types.Type) {
|
|
mset := typ.MethodSet()
|
|
for i, n := 0, mset.Len(); i < n; i++ {
|
|
// Side-effect: creates all wrapper methods.
|
|
visit.function(visit.prog.Method(mset.At(i)))
|
|
}
|
|
}
|
|
|
|
func (visit *visitor) function(fn *Function) {
|
|
if !visit.seen[fn] {
|
|
visit.seen[fn] = true
|
|
for _, b := range fn.Blocks {
|
|
for _, instr := range b.Instrs {
|
|
switch instr := instr.(type) {
|
|
case *MakeInterface:
|
|
visit.methodSet(instr.X.Type())
|
|
}
|
|
var buf [10]*Value // avoid alloc in common case
|
|
for _, op := range instr.Operands(buf[:0]) {
|
|
if fn, ok := (*op).(*Function); ok {
|
|
visit.function(fn)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|