github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gotools/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  package pointer
     6  
     7  // This file runs the pointer analysis on all packages and tests beneath
     8  // $GOROOT.  It provides a "smoke test" that the analysis doesn't crash
     9  // on a large input, and a benchmark for performance measurement.
    10  //
    11  // Because it is relatively slow, the --stdlib flag must be enabled for
    12  // this test to run:
    13  //    % go test -v golang.org/x/tools/go/pointer --stdlib
    14  
    15  import (
    16  	"flag"
    17  	"go/build"
    18  	"go/token"
    19  	"testing"
    20  	"time"
    21  
    22  	"llvm.org/llgo/third_party/gotools/go/buildutil"
    23  	"llvm.org/llgo/third_party/gotools/go/loader"
    24  	"llvm.org/llgo/third_party/gotools/go/ssa"
    25  	"llvm.org/llgo/third_party/gotools/go/ssa/ssautil"
    26  )
    27  
    28  var runStdlibTest = flag.Bool("stdlib", false, "Run the (slow) stdlib test")
    29  
    30  func TestStdlib(t *testing.T) {
    31  	if !*runStdlibTest {
    32  		t.Skip("skipping (slow) stdlib test (use --stdlib)")
    33  	}
    34  
    35  	// Load, parse and type-check the program.
    36  	ctxt := build.Default // copy
    37  	ctxt.GOPATH = ""      // disable GOPATH
    38  	conf := loader.Config{Build: &ctxt}
    39  	if _, err := conf.FromArgs(buildutil.AllPackages(conf.Build), true); err != nil {
    40  		t.Errorf("FromArgs failed: %v", err)
    41  		return
    42  	}
    43  
    44  	iprog, err := conf.Load()
    45  	if err != nil {
    46  		t.Fatalf("Load failed: %v", err)
    47  	}
    48  
    49  	// Create SSA packages.
    50  	prog := ssa.Create(iprog, 0)
    51  	prog.BuildAll()
    52  
    53  	numPkgs := len(prog.AllPackages())
    54  	if want := 240; numPkgs < want {
    55  		t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want)
    56  	}
    57  
    58  	// Determine the set of packages/tests to analyze.
    59  	var testPkgs []*ssa.Package
    60  	for _, info := range iprog.InitialPackages() {
    61  		testPkgs = append(testPkgs, prog.Package(info.Pkg))
    62  	}
    63  	testmain := prog.CreateTestMainPackage(testPkgs...)
    64  	if testmain == nil {
    65  		t.Fatal("analysis scope has tests")
    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:          []*ssa.Package{testmain},
    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  }