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.
7 // Run runs tests in the test directory.
35 verbose = flag.Bool("v", false, "verbose. if set, parallelism is set to 1.")
36 keep = flag.Bool("k", false, "keep. keep temporary directory.")
37 numParallel = flag.Int("n", runtime.NumCPU(), "number of parallel tests to run")
38 summary = flag.Bool("summary", false, "show summary of results")
39 allCodegen = flag.Bool("all_codegen", defaultAllCodeGen(), "run all goos/goarch for codegen")
40 showSkips = flag.Bool("show_skips", false, "show skipped tests")
41 runSkips = flag.Bool("run_skips", false, "run skipped tests (ignore skip and build tags)")
42 linkshared = flag.Bool("linkshared", false, "")
43 updateErrors = flag.Bool("update_errors", false, "update error messages in test file based on compiler output")
44 runoutputLimit = flag.Int("l", defaultRunOutputLimit(), "number of parallel runoutput tests to run")
45 force = flag.Bool("f", false, "ignore expected-failure test lists")
46 generics = flag.String("G", defaultGLevels, "a comma-separated list of -G compiler flags to test with")
48 shard = flag.Int("shard", 0, "shard index to run. Only applicable if -shards is non-zero.")
49 shards = flag.Int("shards", 0, "number of shards. If 0, all tests are run. This is used by the continuous build.")
52 var unifiedEnabled, defaultGLevels = func() (bool, string) {
53 // TODO(mdempsky): Change this to just "go env GOEXPERIMENT" after
54 // CL 328751 is merged back to dev.typeparams. In the mean time, we
55 // infer whether the "unified" experiment is default enabled by
56 // inspecting the output from `go tool compile -V`.
57 output := runOutput(goTool(), "tool", "compile", "-V")
59 // TODO(mdempsky): This will give false negatives if the unified
60 // experiment is enabled by default, but presumably at that point we
61 // won't need to disable tests for it anymore anyway.
62 enabled := strings.Contains(output, "unified")
64 // Normal test runs should test with both -G=0 and -G=3 for types2
65 // coverage. But the unified experiment always uses types2, so
66 // testing with -G=3 is redundant.
72 return enabled, glevels
75 // defaultAllCodeGen returns the default value of the -all_codegen
76 // flag. By default, we prefer to be fast (returning false), except on
77 // the linux-amd64 builder that's already very fast, so we get more
78 // test coverage on trybots. See https://golang.org/issue/34297.
79 func defaultAllCodeGen() bool {
80 return os.Getenv("GO_BUILDER_NAME") == "linux-amd64"
87 // dirs are the directories to look for *.go files in.
88 // TODO(bradfitz): just use all directories?
89 dirs = []string{".", "ken", "chan", "interface", "syntax", "dwarf", "fixedbugs", "codegen", "runtime", "abi", "typeparam", "typeparam/mdempsky"}
91 // ratec controls the max number of tests running at a time.
94 // toRun is the channel of tests to run.
95 // It is nil until the first test is started.
98 // rungatec controls the max number of runoutput tests
99 // executed in parallel as they can each consume a lot of memory.
103 // maxTests is an upper bound on the total number of tests.
104 // It is used as a channel buffer size to make sure sends don't block.
105 const maxTests = 5000
111 for _, s := range strings.Split(*generics, ",") {
112 glevel, err := strconv.Atoi(s)
114 log.Fatalf("invalid -G flag: %v", err)
116 glevels = append(glevels, glevel)
119 goos = getenv("GOOS", runtime.GOOS)
120 goarch = getenv("GOARCH", runtime.GOARCH)
122 cgoEnv := runOutput(goTool(), "env", "CGO_ENABLED")
123 cgoEnabled, _ = strconv.ParseBool(strings.TrimSpace(cgoEnv))
127 // Disable parallelism if printing or if using a simulator.
128 if *verbose || len(findExecCmd()) > 0 {
133 ratec = make(chan bool, *numParallel)
134 rungatec = make(chan bool, *runoutputLimit)
138 for _, arg := range flag.Args() {
139 if arg == "-" || arg == "--" {
141 // $ go run run.go - env.go
142 // $ go run run.go -- env.go
143 // $ go run run.go - ./fixedbugs
144 // $ go run run.go -- ./fixedbugs
147 if fi, err := os.Stat(arg); err == nil && fi.IsDir() {
148 for _, baseGoFile := range goFiles(arg) {
149 tests = append(tests, startTests(arg, baseGoFile, glevels)...)
151 } else if strings.HasSuffix(arg, ".go") {
152 dir, file := filepath.Split(arg)
153 tests = append(tests, startTests(dir, file, glevels)...)
155 log.Fatalf("can't yet deal with non-directory and non-go file %q", arg)
159 for _, dir := range dirs {
160 for _, baseGoFile := range goFiles(dir) {
161 tests = append(tests, startTests(dir, baseGoFile, glevels)...)
167 resCount := map[string]int{}
168 for _, test := range tests {
172 if e, isSkip := test.err.(skipError); isSkip {
174 errStr = "unexpected skip for " + path.Join(test.dir, test.gofile) + ": " + string(e)
178 errStr = test.err.Error()
180 errStr += " (expected)"
184 } else if test.expectFail {
186 errStr = "unexpected success"
188 if status == "FAIL" {
192 dt := fmt.Sprintf("%.3fs", test.dt.Seconds())
193 if status == "FAIL" {
194 fmt.Printf("# go run run.go -G=%v %s\n%s\nFAIL\t%s\t%s\n",
196 path.Join(test.dir, test.gofile),
197 errStr, test.goFileName(), dt)
203 fmt.Printf("%s\t%s\t%s\n", status, test.goFileName(), dt)
207 for k, v := range resCount {
208 fmt.Printf("%5d %s\n", v, k)
217 // runOutput runs the specified command and returns its output as a
218 // string. If the command fails, runOutput logs the error and exits.
219 func runOutput(name string, args ...string) string {
220 cmd := exec.Command(name, args...)
221 output, err := cmd.Output()
223 log.Fatalf("running %v: %v", cmd, err)
225 return string(output)
228 // goTool reports the path of the go tool to use to run the tests.
229 // If possible, use the same Go used to run run.go, otherwise
230 // fallback to the go version found in the PATH.
231 func goTool() string {
233 if runtime.GOOS == "windows" {
236 path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
237 if _, err := os.Stat(path); err == nil {
240 // Just run "go" from PATH
244 func shardMatch(name string) bool {
249 io.WriteString(h, name)
250 return int(h.Sum32()%uint32(*shards)) == *shard
253 func goFiles(dir string) []string {
254 f, err := os.Open(dir)
258 dirnames, err := f.Readdirnames(-1)
264 for _, name := range dirnames {
265 if !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && shardMatch(name) {
266 names = append(names, name)
273 type runCmd func(...string) ([]byte, error)
275 func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, err error) {
276 cmd := []string{goTool(), "tool", "compile", "-e"}
277 cmd = append(cmd, flags...)
279 cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
281 cmd = append(cmd, longname)
282 return runcmd(cmd...)
285 func compileInDir(runcmd runCmd, dir string, flags []string, localImports bool, names ...string) (out []byte, err error) {
286 cmd := []string{goTool(), "tool", "compile", "-e"}
288 // Set relative path for local imports and import search path to current dir.
289 cmd = append(cmd, "-D", ".", "-I", ".")
291 cmd = append(cmd, flags...)
293 cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
295 for _, name := range names {
296 cmd = append(cmd, filepath.Join(dir, name))
298 return runcmd(cmd...)
301 func linkFile(runcmd runCmd, goname string, ldflags []string) (err error) {
302 pfile := strings.Replace(goname, ".go", ".o", -1)
303 cmd := []string{goTool(), "tool", "link", "-w", "-o", "a.exe", "-L", "."}
305 cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
308 cmd = append(cmd, ldflags...)
310 cmd = append(cmd, pfile)
311 _, err = runcmd(cmd...)
315 // skipError describes why a test was skipped.
316 type skipError string
318 func (s skipError) Error() string { return string(s) }
320 // test holds the state of a test.
323 donec chan bool // closed when done
325 glevel int // what -G level this test should use
332 // expectFail indicates whether the (overall) test recipe is
333 // expected to fail under the current test configuration (e.g., -G=3
334 // or GOEXPERIMENT=unified).
338 // initExpectFail initializes t.expectFail based on the build+test
339 // configuration. It should only be called for tests known to use
341 func (t *test) initExpectFail() {
346 failureSets := []map[string]bool{types2Failures}
348 // Note: gccgo supports more 32-bit architectures than this, but
349 // hopefully the 32-bit failures are fixed before this matters.
351 case "386", "arm", "mips", "mipsle":
352 failureSets = append(failureSets, types2Failures32Bit)
356 failureSets = append(failureSets, unifiedFailures)
358 failureSets = append(failureSets, g3Failures)
361 filename := strings.Replace(t.goFileName(), "\\", "/", -1) // goFileName() uses \ on Windows
363 for _, set := range failureSets {
371 func startTests(dir, gofile string, glevels []int) []*test {
372 tests := make([]*test, len(glevels))
373 for i, glevel := range glevels {
378 donec: make(chan bool, 1),
381 toRun = make(chan *test, maxTests)
387 panic("toRun buffer size (maxTests) is too small")
394 // runTests runs tests in parallel, but respecting the order they
395 // were enqueued on the toRun channel.
407 var cwd, _ = os.Getwd()
409 func (t *test) goFileName() string {
410 return filepath.Join(t.dir, t.gofile)
413 func (t *test) goDirName() string {
414 return filepath.Join(t.dir, strings.Replace(t.gofile, ".go", ".dir", -1))
417 func goDirFiles(longdir string) (filter []os.FileInfo, err error) {
418 files, dirErr := ioutil.ReadDir(longdir)
422 for _, gofile := range files {
423 if filepath.Ext(gofile.Name()) == ".go" {
424 filter = append(filter, gofile)
430 var packageRE = regexp.MustCompile(`(?m)^package ([\p{Lu}\p{Ll}\w]+)`)
432 func getPackageNameFromSource(fn string) (string, error) {
433 data, err := ioutil.ReadFile(fn)
437 pkgname := packageRE.FindStringSubmatch(string(data))
439 return "", fmt.Errorf("cannot find package name in %s", fn)
441 return pkgname[1], nil
444 // If singlefilepkgs is set, each file is considered a separate package
445 // even if the package names are the same.
446 func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
447 files, err := goDirFiles(longdir)
452 m := make(map[string]int)
453 for _, file := range files {
455 pkgname, err := getPackageNameFromSource(filepath.Join(longdir, name))
460 if singlefilepkgs || !ok {
462 pkgs = append(pkgs, nil)
465 pkgs[i] = append(pkgs[i], name)
470 type context struct {
477 // shouldTest looks for build tags in a source file and returns
478 // whether the file should be used according to the tags.
479 func shouldTest(src string, goos, goarch string) (ok bool, whyNot string) {
483 for _, line := range strings.Split(src, "\n") {
484 line = strings.TrimSpace(line)
485 if strings.HasPrefix(line, "//") {
490 line = strings.TrimSpace(line)
491 if len(line) == 0 || line[0] != '+' {
494 gcFlags := os.Getenv("GO_GCFLAGS")
498 cgoEnabled: cgoEnabled,
499 noOptEnv: strings.Contains(gcFlags, "-N") || strings.Contains(gcFlags, "-l"),
502 words := strings.Fields(line)
503 if words[0] == "+build" {
505 for _, word := range words[1:] {
506 if ctxt.match(word) {
512 // no matching tag found.
521 func (ctxt *context) match(name string) bool {
525 if i := strings.Index(name, ","); i >= 0 {
526 // comma-separated list
527 return ctxt.match(name[:i]) && ctxt.match(name[i+1:])
529 if strings.HasPrefix(name, "!!") { // bad syntax, reject always
532 if strings.HasPrefix(name, "!") { // negation
533 return len(name) > 1 && !ctxt.match(name[1:])
536 // Tags must be letters, digits, underscores or dots.
537 // Unlike in Go identifiers, all digits are fine (e.g., "386").
538 for _, c := range name {
539 if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
544 if strings.HasPrefix(name, "goexperiment.") {
545 for _, tag := range build.Default.ToolTags {
553 if name == "cgo" && ctxt.cgoEnabled {
557 if name == ctxt.GOOS || name == ctxt.GOARCH || name == "gc" {
561 if ctxt.noOptEnv && name == "gcflags_noopt" {
565 if name == "test_run" {
572 func init() { checkShouldTest() }
574 // goGcflags returns the -gcflags argument to use with go build / go run.
575 // This must match the flags used for building the standard library,
576 // or else the commands will rebuild any needed packages (like runtime)
578 func (t *test) goGcflags() string {
579 flags := os.Getenv("GO_GCFLAGS")
581 flags = fmt.Sprintf("%s -G=%v", flags, t.glevel)
583 return "-gcflags=all=" + flags
586 func (t *test) goGcflagsIsEmpty() bool {
587 return "" == os.Getenv("GO_GCFLAGS") && t.glevel == 0
590 var errTimeout = errors.New("command exceeded time limit")
593 func (t *test) run() {
596 t.dt = time.Since(start)
600 srcBytes, err := ioutil.ReadFile(t.goFileName())
605 t.src = string(srcBytes)
606 if t.src[0] == '\n' {
607 t.err = skipError("starts with newline")
611 // Execution recipe stops at first blank line.
612 pos := strings.Index(t.src, "\n\n")
614 t.err = fmt.Errorf("double newline ending execution recipe not found in %s", t.goFileName())
617 action := t.src[:pos]
618 if nl := strings.Index(action, "\n"); nl >= 0 && strings.Contains(action[:nl], "+build") {
620 action = action[nl+1:]
622 action = strings.TrimPrefix(action, "//")
624 // Check for build constraints only up to the actual code.
625 pkgPos := strings.Index(t.src, "\npackage")
627 pkgPos = pos // some files are intentionally malformed
629 if ok, why := shouldTest(t.src[:pkgPos], goos, goarch); !ok {
631 fmt.Printf("%-20s %-20s: %s\n", "skip", t.goFileName(), why)
636 var args, flags, runenv []string
640 singlefilepkgs := false
643 f, err := splitQuoted(action)
645 t.err = fmt.Errorf("invalid test recipe: %v", err)
653 // TODO: Clean up/simplify this switch statement.
655 case "compile", "compiledir", "build", "builddir", "buildrundir", "run", "buildrun", "runoutput", "rundir", "runindir", "asmcheck":
657 case "errorcheckandrundir":
658 wantError = false // should be no error if also will run
659 case "errorcheckwithauto":
660 action = "errorcheck"
663 case "errorcheck", "errorcheckdir", "errorcheckoutput":
671 t.err = skipError("skipped; unknown pattern: " + action)
676 for len(args) > 0 && strings.HasPrefix(args[0], "-") {
683 singlefilepkgs = true
687 // Do not set relative path for local imports to current dir,
688 // e.g. do not pass -D . -I . to the compiler.
689 // Used in fixedbugs/bug345.go to allow compilation and import of local pkg.
690 // See golang.org/issue/25635
692 case "-t": // timeout in seconds
695 tim, err = strconv.Atoi(args[0])
697 t.err = fmt.Errorf("need number of seconds for -t timeout, got %s instead", args[0])
699 case "-goexperiment": // set GOEXPERIMENT environment
701 runenv = append(runenv, "GOEXPERIMENT="+args[0])
704 flags = append(flags, args[0])
708 if action == "errorcheck" {
710 for i, f := range flags {
711 if strings.HasPrefix(f, "-d=") {
712 flags[i] = f + ",ssa/check/on"
718 flags = append(flags, "-d=ssa/check/on")
732 // validForGLevel reports whether the current test is valid to run
733 // at the specified -G level. If so, it may update flags as
734 // necessary to test with -G.
735 validForGLevel := func(tool Tool) bool {
737 for _, flag := range flags {
738 if strings.Contains(flag, "-G") {
743 if hasGFlag && t.glevel != 0 {
744 // test provides explicit -G flag already; don't run again
746 fmt.Printf("excl\t%s\n", t.goFileName())
751 if t.glevel == 0 && !hasGFlag && !unifiedEnabled {
752 // tests should always pass when run w/o types2 (i.e., using the
753 // legacy typechecker).
761 // ok; handled in goGcflags
765 flags = append(flags, fmt.Sprintf("-G=%v", t.glevel))
769 // we don't know how to add -G for this test yet
771 fmt.Printf("excl\t%s\n", t.goFileName())
781 defer os.RemoveAll(t.tempDir)
784 err = ioutil.WriteFile(filepath.Join(t.tempDir, t.gofile), srcBytes, 0644)
789 // A few tests (of things like the environment) require these to be set.
790 if os.Getenv("GOOS") == "" {
791 os.Setenv("GOOS", runtime.GOOS)
793 if os.Getenv("GOARCH") == "" {
794 os.Setenv("GOARCH", runtime.GOARCH)
799 tempDirIsGOPATH = false
801 runcmd := func(args ...string) ([]byte, error) {
802 cmd := exec.Command(args[0], args[1:]...)
806 cmd.Env = append(os.Environ(), "GOENV=off", "GOFLAGS=")
809 // Set PWD to match Dir to speed up os.Getwd in the child process.
810 cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
813 cmd.Env = append(cmd.Env, "GOPATH="+t.tempDir)
815 cmd.Env = append(cmd.Env, runenv...)
821 // This command-timeout code adapted from cmd/go/test.go
823 tick := time.NewTimer(time.Duration(tim) * time.Second)
824 done := make(chan error)
832 cmd.Process.Signal(os.Interrupt)
833 time.Sleep(1 * time.Second)
843 if err != nil && err != errTimeout {
844 err = fmt.Errorf("%s\n%s", err, buf.Bytes())
846 return buf.Bytes(), err
849 long := filepath.Join(cwd, t.goFileName())
852 t.err = fmt.Errorf("unimplemented action %q", action)
855 if !validForGLevel(AsmCheck) {
859 // Compile Go file and match the generated assembly
860 // against a set of regexps in comments.
861 ops := t.wantedAsmOpcodes(long)
862 self := runtime.GOOS + "/" + runtime.GOARCH
863 for _, env := range ops.Envs() {
864 // Only run checks relevant to the current GOOS/GOARCH,
865 // to avoid triggering a cross-compile of the runtime.
866 if string(env) != self && !strings.HasPrefix(string(env), self+"/") && !*allCodegen {
869 // -S=2 forces outermost line numbers when disassembling inlined code.
870 cmdline := []string{"build", "-gcflags", "-S=2"}
872 // Append flags, but don't override -gcflags=-S=2; add to it instead.
873 for i := 0; i < len(flags); i++ {
876 case strings.HasPrefix(flag, "-gcflags="):
877 cmdline[2] += " " + strings.TrimPrefix(flag, "-gcflags=")
878 case strings.HasPrefix(flag, "--gcflags="):
879 cmdline[2] += " " + strings.TrimPrefix(flag, "--gcflags=")
880 case flag == "-gcflags", flag == "--gcflags":
883 cmdline[2] += " " + flags[i]
886 cmdline = append(cmdline, flag)
890 cmdline = append(cmdline, long)
891 cmd := exec.Command(goTool(), cmdline...)
892 cmd.Env = append(os.Environ(), env.Environ()...)
893 if len(flags) > 0 && flags[0] == "-race" {
894 cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
898 cmd.Stdout, cmd.Stderr = &buf, &buf
899 if err := cmd.Run(); err != nil {
900 fmt.Println(env, "\n", cmd.Stderr)
905 t.err = t.asmCheck(buf.String(), long, env, ops[env])
913 if !validForGLevel(Compile) {
918 // Fail if wantError is true and compilation was successful and vice versa.
919 // Match errors produced by gc against errors in comments.
920 // TODO(gri) remove need for -C (disable printing of columns in error messages)
921 cmdline := []string{goTool(), "tool", "compile", "-d=panic", "-C", "-e", "-o", "a.o"}
922 // No need to add -dynlink even if linkshared if we're just checking for errors...
923 cmdline = append(cmdline, flags...)
924 cmdline = append(cmdline, long)
925 out, err := runcmd(cmdline...)
928 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
931 if err == errTimeout {
932 t.err = fmt.Errorf("compilation timed out")
942 t.updateErrors(string(out), long)
944 t.err = t.errorCheck(string(out), wantAuto, long, t.gofile)
947 if !validForGLevel(Compile) {
952 _, t.err = compileFile(runcmd, long, flags)
955 if !validForGLevel(Compile) {
959 // Compile all files in the directory as packages in lexicographic order.
960 longdir := filepath.Join(cwd, t.goDirName())
961 pkgs, err := goDirPackages(longdir, singlefilepkgs)
966 for _, gofiles := range pkgs {
967 _, t.err = compileInDir(runcmd, longdir, flags, localImports, gofiles...)
973 case "errorcheckdir", "errorcheckandrundir":
974 if !validForGLevel(Compile) {
978 flags = append(flags, "-d=panic")
979 // Compile and errorCheck all files in the directory as packages in lexicographic order.
980 // If errorcheckdir and wantError, compilation of the last package must fail.
981 // If errorcheckandrundir and wantError, compilation of the package prior the last must fail.
982 longdir := filepath.Join(cwd, t.goDirName())
983 pkgs, err := goDirPackages(longdir, singlefilepkgs)
988 errPkg := len(pkgs) - 1
989 if wantError && action == "errorcheckandrundir" {
990 // The last pkg should compiled successfully and will be run in next case.
991 // Preceding pkg must return an error from compileInDir.
994 for i, gofiles := range pkgs {
995 out, err := compileInDir(runcmd, longdir, flags, localImports, gofiles...)
997 if wantError && err == nil {
998 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
1000 } else if !wantError && err != nil {
1004 } else if err != nil {
1008 var fullshort []string
1009 for _, name := range gofiles {
1010 fullshort = append(fullshort, filepath.Join(longdir, name), name)
1012 t.err = t.errorCheck(string(out), wantAuto, fullshort...)
1017 if action == "errorcheckdir" {
1023 if !validForGLevel(Run) {
1027 // Compile all files in the directory as packages in lexicographic order.
1028 // In case of errorcheckandrundir, ignore failed compilation of the package before the last.
1029 // Link as if the last file is the main package, run it.
1030 // Verify the expected output.
1031 longdir := filepath.Join(cwd, t.goDirName())
1032 pkgs, err := goDirPackages(longdir, singlefilepkgs)
1037 // Split flags into gcflags and ldflags
1038 ldflags := []string{}
1039 for i, fl := range flags {
1040 if fl == "-ldflags" {
1041 ldflags = flags[i+1:]
1047 for i, gofiles := range pkgs {
1048 pflags := []string{}
1049 pflags = append(pflags, flags...)
1051 fp := filepath.Join(longdir, gofiles[0])
1052 pkgname, err := getPackageNameFromSource(fp)
1056 pflags = append(pflags, "-p", pkgname)
1058 _, err := compileInDir(runcmd, longdir, pflags, localImports, gofiles...)
1059 // Allow this package compilation fail based on conditions below;
1060 // its errors were checked in previous case.
1061 if err != nil && !(wantError && action == "errorcheckandrundir" && i == len(pkgs)-2) {
1065 if i == len(pkgs)-1 {
1066 err = linkFile(runcmd, gofiles[0], ldflags)
1072 cmd = append(cmd, findExecCmd()...)
1073 cmd = append(cmd, filepath.Join(t.tempDir, "a.exe"))
1074 cmd = append(cmd, args...)
1075 out, err := runcmd(cmd...)
1080 t.checkExpectedOutput(out)
1085 if !validForGLevel(Run) {
1089 // Make a shallow copy of t.goDirName() in its own module and GOPATH, and
1090 // run "go run ." in it. The module path (and hence import path prefix) of
1091 // the copy is equal to the basename of the source directory.
1093 // It's used when test a requires a full 'go build' in order to compile
1094 // the sources, such as when importing multiple packages (issue29612.dir)
1095 // or compiling a package containing assembly files (see issue15609.dir),
1096 // but still needs to be run to verify the expected output.
1097 tempDirIsGOPATH = true
1098 srcDir := t.goDirName()
1099 modName := filepath.Base(srcDir)
1100 gopathSrcDir := filepath.Join(t.tempDir, "src", modName)
1101 runInDir = gopathSrcDir
1103 if err := overlayDir(gopathSrcDir, srcDir); err != nil {
1108 modFile := fmt.Sprintf("module %s\ngo 1.14\n", modName)
1109 if err := ioutil.WriteFile(filepath.Join(gopathSrcDir, "go.mod"), []byte(modFile), 0666); err != nil {
1114 cmd := []string{goTool(), "run", t.goGcflags()}
1116 cmd = append(cmd, "-linkshared")
1118 cmd = append(cmd, flags...)
1119 cmd = append(cmd, ".")
1120 out, err := runcmd(cmd...)
1125 t.checkExpectedOutput(out)
1128 if !validForGLevel(Build) {
1133 _, err := runcmd(goTool(), "build", t.goGcflags(), "-o", "a.exe", long)
1138 case "builddir", "buildrundir":
1139 if !validForGLevel(Build) {
1143 // Build an executable from all the .go and .s files in a subdirectory.
1144 // Run it and verify its output in the buildrundir case.
1145 longdir := filepath.Join(cwd, t.goDirName())
1146 files, dirErr := ioutil.ReadDir(longdir)
1153 for _, file := range files {
1154 switch filepath.Ext(file.Name()) {
1156 gos = append(gos, filepath.Join(longdir, file.Name()))
1158 asms = append(asms, filepath.Join(longdir, file.Name()))
1163 emptyHdrFile := filepath.Join(t.tempDir, "go_asm.h")
1164 if err := ioutil.WriteFile(emptyHdrFile, nil, 0666); err != nil {
1165 t.err = fmt.Errorf("write empty go_asm.h: %s", err)
1168 cmd := []string{goTool(), "tool", "asm", "-gensymabis", "-o", "symabis"}
1169 cmd = append(cmd, asms...)
1170 _, err = runcmd(cmd...)
1177 cmd := []string{goTool(), "tool", "compile", "-e", "-D", ".", "-I", ".", "-o", "go.o"}
1179 cmd = append(cmd, "-asmhdr", "go_asm.h", "-symabis", "symabis")
1181 cmd = append(cmd, gos...)
1182 _, err := runcmd(cmd...)
1187 objs = append(objs, "go.o")
1189 cmd = []string{goTool(), "tool", "asm", "-e", "-I", ".", "-o", "asm.o"}
1190 cmd = append(cmd, asms...)
1191 _, err = runcmd(cmd...)
1196 objs = append(objs, "asm.o")
1198 cmd = []string{goTool(), "tool", "pack", "c", "all.a"}
1199 cmd = append(cmd, objs...)
1200 _, err = runcmd(cmd...)
1205 cmd = []string{goTool(), "tool", "link", "-o", "a.exe", "all.a"}
1206 _, err = runcmd(cmd...)
1211 if action == "buildrundir" {
1212 cmd = append(findExecCmd(), filepath.Join(t.tempDir, "a.exe"))
1213 out, err := runcmd(cmd...)
1218 t.checkExpectedOutput(out)
1222 if !validForGLevel(Build) {
1226 // Build an executable from Go file, then run it, verify its output.
1227 // Useful for timeout tests where failure mode is infinite loop.
1228 // TODO: not supported on NaCl
1229 cmd := []string{goTool(), "build", t.goGcflags(), "-o", "a.exe"}
1231 cmd = append(cmd, "-linkshared")
1233 longdirgofile := filepath.Join(filepath.Join(cwd, t.dir), t.gofile)
1234 cmd = append(cmd, flags...)
1235 cmd = append(cmd, longdirgofile)
1236 _, err := runcmd(cmd...)
1241 cmd = []string{"./a.exe"}
1242 out, err := runcmd(append(cmd, args...)...)
1248 t.checkExpectedOutput(out)
1251 if !validForGLevel(Run) {
1255 // Run Go file if no special go command flags are provided;
1256 // otherwise build an executable and run it.
1257 // Verify the output.
1261 if len(flags)+len(args) == 0 && t.goGcflagsIsEmpty() && !*linkshared && goarch == runtime.GOARCH && goos == runtime.GOOS {
1262 // If we're not using special go command flags,
1263 // skip all the go command machinery.
1264 // This avoids any time the go command would
1265 // spend checking whether, for example, the installed
1266 // package runtime is up to date.
1267 // Because we run lots of trivial test programs,
1268 // the time adds up.
1269 pkg := filepath.Join(t.tempDir, "pkg.a")
1270 if _, err := runcmd(goTool(), "tool", "compile", "-o", pkg, t.goFileName()); err != nil {
1274 exe := filepath.Join(t.tempDir, "test.exe")
1275 cmd := []string{goTool(), "tool", "link", "-s", "-w"}
1276 cmd = append(cmd, "-o", exe, pkg)
1277 if _, err := runcmd(cmd...); err != nil {
1281 out, err = runcmd(append([]string{exe}, args...)...)
1283 cmd := []string{goTool(), "run", t.goGcflags()}
1285 cmd = append(cmd, "-linkshared")
1287 cmd = append(cmd, flags...)
1288 cmd = append(cmd, t.goFileName())
1289 out, err = runcmd(append(cmd, args...)...)
1295 t.checkExpectedOutput(out)
1298 if !validForGLevel(Run) {
1302 // Run Go file and write its output into temporary Go file.
1303 // Run generated Go file and verify its output.
1309 cmd := []string{goTool(), "run", t.goGcflags()}
1311 cmd = append(cmd, "-linkshared")
1313 cmd = append(cmd, t.goFileName())
1314 out, err := runcmd(append(cmd, args...)...)
1319 tfile := filepath.Join(t.tempDir, "tmp__.go")
1320 if err := ioutil.WriteFile(tfile, out, 0666); err != nil {
1321 t.err = fmt.Errorf("write tempfile:%s", err)
1324 cmd = []string{goTool(), "run", t.goGcflags()}
1326 cmd = append(cmd, "-linkshared")
1328 cmd = append(cmd, tfile)
1329 out, err = runcmd(cmd...)
1334 t.checkExpectedOutput(out)
1336 case "errorcheckoutput":
1337 if !validForGLevel(Compile) {
1341 // Run Go file and write its output into temporary Go file.
1342 // Compile and errorCheck generated Go file.
1344 cmd := []string{goTool(), "run", t.goGcflags()}
1346 cmd = append(cmd, "-linkshared")
1348 cmd = append(cmd, t.goFileName())
1349 out, err := runcmd(append(cmd, args...)...)
1354 tfile := filepath.Join(t.tempDir, "tmp__.go")
1355 err = ioutil.WriteFile(tfile, out, 0666)
1357 t.err = fmt.Errorf("write tempfile:%s", err)
1360 cmdline := []string{goTool(), "tool", "compile", "-d=panic", "-e", "-o", "a.o"}
1361 cmdline = append(cmdline, flags...)
1362 cmdline = append(cmdline, tfile)
1363 out, err = runcmd(cmdline...)
1366 t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
1375 t.err = t.errorCheck(string(out), false, tfile, "tmp__.go")
1380 var execCmd []string
1382 func findExecCmd() []string {
1386 execCmd = []string{} // avoid work the second time
1387 if goos == runtime.GOOS && goarch == runtime.GOARCH {
1390 path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch))
1392 execCmd = []string{path}
1397 func (t *test) String() string {
1398 return filepath.Join(t.dir, t.gofile)
1401 func (t *test) makeTempDir() {
1403 t.tempDir, err = ioutil.TempDir("", "")
1408 log.Printf("Temporary directory is %s", t.tempDir)
1412 // checkExpectedOutput compares the output from compiling and/or running with the contents
1413 // of the corresponding reference output file, if any (replace ".go" with ".out").
1414 // If they don't match, fail with an informative message.
1415 func (t *test) checkExpectedOutput(gotBytes []byte) {
1416 got := string(gotBytes)
1417 filename := filepath.Join(t.dir, t.gofile)
1418 filename = filename[:len(filename)-len(".go")]
1420 b, err := ioutil.ReadFile(filename)
1421 // File is allowed to be missing (err != nil) in which case output should be empty.
1422 got = strings.Replace(got, "\r\n", "\n", -1)
1423 if got != string(b) {
1425 t.err = fmt.Errorf("output does not match expected in %s. Instead saw\n%s", filename, got)
1427 t.err = fmt.Errorf("output should be empty when (optional) expected-output file %s is not present. Instead saw\n%s", filename, got)
1432 func splitOutput(out string, wantAuto bool) []string {
1433 // gc error messages continue onto additional lines with leading tabs.
1434 // Split the output at the beginning of each line that doesn't begin with a tab.
1435 // <autogenerated> lines are impossible to match so those are filtered out.
1437 for _, line := range strings.Split(out, "\n") {
1438 if strings.HasSuffix(line, "\r") { // remove '\r', output by compiler on windows
1439 line = line[:len(line)-1]
1441 if strings.HasPrefix(line, "\t") {
1442 res[len(res)-1] += "\n" + line
1443 } else if strings.HasPrefix(line, "go tool") || strings.HasPrefix(line, "#") || !wantAuto && strings.HasPrefix(line, "<autogenerated>") {
1445 } else if strings.TrimSpace(line) != "" {
1446 res = append(res, line)
1452 // errorCheck matches errors in outStr against comments in source files.
1453 // For each line of the source files which should generate an error,
1454 // there should be a comment of the form // ERROR "regexp".
1455 // If outStr has an error for a line which has no such comment,
1456 // this function will report an error.
1457 // Likewise if outStr does not have an error for a line which has a comment,
1458 // or if the error message does not match the <regexp>.
1459 // The <regexp> syntax is Perl but it's best to stick to egrep.
1461 // Sources files are supplied as fullshort slice.
1462 // It consists of pairs: full path to source file and its base name.
1463 func (t *test) errorCheck(outStr string, wantAuto bool, fullshort ...string) (err error) {
1465 if *verbose && err != nil {
1466 log.Printf("%s gc output:\n%s", t, outStr)
1470 out := splitOutput(outStr, wantAuto)
1472 // Cut directory name.
1473 for i := range out {
1474 for j := 0; j < len(fullshort); j += 2 {
1475 full, short := fullshort[j], fullshort[j+1]
1476 out[i] = strings.Replace(out[i], full, short, -1)
1480 var want []wantedError
1481 for j := 0; j < len(fullshort); j += 2 {
1482 full, short := fullshort[j], fullshort[j+1]
1483 want = append(want, t.wantedErrors(full, short)...)
1486 for _, we := range want {
1487 var errmsgs []string
1489 errmsgs, out = partitionStrings("<autogenerated>", out)
1491 errmsgs, out = partitionStrings(we.prefix, out)
1493 if len(errmsgs) == 0 {
1494 errs = append(errs, fmt.Errorf("%s:%d: missing error %q", we.file, we.lineNum, we.reStr))
1499 for _, errmsg := range errmsgs {
1500 // Assume errmsg says "file:line: foo".
1501 // Cut leading "file:line: " to avoid accidental matching of file name instead of message.
1503 if i := strings.Index(text, " "); i >= 0 {
1506 if we.re.MatchString(text) {
1509 out = append(out, errmsg)
1513 errs = append(errs, fmt.Errorf("%s:%d: no match for %#q in:\n\t%s", we.file, we.lineNum, we.reStr, strings.Join(out[n:], "\n\t")))
1519 errs = append(errs, fmt.Errorf("Unmatched Errors:"))
1520 for _, errLine := range out {
1521 errs = append(errs, fmt.Errorf("%s", errLine))
1531 var buf bytes.Buffer
1532 fmt.Fprintf(&buf, "\n")
1533 for _, err := range errs {
1534 fmt.Fprintf(&buf, "%s\n", err.Error())
1536 return errors.New(buf.String())
1539 func (t *test) updateErrors(out, file string) {
1540 base := path.Base(file)
1541 // Read in source file.
1542 src, err := ioutil.ReadFile(file)
1544 fmt.Fprintln(os.Stderr, err)
1547 lines := strings.Split(string(src), "\n")
1548 // Remove old errors.
1549 for i, ln := range lines {
1550 pos := strings.Index(ln, " // ERROR ")
1555 // Parse new errors.
1556 errors := make(map[int]map[string]bool)
1557 tmpRe := regexp.MustCompile(`autotmp_[0-9]+`)
1558 for _, errStr := range splitOutput(out, false) {
1559 colon1 := strings.Index(errStr, ":")
1560 if colon1 < 0 || errStr[:colon1] != file {
1563 colon2 := strings.Index(errStr[colon1+1:], ":")
1567 colon2 += colon1 + 1
1568 line, err := strconv.Atoi(errStr[colon1+1 : colon2])
1570 if err != nil || line < 0 || line >= len(lines) {
1573 msg := errStr[colon2+2:]
1574 msg = strings.Replace(msg, file, base, -1) // normalize file mentions in error itself
1575 msg = strings.TrimLeft(msg, " \t")
1576 for _, r := range []string{`\`, `*`, `+`, `?`, `[`, `]`, `(`, `)`} {
1577 msg = strings.Replace(msg, r, `\`+r, -1)
1579 msg = strings.Replace(msg, `"`, `.`, -1)
1580 msg = tmpRe.ReplaceAllLiteralString(msg, `autotmp_[0-9]+`)
1581 if errors[line] == nil {
1582 errors[line] = make(map[string]bool)
1584 errors[line][msg] = true
1587 for line, errs := range errors {
1589 for e := range errs {
1590 sorted = append(sorted, e)
1592 sort.Strings(sorted)
1593 lines[line] += " // ERROR"
1594 for _, e := range sorted {
1595 lines[line] += fmt.Sprintf(` "%s$"`, e)
1599 err = ioutil.WriteFile(file, []byte(strings.Join(lines, "\n")), 0640)
1601 fmt.Fprintln(os.Stderr, err)
1605 exec.Command(goTool(), "fmt", file).CombinedOutput()
1608 // matchPrefix reports whether s is of the form ^(.*/)?prefix(:|[),
1609 // That is, it needs the file name prefix followed by a : or a [,
1610 // and possibly preceded by a directory name.
1611 func matchPrefix(s, prefix string) bool {
1612 i := strings.Index(s, ":")
1616 j := strings.LastIndex(s[:i], "/")
1618 if len(s) <= len(prefix) || s[:len(prefix)] != prefix {
1621 switch s[len(prefix)] {
1628 func partitionStrings(prefix string, strs []string) (matched, unmatched []string) {
1629 for _, s := range strs {
1630 if matchPrefix(s, prefix) {
1631 matched = append(matched, s)
1633 unmatched = append(unmatched, s)
1639 type wantedError struct {
1643 auto bool // match <autogenerated> line
1649 errRx = regexp.MustCompile(`// (?:GC_)?ERROR (.*)`)
1650 errAutoRx = regexp.MustCompile(`// (?:GC_)?ERRORAUTO (.*)`)
1651 errQuotesRx = regexp.MustCompile(`"([^"]*)"`)
1652 lineRx = regexp.MustCompile(`LINE(([+-])([0-9]+))?`)
1655 func (t *test) wantedErrors(file, short string) (errs []wantedError) {
1656 cache := make(map[string]*regexp.Regexp)
1658 src, _ := ioutil.ReadFile(file)
1659 for i, line := range strings.Split(string(src), "\n") {
1661 if strings.Contains(line, "////") {
1662 // double comment disables ERROR
1666 m := errAutoRx.FindStringSubmatch(line)
1670 m = errRx.FindStringSubmatch(line)
1676 mm := errQuotesRx.FindAllStringSubmatch(all, -1)
1678 log.Fatalf("%s:%d: invalid errchk line: %s", t.goFileName(), lineNum, line)
1680 for _, m := range mm {
1681 rx := lineRx.ReplaceAllStringFunc(m[1], func(m string) string {
1683 if strings.HasPrefix(m, "LINE+") {
1684 delta, _ := strconv.Atoi(m[5:])
1686 } else if strings.HasPrefix(m, "LINE-") {
1687 delta, _ := strconv.Atoi(m[5:])
1690 return fmt.Sprintf("%s:%d", short, n)
1695 re, err = regexp.Compile(rx)
1697 log.Fatalf("%s:%d: invalid regexp \"%s\" in ERROR line: %v", t.goFileName(), lineNum, rx, err)
1701 prefix := fmt.Sprintf("%s:%d", short, lineNum)
1702 errs = append(errs, wantedError{
1717 // Regexp to match a single opcode check: optionally begin with "-" (to indicate
1718 // a negative check), followed by a string literal enclosed in "" or ``. For "",
1719 // backslashes must be handled.
1720 reMatchCheck = `-?(?:\x60[^\x60]*\x60|"(?:[^"\\]|\\.)*")`
1724 // Regexp to split a line in code and comment, trimming spaces
1725 rxAsmComment = regexp.MustCompile(`^\s*(.*?)\s*(?://\s*(.+)\s*)?$`)
1727 // Regexp to extract an architecture check: architecture name (or triplet),
1728 // followed by semi-colon, followed by a comma-separated list of opcode checks.
1729 // Extraneous spaces are ignored.
1730 rxAsmPlatform = regexp.MustCompile(`(\w+)(/\w+)?(/\w*)?\s*:\s*(` + reMatchCheck + `(?:\s*,\s*` + reMatchCheck + `)*)`)
1732 // Regexp to extract a single opcoded check
1733 rxAsmCheck = regexp.MustCompile(reMatchCheck)
1735 // List of all architecture variants. Key is the GOARCH architecture,
1736 // value[0] is the variant-changing environment variable, and values[1:]
1737 // are the supported variants.
1738 archVariants = map[string][]string{
1739 "386": {"GO386", "sse2", "softfloat"},
1741 "arm": {"GOARM", "5", "6", "7"},
1743 "mips": {"GOMIPS", "hardfloat", "softfloat"},
1744 "mips64": {"GOMIPS64", "hardfloat", "softfloat"},
1745 "ppc64": {"GOPPC64", "power8", "power9"},
1746 "ppc64le": {"GOPPC64", "power8", "power9"},
1752 // wantedAsmOpcode is a single asmcheck check
1753 type wantedAsmOpcode struct {
1754 fileline string // original source file/line (eg: "/path/foo.go:45")
1755 line int // original source line
1756 opcode *regexp.Regexp // opcode check to be performed on assembly output
1757 negative bool // true if the check is supposed to fail rather than pass
1758 found bool // true if the opcode check matched at least one in the output
1761 // A build environment triplet separated by slashes (eg: linux/386/sse2).
1762 // The third field can be empty if the arch does not support variants (eg: "plan9/amd64/")
1763 type buildEnv string
1765 // Environ returns the environment it represents in cmd.Environ() "key=val" format
1766 // For instance, "linux/386/sse2".Environ() returns {"GOOS=linux", "GOARCH=386", "GO386=sse2"}
1767 func (b buildEnv) Environ() []string {
1768 fields := strings.Split(string(b), "/")
1769 if len(fields) != 3 {
1770 panic("invalid buildEnv string: " + string(b))
1772 env := []string{"GOOS=" + fields[0], "GOARCH=" + fields[1]}
1773 if fields[2] != "" {
1774 env = append(env, archVariants[fields[1]][0]+"="+fields[2])
1779 // asmChecks represents all the asmcheck checks present in a test file
1780 // The outer map key is the build triplet in which the checks must be performed.
1781 // The inner map key represent the source file line ("filename.go:1234") at which the
1782 // checks must be performed.
1783 type asmChecks map[buildEnv]map[string][]wantedAsmOpcode
1785 // Envs returns all the buildEnv in which at least one check is present
1786 func (a asmChecks) Envs() []buildEnv {
1789 envs = append(envs, e)
1791 sort.Slice(envs, func(i, j int) bool {
1792 return string(envs[i]) < string(envs[j])
1797 func (t *test) wantedAsmOpcodes(fn string) asmChecks {
1798 ops := make(asmChecks)
1801 src, _ := ioutil.ReadFile(fn)
1802 for i, line := range strings.Split(string(src), "\n") {
1803 matches := rxAsmComment.FindStringSubmatch(line)
1804 code, cmt := matches[1], matches[2]
1806 // Keep comments pending in the comment variable until
1807 // we find a line that contains some code.
1808 comment += " " + cmt
1813 // Parse and extract any architecture check from comments,
1814 // made by one architecture name and multiple checks.
1815 lnum := fn + ":" + strconv.Itoa(i+1)
1816 for _, ac := range rxAsmPlatform.FindAllStringSubmatch(comment, -1) {
1817 archspec, allchecks := ac[1:4], ac[4]
1819 var arch, subarch, os string
1821 case archspec[2] != "": // 3 components: "linux/386/sse2"
1822 os, arch, subarch = archspec[0], archspec[1][1:], archspec[2][1:]
1823 case archspec[1] != "": // 2 components: "386/sse2"
1824 os, arch, subarch = "linux", archspec[0], archspec[1][1:]
1825 default: // 1 component: "386"
1826 os, arch, subarch = "linux", archspec[0], ""
1832 if _, ok := archVariants[arch]; !ok {
1833 log.Fatalf("%s:%d: unsupported architecture: %v", t.goFileName(), i+1, arch)
1836 // Create the build environments corresponding the above specifiers
1837 envs := make([]buildEnv, 0, 4)
1839 envs = append(envs, buildEnv(os+"/"+arch+"/"+subarch))
1841 subarchs := archVariants[arch]
1842 if len(subarchs) == 0 {
1843 envs = append(envs, buildEnv(os+"/"+arch+"/"))
1845 for _, sa := range archVariants[arch][1:] {
1846 envs = append(envs, buildEnv(os+"/"+arch+"/"+sa))
1851 for _, m := range rxAsmCheck.FindAllString(allchecks, -1) {
1858 rxsrc, err := strconv.Unquote(m)
1860 log.Fatalf("%s:%d: error unquoting string: %v", t.goFileName(), i+1, err)
1863 // Compile the checks as regular expressions. Notice that we
1864 // consider checks as matching from the beginning of the actual
1865 // assembler source (that is, what is left on each line of the
1866 // compile -S output after we strip file/line info) to avoid
1867 // trivial bugs such as "ADD" matching "FADD". This
1868 // doesn't remove genericity: it's still possible to write
1869 // something like "F?ADD", but we make common cases simpler
1871 oprx, err := regexp.Compile("^" + rxsrc)
1873 log.Fatalf("%s:%d: %v", t.goFileName(), i+1, err)
1876 for _, env := range envs {
1877 if ops[env] == nil {
1878 ops[env] = make(map[string][]wantedAsmOpcode)
1880 ops[env][lnum] = append(ops[env][lnum], wantedAsmOpcode{
1895 func (t *test) asmCheck(outStr string, fn string, env buildEnv, fullops map[string][]wantedAsmOpcode) (err error) {
1896 // The assembly output contains the concatenated dump of multiple functions.
1897 // the first line of each function begins at column 0, while the rest is
1898 // indented by a tabulation. These data structures help us index the
1899 // output by function.
1900 functionMarkers := make([]int, 1)
1901 lineFuncMap := make(map[string]int)
1903 lines := strings.Split(outStr, "\n")
1904 rxLine := regexp.MustCompile(fmt.Sprintf(`\((%s:\d+)\)\s+(.*)`, regexp.QuoteMeta(fn)))
1906 for nl, line := range lines {
1907 // Check if this line begins a function
1908 if len(line) > 0 && line[0] != '\t' {
1909 functionMarkers = append(functionMarkers, nl)
1912 // Search if this line contains a assembly opcode (which is prefixed by the
1913 // original source file/line in parenthesis)
1914 matches := rxLine.FindStringSubmatch(line)
1915 if len(matches) == 0 {
1918 srcFileLine, asm := matches[1], matches[2]
1920 // Associate the original file/line information to the current
1921 // function in the output; it will be useful to dump it in case
1923 lineFuncMap[srcFileLine] = len(functionMarkers) - 1
1925 // If there are opcode checks associated to this source file/line,
1927 if ops, found := fullops[srcFileLine]; found {
1928 for i := range ops {
1929 if !ops[i].found && ops[i].opcode.FindString(asm) != "" {
1935 functionMarkers = append(functionMarkers, len(lines))
1937 var failed []wantedAsmOpcode
1938 for _, ops := range fullops {
1939 for _, o := range ops {
1940 // There's a failure if a negative match was found,
1941 // or a positive match was not found.
1942 if o.negative == o.found {
1943 failed = append(failed, o)
1947 if len(failed) == 0 {
1951 // At least one asmcheck failed; report them
1952 sort.Slice(failed, func(i, j int) bool {
1953 return failed[i].line < failed[j].line
1957 var errbuf bytes.Buffer
1958 fmt.Fprintln(&errbuf)
1959 for _, o := range failed {
1960 // Dump the function in which this opcode check was supposed to
1962 funcIdx := lineFuncMap[o.fileline]
1963 if funcIdx != 0 && funcIdx != lastFunction {
1964 funcLines := lines[functionMarkers[funcIdx]:functionMarkers[funcIdx+1]]
1965 log.Println(strings.Join(funcLines, "\n"))
1966 lastFunction = funcIdx // avoid printing same function twice
1970 fmt.Fprintf(&errbuf, "%s:%d: %s: wrong opcode found: %q\n", t.goFileName(), o.line, env, o.opcode.String())
1972 fmt.Fprintf(&errbuf, "%s:%d: %s: opcode not found: %q\n", t.goFileName(), o.line, env, o.opcode.String())
1975 err = errors.New(errbuf.String())
1979 // defaultRunOutputLimit returns the number of runoutput tests that
1980 // can be executed in parallel.
1981 func defaultRunOutputLimit() int {
1984 cpu := runtime.NumCPU()
1985 if runtime.GOARCH == "arm" && cpu > maxArmCPU {
1991 // checkShouldTest runs sanity checks on the shouldTest function.
1992 func checkShouldTest() {
1993 assert := func(ok bool, _ string) {
1998 assertNot := func(ok bool, _ string) { assert(!ok, "") }
2001 assert(shouldTest("// +build linux", "linux", "arm"))
2002 assert(shouldTest("// +build !windows", "linux", "arm"))
2003 assertNot(shouldTest("// +build !windows", "windows", "amd64"))
2005 // A file with no build tags will always be tested.
2006 assert(shouldTest("// This is a test.", "os", "arch"))
2008 // Build tags separated by a space are OR-ed together.
2009 assertNot(shouldTest("// +build arm 386", "linux", "amd64"))
2011 // Build tags separated by a comma are AND-ed together.
2012 assertNot(shouldTest("// +build !windows,!plan9", "windows", "amd64"))
2013 assertNot(shouldTest("// +build !windows,!plan9", "plan9", "386"))
2015 // Build tags on multiple lines are AND-ed together.
2016 assert(shouldTest("// +build !windows\n// +build amd64", "linux", "amd64"))
2017 assertNot(shouldTest("// +build !windows\n// +build amd64", "windows", "amd64"))
2019 // Test that (!a OR !b) matches anything.
2020 assert(shouldTest("// +build !windows !plan9", "windows", "amd64"))
2023 func getenv(key, def string) string {
2024 value := os.Getenv(key)
2031 // overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
2032 func overlayDir(dstRoot, srcRoot string) error {
2033 dstRoot = filepath.Clean(dstRoot)
2034 if err := os.MkdirAll(dstRoot, 0777); err != nil {
2038 srcRoot, err := filepath.Abs(srcRoot)
2043 return filepath.WalkDir(srcRoot, func(srcPath string, d fs.DirEntry, err error) error {
2044 if err != nil || srcPath == srcRoot {
2048 suffix := strings.TrimPrefix(srcPath, srcRoot)
2049 for len(suffix) > 0 && suffix[0] == filepath.Separator {
2052 dstPath := filepath.Join(dstRoot, suffix)
2054 var info fs.FileInfo
2055 if d.Type()&os.ModeSymlink != 0 {
2056 info, err = os.Stat(srcPath)
2058 info, err = d.Info()
2063 perm := info.Mode() & os.ModePerm
2065 // Always copy directories (don't symlink them).
2066 // If we add a file in the overlay, we don't want to add it in the original.
2068 return os.MkdirAll(dstPath, perm|0200)
2071 // If the OS supports symlinks, use them instead of copying bytes.
2072 if err := os.Symlink(srcPath, dstPath); err == nil {
2076 // Otherwise, copy the bytes.
2077 src, err := os.Open(srcPath)
2083 dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
2088 _, err = io.Copy(dst, src)
2089 if closeErr := dst.Close(); err == nil {
2096 // The following is temporary scaffolding to get types2 typechecker
2097 // up and running against the existing test cases. The explicitly
2098 // listed files don't pass yet, usually because the error messages
2099 // are slightly different (this list is not complete). Any errorcheck
2100 // tests that require output from analysis phases past initial type-
2101 // checking are also excluded since these phases are not running yet.
2102 // We can get rid of this code once types2 is fully plugged in.
2104 // List of files that the compiler cannot errorcheck with the new typechecker (compiler -G option).
2105 // Temporary scaffolding until we pass all the tests at which point this map can be removed.
2106 var types2Failures = setOf(
2107 "directive.go", // misplaced compiler directive checks
2108 "float_lit3.go", // types2 reports extra errors
2109 "import1.go", // types2 reports extra errors
2110 "import6.go", // issue #43109
2111 "initializerr.go", // types2 reports extra errors
2112 "linkname2.go", // error reported by noder (not running for types2 errorcheck test)
2113 "notinheap.go", // types2 doesn't report errors about conversions that are invalid due to //go:notinheap
2114 "shift1.go", // issue #42989
2115 "typecheck.go", // invalid function is not causing errors when called
2117 "interface/private.go", // types2 phrases errors differently (doesn't use non-spec "private" term)
2119 "fixedbugs/bug176.go", // types2 reports all errors (pref: types2)
2120 "fixedbugs/bug195.go", // types2 reports slightly different (but correct) bugs
2121 "fixedbugs/bug228.go", // types2 doesn't run when there are syntax errors
2122 "fixedbugs/bug231.go", // types2 bug? (same error reported twice)
2123 "fixedbugs/bug255.go", // types2 reports extra errors
2124 "fixedbugs/bug374.go", // types2 reports extra errors
2125 "fixedbugs/bug388.go", // types2 not run due to syntax errors
2126 "fixedbugs/bug412.go", // types2 produces a follow-on error
2128 "fixedbugs/issue10700.go", // types2 reports ok hint, but does not match regexp
2129 "fixedbugs/issue11590.go", // types2 doesn't report a follow-on error (pref: types2)
2130 "fixedbugs/issue11610.go", // types2 not run after syntax errors
2131 "fixedbugs/issue11614.go", // types2 reports an extra error
2132 "fixedbugs/issue14520.go", // missing import path error by types2
2133 "fixedbugs/issue16133.go", // types2 doesn't use package path for qualified identifiers when package name is ambiguous
2134 "fixedbugs/issue16428.go", // types2 reports two instead of one error
2135 "fixedbugs/issue17038.go", // types2 doesn't report a follow-on error (pref: types2)
2136 "fixedbugs/issue17645.go", // multiple errors on same line
2137 "fixedbugs/issue18331.go", // missing error about misuse of //go:noescape (irgen needs code from noder)
2138 "fixedbugs/issue18419.go", // types2 reports
2139 "fixedbugs/issue19012.go", // multiple errors on same line
2140 "fixedbugs/issue20233.go", // types2 reports two instead of one error (pref: compiler)
2141 "fixedbugs/issue20245.go", // types2 reports two instead of one error (pref: compiler)
2142 "fixedbugs/issue21979.go", // types2 doesn't report a follow-on error (pref: types2)
2143 "fixedbugs/issue23732.go", // types2 reports different (but ok) line numbers
2144 "fixedbugs/issue25958.go", // types2 doesn't report a follow-on error (pref: types2)
2145 "fixedbugs/issue28079b.go", // types2 reports follow-on errors
2146 "fixedbugs/issue28268.go", // types2 reports follow-on errors
2147 "fixedbugs/issue31053.go", // types2 reports "unknown field" instead of "cannot refer to unexported field"
2148 "fixedbugs/issue33460.go", // types2 reports alternative positions in separate error
2149 "fixedbugs/issue42058a.go", // types2 doesn't report "channel element type too large"
2150 "fixedbugs/issue42058b.go", // types2 doesn't report "channel element type too large"
2151 "fixedbugs/issue4232.go", // types2 reports (correct) extra errors
2152 "fixedbugs/issue4452.go", // types2 reports (correct) extra errors
2153 "fixedbugs/issue4510.go", // types2 reports different (but ok) line numbers
2154 "fixedbugs/issue5609.go", // types2 needs a better error message
2155 "fixedbugs/issue7525b.go", // types2 reports init cycle error on different line - ok otherwise
2156 "fixedbugs/issue7525c.go", // types2 reports init cycle error on different line - ok otherwise
2157 "fixedbugs/issue7525d.go", // types2 reports init cycle error on different line - ok otherwise
2158 "fixedbugs/issue7525e.go", // types2 reports init cycle error on different line - ok otherwise
2159 "fixedbugs/issue7525.go", // types2 reports init cycle error on different line - ok otherwise
2162 var types2Failures32Bit = setOf(
2163 "printbig.go", // large untyped int passed to print (32-bit)
2164 "fixedbugs/bug114.go", // large untyped int passed to println (32-bit)
2165 "fixedbugs/issue23305.go", // large untyped int passed to println (32-bit)
2166 "fixedbugs/bug385_32.go", // types2 doesn't produce missing error "type .* too large" (32-bit specific)
2169 var g3Failures = setOf(
2170 "writebarrier.go", // correct diagnostics, but different lines (probably irgen's fault)
2172 "typeparam/nested.go", // -G=3 doesn't support function-local types with generics
2174 "typeparam/mdempsky/4.go", // -G=3 can't export functions with labeled breaks in loops
2177 var unifiedFailures = setOf(
2178 "closure3.go", // unified IR numbers closures differently than -d=inlfuncswithclosures
2179 "escape4.go", // unified IR can inline f5 and f6; test doesn't expect this
2180 "inline.go", // unified IR reports function literal diagnostics on different lines than -d=inlfuncswithclosures
2182 "fixedbugs/issue42284.go", // prints "T(0) does not escape", but test expects "a.I(a.T(0)) does not escape"
2183 "fixedbugs/issue7921.go", // prints "… escapes to heap", but test expects "string(…) escapes to heap"
2186 func setOf(keys ...string) map[string]bool {
2187 m := make(map[string]bool, len(keys))
2188 for _, key := range keys {
2194 // splitQuoted splits the string s around each instance of one or more consecutive
2195 // white space characters while taking into account quotes and escaping, and
2196 // returns an array of substrings of s or an empty list if s contains only white space.
2197 // Single quotes and double quotes are recognized to prevent splitting within the
2198 // quoted region, and are removed from the resulting substrings. If a quote in s
2199 // isn't closed err will be set and r will have the unclosed argument as the
2200 // last element. The backslash is used for escaping.
2202 // For example, the following string:
2204 // a b:"c d" 'e''f' "g\""
2206 // Would be parsed as:
2208 // []string{"a", "b:c d", "ef", `g"`}
2210 // [copied from src/go/build/build.go]
2211 func splitQuoted(s string) (r []string, err error) {
2213 arg := make([]rune, len(s))
2218 for _, rune := range s {
2225 case quote != '\x00':
2230 case rune == '"' || rune == '\'':
2234 case unicode.IsSpace(rune):
2235 if quoted || i > 0 {
2237 args = append(args, string(arg[:i]))
2245 if quoted || i > 0 {
2246 args = append(args, string(arg[:i]))
2249 err = errors.New("unclosed quote")
2251 err = errors.New("unfinished escaping")