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