1
0
mirror of https://github.com/golang/go synced 2024-09-30 22:38:33 -06:00

go/ssa: add list-of-tests constant to generated testmain package

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 <gri@golang.org>
This commit is contained in:
Alan Donovan 2015-01-22 17:24:58 -05:00
parent f011631cea
commit 96af1504f6
2 changed files with 21 additions and 7 deletions

View File

@ -12,8 +12,10 @@ import (
"go/ast" "go/ast"
"go/token" "go/token"
"os" "os"
"sort"
"strings" "strings"
"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/types" "golang.org/x/tools/go/types"
) )
@ -118,25 +120,33 @@ func (prog *Program) CreateTestMainPackage(pkgs ...*Package) *Package {
} }
// Initialize packages to test. // Initialize packages to test.
var pkgpaths []string
for _, pkg := range pkgs { for _, pkg := range pkgs {
var v Call var v Call
v.Call.Value = pkg.init v.Call.Value = pkg.init
v.setType(types.NewTuple()) v.setType(types.NewTuple())
init.emit(&v) init.emit(&v)
pkgpaths = append(pkgpaths, pkg.Object.Path())
} }
sort.Strings(pkgpaths)
init.emit(new(Return)) init.emit(new(Return))
init.finishBody() init.finishBody()
testmain.init = init testmain.init = init
testmain.Object.MarkComplete() testmain.Object.MarkComplete()
testmain.Members[init.name] = init testmain.Members[init.name] = init
main := &Function{ // For debugging convenience, define an unexported const
name: "main", // that enumerates the packages.
Signature: new(types.Signature), packagesConst := types.NewConst(token.NoPos, testmain.Object, "packages", tString,
Synthetic: "test main function", exact.MakeString(strings.Join(pkgpaths, " ")))
Prog: prog, memberFromObject(testmain, packagesConst, nil)
Pkg: testmain,
} // 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() main.startBody()

View File

@ -57,6 +57,7 @@ import (
"strings" "strings"
"sync" "sync"
"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/loader" "golang.org/x/tools/go/loader"
"golang.org/x/tools/go/pointer" "golang.org/x/tools/go/pointer"
"golang.org/x/tools/go/ssa" "golang.org/x/tools/go/ssa"
@ -400,6 +401,9 @@ func Run(pta bool, result *Result) {
var mainPkgs []*ssa.Package var mainPkgs []*ssa.Package
if testmain := prog.CreateTestMainPackage(allPackages...); testmain != nil { if testmain := prog.CreateTestMainPackage(allPackages...); testmain != nil {
mainPkgs = append(mainPkgs, testmain) 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 { for _, pkg := range allPackages {
if pkg.Object.Name() == "main" && pkg.Func("main") != nil { if pkg.Object.Name() == "main" && pkg.Func("main") != nil {