go.tools/ssa: tweaks to CreateTestMainPackage.

Move mutation of program to the final step.
+ 2 assertions;
+ switches.go: comment fix.

LGTM=gri
R=gri
CC=golang-codereviews
https://golang.org/cl/61510044
This commit is contained in:
Alan Donovan 2014-02-11 15:59:20 -05:00
Родитель ddfef020d1
Коммит ee92efc193
2 изменённых файлов: 11 добавлений и 3 удалений

4
go/ssa/ssautil/testdata/switches.go поставляемый
Просмотреть файл

@ -2,9 +2,9 @@
package main package main
// This file is the input to TestFindSwitches in switch_test.go. // This file is the input to TestSwitches in switch_test.go.
// Each multiway conditional with constant or type cases (Switch) // Each multiway conditional with constant or type cases (Switch)
// discovered by FindSwitches is printed, and compared with the // discovered by Switches is printed, and compared with the
// comments. // comments.
// //
// The body of each case is printed as the value of its first // The body of each case is printed as the value of its first

Просмотреть файл

@ -25,13 +25,15 @@ import (
// It returns nil if the program contains no tests. // It returns nil if the program contains no tests.
// //
func (prog *Program) CreateTestMainPackage(pkgs ...*Package) *Package { func (prog *Program) CreateTestMainPackage(pkgs ...*Package) *Package {
if len(pkgs) == 0 {
return nil
}
testmain := &Package{ testmain := &Package{
Prog: prog, Prog: prog,
Members: make(map[string]Member), Members: make(map[string]Member),
values: make(map[types.Object]Value), values: make(map[types.Object]Value),
Object: types.NewPackage("testmain", "testmain", nil), Object: types.NewPackage("testmain", "testmain", nil),
} }
prog.packages[testmain.Object] = testmain
// Build package's init function. // Build package's init function.
init := &Function{ init := &Function{
@ -42,8 +44,12 @@ func (prog *Program) CreateTestMainPackage(pkgs ...*Package) *Package {
Prog: prog, Prog: prog,
} }
init.startBody() init.startBody()
// TODO(adonovan): use lexical order.
var expfuncs []*Function // all exported functions of *_test.go in pkgs, unordered var expfuncs []*Function // all exported functions of *_test.go in pkgs, unordered
for _, pkg := range pkgs { for _, pkg := range pkgs {
if pkg.Prog != prog {
panic("wrong Program")
}
// Initialize package to test. // Initialize package to test.
var v Call var v Call
v.Call.Value = pkg.init v.Call.Value = pkg.init
@ -135,6 +141,8 @@ func (prog *Program) CreateTestMainPackage(pkgs ...*Package) *Package {
sanityCheckPackage(testmain) sanityCheckPackage(testmain)
} }
prog.packages[testmain.Object] = testmain
return testmain return testmain
} }