Skip to content

Commit

Permalink
go/ssa: add list-of-tests constant to generated testmain package
Browse files Browse the repository at this point in the history
And log its value in godoc -analysis.

Related to issue 8968

Change-Id: I96a96922a3fa5c434c69e0faff1cc8ec4686b6f2
Reviewed-on: https://go-review.googlesource.com/3154
Reviewed-by: Robert Griesemer <[email protected]>
  • Loading branch information
adonovan committed Jan 22, 2015
1 parent f011631 commit 96af150
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
24 changes: 17 additions & 7 deletions go/ssa/testmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"go/ast"
"go/token"
"os"
"sort"
"strings"

"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/types"
)

Expand Down Expand Up @@ -118,25 +120,33 @@ func (prog *Program) CreateTestMainPackage(pkgs ...*Package) *Package {
}

// Initialize packages to test.
var pkgpaths []string
for _, pkg := range pkgs {
var v Call
v.Call.Value = pkg.init
v.setType(types.NewTuple())
init.emit(&v)

pkgpaths = append(pkgpaths, pkg.Object.Path())
}
sort.Strings(pkgpaths)
init.emit(new(Return))
init.finishBody()
testmain.init = init
testmain.Object.MarkComplete()
testmain.Members[init.name] = init

main := &Function{
name: "main",
Signature: new(types.Signature),
Synthetic: "test main function",
Prog: prog,
Pkg: testmain,
}
// For debugging convenience, define an unexported const
// that enumerates the packages.
packagesConst := types.NewConst(token.NoPos, testmain.Object, "packages", tString,
exact.MakeString(strings.Join(pkgpaths, " ")))
memberFromObject(testmain, packagesConst, nil)

// Create main *types.Func and *ssa.Function
mainFunc := types.NewFunc(token.NoPos, testmain.Object, "main", new(types.Signature))
memberFromObject(testmain, mainFunc, nil)
main := testmain.Func("main")
main.Synthetic = "test main function"

main.startBody()

Expand Down
4 changes: 4 additions & 0 deletions godoc/analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"strings"
"sync"

"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/pointer"
"golang.org/x/tools/go/ssa"
Expand Down Expand Up @@ -400,6 +401,9 @@ func Run(pta bool, result *Result) {
var mainPkgs []*ssa.Package
if testmain := prog.CreateTestMainPackage(allPackages...); testmain != nil {
mainPkgs = append(mainPkgs, testmain)
if p := testmain.Const("packages"); p != nil {
log.Printf("Tested packages: %v", exact.StringVal(p.Value.Value))
}
}
for _, pkg := range allPackages {
if pkg.Object.Name() == "main" && pkg.Func("main") != nil {
Expand Down

0 comments on commit 96af150

Please sign in to comment.