Skip to content

Commit

Permalink
go/ssa: remove loader in module-unrelated test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuschen committed Sep 18, 2024
1 parent 0355013 commit 5bd4573
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 145 deletions.
10 changes: 0 additions & 10 deletions go/ssa/builder_generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,16 +842,6 @@ func TestInstructionString(t *testing.T) {
}
}

// packageName is a test helper to extract the package name from a string
// containing the content of a go file.
func packageName(t testing.TB, content string) string {
f, err := parser.ParseFile(token.NewFileSet(), "", content, parser.PackageClauseOnly)
if err != nil {
t.Fatalf("parsing the file %q failed with error: %s", content, err)
}
return f.Name.Name
}

func logFunction(t testing.TB, fn *ssa.Function) {
// TODO: Consider adding a ssa.Function.GoString() so this can be logged to t via '%#v'.
var buf bytes.Buffer
Expand Down
118 changes: 19 additions & 99 deletions go/ssa/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,23 +556,7 @@ func h(error)
// t8 = phi [1: t7, 3: t4] #e
// ...

// Parse
var conf loader.Config
f, err := conf.ParseFile("<input>", input)
if err != nil {
t.Fatalf("parse: %v", err)
}
conf.CreateFromFiles("p", f)

// Load
lprog, err := conf.Load()
if err != nil {
t.Fatalf("Load: %v", err)
}

// Create and build SSA
prog := ssautil.CreateProgram(lprog, ssa.BuilderMode(0))
p := prog.Package(lprog.Package("p").Pkg)
p := loadPackageFromSingleFile(t, input, ssa.BuilderMode(0)).spkg
p.Build()
g := p.Func("g")

Expand Down Expand Up @@ -622,23 +606,7 @@ func LoadPointer(addr *unsafe.Pointer) (val unsafe.Pointer)
// func init func()
// var init$guard bool

// Parse
var conf loader.Config
f, err := conf.ParseFile("<input>", input)
if err != nil {
t.Fatalf("parse: %v", err)
}
conf.CreateFromFiles("p", f)

// Load
lprog, err := conf.Load()
if err != nil {
t.Fatalf("Load: %v", err)
}

// Create and build SSA
prog := ssautil.CreateProgram(lprog, ssa.BuilderMode(0))
p := prog.Package(lprog.Package("p").Pkg)
p := loadPackageFromSingleFile(t, input, ssa.BuilderMode(0)).spkg
p.Build()

if load := p.Func("Load"); load.Signature.TypeParams().Len() != 1 {
Expand Down Expand Up @@ -809,31 +777,22 @@ func TestTypeparamTest(t *testing.T) {

t.Logf("Input: %s\n", input)

ctx := build.Default // copy
ctx.GOROOT = "testdata" // fake goroot. Makes tests ~1s. tests take ~80s.

reportErr := func(err error) {
t.Error(err)
}
conf := loader.Config{Build: &ctx, TypeChecker: types.Config{Error: reportErr}}
if _, err := conf.FromArgs([]string{input}, true); err != nil {
t.Fatalf("FromArgs(%s) failed: %s", input, err)
}

iprog, err := conf.Load()
if iprog != nil {
for _, pkg := range iprog.Created {
for i, e := range pkg.Errors {
t.Errorf("Loading pkg %s error[%d]=%s", pkg, i, e)
}
}
}
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedSyntax |
packages.NeedTypesInfo |
packages.NeedDeps |
packages.NeedName |
packages.NeedFiles |
packages.NeedImports |
packages.NeedCompiledGoFiles |
packages.NeedTypes,
}, input)
if err != nil {
t.Fatalf("conf.Load(%s) failed: %s", input, err)
t.Fatalf("fail to load pkgs from file %s", input)
}

mode := ssa.SanityCheckFunctions | ssa.InstantiateGenerics
prog := ssautil.CreateProgram(iprog, mode)
prog, _ := ssautil.Packages(pkgs, mode)
prog.Build()
})
}
Expand All @@ -856,23 +815,7 @@ func sliceMax(s []int) []int { return s[a():b():c()] }
`

// Parse
var conf loader.Config
f, err := conf.ParseFile("<input>", input)
if err != nil {
t.Fatalf("parse: %v", err)
}
conf.CreateFromFiles("p", f)

// Load
lprog, err := conf.Load()
if err != nil {
t.Fatalf("Load: %v", err)
}

// Create and build SSA
prog := ssautil.CreateProgram(lprog, ssa.BuilderMode(0))
p := prog.Package(lprog.Package("p").Pkg)
p := loadPackageFromSingleFile(t, input, ssa.BuilderMode(0)).spkg
p.Build()

for _, item := range []struct {
Expand Down Expand Up @@ -1176,20 +1119,7 @@ func TestLabels(t *testing.T) {
func main() { _:println(1); _:println(2)}`,
}
for _, test := range tests {
conf := loader.Config{Fset: token.NewFileSet()}
f, err := parser.ParseFile(conf.Fset, "<input>", test, 0)
if err != nil {
t.Errorf("parse error: %s", err)
return
}
conf.CreateFromFiles("main", f)
iprog, err := conf.Load()
if err != nil {
t.Error(err)
continue
}
prog := ssautil.CreateProgram(iprog, ssa.BuilderMode(0))
pkg := prog.Package(iprog.Created[0].Pkg)
pkg := loadPackageFromSingleFile(t, test, ssa.BuilderMode(0)).spkg
pkg.Build()
}
}
Expand Down Expand Up @@ -1224,20 +1154,10 @@ func TestIssue67079(t *testing.T) {

// Load the package.
const src = `package p; type T int; func (T) f() {}; var _ = (*T).f`
conf := loader.Config{Fset: token.NewFileSet()}
f, err := parser.ParseFile(conf.Fset, "p.go", src, 0)
if err != nil {
t.Fatal(err)
}
conf.CreateFromFiles("p", f)
iprog, err := conf.Load()
if err != nil {
t.Fatal(err)
}
pkg := iprog.Created[0].Pkg

// Create and build SSA program.
prog := ssautil.CreateProgram(iprog, ssa.BuilderMode(0))
p := loadPackageFromSingleFile(t, src, ssa.BuilderMode(0)).spkg
pkg := p.Pkg
prog := p.Prog
prog.Build()

var g errgroup.Group
Expand Down
1 change: 1 addition & 0 deletions go/ssa/instantiate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package ssa

// Note: Tests use unexported method _Instances.
// TODO(yuchen): remove deprecated loader after these test cases are moved to ssa_test.

import (
"bytes"
Expand Down
68 changes: 37 additions & 31 deletions go/ssa/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,34 @@ func TestObjValueLookup(t *testing.T) {
t.Skipf("no testdata directory on %s", runtime.GOOS)
}

conf := loader.Config{ParserMode: parser.ParseComments}
src, err := os.ReadFile("testdata/objlookup.go")
src, err := os.ReadFile("testdata/objlookup.txtar")
if err != nil {
t.Fatal(err)
}
readFile := func(filename string) ([]byte, error) { return src, nil }
f, err := conf.ParseFile("testdata/objlookup.go", src)
if err != nil {
t.Fatal(err)
pkgs := packagesFromArchive(t, string(src))
prog, _ := ssautil.Packages(pkgs, ssa.BuilderMode(0))

info := getPkgInfo(prog, pkgs, "main")

if info == nil {
t.Fatalf("fail to get package main from loaded packages")
}
conf.CreateFromFiles("main", f)

conf := info.ppkg
mainInfo := conf.TypesInfo

f := info.file
mainPkg := info.spkg

readFile := func(_ string) ([]byte, error) {
// split the file content to get the exact file content,
// instead of using go/printer which re-formats the file and the positions are no longer exact
strs := strings.SplitAfter(string(src), "-- objlookup.go --\n")
if len(strs) != 2 {
t.Fatalf("expect to get 2 parts after splitting but got %d", len(strs))
}
return []byte(strs[1]), nil
}
// Maps each var Ident (represented "name:linenum") to the
// kind of ssa.Value we expect (represented "Constant", "&Alloc").
expectations := make(map[string]string)
Expand Down Expand Up @@ -82,15 +98,6 @@ func TestObjValueLookup(t *testing.T) {
expectations[key] = exp
}

iprog, err := conf.Load()
if err != nil {
t.Error(err)
return
}

prog := ssautil.CreateProgram(iprog, ssa.BuilderMode(0) /*|ssa.PrintFunctions*/)
mainInfo := iprog.Created[0]
mainPkg := prog.Package(mainInfo.Pkg)
mainPkg.SetDebugMode(true)
mainPkg.Build()

Expand Down Expand Up @@ -222,36 +229,35 @@ func checkVarValue(t *testing.T, prog *ssa.Program, pkg *ssa.Package, ref []ast.
// Ensure that, in debug mode, we can determine the ssa.Value
// corresponding to every ast.Expr.
func TestValueForExpr(t *testing.T) {
testValueForExpr(t, "testdata/valueforexpr.go")
testValueForExpr(t, "testdata/valueforexpr.txtar")
}

func TestValueForExprStructConv(t *testing.T) {
testValueForExpr(t, "testdata/structconv.go")
testValueForExpr(t, "testdata/structconv.txtar")
}

func testValueForExpr(t *testing.T, testfile string) {
if runtime.GOOS == "android" {
t.Skipf("no testdata dir on %s", runtime.GOOS)
}

conf := loader.Config{ParserMode: parser.ParseComments}
f, err := conf.ParseFile(testfile, nil)
src, err := os.ReadFile(testfile)
if err != nil {
t.Error(err)
return
t.Fatal(err)
}
conf.CreateFromFiles("main", f)

iprog, err := conf.Load()
if err != nil {
t.Error(err)
return
}
pkgs := packagesFromArchive(t, string(src))
prog, _ := ssautil.Packages(pkgs, ssa.BuilderMode(0))

mainInfo := iprog.Created[0]
info := getPkgInfo(prog, pkgs, "main")

if info == nil {
t.Fatalf("fail to get package main from loaded packages")
}
mainInfo := info.ppkg.TypesInfo
f := info.file
mainPkg := info.spkg

prog := ssautil.CreateProgram(iprog, ssa.BuilderMode(0))
mainPkg := prog.Package(mainInfo.Pkg)
mainPkg.SetDebugMode(true)
mainPkg.Build()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// +build ignore
-- go.mod --
module example.com
go 1.18

-- objlookup.go --

package main

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// +build ignore
-- go.mod --
module example.com
go 1.18

// This file is the input to TestValueForExprStructConv in identical_test.go,
-- structconv.go --
// This file is the input to TestValueForExprStructConv in source_test.go,
// which uses the same framework as TestValueForExpr does in source_test.go.
//
// In Go 1.8, struct conversions are permitted even when the struct types have
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//go:build ignore
// +build ignore
-- go.mod --
module example.com
go 1.18

-- valueforexpr.go --
package main

// This file is the input to TestValueForExpr in source_test.go, which
Expand Down
Loading

0 comments on commit 5bd4573

Please sign in to comment.