github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/pointer/stdlib_test.go (about) 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 github.com/powerman/golang-tools/go/pointer --stdlib 19 20 import ( 21 "flag" 22 "go/token" 23 "testing" 24 "time" 25 26 "github.com/powerman/golang-tools/go/packages" 27 "github.com/powerman/golang-tools/go/ssa" 28 "github.com/powerman/golang-tools/go/ssa/ssautil" 29 ) 30 31 var runStdlibTest = flag.Bool("stdlib", false, "Run the (slow) stdlib test") 32 33 func TestStdlib(t *testing.T) { 34 if !*runStdlibTest { 35 t.Skip("skipping (slow) stdlib test (use --stdlib)") 36 } 37 38 cfg := &packages.Config{ 39 Mode: packages.LoadAllSyntax, 40 // Create test main packages with a main function. 41 Tests: true, 42 } 43 pkgs, err := 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(pkgs, 0) 50 prog.Build() 51 52 numPkgs := len(prog.AllPackages()) 53 if want := 240; numPkgs < want { 54 t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want) 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(mains, ssapkg) 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 BuildCallGraph: true, 72 Mains: mains, 73 } 74 // TODO(adonovan): add some query values (affects track bits). 75 76 t0 := time.Now() 77 78 result, err := 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.File) bool { 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 }