Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / go / ssa / stdlib_test.go
1 // Copyright 2013 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 // +build !android
8
9 package ssa_test
10
11 // This file runs the SSA builder in sanity-checking mode on all
12 // packages beneath $GOROOT and prints some summary information.
13 //
14 // Run with "go test -cpu=8 to" set GOMAXPROCS.
15
16 import (
17         "go/ast"
18         "go/build"
19         "go/token"
20         "runtime"
21         "testing"
22         "time"
23
24         "golang.org/x/tools/go/buildutil"
25         "golang.org/x/tools/go/loader"
26         "golang.org/x/tools/go/ssa"
27         "golang.org/x/tools/go/ssa/ssautil"
28         "golang.org/x/tools/internal/testenv"
29 )
30
31 func bytesAllocated() uint64 {
32         runtime.GC()
33         var stats runtime.MemStats
34         runtime.ReadMemStats(&stats)
35         return stats.Alloc
36 }
37
38 func TestStdlib(t *testing.T) {
39         if testing.Short() {
40                 t.Skip("skipping in short mode; too slow (https://golang.org/issue/14113)")
41         }
42         testenv.NeedsTool(t, "go")
43
44         // Load, parse and type-check the program.
45         t0 := time.Now()
46         alloc0 := bytesAllocated()
47
48         // Load, parse and type-check the program.
49         ctxt := build.Default // copy
50         ctxt.GOPATH = ""      // disable GOPATH
51         conf := loader.Config{Build: &ctxt}
52         for _, path := range buildutil.AllPackages(conf.Build) {
53                 conf.ImportWithTests(path)
54         }
55
56         iprog, err := conf.Load()
57         if err != nil {
58                 t.Fatalf("Load failed: %v", err)
59         }
60
61         t1 := time.Now()
62         alloc1 := bytesAllocated()
63
64         // Create SSA packages.
65         var mode ssa.BuilderMode
66         // Comment out these lines during benchmarking.  Approx SSA build costs are noted.
67         mode |= ssa.SanityCheckFunctions // + 2% space, + 4% time
68         mode |= ssa.GlobalDebug          // +30% space, +18% time
69         prog := ssautil.CreateProgram(iprog, mode)
70
71         t2 := time.Now()
72
73         // Build SSA.
74         prog.Build()
75
76         t3 := time.Now()
77         alloc3 := bytesAllocated()
78
79         numPkgs := len(prog.AllPackages())
80         if want := 140; numPkgs < want {
81                 t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
82         }
83
84         // Keep iprog reachable until after we've measured memory usage.
85         if len(iprog.AllPackages) == 0 {
86                 panic("unreachable")
87         }
88
89         allFuncs := ssautil.AllFunctions(prog)
90
91         // Check that all non-synthetic functions have distinct names.
92         // Synthetic wrappers for exported methods should be distinct too,
93         // except for unexported ones (explained at (*Function).RelString).
94         byName := make(map[string]*ssa.Function)
95         for fn := range allFuncs {
96                 if fn.Synthetic == "" || ast.IsExported(fn.Name()) {
97                         str := fn.String()
98                         prev := byName[str]
99                         byName[str] = fn
100                         if prev != nil {
101                                 t.Errorf("%s: duplicate function named %s",
102                                         prog.Fset.Position(fn.Pos()), str)
103                                 t.Errorf("%s:   (previously defined here)",
104                                         prog.Fset.Position(prev.Pos()))
105                         }
106                 }
107         }
108
109         // Dump some statistics.
110         var numInstrs int
111         for fn := range allFuncs {
112                 for _, b := range fn.Blocks {
113                         numInstrs += len(b.Instrs)
114                 }
115         }
116
117         // determine line count
118         var lineCount int
119         prog.Fset.Iterate(func(f *token.File) bool {
120                 lineCount += f.LineCount()
121                 return true
122         })
123
124         // NB: when benchmarking, don't forget to clear the debug +
125         // sanity builder flags for better performance.
126
127         t.Log("GOMAXPROCS:           ", runtime.GOMAXPROCS(0))
128         t.Log("#Source lines:        ", lineCount)
129         t.Log("Load/parse/typecheck: ", t1.Sub(t0))
130         t.Log("SSA create:           ", t2.Sub(t1))
131         t.Log("SSA build:            ", t3.Sub(t2))
132
133         // SSA stats:
134         t.Log("#Packages:            ", numPkgs)
135         t.Log("#Functions:           ", len(allFuncs))
136         t.Log("#Instructions:        ", numInstrs)
137         t.Log("#MB AST+types:        ", int64(alloc1-alloc0)/1e6)
138         t.Log("#MB SSA:              ", int64(alloc3-alloc1)/1e6)
139 }