| 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. |
| 4 |
|
| 5 |
package irutil_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"go/ast" |
| 10 |
"go/importer" |
| 11 |
"go/parser" |
| 12 |
"go/token" |
| 13 |
"go/types" |
| 14 |
"os" |
| 15 |
"path" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"honnef.co/go/tools/go/ir" |
| 20 |
"honnef.co/go/tools/go/ir/irutil" |
| 21 |
"honnef.co/go/tools/internal/xtools-internal/packagestest" |
| 22 |
"honnef.co/go/tools/internal/xtools-internal/testenv" |
| 23 |
|
| 24 |
"golang.org/x/tools/go/packages" |
| 25 |
) |
| 26 |
|
| 27 |
const hello = `package main |
| 28 |
|
| 29 |
import "fmt" |
| 30 |
|
| 31 |
func main() { |
| 32 |
fmt.Println("Hello, world") |
| 33 |
} |
| 34 |
` |
| 35 |
|
| 36 |
func TestBuildPackage(t *testing.T) { |
| 37 |
testenv.NeedsGoBuild(t) // for importer.Default() |
| 38 |
|
| 39 |
// There is a more substantial test of BuildPackage and the |
| 40 |
// IR program it builds in ../ir/builder_test.go. |
| 41 |
|
| 42 |
fset := token.NewFileSet() |
| 43 |
f, err := parser.ParseFile(fset, "hello.go", hello, parser.SkipObjectResolution) |
| 44 |
if err != nil { |
| 45 |
t.Fatal(err) |
| 46 |
} |
| 47 |
|
| 48 |
for _, mode := range []ir.BuilderMode{ |
| 49 |
ir.SanityCheckFunctions, |
| 50 |
ir.InstantiateGenerics | ir.SanityCheckFunctions, |
| 51 |
} { |
| 52 |
pkg := types.NewPackage("hello", "") |
| 53 |
irpkg, _, err := irutil.BuildPackage(&types.Config{Importer: importer.Default()}, fset, pkg, []*ast.File{f}, mode) |
| 54 |
if err != nil { |
| 55 |
t.Fatal(err) |
| 56 |
} |
| 57 |
if pkg.Name() != "main" { |
| 58 |
t.Errorf("pkg.Name() = %s, want main", pkg.Name()) |
| 59 |
} |
| 60 |
if irpkg.Func("main") == nil { |
| 61 |
irpkg.WriteTo(os.Stderr) |
| 62 |
t.Errorf("irpkg has no main function") |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func TestPackages(t *testing.T) { |
| 69 |
testenv.NeedsGoPackages(t) |
| 70 |
|
| 71 |
cfg := &packages.Config{Mode: packages.LoadSyntax} |
| 72 |
initial, err := packages.Load(cfg, "bytes") |
| 73 |
if err != nil { |
| 74 |
t.Fatal(err) |
| 75 |
} |
| 76 |
if packages.PrintErrors(initial) > 0 { |
| 77 |
t.Fatal("there were errors") |
| 78 |
} |
| 79 |
|
| 80 |
for _, mode := range []ir.BuilderMode{ |
| 81 |
ir.SanityCheckFunctions, |
| 82 |
ir.SanityCheckFunctions | ir.InstantiateGenerics, |
| 83 |
} { |
| 84 |
prog, pkgs := irutil.Packages(initial, mode) |
| 85 |
bytesNewBuffer := pkgs[0].Func("NewBuffer") |
| 86 |
bytesNewBuffer.Pkg.Build() |
| 87 |
|
| 88 |
// We'll dump the IR of bytes.NewBuffer because it is small and stable. |
| 89 |
out := new(bytes.Buffer) |
| 90 |
bytesNewBuffer.WriteTo(out) |
| 91 |
|
| 92 |
// For determinism, sanitize the location. |
| 93 |
location := prog.Fset.Position(bytesNewBuffer.Pos()).String() |
| 94 |
got := strings.Replace(out.String(), location, "$GOROOT/src/bytes/buffer.go:1", -1) |
| 95 |
|
| 96 |
want := ` |
| 97 |
# Name: bytes.NewBuffer |
| 98 |
# Package: bytes |
| 99 |
# Location: $GOROOT/src/bytes/buffer.go:1 |
| 100 |
func NewBuffer(buf []byte) *Buffer: |
| 101 |
0: entry P:0 S:0 |
| 102 |
t1 = new Buffer (complit) *Buffer |
| 103 |
t2 = {buf: buf, off: 0:int, lastRead: 0:readOp} Buffer |
| 104 |
*t1 = t2 |
| 105 |
return t1 |
| 106 |
|
| 107 |
`[1:] |
| 108 |
if got != want { |
| 109 |
t.Errorf("bytes.NewBuffer IR = <<%s>>, want <<%s>>", got, want) |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func TestBuildPackage_MissingImport(t *testing.T) { |
| 115 |
fset := token.NewFileSet() |
| 116 |
f, err := parser.ParseFile(fset, "bad.go", `package bad; import "missing"`, parser.SkipObjectResolution) |
| 117 |
if err != nil { |
| 118 |
t.Fatal(err) |
| 119 |
} |
| 120 |
|
| 121 |
pkg := types.NewPackage("bad", "") |
| 122 |
irpkg, _, err := irutil.BuildPackage(new(types.Config), fset, pkg, []*ast.File{f}, 0) |
| 123 |
if err == nil || irpkg != nil { |
| 124 |
t.Fatal("BuildPackage succeeded unexpectedly") |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func TestIssue28106(t *testing.T) { |
| 129 |
testenv.NeedsGoPackages(t) |
| 130 |
|
| 131 |
// In go1.10, go/packages loads all packages from source, not |
| 132 |
// export data, but does not type check function bodies of |
| 133 |
// imported packages. This test ensures that we do not attempt |
| 134 |
// to run the IR builder on functions without type information. |
| 135 |
cfg := &packages.Config{Mode: packages.LoadSyntax} |
| 136 |
pkgs, err := packages.Load(cfg, "runtime") |
| 137 |
if err != nil { |
| 138 |
t.Fatal(err) |
| 139 |
} |
| 140 |
prog, _ := irutil.Packages(pkgs, 0) |
| 141 |
prog.Build() // no crash |
| 142 |
} |
| 143 |
|
| 144 |
func TestIssue53604(t *testing.T) { |
| 145 |
// Tests that variable initializers are not added to init() when syntax |
| 146 |
// is not present but types.Info is available. |
| 147 |
// |
| 148 |
// Packages x, y, z are loaded with mode `packages.LoadSyntax`. |
| 149 |
// Package x imports y, and y imports z. |
| 150 |
// Packages are built using irutil.Packages() with x and z as roots. |
| 151 |
// This setup creates y using CreatePackage(pkg, files, info, ...) |
| 152 |
// where len(files) == 0 but info != nil. |
| 153 |
// |
| 154 |
// Tests that globals from y are not initialized. |
| 155 |
e := packagestest.Export(t, packagestest.Modules, []packagestest.Module{ |
| 156 |
{ |
| 157 |
Name: "golang.org/fake", |
| 158 |
Files: map[string]any{ |
| 159 |
"x/x.go": `package x; import "golang.org/fake/y"; var V = y.F()`, |
| 160 |
"y/y.go": `package y; import "golang.org/fake/z"; var F = func () *int { return &z.Z } `, |
| 161 |
"z/z.go": `package z; var Z int`, |
| 162 |
}, |
| 163 |
}, |
| 164 |
}) |
| 165 |
defer e.Cleanup() |
| 166 |
|
| 167 |
// Load x and z as entry packages using packages.LoadSyntax |
| 168 |
e.Config.Mode = packages.LoadSyntax |
| 169 |
pkgs, err := packages.Load(e.Config, path.Join(e.Temp(), "fake/x"), path.Join(e.Temp(), "fake/z")) |
| 170 |
if err != nil { |
| 171 |
t.Fatal(err) |
| 172 |
} |
| 173 |
for _, p := range pkgs { |
| 174 |
if len(p.Errors) > 0 { |
| 175 |
t.Fatalf("%v", p.Errors) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
prog, _ := irutil.Packages(pkgs, ir.BuilderMode(0)) |
| 180 |
prog.Build() |
| 181 |
|
| 182 |
// y does not initialize F. |
| 183 |
y := prog.ImportedPackage("golang.org/fake/y") |
| 184 |
if y == nil { |
| 185 |
t.Fatal("Failed to load intermediate package y") |
| 186 |
} |
| 187 |
yinit := y.Members["init"].(*ir.Function) |
| 188 |
for _, bb := range yinit.Blocks { |
| 189 |
for _, i := range bb.Instrs { |
| 190 |
if store, ok := i.(*ir.Store); ok && store.Addr == y.Var("F") { |
| 191 |
t.Errorf("y.init() stores to F %v", store) |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |