241 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			241 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			Go
		
	
	
	
// Copyright 2013 The Go Authors. All rights reserved.
 | 
						|
// Use of this source code is governed by a BSD-style
 | 
						|
// license that can be found in the LICENSE file.
 | 
						|
 | 
						|
package ssa_test
 | 
						|
 | 
						|
import (
 | 
						|
	"reflect"
 | 
						|
	"sort"
 | 
						|
	"strings"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"code.google.com/p/go.tools/go/loader"
 | 
						|
	"code.google.com/p/go.tools/go/ssa"
 | 
						|
	"code.google.com/p/go.tools/go/types"
 | 
						|
)
 | 
						|
 | 
						|
func isEmpty(f *ssa.Function) bool { return f.Blocks == nil }
 | 
						|
 | 
						|
// Tests that programs partially loaded from gc object files contain
 | 
						|
// functions with no code for the external portions, but are otherwise ok.
 | 
						|
func TestExternalPackages(t *testing.T) {
 | 
						|
	test := `
 | 
						|
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"bytes"
 | 
						|
	"io"
 | 
						|
	"testing"
 | 
						|
)
 | 
						|
 | 
						|
func main() {
 | 
						|
        var t testing.T
 | 
						|
	t.Parallel()    // static call to external declared method
 | 
						|
        t.Fail()        // static call to promoted external declared method
 | 
						|
        testing.Short() // static call to external package-level function
 | 
						|
 | 
						|
        var w io.Writer = new(bytes.Buffer)
 | 
						|
        w.Write(nil)    // interface invoke of external declared method
 | 
						|
}
 | 
						|
`
 | 
						|
 | 
						|
	// Create a single-file main package.
 | 
						|
	var conf loader.Config
 | 
						|
	f, err := conf.ParseFile("<input>", test, 0)
 | 
						|
	if err != nil {
 | 
						|
		t.Error(err)
 | 
						|
		return
 | 
						|
	}
 | 
						|
	conf.CreateFromFiles("main", f)
 | 
						|
 | 
						|
	iprog, err := conf.Load()
 | 
						|
	if err != nil {
 | 
						|
		t.Error(err)
 | 
						|
		return
 | 
						|
	}
 | 
						|
 | 
						|
	prog := ssa.Create(iprog, ssa.SanityCheckFunctions)
 | 
						|
	mainPkg := prog.Package(iprog.Created[0].Pkg)
 | 
						|
	mainPkg.Build()
 | 
						|
 | 
						|
	// The main package, its direct and indirect dependencies are loaded.
 | 
						|
	deps := []string{
 | 
						|
		// directly imported dependencies:
 | 
						|
		"bytes", "io", "testing",
 | 
						|
		// indirect dependencies (partial list):
 | 
						|
		"errors", "fmt", "os", "runtime",
 | 
						|
	}
 | 
						|
 | 
						|
	all := prog.AllPackages()
 | 
						|
	if len(all) <= len(deps) {
 | 
						|
		t.Errorf("unexpected set of loaded packages: %q", all)
 | 
						|
	}
 | 
						|
	for _, path := range deps {
 | 
						|
		pkg := prog.ImportedPackage(path)
 | 
						|
		if pkg == nil {
 | 
						|
			t.Errorf("package not loaded: %q", path)
 | 
						|
			continue
 | 
						|
		}
 | 
						|
 | 
						|
		// External packages should have no function bodies (except for wrappers).
 | 
						|
		isExt := pkg != mainPkg
 | 
						|
 | 
						|
		// init()
 | 
						|
		if isExt && !isEmpty(pkg.Func("init")) {
 | 
						|
			t.Errorf("external package %s has non-empty init", pkg)
 | 
						|
		} else if !isExt && isEmpty(pkg.Func("init")) {
 | 
						|
			t.Errorf("main package %s has empty init", pkg)
 | 
						|
		}
 | 
						|
 | 
						|
		for _, mem := range pkg.Members {
 | 
						|
			switch mem := mem.(type) {
 | 
						|
			case *ssa.Function:
 | 
						|
				// Functions at package level.
 | 
						|
				if isExt && !isEmpty(mem) {
 | 
						|
					t.Errorf("external function %s is non-empty", mem)
 | 
						|
				} else if !isExt && isEmpty(mem) {
 | 
						|
					t.Errorf("function %s is empty", mem)
 | 
						|
				}
 | 
						|
 | 
						|
			case *ssa.Type:
 | 
						|
				// Methods of named types T.
 | 
						|
				// (In this test, all exported methods belong to *T not T.)
 | 
						|
				if !isExt {
 | 
						|
					t.Fatalf("unexpected name type in main package: %s", mem)
 | 
						|
				}
 | 
						|
				mset := prog.MethodSets.MethodSet(types.NewPointer(mem.Type()))
 | 
						|
				for i, n := 0, mset.Len(); i < n; i++ {
 | 
						|
					m := prog.Method(mset.At(i))
 | 
						|
					// For external types, only synthetic wrappers have code.
 | 
						|
					expExt := !strings.Contains(m.Synthetic, "wrapper")
 | 
						|
					if expExt && !isEmpty(m) {
 | 
						|
						t.Errorf("external method %s is non-empty: %s",
 | 
						|
							m, m.Synthetic)
 | 
						|
					} else if !expExt && isEmpty(m) {
 | 
						|
						t.Errorf("method function %s is empty: %s",
 | 
						|
							m, m.Synthetic)
 | 
						|
					}
 | 
						|
				}
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	expectedCallee := []string{
 | 
						|
		"(*testing.T).Parallel",
 | 
						|
		"(*testing.common).Fail",
 | 
						|
		"testing.Short",
 | 
						|
		"N/A",
 | 
						|
	}
 | 
						|
	callNum := 0
 | 
						|
	for _, b := range mainPkg.Func("main").Blocks {
 | 
						|
		for _, instr := range b.Instrs {
 | 
						|
			switch instr := instr.(type) {
 | 
						|
			case ssa.CallInstruction:
 | 
						|
				call := instr.Common()
 | 
						|
				if want := expectedCallee[callNum]; want != "N/A" {
 | 
						|
					got := call.StaticCallee().String()
 | 
						|
					if want != got {
 | 
						|
						t.Errorf("call #%d from main.main: got callee %s, want %s",
 | 
						|
							callNum, got, want)
 | 
						|
					}
 | 
						|
				}
 | 
						|
				callNum++
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
	if callNum != 4 {
 | 
						|
		t.Errorf("in main.main: got %d calls, want %d", callNum, 4)
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
// TestTypesWithMethodSets tests that Package.TypesWithMethodSets includes all necessary types.
 | 
						|
func TestTypesWithMethodSets(t *testing.T) {
 | 
						|
	tests := []struct {
 | 
						|
		input string
 | 
						|
		want  []string
 | 
						|
	}{
 | 
						|
		// An exported package-level type is needed.
 | 
						|
		{`package A; type T struct{}; func (T) f() {}`,
 | 
						|
			[]string{"*p.T", "p.T"},
 | 
						|
		},
 | 
						|
		// An unexported package-level type is not needed.
 | 
						|
		{`package B; type t struct{}; func (t) f() {}`,
 | 
						|
			nil,
 | 
						|
		},
 | 
						|
		// Subcomponents of type of exported package-level var are needed.
 | 
						|
		{`package C; import "bytes"; var V struct {*bytes.Buffer}`,
 | 
						|
			[]string{"*struct{*bytes.Buffer}", "struct{*bytes.Buffer}"},
 | 
						|
		},
 | 
						|
		// Subcomponents of type of unexported package-level var are not needed.
 | 
						|
		{`package D; import "bytes"; var v struct {*bytes.Buffer}`,
 | 
						|
			nil,
 | 
						|
		},
 | 
						|
		// Subcomponents of type of exported package-level function are needed.
 | 
						|
		{`package E; import "bytes"; func F(struct {*bytes.Buffer}) {}`,
 | 
						|
			[]string{"struct{*bytes.Buffer}"},
 | 
						|
		},
 | 
						|
		// Subcomponents of type of unexported package-level function are not needed.
 | 
						|
		{`package F; import "bytes"; func f(struct {*bytes.Buffer}) {}`,
 | 
						|
			nil,
 | 
						|
		},
 | 
						|
		// Subcomponents of type of exported method of uninstantiated unexported type are not needed.
 | 
						|
		{`package G; import "bytes"; type x struct{}; func (x) G(struct {*bytes.Buffer}) {}; var v x`,
 | 
						|
			nil,
 | 
						|
		},
 | 
						|
		// ...unless used by MakeInterface.
 | 
						|
		{`package G2; import "bytes"; type x struct{}; func (x) G(struct {*bytes.Buffer}) {}; var v interface{} = x{}`,
 | 
						|
			[]string{"*p.x", "p.x", "struct{*bytes.Buffer}"},
 | 
						|
		},
 | 
						|
		// Subcomponents of type of unexported method are not needed.
 | 
						|
		{`package I; import "bytes"; type X struct{}; func (X) G(struct {*bytes.Buffer}) {}`,
 | 
						|
			[]string{"*p.X", "p.X", "struct{*bytes.Buffer}"},
 | 
						|
		},
 | 
						|
		// Local types aren't needed.
 | 
						|
		{`package J; import "bytes"; func f() { type T struct {*bytes.Buffer}; var t T; _ = t }`,
 | 
						|
			nil,
 | 
						|
		},
 | 
						|
		// ...unless used by MakeInterface.
 | 
						|
		{`package K; import "bytes"; func f() { type T struct {*bytes.Buffer}; _ = interface{}(T{}) }`,
 | 
						|
			[]string{"*p.T", "p.T"},
 | 
						|
		},
 | 
						|
		// Types used as operand of MakeInterface are needed.
 | 
						|
		{`package L; import "bytes"; func f() { _ = interface{}(struct{*bytes.Buffer}{}) }`,
 | 
						|
			[]string{"struct{*bytes.Buffer}"},
 | 
						|
		},
 | 
						|
		// MakeInterface is optimized away when storing to a blank.
 | 
						|
		{`package M; import "bytes"; var _ interface{} = struct{*bytes.Buffer}{}`,
 | 
						|
			nil,
 | 
						|
		},
 | 
						|
	}
 | 
						|
	for _, test := range tests {
 | 
						|
		// Create a single-file main package.
 | 
						|
		var conf loader.Config
 | 
						|
		f, err := conf.ParseFile("<input>", test.input, 0)
 | 
						|
		if err != nil {
 | 
						|
			t.Errorf("test %q: %s", test.input[:15], err)
 | 
						|
			continue
 | 
						|
		}
 | 
						|
		conf.CreateFromFiles("p", f)
 | 
						|
 | 
						|
		iprog, err := conf.Load()
 | 
						|
		if err != nil {
 | 
						|
			t.Errorf("test 'package %s': Load: %s", f.Name.Name, err)
 | 
						|
			continue
 | 
						|
		}
 | 
						|
		prog := ssa.Create(iprog, ssa.SanityCheckFunctions)
 | 
						|
		mainPkg := prog.Package(iprog.Created[0].Pkg)
 | 
						|
		prog.BuildAll()
 | 
						|
 | 
						|
		var typstrs []string
 | 
						|
		for _, T := range mainPkg.TypesWithMethodSets() {
 | 
						|
			typstrs = append(typstrs, T.String())
 | 
						|
		}
 | 
						|
		sort.Strings(typstrs)
 | 
						|
 | 
						|
		if !reflect.DeepEqual(typstrs, test.want) {
 | 
						|
			t.Errorf("test 'package %s': got %q, want %q", f.Name.Name, typstrs, test.want)
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 |