]> Cypherpunks.ru repositories - gostls13.git/blob - test/run.go
[dev.typeparams] all: merge master (c8f4e61) into dev.typeparams
[gostls13.git] / test / run.go
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         "go/build"
16         "hash/fnv"
17         "io"
18         "io/fs"
19         "io/ioutil"
20         "log"
21         "os"
22         "os/exec"
23         "path"
24         "path/filepath"
25         "regexp"
26         "runtime"
27         "sort"
28         "strconv"
29         "strings"
30         "time"
31         "unicode"
32 )
33
34 var (
35         verbose        = flag.Bool("v", false, "verbose. if set, parallelism is set to 1.")
36         keep           = flag.Bool("k", false, "keep. keep temporary directory.")
37         numParallel    = flag.Int("n", runtime.NumCPU(), "number of parallel tests to run")
38         summary        = flag.Bool("summary", false, "show summary of results")
39         allCodegen     = flag.Bool("all_codegen", defaultAllCodeGen(), "run all goos/goarch for codegen")
40         showSkips      = flag.Bool("show_skips", false, "show skipped tests")
41         runSkips       = flag.Bool("run_skips", false, "run skipped tests (ignore skip and build tags)")
42         linkshared     = flag.Bool("linkshared", false, "")
43         updateErrors   = flag.Bool("update_errors", false, "update error messages in test file based on compiler output")
44         runoutputLimit = flag.Int("l", defaultRunOutputLimit(), "number of parallel runoutput tests to run")
45         force          = flag.Bool("f", false, "ignore expected-failure test lists")
46         generics       = flag.String("G", defaultGLevels, "a comma-separated list of -G compiler flags to test with")
47
48         shard  = flag.Int("shard", 0, "shard index to run. Only applicable if -shards is non-zero.")
49         shards = flag.Int("shards", 0, "number of shards. If 0, all tests are run. This is used by the continuous build.")
50 )
51
52 var unifiedEnabled, defaultGLevels = func() (bool, string) {
53         // TODO(mdempsky): Change this to just "go env GOEXPERIMENT" after
54         // CL 328751 is merged back to dev.typeparams. In the mean time, we
55         // infer whether the "unified" experiment is default enabled by
56         // inspecting the output from `go tool compile -V`.
57         output := runOutput(goTool(), "tool", "compile", "-V")
58
59         // TODO(mdempsky): This will give false negatives if the unified
60         // experiment is enabled by default, but presumably at that point we
61         // won't need to disable tests for it anymore anyway.
62         enabled := strings.Contains(output, "unified")
63
64         // Normal test runs should test with both -G=0 and -G=3 for types2
65         // coverage. But the unified experiment always uses types2, so
66         // testing with -G=3 is redundant.
67         glevels := "0,3"
68         if enabled {
69                 glevels = "0"
70         }
71
72         return enabled, glevels
73 }()
74
75 // defaultAllCodeGen returns the default value of the -all_codegen
76 // flag. By default, we prefer to be fast (returning false), except on
77 // the linux-amd64 builder that's already very fast, so we get more
78 // test coverage on trybots. See https://golang.org/issue/34297.
79 func defaultAllCodeGen() bool {
80         return os.Getenv("GO_BUILDER_NAME") == "linux-amd64"
81 }
82
83 var (
84         goos, goarch string
85         cgoEnabled   bool
86
87         // dirs are the directories to look for *.go files in.
88         // TODO(bradfitz): just use all directories?
89         dirs = []string{".", "ken", "chan", "interface", "syntax", "dwarf", "fixedbugs", "codegen", "runtime", "abi", "typeparam", "typeparam/mdempsky"}
90
91         // ratec controls the max number of tests running at a time.
92         ratec chan bool
93
94         // toRun is the channel of tests to run.
95         // It is nil until the first test is started.
96         toRun chan *test
97
98         // rungatec controls the max number of runoutput tests
99         // executed in parallel as they can each consume a lot of memory.
100         rungatec chan bool
101 )
102
103 // maxTests is an upper bound on the total number of tests.
104 // It is used as a channel buffer size to make sure sends don't block.
105 const maxTests = 5000
106
107 func main() {
108         flag.Parse()
109
110         var glevels []int
111         for _, s := range strings.Split(*generics, ",") {
112                 glevel, err := strconv.Atoi(s)
113                 if err != nil {
114                         log.Fatalf("invalid -G flag: %v", err)
115                 }
116                 glevels = append(glevels, glevel)
117         }
118
119         goos = getenv("GOOS", runtime.GOOS)
120         goarch = getenv("GOARCH", runtime.GOARCH)
121
122         cgoEnv := runOutput(goTool(), "env", "CGO_ENABLED")
123         cgoEnabled, _ = strconv.ParseBool(strings.TrimSpace(cgoEnv))
124
125         findExecCmd()
126
127         // Disable parallelism if printing or if using a simulator.
128         if *verbose || len(findExecCmd()) > 0 {
129                 *numParallel = 1
130                 *runoutputLimit = 1
131         }
132
133         ratec = make(chan bool, *numParallel)
134         rungatec = make(chan bool, *runoutputLimit)
135
136         var tests []*test
137         if flag.NArg() > 0 {
138                 for _, arg := range flag.Args() {
139                         if arg == "-" || arg == "--" {
140                                 // Permit running:
141                                 // $ go run run.go - env.go
142                                 // $ go run run.go -- env.go
143                                 // $ go run run.go - ./fixedbugs
144                                 // $ go run run.go -- ./fixedbugs
145                                 continue
146                         }
147                         if fi, err := os.Stat(arg); err == nil && fi.IsDir() {
148                                 for _, baseGoFile := range goFiles(arg) {
149                                         tests = append(tests, startTests(arg, baseGoFile, glevels)...)
150                                 }
151                         } else if strings.HasSuffix(arg, ".go") {
152                                 dir, file := filepath.Split(arg)
153                                 tests = append(tests, startTests(dir, file, glevels)...)
154                         } else {
155                                 log.Fatalf("can't yet deal with non-directory and non-go file %q", arg)
156                         }
157                 }
158         } else {
159                 for _, dir := range dirs {
160                         for _, baseGoFile := range goFiles(dir) {
161                                 tests = append(tests, startTests(dir, baseGoFile, glevels)...)
162                         }
163                 }
164         }
165
166         failed := false
167         resCount := map[string]int{}
168         for _, test := range tests {
169                 <-test.donec
170                 status := "ok  "
171                 errStr := ""
172                 if e, isSkip := test.err.(skipError); isSkip {
173                         test.err = nil
174                         errStr = "unexpected skip for " + path.Join(test.dir, test.gofile) + ": " + string(e)
175                         status = "FAIL"
176                 }
177                 if test.err != nil {
178                         errStr = test.err.Error()
179                         if test.expectFail {
180                                 errStr += " (expected)"
181                         } else {
182                                 status = "FAIL"
183                         }
184                 } else if test.expectFail {
185                         status = "FAIL"
186                         errStr = "unexpected success"
187                 }
188                 if status == "FAIL" {
189                         failed = true
190                 }
191                 resCount[status]++
192                 dt := fmt.Sprintf("%.3fs", test.dt.Seconds())
193                 if status == "FAIL" {
194                         fmt.Printf("# go run run.go -G=%v %s\n%s\nFAIL\t%s\t%s\n",
195                                 test.glevel,
196                                 path.Join(test.dir, test.gofile),
197                                 errStr, test.goFileName(), dt)
198                         continue
199                 }
200                 if !*verbose {
201                         continue
202                 }
203                 fmt.Printf("%s\t%s\t%s\n", status, test.goFileName(), dt)
204         }
205
206         if *summary {
207                 for k, v := range resCount {
208                         fmt.Printf("%5d %s\n", v, k)
209                 }
210         }
211
212         if failed {
213                 os.Exit(1)
214         }
215 }
216
217 // runOutput runs the specified command and returns its output as a
218 // string. If the command fails, runOutput logs the error and exits.
219 func runOutput(name string, args ...string) string {
220         cmd := exec.Command(name, args...)
221         output, err := cmd.Output()
222         if err != nil {
223                 log.Fatalf("running %v: %v", cmd, err)
224         }
225         return string(output)
226 }
227
228 // goTool reports the path of the go tool to use to run the tests.
229 // If possible, use the same Go used to run run.go, otherwise
230 // fallback to the go version found in the PATH.
231 func goTool() string {
232         var exeSuffix string
233         if runtime.GOOS == "windows" {
234                 exeSuffix = ".exe"
235         }
236         path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
237         if _, err := os.Stat(path); err == nil {
238                 return path
239         }
240         // Just run "go" from PATH
241         return "go"
242 }
243
244 func shardMatch(name string) bool {
245         if *shards == 0 {
246                 return true
247         }
248         h := fnv.New32()
249         io.WriteString(h, name)
250         return int(h.Sum32()%uint32(*shards)) == *shard
251 }
252
253 func goFiles(dir string) []string {
254         f, err := os.Open(dir)
255         if err != nil {
256                 log.Fatal(err)
257         }
258         dirnames, err := f.Readdirnames(-1)
259         f.Close()
260         if err != nil {
261                 log.Fatal(err)
262         }
263         names := []string{}
264         for _, name := range dirnames {
265                 if !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && shardMatch(name) {
266                         names = append(names, name)
267                 }
268         }
269         sort.Strings(names)
270         return names
271 }
272
273 type runCmd func(...string) ([]byte, error)
274
275 func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, err error) {
276         cmd := []string{goTool(), "tool", "compile", "-e"}
277         cmd = append(cmd, flags...)
278         if *linkshared {
279                 cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
280         }
281         cmd = append(cmd, longname)
282         return runcmd(cmd...)
283 }
284
285 func compileInDir(runcmd runCmd, dir string, flags []string, localImports bool, names ...string) (out []byte, err error) {
286         cmd := []string{goTool(), "tool", "compile", "-e"}
287         if localImports {
288                 // Set relative path for local imports and import search path to current dir.
289                 cmd = append(cmd, "-D", ".", "-I", ".")
290         }
291         cmd = append(cmd, flags...)
292         if *linkshared {
293                 cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
294         }
295         for _, name := range names {
296                 cmd = append(cmd, filepath.Join(dir, name))
297         }
298         return runcmd(cmd...)
299 }
300
301 func linkFile(runcmd runCmd, goname string, ldflags []string) (err error) {
302         pfile := strings.Replace(goname, ".go", ".o", -1)
303         cmd := []string{goTool(), "tool", "link", "-w", "-o", "a.exe", "-L", "."}
304         if *linkshared {
305                 cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
306         }
307         if ldflags != nil {
308                 cmd = append(cmd, ldflags...)
309         }
310         cmd = append(cmd, pfile)
311         _, err = runcmd(cmd...)
312         return
313 }
314
315 // skipError describes why a test was skipped.
316 type skipError string
317
318 func (s skipError) Error() string { return string(s) }
319
320 // test holds the state of a test.
321 type test struct {
322         dir, gofile string
323         donec       chan bool // closed when done
324         dt          time.Duration
325         glevel      int // what -G level this test should use
326
327         src string
328
329         tempDir string
330         err     error
331
332         // expectFail indicates whether the (overall) test recipe is
333         // expected to fail under the current test configuration (e.g., -G=3
334         // or GOEXPERIMENT=unified).
335         expectFail bool
336 }
337
338 // initExpectFail initializes t.expectFail based on the build+test
339 // configuration. It should only be called for tests known to use
340 // types2.
341 func (t *test) initExpectFail() {
342         if *force {
343                 return
344         }
345
346         failureSets := []map[string]bool{types2Failures}
347
348         // Note: gccgo supports more 32-bit architectures than this, but
349         // hopefully the 32-bit failures are fixed before this matters.
350         switch goarch {
351         case "386", "arm", "mips", "mipsle":
352                 failureSets = append(failureSets, types2Failures32Bit)
353         }
354
355         if unifiedEnabled {
356                 failureSets = append(failureSets, unifiedFailures)
357         } else {
358                 failureSets = append(failureSets, g3Failures)
359         }
360
361         filename := strings.Replace(t.goFileName(), "\\", "/", -1) // goFileName() uses \ on Windows
362
363         for _, set := range failureSets {
364                 if set[filename] {
365                         t.expectFail = true
366                         return
367                 }
368         }
369 }
370
371 func startTests(dir, gofile string, glevels []int) []*test {
372         tests := make([]*test, len(glevels))
373         for i, glevel := range glevels {
374                 t := &test{
375                         dir:    dir,
376                         gofile: gofile,
377                         glevel: glevel,
378                         donec:  make(chan bool, 1),
379                 }
380                 if toRun == nil {
381                         toRun = make(chan *test, maxTests)
382                         go runTests()
383                 }
384                 select {
385                 case toRun <- t:
386                 default:
387                         panic("toRun buffer size (maxTests) is too small")
388                 }
389                 tests[i] = t
390         }
391         return tests
392 }
393
394 // runTests runs tests in parallel, but respecting the order they
395 // were enqueued on the toRun channel.
396 func runTests() {
397         for {
398                 ratec <- true
399                 t := <-toRun
400                 go func() {
401                         t.run()
402                         <-ratec
403                 }()
404         }
405 }
406
407 var cwd, _ = os.Getwd()
408
409 func (t *test) goFileName() string {
410         return filepath.Join(t.dir, t.gofile)
411 }
412
413 func (t *test) goDirName() string {
414         return filepath.Join(t.dir, strings.Replace(t.gofile, ".go", ".dir", -1))
415 }
416
417 func goDirFiles(longdir string) (filter []os.FileInfo, err error) {
418         files, dirErr := ioutil.ReadDir(longdir)
419         if dirErr != nil {
420                 return nil, dirErr
421         }
422         for _, gofile := range files {
423                 if filepath.Ext(gofile.Name()) == ".go" {
424                         filter = append(filter, gofile)
425                 }
426         }
427         return
428 }
429
430 var packageRE = regexp.MustCompile(`(?m)^package ([\p{Lu}\p{Ll}\w]+)`)
431
432 func getPackageNameFromSource(fn string) (string, error) {
433         data, err := ioutil.ReadFile(fn)
434         if err != nil {
435                 return "", err
436         }
437         pkgname := packageRE.FindStringSubmatch(string(data))
438         if pkgname == nil {
439                 return "", fmt.Errorf("cannot find package name in %s", fn)
440         }
441         return pkgname[1], nil
442 }
443
444 // If singlefilepkgs is set, each file is considered a separate package
445 // even if the package names are the same.
446 func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
447         files, err := goDirFiles(longdir)
448         if err != nil {
449                 return nil, err
450         }
451         var pkgs [][]string
452         m := make(map[string]int)
453         for _, file := range files {
454                 name := file.Name()
455                 pkgname, err := getPackageNameFromSource(filepath.Join(longdir, name))
456                 if err != nil {
457                         log.Fatal(err)
458                 }
459                 i, ok := m[pkgname]
460                 if singlefilepkgs || !ok {
461                         i = len(pkgs)
462                         pkgs = append(pkgs, nil)
463                         m[pkgname] = i
464                 }
465                 pkgs[i] = append(pkgs[i], name)
466         }
467         return pkgs, nil
468 }
469
470 type context struct {
471         GOOS       string
472         GOARCH     string
473         cgoEnabled bool
474         noOptEnv   bool
475 }
476
477 // shouldTest looks for build tags in a source file and returns
478 // whether the file should be used according to the tags.
479 func shouldTest(src string, goos, goarch string) (ok bool, whyNot string) {
480         if *runSkips {
481                 return true, ""
482         }
483         for _, line := range strings.Split(src, "\n") {
484                 line = strings.TrimSpace(line)
485                 if strings.HasPrefix(line, "//") {
486                         line = line[2:]
487                 } else {
488                         continue
489                 }
490                 line = strings.TrimSpace(line)
491                 if len(line) == 0 || line[0] != '+' {
492                         continue
493                 }
494                 gcFlags := os.Getenv("GO_GCFLAGS")
495                 ctxt := &context{
496                         GOOS:       goos,
497                         GOARCH:     goarch,
498                         cgoEnabled: cgoEnabled,
499                         noOptEnv:   strings.Contains(gcFlags, "-N") || strings.Contains(gcFlags, "-l"),
500                 }
501
502                 words := strings.Fields(line)
503                 if words[0] == "+build" {
504                         ok := false
505                         for _, word := range words[1:] {
506                                 if ctxt.match(word) {
507                                         ok = true
508                                         break
509                                 }
510                         }
511                         if !ok {
512                                 // no matching tag found.
513                                 return false, line
514                         }
515                 }
516         }
517         // no build tags
518         return true, ""
519 }
520
521 func (ctxt *context) match(name string) bool {
522         if name == "" {
523                 return false
524         }
525         if i := strings.Index(name, ","); i >= 0 {
526                 // comma-separated list
527                 return ctxt.match(name[:i]) && ctxt.match(name[i+1:])
528         }
529         if strings.HasPrefix(name, "!!") { // bad syntax, reject always
530                 return false
531         }
532         if strings.HasPrefix(name, "!") { // negation
533                 return len(name) > 1 && !ctxt.match(name[1:])
534         }
535
536         // Tags must be letters, digits, underscores or dots.
537         // Unlike in Go identifiers, all digits are fine (e.g., "386").
538         for _, c := range name {
539                 if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
540                         return false
541                 }
542         }
543
544         if strings.HasPrefix(name, "goexperiment.") {
545                 for _, tag := range build.Default.ToolTags {
546                         if tag == name {
547                                 return true
548                         }
549                 }
550                 return false
551         }
552
553         if name == "cgo" && ctxt.cgoEnabled {
554                 return true
555         }
556
557         if name == ctxt.GOOS || name == ctxt.GOARCH || name == "gc" {
558                 return true
559         }
560
561         if ctxt.noOptEnv && name == "gcflags_noopt" {
562                 return true
563         }
564
565         if name == "test_run" {
566                 return true
567         }
568
569         return false
570 }
571
572 func init() { checkShouldTest() }
573
574 // goGcflags returns the -gcflags argument to use with go build / go run.
575 // This must match the flags used for building the standard library,
576 // or else the commands will rebuild any needed packages (like runtime)
577 // over and over.
578 func (t *test) goGcflags() string {
579         flags := os.Getenv("GO_GCFLAGS")
580         if t.glevel != 0 {
581                 flags = fmt.Sprintf("%s -G=%v", flags, t.glevel)
582         }
583         return "-gcflags=all=" + flags
584 }
585
586 func (t *test) goGcflagsIsEmpty() bool {
587         return "" == os.Getenv("GO_GCFLAGS") && t.glevel == 0
588 }
589
590 var errTimeout = errors.New("command exceeded time limit")
591
592 // run runs a test.
593 func (t *test) run() {
594         start := time.Now()
595         defer func() {
596                 t.dt = time.Since(start)
597                 close(t.donec)
598         }()
599
600         srcBytes, err := ioutil.ReadFile(t.goFileName())
601         if err != nil {
602                 t.err = err
603                 return
604         }
605         t.src = string(srcBytes)
606         if t.src[0] == '\n' {
607                 t.err = skipError("starts with newline")
608                 return
609         }
610
611         // Execution recipe stops at first blank line.
612         pos := strings.Index(t.src, "\n\n")
613         if pos == -1 {
614                 t.err = fmt.Errorf("double newline ending execution recipe not found in %s", t.goFileName())
615                 return
616         }
617         action := t.src[:pos]
618         if nl := strings.Index(action, "\n"); nl >= 0 && strings.Contains(action[:nl], "+build") {
619                 // skip first line
620                 action = action[nl+1:]
621         }
622         action = strings.TrimPrefix(action, "//")
623
624         // Check for build constraints only up to the actual code.
625         pkgPos := strings.Index(t.src, "\npackage")
626         if pkgPos == -1 {
627                 pkgPos = pos // some files are intentionally malformed
628         }
629         if ok, why := shouldTest(t.src[:pkgPos], goos, goarch); !ok {
630                 if *showSkips {
631                         fmt.Printf("%-20s %-20s: %s\n", "skip", t.goFileName(), why)
632                 }
633                 return
634         }
635
636         var args, flags, runenv []string
637         var tim int
638         wantError := false
639         wantAuto := false
640         singlefilepkgs := false
641         setpkgpaths := false
642         localImports := true
643         f, err := splitQuoted(action)
644         if err != nil {
645                 t.err = fmt.Errorf("invalid test recipe: %v", err)
646                 return
647         }
648         if len(f) > 0 {
649                 action = f[0]
650                 args = f[1:]
651         }
652
653         // TODO: Clean up/simplify this switch statement.
654         switch action {
655         case "compile", "compiledir", "build", "builddir", "buildrundir", "run", "buildrun", "runoutput", "rundir", "runindir", "asmcheck":
656                 // nothing to do
657         case "errorcheckandrundir":
658                 wantError = false // should be no error if also will run
659         case "errorcheckwithauto":
660                 action = "errorcheck"
661                 wantAuto = true
662                 wantError = true
663         case "errorcheck", "errorcheckdir", "errorcheckoutput":
664                 wantError = true
665         case "skip":
666                 if *runSkips {
667                         break
668                 }
669                 return
670         default:
671                 t.err = skipError("skipped; unknown pattern: " + action)
672                 return
673         }
674
675         // collect flags
676         for len(args) > 0 && strings.HasPrefix(args[0], "-") {
677                 switch args[0] {
678                 case "-1":
679                         wantError = true
680                 case "-0":
681                         wantError = false
682                 case "-s":
683                         singlefilepkgs = true
684                 case "-P":
685                         setpkgpaths = true
686                 case "-n":
687                         // Do not set relative path for local imports to current dir,
688                         // e.g. do not pass -D . -I . to the compiler.
689                         // Used in fixedbugs/bug345.go to allow compilation and import of local pkg.
690                         // See golang.org/issue/25635
691                         localImports = false
692                 case "-t": // timeout in seconds
693                         args = args[1:]
694                         var err error
695                         tim, err = strconv.Atoi(args[0])
696                         if err != nil {
697                                 t.err = fmt.Errorf("need number of seconds for -t timeout, got %s instead", args[0])
698                         }
699                 case "-goexperiment": // set GOEXPERIMENT environment
700                         args = args[1:]
701                         runenv = append(runenv, "GOEXPERIMENT="+args[0])
702
703                 default:
704                         flags = append(flags, args[0])
705                 }
706                 args = args[1:]
707         }
708         if action == "errorcheck" {
709                 found := false
710                 for i, f := range flags {
711                         if strings.HasPrefix(f, "-d=") {
712                                 flags[i] = f + ",ssa/check/on"
713                                 found = true
714                                 break
715                         }
716                 }
717                 if !found {
718                         flags = append(flags, "-d=ssa/check/on")
719                 }
720         }
721
722         type Tool int
723
724         const (
725                 _ Tool = iota
726                 AsmCheck
727                 Build
728                 Run
729                 Compile
730         )
731
732         // validForGLevel reports whether the current test is valid to run
733         // at the specified -G level. If so, it may update flags as
734         // necessary to test with -G.
735         validForGLevel := func(tool Tool) bool {
736                 hasGFlag := false
737                 for _, flag := range flags {
738                         if strings.Contains(flag, "-G") {
739                                 hasGFlag = true
740                         }
741                 }
742
743                 if hasGFlag && t.glevel != 0 {
744                         // test provides explicit -G flag already; don't run again
745                         if *verbose {
746                                 fmt.Printf("excl\t%s\n", t.goFileName())
747                         }
748                         return false
749                 }
750
751                 if t.glevel == 0 && !hasGFlag && !unifiedEnabled {
752                         // tests should always pass when run w/o types2 (i.e., using the
753                         // legacy typechecker).
754                         return true
755                 }
756
757                 t.initExpectFail()
758
759                 switch tool {
760                 case Build, Run:
761                         // ok; handled in goGcflags
762
763                 case Compile:
764                         if !hasGFlag {
765                                 flags = append(flags, fmt.Sprintf("-G=%v", t.glevel))
766                         }
767
768                 default:
769                         // we don't know how to add -G for this test yet
770                         if *verbose {
771                                 fmt.Printf("excl\t%s\n", t.goFileName())
772                         }
773                         return false
774                 }
775
776                 return true
777         }
778
779         t.makeTempDir()
780         if !*keep {
781                 defer os.RemoveAll(t.tempDir)
782         }
783
784         err = ioutil.WriteFile(filepath.Join(t.tempDir, t.gofile), srcBytes, 0644)
785         if err != nil {
786                 log.Fatal(err)
787         }
788
789         // A few tests (of things like the environment) require these to be set.
790         if os.Getenv("GOOS") == "" {
791                 os.Setenv("GOOS", runtime.GOOS)
792         }
793         if os.Getenv("GOARCH") == "" {
794                 os.Setenv("GOARCH", runtime.GOARCH)
795         }
796
797         var (
798                 runInDir        = t.tempDir
799                 tempDirIsGOPATH = false
800         )
801         runcmd := func(args ...string) ([]byte, error) {
802                 cmd := exec.Command(args[0], args[1:]...)
803                 var buf bytes.Buffer
804                 cmd.Stdout = &buf
805                 cmd.Stderr = &buf
806                 cmd.Env = append(os.Environ(), "GOENV=off", "GOFLAGS=")
807                 if runInDir != "" {
808                         cmd.Dir = runInDir
809                         // Set PWD to match Dir to speed up os.Getwd in the child process.
810                         cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
811                 }
812                 if tempDirIsGOPATH {
813                         cmd.Env = append(cmd.Env, "GOPATH="+t.tempDir)
814                 }
815                 cmd.Env = append(cmd.Env, runenv...)
816
817                 var err error
818
819                 if tim != 0 {
820                         err = cmd.Start()
821                         // This command-timeout code adapted from cmd/go/test.go
822                         if err == nil {
823                                 tick := time.NewTimer(time.Duration(tim) * time.Second)
824                                 done := make(chan error)
825                                 go func() {
826                                         done <- cmd.Wait()
827                                 }()
828                                 select {
829                                 case err = <-done:
830                                         // ok
831                                 case <-tick.C:
832                                         cmd.Process.Signal(os.Interrupt)
833                                         time.Sleep(1 * time.Second)
834                                         cmd.Process.Kill()
835                                         <-done
836                                         err = errTimeout
837                                 }
838                                 tick.Stop()
839                         }
840                 } else {
841                         err = cmd.Run()
842                 }
843                 if err != nil && err != errTimeout {
844                         err = fmt.Errorf("%s\n%s", err, buf.Bytes())
845                 }
846                 return buf.Bytes(), err
847         }
848
849         long := filepath.Join(cwd, t.goFileName())
850         switch action {
851         default:
852                 t.err = fmt.Errorf("unimplemented action %q", action)
853
854         case "asmcheck":
855                 if !validForGLevel(AsmCheck) {
856                         return
857                 }
858
859                 // Compile Go file and match the generated assembly
860                 // against a set of regexps in comments.
861                 ops := t.wantedAsmOpcodes(long)
862                 self := runtime.GOOS + "/" + runtime.GOARCH
863                 for _, env := range ops.Envs() {
864                         // Only run checks relevant to the current GOOS/GOARCH,
865                         // to avoid triggering a cross-compile of the runtime.
866                         if string(env) != self && !strings.HasPrefix(string(env), self+"/") && !*allCodegen {
867                                 continue
868                         }
869                         // -S=2 forces outermost line numbers when disassembling inlined code.
870                         cmdline := []string{"build", "-gcflags", "-S=2"}
871
872                         // Append flags, but don't override -gcflags=-S=2; add to it instead.
873                         for i := 0; i < len(flags); i++ {
874                                 flag := flags[i]
875                                 switch {
876                                 case strings.HasPrefix(flag, "-gcflags="):
877                                         cmdline[2] += " " + strings.TrimPrefix(flag, "-gcflags=")
878                                 case strings.HasPrefix(flag, "--gcflags="):
879                                         cmdline[2] += " " + strings.TrimPrefix(flag, "--gcflags=")
880                                 case flag == "-gcflags", flag == "--gcflags":
881                                         i++
882                                         if i < len(flags) {
883                                                 cmdline[2] += " " + flags[i]
884                                         }
885                                 default:
886                                         cmdline = append(cmdline, flag)
887                                 }
888                         }
889
890                         cmdline = append(cmdline, long)
891                         cmd := exec.Command(goTool(), cmdline...)
892                         cmd.Env = append(os.Environ(), env.Environ()...)
893                         if len(flags) > 0 && flags[0] == "-race" {
894                                 cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
895                         }
896
897                         var buf bytes.Buffer
898                         cmd.Stdout, cmd.Stderr = &buf, &buf
899                         if err := cmd.Run(); err != nil {
900                                 fmt.Println(env, "\n", cmd.Stderr)
901                                 t.err = err
902                                 return
903                         }
904
905                         t.err = t.asmCheck(buf.String(), long, env, ops[env])
906                         if t.err != nil {
907                                 return
908                         }
909                 }
910                 return
911
912         case "errorcheck":
913                 if !validForGLevel(Compile) {
914                         return
915                 }
916
917                 // Compile Go file.
918                 // Fail if wantError is true and compilation was successful and vice versa.
919                 // Match errors produced by gc against errors in comments.
920                 // TODO(gri) remove need for -C (disable printing of columns in error messages)
921                 cmdline := []string{goTool(), "tool", "compile", "-d=panic", "-C", "-e", "-o", "a.o"}
922                 // No need to add -dynlink even if linkshared if we're just checking for errors...
923                 cmdline = append(cmdline, flags...)
924                 cmdline = append(cmdline, long)
925                 out, err := runcmd(cmdline...)
926                 if wantError {
927                         if err == nil {
928                                 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
929                                 return
930                         }
931                         if err == errTimeout {
932                                 t.err = fmt.Errorf("compilation timed out")
933                                 return
934                         }
935                 } else {
936                         if err != nil {
937                                 t.err = err
938                                 return
939                         }
940                 }
941                 if *updateErrors {
942                         t.updateErrors(string(out), long)
943                 }
944                 t.err = t.errorCheck(string(out), wantAuto, long, t.gofile)
945
946         case "compile":
947                 if !validForGLevel(Compile) {
948                         return
949                 }
950
951                 // Compile Go file.
952                 _, t.err = compileFile(runcmd, long, flags)
953
954         case "compiledir":
955                 if !validForGLevel(Compile) {
956                         return
957                 }
958
959                 // Compile all files in the directory as packages in lexicographic order.
960                 longdir := filepath.Join(cwd, t.goDirName())
961                 pkgs, err := goDirPackages(longdir, singlefilepkgs)
962                 if err != nil {
963                         t.err = err
964                         return
965                 }
966                 for _, gofiles := range pkgs {
967                         _, t.err = compileInDir(runcmd, longdir, flags, localImports, gofiles...)
968                         if t.err != nil {
969                                 return
970                         }
971                 }
972
973         case "errorcheckdir", "errorcheckandrundir":
974                 if !validForGLevel(Compile) {
975                         return
976                 }
977
978                 flags = append(flags, "-d=panic")
979                 // Compile and errorCheck all files in the directory as packages in lexicographic order.
980                 // If errorcheckdir and wantError, compilation of the last package must fail.
981                 // If errorcheckandrundir and wantError, compilation of the package prior the last must fail.
982                 longdir := filepath.Join(cwd, t.goDirName())
983                 pkgs, err := goDirPackages(longdir, singlefilepkgs)
984                 if err != nil {
985                         t.err = err
986                         return
987                 }
988                 errPkg := len(pkgs) - 1
989                 if wantError && action == "errorcheckandrundir" {
990                         // The last pkg should compiled successfully and will be run in next case.
991                         // Preceding pkg must return an error from compileInDir.
992                         errPkg--
993                 }
994                 for i, gofiles := range pkgs {
995                         out, err := compileInDir(runcmd, longdir, flags, localImports, gofiles...)
996                         if i == errPkg {
997                                 if wantError && err == nil {
998                                         t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
999                                         return
1000                                 } else if !wantError && err != nil {
1001                                         t.err = err
1002                                         return
1003                                 }
1004                         } else if err != nil {
1005                                 t.err = err
1006                                 return
1007                         }
1008                         var fullshort []string
1009                         for _, name := range gofiles {
1010                                 fullshort = append(fullshort, filepath.Join(longdir, name), name)
1011                         }
1012                         t.err = t.errorCheck(string(out), wantAuto, fullshort...)
1013                         if t.err != nil {
1014                                 break
1015                         }
1016                 }
1017                 if action == "errorcheckdir" {
1018                         return
1019                 }
1020                 fallthrough
1021
1022         case "rundir":
1023                 if !validForGLevel(Run) {
1024                         return
1025                 }
1026
1027                 // Compile all files in the directory as packages in lexicographic order.
1028                 // In case of errorcheckandrundir, ignore failed compilation of the package before the last.
1029                 // Link as if the last file is the main package, run it.
1030                 // Verify the expected output.
1031                 longdir := filepath.Join(cwd, t.goDirName())
1032                 pkgs, err := goDirPackages(longdir, singlefilepkgs)
1033                 if err != nil {
1034                         t.err = err
1035                         return
1036                 }
1037                 // Split flags into gcflags and ldflags
1038                 ldflags := []string{}
1039                 for i, fl := range flags {
1040                         if fl == "-ldflags" {
1041                                 ldflags = flags[i+1:]
1042                                 flags = flags[0:i]
1043                                 break
1044                         }
1045                 }
1046
1047                 for i, gofiles := range pkgs {
1048                         pflags := []string{}
1049                         pflags = append(pflags, flags...)
1050                         if setpkgpaths {
1051                                 fp := filepath.Join(longdir, gofiles[0])
1052                                 pkgname, err := getPackageNameFromSource(fp)
1053                                 if err != nil {
1054                                         log.Fatal(err)
1055                                 }
1056                                 pflags = append(pflags, "-p", pkgname)
1057                         }
1058                         _, err := compileInDir(runcmd, longdir, pflags, localImports, gofiles...)
1059                         // Allow this package compilation fail based on conditions below;
1060                         // its errors were checked in previous case.
1061                         if err != nil && !(wantError && action == "errorcheckandrundir" && i == len(pkgs)-2) {
1062                                 t.err = err
1063                                 return
1064                         }
1065                         if i == len(pkgs)-1 {
1066                                 err = linkFile(runcmd, gofiles[0], ldflags)
1067                                 if err != nil {
1068                                         t.err = err
1069                                         return
1070                                 }
1071                                 var cmd []string
1072                                 cmd = append(cmd, findExecCmd()...)
1073                                 cmd = append(cmd, filepath.Join(t.tempDir, "a.exe"))
1074                                 cmd = append(cmd, args...)
1075                                 out, err := runcmd(cmd...)
1076                                 if err != nil {
1077                                         t.err = err
1078                                         return
1079                                 }
1080                                 t.checkExpectedOutput(out)
1081                         }
1082                 }
1083
1084         case "runindir":
1085                 if !validForGLevel(Run) {
1086                         return
1087                 }
1088
1089                 // Make a shallow copy of t.goDirName() in its own module and GOPATH, and
1090                 // run "go run ." in it. The module path (and hence import path prefix) of
1091                 // the copy is equal to the basename of the source directory.
1092                 //
1093                 // It's used when test a requires a full 'go build' in order to compile
1094                 // the sources, such as when importing multiple packages (issue29612.dir)
1095                 // or compiling a package containing assembly files (see issue15609.dir),
1096                 // but still needs to be run to verify the expected output.
1097                 tempDirIsGOPATH = true
1098                 srcDir := t.goDirName()
1099                 modName := filepath.Base(srcDir)
1100                 gopathSrcDir := filepath.Join(t.tempDir, "src", modName)
1101                 runInDir = gopathSrcDir
1102
1103                 if err := overlayDir(gopathSrcDir, srcDir); err != nil {
1104                         t.err = err
1105                         return
1106                 }
1107
1108                 modFile := fmt.Sprintf("module %s\ngo 1.14\n", modName)
1109                 if err := ioutil.WriteFile(filepath.Join(gopathSrcDir, "go.mod"), []byte(modFile), 0666); err != nil {
1110                         t.err = err
1111                         return
1112                 }
1113
1114                 cmd := []string{goTool(), "run", t.goGcflags()}
1115                 if *linkshared {
1116                         cmd = append(cmd, "-linkshared")
1117                 }
1118                 cmd = append(cmd, flags...)
1119                 cmd = append(cmd, ".")
1120                 out, err := runcmd(cmd...)
1121                 if err != nil {
1122                         t.err = err
1123                         return
1124                 }
1125                 t.checkExpectedOutput(out)
1126
1127         case "build":
1128                 if !validForGLevel(Build) {
1129                         return
1130                 }
1131
1132                 // Build Go file.
1133                 _, err := runcmd(goTool(), "build", t.goGcflags(), "-o", "a.exe", long)
1134                 if err != nil {
1135                         t.err = err
1136                 }
1137
1138         case "builddir", "buildrundir":
1139                 if !validForGLevel(Build) {
1140                         return
1141                 }
1142
1143                 // Build an executable from all the .go and .s files in a subdirectory.
1144                 // Run it and verify its output in the buildrundir case.
1145                 longdir := filepath.Join(cwd, t.goDirName())
1146                 files, dirErr := ioutil.ReadDir(longdir)
1147                 if dirErr != nil {
1148                         t.err = dirErr
1149                         break
1150                 }
1151                 var gos []string
1152                 var asms []string
1153                 for _, file := range files {
1154                         switch filepath.Ext(file.Name()) {
1155                         case ".go":
1156                                 gos = append(gos, filepath.Join(longdir, file.Name()))
1157                         case ".s":
1158                                 asms = append(asms, filepath.Join(longdir, file.Name()))
1159                         }
1160
1161                 }
1162                 if len(asms) > 0 {
1163                         emptyHdrFile := filepath.Join(t.tempDir, "go_asm.h")
1164                         if err := ioutil.WriteFile(emptyHdrFile, nil, 0666); err != nil {
1165                                 t.err = fmt.Errorf("write empty go_asm.h: %s", err)
1166                                 return
1167                         }
1168                         cmd := []string{goTool(), "tool", "asm", "-gensymabis", "-o", "symabis"}
1169                         cmd = append(cmd, asms...)
1170                         _, err = runcmd(cmd...)
1171                         if err != nil {
1172                                 t.err = err
1173                                 break
1174                         }
1175                 }
1176                 var objs []string
1177                 cmd := []string{goTool(), "tool", "compile", "-e", "-D", ".", "-I", ".", "-o", "go.o"}
1178                 if len(asms) > 0 {
1179                         cmd = append(cmd, "-asmhdr", "go_asm.h", "-symabis", "symabis")
1180                 }
1181                 cmd = append(cmd, gos...)
1182                 _, err := runcmd(cmd...)
1183                 if err != nil {
1184                         t.err = err
1185                         break
1186                 }
1187                 objs = append(objs, "go.o")
1188                 if len(asms) > 0 {
1189                         cmd = []string{goTool(), "tool", "asm", "-e", "-I", ".", "-o", "asm.o"}
1190                         cmd = append(cmd, asms...)
1191                         _, err = runcmd(cmd...)
1192                         if err != nil {
1193                                 t.err = err
1194                                 break
1195                         }
1196                         objs = append(objs, "asm.o")
1197                 }
1198                 cmd = []string{goTool(), "tool", "pack", "c", "all.a"}
1199                 cmd = append(cmd, objs...)
1200                 _, err = runcmd(cmd...)
1201                 if err != nil {
1202                         t.err = err
1203                         break
1204                 }
1205                 cmd = []string{goTool(), "tool", "link", "-o", "a.exe", "all.a"}
1206                 _, err = runcmd(cmd...)
1207                 if err != nil {
1208                         t.err = err
1209                         break
1210                 }
1211                 if action == "buildrundir" {
1212                         cmd = append(findExecCmd(), filepath.Join(t.tempDir, "a.exe"))
1213                         out, err := runcmd(cmd...)
1214                         if err != nil {
1215                                 t.err = err
1216                                 break
1217                         }
1218                         t.checkExpectedOutput(out)
1219                 }
1220
1221         case "buildrun":
1222                 if !validForGLevel(Build) {
1223                         return
1224                 }
1225
1226                 // Build an executable from Go file, then run it, verify its output.
1227                 // Useful for timeout tests where failure mode is infinite loop.
1228                 // TODO: not supported on NaCl
1229                 cmd := []string{goTool(), "build", t.goGcflags(), "-o", "a.exe"}
1230                 if *linkshared {
1231                         cmd = append(cmd, "-linkshared")
1232                 }
1233                 longdirgofile := filepath.Join(filepath.Join(cwd, t.dir), t.gofile)
1234                 cmd = append(cmd, flags...)
1235                 cmd = append(cmd, longdirgofile)
1236                 _, err := runcmd(cmd...)
1237                 if err != nil {
1238                         t.err = err
1239                         return
1240                 }
1241                 cmd = []string{"./a.exe"}
1242                 out, err := runcmd(append(cmd, args...)...)
1243                 if err != nil {
1244                         t.err = err
1245                         return
1246                 }
1247
1248                 t.checkExpectedOutput(out)
1249
1250         case "run":
1251                 if !validForGLevel(Run) {
1252                         return
1253                 }
1254
1255                 // Run Go file if no special go command flags are provided;
1256                 // otherwise build an executable and run it.
1257                 // Verify the output.
1258                 runInDir = ""
1259                 var out []byte
1260                 var err error
1261                 if len(flags)+len(args) == 0 && t.goGcflagsIsEmpty() && !*linkshared && goarch == runtime.GOARCH && goos == runtime.GOOS {
1262                         // If we're not using special go command flags,
1263                         // skip all the go command machinery.
1264                         // This avoids any time the go command would
1265                         // spend checking whether, for example, the installed
1266                         // package runtime is up to date.
1267                         // Because we run lots of trivial test programs,
1268                         // the time adds up.
1269                         pkg := filepath.Join(t.tempDir, "pkg.a")
1270                         if _, err := runcmd(goTool(), "tool", "compile", "-o", pkg, t.goFileName()); err != nil {
1271                                 t.err = err
1272                                 return
1273                         }
1274                         exe := filepath.Join(t.tempDir, "test.exe")
1275                         cmd := []string{goTool(), "tool", "link", "-s", "-w"}
1276                         cmd = append(cmd, "-o", exe, pkg)
1277                         if _, err := runcmd(cmd...); err != nil {
1278                                 t.err = err
1279                                 return
1280                         }
1281                         out, err = runcmd(append([]string{exe}, args...)...)
1282                 } else {
1283                         cmd := []string{goTool(), "run", t.goGcflags()}
1284                         if *linkshared {
1285                                 cmd = append(cmd, "-linkshared")
1286                         }
1287                         cmd = append(cmd, flags...)
1288                         cmd = append(cmd, t.goFileName())
1289                         out, err = runcmd(append(cmd, args...)...)
1290                 }
1291                 if err != nil {
1292                         t.err = err
1293                         return
1294                 }
1295                 t.checkExpectedOutput(out)
1296
1297         case "runoutput":
1298                 if !validForGLevel(Run) {
1299                         return
1300                 }
1301
1302                 // Run Go file and write its output into temporary Go file.
1303                 // Run generated Go file and verify its output.
1304                 rungatec <- true
1305                 defer func() {
1306                         <-rungatec
1307                 }()
1308                 runInDir = ""
1309                 cmd := []string{goTool(), "run", t.goGcflags()}
1310                 if *linkshared {
1311                         cmd = append(cmd, "-linkshared")
1312                 }
1313                 cmd = append(cmd, t.goFileName())
1314                 out, err := runcmd(append(cmd, args...)...)
1315                 if err != nil {
1316                         t.err = err
1317                         return
1318                 }
1319                 tfile := filepath.Join(t.tempDir, "tmp__.go")
1320                 if err := ioutil.WriteFile(tfile, out, 0666); err != nil {
1321                         t.err = fmt.Errorf("write tempfile:%s", err)
1322                         return
1323                 }
1324                 cmd = []string{goTool(), "run", t.goGcflags()}
1325                 if *linkshared {
1326                         cmd = append(cmd, "-linkshared")
1327                 }
1328                 cmd = append(cmd, tfile)
1329                 out, err = runcmd(cmd...)
1330                 if err != nil {
1331                         t.err = err
1332                         return
1333                 }
1334                 t.checkExpectedOutput(out)
1335
1336         case "errorcheckoutput":
1337                 if !validForGLevel(Compile) {
1338                         return
1339                 }
1340
1341                 // Run Go file and write its output into temporary Go file.
1342                 // Compile and errorCheck generated Go file.
1343                 runInDir = ""
1344                 cmd := []string{goTool(), "run", t.goGcflags()}
1345                 if *linkshared {
1346                         cmd = append(cmd, "-linkshared")
1347                 }
1348                 cmd = append(cmd, t.goFileName())
1349                 out, err := runcmd(append(cmd, args...)...)
1350                 if err != nil {
1351                         t.err = err
1352                         return
1353                 }
1354                 tfile := filepath.Join(t.tempDir, "tmp__.go")
1355                 err = ioutil.WriteFile(tfile, out, 0666)
1356                 if err != nil {
1357                         t.err = fmt.Errorf("write tempfile:%s", err)
1358                         return
1359                 }
1360                 cmdline := []string{goTool(), "tool", "compile", "-d=panic", "-e", "-o", "a.o"}
1361                 cmdline = append(cmdline, flags...)
1362                 cmdline = append(cmdline, tfile)
1363                 out, err = runcmd(cmdline...)
1364                 if wantError {
1365                         if err == nil {
1366                                 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
1367                                 return
1368                         }
1369                 } else {
1370                         if err != nil {
1371                                 t.err = err
1372                                 return
1373                         }
1374                 }
1375                 t.err = t.errorCheck(string(out), false, tfile, "tmp__.go")
1376                 return
1377         }
1378 }
1379
1380 var execCmd []string
1381
1382 func findExecCmd() []string {
1383         if execCmd != nil {
1384                 return execCmd
1385         }
1386         execCmd = []string{} // avoid work the second time
1387         if goos == runtime.GOOS && goarch == runtime.GOARCH {
1388                 return execCmd
1389         }
1390         path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch))
1391         if err == nil {
1392                 execCmd = []string{path}
1393         }
1394         return execCmd
1395 }
1396
1397 func (t *test) String() string {
1398         return filepath.Join(t.dir, t.gofile)
1399 }
1400
1401 func (t *test) makeTempDir() {
1402         var err error
1403         t.tempDir, err = ioutil.TempDir("", "")
1404         if err != nil {
1405                 log.Fatal(err)
1406         }
1407         if *keep {
1408                 log.Printf("Temporary directory is %s", t.tempDir)
1409         }
1410 }
1411
1412 // checkExpectedOutput compares the output from compiling and/or running with the contents
1413 // of the corresponding reference output file, if any (replace ".go" with ".out").
1414 // If they don't match, fail with an informative message.
1415 func (t *test) checkExpectedOutput(gotBytes []byte) {
1416         got := string(gotBytes)
1417         filename := filepath.Join(t.dir, t.gofile)
1418         filename = filename[:len(filename)-len(".go")]
1419         filename += ".out"
1420         b, err := ioutil.ReadFile(filename)
1421         // File is allowed to be missing (err != nil) in which case output should be empty.
1422         got = strings.Replace(got, "\r\n", "\n", -1)
1423         if got != string(b) {
1424                 if err == nil {
1425                         t.err = fmt.Errorf("output does not match expected in %s. Instead saw\n%s", filename, got)
1426                 } else {
1427                         t.err = fmt.Errorf("output should be empty when (optional) expected-output file %s is not present. Instead saw\n%s", filename, got)
1428                 }
1429         }
1430 }
1431
1432 func splitOutput(out string, wantAuto bool) []string {
1433         // gc error messages continue onto additional lines with leading tabs.
1434         // Split the output at the beginning of each line that doesn't begin with a tab.
1435         // <autogenerated> lines are impossible to match so those are filtered out.
1436         var res []string
1437         for _, line := range strings.Split(out, "\n") {
1438                 if strings.HasSuffix(line, "\r") { // remove '\r', output by compiler on windows
1439                         line = line[:len(line)-1]
1440                 }
1441                 if strings.HasPrefix(line, "\t") {
1442                         res[len(res)-1] += "\n" + line
1443                 } else if strings.HasPrefix(line, "go tool") || strings.HasPrefix(line, "#") || !wantAuto && strings.HasPrefix(line, "<autogenerated>") {
1444                         continue
1445                 } else if strings.TrimSpace(line) != "" {
1446                         res = append(res, line)
1447                 }
1448         }
1449         return res
1450 }
1451
1452 // errorCheck matches errors in outStr against comments in source files.
1453 // For each line of the source files which should generate an error,
1454 // there should be a comment of the form // ERROR "regexp".
1455 // If outStr has an error for a line which has no such comment,
1456 // this function will report an error.
1457 // Likewise if outStr does not have an error for a line which has a comment,
1458 // or if the error message does not match the <regexp>.
1459 // The <regexp> syntax is Perl but it's best to stick to egrep.
1460 //
1461 // Sources files are supplied as fullshort slice.
1462 // It consists of pairs: full path to source file and its base name.
1463 func (t *test) errorCheck(outStr string, wantAuto bool, fullshort ...string) (err error) {
1464         defer func() {
1465                 if *verbose && err != nil {
1466                         log.Printf("%s gc output:\n%s", t, outStr)
1467                 }
1468         }()
1469         var errs []error
1470         out := splitOutput(outStr, wantAuto)
1471
1472         // Cut directory name.
1473         for i := range out {
1474                 for j := 0; j < len(fullshort); j += 2 {
1475                         full, short := fullshort[j], fullshort[j+1]
1476                         out[i] = strings.Replace(out[i], full, short, -1)
1477                 }
1478         }
1479
1480         var want []wantedError
1481         for j := 0; j < len(fullshort); j += 2 {
1482                 full, short := fullshort[j], fullshort[j+1]
1483                 want = append(want, t.wantedErrors(full, short)...)
1484         }
1485
1486         for _, we := range want {
1487                 var errmsgs []string
1488                 if we.auto {
1489                         errmsgs, out = partitionStrings("<autogenerated>", out)
1490                 } else {
1491                         errmsgs, out = partitionStrings(we.prefix, out)
1492                 }
1493                 if len(errmsgs) == 0 {
1494                         errs = append(errs, fmt.Errorf("%s:%d: missing error %q", we.file, we.lineNum, we.reStr))
1495                         continue
1496                 }
1497                 matched := false
1498                 n := len(out)
1499                 for _, errmsg := range errmsgs {
1500                         // Assume errmsg says "file:line: foo".
1501                         // Cut leading "file:line: " to avoid accidental matching of file name instead of message.
1502                         text := errmsg
1503                         if i := strings.Index(text, " "); i >= 0 {
1504                                 text = text[i+1:]
1505                         }
1506                         if we.re.MatchString(text) {
1507                                 matched = true
1508                         } else {
1509                                 out = append(out, errmsg)
1510                         }
1511                 }
1512                 if !matched {
1513                         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")))
1514                         continue
1515                 }
1516         }
1517
1518         if len(out) > 0 {
1519                 errs = append(errs, fmt.Errorf("Unmatched Errors:"))
1520                 for _, errLine := range out {
1521                         errs = append(errs, fmt.Errorf("%s", errLine))
1522                 }
1523         }
1524
1525         if len(errs) == 0 {
1526                 return nil
1527         }
1528         if len(errs) == 1 {
1529                 return errs[0]
1530         }
1531         var buf bytes.Buffer
1532         fmt.Fprintf(&buf, "\n")
1533         for _, err := range errs {
1534                 fmt.Fprintf(&buf, "%s\n", err.Error())
1535         }
1536         return errors.New(buf.String())
1537 }
1538
1539 func (t *test) updateErrors(out, file string) {
1540         base := path.Base(file)
1541         // Read in source file.
1542         src, err := ioutil.ReadFile(file)
1543         if err != nil {
1544                 fmt.Fprintln(os.Stderr, err)
1545                 return
1546         }
1547         lines := strings.Split(string(src), "\n")
1548         // Remove old errors.
1549         for i, ln := range lines {
1550                 pos := strings.Index(ln, " // ERROR ")
1551                 if pos >= 0 {
1552                         lines[i] = ln[:pos]
1553                 }
1554         }
1555         // Parse new errors.
1556         errors := make(map[int]map[string]bool)
1557         tmpRe := regexp.MustCompile(`autotmp_[0-9]+`)
1558         for _, errStr := range splitOutput(out, false) {
1559                 colon1 := strings.Index(errStr, ":")
1560                 if colon1 < 0 || errStr[:colon1] != file {
1561                         continue
1562                 }
1563                 colon2 := strings.Index(errStr[colon1+1:], ":")
1564                 if colon2 < 0 {
1565                         continue
1566                 }
1567                 colon2 += colon1 + 1
1568                 line, err := strconv.Atoi(errStr[colon1+1 : colon2])
1569                 line--
1570                 if err != nil || line < 0 || line >= len(lines) {
1571                         continue
1572                 }
1573                 msg := errStr[colon2+2:]
1574                 msg = strings.Replace(msg, file, base, -1) // normalize file mentions in error itself
1575                 msg = strings.TrimLeft(msg, " \t")
1576                 for _, r := range []string{`\`, `*`, `+`, `?`, `[`, `]`, `(`, `)`} {
1577                         msg = strings.Replace(msg, r, `\`+r, -1)
1578                 }
1579                 msg = strings.Replace(msg, `"`, `.`, -1)
1580                 msg = tmpRe.ReplaceAllLiteralString(msg, `autotmp_[0-9]+`)
1581                 if errors[line] == nil {
1582                         errors[line] = make(map[string]bool)
1583                 }
1584                 errors[line][msg] = true
1585         }
1586         // Add new errors.
1587         for line, errs := range errors {
1588                 var sorted []string
1589                 for e := range errs {
1590                         sorted = append(sorted, e)
1591                 }
1592                 sort.Strings(sorted)
1593                 lines[line] += " // ERROR"
1594                 for _, e := range sorted {
1595                         lines[line] += fmt.Sprintf(` "%s$"`, e)
1596                 }
1597         }
1598         // Write new file.
1599         err = ioutil.WriteFile(file, []byte(strings.Join(lines, "\n")), 0640)
1600         if err != nil {
1601                 fmt.Fprintln(os.Stderr, err)
1602                 return
1603         }
1604         // Polish.
1605         exec.Command(goTool(), "fmt", file).CombinedOutput()
1606 }
1607
1608 // matchPrefix reports whether s is of the form ^(.*/)?prefix(:|[),
1609 // That is, it needs the file name prefix followed by a : or a [,
1610 // and possibly preceded by a directory name.
1611 func matchPrefix(s, prefix string) bool {
1612         i := strings.Index(s, ":")
1613         if i < 0 {
1614                 return false
1615         }
1616         j := strings.LastIndex(s[:i], "/")
1617         s = s[j+1:]
1618         if len(s) <= len(prefix) || s[:len(prefix)] != prefix {
1619                 return false
1620         }
1621         switch s[len(prefix)] {
1622         case '[', ':':
1623                 return true
1624         }
1625         return false
1626 }
1627
1628 func partitionStrings(prefix string, strs []string) (matched, unmatched []string) {
1629         for _, s := range strs {
1630                 if matchPrefix(s, prefix) {
1631                         matched = append(matched, s)
1632                 } else {
1633                         unmatched = append(unmatched, s)
1634                 }
1635         }
1636         return
1637 }
1638
1639 type wantedError struct {
1640         reStr   string
1641         re      *regexp.Regexp
1642         lineNum int
1643         auto    bool // match <autogenerated> line
1644         file    string
1645         prefix  string
1646 }
1647
1648 var (
1649         errRx       = regexp.MustCompile(`// (?:GC_)?ERROR (.*)`)
1650         errAutoRx   = regexp.MustCompile(`// (?:GC_)?ERRORAUTO (.*)`)
1651         errQuotesRx = regexp.MustCompile(`"([^"]*)"`)
1652         lineRx      = regexp.MustCompile(`LINE(([+-])([0-9]+))?`)
1653 )
1654
1655 func (t *test) wantedErrors(file, short string) (errs []wantedError) {
1656         cache := make(map[string]*regexp.Regexp)
1657
1658         src, _ := ioutil.ReadFile(file)
1659         for i, line := range strings.Split(string(src), "\n") {
1660                 lineNum := i + 1
1661                 if strings.Contains(line, "////") {
1662                         // double comment disables ERROR
1663                         continue
1664                 }
1665                 var auto bool
1666                 m := errAutoRx.FindStringSubmatch(line)
1667                 if m != nil {
1668                         auto = true
1669                 } else {
1670                         m = errRx.FindStringSubmatch(line)
1671                 }
1672                 if m == nil {
1673                         continue
1674                 }
1675                 all := m[1]
1676                 mm := errQuotesRx.FindAllStringSubmatch(all, -1)
1677                 if mm == nil {
1678                         log.Fatalf("%s:%d: invalid errchk line: %s", t.goFileName(), lineNum, line)
1679                 }
1680                 for _, m := range mm {
1681                         rx := lineRx.ReplaceAllStringFunc(m[1], func(m string) string {
1682                                 n := lineNum
1683                                 if strings.HasPrefix(m, "LINE+") {
1684                                         delta, _ := strconv.Atoi(m[5:])
1685                                         n += delta
1686                                 } else if strings.HasPrefix(m, "LINE-") {
1687                                         delta, _ := strconv.Atoi(m[5:])
1688                                         n -= delta
1689                                 }
1690                                 return fmt.Sprintf("%s:%d", short, n)
1691                         })
1692                         re := cache[rx]
1693                         if re == nil {
1694                                 var err error
1695                                 re, err = regexp.Compile(rx)
1696                                 if err != nil {
1697                                         log.Fatalf("%s:%d: invalid regexp \"%s\" in ERROR line: %v", t.goFileName(), lineNum, rx, err)
1698                                 }
1699                                 cache[rx] = re
1700                         }
1701                         prefix := fmt.Sprintf("%s:%d", short, lineNum)
1702                         errs = append(errs, wantedError{
1703                                 reStr:   rx,
1704                                 re:      re,
1705                                 prefix:  prefix,
1706                                 auto:    auto,
1707                                 lineNum: lineNum,
1708                                 file:    short,
1709                         })
1710                 }
1711         }
1712
1713         return
1714 }
1715
1716 const (
1717         // Regexp to match a single opcode check: optionally begin with "-" (to indicate
1718         // a negative check), followed by a string literal enclosed in "" or ``. For "",
1719         // backslashes must be handled.
1720         reMatchCheck = `-?(?:\x60[^\x60]*\x60|"(?:[^"\\]|\\.)*")`
1721 )
1722
1723 var (
1724         // Regexp to split a line in code and comment, trimming spaces
1725         rxAsmComment = regexp.MustCompile(`^\s*(.*?)\s*(?://\s*(.+)\s*)?$`)
1726
1727         // Regexp to extract an architecture check: architecture name (or triplet),
1728         // followed by semi-colon, followed by a comma-separated list of opcode checks.
1729         // Extraneous spaces are ignored.
1730         rxAsmPlatform = regexp.MustCompile(`(\w+)(/\w+)?(/\w*)?\s*:\s*(` + reMatchCheck + `(?:\s*,\s*` + reMatchCheck + `)*)`)
1731
1732         // Regexp to extract a single opcoded check
1733         rxAsmCheck = regexp.MustCompile(reMatchCheck)
1734
1735         // List of all architecture variants. Key is the GOARCH architecture,
1736         // value[0] is the variant-changing environment variable, and values[1:]
1737         // are the supported variants.
1738         archVariants = map[string][]string{
1739                 "386":     {"GO386", "sse2", "softfloat"},
1740                 "amd64":   {},
1741                 "arm":     {"GOARM", "5", "6", "7"},
1742                 "arm64":   {},
1743                 "mips":    {"GOMIPS", "hardfloat", "softfloat"},
1744                 "mips64":  {"GOMIPS64", "hardfloat", "softfloat"},
1745                 "ppc64":   {"GOPPC64", "power8", "power9"},
1746                 "ppc64le": {"GOPPC64", "power8", "power9"},
1747                 "s390x":   {},
1748                 "wasm":    {},
1749         }
1750 )
1751
1752 // wantedAsmOpcode is a single asmcheck check
1753 type wantedAsmOpcode struct {
1754         fileline string         // original source file/line (eg: "/path/foo.go:45")
1755         line     int            // original source line
1756         opcode   *regexp.Regexp // opcode check to be performed on assembly output
1757         negative bool           // true if the check is supposed to fail rather than pass
1758         found    bool           // true if the opcode check matched at least one in the output
1759 }
1760
1761 // A build environment triplet separated by slashes (eg: linux/386/sse2).
1762 // The third field can be empty if the arch does not support variants (eg: "plan9/amd64/")
1763 type buildEnv string
1764
1765 // Environ returns the environment it represents in cmd.Environ() "key=val" format
1766 // For instance, "linux/386/sse2".Environ() returns {"GOOS=linux", "GOARCH=386", "GO386=sse2"}
1767 func (b buildEnv) Environ() []string {
1768         fields := strings.Split(string(b), "/")
1769         if len(fields) != 3 {
1770                 panic("invalid buildEnv string: " + string(b))
1771         }
1772         env := []string{"GOOS=" + fields[0], "GOARCH=" + fields[1]}
1773         if fields[2] != "" {
1774                 env = append(env, archVariants[fields[1]][0]+"="+fields[2])
1775         }
1776         return env
1777 }
1778
1779 // asmChecks represents all the asmcheck checks present in a test file
1780 // The outer map key is the build triplet in which the checks must be performed.
1781 // The inner map key represent the source file line ("filename.go:1234") at which the
1782 // checks must be performed.
1783 type asmChecks map[buildEnv]map[string][]wantedAsmOpcode
1784
1785 // Envs returns all the buildEnv in which at least one check is present
1786 func (a asmChecks) Envs() []buildEnv {
1787         var envs []buildEnv
1788         for e := range a {
1789                 envs = append(envs, e)
1790         }
1791         sort.Slice(envs, func(i, j int) bool {
1792                 return string(envs[i]) < string(envs[j])
1793         })
1794         return envs
1795 }
1796
1797 func (t *test) wantedAsmOpcodes(fn string) asmChecks {
1798         ops := make(asmChecks)
1799
1800         comment := ""
1801         src, _ := ioutil.ReadFile(fn)
1802         for i, line := range strings.Split(string(src), "\n") {
1803                 matches := rxAsmComment.FindStringSubmatch(line)
1804                 code, cmt := matches[1], matches[2]
1805
1806                 // Keep comments pending in the comment variable until
1807                 // we find a line that contains some code.
1808                 comment += " " + cmt
1809                 if code == "" {
1810                         continue
1811                 }
1812
1813                 // Parse and extract any architecture check from comments,
1814                 // made by one architecture name and multiple checks.
1815                 lnum := fn + ":" + strconv.Itoa(i+1)
1816                 for _, ac := range rxAsmPlatform.FindAllStringSubmatch(comment, -1) {
1817                         archspec, allchecks := ac[1:4], ac[4]
1818
1819                         var arch, subarch, os string
1820                         switch {
1821                         case archspec[2] != "": // 3 components: "linux/386/sse2"
1822                                 os, arch, subarch = archspec[0], archspec[1][1:], archspec[2][1:]
1823                         case archspec[1] != "": // 2 components: "386/sse2"
1824                                 os, arch, subarch = "linux", archspec[0], archspec[1][1:]
1825                         default: // 1 component: "386"
1826                                 os, arch, subarch = "linux", archspec[0], ""
1827                                 if arch == "wasm" {
1828                                         os = "js"
1829                                 }
1830                         }
1831
1832                         if _, ok := archVariants[arch]; !ok {
1833                                 log.Fatalf("%s:%d: unsupported architecture: %v", t.goFileName(), i+1, arch)
1834                         }
1835
1836                         // Create the build environments corresponding the above specifiers
1837                         envs := make([]buildEnv, 0, 4)
1838                         if subarch != "" {
1839                                 envs = append(envs, buildEnv(os+"/"+arch+"/"+subarch))
1840                         } else {
1841                                 subarchs := archVariants[arch]
1842                                 if len(subarchs) == 0 {
1843                                         envs = append(envs, buildEnv(os+"/"+arch+"/"))
1844                                 } else {
1845                                         for _, sa := range archVariants[arch][1:] {
1846                                                 envs = append(envs, buildEnv(os+"/"+arch+"/"+sa))
1847                                         }
1848                                 }
1849                         }
1850
1851                         for _, m := range rxAsmCheck.FindAllString(allchecks, -1) {
1852                                 negative := false
1853                                 if m[0] == '-' {
1854                                         negative = true
1855                                         m = m[1:]
1856                                 }
1857
1858                                 rxsrc, err := strconv.Unquote(m)
1859                                 if err != nil {
1860                                         log.Fatalf("%s:%d: error unquoting string: %v", t.goFileName(), i+1, err)
1861                                 }
1862
1863                                 // Compile the checks as regular expressions. Notice that we
1864                                 // consider checks as matching from the beginning of the actual
1865                                 // assembler source (that is, what is left on each line of the
1866                                 // compile -S output after we strip file/line info) to avoid
1867                                 // trivial bugs such as "ADD" matching "FADD". This
1868                                 // doesn't remove genericity: it's still possible to write
1869                                 // something like "F?ADD", but we make common cases simpler
1870                                 // to get right.
1871                                 oprx, err := regexp.Compile("^" + rxsrc)
1872                                 if err != nil {
1873                                         log.Fatalf("%s:%d: %v", t.goFileName(), i+1, err)
1874                                 }
1875
1876                                 for _, env := range envs {
1877                                         if ops[env] == nil {
1878                                                 ops[env] = make(map[string][]wantedAsmOpcode)
1879                                         }
1880                                         ops[env][lnum] = append(ops[env][lnum], wantedAsmOpcode{
1881                                                 negative: negative,
1882                                                 fileline: lnum,
1883                                                 line:     i + 1,
1884                                                 opcode:   oprx,
1885                                         })
1886                                 }
1887                         }
1888                 }
1889                 comment = ""
1890         }
1891
1892         return ops
1893 }
1894
1895 func (t *test) asmCheck(outStr string, fn string, env buildEnv, fullops map[string][]wantedAsmOpcode) (err error) {
1896         // The assembly output contains the concatenated dump of multiple functions.
1897         // the first line of each function begins at column 0, while the rest is
1898         // indented by a tabulation. These data structures help us index the
1899         // output by function.
1900         functionMarkers := make([]int, 1)
1901         lineFuncMap := make(map[string]int)
1902
1903         lines := strings.Split(outStr, "\n")
1904         rxLine := regexp.MustCompile(fmt.Sprintf(`\((%s:\d+)\)\s+(.*)`, regexp.QuoteMeta(fn)))
1905
1906         for nl, line := range lines {
1907                 // Check if this line begins a function
1908                 if len(line) > 0 && line[0] != '\t' {
1909                         functionMarkers = append(functionMarkers, nl)
1910                 }
1911
1912                 // Search if this line contains a assembly opcode (which is prefixed by the
1913                 // original source file/line in parenthesis)
1914                 matches := rxLine.FindStringSubmatch(line)
1915                 if len(matches) == 0 {
1916                         continue
1917                 }
1918                 srcFileLine, asm := matches[1], matches[2]
1919
1920                 // Associate the original file/line information to the current
1921                 // function in the output; it will be useful to dump it in case
1922                 // of error.
1923                 lineFuncMap[srcFileLine] = len(functionMarkers) - 1
1924
1925                 // If there are opcode checks associated to this source file/line,
1926                 // run the checks.
1927                 if ops, found := fullops[srcFileLine]; found {
1928                         for i := range ops {
1929                                 if !ops[i].found && ops[i].opcode.FindString(asm) != "" {
1930                                         ops[i].found = true
1931                                 }
1932                         }
1933                 }
1934         }
1935         functionMarkers = append(functionMarkers, len(lines))
1936
1937         var failed []wantedAsmOpcode
1938         for _, ops := range fullops {
1939                 for _, o := range ops {
1940                         // There's a failure if a negative match was found,
1941                         // or a positive match was not found.
1942                         if o.negative == o.found {
1943                                 failed = append(failed, o)
1944                         }
1945                 }
1946         }
1947         if len(failed) == 0 {
1948                 return
1949         }
1950
1951         // At least one asmcheck failed; report them
1952         sort.Slice(failed, func(i, j int) bool {
1953                 return failed[i].line < failed[j].line
1954         })
1955
1956         lastFunction := -1
1957         var errbuf bytes.Buffer
1958         fmt.Fprintln(&errbuf)
1959         for _, o := range failed {
1960                 // Dump the function in which this opcode check was supposed to
1961                 // pass but failed.
1962                 funcIdx := lineFuncMap[o.fileline]
1963                 if funcIdx != 0 && funcIdx != lastFunction {
1964                         funcLines := lines[functionMarkers[funcIdx]:functionMarkers[funcIdx+1]]
1965                         log.Println(strings.Join(funcLines, "\n"))
1966                         lastFunction = funcIdx // avoid printing same function twice
1967                 }
1968
1969                 if o.negative {
1970                         fmt.Fprintf(&errbuf, "%s:%d: %s: wrong opcode found: %q\n", t.goFileName(), o.line, env, o.opcode.String())
1971                 } else {
1972                         fmt.Fprintf(&errbuf, "%s:%d: %s: opcode not found: %q\n", t.goFileName(), o.line, env, o.opcode.String())
1973                 }
1974         }
1975         err = errors.New(errbuf.String())
1976         return
1977 }
1978
1979 // defaultRunOutputLimit returns the number of runoutput tests that
1980 // can be executed in parallel.
1981 func defaultRunOutputLimit() int {
1982         const maxArmCPU = 2
1983
1984         cpu := runtime.NumCPU()
1985         if runtime.GOARCH == "arm" && cpu > maxArmCPU {
1986                 cpu = maxArmCPU
1987         }
1988         return cpu
1989 }
1990
1991 // checkShouldTest runs sanity checks on the shouldTest function.
1992 func checkShouldTest() {
1993         assert := func(ok bool, _ string) {
1994                 if !ok {
1995                         panic("fail")
1996                 }
1997         }
1998         assertNot := func(ok bool, _ string) { assert(!ok, "") }
1999
2000         // Simple tests.
2001         assert(shouldTest("// +build linux", "linux", "arm"))
2002         assert(shouldTest("// +build !windows", "linux", "arm"))
2003         assertNot(shouldTest("// +build !windows", "windows", "amd64"))
2004
2005         // A file with no build tags will always be tested.
2006         assert(shouldTest("// This is a test.", "os", "arch"))
2007
2008         // Build tags separated by a space are OR-ed together.
2009         assertNot(shouldTest("// +build arm 386", "linux", "amd64"))
2010
2011         // Build tags separated by a comma are AND-ed together.
2012         assertNot(shouldTest("// +build !windows,!plan9", "windows", "amd64"))
2013         assertNot(shouldTest("// +build !windows,!plan9", "plan9", "386"))
2014
2015         // Build tags on multiple lines are AND-ed together.
2016         assert(shouldTest("// +build !windows\n// +build amd64", "linux", "amd64"))
2017         assertNot(shouldTest("// +build !windows\n// +build amd64", "windows", "amd64"))
2018
2019         // Test that (!a OR !b) matches anything.
2020         assert(shouldTest("// +build !windows !plan9", "windows", "amd64"))
2021 }
2022
2023 func getenv(key, def string) string {
2024         value := os.Getenv(key)
2025         if value != "" {
2026                 return value
2027         }
2028         return def
2029 }
2030
2031 // overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
2032 func overlayDir(dstRoot, srcRoot string) error {
2033         dstRoot = filepath.Clean(dstRoot)
2034         if err := os.MkdirAll(dstRoot, 0777); err != nil {
2035                 return err
2036         }
2037
2038         srcRoot, err := filepath.Abs(srcRoot)
2039         if err != nil {
2040                 return err
2041         }
2042
2043         return filepath.WalkDir(srcRoot, func(srcPath string, d fs.DirEntry, err error) error {
2044                 if err != nil || srcPath == srcRoot {
2045                         return err
2046                 }
2047
2048                 suffix := strings.TrimPrefix(srcPath, srcRoot)
2049                 for len(suffix) > 0 && suffix[0] == filepath.Separator {
2050                         suffix = suffix[1:]
2051                 }
2052                 dstPath := filepath.Join(dstRoot, suffix)
2053
2054                 var info fs.FileInfo
2055                 if d.Type()&os.ModeSymlink != 0 {
2056                         info, err = os.Stat(srcPath)
2057                 } else {
2058                         info, err = d.Info()
2059                 }
2060                 if err != nil {
2061                         return err
2062                 }
2063                 perm := info.Mode() & os.ModePerm
2064
2065                 // Always copy directories (don't symlink them).
2066                 // If we add a file in the overlay, we don't want to add it in the original.
2067                 if info.IsDir() {
2068                         return os.MkdirAll(dstPath, perm|0200)
2069                 }
2070
2071                 // If the OS supports symlinks, use them instead of copying bytes.
2072                 if err := os.Symlink(srcPath, dstPath); err == nil {
2073                         return nil
2074                 }
2075
2076                 // Otherwise, copy the bytes.
2077                 src, err := os.Open(srcPath)
2078                 if err != nil {
2079                         return err
2080                 }
2081                 defer src.Close()
2082
2083                 dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
2084                 if err != nil {
2085                         return err
2086                 }
2087
2088                 _, err = io.Copy(dst, src)
2089                 if closeErr := dst.Close(); err == nil {
2090                         err = closeErr
2091                 }
2092                 return err
2093         })
2094 }
2095
2096 // The following is temporary scaffolding to get types2 typechecker
2097 // up and running against the existing test cases. The explicitly
2098 // listed files don't pass yet, usually because the error messages
2099 // are slightly different (this list is not complete). Any errorcheck
2100 // tests that require output from analysis phases past initial type-
2101 // checking are also excluded since these phases are not running yet.
2102 // We can get rid of this code once types2 is fully plugged in.
2103
2104 // List of files that the compiler cannot errorcheck with the new typechecker (compiler -G option).
2105 // Temporary scaffolding until we pass all the tests at which point this map can be removed.
2106 var types2Failures = setOf(
2107         "directive.go",    // misplaced compiler directive checks
2108         "float_lit3.go",   // types2 reports extra errors
2109         "import1.go",      // types2 reports extra errors
2110         "import6.go",      // issue #43109
2111         "initializerr.go", // types2 reports extra errors
2112         "linkname2.go",    // error reported by noder (not running for types2 errorcheck test)
2113         "notinheap.go",    // types2 doesn't report errors about conversions that are invalid due to //go:notinheap
2114         "shift1.go",       // issue #42989
2115         "typecheck.go",    // invalid function is not causing errors when called
2116
2117         "interface/private.go", // types2 phrases errors differently (doesn't use non-spec "private" term)
2118
2119         "fixedbugs/bug176.go", // types2 reports all errors (pref: types2)
2120         "fixedbugs/bug195.go", // types2 reports slightly different (but correct) bugs
2121         "fixedbugs/bug228.go", // types2 doesn't run when there are syntax errors
2122         "fixedbugs/bug231.go", // types2 bug? (same error reported twice)
2123         "fixedbugs/bug255.go", // types2 reports extra errors
2124         "fixedbugs/bug374.go", // types2 reports extra errors
2125         "fixedbugs/bug388.go", // types2 not run due to syntax errors
2126         "fixedbugs/bug412.go", // types2 produces a follow-on error
2127
2128         "fixedbugs/issue10700.go",  // types2 reports ok hint, but does not match regexp
2129         "fixedbugs/issue11590.go",  // types2 doesn't report a follow-on error (pref: types2)
2130         "fixedbugs/issue11610.go",  // types2 not run after syntax errors
2131         "fixedbugs/issue11614.go",  // types2 reports an extra error
2132         "fixedbugs/issue14520.go",  // missing import path error by types2
2133         "fixedbugs/issue16133.go",  // types2 doesn't use package path for qualified identifiers when package name is ambiguous
2134         "fixedbugs/issue16428.go",  // types2 reports two instead of one error
2135         "fixedbugs/issue17038.go",  // types2 doesn't report a follow-on error (pref: types2)
2136         "fixedbugs/issue17645.go",  // multiple errors on same line
2137         "fixedbugs/issue18331.go",  // missing error about misuse of //go:noescape (irgen needs code from noder)
2138         "fixedbugs/issue18419.go",  // types2 reports
2139         "fixedbugs/issue19012.go",  // multiple errors on same line
2140         "fixedbugs/issue20233.go",  // types2 reports two instead of one error (pref: compiler)
2141         "fixedbugs/issue20245.go",  // types2 reports two instead of one error (pref: compiler)
2142         "fixedbugs/issue21979.go",  // types2 doesn't report a follow-on error (pref: types2)
2143         "fixedbugs/issue23732.go",  // types2 reports different (but ok) line numbers
2144         "fixedbugs/issue25958.go",  // types2 doesn't report a follow-on error (pref: types2)
2145         "fixedbugs/issue28079b.go", // types2 reports follow-on errors
2146         "fixedbugs/issue28268.go",  // types2 reports follow-on errors
2147         "fixedbugs/issue31053.go",  // types2 reports "unknown field" instead of "cannot refer to unexported field"
2148         "fixedbugs/issue33460.go",  // types2 reports alternative positions in separate error
2149         "fixedbugs/issue42058a.go", // types2 doesn't report "channel element type too large"
2150         "fixedbugs/issue42058b.go", // types2 doesn't report "channel element type too large"
2151         "fixedbugs/issue4232.go",   // types2 reports (correct) extra errors
2152         "fixedbugs/issue4452.go",   // types2 reports (correct) extra errors
2153         "fixedbugs/issue4510.go",   // types2 reports different (but ok) line numbers
2154         "fixedbugs/issue47201.go",  // types2 spells the error message differently
2155         "fixedbugs/issue5609.go",   // types2 needs a better error message
2156         "fixedbugs/issue7525b.go",  // types2 reports init cycle error on different line - ok otherwise
2157         "fixedbugs/issue7525c.go",  // types2 reports init cycle error on different line - ok otherwise
2158         "fixedbugs/issue7525d.go",  // types2 reports init cycle error on different line - ok otherwise
2159         "fixedbugs/issue7525e.go",  // types2 reports init cycle error on different line - ok otherwise
2160         "fixedbugs/issue7525.go",   // types2 reports init cycle error on different line - ok otherwise
2161 )
2162
2163 var types2Failures32Bit = setOf(
2164         "printbig.go",             // large untyped int passed to print (32-bit)
2165         "fixedbugs/bug114.go",     // large untyped int passed to println (32-bit)
2166         "fixedbugs/issue23305.go", // large untyped int passed to println (32-bit)
2167         "fixedbugs/bug385_32.go",  // types2 doesn't produce missing error "type .* too large" (32-bit specific)
2168 )
2169
2170 var g3Failures = setOf(
2171         "writebarrier.go", // correct diagnostics, but different lines (probably irgen's fault)
2172
2173         "typeparam/nested.go", // -G=3 doesn't support function-local types with generics
2174
2175         "typeparam/mdempsky/4.go", // -G=3 can't export functions with labeled breaks in loops
2176 )
2177
2178 var unifiedFailures = setOf(
2179         "closure3.go", // unified IR numbers closures differently than -d=inlfuncswithclosures
2180         "escape4.go",  // unified IR can inline f5 and f6; test doesn't expect this
2181         "inline.go",   // unified IR reports function literal diagnostics on different lines than -d=inlfuncswithclosures
2182
2183         "fixedbugs/issue42284.go", // prints "T(0) does not escape", but test expects "a.I(a.T(0)) does not escape"
2184         "fixedbugs/issue7921.go",  // prints "… escapes to heap", but test expects "string(…) escapes to heap"
2185 )
2186
2187 func setOf(keys ...string) map[string]bool {
2188         m := make(map[string]bool, len(keys))
2189         for _, key := range keys {
2190                 m[key] = true
2191         }
2192         return m
2193 }
2194
2195 // splitQuoted splits the string s around each instance of one or more consecutive
2196 // white space characters while taking into account quotes and escaping, and
2197 // returns an array of substrings of s or an empty list if s contains only white space.
2198 // Single quotes and double quotes are recognized to prevent splitting within the
2199 // quoted region, and are removed from the resulting substrings. If a quote in s
2200 // isn't closed err will be set and r will have the unclosed argument as the
2201 // last element. The backslash is used for escaping.
2202 //
2203 // For example, the following string:
2204 //
2205 //     a b:"c d" 'e''f'  "g\""
2206 //
2207 // Would be parsed as:
2208 //
2209 //     []string{"a", "b:c d", "ef", `g"`}
2210 //
2211 // [copied from src/go/build/build.go]
2212 func splitQuoted(s string) (r []string, err error) {
2213         var args []string
2214         arg := make([]rune, len(s))
2215         escaped := false
2216         quoted := false
2217         quote := '\x00'
2218         i := 0
2219         for _, rune := range s {
2220                 switch {
2221                 case escaped:
2222                         escaped = false
2223                 case rune == '\\':
2224                         escaped = true
2225                         continue
2226                 case quote != '\x00':
2227                         if rune == quote {
2228                                 quote = '\x00'
2229                                 continue
2230                         }
2231                 case rune == '"' || rune == '\'':
2232                         quoted = true
2233                         quote = rune
2234                         continue
2235                 case unicode.IsSpace(rune):
2236                         if quoted || i > 0 {
2237                                 quoted = false
2238                                 args = append(args, string(arg[:i]))
2239                                 i = 0
2240                         }
2241                         continue
2242                 }
2243                 arg[i] = rune
2244                 i++
2245         }
2246         if quoted || i > 0 {
2247                 args = append(args, string(arg[:i]))
2248         }
2249         if quote != 0 {
2250                 err = errors.New("unclosed quote")
2251         } else if escaped {
2252                 err = errors.New("unfinished escaping")
2253         }
2254         return args, err
2255 }