...
Run Format

Source file test/run.go

Documentation: test

     1  // skip
     2  
     3  // Copyright 2012 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Run runs tests in the test directory.
     8  package main
     9  
    10  import (
    11  	"bytes"
    12  	"errors"
    13  	"flag"
    14  	"fmt"
    15  	"hash/fnv"
    16  	"io"
    17  	"io/ioutil"
    18  	"log"
    19  	"os"
    20  	"os/exec"
    21  	"path"
    22  	"path/filepath"
    23  	"regexp"
    24  	"runtime"
    25  	"sort"
    26  	"strconv"
    27  	"strings"
    28  	"time"
    29  	"unicode"
    30  )
    31  
    32  var (
    33  	verbose        = flag.Bool("v", false, "verbose. if set, parallelism is set to 1.")
    34  	keep           = flag.Bool("k", false, "keep. keep temporary directory.")
    35  	numParallel    = flag.Int("n", runtime.NumCPU(), "number of parallel tests to run")
    36  	summary        = flag.Bool("summary", false, "show summary of results")
    37  	showSkips      = flag.Bool("show_skips", false, "show skipped tests")
    38  	runSkips       = flag.Bool("run_skips", false, "run skipped tests (ignore skip and build tags)")
    39  	linkshared     = flag.Bool("linkshared", false, "")
    40  	updateErrors   = flag.Bool("update_errors", false, "update error messages in test file based on compiler output")
    41  	runoutputLimit = flag.Int("l", defaultRunOutputLimit(), "number of parallel runoutput tests to run")
    42  
    43  	shard  = flag.Int("shard", 0, "shard index to run. Only applicable if -shards is non-zero.")
    44  	shards = flag.Int("shards", 0, "number of shards. If 0, all tests are run. This is used by the continuous build.")
    45  )
    46  
    47  var (
    48  	goos, goarch string
    49  
    50  	// dirs are the directories to look for *.go files in.
    51  	// TODO(bradfitz): just use all directories?
    52  	dirs = []string{".", "ken", "chan", "interface", "syntax", "dwarf", "fixedbugs", "codegen"}
    53  
    54  	// ratec controls the max number of tests running at a time.
    55  	ratec chan bool
    56  
    57  	// toRun is the channel of tests to run.
    58  	// It is nil until the first test is started.
    59  	toRun chan *test
    60  
    61  	// rungatec controls the max number of runoutput tests
    62  	// executed in parallel as they can each consume a lot of memory.
    63  	rungatec chan bool
    64  )
    65  
    66  // maxTests is an upper bound on the total number of tests.
    67  // It is used as a channel buffer size to make sure sends don't block.
    68  const maxTests = 5000
    69  
    70  func main() {
    71  	flag.Parse()
    72  
    73  	goos = getenv("GOOS", runtime.GOOS)
    74  	goarch = getenv("GOARCH", runtime.GOARCH)
    75  
    76  	findExecCmd()
    77  
    78  	// Disable parallelism if printing or if using a simulator.
    79  	if *verbose || len(findExecCmd()) > 0 {
    80  		*numParallel = 1
    81  	}
    82  
    83  	ratec = make(chan bool, *numParallel)
    84  	rungatec = make(chan bool, *runoutputLimit)
    85  
    86  	var tests []*test
    87  	if flag.NArg() > 0 {
    88  		for _, arg := range flag.Args() {
    89  			if arg == "-" || arg == "--" {
    90  				// Permit running:
    91  				// $ go run run.go - env.go
    92  				// $ go run run.go -- env.go
    93  				// $ go run run.go - ./fixedbugs
    94  				// $ go run run.go -- ./fixedbugs
    95  				continue
    96  			}
    97  			if fi, err := os.Stat(arg); err == nil && fi.IsDir() {
    98  				for _, baseGoFile := range goFiles(arg) {
    99  					tests = append(tests, startTest(arg, baseGoFile))
   100  				}
   101  			} else if strings.HasSuffix(arg, ".go") {
   102  				dir, file := filepath.Split(arg)
   103  				tests = append(tests, startTest(dir, file))
   104  			} else {
   105  				log.Fatalf("can't yet deal with non-directory and non-go file %q", arg)
   106  			}
   107  		}
   108  	} else {
   109  		for _, dir := range dirs {
   110  			for _, baseGoFile := range goFiles(dir) {
   111  				tests = append(tests, startTest(dir, baseGoFile))
   112  			}
   113  		}
   114  	}
   115  
   116  	failed := false
   117  	resCount := map[string]int{}
   118  	for _, test := range tests {
   119  		<-test.donec
   120  		status := "ok  "
   121  		errStr := ""
   122  		if e, isSkip := test.err.(skipError); isSkip {
   123  			test.err = nil
   124  			errStr = "unexpected skip for " + path.Join(test.dir, test.gofile) + ": " + string(e)
   125  			status = "FAIL"
   126  		}
   127  		if test.err != nil {
   128  			status = "FAIL"
   129  			errStr = test.err.Error()
   130  		}
   131  		if status == "FAIL" {
   132  			failed = true
   133  		}
   134  		resCount[status]++
   135  		dt := fmt.Sprintf("%.3fs", test.dt.Seconds())
   136  		if status == "FAIL" {
   137  			fmt.Printf("# go run run.go -- %s\n%s\nFAIL\t%s\t%s\n",
   138  				path.Join(test.dir, test.gofile),
   139  				errStr, test.goFileName(), dt)
   140  			continue
   141  		}
   142  		if !*verbose {
   143  			continue
   144  		}
   145  		fmt.Printf("%s\t%s\t%s\n", status, test.goFileName(), dt)
   146  	}
   147  
   148  	if *summary {
   149  		for k, v := range resCount {
   150  			fmt.Printf("%5d %s\n", v, k)
   151  		}
   152  	}
   153  
   154  	if failed {
   155  		os.Exit(1)
   156  	}
   157  }
   158  
   159  func toolPath(name string) string {
   160  	p := filepath.Join(os.Getenv("GOROOT"), "bin", "tool", name)
   161  	if _, err := os.Stat(p); err != nil {
   162  		log.Fatalf("didn't find binary at %s", p)
   163  	}
   164  	return p
   165  }
   166  
   167  // goTool reports the path of the go tool to use to run the tests.
   168  // If possible, use the same Go used to run run.go, otherwise
   169  // fallback to the go version found in the PATH.
   170  func goTool() string {
   171  	var exeSuffix string
   172  	if runtime.GOOS == "windows" {
   173  		exeSuffix = ".exe"
   174  	}
   175  	path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
   176  	if _, err := os.Stat(path); err == nil {
   177  		return path
   178  	}
   179  	// Just run "go" from PATH
   180  	return "go"
   181  }
   182  
   183  func shardMatch(name string) bool {
   184  	if *shards == 0 {
   185  		return true
   186  	}
   187  	h := fnv.New32()
   188  	io.WriteString(h, name)
   189  	return int(h.Sum32()%uint32(*shards)) == *shard
   190  }
   191  
   192  func goFiles(dir string) []string {
   193  	f, err := os.Open(dir)
   194  	check(err)
   195  	dirnames, err := f.Readdirnames(-1)
   196  	check(err)
   197  	names := []string{}
   198  	for _, name := range dirnames {
   199  		if !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && shardMatch(name) {
   200  			names = append(names, name)
   201  		}
   202  	}
   203  	sort.Strings(names)
   204  	return names
   205  }
   206  
   207  type runCmd func(...string) ([]byte, error)
   208  
   209  func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, err error) {
   210  	cmd := []string{goTool(), "tool", "compile", "-e"}
   211  	cmd = append(cmd, flags...)
   212  	if *linkshared {
   213  		cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
   214  	}
   215  	cmd = append(cmd, longname)
   216  	return runcmd(cmd...)
   217  }
   218  
   219  func compileInDir(runcmd runCmd, dir string, flags []string, localImports bool, names ...string) (out []byte, err error) {
   220  	cmd := []string{goTool(), "tool", "compile", "-e"}
   221  	if localImports {
   222  		// Set relative path for local imports and import search path to current dir.
   223  		cmd = append(cmd, "-D", ".", "-I", ".")
   224  	}
   225  	cmd = append(cmd, flags...)
   226  	if *linkshared {
   227  		cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
   228  	}
   229  	for _, name := range names {
   230  		cmd = append(cmd, filepath.Join(dir, name))
   231  	}
   232  	return runcmd(cmd...)
   233  }
   234  
   235  func linkFile(runcmd runCmd, goname string) (err error) {
   236  	pfile := strings.Replace(goname, ".go", ".o", -1)
   237  	cmd := []string{goTool(), "tool", "link", "-w", "-o", "a.exe", "-L", "."}
   238  	if *linkshared {
   239  		cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
   240  	}
   241  	cmd = append(cmd, pfile)
   242  	_, err = runcmd(cmd...)
   243  	return
   244  }
   245  
   246  // skipError describes why a test was skipped.
   247  type skipError string
   248  
   249  func (s skipError) Error() string { return string(s) }
   250  
   251  func check(err error) {
   252  	if err != nil {
   253  		log.Fatal(err)
   254  	}
   255  }
   256  
   257  // test holds the state of a test.
   258  type test struct {
   259  	dir, gofile string
   260  	donec       chan bool // closed when done
   261  	dt          time.Duration
   262  
   263  	src string
   264  
   265  	tempDir string
   266  	err     error
   267  }
   268  
   269  // startTest
   270  func startTest(dir, gofile string) *test {
   271  	t := &test{
   272  		dir:    dir,
   273  		gofile: gofile,
   274  		donec:  make(chan bool, 1),
   275  	}
   276  	if toRun == nil {
   277  		toRun = make(chan *test, maxTests)
   278  		go runTests()
   279  	}
   280  	select {
   281  	case toRun <- t:
   282  	default:
   283  		panic("toRun buffer size (maxTests) is too small")
   284  	}
   285  	return t
   286  }
   287  
   288  // runTests runs tests in parallel, but respecting the order they
   289  // were enqueued on the toRun channel.
   290  func runTests() {
   291  	for {
   292  		ratec <- true
   293  		t := <-toRun
   294  		go func() {
   295  			t.run()
   296  			<-ratec
   297  		}()
   298  	}
   299  }
   300  
   301  var cwd, _ = os.Getwd()
   302  
   303  func (t *test) goFileName() string {
   304  	return filepath.Join(t.dir, t.gofile)
   305  }
   306  
   307  func (t *test) goDirName() string {
   308  	return filepath.Join(t.dir, strings.Replace(t.gofile, ".go", ".dir", -1))
   309  }
   310  
   311  func goDirFiles(longdir string) (filter []os.FileInfo, err error) {
   312  	files, dirErr := ioutil.ReadDir(longdir)
   313  	if dirErr != nil {
   314  		return nil, dirErr
   315  	}
   316  	for _, gofile := range files {
   317  		if filepath.Ext(gofile.Name()) == ".go" {
   318  			filter = append(filter, gofile)
   319  		}
   320  	}
   321  	return
   322  }
   323  
   324  var packageRE = regexp.MustCompile(`(?m)^package (\w+)`)
   325  
   326  // If singlefilepkgs is set, each file is considered a separate package
   327  // even if the package names are the same.
   328  func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
   329  	files, err := goDirFiles(longdir)
   330  	if err != nil {
   331  		return nil, err
   332  	}
   333  	var pkgs [][]string
   334  	m := make(map[string]int)
   335  	for _, file := range files {
   336  		name := file.Name()
   337  		data, err := ioutil.ReadFile(filepath.Join(longdir, name))
   338  		if err != nil {
   339  			return nil, err
   340  		}
   341  		pkgname := packageRE.FindStringSubmatch(string(data))
   342  		if pkgname == nil {
   343  			return nil, fmt.Errorf("cannot find package name in %s", name)
   344  		}
   345  		i, ok := m[pkgname[1]]
   346  		if singlefilepkgs || !ok {
   347  			i = len(pkgs)
   348  			pkgs = append(pkgs, nil)
   349  			m[pkgname[1]] = i
   350  		}
   351  		pkgs[i] = append(pkgs[i], name)
   352  	}
   353  	return pkgs, nil
   354  }
   355  
   356  type context struct {
   357  	GOOS   string
   358  	GOARCH string
   359  }
   360  
   361  // shouldTest looks for build tags in a source file and returns
   362  // whether the file should be used according to the tags.
   363  func shouldTest(src string, goos, goarch string) (ok bool, whyNot string) {
   364  	if *runSkips {
   365  		return true, ""
   366  	}
   367  	for _, line := range strings.Split(src, "\n") {
   368  		line = strings.TrimSpace(line)
   369  		if strings.HasPrefix(line, "//") {
   370  			line = line[2:]
   371  		} else {
   372  			continue
   373  		}
   374  		line = strings.TrimSpace(line)
   375  		if len(line) == 0 || line[0] != '+' {
   376  			continue
   377  		}
   378  		ctxt := &context{
   379  			GOOS:   goos,
   380  			GOARCH: goarch,
   381  		}
   382  		words := strings.Fields(line)
   383  		if words[0] == "+build" {
   384  			ok := false
   385  			for _, word := range words[1:] {
   386  				if ctxt.match(word) {
   387  					ok = true
   388  					break
   389  				}
   390  			}
   391  			if !ok {
   392  				// no matching tag found.
   393  				return false, line
   394  			}
   395  		}
   396  	}
   397  	// no build tags
   398  	return true, ""
   399  }
   400  
   401  func (ctxt *context) match(name string) bool {
   402  	if name == "" {
   403  		return false
   404  	}
   405  	if i := strings.Index(name, ","); i >= 0 {
   406  		// comma-separated list
   407  		return ctxt.match(name[:i]) && ctxt.match(name[i+1:])
   408  	}
   409  	if strings.HasPrefix(name, "!!") { // bad syntax, reject always
   410  		return false
   411  	}
   412  	if strings.HasPrefix(name, "!") { // negation
   413  		return len(name) > 1 && !ctxt.match(name[1:])
   414  	}
   415  
   416  	// Tags must be letters, digits, underscores or dots.
   417  	// Unlike in Go identifiers, all digits are fine (e.g., "386").
   418  	for _, c := range name {
   419  		if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
   420  			return false
   421  		}
   422  	}
   423  
   424  	if name == ctxt.GOOS || name == ctxt.GOARCH {
   425  		return true
   426  	}
   427  
   428  	if name == "test_run" {
   429  		return true
   430  	}
   431  
   432  	return false
   433  }
   434  
   435  func init() { checkShouldTest() }
   436  
   437  // goGcflags returns the -gcflags argument to use with go build / go run.
   438  // This must match the flags used for building the standard libary,
   439  // or else the commands will rebuild any needed packages (like runtime)
   440  // over and over.
   441  func goGcflags() string {
   442  	return "-gcflags=" + os.Getenv("GO_GCFLAGS")
   443  }
   444  
   445  // run runs a test.
   446  func (t *test) run() {
   447  	start := time.Now()
   448  	defer func() {
   449  		t.dt = time.Since(start)
   450  		close(t.donec)
   451  	}()
   452  
   453  	srcBytes, err := ioutil.ReadFile(t.goFileName())
   454  	if err != nil {
   455  		t.err = err
   456  		return
   457  	}
   458  	t.src = string(srcBytes)
   459  	if t.src[0] == '\n' {
   460  		t.err = skipError("starts with newline")
   461  		return
   462  	}
   463  
   464  	// Execution recipe stops at first blank line.
   465  	pos := strings.Index(t.src, "\n\n")
   466  	if pos == -1 {
   467  		t.err = errors.New("double newline not found")
   468  		return
   469  	}
   470  	action := t.src[:pos]
   471  	if nl := strings.Index(action, "\n"); nl >= 0 && strings.Contains(action[:nl], "+build") {
   472  		// skip first line
   473  		action = action[nl+1:]
   474  	}
   475  	if strings.HasPrefix(action, "//") {
   476  		action = action[2:]
   477  	}
   478  
   479  	// Check for build constraints only up to the actual code.
   480  	pkgPos := strings.Index(t.src, "\npackage")
   481  	if pkgPos == -1 {
   482  		pkgPos = pos // some files are intentionally malformed
   483  	}
   484  	if ok, why := shouldTest(t.src[:pkgPos], goos, goarch); !ok {
   485  		if *showSkips {
   486  			fmt.Printf("%-20s %-20s: %s\n", "skip", t.goFileName(), why)
   487  		}
   488  		return
   489  	}
   490  
   491  	var args, flags []string
   492  	var tim int
   493  	wantError := false
   494  	wantAuto := false
   495  	singlefilepkgs := false
   496  	localImports := true
   497  	f := strings.Fields(action)
   498  	if len(f) > 0 {
   499  		action = f[0]
   500  		args = f[1:]
   501  	}
   502  
   503  	// TODO: Clean up/simplify this switch statement.
   504  	switch action {
   505  	case "compile", "compiledir", "build", "builddir", "buildrundir", "run", "buildrun", "runoutput", "rundir", "asmcheck":
   506  		// nothing to do
   507  	case "errorcheckandrundir":
   508  		wantError = false // should be no error if also will run
   509  	case "errorcheckwithauto":
   510  		action = "errorcheck"
   511  		wantAuto = true
   512  		wantError = true
   513  	case "errorcheck", "errorcheckdir", "errorcheckoutput":
   514  		wantError = true
   515  	case "skip":
   516  		if *runSkips {
   517  			break
   518  		}
   519  		return
   520  	default:
   521  		t.err = skipError("skipped; unknown pattern: " + action)
   522  		return
   523  	}
   524  
   525  	// collect flags
   526  	for len(args) > 0 && strings.HasPrefix(args[0], "-") {
   527  		switch args[0] {
   528  		case "-1":
   529  			wantError = true
   530  		case "-0":
   531  			wantError = false
   532  		case "-s":
   533  			singlefilepkgs = true
   534  		case "-n":
   535  			// Do not set relative path for local imports to current dir,
   536  			// e.g. do not pass -D . -I . to the compiler.
   537  			// Used in fixedbugs/bug345.go to allow compilation and import of local pkg.
   538  			// See golang.org/issue/25635
   539  			localImports = false
   540  		case "-t": // timeout in seconds
   541  			args = args[1:]
   542  			var err error
   543  			tim, err = strconv.Atoi(args[0])
   544  			if err != nil {
   545  				t.err = fmt.Errorf("need number of seconds for -t timeout, got %s instead", args[0])
   546  			}
   547  
   548  		default:
   549  			flags = append(flags, args[0])
   550  		}
   551  		args = args[1:]
   552  	}
   553  
   554  	t.makeTempDir()
   555  	if !*keep {
   556  		defer os.RemoveAll(t.tempDir)
   557  	}
   558  
   559  	err = ioutil.WriteFile(filepath.Join(t.tempDir, t.gofile), srcBytes, 0644)
   560  	check(err)
   561  
   562  	// A few tests (of things like the environment) require these to be set.
   563  	if os.Getenv("GOOS") == "" {
   564  		os.Setenv("GOOS", runtime.GOOS)
   565  	}
   566  	if os.Getenv("GOARCH") == "" {
   567  		os.Setenv("GOARCH", runtime.GOARCH)
   568  	}
   569  
   570  	useTmp := true
   571  	runcmd := func(args ...string) ([]byte, error) {
   572  		cmd := exec.Command(args[0], args[1:]...)
   573  		var buf bytes.Buffer
   574  		cmd.Stdout = &buf
   575  		cmd.Stderr = &buf
   576  		if useTmp {
   577  			cmd.Dir = t.tempDir
   578  			cmd.Env = envForDir(cmd.Dir)
   579  		} else {
   580  			cmd.Env = os.Environ()
   581  		}
   582  
   583  		var err error
   584  
   585  		if tim != 0 {
   586  			err = cmd.Start()
   587  			// This command-timeout code adapted from cmd/go/test.go
   588  			if err == nil {
   589  				tick := time.NewTimer(time.Duration(tim) * time.Second)
   590  				done := make(chan error)
   591  				go func() {
   592  					done <- cmd.Wait()
   593  				}()
   594  				select {
   595  				case err = <-done:
   596  					// ok
   597  				case <-tick.C:
   598  					cmd.Process.Kill()
   599  					err = <-done
   600  					// err = errors.New("Test timeout")
   601  				}
   602  				tick.Stop()
   603  			}
   604  		} else {
   605  			err = cmd.Run()
   606  		}
   607  		if err != nil {
   608  			err = fmt.Errorf("%s\n%s", err, buf.Bytes())
   609  		}
   610  		return buf.Bytes(), err
   611  	}
   612  
   613  	long := filepath.Join(cwd, t.goFileName())
   614  	switch action {
   615  	default:
   616  		t.err = fmt.Errorf("unimplemented action %q", action)
   617  
   618  	case "asmcheck":
   619  		// Compile Go file and match the generated assembly
   620  		// against a set of regexps in comments.
   621  		ops := t.wantedAsmOpcodes(long)
   622  		for _, env := range ops.Envs() {
   623  			cmdline := []string{"build", "-gcflags", "-S"}
   624  			cmdline = append(cmdline, flags...)
   625  			cmdline = append(cmdline, long)
   626  			cmd := exec.Command(goTool(), cmdline...)
   627  			cmd.Env = append(os.Environ(), env.Environ()...)
   628  
   629  			var buf bytes.Buffer
   630  			cmd.Stdout, cmd.Stderr = &buf, &buf
   631  			if err := cmd.Run(); err != nil {
   632  				fmt.Println(env, "\n", cmd.Stderr)
   633  				t.err = err
   634  				return
   635  			}
   636  
   637  			t.err = t.asmCheck(buf.String(), long, env, ops[env])
   638  			if t.err != nil {
   639  				return
   640  			}
   641  		}
   642  		return
   643  
   644  	case "errorcheck":
   645  		// Compile Go file.
   646  		// Fail if wantError is true and compilation was successful and vice versa.
   647  		// Match errors produced by gc against errors in comments.
   648  		// TODO(gri) remove need for -C (disable printing of columns in error messages)
   649  		cmdline := []string{goTool(), "tool", "compile", "-C", "-e", "-o", "a.o"}
   650  		// No need to add -dynlink even if linkshared if we're just checking for errors...
   651  		cmdline = append(cmdline, flags...)
   652  		cmdline = append(cmdline, long)
   653  		out, err := runcmd(cmdline...)
   654  		if wantError {
   655  			if err == nil {
   656  				t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
   657  				return
   658  			}
   659  		} else {
   660  			if err != nil {
   661  				t.err = err
   662  				return
   663  			}
   664  		}
   665  		if *updateErrors {
   666  			t.updateErrors(string(out), long)
   667  		}
   668  		t.err = t.errorCheck(string(out), wantAuto, long, t.gofile)
   669  		return
   670  
   671  	case "compile":
   672  		// Compile Go file.
   673  		_, t.err = compileFile(runcmd, long, flags)
   674  
   675  	case "compiledir":
   676  		// Compile all files in the directory as packages in lexicographic order.
   677  		longdir := filepath.Join(cwd, t.goDirName())
   678  		pkgs, err := goDirPackages(longdir, singlefilepkgs)
   679  		if err != nil {
   680  			t.err = err
   681  			return
   682  		}
   683  		for _, gofiles := range pkgs {
   684  			_, t.err = compileInDir(runcmd, longdir, flags, localImports, gofiles...)
   685  			if t.err != nil {
   686  				return
   687  			}
   688  		}
   689  
   690  	case "errorcheckdir", "errorcheckandrundir":
   691  		// Compile and errorCheck all files in the directory as packages in lexicographic order.
   692  		// If errorcheckdir and wantError, compilation of the last package must fail.
   693  		// If errorcheckandrundir and wantError, compilation of the package prior the last must fail.
   694  		longdir := filepath.Join(cwd, t.goDirName())
   695  		pkgs, err := goDirPackages(longdir, singlefilepkgs)
   696  		if err != nil {
   697  			t.err = err
   698  			return
   699  		}
   700  		errPkg := len(pkgs) - 1
   701  		if wantError && action == "errorcheckandrundir" {
   702  			// The last pkg should compiled successfully and will be run in next case.
   703  			// Preceding pkg must return an error from compileInDir.
   704  			errPkg--
   705  		}
   706  		for i, gofiles := range pkgs {
   707  			out, err := compileInDir(runcmd, longdir, flags, localImports, gofiles...)
   708  			if i == errPkg {
   709  				if wantError && err == nil {
   710  					t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
   711  					return
   712  				} else if !wantError && err != nil {
   713  					t.err = err
   714  					return
   715  				}
   716  			} else if err != nil {
   717  				t.err = err
   718  				return
   719  			}
   720  			var fullshort []string
   721  			for _, name := range gofiles {
   722  				fullshort = append(fullshort, filepath.Join(longdir, name), name)
   723  			}
   724  			t.err = t.errorCheck(string(out), wantAuto, fullshort...)
   725  			if t.err != nil {
   726  				break
   727  			}
   728  		}
   729  		if action == "errorcheckdir" {
   730  			return
   731  		}
   732  		fallthrough
   733  
   734  	case "rundir":
   735  		// Compile all files in the directory as packages in lexicographic order.
   736  		// In case of errorcheckandrundir, ignore failed compilation of the package before the last.
   737  		// Link as if the last file is the main package, run it.
   738  		// Verify the expected output.
   739  		longdir := filepath.Join(cwd, t.goDirName())
   740  		pkgs, err := goDirPackages(longdir, singlefilepkgs)
   741  		if err != nil {
   742  			t.err = err
   743  			return
   744  		}
   745  		for i, gofiles := range pkgs {
   746  			_, err := compileInDir(runcmd, longdir, flags, localImports, gofiles...)
   747  			// Allow this package compilation fail based on conditions below;
   748  			// its errors were checked in previous case.
   749  			if err != nil && !(wantError && action == "errorcheckandrundir" && i == len(pkgs)-2) {
   750  				t.err = err
   751  				return
   752  			}
   753  			if i == len(pkgs)-1 {
   754  				err = linkFile(runcmd, gofiles[0])
   755  				if err != nil {
   756  					t.err = err
   757  					return
   758  				}
   759  				var cmd []string
   760  				cmd = append(cmd, findExecCmd()...)
   761  				cmd = append(cmd, filepath.Join(t.tempDir, "a.exe"))
   762  				cmd = append(cmd, args...)
   763  				out, err := runcmd(cmd...)
   764  				if err != nil {
   765  					t.err = err
   766  					return
   767  				}
   768  				if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() {
   769  					t.err = fmt.Errorf("incorrect output\n%s", out)
   770  				}
   771  			}
   772  		}
   773  
   774  	case "build":
   775  		// Build Go file.
   776  		_, err := runcmd(goTool(), "build", goGcflags(), "-o", "a.exe", long)
   777  		if err != nil {
   778  			t.err = err
   779  		}
   780  
   781  	case "builddir", "buildrundir":
   782  		// Build an executable from all the .go and .s files in a subdirectory.
   783  		// Run it and verify its output in the buildrundir case.
   784  		longdir := filepath.Join(cwd, t.goDirName())
   785  		files, dirErr := ioutil.ReadDir(longdir)
   786  		if dirErr != nil {
   787  			t.err = dirErr
   788  			break
   789  		}
   790  		var gos []os.FileInfo
   791  		var asms []os.FileInfo
   792  		for _, file := range files {
   793  			switch filepath.Ext(file.Name()) {
   794  			case ".go":
   795  				gos = append(gos, file)
   796  			case ".s":
   797  				asms = append(asms, file)
   798  			}
   799  
   800  		}
   801  		var objs []string
   802  		cmd := []string{goTool(), "tool", "compile", "-e", "-D", ".", "-I", ".", "-o", "go.o"}
   803  		if len(asms) > 0 {
   804  			cmd = append(cmd, "-asmhdr", "go_asm.h")
   805  		}
   806  		for _, file := range gos {
   807  			cmd = append(cmd, filepath.Join(longdir, file.Name()))
   808  		}
   809  		_, err := runcmd(cmd...)
   810  		if err != nil {
   811  			t.err = err
   812  			break
   813  		}
   814  		objs = append(objs, "go.o")
   815  		if len(asms) > 0 {
   816  			cmd = []string{goTool(), "tool", "asm", "-e", "-I", ".", "-o", "asm.o"}
   817  			for _, file := range asms {
   818  				cmd = append(cmd, filepath.Join(longdir, file.Name()))
   819  			}
   820  			_, err = runcmd(cmd...)
   821  			if err != nil {
   822  				t.err = err
   823  				break
   824  			}
   825  			objs = append(objs, "asm.o")
   826  		}
   827  		cmd = []string{goTool(), "tool", "pack", "c", "all.a"}
   828  		cmd = append(cmd, objs...)
   829  		_, err = runcmd(cmd...)
   830  		if err != nil {
   831  			t.err = err
   832  			break
   833  		}
   834  		cmd = []string{goTool(), "tool", "link", "-o", "a.exe", "all.a"}
   835  		_, err = runcmd(cmd...)
   836  		if err != nil {
   837  			t.err = err
   838  			break
   839  		}
   840  		if action == "buildrundir" {
   841  			cmd = append(findExecCmd(), filepath.Join(t.tempDir, "a.exe"))
   842  			out, err := runcmd(cmd...)
   843  			if err != nil {
   844  				t.err = err
   845  				break
   846  			}
   847  			if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() {
   848  				t.err = fmt.Errorf("incorrect output\n%s", out)
   849  			}
   850  		}
   851  
   852  	case "buildrun":
   853  		// Build an executable from Go file, then run it, verify its output.
   854  		// Useful for timeout tests where failure mode is infinite loop.
   855  		// TODO: not supported on NaCl
   856  		cmd := []string{goTool(), "build", goGcflags(), "-o", "a.exe"}
   857  		if *linkshared {
   858  			cmd = append(cmd, "-linkshared")
   859  		}
   860  		longdirgofile := filepath.Join(filepath.Join(cwd, t.dir), t.gofile)
   861  		cmd = append(cmd, flags...)
   862  		cmd = append(cmd, longdirgofile)
   863  		out, err := runcmd(cmd...)
   864  		if err != nil {
   865  			t.err = err
   866  			return
   867  		}
   868  		cmd = []string{"./a.exe"}
   869  		out, err = runcmd(append(cmd, args...)...)
   870  		if err != nil {
   871  			t.err = err
   872  			return
   873  		}
   874  
   875  		if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() {
   876  			t.err = fmt.Errorf("incorrect output\n%s", out)
   877  		}
   878  
   879  	case "run":
   880  		// Run Go file if no special go command flags are provided;
   881  		// otherwise build an executable and run it.
   882  		// Verify the output.
   883  		useTmp = false
   884  		var out []byte
   885  		var err error
   886  		if len(flags)+len(args) == 0 && goGcflags() == "" && !*linkshared {
   887  			// If we're not using special go command flags,
   888  			// skip all the go command machinery.
   889  			// This avoids any time the go command would
   890  			// spend checking whether, for example, the installed
   891  			// package runtime is up to date.
   892  			// Because we run lots of trivial test programs,
   893  			// the time adds up.
   894  			pkg := filepath.Join(t.tempDir, "pkg.a")
   895  			if _, err := runcmd(goTool(), "tool", "compile", "-o", pkg, t.goFileName()); err != nil {
   896  				t.err = err
   897  				return
   898  			}
   899  			exe := filepath.Join(t.tempDir, "test.exe")
   900  			cmd := []string{goTool(), "tool", "link", "-s", "-w"}
   901  			cmd = append(cmd, "-o", exe, pkg)
   902  			if _, err := runcmd(cmd...); err != nil {
   903  				t.err = err
   904  				return
   905  			}
   906  			out, err = runcmd(append([]string{exe}, args...)...)
   907  		} else {
   908  			cmd := []string{goTool(), "run", goGcflags()}
   909  			if *linkshared {
   910  				cmd = append(cmd, "-linkshared")
   911  			}
   912  			cmd = append(cmd, flags...)
   913  			cmd = append(cmd, t.goFileName())
   914  			out, err = runcmd(append(cmd, args...)...)
   915  		}
   916  		if err != nil {
   917  			t.err = err
   918  			return
   919  		}
   920  		if strings.Replace(string(out), "\r\n", "\n", -1) != t.expectedOutput() {
   921  			t.err = fmt.Errorf("incorrect output\n%s", out)
   922  		}
   923  
   924  	case "runoutput":
   925  		// Run Go file and write its output into temporary Go file.
   926  		// Run generated Go file and verify its output.
   927  		rungatec <- true
   928  		defer func() {
   929  			<-rungatec
   930  		}()
   931  		useTmp = false
   932  		cmd := []string{goTool(), "run", goGcflags()}
   933  		if *linkshared {
   934  			cmd = append(cmd, "-linkshared")
   935  		}
   936  		cmd = append(cmd, t.goFileName())
   937  		out, err := runcmd(append(cmd, args...)...)
   938  		if err != nil {
   939  			t.err = err
   940  			return
   941  		}
   942  		tfile := filepath.Join(t.tempDir, "tmp__.go")
   943  		if err := ioutil.WriteFile(tfile, out, 0666); err != nil {
   944  			t.err = fmt.Errorf("write tempfile:%s", err)
   945  			return
   946  		}
   947  		cmd = []string{goTool(), "run", goGcflags()}
   948  		if *linkshared {
   949  			cmd = append(cmd, "-linkshared")
   950  		}
   951  		cmd = append(cmd, tfile)
   952  		out, err = runcmd(cmd...)
   953  		if err != nil {
   954  			t.err = err
   955  			return
   956  		}
   957  		if string(out) != t.expectedOutput() {
   958  			t.err = fmt.Errorf("incorrect output\n%s", out)
   959  		}
   960  
   961  	case "errorcheckoutput":
   962  		// Run Go file and write its output into temporary Go file.
   963  		// Compile and errorCheck generated Go file.
   964  		useTmp = false
   965  		cmd := []string{goTool(), "run", goGcflags()}
   966  		if *linkshared {
   967  			cmd = append(cmd, "-linkshared")
   968  		}
   969  		cmd = append(cmd, t.goFileName())
   970  		out, err := runcmd(append(cmd, args...)...)
   971  		if err != nil {
   972  			t.err = err
   973  			return
   974  		}
   975  		tfile := filepath.Join(t.tempDir, "tmp__.go")
   976  		err = ioutil.WriteFile(tfile, out, 0666)
   977  		if err != nil {
   978  			t.err = fmt.Errorf("write tempfile:%s", err)
   979  			return
   980  		}
   981  		cmdline := []string{goTool(), "tool", "compile", "-e", "-o", "a.o"}
   982  		cmdline = append(cmdline, flags...)
   983  		cmdline = append(cmdline, tfile)
   984  		out, err = runcmd(cmdline...)
   985  		if wantError {
   986  			if err == nil {
   987  				t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
   988  				return
   989  			}
   990  		} else {
   991  			if err != nil {
   992  				t.err = err
   993  				return
   994  			}
   995  		}
   996  		t.err = t.errorCheck(string(out), false, tfile, "tmp__.go")
   997  		return
   998  	}
   999  }
  1000  
  1001  var execCmd []string
  1002  
  1003  func findExecCmd() []string {
  1004  	if execCmd != nil {
  1005  		return execCmd
  1006  	}
  1007  	execCmd = []string{} // avoid work the second time
  1008  	if goos == runtime.GOOS && goarch == runtime.GOARCH {
  1009  		return execCmd
  1010  	}
  1011  	path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch))
  1012  	if err == nil {
  1013  		execCmd = []string{path}
  1014  	}
  1015  	return execCmd
  1016  }
  1017  
  1018  func (t *test) String() string {
  1019  	return filepath.Join(t.dir, t.gofile)
  1020  }
  1021  
  1022  func (t *test) makeTempDir() {
  1023  	var err error
  1024  	t.tempDir, err = ioutil.TempDir("", "")
  1025  	check(err)
  1026  	if *keep {
  1027  		log.Printf("Temporary directory is %s", t.tempDir)
  1028  	}
  1029  }
  1030  
  1031  func (t *test) expectedOutput() string {
  1032  	filename := filepath.Join(t.dir, t.gofile)
  1033  	filename = filename[:len(filename)-len(".go")]
  1034  	filename += ".out"
  1035  	b, _ := ioutil.ReadFile(filename)
  1036  	return string(b)
  1037  }
  1038  
  1039  func splitOutput(out string, wantAuto bool) []string {
  1040  	// gc error messages continue onto additional lines with leading tabs.
  1041  	// Split the output at the beginning of each line that doesn't begin with a tab.
  1042  	// <autogenerated> lines are impossible to match so those are filtered out.
  1043  	var res []string
  1044  	for _, line := range strings.Split(out, "\n") {
  1045  		if strings.HasSuffix(line, "\r") { // remove '\r', output by compiler on windows
  1046  			line = line[:len(line)-1]
  1047  		}
  1048  		if strings.HasPrefix(line, "\t") {
  1049  			res[len(res)-1] += "\n" + line
  1050  		} else if strings.HasPrefix(line, "go tool") || strings.HasPrefix(line, "#") || !wantAuto && strings.HasPrefix(line, "<autogenerated>") {
  1051  			continue
  1052  		} else if strings.TrimSpace(line) != "" {
  1053  			res = append(res, line)
  1054  		}
  1055  	}
  1056  	return res
  1057  }
  1058  
  1059  // errorCheck matches errors in outStr against comments in source files.
  1060  // For each line of the source files which should generate an error,
  1061  // there should be a comment of the form // ERROR "regexp".
  1062  // If outStr has an error for a line which has no such comment,
  1063  // this function will report an error.
  1064  // Likewise if outStr does not have an error for a line which has a comment,
  1065  // or if the error message does not match the <regexp>.
  1066  // The <regexp> syntax is Perl but its best to stick to egrep.
  1067  //
  1068  // Sources files are supplied as fullshort slice.
  1069  // It consists of pairs: full path to source file and it's base name.
  1070  func (t *test) errorCheck(outStr string, wantAuto bool, fullshort ...string) (err error) {
  1071  	defer func() {
  1072  		if *verbose && err != nil {
  1073  			log.Printf("%s gc output:\n%s", t, outStr)
  1074  		}
  1075  	}()
  1076  	var errs []error
  1077  	out := splitOutput(outStr, wantAuto)
  1078  
  1079  	// Cut directory name.
  1080  	for i := range out {
  1081  		for j := 0; j < len(fullshort); j += 2 {
  1082  			full, short := fullshort[j], fullshort[j+1]
  1083  			out[i] = strings.Replace(out[i], full, short, -1)
  1084  		}
  1085  	}
  1086  
  1087  	var want []wantedError
  1088  	for j := 0; j < len(fullshort); j += 2 {
  1089  		full, short := fullshort[j], fullshort[j+1]
  1090  		want = append(want, t.wantedErrors(full, short)...)
  1091  	}
  1092  
  1093  	for _, we := range want {
  1094  		var errmsgs []string
  1095  		if we.auto {
  1096  			errmsgs, out = partitionStrings("<autogenerated>", out)
  1097  		} else {
  1098  			errmsgs, out = partitionStrings(we.prefix, out)
  1099  		}
  1100  		if len(errmsgs) == 0 {
  1101  			errs = append(errs, fmt.Errorf("%s:%d: missing error %q", we.file, we.lineNum, we.reStr))
  1102  			continue
  1103  		}
  1104  		matched := false
  1105  		n := len(out)
  1106  		for _, errmsg := range errmsgs {
  1107  			// Assume errmsg says "file:line: foo".
  1108  			// Cut leading "file:line: " to avoid accidental matching of file name instead of message.
  1109  			text := errmsg
  1110  			if i := strings.Index(text, " "); i >= 0 {
  1111  				text = text[i+1:]
  1112  			}
  1113  			if we.re.MatchString(text) {
  1114  				matched = true
  1115  			} else {
  1116  				out = append(out, errmsg)
  1117  			}
  1118  		}
  1119  		if !matched {
  1120  			errs = append(errs, fmt.Errorf("%s:%d: no match for %#q in:\n\t%s", we.file, we.lineNum, we.reStr, strings.Join(out[n:], "\n\t")))
  1121  			continue
  1122  		}
  1123  	}
  1124  
  1125  	if len(out) > 0 {
  1126  		errs = append(errs, fmt.Errorf("Unmatched Errors:"))
  1127  		for _, errLine := range out {
  1128  			errs = append(errs, fmt.Errorf("%s", errLine))
  1129  		}
  1130  	}
  1131  
  1132  	if len(errs) == 0 {
  1133  		return nil
  1134  	}
  1135  	if len(errs) == 1 {
  1136  		return errs[0]
  1137  	}
  1138  	var buf bytes.Buffer
  1139  	fmt.Fprintf(&buf, "\n")
  1140  	for _, err := range errs {
  1141  		fmt.Fprintf(&buf, "%s\n", err.Error())
  1142  	}
  1143  	return errors.New(buf.String())
  1144  }
  1145  
  1146  func (t *test) updateErrors(out, file string) {
  1147  	base := path.Base(file)
  1148  	// Read in source file.
  1149  	src, err := ioutil.ReadFile(file)
  1150  	if err != nil {
  1151  		fmt.Fprintln(os.Stderr, err)
  1152  		return
  1153  	}
  1154  	lines := strings.Split(string(src), "\n")
  1155  	// Remove old errors.
  1156  	for i, ln := range lines {
  1157  		pos := strings.Index(ln, " // ERROR ")
  1158  		if pos >= 0 {
  1159  			lines[i] = ln[:pos]
  1160  		}
  1161  	}
  1162  	// Parse new errors.
  1163  	errors := make(map[int]map[string]bool)
  1164  	tmpRe := regexp.MustCompile(`autotmp_[0-9]+`)
  1165  	for _, errStr := range splitOutput(out, false) {
  1166  		colon1 := strings.Index(errStr, ":")
  1167  		if colon1 < 0 || errStr[:colon1] != file {
  1168  			continue
  1169  		}
  1170  		colon2 := strings.Index(errStr[colon1+1:], ":")
  1171  		if colon2 < 0 {
  1172  			continue
  1173  		}
  1174  		colon2 += colon1 + 1
  1175  		line, err := strconv.Atoi(errStr[colon1+1 : colon2])
  1176  		line--
  1177  		if err != nil || line < 0 || line >= len(lines) {
  1178  			continue
  1179  		}
  1180  		msg := errStr[colon2+2:]
  1181  		msg = strings.Replace(msg, file, base, -1) // normalize file mentions in error itself
  1182  		msg = strings.TrimLeft(msg, " \t")
  1183  		for _, r := range []string{`\`, `*`, `+`, `[`, `]`, `(`, `)`} {
  1184  			msg = strings.Replace(msg, r, `\`+r, -1)
  1185  		}
  1186  		msg = strings.Replace(msg, `"`, `.`, -1)
  1187  		msg = tmpRe.ReplaceAllLiteralString(msg, `autotmp_[0-9]+`)
  1188  		if errors[line] == nil {
  1189  			errors[line] = make(map[string]bool)
  1190  		}
  1191  		errors[line][msg] = true
  1192  	}
  1193  	// Add new errors.
  1194  	for line, errs := range errors {
  1195  		var sorted []string
  1196  		for e := range errs {
  1197  			sorted = append(sorted, e)
  1198  		}
  1199  		sort.Strings(sorted)
  1200  		lines[line] += " // ERROR"
  1201  		for _, e := range sorted {
  1202  			lines[line] += fmt.Sprintf(` "%s$"`, e)
  1203  		}
  1204  	}
  1205  	// Write new file.
  1206  	err = ioutil.WriteFile(file, []byte(strings.Join(lines, "\n")), 0640)
  1207  	if err != nil {
  1208  		fmt.Fprintln(os.Stderr, err)
  1209  		return
  1210  	}
  1211  	// Polish.
  1212  	exec.Command(goTool(), "fmt", file).CombinedOutput()
  1213  }
  1214  
  1215  // matchPrefix reports whether s is of the form ^(.*/)?prefix(:|[),
  1216  // That is, it needs the file name prefix followed by a : or a [,
  1217  // and possibly preceded by a directory name.
  1218  func matchPrefix(s, prefix string) bool {
  1219  	i := strings.Index(s, ":")
  1220  	if i < 0 {
  1221  		return false
  1222  	}
  1223  	j := strings.LastIndex(s[:i], "/")
  1224  	s = s[j+1:]
  1225  	if len(s) <= len(prefix) || s[:len(prefix)] != prefix {
  1226  		return false
  1227  	}
  1228  	switch s[len(prefix)] {
  1229  	case '[', ':':
  1230  		return true
  1231  	}
  1232  	return false
  1233  }
  1234  
  1235  func partitionStrings(prefix string, strs []string) (matched, unmatched []string) {
  1236  	for _, s := range strs {
  1237  		if matchPrefix(s, prefix) {
  1238  			matched = append(matched, s)
  1239  		} else {
  1240  			unmatched = append(unmatched, s)
  1241  		}
  1242  	}
  1243  	return
  1244  }
  1245  
  1246  type wantedError struct {
  1247  	reStr   string
  1248  	re      *regexp.Regexp
  1249  	lineNum int
  1250  	auto    bool // match <autogenerated> line
  1251  	file    string
  1252  	prefix  string
  1253  }
  1254  
  1255  var (
  1256  	errRx       = regexp.MustCompile(`// (?:GC_)?ERROR (.*)`)
  1257  	errAutoRx   = regexp.MustCompile(`// (?:GC_)?ERRORAUTO (.*)`)
  1258  	errQuotesRx = regexp.MustCompile(`"([^"]*)"`)
  1259  	lineRx      = regexp.MustCompile(`LINE(([+-])([0-9]+))?`)
  1260  )
  1261  
  1262  func (t *test) wantedErrors(file, short string) (errs []wantedError) {
  1263  	cache := make(map[string]*regexp.Regexp)
  1264  
  1265  	src, _ := ioutil.ReadFile(file)
  1266  	for i, line := range strings.Split(string(src), "\n") {
  1267  		lineNum := i + 1
  1268  		if strings.Contains(line, "////") {
  1269  			// double comment disables ERROR
  1270  			continue
  1271  		}
  1272  		var auto bool
  1273  		m := errAutoRx.FindStringSubmatch(line)
  1274  		if m != nil {
  1275  			auto = true
  1276  		} else {
  1277  			m = errRx.FindStringSubmatch(line)
  1278  		}
  1279  		if m == nil {
  1280  			continue
  1281  		}
  1282  		all := m[1]
  1283  		mm := errQuotesRx.FindAllStringSubmatch(all, -1)
  1284  		if mm == nil {
  1285  			log.Fatalf("%s:%d: invalid errchk line: %s", t.goFileName(), lineNum, line)
  1286  		}
  1287  		for _, m := range mm {
  1288  			rx := lineRx.ReplaceAllStringFunc(m[1], func(m string) string {
  1289  				n := lineNum
  1290  				if strings.HasPrefix(m, "LINE+") {
  1291  					delta, _ := strconv.Atoi(m[5:])
  1292  					n += delta
  1293  				} else if strings.HasPrefix(m, "LINE-") {
  1294  					delta, _ := strconv.Atoi(m[5:])
  1295  					n -= delta
  1296  				}
  1297  				return fmt.Sprintf("%s:%d", short, n)
  1298  			})
  1299  			re := cache[rx]
  1300  			if re == nil {
  1301  				var err error
  1302  				re, err = regexp.Compile(rx)
  1303  				if err != nil {
  1304  					log.Fatalf("%s:%d: invalid regexp \"%s\" in ERROR line: %v", t.goFileName(), lineNum, rx, err)
  1305  				}
  1306  				cache[rx] = re
  1307  			}
  1308  			prefix := fmt.Sprintf("%s:%d", short, lineNum)
  1309  			errs = append(errs, wantedError{
  1310  				reStr:   rx,
  1311  				re:      re,
  1312  				prefix:  prefix,
  1313  				auto:    auto,
  1314  				lineNum: lineNum,
  1315  				file:    short,
  1316  			})
  1317  		}
  1318  	}
  1319  
  1320  	return
  1321  }
  1322  
  1323  const (
  1324  	// Regexp to match a single opcode check: optionally begin with "-" (to indicate
  1325  	// a negative check), followed by a string literal enclosed in "" or ``. For "",
  1326  	// backslashes must be handled.
  1327  	reMatchCheck = `-?(?:\x60[^\x60]*\x60|"(?:[^"\\]|\\.)*")`
  1328  )
  1329  
  1330  var (
  1331  	// Regexp to split a line in code and comment, trimming spaces
  1332  	rxAsmComment = regexp.MustCompile(`^\s*(.*?)\s*(?:\/\/\s*(.+)\s*)?$`)
  1333  
  1334  	// Regexp to extract an architecture check: architecture name, followed by semi-colon,
  1335  	// followed by a comma-separated list of opcode checks.
  1336  	rxAsmPlatform = regexp.MustCompile(`(\w+)(/\w+)?(/\w*)?:(` + reMatchCheck + `(?:,` + reMatchCheck + `)*)`)
  1337  
  1338  	// Regexp to extract a single opcoded check
  1339  	rxAsmCheck = regexp.MustCompile(reMatchCheck)
  1340  
  1341  	// List of all architecture variants. Key is the GOARCH architecture,
  1342  	// value[0] is the variant-changing environment variable, and values[1:]
  1343  	// are the supported variants.
  1344  	archVariants = map[string][]string{
  1345  		"386":     {"GO386", "387", "sse2"},
  1346  		"amd64":   {},
  1347  		"arm":     {"GOARM", "5", "6", "7"},
  1348  		"arm64":   {},
  1349  		"mips":    {"GOMIPS", "hardfloat", "softfloat"},
  1350  		"mips64":  {"GOMIPS64", "hardfloat", "softfloat"},
  1351  		"ppc64":   {},
  1352  		"ppc64le": {},
  1353  		"s390x":   {},
  1354  	}
  1355  )
  1356  
  1357  // wantedAsmOpcode is a single asmcheck check
  1358  type wantedAsmOpcode struct {
  1359  	fileline string         // original source file/line (eg: "/path/foo.go:45")
  1360  	line     int            // original source line
  1361  	opcode   *regexp.Regexp // opcode check to be performed on assembly output
  1362  	negative bool           // true if the check is supposed to fail rather than pass
  1363  	found    bool           // true if the opcode check matched at least one in the output
  1364  }
  1365  
  1366  // A build environment triplet separated by slashes (eg: linux/386/sse2).
  1367  // The third field can be empty if the arch does not support variants (eg: "plan9/amd64/")
  1368  type buildEnv string
  1369  
  1370  // Environ returns the environment it represents in cmd.Environ() "key=val" format
  1371  // For instance, "linux/386/sse2".Environ() returns {"GOOS=linux", "GOARCH=386", "GO386=sse2"}
  1372  func (b buildEnv) Environ() []string {
  1373  	fields := strings.Split(string(b), "/")
  1374  	if len(fields) != 3 {
  1375  		panic("invalid buildEnv string: " + string(b))
  1376  	}
  1377  	env := []string{"GOOS=" + fields[0], "GOARCH=" + fields[1]}
  1378  	if fields[2] != "" {
  1379  		env = append(env, archVariants[fields[1]][0]+"="+fields[2])
  1380  	}
  1381  	return env
  1382  }
  1383  
  1384  // asmChecks represents all the asmcheck checks present in a test file
  1385  // The outer map key is the build triplet in which the checks must be performed.
  1386  // The inner map key represent the source file line ("filename.go:1234") at which the
  1387  // checks must be performed.
  1388  type asmChecks map[buildEnv]map[string][]wantedAsmOpcode
  1389  
  1390  // Envs returns all the buildEnv in which at least one check is present
  1391  func (a asmChecks) Envs() []buildEnv {
  1392  	var envs []buildEnv
  1393  	for e := range a {
  1394  		envs = append(envs, e)
  1395  	}
  1396  	sort.Slice(envs, func(i, j int) bool {
  1397  		return string(envs[i]) < string(envs[j])
  1398  	})
  1399  	return envs
  1400  }
  1401  
  1402  func (t *test) wantedAsmOpcodes(fn string) asmChecks {
  1403  	ops := make(asmChecks)
  1404  
  1405  	comment := ""
  1406  	src, _ := ioutil.ReadFile(fn)
  1407  	for i, line := range strings.Split(string(src), "\n") {
  1408  		matches := rxAsmComment.FindStringSubmatch(line)
  1409  		code, cmt := matches[1], matches[2]
  1410  
  1411  		// Keep comments pending in the comment variable until
  1412  		// we find a line that contains some code.
  1413  		comment += " " + cmt
  1414  		if code == "" {
  1415  			continue
  1416  		}
  1417  
  1418  		// Parse and extract any architecture check from comments,
  1419  		// made by one architecture name and multiple checks.
  1420  		lnum := fn + ":" + strconv.Itoa(i+1)
  1421  		for _, ac := range rxAsmPlatform.FindAllStringSubmatch(comment, -1) {
  1422  			archspec, allchecks := ac[1:4], ac[4]
  1423  
  1424  			var arch, subarch, os string
  1425  			switch {
  1426  			case archspec[2] != "": // 3 components: "linux/386/sse2"
  1427  				os, arch, subarch = archspec[0], archspec[1][1:], archspec[2][1:]
  1428  			case archspec[1] != "": // 2 components: "386/sse2"
  1429  				os, arch, subarch = "linux", archspec[0], archspec[1][1:]
  1430  			default: // 1 component: "386"
  1431  				os, arch, subarch = "linux", archspec[0], ""
  1432  			}
  1433  
  1434  			if _, ok := archVariants[arch]; !ok {
  1435  				log.Fatalf("%s:%d: unsupported architecture: %v", t.goFileName(), i+1, arch)
  1436  			}
  1437  
  1438  			// Create the build environments corresponding the above specifiers
  1439  			envs := make([]buildEnv, 0, 4)
  1440  			if subarch != "" {
  1441  				envs = append(envs, buildEnv(os+"/"+arch+"/"+subarch))
  1442  			} else {
  1443  				subarchs := archVariants[arch]
  1444  				if len(subarchs) == 0 {
  1445  					envs = append(envs, buildEnv(os+"/"+arch+"/"))
  1446  				} else {
  1447  					for _, sa := range archVariants[arch][1:] {
  1448  						envs = append(envs, buildEnv(os+"/"+arch+"/"+sa))
  1449  					}
  1450  				}
  1451  			}
  1452  
  1453  			for _, m := range rxAsmCheck.FindAllString(allchecks, -1) {
  1454  				negative := false
  1455  				if m[0] == '-' {
  1456  					negative = true
  1457  					m = m[1:]
  1458  				}
  1459  
  1460  				rxsrc, err := strconv.Unquote(m)
  1461  				if err != nil {
  1462  					log.Fatalf("%s:%d: error unquoting string: %v", t.goFileName(), i+1, err)
  1463  				}
  1464  
  1465  				// Compile the checks as regular expressions. Notice that we
  1466  				// consider checks as matching from the beginning of the actual
  1467  				// assembler source (that is, what is left on each line of the
  1468  				// compile -S output after we strip file/line info) to avoid
  1469  				// trivial bugs such as "ADD" matching "FADD". This
  1470  				// doesn't remove genericity: it's still possible to write
  1471  				// something like "F?ADD", but we make common cases simpler
  1472  				// to get right.
  1473  				oprx, err := regexp.Compile("^" + rxsrc)
  1474  				if err != nil {
  1475  					log.Fatalf("%s:%d: %v", t.goFileName(), i+1, err)
  1476  				}
  1477  
  1478  				for _, env := range envs {
  1479  					if ops[env] == nil {
  1480  						ops[env] = make(map[string][]wantedAsmOpcode)
  1481  					}
  1482  					ops[env][lnum] = append(ops[env][lnum], wantedAsmOpcode{
  1483  						negative: negative,
  1484  						fileline: lnum,
  1485  						line:     i + 1,
  1486  						opcode:   oprx,
  1487  					})
  1488  				}
  1489  			}
  1490  		}
  1491  		comment = ""
  1492  	}
  1493  
  1494  	return ops
  1495  }
  1496  
  1497  func (t *test) asmCheck(outStr string, fn string, env buildEnv, fullops map[string][]wantedAsmOpcode) (err error) {
  1498  	// The assembly output contains the concatenated dump of multiple functions.
  1499  	// the first line of each function begins at column 0, while the rest is
  1500  	// indented by a tabulation. These data structures help us index the
  1501  	// output by function.
  1502  	functionMarkers := make([]int, 1)
  1503  	lineFuncMap := make(map[string]int)
  1504  
  1505  	lines := strings.Split(outStr, "\n")
  1506  	rxLine := regexp.MustCompile(fmt.Sprintf(`\((%s:\d+)\)\s+(.*)`, regexp.QuoteMeta(fn)))
  1507  
  1508  	for nl, line := range lines {
  1509  		// Check if this line begins a function
  1510  		if len(line) > 0 && line[0] != '\t' {
  1511  			functionMarkers = append(functionMarkers, nl)
  1512  		}
  1513  
  1514  		// Search if this line contains a assembly opcode (which is prefixed by the
  1515  		// original source file/line in parenthesis)
  1516  		matches := rxLine.FindStringSubmatch(line)
  1517  		if len(matches) == 0 {
  1518  			continue
  1519  		}
  1520  		srcFileLine, asm := matches[1], matches[2]
  1521  
  1522  		// Associate the original file/line information to the current
  1523  		// function in the output; it will be useful to dump it in case
  1524  		// of error.
  1525  		lineFuncMap[srcFileLine] = len(functionMarkers) - 1
  1526  
  1527  		// If there are opcode checks associated to this source file/line,
  1528  		// run the checks.
  1529  		if ops, found := fullops[srcFileLine]; found {
  1530  			for i := range ops {
  1531  				if !ops[i].found && ops[i].opcode.FindString(asm) != "" {
  1532  					ops[i].found = true
  1533  				}
  1534  			}
  1535  		}
  1536  	}
  1537  	functionMarkers = append(functionMarkers, len(lines))
  1538  
  1539  	var failed []wantedAsmOpcode
  1540  	for _, ops := range fullops {
  1541  		for _, o := range ops {
  1542  			// There's a failure if a negative match was found,
  1543  			// or a positive match was not found.
  1544  			if o.negative == o.found {
  1545  				failed = append(failed, o)
  1546  			}
  1547  		}
  1548  	}
  1549  	if len(failed) == 0 {
  1550  		return
  1551  	}
  1552  
  1553  	// At least one asmcheck failed; report them
  1554  	sort.Slice(failed, func(i, j int) bool {
  1555  		return failed[i].line < failed[j].line
  1556  	})
  1557  
  1558  	lastFunction := -1
  1559  	var errbuf bytes.Buffer
  1560  	fmt.Fprintln(&errbuf)
  1561  	for _, o := range failed {
  1562  		// Dump the function in which this opcode check was supposed to
  1563  		// pass but failed.
  1564  		funcIdx := lineFuncMap[o.fileline]
  1565  		if funcIdx != 0 && funcIdx != lastFunction {
  1566  			funcLines := lines[functionMarkers[funcIdx]:functionMarkers[funcIdx+1]]
  1567  			log.Println(strings.Join(funcLines, "\n"))
  1568  			lastFunction = funcIdx // avoid printing same function twice
  1569  		}
  1570  
  1571  		if o.negative {
  1572  			fmt.Fprintf(&errbuf, "%s:%d: %s: wrong opcode found: %q\n", t.goFileName(), o.line, env, o.opcode.String())
  1573  		} else {
  1574  			fmt.Fprintf(&errbuf, "%s:%d: %s: opcode not found: %q\n", t.goFileName(), o.line, env, o.opcode.String())
  1575  		}
  1576  	}
  1577  	err = errors.New(errbuf.String())
  1578  	return
  1579  }
  1580  
  1581  // defaultRunOutputLimit returns the number of runoutput tests that
  1582  // can be executed in parallel.
  1583  func defaultRunOutputLimit() int {
  1584  	const maxArmCPU = 2
  1585  
  1586  	cpu := runtime.NumCPU()
  1587  	if runtime.GOARCH == "arm" && cpu > maxArmCPU {
  1588  		cpu = maxArmCPU
  1589  	}
  1590  	return cpu
  1591  }
  1592  
  1593  // checkShouldTest runs sanity checks on the shouldTest function.
  1594  func checkShouldTest() {
  1595  	assert := func(ok bool, _ string) {
  1596  		if !ok {
  1597  			panic("fail")
  1598  		}
  1599  	}
  1600  	assertNot := func(ok bool, _ string) { assert(!ok, "") }
  1601  
  1602  	// Simple tests.
  1603  	assert(shouldTest("// +build linux", "linux", "arm"))
  1604  	assert(shouldTest("// +build !windows", "linux", "arm"))
  1605  	assertNot(shouldTest("// +build !windows", "windows", "amd64"))
  1606  
  1607  	// A file with no build tags will always be tested.
  1608  	assert(shouldTest("// This is a test.", "os", "arch"))
  1609  
  1610  	// Build tags separated by a space are OR-ed together.
  1611  	assertNot(shouldTest("// +build arm 386", "linux", "amd64"))
  1612  
  1613  	// Build tags separated by a comma are AND-ed together.
  1614  	assertNot(shouldTest("// +build !windows,!plan9", "windows", "amd64"))
  1615  	assertNot(shouldTest("// +build !windows,!plan9", "plan9", "386"))
  1616  
  1617  	// Build tags on multiple lines are AND-ed together.
  1618  	assert(shouldTest("// +build !windows\n// +build amd64", "linux", "amd64"))
  1619  	assertNot(shouldTest("// +build !windows\n// +build amd64", "windows", "amd64"))
  1620  
  1621  	// Test that (!a OR !b) matches anything.
  1622  	assert(shouldTest("// +build !windows !plan9", "windows", "amd64"))
  1623  }
  1624  
  1625  // envForDir returns a copy of the environment
  1626  // suitable for running in the given directory.
  1627  // The environment is the current process's environment
  1628  // but with an updated $PWD, so that an os.Getwd in the
  1629  // child will be faster.
  1630  func envForDir(dir string) []string {
  1631  	env := os.Environ()
  1632  	for i, kv := range env {
  1633  		if strings.HasPrefix(kv, "PWD=") {
  1634  			env[i] = "PWD=" + dir
  1635  			return env
  1636  		}
  1637  	}
  1638  	env = append(env, "PWD="+dir)
  1639  	return env
  1640  }
  1641  
  1642  func getenv(key, def string) string {
  1643  	value := os.Getenv(key)
  1644  	if value != "" {
  1645  		return value
  1646  	}
  1647  	return def
  1648  }
  1649  

View as plain text