GoPLS Viewer

Home|gopls/go/pointer/stdlib_test.go
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.
4
5// Incomplete source tree on Android.
6
7//go:build !android
8// +build !android
9
10package pointer
11
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.
15//
16// Because it is relatively slow, the --stdlib flag must be enabled for
17// this test to run:
18//    % go test -v golang.org/x/tools/go/pointer --stdlib
19
20import (
21    "flag"
22    "go/token"
23    "testing"
24    "time"
25
26    "golang.org/x/tools/go/packages"
27    "golang.org/x/tools/go/ssa"
28    "golang.org/x/tools/go/ssa/ssautil"
29)
30
31var runStdlibTest = flag.Bool("stdlib"false"Run the (slow) stdlib test")
32
33func TestStdlib(t *testing.T) {
34    if !*runStdlibTest {
35        t.Skip("skipping (slow) stdlib test (use --stdlib)")
36    }
37
38    cfg := &packages.Config{
39        Modepackages.LoadAllSyntax,
40        // Create test main packages with a main function.
41        Teststrue,
42    }
43    pkgserr := packages.Load(cfg"std")
44    if err != nil || packages.PrintErrors(pkgs) > 0 {
45        t.Fatalf("Load failed: %v"err)
46    }
47
48    // Create SSA packages.
49    prog_ := ssautil.AllPackages(pkgsssa.InstantiateGenerics)
50    prog.Build()
51
52    numPkgs := len(prog.AllPackages())
53    if want := 240numPkgs < want {
54        t.Errorf("Loaded only %d packages, want at least %d"numPkgswant)
55    }
56
57    // Determine the set of packages/tests to analyze.
58    var mains []*ssa.Package
59    for _ssapkg := range prog.AllPackages() {
60        if ssapkg.Pkg.Name() == "main" && ssapkg.Func("main") != nil {
61            mains = append(mainsssapkg)
62        }
63    }
64    if mains == nil {
65        t.Fatal("no tests found in analysis scope")
66    }
67
68    // Run the analysis.
69    config := &Config{
70        Reflection:     false// TODO(adonovan): fix remaining bug in rVCallConstraint, then enable.
71        BuildCallGraphtrue,
72        Mains:          mains,
73    }
74    // TODO(adonovan): add some query values (affects track bits).
75
76    t0 := time.Now()
77
78    resulterr := Analyze(config)
79    if err != nil {
80        t.Fatal(err// internal error in pointer analysis
81    }
82    _ = result // TODO(adonovan): measure something
83
84    t1 := time.Now()
85
86    // Dump some statistics.
87    allFuncs := ssautil.AllFunctions(prog)
88    var numInstrs int
89    for fn := range allFuncs {
90        for _b := range fn.Blocks {
91            numInstrs += len(b.Instrs)
92        }
93    }
94
95    // determine line count
96    var lineCount int
97    prog.Fset.Iterate(func(f *token.Filebool {
98        lineCount += f.LineCount()
99        return true
100    })
101
102    t.Log("#Source lines:          "lineCount)
103    t.Log("#Instructions:          "numInstrs)
104    t.Log("Pointer analysis:       "t1.Sub(t0))
105}
106
MembersX
TestStdlib.err
TestStdlib.RangeStmt_1552.ssapkg
TestStdlib.config
TestStdlib.allFuncs
TestStdlib.cfg
TestStdlib.t1
TestStdlib.RangeStmt_2304.BlockStmt.RangeStmt_2333.b
flag
time
TestStdlib.pkgs
TestStdlib._
TestStdlib.t0
TestStdlib.result
TestStdlib.numInstrs
TestStdlib
TestStdlib.t
TestStdlib.prog
TestStdlib.numPkgs
TestStdlib.want
TestStdlib.mains
TestStdlib.RangeStmt_2304.fn
TestStdlib.lineCount
Members
X