From 96af1504f60193ba43510e8d515cc0ad6cac5c5c Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 22 Jan 2015 17:24:58 -0500 Subject: [PATCH] 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 --- go/ssa/testmain.go | 24 +++++++++++++++++------- godoc/analysis/analysis.go | 4 ++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/go/ssa/testmain.go b/go/ssa/testmain.go index a525c0b179..7935f4efb5 100644 --- a/go/ssa/testmain.go +++ b/go/ssa/testmain.go @@ -12,8 +12,10 @@ import ( "go/ast" "go/token" "os" + "sort" "strings" + "golang.org/x/tools/go/exact" "golang.org/x/tools/go/types" ) @@ -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() diff --git a/godoc/analysis/analysis.go b/godoc/analysis/analysis.go index ef060b2f8d..4d7e1ea688 100644 --- a/godoc/analysis/analysis.go +++ b/godoc/analysis/analysis.go @@ -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" @@ -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 {