Skip to content

Commit

Permalink
go/ssa: remove loader utility functions
Browse files Browse the repository at this point in the history
Removes the loader utility functions loadProgram and buildPackage.
Renames buildContent to buildPackages.

For golang/go#69556

Change-Id: I13b4f14d8d8aedaa3edbaa54d34a97cc2d56253f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/614717
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
timothy-king committed Sep 20, 2024
1 parent 6a0cacb commit 0b6abe3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 56 deletions.
2 changes: 1 addition & 1 deletion go/ssa/builder_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func TestGenericBodies(t *testing.T) {
pkgname := parsePackageClause(t, content)
t.Run(pkgname, func(t *testing.T) {
t.Parallel()
ssapkg, f := buildContent(t, content, ssa.SanityCheckFunctions)
ssapkg, f := buildPackage(t, content, ssa.SanityCheckFunctions)
fset := ssapkg.Prog.Fset

// Collect all notes in f, i.e. comments starting with "//@ types".
Expand Down
4 changes: 2 additions & 2 deletions go/ssa/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func init():
}
for _, test := range tests {
// Create a single-file main package.
mainPkg, _ := buildContent(t, test.input, test.mode)
mainPkg, _ := buildPackage(t, test.input, test.mode)
name := mainPkg.Pkg.Name()
initFunc := mainPkg.Func("init")
if initFunc == nil {
Expand Down Expand Up @@ -406,7 +406,7 @@ var (
t interface{} = new(struct{*T})
)
`
pkg, _ := buildContent(t, input, ssa.BuilderMode(0))
pkg, _ := buildPackage(t, input, ssa.BuilderMode(0))

// Enumerate reachable synthetic functions
want := map[string]string{
Expand Down
54 changes: 3 additions & 51 deletions go/ssa/instantiate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,10 @@ import (
"strings"
"testing"

"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)

// loadProgram creates loader.Program out of p.
func loadProgram(p string) (*loader.Program, error) {
// Parse
var conf loader.Config
f, err := conf.ParseFile("<input>", p)
if err != nil {
return nil, fmt.Errorf("parse: %v", err)
}
conf.CreateFromFiles("p", f)

// Load
lprog, err := conf.Load()
if err != nil {
return nil, fmt.Errorf("Load: %v", err)
}
return lprog, nil
}

// buildPackage builds and returns ssa representation of package pkg of lprog.
func buildPackage(lprog *loader.Program, pkg string, mode ssa.BuilderMode) *ssa.Package {
prog := ssa.NewProgram(lprog.Fset, mode)

for _, info := range lprog.AllPackages {
prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable)
}

p := prog.Package(lprog.Package(pkg).Pkg)
p.Build()
return p
}

// TestNeedsInstance ensures that new method instances can be created via MethodValue.
func TestNeedsInstance(t *testing.T) {
const input = `
Expand All @@ -72,17 +40,11 @@ func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
// func init func()
// var init$guard bool

lprog, err := loadProgram(input)
if err != nil {
t.Fatal(err)
}

for _, mode := range []ssa.BuilderMode{
ssa.SanityCheckFunctions,
ssa.SanityCheckFunctions | ssa.InstantiateGenerics,
} {
// Create and build SSA
p := buildPackage(lprog, "p", mode)
p, _ := buildPackage(t, input, mode)
prog := p.Prog

ptr := p.Type("Pointer").Type().(*types.Named)
Expand Down Expand Up @@ -185,12 +147,7 @@ func entry(i int, a A) int {
return Id[int](i)
}
`
lprog, err := loadProgram(input)
if err != nil {
t.Fatal(err)
}

p := buildPackage(lprog, "p", ssa.SanityCheckFunctions)
p, _ := buildPackage(t, input, ssa.SanityCheckFunctions)
all := ssautil.AllFunctions(p.Prog)

for _, ti := range []struct {
Expand Down Expand Up @@ -298,12 +255,7 @@ func Foo[T any, S any](t T, s S) {
Foo[T, S](t, s)
}
`
lprog, err := loadProgram(input)
if err != nil {
t.Fatal(err)
}

p := buildPackage(lprog, "p", ssa.SanityCheckFunctions)
p, _ := buildPackage(t, input, ssa.SanityCheckFunctions)

all := ssautil.AllFunctions(p.Prog)
for _, test := range []struct {
Expand Down
4 changes: 2 additions & 2 deletions go/ssa/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ func loadPackages(t testing.TB, src fs.FS, patterns ...string) []*packages.Packa
return pkgs
}

// buildContent builds the content of a go file into:
// buildPackage builds the content of a go file into:
// * a module with the same name as the package at the current go version,
// * loads the package (parses and types checks),
// * builds the package and its dependencies, and
// * returns the built package and the parsed file.
func buildContent(t testing.TB, content string, mode ssa.BuilderMode) (*ssa.Package, *ast.File) {
func buildPackage(t testing.TB, content string, mode ssa.BuilderMode) (*ssa.Package, *ast.File) {
name := parsePackageClause(t, content)

fs := overlayFS(map[string][]byte{
Expand Down

0 comments on commit 0b6abe3

Please sign in to comment.