.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.1-0.20210319172145-bda8f5cee399 / 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
10 package 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
20 import (
21         "flag"
22         "go/build"
23         "go/token"
24         "testing"
25         "time"
26
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"
31 )
32
33 var runStdlibTest = flag.Bool("stdlib", false, "Run the (slow) stdlib test")
34
35 func TestStdlib(t *testing.T) {
36         if !*runStdlibTest {
37                 t.Skip("skipping (slow) stdlib test (use --stdlib)")
38         }
39
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)
46                 return
47         }
48
49         iprog, err := conf.Load()
50         if err != nil {
51                 t.Fatalf("Load failed: %v", err)
52         }
53
54         // Create SSA packages.
55         prog := ssautil.CreateProgram(iprog, 0)
56         prog.Build()
57
58         numPkgs := len(prog.AllPackages())
59         if want := 240; numPkgs < want {
60                 t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
61         }
62
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)
69                 }
70         }
71         if mains == nil {
72                 t.Fatal("no tests found in analysis scope")
73         }
74
75         // Run the analysis.
76         config := &Config{
77                 Reflection:     false, // TODO(adonovan): fix remaining bug in rVCallConstraint, then enable.
78                 BuildCallGraph: true,
79                 Mains:          mains,
80         }
81         // TODO(adonovan): add some query values (affects track bits).
82
83         t0 := time.Now()
84
85         result, err := Analyze(config)
86         if err != nil {
87                 t.Fatal(err) // internal error in pointer analysis
88         }
89         _ = result // TODO(adonovan): measure something
90
91         t1 := time.Now()
92
93         // Dump some statistics.
94         allFuncs := ssautil.AllFunctions(prog)
95         var numInstrs int
96         for fn := range allFuncs {
97                 for _, b := range fn.Blocks {
98                         numInstrs += len(b.Instrs)
99                 }
100         }
101
102         // determine line count
103         var lineCount int
104         prog.Fset.Iterate(func(f *token.File) bool {
105                 lineCount += f.LineCount()
106                 return true
107         })
108
109         t.Log("#Source lines:          ", lineCount)
110         t.Log("#Instructions:          ", numInstrs)
111         t.Log("Pointer analysis:       ", t1.Sub(t0))
112 }