github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/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  // +build !android
     8  
     9  package pointer
    10  
    11  // This file runs the pointer analysis on all packages and tests beneath
    12  // $GOROOT.  It provides a "smoke test" that the analysis doesn't crash
    13  // on a large input, and a benchmark for performance measurement.
    14  //
    15  // Because it is relatively slow, the --stdlib flag must be enabled for
    16  // this test to run:
    17  //    % go test -v golang.org/x/tools/go/pointer --stdlib
    18  
    19  import (
    20  	"flag"
    21  	"go/build"
    22  	"go/token"
    23  	"testing"
    24  	"time"
    25  
    26  	"golang.org/x/tools/go/buildutil"
    27  	"golang.org/x/tools/go/loader"
    28  	"golang.org/x/tools/go/ssa"
    29  	"golang.org/x/tools/go/ssa/ssautil"
    30  )
    31  
    32  var runStdlibTest = flag.Bool("stdlib", false, "Run the (slow) stdlib test")
    33  
    34  func TestStdlib(t *testing.T) {
    35  	if !*runStdlibTest {
    36  		t.Skip("skipping (slow) stdlib test (use --stdlib)")
    37  	}
    38  
    39  	// Load, parse and type-check the program.
    40  	ctxt := build.Default // copy
    41  	ctxt.GOPATH = ""      // disable GOPATH
    42  	conf := loader.Config{Build: &ctxt}
    43  	if _, err := conf.FromArgs(buildutil.AllPackages(conf.Build), true); err != nil {
    44  		t.Errorf("FromArgs failed: %v", err)
    45  		return
    46  	}
    47  
    48  	iprog, err := conf.Load()
    49  	if err != nil {
    50  		t.Fatalf("Load failed: %v", err)
    51  	}
    52  
    53  	// Create SSA packages.
    54  	prog := ssautil.CreateProgram(iprog, 0)
    55  	prog.Build()
    56  
    57  	numPkgs := len(prog.AllPackages())
    58  	if want := 240; numPkgs < want {
    59  		t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
    60  	}
    61  
    62  	// Determine the set of packages/tests to analyze.
    63  	var testPkgs []*ssa.Package
    64  	for _, info := range iprog.InitialPackages() {
    65  		testPkgs = append(testPkgs, prog.Package(info.Pkg))
    66  	}
    67  	testmain := prog.CreateTestMainPackage(testPkgs...)
    68  	if testmain == nil {
    69  		t.Fatal("analysis scope has tests")
    70  	}
    71  
    72  	// Run the analysis.
    73  	config := &Config{
    74  		Reflection:     false, // TODO(adonovan): fix remaining bug in rVCallConstraint, then enable.
    75  		BuildCallGraph: true,
    76  		Mains:          []*ssa.Package{testmain},
    77  	}
    78  	// TODO(adonovan): add some query values (affects track bits).
    79  
    80  	t0 := time.Now()
    81  
    82  	result, err := Analyze(config)
    83  	if err != nil {
    84  		t.Fatal(err) // internal error in pointer analysis
    85  	}
    86  	_ = result // TODO(adonovan): measure something
    87  
    88  	t1 := time.Now()
    89  
    90  	// Dump some statistics.
    91  	allFuncs := ssautil.AllFunctions(prog)
    92  	var numInstrs int
    93  	for fn := range allFuncs {
    94  		for _, b := range fn.Blocks {
    95  			numInstrs += len(b.Instrs)
    96  		}
    97  	}
    98  
    99  	// determine line count
   100  	var lineCount int
   101  	prog.Fset.Iterate(func(f *token.File) bool {
   102  		lineCount += f.LineCount()
   103  		return true
   104  	})
   105  
   106  	t.Log("#Source lines:          ", lineCount)
   107  	t.Log("#Instructions:          ", numInstrs)
   108  	t.Log("Pointer analysis:       ", t1.Sub(t0))
   109  }