1 // Copyright 2014 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.
5 // Incomplete source tree on Android.
12 // This file runs the pointer analysis on all packages and tests beneath
13 // $GOROOT. It provides a "smoke test" that the analysis doesn't crash
14 // on a large input, and a benchmark for performance measurement.
16 // Because it is relatively slow, the --stdlib flag must be enabled for
18 // % go test -v golang.org/x/tools/go/pointer --stdlib
27 "golang.org/x/tools/go/buildutil"
28 "golang.org/x/tools/go/loader"
29 "golang.org/x/tools/go/ssa"
30 "golang.org/x/tools/go/ssa/ssautil"
33 var runStdlibTest = flag.Bool("stdlib", false, "Run the (slow) stdlib test")
35 func TestStdlib(t *testing.T) {
37 t.Skip("skipping (slow) stdlib test (use --stdlib)")
40 // Load, parse and type-check the program.
41 ctxt := build.Default // copy
42 ctxt.GOPATH = "" // disable GOPATH
43 conf := loader.Config{Build: &ctxt}
44 if _, err := conf.FromArgs(buildutil.AllPackages(conf.Build), true); err != nil {
45 t.Errorf("FromArgs failed: %v", err)
49 iprog, err := conf.Load()
51 t.Fatalf("Load failed: %v", err)
54 // Create SSA packages.
55 prog := ssautil.CreateProgram(iprog, 0)
58 numPkgs := len(prog.AllPackages())
59 if want := 240; numPkgs < want {
60 t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
63 // Determine the set of packages/tests to analyze.
64 var mains []*ssa.Package
65 for _, info := range iprog.InitialPackages() {
66 ssapkg := prog.Package(info.Pkg)
67 if main := prog.CreateTestMainPackage(ssapkg); main != nil {
68 mains = append(mains, main)
72 t.Fatal("no tests found in analysis scope")
77 Reflection: false, // TODO(adonovan): fix remaining bug in rVCallConstraint, then enable.
81 // TODO(adonovan): add some query values (affects track bits).
85 result, err := Analyze(config)
87 t.Fatal(err) // internal error in pointer analysis
89 _ = result // TODO(adonovan): measure something
93 // Dump some statistics.
94 allFuncs := ssautil.AllFunctions(prog)
96 for fn := range allFuncs {
97 for _, b := range fn.Blocks {
98 numInstrs += len(b.Instrs)
102 // determine line count
104 prog.Fset.Iterate(func(f *token.File) bool {
105 lineCount += f.LineCount()
109 t.Log("#Source lines: ", lineCount)
110 t.Log("#Instructions: ", numInstrs)
111 t.Log("Pointer analysis: ", t1.Sub(t0))