1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 "golang.org/x/tools/go/packages"
19 "golang.org/x/tools/go/ssa/ssautil"
20 "golang.org/x/tools/internal/testenv"
23 const hello = `package main
28 fmt.Println("Hello, world")
32 func TestBuildPackage(t *testing.T) {
33 // There is a more substantial test of BuildPackage and the
34 // SSA program it builds in ../ssa/builder_test.go.
36 fset := token.NewFileSet()
37 f, err := parser.ParseFile(fset, "hello.go", hello, 0)
42 pkg := types.NewPackage("hello", "")
43 ssapkg, _, err := ssautil.BuildPackage(&types.Config{Importer: importer.Default()}, fset, pkg, []*ast.File{f}, 0)
47 if pkg.Name() != "main" {
48 t.Errorf("pkg.Name() = %s, want main", pkg.Name())
50 if ssapkg.Func("main") == nil {
51 ssapkg.WriteTo(os.Stderr)
52 t.Errorf("ssapkg has no main function")
56 func TestPackages(t *testing.T) {
57 testenv.NeedsGoPackages(t)
59 cfg := &packages.Config{Mode: packages.LoadSyntax}
60 initial, err := packages.Load(cfg, "bytes")
64 if packages.PrintErrors(initial) > 0 {
65 t.Fatal("there were errors")
68 prog, pkgs := ssautil.Packages(initial, 0)
69 bytesNewBuffer := pkgs[0].Func("NewBuffer")
70 bytesNewBuffer.Pkg.Build()
72 // We'll dump the SSA of bytes.NewBuffer because it is small and stable.
73 out := new(bytes.Buffer)
74 bytesNewBuffer.WriteTo(out)
76 // For determinism, sanitize the location.
77 location := prog.Fset.Position(bytesNewBuffer.Pos()).String()
78 got := strings.Replace(out.String(), location, "$GOROOT/src/bytes/buffer.go:1", -1)
81 # Name: bytes.NewBuffer
83 # Location: $GOROOT/src/bytes/buffer.go:1
84 func NewBuffer(buf []byte) *Buffer:
86 t0 = new Buffer (complit) *Buffer
87 t1 = &t0.buf [#0] *[]byte
93 t.Errorf("bytes.NewBuffer SSA = <<%s>>, want <<%s>>", got, want)
97 func TestBuildPackage_MissingImport(t *testing.T) {
98 fset := token.NewFileSet()
99 f, err := parser.ParseFile(fset, "bad.go", `package bad; import "missing"`, 0)
104 pkg := types.NewPackage("bad", "")
105 ssapkg, _, err := ssautil.BuildPackage(new(types.Config), fset, pkg, []*ast.File{f}, 0)
106 if err == nil || ssapkg != nil {
107 t.Fatal("BuildPackage succeeded unexpectedly")
111 func TestIssue28106(t *testing.T) {
112 testenv.NeedsGoPackages(t)
114 // In go1.10, go/packages loads all packages from source, not
115 // export data, but does not type check function bodies of
116 // imported packages. This test ensures that we do not attempt
117 // to run the SSA builder on functions without type information.
118 cfg := &packages.Config{Mode: packages.LoadSyntax}
119 pkgs, err := packages.Load(cfg, "runtime")
123 prog, _ := ssautil.Packages(pkgs, 0)
124 prog.Build() // no crash