Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/ssa: remove loader in module-unrelated test cases #524

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
113 changes: 10 additions & 103 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 := packageFromBytes(t, input, ssa.BuilderMode(0))
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 := packageFromBytes(t, input, ssa.BuilderMode(0))
p.Build()

if load := p.Func("Load"); load.Signature.TypeParams().Len() != 1 {
Expand Down Expand Up @@ -791,7 +759,7 @@ func TestTypeparamTest(t *testing.T) {
if err != nil {
t.Fatal(err)
}

fsys := os.DirFS(dir)
for _, entry := range list {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".go") {
continue // Consider standalone go files.
Expand All @@ -809,31 +777,9 @@ 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)
}
}
}
if err != nil {
t.Fatalf("conf.Load(%s) failed: %s", input, err)
}

pkgs := fromFS(t, fsys, input)
mode := ssa.SanityCheckFunctions | ssa.InstantiateGenerics
prog := ssautil.CreateProgram(iprog, mode)
prog, _ := ssautil.Packages(pkgs, mode)
prog.Build()
})
}
Expand All @@ -856,23 +802,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 := packageFromBytes(t, input, ssa.BuilderMode(0))
p.Build()

for _, item := range []struct {
Expand Down Expand Up @@ -1176,20 +1106,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 := packageFromBytes(t, test, ssa.BuilderMode(0))
pkg.Build()
}
}
Expand Down Expand Up @@ -1224,20 +1141,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 := packageFromBytes(t, src, ssa.BuilderMode(0))
pkg := p.Pkg
prog := p.Prog
prog.Build()

var g errgroup.Group
Expand Down
72 changes: 45 additions & 27 deletions go/ssa/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,48 @@ import (
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
"golang.org/x/tools/txtar"
)

func TestObjValueLookup(t *testing.T) {
if runtime.GOOS == "android" {
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)

fs, err := txtar.FS(txtar.Parse(src))
if err != nil {
t.Fatal(err)
}
conf.CreateFromFiles("main", f)

pkgs := fromFS(t, fs, ".")
prog, _ := ssautil.Packages(pkgs, ssa.BuilderMode(0))

info := getPkgInfo(pkgs, "main")

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

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

f := info.file
mainPkg := prog.Package(info.ppkg.Types)

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 +105,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 +236,40 @@ 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()
fs, err := txtar.FS(txtar.Parse(src))
if err != nil {
t.Error(err)
return
t.Fatal(err)
}

mainInfo := iprog.Created[0]
pkgs := fromFS(t, fs, ".")
prog, _ := ssautil.Packages(pkgs, ssa.BuilderMode(0))

info := getPkgInfo(pkgs, "main")

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

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