9.7 Unit Test
逃逸分析发生在编译器内联操作之后,所以可以参考内联的UT来设置逃逸分析的UT, 在测试函数的最后调用逃逸分析的入口函数 escape.Funcs(typecheck.Target.Decls)
即可。我们创建文件 cmd/compile/internal/noder/escape_test.go
用来测试示例分析中的代码,完整的内容如下:
package noder
import (
"bufio"
"cmd/compile/internal/amd64"
"cmd/compile/internal/base"
"cmd/compile/internal/escape"
"cmd/compile/internal/inline"
"cmd/compile/internal/ir"
"cmd/compile/internal/reflectdata"
"cmd/compile/internal/ssa"
"cmd/compile/internal/ssagen"
"cmd/compile/internal/syntax"
"cmd/compile/internal/typecheck"
"cmd/compile/internal/types"
"cmd/compile/internal/types2"
"cmd/internal/obj"
"cmd/internal/objabi"
"cmd/internal/src"
"fmt"
"os"
"strings"
"testing"
)
func parseSrc(path, src string) (*syntax.File, error) {
var mode syntax.Mode
mode = syntax.AllowGenerics | syntax.CheckBranches
errh := func(error) {} // dummy error handler so that parsing continues in presence of errors
return syntax.Parse(syntax.NewFileBase(path), strings.NewReader(src), errh, nil, mode)
}
func defaultImporter() *gcimports {
return &gcimports{
packages: map[string]*types2.Package{},
}
}
func init() {
amd64.Init(&ssagen.Arch)
base.Ctxt = obj.Linknew(ssagen.Arch.LinkArch)
base.Ctxt.DiagFunc = base.Errorf
base.Ctxt.DiagFlush = base.FlushErrors
base.Ctxt.Bso = bufio.NewWriter(os.Stdout)
base.Ctxt.UseBASEntries = base.Ctxt.Headtype != objabi.Hdarwin
types.LocalPkg = types.NewPkg("", "")
types.LocalPkg.Prefix = "\"\""
types.LocalPkg.Height = types.MaxPkgHeight
types.BuiltinPkg = types.NewPkg("go.builtin", "") // TODO(gri) name this package go.builtin?
types.BuiltinPkg.Prefix = "go.builtin" // not go%2ebuiltin
ir.Pkgs.Unsafe = types.NewPkg("unsafe", "unsafe")
ir.Pkgs.Runtime = types.NewPkg("go.runtime", "runtime")
ir.Pkgs.Runtime.Prefix = "runtime"
ir.Pkgs.Itab = types.NewPkg("go.itab", "go.itab")
ir.Pkgs.Itab.Prefix = "go.itab" // not go%2eitab
ir.Pkgs.Go = types.NewPkg("go", "")
base.DebugSSA = ssa.PhaseOption
ssagen.Arch.LinkArch.Init(base.Ctxt)
ir.EscFmt = escape.Fmt
ir.IsIntrinsicCall = ssagen.IsIntrinsicCall
inline.SSADumpInline = ssagen.DumpInline
ssagen.InitEnv()
ssagen.InitTables()
types.PtrSize = ssagen.Arch.LinkArch.PtrSize
types.RegSize = ssagen.Arch.LinkArch.RegSize
types.MaxWidth = ssagen.Arch.MAXWIDTH
typecheck.Target = new(ir.Package)
typecheck.NeedITab = func(t, iface *types.Type) { reflectdata.ITabAddr(t, iface) }
typecheck.NeedRuntimeType = reflectdata.NeedRuntimeType // TODO(rsc): TypeSym for lock?
base.AutogeneratedPos = base.Ctxt.PosTable.XPos(src.MakePos(src.NewFileBase("<autogenerated>", "<autogenerated>"), 1, 0))
typecheck.InitUniverse()
}
func dumpIrTree(nodes []ir.Node) {
for _, n := range nodes {
s := fmt.Sprintf("\nafter noder2 %v", n)
ir.Dump(s, n)
}
}
func TestEscapeAnalysis(t *testing.T) {
code := `
package p
type T struct {
Name string
}
func GetT() **T {
var t T
l1 := &t
l2 := &l1
r3 := l1
r4 :=&r3
var l4 **T
l4 = l2
l4 = r4
return l4
}
`
base.Flag.LowerM = 2
f, err := parseSrc("Escape", code)
if err != nil {
panic(err)
}
var conf types2.Config
conf.Trace = false
conf.Importer = defaultImporter()
conf.Error = func(err error) {
fmt.Printf("Typecheck Error: %v\n", err)
}
info := types2.Info{
Types: make(map[syntax.Expr]types2.TypeAndValue),
Defs: make(map[*syntax.Name]types2.Object),
Uses: make(map[*syntax.Name]types2.Object),
Selections: make(map[*syntax.SelectorExpr]*types2.Selection),
Implicits: make(map[syntax.Node]types2.Object),
Scopes: make(map[syntax.Node]*types2.Scope),
Inferred: make(map[syntax.Expr]types2.Inferred),
}
pkg, err := conf.Check("<no package>", []*syntax.File{f}, &info)
if err != nil {
panic(err)
}
var m posMap
g := irgen{
target: typecheck.Target,
self: pkg,
info: &info,
posMap: m,
objs: make(map[types2.Object]*ir.Name),
typs: make(map[types2.Type]*types.Type),
}
p := &noder{
err: make(chan syntax.Error),
trackScopes: base.Flag.Dwarf,
file: f,
}
g.generate([]*noder{p})
escape.Funcs(typecheck.Target.Decls)
}
运行函数 TestEscapeAnalysis()
便可以看到逃逸分析的结果,可以通过修改 base.Flag.LowerM
的数值来控制编译器日志的打印。
最后更新于