2016-04-10 14:32:26 -07:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
2012-02-21 14:28:49 +11:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2023-02-08 11:40:06 -05:00
|
|
|
// Package testdir_test runs tests in the GOROOT/test directory.
|
|
|
|
|
package testdir_test
|
2012-02-21 14:28:49 +11:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2021-07-02 15:42:20 -07:00
|
|
|
"encoding/json"
|
2012-02-21 14:28:49 +11:00
|
|
|
"errors"
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
2021-04-15 23:07:41 -04:00
|
|
|
"go/build"
|
2022-04-11 17:35:24 -07:00
|
|
|
"go/build/constraint"
|
2015-06-03 23:21:30 -07:00
|
|
|
"hash/fnv"
|
2023-02-06 17:10:22 -05:00
|
|
|
"internal/testenv"
|
2015-06-03 23:21:30 -07:00
|
|
|
"io"
|
2020-11-04 18:20:17 -05:00
|
|
|
"io/fs"
|
2012-02-21 14:28:49 +11:00
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
2012-09-23 13:16:14 -04:00
|
|
|
"path"
|
2012-02-21 14:28:49 +11:00
|
|
|
"path/filepath"
|
|
|
|
|
"regexp"
|
|
|
|
|
"runtime"
|
2023-10-18 00:26:42 +07:00
|
|
|
"slices"
|
2012-02-21 14:28:49 +11:00
|
|
|
"sort"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2022-09-21 15:51:27 -04:00
|
|
|
"sync"
|
2023-02-08 11:40:06 -05:00
|
|
|
"testing"
|
2013-12-10 14:02:42 -05:00
|
|
|
"time"
|
2013-08-13 12:25:41 -04:00
|
|
|
"unicode"
|
2012-02-21 14:28:49 +11:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2019-09-26 17:25:04 +00:00
|
|
|
allCodegen = flag.Bool("all_codegen", defaultAllCodeGen(), "run all goos/goarch for codegen")
|
2015-10-26 17:34:06 -04:00
|
|
|
runSkips = flag.Bool("run_skips", false, "run skipped tests (ignore skip and build tags)")
|
2015-10-27 14:54:19 +13:00
|
|
|
linkshared = flag.Bool("linkshared", false, "")
|
2015-02-19 22:00:11 +03:00
|
|
|
updateErrors = flag.Bool("update_errors", false, "update error messages in test file based on compiler output")
|
2013-01-12 17:52:52 +11:00
|
|
|
runoutputLimit = flag.Int("l", defaultRunOutputLimit(), "number of parallel runoutput tests to run")
|
2021-07-02 13:18:03 -07:00
|
|
|
force = flag.Bool("f", false, "ignore expected-failure test lists")
|
2023-05-05 13:26:16 -04:00
|
|
|
target = flag.String("target", "", "cross-compile tests for `goos/goarch`")
|
2015-06-03 23:21:30 -07:00
|
|
|
|
|
|
|
|
shard = flag.Int("shard", 0, "shard index to run. Only applicable if -shards is non-zero.")
|
|
|
|
|
shards = flag.Int("shards", 0, "number of shards. If 0, all tests are run. This is used by the continuous build.")
|
2012-02-21 14:28:49 +11:00
|
|
|
)
|
|
|
|
|
|
2019-09-26 17:25:04 +00:00
|
|
|
// defaultAllCodeGen returns the default value of the -all_codegen
|
|
|
|
|
// flag. By default, we prefer to be fast (returning false), except on
|
|
|
|
|
// the linux-amd64 builder that's already very fast, so we get more
|
2023-02-06 17:10:22 -05:00
|
|
|
// test coverage on trybots. See https://go.dev/issue/34297.
|
2019-09-26 17:25:04 +00:00
|
|
|
func defaultAllCodeGen() bool {
|
|
|
|
|
return os.Getenv("GO_BUILDER_NAME") == "linux-amd64"
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 14:28:49 +11:00
|
|
|
var (
|
2023-02-06 17:10:22 -05:00
|
|
|
// Package-scoped variables that are initialized at the start of Test.
|
|
|
|
|
goTool string
|
2023-05-05 13:26:16 -04:00
|
|
|
goos string // Target GOOS
|
|
|
|
|
goarch string // Target GOARCH
|
2023-02-06 17:10:22 -05:00
|
|
|
cgoEnabled bool
|
|
|
|
|
goExperiment string
|
2024-02-29 20:17:09 +07:00
|
|
|
goDebug string
|
2024-09-04 22:40:31 -07:00
|
|
|
tmpDir string
|
2012-02-21 14:28:49 +11:00
|
|
|
|
|
|
|
|
// dirs are the directories to look for *.go files in.
|
|
|
|
|
// TODO(bradfitz): just use all directories?
|
2024-09-16 15:58:36 -04:00
|
|
|
dirs = []string{".", "ken", "chan", "interface", "internal/runtime/sys", "syntax", "dwarf", "fixedbugs", "codegen", "abi", "typeparam", "typeparam/mdempsky", "arenas"}
|
2012-02-21 14:28:49 +11:00
|
|
|
)
|
|
|
|
|
|
2023-02-08 11:40:06 -05:00
|
|
|
// Test is the main entrypoint that runs tests in the GOROOT/test directory.
|
2023-02-06 17:10:22 -05:00
|
|
|
//
|
2024-08-19 00:07:37 +03:00
|
|
|
// Each .go file test case in GOROOT/test is registered as a subtest with
|
2023-02-06 17:10:22 -05:00
|
|
|
// a full name like "Test/fixedbugs/bug000.go" ('/'-separated relative path).
|
2023-02-08 11:40:06 -05:00
|
|
|
func Test(t *testing.T) {
|
2023-05-05 13:26:16 -04:00
|
|
|
if *target != "" {
|
|
|
|
|
// When -target is set, propagate it to GOOS/GOARCH in our environment
|
|
|
|
|
// so that all commands run with the target GOOS/GOARCH.
|
|
|
|
|
//
|
|
|
|
|
// We do this before even calling "go env", because GOOS/GOARCH can
|
|
|
|
|
// affect other settings we get from go env (notably CGO_ENABLED).
|
|
|
|
|
goos, goarch, ok := strings.Cut(*target, "/")
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Fatalf("bad -target flag %q, expected goos/goarch", *target)
|
|
|
|
|
}
|
|
|
|
|
t.Setenv("GOOS", goos)
|
|
|
|
|
t.Setenv("GOARCH", goarch)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
goTool = testenv.GoToolPath(t)
|
|
|
|
|
cmd := exec.Command(goTool, "env", "-json")
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
2023-02-08 11:40:06 -05:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatal("StdoutPipe:", err)
|
|
|
|
|
}
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
|
t.Fatal("Start:", err)
|
|
|
|
|
}
|
|
|
|
|
var env struct {
|
|
|
|
|
GOOS string
|
|
|
|
|
GOARCH string
|
|
|
|
|
GOEXPERIMENT string
|
2024-02-29 20:17:09 +07:00
|
|
|
GODEBUG string
|
2023-02-06 17:10:22 -05:00
|
|
|
CGO_ENABLED string
|
|
|
|
|
}
|
|
|
|
|
if err := json.NewDecoder(stdout).Decode(&env); err != nil {
|
|
|
|
|
t.Fatal("Decode:", err)
|
|
|
|
|
}
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
|
t.Fatal("Wait:", err)
|
2014-05-28 01:01:08 -04:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
goos = env.GOOS
|
|
|
|
|
goarch = env.GOARCH
|
|
|
|
|
cgoEnabled, _ = strconv.ParseBool(env.CGO_ENABLED)
|
|
|
|
|
goExperiment = env.GOEXPERIMENT
|
2024-02-29 20:17:09 +07:00
|
|
|
goDebug = env.GODEBUG
|
2024-09-04 22:40:31 -07:00
|
|
|
tmpDir = t.TempDir()
|
2014-05-28 01:01:08 -04:00
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
common := testCommon{
|
|
|
|
|
gorootTestDir: filepath.Join(testenv.GOROOT(t), "test"),
|
|
|
|
|
runoutputGate: make(chan bool, *runoutputLimit),
|
|
|
|
|
}
|
2012-02-21 14:28:49 +11:00
|
|
|
|
2023-06-16 15:45:13 -04:00
|
|
|
// cmd/distpack deletes GOROOT/test, so skip the test if it isn't present.
|
|
|
|
|
// cmd/distpack also requires GOROOT/VERSION to exist, so use that to
|
|
|
|
|
// suppress false-positive skips.
|
|
|
|
|
if _, err := os.Stat(common.gorootTestDir); os.IsNotExist(err) {
|
|
|
|
|
if _, err := os.Stat(filepath.Join(testenv.GOROOT(t), "VERSION")); err == nil {
|
|
|
|
|
t.Skipf("skipping: GOROOT/test not present")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 11:40:06 -05:00
|
|
|
for _, dir := range dirs {
|
2023-02-06 17:10:22 -05:00
|
|
|
for _, goFile := range goFiles(t, dir) {
|
|
|
|
|
test := test{testCommon: common, dir: dir, goFile: goFile}
|
|
|
|
|
t.Run(path.Join(dir, goFile), func(t *testing.T) {
|
2023-02-08 11:40:06 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
test.T = t
|
2023-02-06 17:10:22 -05:00
|
|
|
testError := test.run()
|
|
|
|
|
wantError := test.expectFail() && !*force
|
|
|
|
|
if testError != nil {
|
|
|
|
|
if wantError {
|
|
|
|
|
t.Log(testError.Error() + " (expected)")
|
2023-02-08 11:40:06 -05:00
|
|
|
} else {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatal(testError)
|
2023-02-08 11:40:06 -05:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
} else if wantError {
|
2023-02-08 11:40:06 -05:00
|
|
|
t.Fatal("unexpected success")
|
|
|
|
|
}
|
|
|
|
|
})
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-03 23:21:30 -07:00
|
|
|
func shardMatch(name string) bool {
|
2023-02-08 11:40:06 -05:00
|
|
|
if *shards <= 1 {
|
2015-06-03 23:21:30 -07:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
h := fnv.New32()
|
|
|
|
|
io.WriteString(h, name)
|
|
|
|
|
return int(h.Sum32()%uint32(*shards)) == *shard
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
func goFiles(t *testing.T, dir string) []string {
|
2024-09-04 21:56:06 -07:00
|
|
|
files, err := os.ReadDir(filepath.Join(testenv.GOROOT(t), "test", dir))
|
2020-02-09 01:40:45 -05:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatal(err)
|
2020-02-09 01:40:45 -05:00
|
|
|
}
|
2012-02-21 14:28:49 +11:00
|
|
|
names := []string{}
|
2024-09-04 21:56:06 -07:00
|
|
|
for _, file := range files {
|
|
|
|
|
name := file.Name()
|
2015-06-03 23:21:30 -07:00
|
|
|
if !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && shardMatch(name) {
|
2012-02-21 14:28:49 +11:00
|
|
|
names = append(names, name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return names
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-06 09:23:31 +02:00
|
|
|
type runCmd func(...string) ([]byte, error)
|
|
|
|
|
|
2017-04-28 07:28:49 -07:00
|
|
|
func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, err error) {
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd := []string{goTool, "tool", "compile", "-e", "-p=p", "-importcfg=" + stdlibImportcfgFile()}
|
2017-04-28 07:28:49 -07:00
|
|
|
cmd = append(cmd, flags...)
|
2015-10-27 14:54:19 +13:00
|
|
|
if *linkshared {
|
|
|
|
|
cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
|
|
|
|
|
}
|
|
|
|
|
cmd = append(cmd, longname)
|
|
|
|
|
return runcmd(cmd...)
|
2012-10-06 09:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-21 15:51:27 -04:00
|
|
|
func compileInDir(runcmd runCmd, dir string, flags []string, importcfg string, pkgname string, names ...string) (out []byte, err error) {
|
|
|
|
|
if importcfg == "" {
|
|
|
|
|
importcfg = stdlibImportcfgFile()
|
|
|
|
|
}
|
2023-05-24 16:30:49 +00:00
|
|
|
cmd := []string{goTool, "tool", "compile", "-e", "-D", "test", "-importcfg=" + importcfg}
|
2022-03-23 14:24:26 -07:00
|
|
|
if pkgname == "main" {
|
|
|
|
|
cmd = append(cmd, "-p=main")
|
|
|
|
|
} else {
|
|
|
|
|
pkgname = path.Join("test", strings.TrimSuffix(names[0], ".go"))
|
|
|
|
|
cmd = append(cmd, "-o", pkgname+".a", "-p", pkgname)
|
2018-05-30 19:46:59 +03:00
|
|
|
}
|
2016-03-11 00:10:52 -05:00
|
|
|
cmd = append(cmd, flags...)
|
2015-10-27 14:54:19 +13:00
|
|
|
if *linkshared {
|
|
|
|
|
cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
|
|
|
|
|
}
|
2013-01-02 15:31:49 -05:00
|
|
|
for _, name := range names {
|
|
|
|
|
cmd = append(cmd, filepath.Join(dir, name))
|
|
|
|
|
}
|
|
|
|
|
return runcmd(cmd...)
|
2012-10-06 09:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 18:15:04 -07:00
|
|
|
var stdlibImportcfg = sync.OnceValue(func() string {
|
|
|
|
|
cmd := exec.Command(goTool, "list", "-export", "-f", "{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}", "std")
|
|
|
|
|
cmd.Env = append(os.Environ(), "GOENV=off", "GOFLAGS=")
|
|
|
|
|
output, err := cmd.Output()
|
2023-06-21 14:42:03 -04:00
|
|
|
if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) != 0 {
|
|
|
|
|
log.Fatalf("'go list' failed: %v: %s", err, err.Stderr)
|
|
|
|
|
}
|
2024-09-04 18:15:04 -07:00
|
|
|
if err != nil {
|
2023-06-21 14:42:03 -04:00
|
|
|
log.Fatalf("'go list' failed: %v", err)
|
2024-09-04 18:15:04 -07:00
|
|
|
}
|
|
|
|
|
return string(output)
|
|
|
|
|
})
|
2023-02-06 17:10:22 -05:00
|
|
|
|
2024-09-04 18:15:04 -07:00
|
|
|
var stdlibImportcfgFile = sync.OnceValue(func() string {
|
2024-09-04 22:40:31 -07:00
|
|
|
filename := filepath.Join(tmpDir, "importcfg")
|
|
|
|
|
err := os.WriteFile(filename, []byte(stdlibImportcfg()), 0644)
|
2024-09-04 18:15:04 -07:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
return filename
|
|
|
|
|
})
|
2022-09-21 15:51:27 -04:00
|
|
|
|
|
|
|
|
func linkFile(runcmd runCmd, goname string, importcfg string, ldflags []string) (err error) {
|
|
|
|
|
if importcfg == "" {
|
|
|
|
|
importcfg = stdlibImportcfgFile()
|
|
|
|
|
}
|
2025-04-14 15:34:30 +00:00
|
|
|
pfile := strings.ReplaceAll(goname, ".go", ".o")
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd := []string{goTool, "tool", "link", "-w", "-o", "a.exe", "-importcfg=" + importcfg}
|
2015-10-27 14:54:19 +13:00
|
|
|
if *linkshared {
|
|
|
|
|
cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
|
|
|
|
|
}
|
2019-03-22 15:24:36 -04:00
|
|
|
if ldflags != nil {
|
|
|
|
|
cmd = append(cmd, ldflags...)
|
|
|
|
|
}
|
2015-10-27 14:54:19 +13:00
|
|
|
cmd = append(cmd, pfile)
|
|
|
|
|
_, err = runcmd(cmd...)
|
2012-10-06 09:23:31 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
type testCommon struct {
|
|
|
|
|
// gorootTestDir is the GOROOT/test directory path.
|
|
|
|
|
gorootTestDir string
|
2012-02-21 14:28:49 +11:00
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
// runoutputGate controls the max number of runoutput tests
|
|
|
|
|
// executed in parallel as they can each consume a lot of memory.
|
|
|
|
|
runoutputGate chan bool
|
|
|
|
|
}
|
2012-02-21 14:28:49 +11:00
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
// test is a single test case in the GOROOT/test directory.
|
2012-02-21 14:28:49 +11:00
|
|
|
type test struct {
|
2023-02-06 17:10:22 -05:00
|
|
|
testCommon
|
2023-02-08 11:40:06 -05:00
|
|
|
*testing.T
|
2023-02-06 17:10:22 -05:00
|
|
|
// dir and goFile identify the test case.
|
|
|
|
|
// For example, "fixedbugs", "bug000.go".
|
|
|
|
|
dir, goFile string
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
// expectFail reports whether the (overall) test recipe is
|
|
|
|
|
// expected to fail under the current build+test configuration.
|
|
|
|
|
func (t test) expectFail() bool {
|
2022-02-28 14:32:19 -08:00
|
|
|
failureSets := []map[string]bool{types2Failures}
|
2021-08-19 15:53:13 -07:00
|
|
|
|
2022-02-28 14:32:19 -08:00
|
|
|
// Note: gccgo supports more 32-bit architectures than this, but
|
|
|
|
|
// hopefully the 32-bit failures are fixed before this matters.
|
|
|
|
|
switch goarch {
|
|
|
|
|
case "386", "arm", "mips", "mipsle":
|
|
|
|
|
failureSets = append(failureSets, types2Failures32Bit)
|
|
|
|
|
}
|
2021-07-02 13:18:03 -07:00
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
testName := path.Join(t.dir, t.goFile) // Test name is '/'-separated.
|
2021-07-02 13:18:03 -07:00
|
|
|
|
|
|
|
|
for _, set := range failureSets {
|
2023-02-06 17:10:22 -05:00
|
|
|
if set[testName] {
|
|
|
|
|
return true
|
2021-07-02 13:18:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return false
|
2021-07-02 13:18:03 -07:00
|
|
|
}
|
2021-06-16 21:41:28 -07:00
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
func (t test) goFileName() string {
|
|
|
|
|
return filepath.Join(t.dir, t.goFile)
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
func (t test) goDirName() string {
|
2025-04-14 15:34:30 +00:00
|
|
|
return filepath.Join(t.dir, strings.ReplaceAll(t.goFile, ".go", ".dir"))
|
2012-07-30 21:12:05 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
// goDirFiles returns .go files in dir.
|
|
|
|
|
func goDirFiles(dir string) (filter []fs.DirEntry, _ error) {
|
|
|
|
|
files, err := os.ReadDir(dir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2012-10-06 09:23:31 +02:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
for _, goFile := range files {
|
|
|
|
|
if filepath.Ext(goFile.Name()) == ".go" {
|
|
|
|
|
filter = append(filter, goFile)
|
2012-10-06 09:23:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return filter, nil
|
2012-10-06 09:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
2018-09-27 08:46:08 -04:00
|
|
|
var packageRE = regexp.MustCompile(`(?m)^package ([\p{Lu}\p{Ll}\w]+)`)
|
2013-01-02 15:31:49 -05:00
|
|
|
|
2019-03-22 15:24:36 -04:00
|
|
|
func getPackageNameFromSource(fn string) (string, error) {
|
2023-02-06 17:10:22 -05:00
|
|
|
data, err := os.ReadFile(fn)
|
2019-03-22 15:24:36 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
pkgname := packageRE.FindStringSubmatch(string(data))
|
|
|
|
|
if pkgname == nil {
|
|
|
|
|
return "", fmt.Errorf("cannot find package name in %s", fn)
|
|
|
|
|
}
|
|
|
|
|
return pkgname[1], nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
// goDirPkg represents a Go package in some directory.
|
cmd/compile: require -p flag
The -p flag specifies the import path of the package being compiled.
This CL makes it required when invoking the compiler and
adjusts tests that invoke the compiler directly to conform to this
new requirement. The go command already passes the flag, so it
is unmodified in this CL. It is expected that any other Go build systems
also already pass -p, or else they will need to arrange to do so before
updating to Go 1.19. Of particular note, Bazel already does for rules
with an importpath= attribute, which includes all Gazelle-generated rules.
There is more cleanup possible now in cmd/compile, cmd/link,
and other consumers of Go object files, but that is left to future CLs.
Additional historical background follows but can be ignored.
Long ago, before the go command, or modules, or any kind of
versioning, symbols in Go archive files were named using just the
package name, so that for example func F in math/rand and func F in
crypto/rand would both be the object file symbol 'rand.F'. This led to
collisions even in small source trees, which made certain packages
unusable in the presence of other packages and generally was a problem
for Go's goal of scaling to very large source trees.
Fixing this problem required changing from package names to import
paths in symbol names, which was mostly straightforward. One wrinkle,
though, is that the compiler did not know the import path of the
package being compiled; it only knew the package name. At the time,
there was no go command, just Makefiles that people had invoking 6g
(now “go tool compile”) and then copying the resulting object file to
an importable location. That is, everyone had a custom build setup for
Go, because there was no standard one. So it was not particularly
attractive to change how the compiler was invoked, since that would
break approximately every Go user at the time. Instead, we arranged
for the compiler to emit, and other tools reading object files to
recognize, a special import path (the empty string, it turned out)
denoting “the import path of this object file”. This worked well
enough at the time and maintained complete command-line compatibility
with existing Go usage.
The changes implementing this transition can be found by searching
the Git history for “package global name space”, which is what they
eliminated. In particular, CL 190076 (a6736fa4), CL 186263 (758f2bc5),
CL 193080 (1cecac81), CL 194053 (19126320), and CL 194071 (531e6b77)
did the bulk of this transformation in January 2010.
Later, in September 2011, we added the -p flag to the compiler for
diagnostic purposes. The problem was that it was easy to create import
cycles, especially in tests, and these could not be diagnosed until
link time. You'd really want the compiler to diagnose these, for
example if the compilation of package sort noticed it was importing a
package that itself imported "sort". But the compilation of package
sort didn't know its own import path, and so it could not tell whether
it had found itself as a transitive dependency. Adding the -p flag
solved this problem, and its use was optional, since the linker would
still diagnose the import cycle in builds that had not updated to
start passing -p. This was CL 4972057 (1e480cd1).
There was still no go command at this point, but when we introduced
the go command we made it pass -p, which it has for many years at this
point.
Over time, parts of the compiler began to depend on the presence of
the -p flag for various reasonable purposes. For example:
In CL 6497074 (041fc8bf; Oct 2012), the race detector used -p to
detect packages that should not have race annotations, such as
runtime/race and sync/atomic.
In CL 13367052 (7276c02b; Sep 2013), a bug fix used -p to detect the
compilation of package reflect.
In CL 30539 (8aadcc55; Oct 2016), the compiler started using -p to
identify package math, to be able to intrinsify calls to Sqrt inside
that package.
In CL 61019 (9daee931; Sep 2017), CL 71430 (2c1d2e06; Oct 2017), and
later related CLs, the compiler started using the -p value when
creating various DWARF debugging information.
In CL 174657 (cc5eaf93; May 2019), the compiler started writing
symbols without the magic empty string whenever -p was used, to reduce
the amount of work required in the linker.
In CL 179861 (dde7c770; Jun 2019), the compiler made the second
argument to //go:linkname optional when -p is used, because in that
case the compiler can derive an appropriate default.
There are more examples. Today it is impossible to compile the Go
standard library without using -p, and DWARF debug information is
incomplete without using -p.
All known Go build systems pass -p. In particular, the go command
does, which is what nearly all Go developers invoke to build Go code.
And Bazel does, for go_library rules that set the importpath
attribute, which is all rules generated by Gazelle.
Gccgo has an equivalent of -p and has required its use in order to
disambiguate packages with the same name but different import paths
since 2010.
On top of all this, various parts of code generation for generics
are made more complicated by needing to cope with the case where -p
is not specified, even though it's essentially always specified.
In summary, the current state is:
- Use of the -p flag with cmd/compile is required for building
the standard library, and for complete DWARF information,
and to enable certain linker speedups.
- The go command and Bazel, which we expect account for just
about 100% of Go builds, both invoke cmd/compile with -p.
- The code in cmd/compile to support builds without -p is
complex and has become more complex with generics, but it is
almost always dead code and therefore not worth maintaining.
- Gccgo already requires its equivalent of -p in any build
where two packages have the same name.
All this supports the change in this CL, which makes -p required
and adjusts tests that invoke cmd/compile to add -p appropriately.
Future CLs will be able to remove all the code dealing with the
possibility of -p not having been specified.
Change-Id: I6b95b9d4cffe59c7bac82eb273ef6c4a67bb0e43
Reviewed-on: https://go-review.googlesource.com/c/go/+/391014
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2022-03-08 18:16:35 -05:00
|
|
|
type goDirPkg struct {
|
|
|
|
|
name string
|
|
|
|
|
files []string
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
// goDirPackages returns distinct Go packages in dir.
|
2016-06-21 15:33:04 -07:00
|
|
|
// If singlefilepkgs is set, each file is considered a separate package
|
|
|
|
|
// even if the package names are the same.
|
2023-02-06 17:10:22 -05:00
|
|
|
func goDirPackages(t *testing.T, dir string, singlefilepkgs bool) []*goDirPkg {
|
|
|
|
|
files, err := goDirFiles(dir)
|
2013-01-02 15:31:49 -05:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatal(err)
|
2013-01-02 15:31:49 -05:00
|
|
|
}
|
cmd/compile: require -p flag
The -p flag specifies the import path of the package being compiled.
This CL makes it required when invoking the compiler and
adjusts tests that invoke the compiler directly to conform to this
new requirement. The go command already passes the flag, so it
is unmodified in this CL. It is expected that any other Go build systems
also already pass -p, or else they will need to arrange to do so before
updating to Go 1.19. Of particular note, Bazel already does for rules
with an importpath= attribute, which includes all Gazelle-generated rules.
There is more cleanup possible now in cmd/compile, cmd/link,
and other consumers of Go object files, but that is left to future CLs.
Additional historical background follows but can be ignored.
Long ago, before the go command, or modules, or any kind of
versioning, symbols in Go archive files were named using just the
package name, so that for example func F in math/rand and func F in
crypto/rand would both be the object file symbol 'rand.F'. This led to
collisions even in small source trees, which made certain packages
unusable in the presence of other packages and generally was a problem
for Go's goal of scaling to very large source trees.
Fixing this problem required changing from package names to import
paths in symbol names, which was mostly straightforward. One wrinkle,
though, is that the compiler did not know the import path of the
package being compiled; it only knew the package name. At the time,
there was no go command, just Makefiles that people had invoking 6g
(now “go tool compile”) and then copying the resulting object file to
an importable location. That is, everyone had a custom build setup for
Go, because there was no standard one. So it was not particularly
attractive to change how the compiler was invoked, since that would
break approximately every Go user at the time. Instead, we arranged
for the compiler to emit, and other tools reading object files to
recognize, a special import path (the empty string, it turned out)
denoting “the import path of this object file”. This worked well
enough at the time and maintained complete command-line compatibility
with existing Go usage.
The changes implementing this transition can be found by searching
the Git history for “package global name space”, which is what they
eliminated. In particular, CL 190076 (a6736fa4), CL 186263 (758f2bc5),
CL 193080 (1cecac81), CL 194053 (19126320), and CL 194071 (531e6b77)
did the bulk of this transformation in January 2010.
Later, in September 2011, we added the -p flag to the compiler for
diagnostic purposes. The problem was that it was easy to create import
cycles, especially in tests, and these could not be diagnosed until
link time. You'd really want the compiler to diagnose these, for
example if the compilation of package sort noticed it was importing a
package that itself imported "sort". But the compilation of package
sort didn't know its own import path, and so it could not tell whether
it had found itself as a transitive dependency. Adding the -p flag
solved this problem, and its use was optional, since the linker would
still diagnose the import cycle in builds that had not updated to
start passing -p. This was CL 4972057 (1e480cd1).
There was still no go command at this point, but when we introduced
the go command we made it pass -p, which it has for many years at this
point.
Over time, parts of the compiler began to depend on the presence of
the -p flag for various reasonable purposes. For example:
In CL 6497074 (041fc8bf; Oct 2012), the race detector used -p to
detect packages that should not have race annotations, such as
runtime/race and sync/atomic.
In CL 13367052 (7276c02b; Sep 2013), a bug fix used -p to detect the
compilation of package reflect.
In CL 30539 (8aadcc55; Oct 2016), the compiler started using -p to
identify package math, to be able to intrinsify calls to Sqrt inside
that package.
In CL 61019 (9daee931; Sep 2017), CL 71430 (2c1d2e06; Oct 2017), and
later related CLs, the compiler started using the -p value when
creating various DWARF debugging information.
In CL 174657 (cc5eaf93; May 2019), the compiler started writing
symbols without the magic empty string whenever -p was used, to reduce
the amount of work required in the linker.
In CL 179861 (dde7c770; Jun 2019), the compiler made the second
argument to //go:linkname optional when -p is used, because in that
case the compiler can derive an appropriate default.
There are more examples. Today it is impossible to compile the Go
standard library without using -p, and DWARF debug information is
incomplete without using -p.
All known Go build systems pass -p. In particular, the go command
does, which is what nearly all Go developers invoke to build Go code.
And Bazel does, for go_library rules that set the importpath
attribute, which is all rules generated by Gazelle.
Gccgo has an equivalent of -p and has required its use in order to
disambiguate packages with the same name but different import paths
since 2010.
On top of all this, various parts of code generation for generics
are made more complicated by needing to cope with the case where -p
is not specified, even though it's essentially always specified.
In summary, the current state is:
- Use of the -p flag with cmd/compile is required for building
the standard library, and for complete DWARF information,
and to enable certain linker speedups.
- The go command and Bazel, which we expect account for just
about 100% of Go builds, both invoke cmd/compile with -p.
- The code in cmd/compile to support builds without -p is
complex and has become more complex with generics, but it is
almost always dead code and therefore not worth maintaining.
- Gccgo already requires its equivalent of -p in any build
where two packages have the same name.
All this supports the change in this CL, which makes -p required
and adjusts tests that invoke cmd/compile to add -p appropriately.
Future CLs will be able to remove all the code dealing with the
possibility of -p not having been specified.
Change-Id: I6b95b9d4cffe59c7bac82eb273ef6c4a67bb0e43
Reviewed-on: https://go-review.googlesource.com/c/go/+/391014
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2022-03-08 18:16:35 -05:00
|
|
|
var pkgs []*goDirPkg
|
|
|
|
|
m := make(map[string]*goDirPkg)
|
2013-01-02 15:31:49 -05:00
|
|
|
for _, file := range files {
|
|
|
|
|
name := file.Name()
|
2023-02-06 17:10:22 -05:00
|
|
|
pkgname, err := getPackageNameFromSource(filepath.Join(dir, name))
|
2020-02-09 01:40:45 -05:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatal(err)
|
2020-02-09 01:40:45 -05:00
|
|
|
}
|
cmd/compile: require -p flag
The -p flag specifies the import path of the package being compiled.
This CL makes it required when invoking the compiler and
adjusts tests that invoke the compiler directly to conform to this
new requirement. The go command already passes the flag, so it
is unmodified in this CL. It is expected that any other Go build systems
also already pass -p, or else they will need to arrange to do so before
updating to Go 1.19. Of particular note, Bazel already does for rules
with an importpath= attribute, which includes all Gazelle-generated rules.
There is more cleanup possible now in cmd/compile, cmd/link,
and other consumers of Go object files, but that is left to future CLs.
Additional historical background follows but can be ignored.
Long ago, before the go command, or modules, or any kind of
versioning, symbols in Go archive files were named using just the
package name, so that for example func F in math/rand and func F in
crypto/rand would both be the object file symbol 'rand.F'. This led to
collisions even in small source trees, which made certain packages
unusable in the presence of other packages and generally was a problem
for Go's goal of scaling to very large source trees.
Fixing this problem required changing from package names to import
paths in symbol names, which was mostly straightforward. One wrinkle,
though, is that the compiler did not know the import path of the
package being compiled; it only knew the package name. At the time,
there was no go command, just Makefiles that people had invoking 6g
(now “go tool compile”) and then copying the resulting object file to
an importable location. That is, everyone had a custom build setup for
Go, because there was no standard one. So it was not particularly
attractive to change how the compiler was invoked, since that would
break approximately every Go user at the time. Instead, we arranged
for the compiler to emit, and other tools reading object files to
recognize, a special import path (the empty string, it turned out)
denoting “the import path of this object file”. This worked well
enough at the time and maintained complete command-line compatibility
with existing Go usage.
The changes implementing this transition can be found by searching
the Git history for “package global name space”, which is what they
eliminated. In particular, CL 190076 (a6736fa4), CL 186263 (758f2bc5),
CL 193080 (1cecac81), CL 194053 (19126320), and CL 194071 (531e6b77)
did the bulk of this transformation in January 2010.
Later, in September 2011, we added the -p flag to the compiler for
diagnostic purposes. The problem was that it was easy to create import
cycles, especially in tests, and these could not be diagnosed until
link time. You'd really want the compiler to diagnose these, for
example if the compilation of package sort noticed it was importing a
package that itself imported "sort". But the compilation of package
sort didn't know its own import path, and so it could not tell whether
it had found itself as a transitive dependency. Adding the -p flag
solved this problem, and its use was optional, since the linker would
still diagnose the import cycle in builds that had not updated to
start passing -p. This was CL 4972057 (1e480cd1).
There was still no go command at this point, but when we introduced
the go command we made it pass -p, which it has for many years at this
point.
Over time, parts of the compiler began to depend on the presence of
the -p flag for various reasonable purposes. For example:
In CL 6497074 (041fc8bf; Oct 2012), the race detector used -p to
detect packages that should not have race annotations, such as
runtime/race and sync/atomic.
In CL 13367052 (7276c02b; Sep 2013), a bug fix used -p to detect the
compilation of package reflect.
In CL 30539 (8aadcc55; Oct 2016), the compiler started using -p to
identify package math, to be able to intrinsify calls to Sqrt inside
that package.
In CL 61019 (9daee931; Sep 2017), CL 71430 (2c1d2e06; Oct 2017), and
later related CLs, the compiler started using the -p value when
creating various DWARF debugging information.
In CL 174657 (cc5eaf93; May 2019), the compiler started writing
symbols without the magic empty string whenever -p was used, to reduce
the amount of work required in the linker.
In CL 179861 (dde7c770; Jun 2019), the compiler made the second
argument to //go:linkname optional when -p is used, because in that
case the compiler can derive an appropriate default.
There are more examples. Today it is impossible to compile the Go
standard library without using -p, and DWARF debug information is
incomplete without using -p.
All known Go build systems pass -p. In particular, the go command
does, which is what nearly all Go developers invoke to build Go code.
And Bazel does, for go_library rules that set the importpath
attribute, which is all rules generated by Gazelle.
Gccgo has an equivalent of -p and has required its use in order to
disambiguate packages with the same name but different import paths
since 2010.
On top of all this, various parts of code generation for generics
are made more complicated by needing to cope with the case where -p
is not specified, even though it's essentially always specified.
In summary, the current state is:
- Use of the -p flag with cmd/compile is required for building
the standard library, and for complete DWARF information,
and to enable certain linker speedups.
- The go command and Bazel, which we expect account for just
about 100% of Go builds, both invoke cmd/compile with -p.
- The code in cmd/compile to support builds without -p is
complex and has become more complex with generics, but it is
almost always dead code and therefore not worth maintaining.
- Gccgo already requires its equivalent of -p in any build
where two packages have the same name.
All this supports the change in this CL, which makes -p required
and adjusts tests that invoke cmd/compile to add -p appropriately.
Future CLs will be able to remove all the code dealing with the
possibility of -p not having been specified.
Change-Id: I6b95b9d4cffe59c7bac82eb273ef6c4a67bb0e43
Reviewed-on: https://go-review.googlesource.com/c/go/+/391014
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2022-03-08 18:16:35 -05:00
|
|
|
p, ok := m[pkgname]
|
2016-06-21 15:33:04 -07:00
|
|
|
if singlefilepkgs || !ok {
|
cmd/compile: require -p flag
The -p flag specifies the import path of the package being compiled.
This CL makes it required when invoking the compiler and
adjusts tests that invoke the compiler directly to conform to this
new requirement. The go command already passes the flag, so it
is unmodified in this CL. It is expected that any other Go build systems
also already pass -p, or else they will need to arrange to do so before
updating to Go 1.19. Of particular note, Bazel already does for rules
with an importpath= attribute, which includes all Gazelle-generated rules.
There is more cleanup possible now in cmd/compile, cmd/link,
and other consumers of Go object files, but that is left to future CLs.
Additional historical background follows but can be ignored.
Long ago, before the go command, or modules, or any kind of
versioning, symbols in Go archive files were named using just the
package name, so that for example func F in math/rand and func F in
crypto/rand would both be the object file symbol 'rand.F'. This led to
collisions even in small source trees, which made certain packages
unusable in the presence of other packages and generally was a problem
for Go's goal of scaling to very large source trees.
Fixing this problem required changing from package names to import
paths in symbol names, which was mostly straightforward. One wrinkle,
though, is that the compiler did not know the import path of the
package being compiled; it only knew the package name. At the time,
there was no go command, just Makefiles that people had invoking 6g
(now “go tool compile”) and then copying the resulting object file to
an importable location. That is, everyone had a custom build setup for
Go, because there was no standard one. So it was not particularly
attractive to change how the compiler was invoked, since that would
break approximately every Go user at the time. Instead, we arranged
for the compiler to emit, and other tools reading object files to
recognize, a special import path (the empty string, it turned out)
denoting “the import path of this object file”. This worked well
enough at the time and maintained complete command-line compatibility
with existing Go usage.
The changes implementing this transition can be found by searching
the Git history for “package global name space”, which is what they
eliminated. In particular, CL 190076 (a6736fa4), CL 186263 (758f2bc5),
CL 193080 (1cecac81), CL 194053 (19126320), and CL 194071 (531e6b77)
did the bulk of this transformation in January 2010.
Later, in September 2011, we added the -p flag to the compiler for
diagnostic purposes. The problem was that it was easy to create import
cycles, especially in tests, and these could not be diagnosed until
link time. You'd really want the compiler to diagnose these, for
example if the compilation of package sort noticed it was importing a
package that itself imported "sort". But the compilation of package
sort didn't know its own import path, and so it could not tell whether
it had found itself as a transitive dependency. Adding the -p flag
solved this problem, and its use was optional, since the linker would
still diagnose the import cycle in builds that had not updated to
start passing -p. This was CL 4972057 (1e480cd1).
There was still no go command at this point, but when we introduced
the go command we made it pass -p, which it has for many years at this
point.
Over time, parts of the compiler began to depend on the presence of
the -p flag for various reasonable purposes. For example:
In CL 6497074 (041fc8bf; Oct 2012), the race detector used -p to
detect packages that should not have race annotations, such as
runtime/race and sync/atomic.
In CL 13367052 (7276c02b; Sep 2013), a bug fix used -p to detect the
compilation of package reflect.
In CL 30539 (8aadcc55; Oct 2016), the compiler started using -p to
identify package math, to be able to intrinsify calls to Sqrt inside
that package.
In CL 61019 (9daee931; Sep 2017), CL 71430 (2c1d2e06; Oct 2017), and
later related CLs, the compiler started using the -p value when
creating various DWARF debugging information.
In CL 174657 (cc5eaf93; May 2019), the compiler started writing
symbols without the magic empty string whenever -p was used, to reduce
the amount of work required in the linker.
In CL 179861 (dde7c770; Jun 2019), the compiler made the second
argument to //go:linkname optional when -p is used, because in that
case the compiler can derive an appropriate default.
There are more examples. Today it is impossible to compile the Go
standard library without using -p, and DWARF debug information is
incomplete without using -p.
All known Go build systems pass -p. In particular, the go command
does, which is what nearly all Go developers invoke to build Go code.
And Bazel does, for go_library rules that set the importpath
attribute, which is all rules generated by Gazelle.
Gccgo has an equivalent of -p and has required its use in order to
disambiguate packages with the same name but different import paths
since 2010.
On top of all this, various parts of code generation for generics
are made more complicated by needing to cope with the case where -p
is not specified, even though it's essentially always specified.
In summary, the current state is:
- Use of the -p flag with cmd/compile is required for building
the standard library, and for complete DWARF information,
and to enable certain linker speedups.
- The go command and Bazel, which we expect account for just
about 100% of Go builds, both invoke cmd/compile with -p.
- The code in cmd/compile to support builds without -p is
complex and has become more complex with generics, but it is
almost always dead code and therefore not worth maintaining.
- Gccgo already requires its equivalent of -p in any build
where two packages have the same name.
All this supports the change in this CL, which makes -p required
and adjusts tests that invoke cmd/compile to add -p appropriately.
Future CLs will be able to remove all the code dealing with the
possibility of -p not having been specified.
Change-Id: I6b95b9d4cffe59c7bac82eb273ef6c4a67bb0e43
Reviewed-on: https://go-review.googlesource.com/c/go/+/391014
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2022-03-08 18:16:35 -05:00
|
|
|
p = &goDirPkg{name: pkgname}
|
|
|
|
|
pkgs = append(pkgs, p)
|
|
|
|
|
m[pkgname] = p
|
2013-01-02 15:31:49 -05:00
|
|
|
}
|
cmd/compile: require -p flag
The -p flag specifies the import path of the package being compiled.
This CL makes it required when invoking the compiler and
adjusts tests that invoke the compiler directly to conform to this
new requirement. The go command already passes the flag, so it
is unmodified in this CL. It is expected that any other Go build systems
also already pass -p, or else they will need to arrange to do so before
updating to Go 1.19. Of particular note, Bazel already does for rules
with an importpath= attribute, which includes all Gazelle-generated rules.
There is more cleanup possible now in cmd/compile, cmd/link,
and other consumers of Go object files, but that is left to future CLs.
Additional historical background follows but can be ignored.
Long ago, before the go command, or modules, or any kind of
versioning, symbols in Go archive files were named using just the
package name, so that for example func F in math/rand and func F in
crypto/rand would both be the object file symbol 'rand.F'. This led to
collisions even in small source trees, which made certain packages
unusable in the presence of other packages and generally was a problem
for Go's goal of scaling to very large source trees.
Fixing this problem required changing from package names to import
paths in symbol names, which was mostly straightforward. One wrinkle,
though, is that the compiler did not know the import path of the
package being compiled; it only knew the package name. At the time,
there was no go command, just Makefiles that people had invoking 6g
(now “go tool compile”) and then copying the resulting object file to
an importable location. That is, everyone had a custom build setup for
Go, because there was no standard one. So it was not particularly
attractive to change how the compiler was invoked, since that would
break approximately every Go user at the time. Instead, we arranged
for the compiler to emit, and other tools reading object files to
recognize, a special import path (the empty string, it turned out)
denoting “the import path of this object file”. This worked well
enough at the time and maintained complete command-line compatibility
with existing Go usage.
The changes implementing this transition can be found by searching
the Git history for “package global name space”, which is what they
eliminated. In particular, CL 190076 (a6736fa4), CL 186263 (758f2bc5),
CL 193080 (1cecac81), CL 194053 (19126320), and CL 194071 (531e6b77)
did the bulk of this transformation in January 2010.
Later, in September 2011, we added the -p flag to the compiler for
diagnostic purposes. The problem was that it was easy to create import
cycles, especially in tests, and these could not be diagnosed until
link time. You'd really want the compiler to diagnose these, for
example if the compilation of package sort noticed it was importing a
package that itself imported "sort". But the compilation of package
sort didn't know its own import path, and so it could not tell whether
it had found itself as a transitive dependency. Adding the -p flag
solved this problem, and its use was optional, since the linker would
still diagnose the import cycle in builds that had not updated to
start passing -p. This was CL 4972057 (1e480cd1).
There was still no go command at this point, but when we introduced
the go command we made it pass -p, which it has for many years at this
point.
Over time, parts of the compiler began to depend on the presence of
the -p flag for various reasonable purposes. For example:
In CL 6497074 (041fc8bf; Oct 2012), the race detector used -p to
detect packages that should not have race annotations, such as
runtime/race and sync/atomic.
In CL 13367052 (7276c02b; Sep 2013), a bug fix used -p to detect the
compilation of package reflect.
In CL 30539 (8aadcc55; Oct 2016), the compiler started using -p to
identify package math, to be able to intrinsify calls to Sqrt inside
that package.
In CL 61019 (9daee931; Sep 2017), CL 71430 (2c1d2e06; Oct 2017), and
later related CLs, the compiler started using the -p value when
creating various DWARF debugging information.
In CL 174657 (cc5eaf93; May 2019), the compiler started writing
symbols without the magic empty string whenever -p was used, to reduce
the amount of work required in the linker.
In CL 179861 (dde7c770; Jun 2019), the compiler made the second
argument to //go:linkname optional when -p is used, because in that
case the compiler can derive an appropriate default.
There are more examples. Today it is impossible to compile the Go
standard library without using -p, and DWARF debug information is
incomplete without using -p.
All known Go build systems pass -p. In particular, the go command
does, which is what nearly all Go developers invoke to build Go code.
And Bazel does, for go_library rules that set the importpath
attribute, which is all rules generated by Gazelle.
Gccgo has an equivalent of -p and has required its use in order to
disambiguate packages with the same name but different import paths
since 2010.
On top of all this, various parts of code generation for generics
are made more complicated by needing to cope with the case where -p
is not specified, even though it's essentially always specified.
In summary, the current state is:
- Use of the -p flag with cmd/compile is required for building
the standard library, and for complete DWARF information,
and to enable certain linker speedups.
- The go command and Bazel, which we expect account for just
about 100% of Go builds, both invoke cmd/compile with -p.
- The code in cmd/compile to support builds without -p is
complex and has become more complex with generics, but it is
almost always dead code and therefore not worth maintaining.
- Gccgo already requires its equivalent of -p in any build
where two packages have the same name.
All this supports the change in this CL, which makes -p required
and adjusts tests that invoke cmd/compile to add -p appropriately.
Future CLs will be able to remove all the code dealing with the
possibility of -p not having been specified.
Change-Id: I6b95b9d4cffe59c7bac82eb273ef6c4a67bb0e43
Reviewed-on: https://go-review.googlesource.com/c/go/+/391014
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2022-03-08 18:16:35 -05:00
|
|
|
p.files = append(p.files, name)
|
2013-01-02 15:31:49 -05:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return pkgs
|
2013-01-02 15:31:49 -05:00
|
|
|
}
|
2013-01-11 22:00:48 +01:00
|
|
|
|
2013-08-13 12:25:41 -04:00
|
|
|
type context struct {
|
2021-03-24 13:16:42 -07:00
|
|
|
GOOS string
|
|
|
|
|
GOARCH string
|
|
|
|
|
cgoEnabled bool
|
|
|
|
|
noOptEnv bool
|
2013-08-13 12:25:41 -04:00
|
|
|
}
|
|
|
|
|
|
2013-01-28 21:29:45 +01:00
|
|
|
// shouldTest looks for build tags in a source file and returns
|
|
|
|
|
// whether the file should be used according to the tags.
|
|
|
|
|
func shouldTest(src string, goos, goarch string) (ok bool, whyNot string) {
|
2015-10-26 17:34:06 -04:00
|
|
|
if *runSkips {
|
|
|
|
|
return true, ""
|
|
|
|
|
}
|
2013-01-28 21:29:45 +01:00
|
|
|
for _, line := range strings.Split(src, "\n") {
|
2022-04-11 17:35:24 -07:00
|
|
|
if strings.HasPrefix(line, "package ") {
|
|
|
|
|
break
|
2013-08-13 12:25:41 -04:00
|
|
|
}
|
2018-09-24 16:48:54 +00:00
|
|
|
|
2022-04-11 17:35:24 -07:00
|
|
|
if expr, err := constraint.Parse(line); err == nil {
|
|
|
|
|
gcFlags := os.Getenv("GO_GCFLAGS")
|
|
|
|
|
ctxt := &context{
|
|
|
|
|
GOOS: goos,
|
|
|
|
|
GOARCH: goarch,
|
|
|
|
|
cgoEnabled: cgoEnabled,
|
|
|
|
|
noOptEnv: strings.Contains(gcFlags, "-N") || strings.Contains(gcFlags, "-l"),
|
2013-01-28 21:29:45 +01:00
|
|
|
}
|
2022-04-11 17:35:24 -07:00
|
|
|
|
|
|
|
|
if !expr.Eval(ctxt.match) {
|
2013-08-13 12:25:41 -04:00
|
|
|
return false, line
|
|
|
|
|
}
|
2013-01-28 21:29:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true, ""
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-13 12:25:41 -04:00
|
|
|
func (ctxt *context) match(name string) bool {
|
|
|
|
|
if name == "" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tags must be letters, digits, underscores or dots.
|
|
|
|
|
// Unlike in Go identifiers, all digits are fine (e.g., "386").
|
|
|
|
|
for _, c := range name {
|
|
|
|
|
if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-18 00:26:42 +07:00
|
|
|
if slices.Contains(build.Default.ReleaseTags, name) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 13:25:31 -04:00
|
|
|
if strings.HasPrefix(name, "goexperiment.") {
|
2023-10-18 00:26:42 +07:00
|
|
|
return slices.Contains(build.Default.ToolTags, name)
|
2021-02-24 12:55:52 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-24 13:16:42 -07:00
|
|
|
if name == "cgo" && ctxt.cgoEnabled {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-17 14:03:07 -08:00
|
|
|
if name == ctxt.GOOS || name == ctxt.GOARCH || name == "gc" {
|
2013-08-13 12:25:41 -04:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-24 16:48:54 +00:00
|
|
|
if ctxt.noOptEnv && name == "gcflags_noopt" {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 22:51:03 -05:00
|
|
|
if name == "test_run" {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-13 12:25:41 -04:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-30 15:28:11 -04:00
|
|
|
// goGcflags returns the -gcflags argument to use with go build / go run.
|
2018-08-23 05:06:47 +00:00
|
|
|
// This must match the flags used for building the standard library,
|
2017-10-30 15:28:11 -04:00
|
|
|
// or else the commands will rebuild any needed packages (like runtime)
|
|
|
|
|
// over and over.
|
2023-02-06 17:10:22 -05:00
|
|
|
func (test) goGcflags() string {
|
2022-02-28 14:32:19 -08:00
|
|
|
return "-gcflags=all=" + os.Getenv("GO_GCFLAGS")
|
2017-10-30 15:28:11 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
func (test) goGcflagsIsEmpty() bool {
|
2022-02-28 14:32:19 -08:00
|
|
|
return "" == os.Getenv("GO_GCFLAGS")
|
2020-03-11 20:17:14 -04:00
|
|
|
}
|
|
|
|
|
|
2021-01-07 11:22:42 -05:00
|
|
|
var errTimeout = errors.New("command exceeded time limit")
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
// run runs the test case.
|
|
|
|
|
//
|
|
|
|
|
// When there is a problem, run uses t.Fatal to signify that it's an unskippable
|
|
|
|
|
// infrastructure error (such as failing to read an input file or the test recipe
|
|
|
|
|
// being malformed), or it returns a non-nil error to signify a test case error.
|
|
|
|
|
//
|
|
|
|
|
// t.Error isn't used here to give the caller the opportunity to decide whether
|
|
|
|
|
// the test case failing is expected before promoting it to a real test failure.
|
|
|
|
|
// See expectFail and -f flag.
|
|
|
|
|
func (t test) run() error {
|
|
|
|
|
srcBytes, err := os.ReadFile(filepath.Join(t.gorootTestDir, t.goFileName()))
|
2012-02-21 14:28:49 +11:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatal("reading test case .go file:", err)
|
2023-02-08 11:40:06 -05:00
|
|
|
} else if bytes.HasPrefix(srcBytes, []byte{'\n'}) {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatal(".go file source starts with a newline")
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
2023-02-08 11:40:06 -05:00
|
|
|
src := string(srcBytes)
|
2015-07-10 12:32:03 -04:00
|
|
|
|
2023-08-24 13:16:48 -04:00
|
|
|
// Execution recipe is contained in a comment in
|
|
|
|
|
// the first non-empty line that is not a build constraint.
|
|
|
|
|
var action string
|
|
|
|
|
for actionSrc := src; action == "" && actionSrc != ""; {
|
|
|
|
|
var line string
|
|
|
|
|
line, actionSrc, _ = strings.Cut(actionSrc, "\n")
|
|
|
|
|
if constraint.IsGoBuild(line) || constraint.IsPlusBuild(line) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
action = strings.TrimSpace(strings.TrimPrefix(line, "//"))
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
2023-08-24 13:16:48 -04:00
|
|
|
if action == "" {
|
|
|
|
|
t.Fatalf("execution recipe not found in GOROOT/test/%s", t.goFileName())
|
2013-01-28 21:29:45 +01:00
|
|
|
}
|
2012-03-08 14:03:40 -05:00
|
|
|
|
2015-07-10 12:32:03 -04:00
|
|
|
// Check for build constraints only up to the actual code.
|
2023-02-08 11:40:06 -05:00
|
|
|
header, _, ok := strings.Cut(src, "\npackage")
|
2021-09-22 10:46:32 -04:00
|
|
|
if !ok {
|
|
|
|
|
header = action // some files are intentionally malformed
|
2015-07-10 12:32:03 -04:00
|
|
|
}
|
2021-09-22 10:46:32 -04:00
|
|
|
if ok, why := shouldTest(header, goos, goarch); !ok {
|
2023-02-08 11:40:06 -05:00
|
|
|
t.Skip(why)
|
2015-07-10 12:32:03 -04:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 11:35:45 -04:00
|
|
|
var args, flags, runenv []string
|
2016-11-10 16:03:47 -05:00
|
|
|
var tim int
|
2012-09-23 13:16:14 -04:00
|
|
|
wantError := false
|
2016-09-22 13:50:16 -04:00
|
|
|
wantAuto := false
|
2016-06-21 15:33:04 -07:00
|
|
|
singlefilepkgs := false
|
2021-06-11 09:54:40 -07:00
|
|
|
f, err := splitQuoted(action)
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatal("invalid test recipe:", err)
|
2021-06-11 09:54:40 -07:00
|
|
|
}
|
2012-03-07 01:54:39 -05:00
|
|
|
if len(f) > 0 {
|
|
|
|
|
action = f[0]
|
|
|
|
|
args = f[1:]
|
|
|
|
|
}
|
2012-02-21 14:28:49 +11:00
|
|
|
|
2016-06-21 15:33:04 -07:00
|
|
|
// TODO: Clean up/simplify this switch statement.
|
2012-02-21 14:28:49 +11:00
|
|
|
switch action {
|
2019-05-17 17:25:07 +07:00
|
|
|
case "compile", "compiledir", "build", "builddir", "buildrundir", "run", "buildrun", "runoutput", "rundir", "runindir", "asmcheck":
|
2016-09-22 13:50:16 -04:00
|
|
|
// nothing to do
|
2016-03-11 00:10:52 -05:00
|
|
|
case "errorcheckandrundir":
|
|
|
|
|
wantError = false // should be no error if also will run
|
2016-09-22 13:50:16 -04:00
|
|
|
case "errorcheckwithauto":
|
|
|
|
|
action = "errorcheck"
|
|
|
|
|
wantAuto = true
|
|
|
|
|
wantError = true
|
2012-11-07 12:33:54 -08:00
|
|
|
case "errorcheck", "errorcheckdir", "errorcheckoutput":
|
2012-09-23 13:16:14 -04:00
|
|
|
wantError = true
|
2012-03-22 02:14:44 +08:00
|
|
|
case "skip":
|
2015-10-26 17:34:06 -04:00
|
|
|
if *runSkips {
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-02-08 11:40:06 -05:00
|
|
|
t.Skip("skip")
|
2012-02-21 14:28:49 +11:00
|
|
|
default:
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatalf("unknown pattern: %q", action)
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
goexp := goExperiment
|
2024-02-29 20:17:09 +07:00
|
|
|
godebug := goDebug
|
2024-08-09 13:32:12 -07:00
|
|
|
gomodvers := ""
|
2021-07-02 15:42:20 -07:00
|
|
|
|
2016-06-21 15:33:04 -07:00
|
|
|
// collect flags
|
|
|
|
|
for len(args) > 0 && strings.HasPrefix(args[0], "-") {
|
|
|
|
|
switch args[0] {
|
2018-05-26 12:57:50 +03:00
|
|
|
case "-1":
|
|
|
|
|
wantError = true
|
2016-06-21 15:33:04 -07:00
|
|
|
case "-0":
|
|
|
|
|
wantError = false
|
|
|
|
|
case "-s":
|
|
|
|
|
singlefilepkgs = true
|
2016-11-10 16:03:47 -05:00
|
|
|
case "-t": // timeout in seconds
|
|
|
|
|
args = args[1:]
|
|
|
|
|
var err error
|
|
|
|
|
tim, err = strconv.Atoi(args[0])
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatalf("need number of seconds for -t timeout, got %s instead", args[0])
|
2016-11-10 16:03:47 -05:00
|
|
|
}
|
2022-02-02 14:09:26 -05:00
|
|
|
if s := os.Getenv("GO_TEST_TIMEOUT_SCALE"); s != "" {
|
|
|
|
|
timeoutScale, err := strconv.Atoi(s)
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatalf("failed to parse $GO_TEST_TIMEOUT_SCALE = %q as integer: %v", s, err)
|
2022-02-02 14:09:26 -05:00
|
|
|
}
|
|
|
|
|
tim *= timeoutScale
|
|
|
|
|
}
|
2021-03-18 11:35:45 -04:00
|
|
|
case "-goexperiment": // set GOEXPERIMENT environment
|
|
|
|
|
args = args[1:]
|
2021-07-02 15:42:20 -07:00
|
|
|
if goexp != "" {
|
|
|
|
|
goexp += ","
|
|
|
|
|
}
|
|
|
|
|
goexp += args[0]
|
|
|
|
|
runenv = append(runenv, "GOEXPERIMENT="+goexp)
|
2016-11-10 16:03:47 -05:00
|
|
|
|
2024-02-29 20:17:09 +07:00
|
|
|
case "-godebug": // set GODEBUG environment
|
|
|
|
|
args = args[1:]
|
|
|
|
|
if godebug != "" {
|
|
|
|
|
godebug += ","
|
|
|
|
|
}
|
|
|
|
|
godebug += args[0]
|
|
|
|
|
runenv = append(runenv, "GODEBUG="+godebug)
|
|
|
|
|
|
2024-08-09 13:32:12 -07:00
|
|
|
case "-gomodversion": // set the GoVersion in generated go.mod files (just runindir ATM)
|
|
|
|
|
args = args[1:]
|
|
|
|
|
gomodvers = args[0]
|
|
|
|
|
|
2016-06-21 15:33:04 -07:00
|
|
|
default:
|
|
|
|
|
flags = append(flags, args[0])
|
|
|
|
|
}
|
|
|
|
|
args = args[1:]
|
|
|
|
|
}
|
2016-12-05 15:41:04 -08:00
|
|
|
if action == "errorcheck" {
|
|
|
|
|
found := false
|
|
|
|
|
for i, f := range flags {
|
|
|
|
|
if strings.HasPrefix(f, "-d=") {
|
|
|
|
|
flags[i] = f + ",ssa/check/on"
|
|
|
|
|
found = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
flags = append(flags, "-d=ssa/check/on")
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-21 15:33:04 -07:00
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
tempDir := t.TempDir()
|
|
|
|
|
err = os.Mkdir(filepath.Join(tempDir, "test"), 0755)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
2016-03-11 00:10:52 -05:00
|
|
|
}
|
2012-02-21 14:28:49 +11:00
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
err = os.WriteFile(filepath.Join(tempDir, t.goFile), srcBytes, 0644)
|
2020-02-09 01:40:45 -05:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatal(err)
|
2020-02-09 01:40:45 -05:00
|
|
|
}
|
2012-03-08 14:03:40 -05:00
|
|
|
|
2020-03-24 16:18:02 -04:00
|
|
|
var (
|
2023-02-06 17:10:22 -05:00
|
|
|
runInDir = tempDir
|
2020-03-24 16:18:02 -04:00
|
|
|
tempDirIsGOPATH = false
|
|
|
|
|
)
|
2012-03-07 01:54:39 -05:00
|
|
|
runcmd := func(args ...string) ([]byte, error) {
|
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
cmd.Stdout = &buf
|
|
|
|
|
cmd.Stderr = &buf
|
2020-03-24 16:18:02 -04:00
|
|
|
cmd.Env = append(os.Environ(), "GOENV=off", "GOFLAGS=")
|
|
|
|
|
if runInDir != "" {
|
|
|
|
|
cmd.Dir = runInDir
|
|
|
|
|
// Set PWD to match Dir to speed up os.Getwd in the child process.
|
|
|
|
|
cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
|
2023-02-06 17:10:22 -05:00
|
|
|
} else {
|
|
|
|
|
// Default to running in the GOROOT/test directory.
|
|
|
|
|
cmd.Dir = t.gorootTestDir
|
|
|
|
|
// Set PWD to match Dir to speed up os.Getwd in the child process.
|
|
|
|
|
cmd.Env = append(cmd.Env, "PWD="+cmd.Dir)
|
2019-05-17 17:25:07 +07:00
|
|
|
}
|
2020-03-24 16:18:02 -04:00
|
|
|
if tempDirIsGOPATH {
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd.Env = append(cmd.Env, "GOPATH="+tempDir)
|
2015-09-06 19:39:07 -07:00
|
|
|
}
|
2022-09-21 15:51:27 -04:00
|
|
|
cmd.Env = append(cmd.Env, "STDLIB_IMPORTCFG="+stdlibImportcfgFile())
|
2021-03-18 11:35:45 -04:00
|
|
|
cmd.Env = append(cmd.Env, runenv...)
|
2016-11-10 16:03:47 -05:00
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if tim != 0 {
|
|
|
|
|
err = cmd.Start()
|
|
|
|
|
// This command-timeout code adapted from cmd/go/test.go
|
2022-02-02 14:09:26 -05:00
|
|
|
// Note: the Go command uses a more sophisticated timeout
|
|
|
|
|
// strategy, first sending SIGQUIT (if appropriate for the
|
|
|
|
|
// OS in question) to try to trigger a stack trace, then
|
|
|
|
|
// finally much later SIGKILL. If timeouts prove to be a
|
|
|
|
|
// common problem here, it would be worth porting over
|
|
|
|
|
// that code as well. See https://do.dev/issue/50973
|
|
|
|
|
// for more discussion.
|
2016-11-10 16:03:47 -05:00
|
|
|
if err == nil {
|
|
|
|
|
tick := time.NewTimer(time.Duration(tim) * time.Second)
|
|
|
|
|
done := make(chan error)
|
|
|
|
|
go func() {
|
|
|
|
|
done <- cmd.Wait()
|
|
|
|
|
}()
|
|
|
|
|
select {
|
|
|
|
|
case err = <-done:
|
|
|
|
|
// ok
|
|
|
|
|
case <-tick.C:
|
2021-01-07 11:22:42 -05:00
|
|
|
cmd.Process.Signal(os.Interrupt)
|
|
|
|
|
time.Sleep(1 * time.Second)
|
2016-11-10 16:03:47 -05:00
|
|
|
cmd.Process.Kill()
|
2021-01-07 11:22:42 -05:00
|
|
|
<-done
|
|
|
|
|
err = errTimeout
|
2016-11-10 16:03:47 -05:00
|
|
|
}
|
|
|
|
|
tick.Stop()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
err = cmd.Run()
|
|
|
|
|
}
|
2021-01-07 11:22:42 -05:00
|
|
|
if err != nil && err != errTimeout {
|
2012-10-06 09:23:31 +02:00
|
|
|
err = fmt.Errorf("%s\n%s", err, buf.Bytes())
|
|
|
|
|
}
|
2012-03-07 01:54:39 -05:00
|
|
|
return buf.Bytes(), err
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
importcfg := func(pkgs []*goDirPkg) string {
|
2022-09-21 15:51:27 -04:00
|
|
|
cfg := stdlibImportcfg()
|
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
|
pkgpath := path.Join("test", strings.TrimSuffix(pkg.files[0], ".go"))
|
2023-02-06 17:10:22 -05:00
|
|
|
cfg += "\npackagefile " + pkgpath + "=" + filepath.Join(tempDir, pkgpath+".a")
|
|
|
|
|
}
|
|
|
|
|
filename := filepath.Join(tempDir, "importcfg")
|
|
|
|
|
err := os.WriteFile(filename, []byte(cfg), 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
2022-09-21 15:51:27 -04:00
|
|
|
}
|
|
|
|
|
return filename
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
long := filepath.Join(t.gorootTestDir, t.goFileName())
|
2012-03-08 14:03:40 -05:00
|
|
|
switch action {
|
2012-03-07 01:54:39 -05:00
|
|
|
default:
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatalf("unimplemented action %q", action)
|
|
|
|
|
panic("unreachable")
|
2012-02-21 14:28:49 +11:00
|
|
|
|
2018-02-27 01:59:58 +01:00
|
|
|
case "asmcheck":
|
2018-06-01 13:14:48 +03:00
|
|
|
// Compile Go file and match the generated assembly
|
|
|
|
|
// against a set of regexps in comments.
|
2018-04-15 19:00:27 +02:00
|
|
|
ops := t.wantedAsmOpcodes(long)
|
2019-05-16 00:12:34 -04:00
|
|
|
self := runtime.GOOS + "/" + runtime.GOARCH
|
2018-04-15 19:00:27 +02:00
|
|
|
for _, env := range ops.Envs() {
|
2019-05-16 00:12:34 -04:00
|
|
|
// Only run checks relevant to the current GOOS/GOARCH,
|
|
|
|
|
// to avoid triggering a cross-compile of the runtime.
|
|
|
|
|
if string(env) != self && !strings.HasPrefix(string(env), self+"/") && !*allCodegen {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2018-12-07 10:00:36 -08:00
|
|
|
// -S=2 forces outermost line numbers when disassembling inlined code.
|
|
|
|
|
cmdline := []string{"build", "-gcflags", "-S=2"}
|
2020-03-10 13:45:19 -04:00
|
|
|
|
|
|
|
|
// Append flags, but don't override -gcflags=-S=2; add to it instead.
|
|
|
|
|
for i := 0; i < len(flags); i++ {
|
|
|
|
|
flag := flags[i]
|
|
|
|
|
switch {
|
|
|
|
|
case strings.HasPrefix(flag, "-gcflags="):
|
|
|
|
|
cmdline[2] += " " + strings.TrimPrefix(flag, "-gcflags=")
|
|
|
|
|
case strings.HasPrefix(flag, "--gcflags="):
|
|
|
|
|
cmdline[2] += " " + strings.TrimPrefix(flag, "--gcflags=")
|
|
|
|
|
case flag == "-gcflags", flag == "--gcflags":
|
|
|
|
|
i++
|
|
|
|
|
if i < len(flags) {
|
|
|
|
|
cmdline[2] += " " + flags[i]
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
cmdline = append(cmdline, flag)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-27 01:59:58 +01:00
|
|
|
cmdline = append(cmdline, long)
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd := exec.Command(goTool, cmdline...)
|
2018-04-15 19:00:27 +02:00
|
|
|
cmd.Env = append(os.Environ(), env.Environ()...)
|
2019-04-03 13:16:58 -07:00
|
|
|
if len(flags) > 0 && flags[0] == "-race" {
|
|
|
|
|
cmd.Env = append(cmd.Env, "CGO_ENABLED=1")
|
|
|
|
|
}
|
2018-03-29 18:52:12 +02:00
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
cmd.Stdout, cmd.Stderr = &buf, &buf
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Log(env, "\n", cmd.Stderr)
|
|
|
|
|
return err
|
2018-02-27 01:59:58 +01:00
|
|
|
}
|
2018-03-29 18:52:12 +02:00
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
err := t.asmCheck(buf.String(), long, env, ops[env])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2018-02-27 01:59:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return nil
|
2018-02-27 01:59:58 +01:00
|
|
|
|
2012-03-07 01:54:39 -05:00
|
|
|
case "errorcheck":
|
2018-06-01 13:14:48 +03:00
|
|
|
// Compile Go file.
|
|
|
|
|
// Fail if wantError is true and compilation was successful and vice versa.
|
|
|
|
|
// Match errors produced by gc against errors in comments.
|
2017-03-08 14:26:23 -08:00
|
|
|
// TODO(gri) remove need for -C (disable printing of columns in error messages)
|
2023-05-24 16:30:49 +00:00
|
|
|
cmdline := []string{goTool, "tool", "compile", "-p=p", "-d=panic", "-C", "-e", "-importcfg=" + stdlibImportcfgFile(), "-o", "a.o"}
|
2015-10-27 14:54:19 +13:00
|
|
|
// No need to add -dynlink even if linkshared if we're just checking for errors...
|
2012-09-23 13:16:14 -04:00
|
|
|
cmdline = append(cmdline, flags...)
|
|
|
|
|
cmdline = append(cmdline, long)
|
|
|
|
|
out, err := runcmd(cmdline...)
|
|
|
|
|
if wantError {
|
|
|
|
|
if err == nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
|
2012-09-23 13:16:14 -04:00
|
|
|
}
|
2021-01-07 11:22:42 -05:00
|
|
|
if err == errTimeout {
|
2023-02-06 17:10:22 -05:00
|
|
|
return fmt.Errorf("compilation timed out")
|
2021-01-07 11:22:42 -05:00
|
|
|
}
|
2012-09-23 13:16:14 -04:00
|
|
|
} else {
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2012-09-23 13:16:14 -04:00
|
|
|
}
|
|
|
|
|
}
|
2015-02-19 22:00:11 +03:00
|
|
|
if *updateErrors {
|
|
|
|
|
t.updateErrors(string(out), long)
|
|
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return t.errorCheck(string(out), wantAuto, long, t.goFile)
|
2020-11-30 21:38:49 -08:00
|
|
|
|
2021-05-17 13:59:25 -07:00
|
|
|
case "compile":
|
2018-06-01 13:14:48 +03:00
|
|
|
// Compile Go file.
|
2023-02-06 17:10:22 -05:00
|
|
|
_, err := compileFile(runcmd, long, flags)
|
|
|
|
|
return err
|
2012-03-08 14:03:40 -05:00
|
|
|
|
2012-07-30 21:12:05 +02:00
|
|
|
case "compiledir":
|
2018-06-01 13:14:48 +03:00
|
|
|
// Compile all files in the directory as packages in lexicographic order.
|
2023-02-06 17:10:22 -05:00
|
|
|
longdir := filepath.Join(t.gorootTestDir, t.goDirName())
|
|
|
|
|
pkgs := goDirPackages(t.T, longdir, singlefilepkgs)
|
|
|
|
|
importcfgfile := importcfg(pkgs)
|
2022-09-21 15:51:27 -04:00
|
|
|
|
cmd/compile: require -p flag
The -p flag specifies the import path of the package being compiled.
This CL makes it required when invoking the compiler and
adjusts tests that invoke the compiler directly to conform to this
new requirement. The go command already passes the flag, so it
is unmodified in this CL. It is expected that any other Go build systems
also already pass -p, or else they will need to arrange to do so before
updating to Go 1.19. Of particular note, Bazel already does for rules
with an importpath= attribute, which includes all Gazelle-generated rules.
There is more cleanup possible now in cmd/compile, cmd/link,
and other consumers of Go object files, but that is left to future CLs.
Additional historical background follows but can be ignored.
Long ago, before the go command, or modules, or any kind of
versioning, symbols in Go archive files were named using just the
package name, so that for example func F in math/rand and func F in
crypto/rand would both be the object file symbol 'rand.F'. This led to
collisions even in small source trees, which made certain packages
unusable in the presence of other packages and generally was a problem
for Go's goal of scaling to very large source trees.
Fixing this problem required changing from package names to import
paths in symbol names, which was mostly straightforward. One wrinkle,
though, is that the compiler did not know the import path of the
package being compiled; it only knew the package name. At the time,
there was no go command, just Makefiles that people had invoking 6g
(now “go tool compile”) and then copying the resulting object file to
an importable location. That is, everyone had a custom build setup for
Go, because there was no standard one. So it was not particularly
attractive to change how the compiler was invoked, since that would
break approximately every Go user at the time. Instead, we arranged
for the compiler to emit, and other tools reading object files to
recognize, a special import path (the empty string, it turned out)
denoting “the import path of this object file”. This worked well
enough at the time and maintained complete command-line compatibility
with existing Go usage.
The changes implementing this transition can be found by searching
the Git history for “package global name space”, which is what they
eliminated. In particular, CL 190076 (a6736fa4), CL 186263 (758f2bc5),
CL 193080 (1cecac81), CL 194053 (19126320), and CL 194071 (531e6b77)
did the bulk of this transformation in January 2010.
Later, in September 2011, we added the -p flag to the compiler for
diagnostic purposes. The problem was that it was easy to create import
cycles, especially in tests, and these could not be diagnosed until
link time. You'd really want the compiler to diagnose these, for
example if the compilation of package sort noticed it was importing a
package that itself imported "sort". But the compilation of package
sort didn't know its own import path, and so it could not tell whether
it had found itself as a transitive dependency. Adding the -p flag
solved this problem, and its use was optional, since the linker would
still diagnose the import cycle in builds that had not updated to
start passing -p. This was CL 4972057 (1e480cd1).
There was still no go command at this point, but when we introduced
the go command we made it pass -p, which it has for many years at this
point.
Over time, parts of the compiler began to depend on the presence of
the -p flag for various reasonable purposes. For example:
In CL 6497074 (041fc8bf; Oct 2012), the race detector used -p to
detect packages that should not have race annotations, such as
runtime/race and sync/atomic.
In CL 13367052 (7276c02b; Sep 2013), a bug fix used -p to detect the
compilation of package reflect.
In CL 30539 (8aadcc55; Oct 2016), the compiler started using -p to
identify package math, to be able to intrinsify calls to Sqrt inside
that package.
In CL 61019 (9daee931; Sep 2017), CL 71430 (2c1d2e06; Oct 2017), and
later related CLs, the compiler started using the -p value when
creating various DWARF debugging information.
In CL 174657 (cc5eaf93; May 2019), the compiler started writing
symbols without the magic empty string whenever -p was used, to reduce
the amount of work required in the linker.
In CL 179861 (dde7c770; Jun 2019), the compiler made the second
argument to //go:linkname optional when -p is used, because in that
case the compiler can derive an appropriate default.
There are more examples. Today it is impossible to compile the Go
standard library without using -p, and DWARF debug information is
incomplete without using -p.
All known Go build systems pass -p. In particular, the go command
does, which is what nearly all Go developers invoke to build Go code.
And Bazel does, for go_library rules that set the importpath
attribute, which is all rules generated by Gazelle.
Gccgo has an equivalent of -p and has required its use in order to
disambiguate packages with the same name but different import paths
since 2010.
On top of all this, various parts of code generation for generics
are made more complicated by needing to cope with the case where -p
is not specified, even though it's essentially always specified.
In summary, the current state is:
- Use of the -p flag with cmd/compile is required for building
the standard library, and for complete DWARF information,
and to enable certain linker speedups.
- The go command and Bazel, which we expect account for just
about 100% of Go builds, both invoke cmd/compile with -p.
- The code in cmd/compile to support builds without -p is
complex and has become more complex with generics, but it is
almost always dead code and therefore not worth maintaining.
- Gccgo already requires its equivalent of -p in any build
where two packages have the same name.
All this supports the change in this CL, which makes -p required
and adjusts tests that invoke cmd/compile to add -p appropriately.
Future CLs will be able to remove all the code dealing with the
possibility of -p not having been specified.
Change-Id: I6b95b9d4cffe59c7bac82eb273ef6c4a67bb0e43
Reviewed-on: https://go-review.googlesource.com/c/go/+/391014
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2022-03-08 18:16:35 -05:00
|
|
|
for _, pkg := range pkgs {
|
2023-02-06 17:10:22 -05:00
|
|
|
_, err := compileInDir(runcmd, longdir, flags, importcfgfile, pkg.name, pkg.files...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2012-08-06 14:56:39 +10:00
|
|
|
}
|
2012-10-06 09:23:31 +02:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return nil
|
2012-10-06 09:23:31 +02:00
|
|
|
|
2016-03-11 00:10:52 -05:00
|
|
|
case "errorcheckdir", "errorcheckandrundir":
|
2021-03-04 22:31:43 +07:00
|
|
|
flags = append(flags, "-d=panic")
|
2018-06-01 13:14:48 +03:00
|
|
|
// Compile and errorCheck all files in the directory as packages in lexicographic order.
|
|
|
|
|
// If errorcheckdir and wantError, compilation of the last package must fail.
|
|
|
|
|
// If errorcheckandrundir and wantError, compilation of the package prior the last must fail.
|
2023-02-06 17:10:22 -05:00
|
|
|
longdir := filepath.Join(t.gorootTestDir, t.goDirName())
|
|
|
|
|
pkgs := goDirPackages(t.T, longdir, singlefilepkgs)
|
2018-05-26 12:57:50 +03:00
|
|
|
errPkg := len(pkgs) - 1
|
|
|
|
|
if wantError && action == "errorcheckandrundir" {
|
|
|
|
|
// The last pkg should compiled successfully and will be run in next case.
|
|
|
|
|
// Preceding pkg must return an error from compileInDir.
|
|
|
|
|
errPkg--
|
|
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
importcfgfile := importcfg(pkgs)
|
cmd/compile: require -p flag
The -p flag specifies the import path of the package being compiled.
This CL makes it required when invoking the compiler and
adjusts tests that invoke the compiler directly to conform to this
new requirement. The go command already passes the flag, so it
is unmodified in this CL. It is expected that any other Go build systems
also already pass -p, or else they will need to arrange to do so before
updating to Go 1.19. Of particular note, Bazel already does for rules
with an importpath= attribute, which includes all Gazelle-generated rules.
There is more cleanup possible now in cmd/compile, cmd/link,
and other consumers of Go object files, but that is left to future CLs.
Additional historical background follows but can be ignored.
Long ago, before the go command, or modules, or any kind of
versioning, symbols in Go archive files were named using just the
package name, so that for example func F in math/rand and func F in
crypto/rand would both be the object file symbol 'rand.F'. This led to
collisions even in small source trees, which made certain packages
unusable in the presence of other packages and generally was a problem
for Go's goal of scaling to very large source trees.
Fixing this problem required changing from package names to import
paths in symbol names, which was mostly straightforward. One wrinkle,
though, is that the compiler did not know the import path of the
package being compiled; it only knew the package name. At the time,
there was no go command, just Makefiles that people had invoking 6g
(now “go tool compile”) and then copying the resulting object file to
an importable location. That is, everyone had a custom build setup for
Go, because there was no standard one. So it was not particularly
attractive to change how the compiler was invoked, since that would
break approximately every Go user at the time. Instead, we arranged
for the compiler to emit, and other tools reading object files to
recognize, a special import path (the empty string, it turned out)
denoting “the import path of this object file”. This worked well
enough at the time and maintained complete command-line compatibility
with existing Go usage.
The changes implementing this transition can be found by searching
the Git history for “package global name space”, which is what they
eliminated. In particular, CL 190076 (a6736fa4), CL 186263 (758f2bc5),
CL 193080 (1cecac81), CL 194053 (19126320), and CL 194071 (531e6b77)
did the bulk of this transformation in January 2010.
Later, in September 2011, we added the -p flag to the compiler for
diagnostic purposes. The problem was that it was easy to create import
cycles, especially in tests, and these could not be diagnosed until
link time. You'd really want the compiler to diagnose these, for
example if the compilation of package sort noticed it was importing a
package that itself imported "sort". But the compilation of package
sort didn't know its own import path, and so it could not tell whether
it had found itself as a transitive dependency. Adding the -p flag
solved this problem, and its use was optional, since the linker would
still diagnose the import cycle in builds that had not updated to
start passing -p. This was CL 4972057 (1e480cd1).
There was still no go command at this point, but when we introduced
the go command we made it pass -p, which it has for many years at this
point.
Over time, parts of the compiler began to depend on the presence of
the -p flag for various reasonable purposes. For example:
In CL 6497074 (041fc8bf; Oct 2012), the race detector used -p to
detect packages that should not have race annotations, such as
runtime/race and sync/atomic.
In CL 13367052 (7276c02b; Sep 2013), a bug fix used -p to detect the
compilation of package reflect.
In CL 30539 (8aadcc55; Oct 2016), the compiler started using -p to
identify package math, to be able to intrinsify calls to Sqrt inside
that package.
In CL 61019 (9daee931; Sep 2017), CL 71430 (2c1d2e06; Oct 2017), and
later related CLs, the compiler started using the -p value when
creating various DWARF debugging information.
In CL 174657 (cc5eaf93; May 2019), the compiler started writing
symbols without the magic empty string whenever -p was used, to reduce
the amount of work required in the linker.
In CL 179861 (dde7c770; Jun 2019), the compiler made the second
argument to //go:linkname optional when -p is used, because in that
case the compiler can derive an appropriate default.
There are more examples. Today it is impossible to compile the Go
standard library without using -p, and DWARF debug information is
incomplete without using -p.
All known Go build systems pass -p. In particular, the go command
does, which is what nearly all Go developers invoke to build Go code.
And Bazel does, for go_library rules that set the importpath
attribute, which is all rules generated by Gazelle.
Gccgo has an equivalent of -p and has required its use in order to
disambiguate packages with the same name but different import paths
since 2010.
On top of all this, various parts of code generation for generics
are made more complicated by needing to cope with the case where -p
is not specified, even though it's essentially always specified.
In summary, the current state is:
- Use of the -p flag with cmd/compile is required for building
the standard library, and for complete DWARF information,
and to enable certain linker speedups.
- The go command and Bazel, which we expect account for just
about 100% of Go builds, both invoke cmd/compile with -p.
- The code in cmd/compile to support builds without -p is
complex and has become more complex with generics, but it is
almost always dead code and therefore not worth maintaining.
- Gccgo already requires its equivalent of -p in any build
where two packages have the same name.
All this supports the change in this CL, which makes -p required
and adjusts tests that invoke cmd/compile to add -p appropriately.
Future CLs will be able to remove all the code dealing with the
possibility of -p not having been specified.
Change-Id: I6b95b9d4cffe59c7bac82eb273ef6c4a67bb0e43
Reviewed-on: https://go-review.googlesource.com/c/go/+/391014
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2022-03-08 18:16:35 -05:00
|
|
|
for i, pkg := range pkgs {
|
2022-09-21 15:51:27 -04:00
|
|
|
out, err := compileInDir(runcmd, longdir, flags, importcfgfile, pkg.name, pkg.files...)
|
2018-05-26 12:57:50 +03:00
|
|
|
if i == errPkg {
|
2012-10-06 09:23:31 +02:00
|
|
|
if wantError && err == nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
|
2012-10-06 09:23:31 +02:00
|
|
|
} else if !wantError && err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2012-10-06 09:23:31 +02:00
|
|
|
}
|
|
|
|
|
} else if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2012-10-06 09:23:31 +02:00
|
|
|
}
|
2013-01-02 15:31:49 -05:00
|
|
|
var fullshort []string
|
cmd/compile: require -p flag
The -p flag specifies the import path of the package being compiled.
This CL makes it required when invoking the compiler and
adjusts tests that invoke the compiler directly to conform to this
new requirement. The go command already passes the flag, so it
is unmodified in this CL. It is expected that any other Go build systems
also already pass -p, or else they will need to arrange to do so before
updating to Go 1.19. Of particular note, Bazel already does for rules
with an importpath= attribute, which includes all Gazelle-generated rules.
There is more cleanup possible now in cmd/compile, cmd/link,
and other consumers of Go object files, but that is left to future CLs.
Additional historical background follows but can be ignored.
Long ago, before the go command, or modules, or any kind of
versioning, symbols in Go archive files were named using just the
package name, so that for example func F in math/rand and func F in
crypto/rand would both be the object file symbol 'rand.F'. This led to
collisions even in small source trees, which made certain packages
unusable in the presence of other packages and generally was a problem
for Go's goal of scaling to very large source trees.
Fixing this problem required changing from package names to import
paths in symbol names, which was mostly straightforward. One wrinkle,
though, is that the compiler did not know the import path of the
package being compiled; it only knew the package name. At the time,
there was no go command, just Makefiles that people had invoking 6g
(now “go tool compile”) and then copying the resulting object file to
an importable location. That is, everyone had a custom build setup for
Go, because there was no standard one. So it was not particularly
attractive to change how the compiler was invoked, since that would
break approximately every Go user at the time. Instead, we arranged
for the compiler to emit, and other tools reading object files to
recognize, a special import path (the empty string, it turned out)
denoting “the import path of this object file”. This worked well
enough at the time and maintained complete command-line compatibility
with existing Go usage.
The changes implementing this transition can be found by searching
the Git history for “package global name space”, which is what they
eliminated. In particular, CL 190076 (a6736fa4), CL 186263 (758f2bc5),
CL 193080 (1cecac81), CL 194053 (19126320), and CL 194071 (531e6b77)
did the bulk of this transformation in January 2010.
Later, in September 2011, we added the -p flag to the compiler for
diagnostic purposes. The problem was that it was easy to create import
cycles, especially in tests, and these could not be diagnosed until
link time. You'd really want the compiler to diagnose these, for
example if the compilation of package sort noticed it was importing a
package that itself imported "sort". But the compilation of package
sort didn't know its own import path, and so it could not tell whether
it had found itself as a transitive dependency. Adding the -p flag
solved this problem, and its use was optional, since the linker would
still diagnose the import cycle in builds that had not updated to
start passing -p. This was CL 4972057 (1e480cd1).
There was still no go command at this point, but when we introduced
the go command we made it pass -p, which it has for many years at this
point.
Over time, parts of the compiler began to depend on the presence of
the -p flag for various reasonable purposes. For example:
In CL 6497074 (041fc8bf; Oct 2012), the race detector used -p to
detect packages that should not have race annotations, such as
runtime/race and sync/atomic.
In CL 13367052 (7276c02b; Sep 2013), a bug fix used -p to detect the
compilation of package reflect.
In CL 30539 (8aadcc55; Oct 2016), the compiler started using -p to
identify package math, to be able to intrinsify calls to Sqrt inside
that package.
In CL 61019 (9daee931; Sep 2017), CL 71430 (2c1d2e06; Oct 2017), and
later related CLs, the compiler started using the -p value when
creating various DWARF debugging information.
In CL 174657 (cc5eaf93; May 2019), the compiler started writing
symbols without the magic empty string whenever -p was used, to reduce
the amount of work required in the linker.
In CL 179861 (dde7c770; Jun 2019), the compiler made the second
argument to //go:linkname optional when -p is used, because in that
case the compiler can derive an appropriate default.
There are more examples. Today it is impossible to compile the Go
standard library without using -p, and DWARF debug information is
incomplete without using -p.
All known Go build systems pass -p. In particular, the go command
does, which is what nearly all Go developers invoke to build Go code.
And Bazel does, for go_library rules that set the importpath
attribute, which is all rules generated by Gazelle.
Gccgo has an equivalent of -p and has required its use in order to
disambiguate packages with the same name but different import paths
since 2010.
On top of all this, various parts of code generation for generics
are made more complicated by needing to cope with the case where -p
is not specified, even though it's essentially always specified.
In summary, the current state is:
- Use of the -p flag with cmd/compile is required for building
the standard library, and for complete DWARF information,
and to enable certain linker speedups.
- The go command and Bazel, which we expect account for just
about 100% of Go builds, both invoke cmd/compile with -p.
- The code in cmd/compile to support builds without -p is
complex and has become more complex with generics, but it is
almost always dead code and therefore not worth maintaining.
- Gccgo already requires its equivalent of -p in any build
where two packages have the same name.
All this supports the change in this CL, which makes -p required
and adjusts tests that invoke cmd/compile to add -p appropriately.
Future CLs will be able to remove all the code dealing with the
possibility of -p not having been specified.
Change-Id: I6b95b9d4cffe59c7bac82eb273ef6c4a67bb0e43
Reviewed-on: https://go-review.googlesource.com/c/go/+/391014
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2022-03-08 18:16:35 -05:00
|
|
|
for _, name := range pkg.files {
|
2013-01-02 15:31:49 -05:00
|
|
|
fullshort = append(fullshort, filepath.Join(longdir, name), name)
|
|
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
err = t.errorCheck(string(out), wantAuto, fullshort...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2012-07-30 21:12:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-03-11 00:10:52 -05:00
|
|
|
if action == "errorcheckdir" {
|
2023-02-06 17:10:22 -05:00
|
|
|
return nil
|
2016-03-11 00:10:52 -05:00
|
|
|
}
|
|
|
|
|
fallthrough
|
2012-07-30 21:12:05 +02:00
|
|
|
|
2012-10-06 09:23:31 +02:00
|
|
|
case "rundir":
|
2018-06-01 13:14:48 +03:00
|
|
|
// Compile all files in the directory as packages in lexicographic order.
|
|
|
|
|
// In case of errorcheckandrundir, ignore failed compilation of the package before the last.
|
|
|
|
|
// Link as if the last file is the main package, run it.
|
|
|
|
|
// Verify the expected output.
|
2023-02-06 17:10:22 -05:00
|
|
|
longdir := filepath.Join(t.gorootTestDir, t.goDirName())
|
|
|
|
|
pkgs := goDirPackages(t.T, longdir, singlefilepkgs)
|
2019-03-22 15:24:36 -04:00
|
|
|
// Split flags into gcflags and ldflags
|
|
|
|
|
ldflags := []string{}
|
|
|
|
|
for i, fl := range flags {
|
|
|
|
|
if fl == "-ldflags" {
|
|
|
|
|
ldflags = flags[i+1:]
|
|
|
|
|
flags = flags[0:i]
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
importcfgfile := importcfg(pkgs)
|
2022-09-21 15:51:27 -04:00
|
|
|
|
cmd/compile: require -p flag
The -p flag specifies the import path of the package being compiled.
This CL makes it required when invoking the compiler and
adjusts tests that invoke the compiler directly to conform to this
new requirement. The go command already passes the flag, so it
is unmodified in this CL. It is expected that any other Go build systems
also already pass -p, or else they will need to arrange to do so before
updating to Go 1.19. Of particular note, Bazel already does for rules
with an importpath= attribute, which includes all Gazelle-generated rules.
There is more cleanup possible now in cmd/compile, cmd/link,
and other consumers of Go object files, but that is left to future CLs.
Additional historical background follows but can be ignored.
Long ago, before the go command, or modules, or any kind of
versioning, symbols in Go archive files were named using just the
package name, so that for example func F in math/rand and func F in
crypto/rand would both be the object file symbol 'rand.F'. This led to
collisions even in small source trees, which made certain packages
unusable in the presence of other packages and generally was a problem
for Go's goal of scaling to very large source trees.
Fixing this problem required changing from package names to import
paths in symbol names, which was mostly straightforward. One wrinkle,
though, is that the compiler did not know the import path of the
package being compiled; it only knew the package name. At the time,
there was no go command, just Makefiles that people had invoking 6g
(now “go tool compile”) and then copying the resulting object file to
an importable location. That is, everyone had a custom build setup for
Go, because there was no standard one. So it was not particularly
attractive to change how the compiler was invoked, since that would
break approximately every Go user at the time. Instead, we arranged
for the compiler to emit, and other tools reading object files to
recognize, a special import path (the empty string, it turned out)
denoting “the import path of this object file”. This worked well
enough at the time and maintained complete command-line compatibility
with existing Go usage.
The changes implementing this transition can be found by searching
the Git history for “package global name space”, which is what they
eliminated. In particular, CL 190076 (a6736fa4), CL 186263 (758f2bc5),
CL 193080 (1cecac81), CL 194053 (19126320), and CL 194071 (531e6b77)
did the bulk of this transformation in January 2010.
Later, in September 2011, we added the -p flag to the compiler for
diagnostic purposes. The problem was that it was easy to create import
cycles, especially in tests, and these could not be diagnosed until
link time. You'd really want the compiler to diagnose these, for
example if the compilation of package sort noticed it was importing a
package that itself imported "sort". But the compilation of package
sort didn't know its own import path, and so it could not tell whether
it had found itself as a transitive dependency. Adding the -p flag
solved this problem, and its use was optional, since the linker would
still diagnose the import cycle in builds that had not updated to
start passing -p. This was CL 4972057 (1e480cd1).
There was still no go command at this point, but when we introduced
the go command we made it pass -p, which it has for many years at this
point.
Over time, parts of the compiler began to depend on the presence of
the -p flag for various reasonable purposes. For example:
In CL 6497074 (041fc8bf; Oct 2012), the race detector used -p to
detect packages that should not have race annotations, such as
runtime/race and sync/atomic.
In CL 13367052 (7276c02b; Sep 2013), a bug fix used -p to detect the
compilation of package reflect.
In CL 30539 (8aadcc55; Oct 2016), the compiler started using -p to
identify package math, to be able to intrinsify calls to Sqrt inside
that package.
In CL 61019 (9daee931; Sep 2017), CL 71430 (2c1d2e06; Oct 2017), and
later related CLs, the compiler started using the -p value when
creating various DWARF debugging information.
In CL 174657 (cc5eaf93; May 2019), the compiler started writing
symbols without the magic empty string whenever -p was used, to reduce
the amount of work required in the linker.
In CL 179861 (dde7c770; Jun 2019), the compiler made the second
argument to //go:linkname optional when -p is used, because in that
case the compiler can derive an appropriate default.
There are more examples. Today it is impossible to compile the Go
standard library without using -p, and DWARF debug information is
incomplete without using -p.
All known Go build systems pass -p. In particular, the go command
does, which is what nearly all Go developers invoke to build Go code.
And Bazel does, for go_library rules that set the importpath
attribute, which is all rules generated by Gazelle.
Gccgo has an equivalent of -p and has required its use in order to
disambiguate packages with the same name but different import paths
since 2010.
On top of all this, various parts of code generation for generics
are made more complicated by needing to cope with the case where -p
is not specified, even though it's essentially always specified.
In summary, the current state is:
- Use of the -p flag with cmd/compile is required for building
the standard library, and for complete DWARF information,
and to enable certain linker speedups.
- The go command and Bazel, which we expect account for just
about 100% of Go builds, both invoke cmd/compile with -p.
- The code in cmd/compile to support builds without -p is
complex and has become more complex with generics, but it is
almost always dead code and therefore not worth maintaining.
- Gccgo already requires its equivalent of -p in any build
where two packages have the same name.
All this supports the change in this CL, which makes -p required
and adjusts tests that invoke cmd/compile to add -p appropriately.
Future CLs will be able to remove all the code dealing with the
possibility of -p not having been specified.
Change-Id: I6b95b9d4cffe59c7bac82eb273ef6c4a67bb0e43
Reviewed-on: https://go-review.googlesource.com/c/go/+/391014
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2022-03-08 18:16:35 -05:00
|
|
|
for i, pkg := range pkgs {
|
2022-09-21 15:51:27 -04:00
|
|
|
_, err := compileInDir(runcmd, longdir, flags, importcfgfile, pkg.name, pkg.files...)
|
2018-05-26 12:57:50 +03:00
|
|
|
// Allow this package compilation fail based on conditions below;
|
|
|
|
|
// its errors were checked in previous case.
|
|
|
|
|
if err != nil && !(wantError && action == "errorcheckandrundir" && i == len(pkgs)-2) {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2012-10-06 09:23:31 +02:00
|
|
|
}
|
2022-09-21 15:51:27 -04:00
|
|
|
|
2013-01-11 22:00:48 +01:00
|
|
|
if i == len(pkgs)-1 {
|
2022-09-21 15:51:27 -04:00
|
|
|
err = linkFile(runcmd, pkg.files[0], importcfgfile, ldflags)
|
2013-01-11 22:00:48 +01:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2013-01-11 22:00:48 +01:00
|
|
|
}
|
all: merge NaCl branch (part 1)
See golang.org/s/go13nacl for design overview.
This CL is the mostly mechanical changes from rsc's Go 1.2 based NaCl branch, specifically 39cb35750369 to 500771b477cf from https://code.google.com/r/rsc-go13nacl. This CL does not include working NaCl support, there are probably two or three more large merges to come.
CL 15750044 is not included as it involves more invasive changes to the linker which will need to be merged separately.
The exact change lists included are
15050047: syscall: support for Native Client
15360044: syscall: unzip implementation for Native Client
15370044: syscall: Native Client SRPC implementation
15400047: cmd/dist, cmd/go, go/build, test: support for Native Client
15410048: runtime: support for Native Client
15410049: syscall: file descriptor table for Native Client
15410050: syscall: in-memory file system for Native Client
15440048: all: update +build lines for Native Client port
15540045: cmd/6g, cmd/8g, cmd/gc: support for Native Client
15570045: os: support for Native Client
15680044: crypto/..., hash/crc32, reflect, sync/atomic: support for amd64p32
15690044: net: support for Native Client
15690048: runtime: support for fake time like on Go Playground
15690051: build: disable various tests on Native Client
LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/68150047
2014-02-25 09:47:42 -05:00
|
|
|
var cmd []string
|
|
|
|
|
cmd = append(cmd, findExecCmd()...)
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd = append(cmd, filepath.Join(tempDir, "a.exe"))
|
all: merge NaCl branch (part 1)
See golang.org/s/go13nacl for design overview.
This CL is the mostly mechanical changes from rsc's Go 1.2 based NaCl branch, specifically 39cb35750369 to 500771b477cf from https://code.google.com/r/rsc-go13nacl. This CL does not include working NaCl support, there are probably two or three more large merges to come.
CL 15750044 is not included as it involves more invasive changes to the linker which will need to be merged separately.
The exact change lists included are
15050047: syscall: support for Native Client
15360044: syscall: unzip implementation for Native Client
15370044: syscall: Native Client SRPC implementation
15400047: cmd/dist, cmd/go, go/build, test: support for Native Client
15410048: runtime: support for Native Client
15410049: syscall: file descriptor table for Native Client
15410050: syscall: in-memory file system for Native Client
15440048: all: update +build lines for Native Client port
15540045: cmd/6g, cmd/8g, cmd/gc: support for Native Client
15570045: os: support for Native Client
15680044: crypto/..., hash/crc32, reflect, sync/atomic: support for amd64p32
15690044: net: support for Native Client
15690048: runtime: support for fake time like on Go Playground
15690051: build: disable various tests on Native Client
LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/68150047
2014-02-25 09:47:42 -05:00
|
|
|
cmd = append(cmd, args...)
|
|
|
|
|
out, err := runcmd(cmd...)
|
2013-01-11 22:00:48 +01:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2013-01-11 22:00:48 +01:00
|
|
|
}
|
2021-01-04 14:05:17 -05:00
|
|
|
t.checkExpectedOutput(out)
|
2013-01-11 22:00:48 +01:00
|
|
|
}
|
2012-10-06 09:23:31 +02:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return nil
|
2012-10-06 09:23:31 +02:00
|
|
|
|
2019-05-17 17:25:07 +07:00
|
|
|
case "runindir":
|
2020-03-24 16:18:02 -04:00
|
|
|
// Make a shallow copy of t.goDirName() in its own module and GOPATH, and
|
|
|
|
|
// run "go run ." in it. The module path (and hence import path prefix) of
|
|
|
|
|
// the copy is equal to the basename of the source directory.
|
|
|
|
|
//
|
|
|
|
|
// It's used when test a requires a full 'go build' in order to compile
|
|
|
|
|
// the sources, such as when importing multiple packages (issue29612.dir)
|
|
|
|
|
// or compiling a package containing assembly files (see issue15609.dir),
|
|
|
|
|
// but still needs to be run to verify the expected output.
|
|
|
|
|
tempDirIsGOPATH = true
|
2023-02-06 17:10:22 -05:00
|
|
|
srcDir := filepath.Join(t.gorootTestDir, t.goDirName())
|
2020-03-24 16:18:02 -04:00
|
|
|
modName := filepath.Base(srcDir)
|
2023-02-06 17:10:22 -05:00
|
|
|
gopathSrcDir := filepath.Join(tempDir, "src", modName)
|
2020-03-24 16:18:02 -04:00
|
|
|
runInDir = gopathSrcDir
|
|
|
|
|
|
|
|
|
|
if err := overlayDir(gopathSrcDir, srcDir); err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatal(err)
|
2020-03-24 16:18:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-08-09 13:32:12 -07:00
|
|
|
modVersion := gomodvers
|
|
|
|
|
if modVersion == "" {
|
|
|
|
|
modVersion = "1.14"
|
|
|
|
|
}
|
|
|
|
|
modFile := fmt.Sprintf("module %s\ngo %s\n", modName, modVersion)
|
2023-02-06 17:10:22 -05:00
|
|
|
if err := os.WriteFile(filepath.Join(gopathSrcDir, "go.mod"), []byte(modFile), 0666); err != nil {
|
|
|
|
|
t.Fatal(err)
|
2020-03-24 16:18:02 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd := []string{goTool, "run", t.goGcflags()}
|
2019-05-17 17:25:07 +07:00
|
|
|
if *linkshared {
|
|
|
|
|
cmd = append(cmd, "-linkshared")
|
|
|
|
|
}
|
2021-02-11 19:55:07 -05:00
|
|
|
cmd = append(cmd, flags...)
|
2019-05-17 17:25:07 +07:00
|
|
|
cmd = append(cmd, ".")
|
|
|
|
|
out, err := runcmd(cmd...)
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2019-05-17 17:25:07 +07:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return t.checkExpectedOutput(out)
|
2019-05-17 17:25:07 +07:00
|
|
|
|
2012-03-07 01:54:39 -05:00
|
|
|
case "build":
|
2018-06-01 13:14:48 +03:00
|
|
|
// Build Go file.
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd := []string{goTool, "build", t.goGcflags()}
|
2022-05-16 17:03:59 -07:00
|
|
|
cmd = append(cmd, flags...)
|
|
|
|
|
cmd = append(cmd, "-o", "a.exe", long)
|
|
|
|
|
_, err := runcmd(cmd...)
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2012-03-08 14:03:40 -05:00
|
|
|
|
2018-02-14 19:35:03 -05:00
|
|
|
case "builddir", "buildrundir":
|
2017-06-14 11:36:36 -07:00
|
|
|
// Build an executable from all the .go and .s files in a subdirectory.
|
2018-06-01 13:14:48 +03:00
|
|
|
// Run it and verify its output in the buildrundir case.
|
2023-02-06 17:10:22 -05:00
|
|
|
longdir := filepath.Join(t.gorootTestDir, t.goDirName())
|
|
|
|
|
files, err := os.ReadDir(longdir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
2017-06-14 11:36:36 -07:00
|
|
|
}
|
2018-11-01 22:04:02 -04:00
|
|
|
var gos []string
|
|
|
|
|
var asms []string
|
2017-06-14 11:36:36 -07:00
|
|
|
for _, file := range files {
|
|
|
|
|
switch filepath.Ext(file.Name()) {
|
|
|
|
|
case ".go":
|
2018-11-01 22:04:02 -04:00
|
|
|
gos = append(gos, filepath.Join(longdir, file.Name()))
|
2017-06-14 11:36:36 -07:00
|
|
|
case ".s":
|
2018-11-01 22:04:02 -04:00
|
|
|
asms = append(asms, filepath.Join(longdir, file.Name()))
|
2017-06-14 11:36:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-10-22 11:21:56 -04:00
|
|
|
if len(asms) > 0 {
|
2023-02-06 17:10:22 -05:00
|
|
|
emptyHdrFile := filepath.Join(tempDir, "go_asm.h")
|
|
|
|
|
if err := os.WriteFile(emptyHdrFile, nil, 0666); err != nil {
|
|
|
|
|
t.Fatalf("write empty go_asm.h: %v", err)
|
2018-10-22 11:21:56 -04:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd := []string{goTool, "tool", "asm", "-p=main", "-gensymabis", "-o", "symabis"}
|
2018-10-22 11:21:56 -04:00
|
|
|
cmd = append(cmd, asms...)
|
|
|
|
|
_, err = runcmd(cmd...)
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2018-10-22 11:21:56 -04:00
|
|
|
}
|
|
|
|
|
}
|
2017-06-14 11:36:36 -07:00
|
|
|
var objs []string
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd := []string{goTool, "tool", "compile", "-p=main", "-e", "-D", ".", "-importcfg=" + stdlibImportcfgFile(), "-o", "go.o"}
|
2017-11-29 11:58:03 -08:00
|
|
|
if len(asms) > 0 {
|
2018-10-22 11:21:56 -04:00
|
|
|
cmd = append(cmd, "-asmhdr", "go_asm.h", "-symabis", "symabis")
|
2017-11-29 11:58:03 -08:00
|
|
|
}
|
2018-11-01 22:04:02 -04:00
|
|
|
cmd = append(cmd, gos...)
|
2023-02-06 17:10:22 -05:00
|
|
|
_, err = runcmd(cmd...)
|
2017-06-14 11:36:36 -07:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2017-06-14 11:36:36 -07:00
|
|
|
}
|
|
|
|
|
objs = append(objs, "go.o")
|
|
|
|
|
if len(asms) > 0 {
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd = []string{goTool, "tool", "asm", "-p=main", "-e", "-I", ".", "-o", "asm.o"}
|
2018-11-01 22:04:02 -04:00
|
|
|
cmd = append(cmd, asms...)
|
2017-06-14 11:36:36 -07:00
|
|
|
_, err = runcmd(cmd...)
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2017-06-14 11:36:36 -07:00
|
|
|
}
|
|
|
|
|
objs = append(objs, "asm.o")
|
|
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd = []string{goTool, "tool", "pack", "c", "all.a"}
|
2017-06-14 11:36:36 -07:00
|
|
|
cmd = append(cmd, objs...)
|
|
|
|
|
_, err = runcmd(cmd...)
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2017-06-14 11:36:36 -07:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd = []string{goTool, "tool", "link", "-importcfg=" + stdlibImportcfgFile(), "-o", "a.exe", "all.a"}
|
2017-06-14 11:36:36 -07:00
|
|
|
_, err = runcmd(cmd...)
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2017-06-14 11:36:36 -07:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
|
|
|
|
|
if action == "builddir" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
cmd = append(findExecCmd(), filepath.Join(tempDir, "a.exe"))
|
|
|
|
|
out, err := runcmd(cmd...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2018-02-14 19:35:03 -05:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return t.checkExpectedOutput(out)
|
2017-06-14 11:36:36 -07:00
|
|
|
|
2018-06-01 13:14:48 +03:00
|
|
|
case "buildrun":
|
|
|
|
|
// Build an executable from Go file, then run it, verify its output.
|
|
|
|
|
// Useful for timeout tests where failure mode is infinite loop.
|
2016-11-10 16:03:47 -05:00
|
|
|
// TODO: not supported on NaCl
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd := []string{goTool, "build", t.goGcflags(), "-o", "a.exe"}
|
2016-11-10 16:03:47 -05:00
|
|
|
if *linkshared {
|
|
|
|
|
cmd = append(cmd, "-linkshared")
|
|
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
longDirGoFile := filepath.Join(filepath.Join(t.gorootTestDir, t.dir), t.goFile)
|
2016-11-10 16:03:47 -05:00
|
|
|
cmd = append(cmd, flags...)
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd = append(cmd, longDirGoFile)
|
2020-02-09 01:40:45 -05:00
|
|
|
_, err := runcmd(cmd...)
|
2016-11-10 16:03:47 -05:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2016-11-10 16:03:47 -05:00
|
|
|
}
|
|
|
|
|
cmd = []string{"./a.exe"}
|
2020-02-09 01:40:45 -05:00
|
|
|
out, err := runcmd(append(cmd, args...)...)
|
2016-11-10 16:03:47 -05:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2016-11-10 16:03:47 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
return t.checkExpectedOutput(out)
|
2016-11-10 16:03:47 -05:00
|
|
|
|
2012-03-07 01:54:39 -05:00
|
|
|
case "run":
|
2018-06-01 13:14:48 +03:00
|
|
|
// Run Go file if no special go command flags are provided;
|
|
|
|
|
// otherwise build an executable and run it.
|
|
|
|
|
// Verify the output.
|
2020-03-24 16:18:02 -04:00
|
|
|
runInDir = ""
|
2017-10-27 14:11:21 -04:00
|
|
|
var out []byte
|
|
|
|
|
var err error
|
2024-02-29 20:17:09 +07:00
|
|
|
if len(flags)+len(args) == 0 && t.goGcflagsIsEmpty() && !*linkshared && goarch == runtime.GOARCH && goos == runtime.GOOS && goexp == goExperiment && godebug == goDebug {
|
2017-10-27 14:11:21 -04:00
|
|
|
// If we're not using special go command flags,
|
|
|
|
|
// skip all the go command machinery.
|
|
|
|
|
// This avoids any time the go command would
|
|
|
|
|
// spend checking whether, for example, the installed
|
|
|
|
|
// package runtime is up to date.
|
|
|
|
|
// Because we run lots of trivial test programs,
|
|
|
|
|
// the time adds up.
|
2023-02-06 17:10:22 -05:00
|
|
|
pkg := filepath.Join(tempDir, "pkg.a")
|
|
|
|
|
if _, err := runcmd(goTool, "tool", "compile", "-p=main", "-importcfg="+stdlibImportcfgFile(), "-o", pkg, t.goFileName()); err != nil {
|
|
|
|
|
return err
|
2017-10-27 14:11:21 -04:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
exe := filepath.Join(tempDir, "test.exe")
|
|
|
|
|
cmd := []string{goTool, "tool", "link", "-s", "-w", "-importcfg=" + stdlibImportcfgFile()}
|
2017-10-27 14:11:21 -04:00
|
|
|
cmd = append(cmd, "-o", exe, pkg)
|
|
|
|
|
if _, err := runcmd(cmd...); err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2017-10-27 14:11:21 -04:00
|
|
|
}
|
|
|
|
|
out, err = runcmd(append([]string{exe}, args...)...)
|
|
|
|
|
} else {
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd := []string{goTool, "run", t.goGcflags()}
|
2017-10-27 14:11:21 -04:00
|
|
|
if *linkshared {
|
|
|
|
|
cmd = append(cmd, "-linkshared")
|
|
|
|
|
}
|
|
|
|
|
cmd = append(cmd, flags...)
|
|
|
|
|
cmd = append(cmd, t.goFileName())
|
|
|
|
|
out, err = runcmd(append(cmd, args...)...)
|
2015-10-27 14:54:19 +13:00
|
|
|
}
|
2012-02-21 14:28:49 +11:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return t.checkExpectedOutput(out)
|
2012-04-20 23:45:43 +08:00
|
|
|
|
|
|
|
|
case "runoutput":
|
2018-06-01 13:14:48 +03:00
|
|
|
// Run Go file and write its output into temporary Go file.
|
|
|
|
|
// Run generated Go file and verify its output.
|
2023-02-06 17:10:22 -05:00
|
|
|
t.runoutputGate <- true
|
2013-01-12 17:52:52 +11:00
|
|
|
defer func() {
|
2023-02-06 17:10:22 -05:00
|
|
|
<-t.runoutputGate
|
2013-01-12 17:52:52 +11:00
|
|
|
}()
|
2020-03-24 16:18:02 -04:00
|
|
|
runInDir = ""
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd := []string{goTool, "run", t.goGcflags()}
|
2015-10-27 14:54:19 +13:00
|
|
|
if *linkshared {
|
|
|
|
|
cmd = append(cmd, "-linkshared")
|
|
|
|
|
}
|
|
|
|
|
cmd = append(cmd, t.goFileName())
|
|
|
|
|
out, err := runcmd(append(cmd, args...)...)
|
2012-04-20 23:45:43 +08:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2012-04-20 23:45:43 +08:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
tfile := filepath.Join(tempDir, "tmp__.go")
|
|
|
|
|
if err := os.WriteFile(tfile, out, 0666); err != nil {
|
|
|
|
|
t.Fatalf("write tempfile: %v", err)
|
2012-04-20 23:45:43 +08:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd = []string{goTool, "run", t.goGcflags()}
|
2015-10-27 14:54:19 +13:00
|
|
|
if *linkshared {
|
|
|
|
|
cmd = append(cmd, "-linkshared")
|
|
|
|
|
}
|
|
|
|
|
cmd = append(cmd, tfile)
|
|
|
|
|
out, err = runcmd(cmd...)
|
2012-04-20 23:45:43 +08:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2012-04-20 23:45:43 +08:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return t.checkExpectedOutput(out)
|
2012-11-07 12:33:54 -08:00
|
|
|
|
|
|
|
|
case "errorcheckoutput":
|
2018-06-01 13:14:48 +03:00
|
|
|
// Run Go file and write its output into temporary Go file.
|
|
|
|
|
// Compile and errorCheck generated Go file.
|
2020-03-24 16:18:02 -04:00
|
|
|
runInDir = ""
|
2023-02-06 17:10:22 -05:00
|
|
|
cmd := []string{goTool, "run", t.goGcflags()}
|
2015-10-27 14:54:19 +13:00
|
|
|
if *linkshared {
|
|
|
|
|
cmd = append(cmd, "-linkshared")
|
|
|
|
|
}
|
|
|
|
|
cmd = append(cmd, t.goFileName())
|
|
|
|
|
out, err := runcmd(append(cmd, args...)...)
|
2012-11-07 12:33:54 -08:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2012-11-07 12:33:54 -08:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
tfile := filepath.Join(tempDir, "tmp__.go")
|
|
|
|
|
err = os.WriteFile(tfile, out, 0666)
|
2012-11-07 12:33:54 -08:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatalf("write tempfile: %v", err)
|
2012-11-07 12:33:54 -08:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
cmdline := []string{goTool, "tool", "compile", "-importcfg=" + stdlibImportcfgFile(), "-p=p", "-d=panic", "-e", "-o", "a.o"}
|
2012-11-07 12:33:54 -08:00
|
|
|
cmdline = append(cmdline, flags...)
|
|
|
|
|
cmdline = append(cmdline, tfile)
|
|
|
|
|
out, err = runcmd(cmdline...)
|
|
|
|
|
if wantError {
|
|
|
|
|
if err == nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
|
2012-11-07 12:33:54 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return err
|
2012-11-07 12:33:54 -08:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return t.errorCheck(string(out), false, tfile, "tmp__.go")
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 18:15:04 -07:00
|
|
|
var findExecCmd = sync.OnceValue(func() (execCmd []string) {
|
|
|
|
|
if goos == runtime.GOOS && goarch == runtime.GOARCH {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if path, err := exec.LookPath(fmt.Sprintf("go_%s_%s_exec", goos, goarch)); err == nil {
|
|
|
|
|
execCmd = []string{path}
|
|
|
|
|
}
|
all: merge NaCl branch (part 1)
See golang.org/s/go13nacl for design overview.
This CL is the mostly mechanical changes from rsc's Go 1.2 based NaCl branch, specifically 39cb35750369 to 500771b477cf from https://code.google.com/r/rsc-go13nacl. This CL does not include working NaCl support, there are probably two or three more large merges to come.
CL 15750044 is not included as it involves more invasive changes to the linker which will need to be merged separately.
The exact change lists included are
15050047: syscall: support for Native Client
15360044: syscall: unzip implementation for Native Client
15370044: syscall: Native Client SRPC implementation
15400047: cmd/dist, cmd/go, go/build, test: support for Native Client
15410048: runtime: support for Native Client
15410049: syscall: file descriptor table for Native Client
15410050: syscall: in-memory file system for Native Client
15440048: all: update +build lines for Native Client port
15540045: cmd/6g, cmd/8g, cmd/gc: support for Native Client
15570045: os: support for Native Client
15680044: crypto/..., hash/crc32, reflect, sync/atomic: support for amd64p32
15690044: net: support for Native Client
15690048: runtime: support for fake time like on Go Playground
15690051: build: disable various tests on Native Client
LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/68150047
2014-02-25 09:47:42 -05:00
|
|
|
return execCmd
|
2024-09-04 18:15:04 -07:00
|
|
|
})
|
all: merge NaCl branch (part 1)
See golang.org/s/go13nacl for design overview.
This CL is the mostly mechanical changes from rsc's Go 1.2 based NaCl branch, specifically 39cb35750369 to 500771b477cf from https://code.google.com/r/rsc-go13nacl. This CL does not include working NaCl support, there are probably two or three more large merges to come.
CL 15750044 is not included as it involves more invasive changes to the linker which will need to be merged separately.
The exact change lists included are
15050047: syscall: support for Native Client
15360044: syscall: unzip implementation for Native Client
15370044: syscall: Native Client SRPC implementation
15400047: cmd/dist, cmd/go, go/build, test: support for Native Client
15410048: runtime: support for Native Client
15410049: syscall: file descriptor table for Native Client
15410050: syscall: in-memory file system for Native Client
15440048: all: update +build lines for Native Client port
15540045: cmd/6g, cmd/8g, cmd/gc: support for Native Client
15570045: os: support for Native Client
15680044: crypto/..., hash/crc32, reflect, sync/atomic: support for amd64p32
15690044: net: support for Native Client
15690048: runtime: support for fake time like on Go Playground
15690051: build: disable various tests on Native Client
LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/68150047
2014-02-25 09:47:42 -05:00
|
|
|
|
2021-01-04 14:05:17 -05:00
|
|
|
// checkExpectedOutput compares the output from compiling and/or running with the contents
|
|
|
|
|
// of the corresponding reference output file, if any (replace ".go" with ".out").
|
|
|
|
|
// If they don't match, fail with an informative message.
|
2023-02-06 17:10:22 -05:00
|
|
|
func (t test) checkExpectedOutput(gotBytes []byte) error {
|
2021-01-04 14:05:17 -05:00
|
|
|
got := string(gotBytes)
|
2023-02-06 17:10:22 -05:00
|
|
|
filename := filepath.Join(t.dir, t.goFile)
|
2012-02-21 14:28:49 +11:00
|
|
|
filename = filename[:len(filename)-len(".go")]
|
|
|
|
|
filename += ".out"
|
2023-02-06 17:10:22 -05:00
|
|
|
b, err := os.ReadFile(filepath.Join(t.gorootTestDir, filename))
|
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
|
// File is allowed to be missing, in which case output should be empty.
|
|
|
|
|
b = nil
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2025-04-14 15:34:30 +00:00
|
|
|
got = strings.ReplaceAll(got, "\r\n", "\n")
|
2021-01-04 14:05:17 -05:00
|
|
|
if got != string(b) {
|
|
|
|
|
if err == nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
return fmt.Errorf("output does not match expected in %s. Instead saw\n%s", filename, got)
|
2021-01-04 14:05:17 -05:00
|
|
|
} else {
|
2023-02-06 17:10:22 -05:00
|
|
|
return fmt.Errorf("output should be empty when (optional) expected-output file %s is not present. Instead saw\n%s", filename, got)
|
2021-01-04 14:05:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return nil
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
|
|
|
|
|
2016-09-22 13:50:16 -04:00
|
|
|
func splitOutput(out string, wantAuto bool) []string {
|
2015-06-24 09:50:12 +10:00
|
|
|
// gc error messages continue onto additional lines with leading tabs.
|
2012-02-21 14:28:49 +11:00
|
|
|
// Split the output at the beginning of each line that doesn't begin with a tab.
|
2015-06-16 18:28:01 -04:00
|
|
|
// <autogenerated> lines are impossible to match so those are filtered out.
|
2015-02-19 22:00:11 +03:00
|
|
|
var res []string
|
|
|
|
|
for _, line := range strings.Split(out, "\n") {
|
2012-09-23 13:16:14 -04:00
|
|
|
if strings.HasSuffix(line, "\r") { // remove '\r', output by compiler on windows
|
2012-08-16 16:46:59 +10:00
|
|
|
line = line[:len(line)-1]
|
|
|
|
|
}
|
2012-02-21 14:28:49 +11:00
|
|
|
if strings.HasPrefix(line, "\t") {
|
2015-02-19 22:00:11 +03:00
|
|
|
res[len(res)-1] += "\n" + line
|
2016-09-22 13:50:16 -04:00
|
|
|
} else if strings.HasPrefix(line, "go tool") || strings.HasPrefix(line, "#") || !wantAuto && strings.HasPrefix(line, "<autogenerated>") {
|
2012-10-08 16:36:45 +02:00
|
|
|
continue
|
|
|
|
|
} else if strings.TrimSpace(line) != "" {
|
2015-02-19 22:00:11 +03:00
|
|
|
res = append(res, line)
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
|
|
|
|
}
|
2015-02-19 22:00:11 +03:00
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-01 13:14:48 +03:00
|
|
|
// errorCheck matches errors in outStr against comments in source files.
|
|
|
|
|
// For each line of the source files which should generate an error,
|
|
|
|
|
// there should be a comment of the form // ERROR "regexp".
|
|
|
|
|
// If outStr has an error for a line which has no such comment,
|
|
|
|
|
// this function will report an error.
|
|
|
|
|
// Likewise if outStr does not have an error for a line which has a comment,
|
|
|
|
|
// or if the error message does not match the <regexp>.
|
2018-10-08 01:19:51 +00:00
|
|
|
// The <regexp> syntax is Perl but it's best to stick to egrep.
|
2018-06-01 13:14:48 +03:00
|
|
|
//
|
|
|
|
|
// Sources files are supplied as fullshort slice.
|
2018-10-08 01:19:51 +00:00
|
|
|
// It consists of pairs: full path to source file and its base name.
|
2023-02-06 17:10:22 -05:00
|
|
|
func (t test) errorCheck(outStr string, wantAuto bool, fullshort ...string) (err error) {
|
2015-02-19 22:00:11 +03:00
|
|
|
defer func() {
|
2023-02-06 17:10:22 -05:00
|
|
|
if testing.Verbose() && err != nil {
|
|
|
|
|
t.Logf("gc output:\n%s", outStr)
|
2015-02-19 22:00:11 +03:00
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
var errs []error
|
2016-09-22 13:50:16 -04:00
|
|
|
out := splitOutput(outStr, wantAuto)
|
2012-02-21 14:28:49 +11:00
|
|
|
|
2012-03-07 01:54:39 -05:00
|
|
|
// Cut directory name.
|
|
|
|
|
for i := range out {
|
2013-01-02 15:31:49 -05:00
|
|
|
for j := 0; j < len(fullshort); j += 2 {
|
|
|
|
|
full, short := fullshort[j], fullshort[j+1]
|
2024-08-05 12:57:33 -07:00
|
|
|
out[i] = replacePrefix(out[i], full, short)
|
2013-01-02 15:31:49 -05:00
|
|
|
}
|
|
|
|
|
}
|
2013-01-11 22:00:48 +01:00
|
|
|
|
2013-01-02 15:31:49 -05:00
|
|
|
var want []wantedError
|
|
|
|
|
for j := 0; j < len(fullshort); j += 2 {
|
|
|
|
|
full, short := fullshort[j], fullshort[j+1]
|
|
|
|
|
want = append(want, t.wantedErrors(full, short)...)
|
2012-03-07 01:54:39 -05:00
|
|
|
}
|
|
|
|
|
|
2013-01-02 15:31:49 -05:00
|
|
|
for _, we := range want {
|
2012-02-21 14:28:49 +11:00
|
|
|
var errmsgs []string
|
2016-09-22 13:50:16 -04:00
|
|
|
if we.auto {
|
|
|
|
|
errmsgs, out = partitionStrings("<autogenerated>", out)
|
|
|
|
|
} else {
|
|
|
|
|
errmsgs, out = partitionStrings(we.prefix, out)
|
|
|
|
|
}
|
2012-02-21 14:28:49 +11:00
|
|
|
if len(errmsgs) == 0 {
|
2012-03-07 01:54:39 -05:00
|
|
|
errs = append(errs, fmt.Errorf("%s:%d: missing error %q", we.file, we.lineNum, we.reStr))
|
2012-02-21 14:28:49 +11:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
matched := false
|
2013-02-01 23:10:02 -05:00
|
|
|
n := len(out)
|
2012-02-21 14:28:49 +11:00
|
|
|
for _, errmsg := range errmsgs {
|
2016-10-18 00:34:57 -04:00
|
|
|
// Assume errmsg says "file:line: foo".
|
|
|
|
|
// Cut leading "file:line: " to avoid accidental matching of file name instead of message.
|
|
|
|
|
text := errmsg
|
2021-09-22 10:46:32 -04:00
|
|
|
if _, suffix, ok := strings.Cut(text, " "); ok {
|
|
|
|
|
text = suffix
|
2016-10-18 00:34:57 -04:00
|
|
|
}
|
|
|
|
|
if we.re.MatchString(text) {
|
2012-02-21 14:28:49 +11:00
|
|
|
matched = true
|
|
|
|
|
} else {
|
|
|
|
|
out = append(out, errmsg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !matched {
|
2013-02-01 23:10:02 -05:00
|
|
|
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")))
|
2012-02-21 14:28:49 +11:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 16:36:45 +02:00
|
|
|
if len(out) > 0 {
|
|
|
|
|
errs = append(errs, fmt.Errorf("Unmatched Errors:"))
|
|
|
|
|
for _, errLine := range out {
|
|
|
|
|
errs = append(errs, fmt.Errorf("%s", errLine))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-21 14:28:49 +11:00
|
|
|
if len(errs) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if len(errs) == 1 {
|
|
|
|
|
return errs[0]
|
|
|
|
|
}
|
|
|
|
|
var buf bytes.Buffer
|
2012-03-07 01:54:39 -05:00
|
|
|
fmt.Fprintf(&buf, "\n")
|
2012-02-21 14:28:49 +11:00
|
|
|
for _, err := range errs {
|
|
|
|
|
fmt.Fprintf(&buf, "%s\n", err.Error())
|
|
|
|
|
}
|
|
|
|
|
return errors.New(buf.String())
|
2015-02-19 22:00:11 +03:00
|
|
|
}
|
2012-02-21 14:28:49 +11:00
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
func (test) updateErrors(out, file string) {
|
2016-02-29 10:43:18 -05:00
|
|
|
base := path.Base(file)
|
2015-02-19 22:00:11 +03:00
|
|
|
// Read in source file.
|
2023-02-06 17:10:22 -05:00
|
|
|
src, err := os.ReadFile(file)
|
2015-02-19 22:00:11 +03:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
lines := strings.Split(string(src), "\n")
|
|
|
|
|
// Remove old errors.
|
2021-09-22 10:46:32 -04:00
|
|
|
for i := range lines {
|
|
|
|
|
lines[i], _, _ = strings.Cut(lines[i], " // ERROR ")
|
2015-02-19 22:00:11 +03:00
|
|
|
}
|
|
|
|
|
// Parse new errors.
|
|
|
|
|
errors := make(map[int]map[string]bool)
|
2022-09-27 04:18:15 +00:00
|
|
|
tmpRe := regexp.MustCompile(`autotmp_\d+`)
|
2023-08-18 15:24:39 -04:00
|
|
|
fileRe := regexp.MustCompile(`(\.go):\d+:`)
|
2016-09-22 13:50:16 -04:00
|
|
|
for _, errStr := range splitOutput(out, false) {
|
2023-08-18 15:24:39 -04:00
|
|
|
m := fileRe.FindStringSubmatchIndex(errStr)
|
|
|
|
|
if len(m) != 4 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// The end of the file is the end of the first and only submatch.
|
|
|
|
|
errFile := errStr[:m[3]]
|
|
|
|
|
rest := errStr[m[3]+1:]
|
|
|
|
|
if errFile != file {
|
2015-02-19 22:00:11 +03:00
|
|
|
continue
|
|
|
|
|
}
|
2021-09-22 10:46:32 -04:00
|
|
|
lineStr, msg, ok := strings.Cut(rest, ":")
|
|
|
|
|
if !ok {
|
2015-02-19 22:00:11 +03:00
|
|
|
continue
|
|
|
|
|
}
|
2021-09-22 10:46:32 -04:00
|
|
|
line, err := strconv.Atoi(lineStr)
|
2015-02-19 22:00:11 +03:00
|
|
|
line--
|
|
|
|
|
if err != nil || line < 0 || line >= len(lines) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2025-04-14 15:34:30 +00:00
|
|
|
msg = strings.ReplaceAll(msg, file, base) // normalize file mentions in error itself
|
2016-02-29 10:43:18 -05:00
|
|
|
msg = strings.TrimLeft(msg, " \t")
|
2018-12-04 10:00:16 -05:00
|
|
|
for _, r := range []string{`\`, `*`, `+`, `?`, `[`, `]`, `(`, `)`} {
|
2025-04-14 15:34:30 +00:00
|
|
|
msg = strings.ReplaceAll(msg, r, `\`+r)
|
2015-02-19 22:00:11 +03:00
|
|
|
}
|
2025-04-14 15:34:30 +00:00
|
|
|
msg = strings.ReplaceAll(msg, `"`, `.`)
|
2015-02-19 22:00:11 +03:00
|
|
|
msg = tmpRe.ReplaceAllLiteralString(msg, `autotmp_[0-9]+`)
|
|
|
|
|
if errors[line] == nil {
|
|
|
|
|
errors[line] = make(map[string]bool)
|
|
|
|
|
}
|
|
|
|
|
errors[line][msg] = true
|
|
|
|
|
}
|
|
|
|
|
// Add new errors.
|
|
|
|
|
for line, errs := range errors {
|
|
|
|
|
var sorted []string
|
|
|
|
|
for e := range errs {
|
|
|
|
|
sorted = append(sorted, e)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(sorted)
|
|
|
|
|
lines[line] += " // ERROR"
|
|
|
|
|
for _, e := range sorted {
|
|
|
|
|
lines[line] += fmt.Sprintf(` "%s$"`, e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Write new file.
|
2023-02-06 17:10:22 -05:00
|
|
|
err = os.WriteFile(file, []byte(strings.Join(lines, "\n")), 0640)
|
2015-02-19 22:00:11 +03:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Polish.
|
2023-02-06 17:10:22 -05:00
|
|
|
exec.Command(goTool, "fmt", file).CombinedOutput()
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
|
|
|
|
|
2014-03-11 23:58:24 -04:00
|
|
|
// matchPrefix reports whether s is of the form ^(.*/)?prefix(:|[),
|
|
|
|
|
// That is, it needs the file name prefix followed by a : or a [,
|
|
|
|
|
// and possibly preceded by a directory name.
|
|
|
|
|
func matchPrefix(s, prefix string) bool {
|
|
|
|
|
i := strings.Index(s, ":")
|
|
|
|
|
if i < 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
j := strings.LastIndex(s[:i], "/")
|
|
|
|
|
s = s[j+1:]
|
|
|
|
|
if len(s) <= len(prefix) || s[:len(prefix)] != prefix {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
switch s[len(prefix)] {
|
|
|
|
|
case '[', ':':
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func partitionStrings(prefix string, strs []string) (matched, unmatched []string) {
|
2012-02-21 14:28:49 +11:00
|
|
|
for _, s := range strs {
|
2014-03-11 23:58:24 -04:00
|
|
|
if matchPrefix(s, prefix) {
|
2012-02-21 14:28:49 +11:00
|
|
|
matched = append(matched, s)
|
|
|
|
|
} else {
|
|
|
|
|
unmatched = append(unmatched, s)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type wantedError struct {
|
2014-08-01 22:34:36 +02:00
|
|
|
reStr string
|
|
|
|
|
re *regexp.Regexp
|
|
|
|
|
lineNum int
|
2016-09-22 13:50:16 -04:00
|
|
|
auto bool // match <autogenerated> line
|
2014-08-01 22:34:36 +02:00
|
|
|
file string
|
|
|
|
|
prefix string
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
errRx = regexp.MustCompile(`// (?:GC_)?ERROR (.*)`)
|
2016-09-22 13:50:16 -04:00
|
|
|
errAutoRx = regexp.MustCompile(`// (?:GC_)?ERRORAUTO (.*)`)
|
2012-02-21 14:28:49 +11:00
|
|
|
errQuotesRx = regexp.MustCompile(`"([^"]*)"`)
|
2022-09-27 04:18:15 +00:00
|
|
|
lineRx = regexp.MustCompile(`LINE(([+-])(\d+))?`)
|
2012-02-21 14:28:49 +11:00
|
|
|
)
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
func (t test) wantedErrors(file, short string) (errs []wantedError) {
|
2014-03-11 23:58:24 -04:00
|
|
|
cache := make(map[string]*regexp.Regexp)
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
src, _ := os.ReadFile(file)
|
2012-10-06 09:23:31 +02:00
|
|
|
for i, line := range strings.Split(string(src), "\n") {
|
2012-02-21 14:28:49 +11:00
|
|
|
lineNum := i + 1
|
|
|
|
|
if strings.Contains(line, "////") {
|
|
|
|
|
// double comment disables ERROR
|
|
|
|
|
continue
|
|
|
|
|
}
|
2016-09-22 13:50:16 -04:00
|
|
|
var auto bool
|
|
|
|
|
m := errAutoRx.FindStringSubmatch(line)
|
|
|
|
|
if m != nil {
|
|
|
|
|
auto = true
|
|
|
|
|
} else {
|
|
|
|
|
m = errRx.FindStringSubmatch(line)
|
|
|
|
|
}
|
2012-02-21 14:28:49 +11:00
|
|
|
if m == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
all := m[1]
|
|
|
|
|
mm := errQuotesRx.FindAllStringSubmatch(all, -1)
|
|
|
|
|
if mm == nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatalf("%s:%d: invalid errchk line: %s", t.goFileName(), lineNum, line)
|
2012-02-21 14:28:49 +11:00
|
|
|
}
|
|
|
|
|
for _, m := range mm {
|
|
|
|
|
rx := lineRx.ReplaceAllStringFunc(m[1], func(m string) string {
|
|
|
|
|
n := lineNum
|
|
|
|
|
if strings.HasPrefix(m, "LINE+") {
|
|
|
|
|
delta, _ := strconv.Atoi(m[5:])
|
|
|
|
|
n += delta
|
|
|
|
|
} else if strings.HasPrefix(m, "LINE-") {
|
|
|
|
|
delta, _ := strconv.Atoi(m[5:])
|
|
|
|
|
n -= delta
|
|
|
|
|
}
|
2012-10-06 09:23:31 +02:00
|
|
|
return fmt.Sprintf("%s:%d", short, n)
|
2012-02-21 14:28:49 +11:00
|
|
|
})
|
2014-03-11 23:58:24 -04:00
|
|
|
re := cache[rx]
|
|
|
|
|
if re == nil {
|
|
|
|
|
var err error
|
|
|
|
|
re, err = regexp.Compile(rx)
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatalf("%s:%d: invalid regexp \"%s\" in ERROR line: %v", t.goFileName(), lineNum, rx, err)
|
2014-03-11 23:58:24 -04:00
|
|
|
}
|
|
|
|
|
cache[rx] = re
|
2013-02-01 23:10:02 -05:00
|
|
|
}
|
2014-03-11 23:58:24 -04:00
|
|
|
prefix := fmt.Sprintf("%s:%d", short, lineNum)
|
2012-02-21 14:28:49 +11:00
|
|
|
errs = append(errs, wantedError{
|
2014-08-01 22:34:36 +02:00
|
|
|
reStr: rx,
|
|
|
|
|
re: re,
|
|
|
|
|
prefix: prefix,
|
2016-09-22 13:50:16 -04:00
|
|
|
auto: auto,
|
2014-08-01 22:34:36 +02:00
|
|
|
lineNum: lineNum,
|
|
|
|
|
file: short,
|
2012-02-21 14:28:49 +11:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
2012-09-23 13:16:14 -04:00
|
|
|
|
2018-03-01 01:39:01 +01:00
|
|
|
const (
|
|
|
|
|
// Regexp to match a single opcode check: optionally begin with "-" (to indicate
|
|
|
|
|
// a negative check), followed by a string literal enclosed in "" or ``. For "",
|
|
|
|
|
// backslashes must be handled.
|
|
|
|
|
reMatchCheck = `-?(?:\x60[^\x60]*\x60|"(?:[^"\\]|\\.)*")`
|
|
|
|
|
)
|
|
|
|
|
|
2018-02-27 01:59:58 +01:00
|
|
|
var (
|
2018-03-01 01:39:01 +01:00
|
|
|
// Regexp to split a line in code and comment, trimming spaces
|
2018-05-19 09:42:52 +02:00
|
|
|
rxAsmComment = regexp.MustCompile(`^\s*(.*?)\s*(?://\s*(.+)\s*)?$`)
|
2018-03-01 01:39:01 +01:00
|
|
|
|
2018-05-19 09:42:52 +02:00
|
|
|
// Regexp to extract an architecture check: architecture name (or triplet),
|
|
|
|
|
// followed by semi-colon, followed by a comma-separated list of opcode checks.
|
|
|
|
|
// Extraneous spaces are ignored.
|
2024-03-06 17:44:03 +03:00
|
|
|
//
|
|
|
|
|
// An example: arm64/v8.1 : -`ADD` , `SUB`
|
|
|
|
|
// "(\w+)" matches "arm64" (architecture name)
|
|
|
|
|
// "(/[\w.]+)?" matches "v8.1" (architecture version)
|
|
|
|
|
// "(/\w*)?" doesn't match anything here (it's an optional part of the triplet)
|
|
|
|
|
// "\s*:\s*" matches " : " (semi-colon)
|
|
|
|
|
// "(" starts a capturing group
|
|
|
|
|
// first reMatchCheck matches "-`ADD`"
|
|
|
|
|
// `(?:" starts a non-capturing group
|
|
|
|
|
// "\s*,\s*` matches " , "
|
|
|
|
|
// second reMatchCheck matches "`SUB`"
|
|
|
|
|
// ")*)" closes started groups; "*" means that there might be other elements in the comma-separated list
|
|
|
|
|
rxAsmPlatform = regexp.MustCompile(`(\w+)(/[\w.]+)?(/\w*)?\s*:\s*(` + reMatchCheck + `(?:\s*,\s*` + reMatchCheck + `)*)`)
|
2018-03-01 01:39:01 +01:00
|
|
|
|
|
|
|
|
// Regexp to extract a single opcoded check
|
|
|
|
|
rxAsmCheck = regexp.MustCompile(reMatchCheck)
|
2018-04-15 19:00:27 +02:00
|
|
|
|
|
|
|
|
// List of all architecture variants. Key is the GOARCH architecture,
|
2018-04-15 22:53:58 +02:00
|
|
|
// value[0] is the variant-changing environment variable, and values[1:]
|
2018-04-15 19:00:27 +02:00
|
|
|
// are the supported variants.
|
|
|
|
|
archVariants = map[string][]string{
|
2020-10-06 14:42:15 -07:00
|
|
|
"386": {"GO386", "sse2", "softfloat"},
|
2021-09-15 04:30:49 +00:00
|
|
|
"amd64": {"GOAMD64", "v1", "v2", "v3", "v4"},
|
2023-07-29 18:25:42 -07:00
|
|
|
"arm": {"GOARM", "5", "6", "7", "7,softfloat"},
|
2024-03-06 17:44:03 +03:00
|
|
|
"arm64": {"GOARM64", "v8.0", "v8.1"},
|
2022-05-19 20:01:10 +08:00
|
|
|
"loong64": {},
|
2018-04-15 19:00:27 +02:00
|
|
|
"mips": {"GOMIPS", "hardfloat", "softfloat"},
|
2018-04-26 15:37:27 +02:00
|
|
|
"mips64": {"GOMIPS64", "hardfloat", "softfloat"},
|
2022-10-31 11:47:17 -05:00
|
|
|
"ppc64": {"GOPPC64", "power8", "power9", "power10"},
|
|
|
|
|
"ppc64le": {"GOPPC64", "power8", "power9", "power10"},
|
2023-01-18 15:20:15 -06:00
|
|
|
"ppc64x": {}, // A pseudo-arch representing both ppc64 and ppc64le
|
2018-04-15 19:00:27 +02:00
|
|
|
"s390x": {},
|
2019-03-05 01:56:17 +01:00
|
|
|
"wasm": {},
|
2024-12-02 15:47:25 +01:00
|
|
|
"riscv64": {"GORISCV64", "rva20u64", "rva22u64", "rva23u64"},
|
2018-04-15 19:00:27 +02:00
|
|
|
}
|
2018-02-27 01:59:58 +01:00
|
|
|
)
|
|
|
|
|
|
2018-04-15 19:00:27 +02:00
|
|
|
// wantedAsmOpcode is a single asmcheck check
|
2018-02-27 01:59:58 +01:00
|
|
|
type wantedAsmOpcode struct {
|
2018-03-03 19:44:47 +01:00
|
|
|
fileline string // original source file/line (eg: "/path/foo.go:45")
|
|
|
|
|
line int // original source line
|
|
|
|
|
opcode *regexp.Regexp // opcode check to be performed on assembly output
|
|
|
|
|
negative bool // true if the check is supposed to fail rather than pass
|
|
|
|
|
found bool // true if the opcode check matched at least one in the output
|
2018-02-27 01:59:58 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-06 14:42:15 -07:00
|
|
|
// A build environment triplet separated by slashes (eg: linux/386/sse2).
|
2018-04-15 22:53:58 +02:00
|
|
|
// The third field can be empty if the arch does not support variants (eg: "plan9/amd64/")
|
2018-04-15 19:00:27 +02:00
|
|
|
type buildEnv string
|
|
|
|
|
|
|
|
|
|
// Environ returns the environment it represents in cmd.Environ() "key=val" format
|
2020-10-06 14:42:15 -07:00
|
|
|
// For instance, "linux/386/sse2".Environ() returns {"GOOS=linux", "GOARCH=386", "GO386=sse2"}
|
2018-04-15 19:00:27 +02:00
|
|
|
func (b buildEnv) Environ() []string {
|
|
|
|
|
fields := strings.Split(string(b), "/")
|
2018-04-15 22:53:58 +02:00
|
|
|
if len(fields) != 3 {
|
2018-04-15 19:00:27 +02:00
|
|
|
panic("invalid buildEnv string: " + string(b))
|
|
|
|
|
}
|
|
|
|
|
env := []string{"GOOS=" + fields[0], "GOARCH=" + fields[1]}
|
2018-04-15 22:53:58 +02:00
|
|
|
if fields[2] != "" {
|
2018-04-15 19:00:27 +02:00
|
|
|
env = append(env, archVariants[fields[1]][0]+"="+fields[2])
|
|
|
|
|
}
|
|
|
|
|
return env
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// asmChecks represents all the asmcheck checks present in a test file
|
|
|
|
|
// The outer map key is the build triplet in which the checks must be performed.
|
|
|
|
|
// The inner map key represent the source file line ("filename.go:1234") at which the
|
|
|
|
|
// checks must be performed.
|
|
|
|
|
type asmChecks map[buildEnv]map[string][]wantedAsmOpcode
|
|
|
|
|
|
|
|
|
|
// Envs returns all the buildEnv in which at least one check is present
|
|
|
|
|
func (a asmChecks) Envs() []buildEnv {
|
|
|
|
|
var envs []buildEnv
|
|
|
|
|
for e := range a {
|
|
|
|
|
envs = append(envs, e)
|
|
|
|
|
}
|
|
|
|
|
sort.Slice(envs, func(i, j int) bool {
|
|
|
|
|
return string(envs[i]) < string(envs[j])
|
|
|
|
|
})
|
|
|
|
|
return envs
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
func (t test) wantedAsmOpcodes(fn string) asmChecks {
|
2018-04-15 19:00:27 +02:00
|
|
|
ops := make(asmChecks)
|
2018-02-27 01:59:58 +01:00
|
|
|
|
2018-03-01 01:39:01 +01:00
|
|
|
comment := ""
|
2023-02-06 17:10:22 -05:00
|
|
|
src, err := os.ReadFile(fn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2018-02-27 01:59:58 +01:00
|
|
|
for i, line := range strings.Split(string(src), "\n") {
|
2018-03-01 01:39:01 +01:00
|
|
|
matches := rxAsmComment.FindStringSubmatch(line)
|
|
|
|
|
code, cmt := matches[1], matches[2]
|
|
|
|
|
|
|
|
|
|
// Keep comments pending in the comment variable until
|
|
|
|
|
// we find a line that contains some code.
|
|
|
|
|
comment += " " + cmt
|
|
|
|
|
if code == "" {
|
2018-02-27 01:59:58 +01:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-01 01:39:01 +01:00
|
|
|
// Parse and extract any architecture check from comments,
|
|
|
|
|
// made by one architecture name and multiple checks.
|
2018-02-27 01:59:58 +01:00
|
|
|
lnum := fn + ":" + strconv.Itoa(i+1)
|
2018-03-01 01:39:01 +01:00
|
|
|
for _, ac := range rxAsmPlatform.FindAllStringSubmatch(comment, -1) {
|
2018-04-15 19:00:27 +02:00
|
|
|
archspec, allchecks := ac[1:4], ac[4]
|
|
|
|
|
|
|
|
|
|
var arch, subarch, os string
|
|
|
|
|
switch {
|
2020-10-06 14:42:15 -07:00
|
|
|
case archspec[2] != "": // 3 components: "linux/386/sse2"
|
2018-04-15 19:00:27 +02:00
|
|
|
os, arch, subarch = archspec[0], archspec[1][1:], archspec[2][1:]
|
2020-10-06 14:42:15 -07:00
|
|
|
case archspec[1] != "": // 2 components: "386/sse2"
|
2018-04-15 19:00:27 +02:00
|
|
|
os, arch, subarch = "linux", archspec[0], archspec[1][1:]
|
2020-10-06 14:42:15 -07:00
|
|
|
default: // 1 component: "386"
|
2018-04-15 19:00:27 +02:00
|
|
|
os, arch, subarch = "linux", archspec[0], ""
|
2019-03-05 01:56:17 +01:00
|
|
|
if arch == "wasm" {
|
|
|
|
|
os = "js"
|
|
|
|
|
}
|
2018-04-15 19:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, ok := archVariants[arch]; !ok {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatalf("%s:%d: unsupported architecture: %v", t.goFileName(), i+1, arch)
|
2018-04-15 19:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the build environments corresponding the above specifiers
|
|
|
|
|
envs := make([]buildEnv, 0, 4)
|
2023-01-18 15:20:15 -06:00
|
|
|
arches := []string{arch}
|
|
|
|
|
// ppc64x is a pseudo-arch, generate tests for both endian variants.
|
|
|
|
|
if arch == "ppc64x" {
|
|
|
|
|
arches = []string{"ppc64", "ppc64le"}
|
|
|
|
|
}
|
|
|
|
|
for _, arch := range arches {
|
|
|
|
|
if subarch != "" {
|
|
|
|
|
envs = append(envs, buildEnv(os+"/"+arch+"/"+subarch))
|
2018-04-15 19:00:27 +02:00
|
|
|
} else {
|
2023-01-18 15:20:15 -06:00
|
|
|
subarchs := archVariants[arch]
|
|
|
|
|
if len(subarchs) == 0 {
|
|
|
|
|
envs = append(envs, buildEnv(os+"/"+arch+"/"))
|
|
|
|
|
} else {
|
|
|
|
|
for _, sa := range archVariants[arch][1:] {
|
|
|
|
|
envs = append(envs, buildEnv(os+"/"+arch+"/"+sa))
|
|
|
|
|
}
|
2018-04-15 19:00:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-01 01:39:01 +01:00
|
|
|
|
|
|
|
|
for _, m := range rxAsmCheck.FindAllString(allchecks, -1) {
|
|
|
|
|
negative := false
|
|
|
|
|
if m[0] == '-' {
|
|
|
|
|
negative = true
|
|
|
|
|
m = m[1:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rxsrc, err := strconv.Unquote(m)
|
|
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatalf("%s:%d: error unquoting string: %v", t.goFileName(), i+1, err)
|
2018-03-01 01:39:01 +01:00
|
|
|
}
|
2018-03-01 01:55:33 +01:00
|
|
|
|
|
|
|
|
// Compile the checks as regular expressions. Notice that we
|
|
|
|
|
// consider checks as matching from the beginning of the actual
|
|
|
|
|
// assembler source (that is, what is left on each line of the
|
|
|
|
|
// compile -S output after we strip file/line info) to avoid
|
|
|
|
|
// trivial bugs such as "ADD" matching "FADD". This
|
|
|
|
|
// doesn't remove genericity: it's still possible to write
|
|
|
|
|
// something like "F?ADD", but we make common cases simpler
|
|
|
|
|
// to get right.
|
|
|
|
|
oprx, err := regexp.Compile("^" + rxsrc)
|
2018-03-01 01:39:01 +01:00
|
|
|
if err != nil {
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Fatalf("%s:%d: %v", t.goFileName(), i+1, err)
|
2018-03-01 01:39:01 +01:00
|
|
|
}
|
2018-04-15 19:00:27 +02:00
|
|
|
|
|
|
|
|
for _, env := range envs {
|
|
|
|
|
if ops[env] == nil {
|
|
|
|
|
ops[env] = make(map[string][]wantedAsmOpcode)
|
|
|
|
|
}
|
|
|
|
|
ops[env][lnum] = append(ops[env][lnum], wantedAsmOpcode{
|
|
|
|
|
negative: negative,
|
|
|
|
|
fileline: lnum,
|
|
|
|
|
line: i + 1,
|
|
|
|
|
opcode: oprx,
|
|
|
|
|
})
|
2018-03-01 01:39:01 +01:00
|
|
|
}
|
2018-02-27 01:59:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-03-01 01:39:01 +01:00
|
|
|
comment = ""
|
2018-02-27 01:59:58 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-15 19:00:27 +02:00
|
|
|
return ops
|
2018-02-27 01:59:58 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
func (t test) asmCheck(outStr string, fn string, env buildEnv, fullops map[string][]wantedAsmOpcode) error {
|
2018-03-03 19:44:47 +01:00
|
|
|
// The assembly output contains the concatenated dump of multiple functions.
|
|
|
|
|
// the first line of each function begins at column 0, while the rest is
|
|
|
|
|
// indented by a tabulation. These data structures help us index the
|
|
|
|
|
// output by function.
|
|
|
|
|
functionMarkers := make([]int, 1)
|
|
|
|
|
lineFuncMap := make(map[string]int)
|
|
|
|
|
|
|
|
|
|
lines := strings.Split(outStr, "\n")
|
2018-02-27 01:59:58 +01:00
|
|
|
rxLine := regexp.MustCompile(fmt.Sprintf(`\((%s:\d+)\)\s+(.*)`, regexp.QuoteMeta(fn)))
|
|
|
|
|
|
2018-03-03 19:44:47 +01:00
|
|
|
for nl, line := range lines {
|
|
|
|
|
// Check if this line begins a function
|
|
|
|
|
if len(line) > 0 && line[0] != '\t' {
|
|
|
|
|
functionMarkers = append(functionMarkers, nl)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search if this line contains a assembly opcode (which is prefixed by the
|
|
|
|
|
// original source file/line in parenthesis)
|
2018-02-27 01:59:58 +01:00
|
|
|
matches := rxLine.FindStringSubmatch(line)
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2018-03-03 19:44:47 +01:00
|
|
|
srcFileLine, asm := matches[1], matches[2]
|
|
|
|
|
|
|
|
|
|
// Associate the original file/line information to the current
|
|
|
|
|
// function in the output; it will be useful to dump it in case
|
|
|
|
|
// of error.
|
|
|
|
|
lineFuncMap[srcFileLine] = len(functionMarkers) - 1
|
2018-02-27 01:59:58 +01:00
|
|
|
|
2018-03-03 19:44:47 +01:00
|
|
|
// If there are opcode checks associated to this source file/line,
|
|
|
|
|
// run the checks.
|
|
|
|
|
if ops, found := fullops[srcFileLine]; found {
|
|
|
|
|
for i := range ops {
|
|
|
|
|
if !ops[i].found && ops[i].opcode.FindString(asm) != "" {
|
|
|
|
|
ops[i].found = true
|
|
|
|
|
}
|
2018-02-27 01:59:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-03 19:44:47 +01:00
|
|
|
functionMarkers = append(functionMarkers, len(lines))
|
2018-02-27 01:59:58 +01:00
|
|
|
|
2018-03-01 01:56:07 +01:00
|
|
|
var failed []wantedAsmOpcode
|
2018-02-27 01:59:58 +01:00
|
|
|
for _, ops := range fullops {
|
|
|
|
|
for _, o := range ops {
|
2018-03-01 01:56:07 +01:00
|
|
|
// There's a failure if a negative match was found,
|
|
|
|
|
// or a positive match was not found.
|
|
|
|
|
if o.negative == o.found {
|
|
|
|
|
failed = append(failed, o)
|
2018-02-27 01:59:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-01 01:56:07 +01:00
|
|
|
if len(failed) == 0 {
|
2023-02-06 17:10:22 -05:00
|
|
|
return nil
|
2018-02-27 01:59:58 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:10:22 -05:00
|
|
|
// At least one asmcheck failed; report them.
|
2018-03-03 19:44:47 +01:00
|
|
|
lastFunction := -1
|
2018-02-27 01:59:58 +01:00
|
|
|
var errbuf bytes.Buffer
|
|
|
|
|
fmt.Fprintln(&errbuf)
|
2023-02-06 17:10:22 -05:00
|
|
|
sort.Slice(failed, func(i, j int) bool { return failed[i].line < failed[j].line })
|
2018-03-01 01:56:07 +01:00
|
|
|
for _, o := range failed {
|
2018-03-03 19:44:47 +01:00
|
|
|
// Dump the function in which this opcode check was supposed to
|
|
|
|
|
// pass but failed.
|
|
|
|
|
funcIdx := lineFuncMap[o.fileline]
|
|
|
|
|
if funcIdx != 0 && funcIdx != lastFunction {
|
|
|
|
|
funcLines := lines[functionMarkers[funcIdx]:functionMarkers[funcIdx+1]]
|
2023-02-06 17:10:22 -05:00
|
|
|
t.Log(strings.Join(funcLines, "\n"))
|
2018-03-03 19:44:47 +01:00
|
|
|
lastFunction = funcIdx // avoid printing same function twice
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-01 01:56:07 +01:00
|
|
|
if o.negative {
|
2018-04-15 19:00:27 +02:00
|
|
|
fmt.Fprintf(&errbuf, "%s:%d: %s: wrong opcode found: %q\n", t.goFileName(), o.line, env, o.opcode.String())
|
2018-03-01 01:56:07 +01:00
|
|
|
} else {
|
2018-04-15 19:00:27 +02:00
|
|
|
fmt.Fprintf(&errbuf, "%s:%d: %s: opcode not found: %q\n", t.goFileName(), o.line, env, o.opcode.String())
|
2018-03-01 01:56:07 +01:00
|
|
|
}
|
2018-02-27 01:59:58 +01:00
|
|
|
}
|
2023-02-06 17:10:22 -05:00
|
|
|
return errors.New(errbuf.String())
|
2018-02-27 01:59:58 +01:00
|
|
|
}
|
|
|
|
|
|
2013-01-12 17:52:52 +11:00
|
|
|
// defaultRunOutputLimit returns the number of runoutput tests that
|
|
|
|
|
// can be executed in parallel.
|
|
|
|
|
func defaultRunOutputLimit() int {
|
|
|
|
|
const maxArmCPU = 2
|
|
|
|
|
|
|
|
|
|
cpu := runtime.NumCPU()
|
|
|
|
|
if runtime.GOARCH == "arm" && cpu > maxArmCPU {
|
|
|
|
|
cpu = maxArmCPU
|
|
|
|
|
}
|
|
|
|
|
return cpu
|
|
|
|
|
}
|
2013-01-28 21:29:45 +01:00
|
|
|
|
2023-02-08 11:40:06 -05:00
|
|
|
func TestShouldTest(t *testing.T) {
|
|
|
|
|
if *shard != 0 {
|
|
|
|
|
t.Skipf("nothing to test on shard index %d", *shard)
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-28 21:29:45 +01:00
|
|
|
assert := func(ok bool, _ string) {
|
2023-02-08 11:40:06 -05:00
|
|
|
t.Helper()
|
2013-01-28 21:29:45 +01:00
|
|
|
if !ok {
|
2023-02-08 11:40:06 -05:00
|
|
|
t.Error("test case failed")
|
2013-01-28 21:29:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 11:40:06 -05:00
|
|
|
assertNot := func(ok bool, _ string) { t.Helper(); assert(!ok, "") }
|
2013-08-13 12:25:41 -04:00
|
|
|
|
|
|
|
|
// Simple tests.
|
2013-01-28 21:29:45 +01:00
|
|
|
assert(shouldTest("// +build linux", "linux", "arm"))
|
|
|
|
|
assert(shouldTest("// +build !windows", "linux", "arm"))
|
|
|
|
|
assertNot(shouldTest("// +build !windows", "windows", "amd64"))
|
2013-08-13 12:25:41 -04:00
|
|
|
|
|
|
|
|
// A file with no build tags will always be tested.
|
2013-01-28 21:29:45 +01:00
|
|
|
assert(shouldTest("// This is a test.", "os", "arch"))
|
2013-08-13 12:25:41 -04:00
|
|
|
|
|
|
|
|
// Build tags separated by a space are OR-ed together.
|
|
|
|
|
assertNot(shouldTest("// +build arm 386", "linux", "amd64"))
|
|
|
|
|
|
2013-12-27 08:59:02 -08:00
|
|
|
// Build tags separated by a comma are AND-ed together.
|
2013-08-13 12:25:41 -04:00
|
|
|
assertNot(shouldTest("// +build !windows,!plan9", "windows", "amd64"))
|
|
|
|
|
assertNot(shouldTest("// +build !windows,!plan9", "plan9", "386"))
|
|
|
|
|
|
|
|
|
|
// Build tags on multiple lines are AND-ed together.
|
|
|
|
|
assert(shouldTest("// +build !windows\n// +build amd64", "linux", "amd64"))
|
|
|
|
|
assertNot(shouldTest("// +build !windows\n// +build amd64", "windows", "amd64"))
|
|
|
|
|
|
|
|
|
|
// Test that (!a OR !b) matches anything.
|
|
|
|
|
assert(shouldTest("// +build !windows !plan9", "windows", "amd64"))
|
2023-10-18 00:26:42 +07:00
|
|
|
|
|
|
|
|
// Test that //go:build tag match.
|
|
|
|
|
assert(shouldTest("//go:build go1.4", "linux", "amd64"))
|
2013-01-28 21:29:45 +01:00
|
|
|
}
|
2013-02-14 14:21:26 -05:00
|
|
|
|
2020-03-24 16:18:02 -04:00
|
|
|
// overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
|
|
|
|
|
func overlayDir(dstRoot, srcRoot string) error {
|
|
|
|
|
dstRoot = filepath.Clean(dstRoot)
|
|
|
|
|
if err := os.MkdirAll(dstRoot, 0777); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srcRoot, err := filepath.Abs(srcRoot)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-04 18:20:17 -05:00
|
|
|
return filepath.WalkDir(srcRoot, func(srcPath string, d fs.DirEntry, err error) error {
|
2020-03-24 16:18:02 -04:00
|
|
|
if err != nil || srcPath == srcRoot {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
suffix := strings.TrimPrefix(srcPath, srcRoot)
|
|
|
|
|
for len(suffix) > 0 && suffix[0] == filepath.Separator {
|
|
|
|
|
suffix = suffix[1:]
|
|
|
|
|
}
|
|
|
|
|
dstPath := filepath.Join(dstRoot, suffix)
|
|
|
|
|
|
2020-11-04 18:20:17 -05:00
|
|
|
var info fs.FileInfo
|
|
|
|
|
if d.Type()&os.ModeSymlink != 0 {
|
2020-03-24 16:18:02 -04:00
|
|
|
info, err = os.Stat(srcPath)
|
2020-11-04 18:20:17 -05:00
|
|
|
} else {
|
|
|
|
|
info, err = d.Info()
|
2020-03-24 16:18:02 -04:00
|
|
|
}
|
2020-11-04 18:20:17 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
perm := info.Mode() & os.ModePerm
|
2020-03-24 16:18:02 -04:00
|
|
|
|
|
|
|
|
// Always copy directories (don't symlink them).
|
|
|
|
|
// If we add a file in the overlay, we don't want to add it in the original.
|
|
|
|
|
if info.IsDir() {
|
|
|
|
|
return os.MkdirAll(dstPath, perm|0200)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the OS supports symlinks, use them instead of copying bytes.
|
|
|
|
|
if err := os.Symlink(srcPath, dstPath); err == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise, copy the bytes.
|
|
|
|
|
src, err := os.Open(srcPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer src.Close()
|
|
|
|
|
|
|
|
|
|
dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(dst, src)
|
|
|
|
|
if closeErr := dst.Close(); err == nil {
|
|
|
|
|
err = closeErr
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
})
|
|
|
|
|
}
|
2020-12-03 11:11:05 -08:00
|
|
|
|
2022-03-31 09:34:50 -07:00
|
|
|
// The following sets of files are excluded from testing depending on configuration.
|
|
|
|
|
// The types2Failures(32Bit) files pass with the 1.17 compiler but don't pass with
|
|
|
|
|
// the 1.18 compiler using the new types2 type checker, or pass with sub-optimal
|
|
|
|
|
// error(s).
|
2021-05-17 13:59:25 -07:00
|
|
|
|
2022-03-31 09:34:50 -07:00
|
|
|
// List of files that the compiler cannot errorcheck with the new typechecker (types2).
|
2021-07-02 13:18:03 -07:00
|
|
|
var types2Failures = setOf(
|
test: re-enable most go/tests that were disabled because of types2 differences
I made the default be that, where there are differences between types2
and -G=0 error messages, we want errorcheck tests to pass types2.
Typically, we can get errorcheck to pass on types2 and -G=0 if they give
the same number of error messages on the same lines, just different
wording. If they give a different number of error messages, then I made
types2 pass. I added an exception list for -G=0 to cover those cases
where -G=0 and types give different numbers of error messages.
Because types2 does not run if there are syntax errors, for several
tests, I had to split the tests into two parts in order to get all the
indicated errors to be reported in types2 (bug228.go, bug388.go,
issue11610.go, issue14520.go)
I tried to preserve the GCCGO labeling correctly (but may have gotten
some wrong). When types2 now matches where a GCCGO error previously
occurred, I transformed GCCGO_ERROR -> ERROR. When types2 no longer
reports an error in a certain place, I transformed ERROR -> GCCGO_ERROR.
When types2 reports an error in a new place, I used GC_ERROR.
The remaining entries in types2Failures are things that I think we
probably still need to fix - either actually missing errors in types2,
or cases where types2 gives worse errors than -G=0.
Change-Id: I7f01e82b322b16094096b67d7ed2bb39b410c34f
Reviewed-on: https://go-review.googlesource.com/c/go/+/372854
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2021-12-03 16:10:10 -08:00
|
|
|
"shift1.go", // types2 reports two new errors which are probably not right
|
|
|
|
|
"fixedbugs/issue10700.go", // types2 should give hint about ptr to interface
|
|
|
|
|
"fixedbugs/issue18331.go", // missing error about misuse of //go:noescape (irgen needs code from noder)
|
|
|
|
|
"fixedbugs/issue18419.go", // types2 reports no field or method member, but should say unexported
|
2022-03-31 09:34:50 -07:00
|
|
|
"fixedbugs/issue20233.go", // types2 reports two instead of one error (preference: 1.17 compiler)
|
|
|
|
|
"fixedbugs/issue20245.go", // types2 reports two instead of one error (preference: 1.17 compiler)
|
test: re-enable most go/tests that were disabled because of types2 differences
I made the default be that, where there are differences between types2
and -G=0 error messages, we want errorcheck tests to pass types2.
Typically, we can get errorcheck to pass on types2 and -G=0 if they give
the same number of error messages on the same lines, just different
wording. If they give a different number of error messages, then I made
types2 pass. I added an exception list for -G=0 to cover those cases
where -G=0 and types give different numbers of error messages.
Because types2 does not run if there are syntax errors, for several
tests, I had to split the tests into two parts in order to get all the
indicated errors to be reported in types2 (bug228.go, bug388.go,
issue11610.go, issue14520.go)
I tried to preserve the GCCGO labeling correctly (but may have gotten
some wrong). When types2 now matches where a GCCGO error previously
occurred, I transformed GCCGO_ERROR -> ERROR. When types2 no longer
reports an error in a certain place, I transformed ERROR -> GCCGO_ERROR.
When types2 reports an error in a new place, I used GC_ERROR.
The remaining entries in types2Failures are things that I think we
probably still need to fix - either actually missing errors in types2,
or cases where types2 gives worse errors than -G=0.
Change-Id: I7f01e82b322b16094096b67d7ed2bb39b410c34f
Reviewed-on: https://go-review.googlesource.com/c/go/+/372854
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2021-12-03 16:10:10 -08:00
|
|
|
"fixedbugs/issue31053.go", // types2 reports "unknown field" instead of "cannot refer to unexported field"
|
2021-07-02 13:18:03 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var types2Failures32Bit = setOf(
|
|
|
|
|
"printbig.go", // large untyped int passed to print (32-bit)
|
|
|
|
|
"fixedbugs/bug114.go", // large untyped int passed to println (32-bit)
|
|
|
|
|
"fixedbugs/issue23305.go", // large untyped int passed to println (32-bit)
|
|
|
|
|
)
|
|
|
|
|
|
2022-03-31 09:34:50 -07:00
|
|
|
// In all of these cases, the 1.17 compiler reports reasonable errors, but either the
|
|
|
|
|
// 1.17 or 1.18 compiler report extra errors, so we can't match correctly on both. We
|
|
|
|
|
// now set the patterns to match correctly on all the 1.18 errors.
|
|
|
|
|
// This list remains here just as a reference and for comparison - these files all pass.
|
|
|
|
|
var _ = setOf(
|
test: re-enable most go/tests that were disabled because of types2 differences
I made the default be that, where there are differences between types2
and -G=0 error messages, we want errorcheck tests to pass types2.
Typically, we can get errorcheck to pass on types2 and -G=0 if they give
the same number of error messages on the same lines, just different
wording. If they give a different number of error messages, then I made
types2 pass. I added an exception list for -G=0 to cover those cases
where -G=0 and types give different numbers of error messages.
Because types2 does not run if there are syntax errors, for several
tests, I had to split the tests into two parts in order to get all the
indicated errors to be reported in types2 (bug228.go, bug388.go,
issue11610.go, issue14520.go)
I tried to preserve the GCCGO labeling correctly (but may have gotten
some wrong). When types2 now matches where a GCCGO error previously
occurred, I transformed GCCGO_ERROR -> ERROR. When types2 no longer
reports an error in a certain place, I transformed ERROR -> GCCGO_ERROR.
When types2 reports an error in a new place, I used GC_ERROR.
The remaining entries in types2Failures are things that I think we
probably still need to fix - either actually missing errors in types2,
or cases where types2 gives worse errors than -G=0.
Change-Id: I7f01e82b322b16094096b67d7ed2bb39b410c34f
Reviewed-on: https://go-review.googlesource.com/c/go/+/372854
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2021-12-03 16:10:10 -08:00
|
|
|
"import1.go", // types2 reports extra errors
|
|
|
|
|
"initializerr.go", // types2 reports extra error
|
|
|
|
|
"typecheck.go", // types2 reports extra error at function call
|
|
|
|
|
|
|
|
|
|
"fixedbugs/bug176.go", // types2 reports all errors (pref: types2)
|
|
|
|
|
"fixedbugs/bug195.go", // types2 reports slight different errors, and an extra error
|
|
|
|
|
"fixedbugs/bug412.go", // types2 produces a follow-on error
|
|
|
|
|
|
|
|
|
|
"fixedbugs/issue11614.go", // types2 reports an extra error
|
|
|
|
|
"fixedbugs/issue17038.go", // types2 doesn't report a follow-on error (pref: types2)
|
|
|
|
|
"fixedbugs/issue23732.go", // types2 reports different (but ok) line numbers
|
|
|
|
|
"fixedbugs/issue4510.go", // types2 reports different (but ok) line numbers
|
|
|
|
|
"fixedbugs/issue7525b.go", // types2 reports init cycle error on different line - ok otherwise
|
|
|
|
|
"fixedbugs/issue7525c.go", // types2 reports init cycle error on different line - ok otherwise
|
|
|
|
|
"fixedbugs/issue7525d.go", // types2 reports init cycle error on different line - ok otherwise
|
|
|
|
|
"fixedbugs/issue7525e.go", // types2 reports init cycle error on different line - ok otherwise
|
|
|
|
|
"fixedbugs/issue7525.go", // types2 reports init cycle error on different line - ok otherwise
|
|
|
|
|
)
|
|
|
|
|
|
2021-07-02 13:18:03 -07:00
|
|
|
func setOf(keys ...string) map[string]bool {
|
|
|
|
|
m := make(map[string]bool, len(keys))
|
|
|
|
|
for _, key := range keys {
|
|
|
|
|
m[key] = true
|
|
|
|
|
}
|
|
|
|
|
return m
|
2020-12-03 11:11:05 -08:00
|
|
|
}
|
2021-06-11 09:54:40 -07:00
|
|
|
|
|
|
|
|
// splitQuoted splits the string s around each instance of one or more consecutive
|
|
|
|
|
// white space characters while taking into account quotes and escaping, and
|
|
|
|
|
// returns an array of substrings of s or an empty list if s contains only white space.
|
|
|
|
|
// Single quotes and double quotes are recognized to prevent splitting within the
|
|
|
|
|
// quoted region, and are removed from the resulting substrings. If a quote in s
|
|
|
|
|
// isn't closed err will be set and r will have the unclosed argument as the
|
|
|
|
|
// last element. The backslash is used for escaping.
|
|
|
|
|
//
|
|
|
|
|
// For example, the following string:
|
|
|
|
|
//
|
go/types,types2: delay the check for conflicting struct field names
In #52529, we observed that checking types for duplicate fields and
methods during method collection can result in incorrect early expansion
of the base type. Fix this by delaying the check for duplicate fields.
Notably, we can't delay the check for duplicate methods as we must
preserve the invariant that added method names are unique.
After this change, it may be possible in the presence of errors to have
a type-checked type containing a method name that conflicts with a field
name. With the previous logic conflicting methods would have been
skipped. This is a change in behavior, but only for invalid code.
Preserving the existing behavior would likely require delaying method
collection, which could have more significant consequences.
As a result of this change, the compiler test fixedbugs/issue28268.go
started passing with types2, being previously marked as broken. The fix
was not actually related to the duplicate method error, but rather the
fact that we stopped reporting redundant errors on the calls to x.b()
and x.E(), because they are now (valid!) methods.
Fixes #52529
Change-Id: I850ce85c6ba76d79544f46bfd3deb8538d8c7d00
Reviewed-on: https://go-review.googlesource.com/c/go/+/403455
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-05-02 11:13:08 -04:00
|
|
|
// a b:"c d" 'e''f' "g\""
|
2021-06-11 09:54:40 -07:00
|
|
|
//
|
|
|
|
|
// Would be parsed as:
|
|
|
|
|
//
|
go/types,types2: delay the check for conflicting struct field names
In #52529, we observed that checking types for duplicate fields and
methods during method collection can result in incorrect early expansion
of the base type. Fix this by delaying the check for duplicate fields.
Notably, we can't delay the check for duplicate methods as we must
preserve the invariant that added method names are unique.
After this change, it may be possible in the presence of errors to have
a type-checked type containing a method name that conflicts with a field
name. With the previous logic conflicting methods would have been
skipped. This is a change in behavior, but only for invalid code.
Preserving the existing behavior would likely require delaying method
collection, which could have more significant consequences.
As a result of this change, the compiler test fixedbugs/issue28268.go
started passing with types2, being previously marked as broken. The fix
was not actually related to the duplicate method error, but rather the
fact that we stopped reporting redundant errors on the calls to x.b()
and x.E(), because they are now (valid!) methods.
Fixes #52529
Change-Id: I850ce85c6ba76d79544f46bfd3deb8538d8c7d00
Reviewed-on: https://go-review.googlesource.com/c/go/+/403455
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-05-02 11:13:08 -04:00
|
|
|
// []string{"a", "b:c d", "ef", `g"`}
|
2021-06-11 09:54:40 -07:00
|
|
|
//
|
|
|
|
|
// [copied from src/go/build/build.go]
|
|
|
|
|
func splitQuoted(s string) (r []string, err error) {
|
|
|
|
|
var args []string
|
|
|
|
|
arg := make([]rune, len(s))
|
|
|
|
|
escaped := false
|
|
|
|
|
quoted := false
|
|
|
|
|
quote := '\x00'
|
|
|
|
|
i := 0
|
|
|
|
|
for _, rune := range s {
|
|
|
|
|
switch {
|
|
|
|
|
case escaped:
|
|
|
|
|
escaped = false
|
|
|
|
|
case rune == '\\':
|
|
|
|
|
escaped = true
|
|
|
|
|
continue
|
|
|
|
|
case quote != '\x00':
|
|
|
|
|
if rune == quote {
|
|
|
|
|
quote = '\x00'
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
case rune == '"' || rune == '\'':
|
|
|
|
|
quoted = true
|
|
|
|
|
quote = rune
|
|
|
|
|
continue
|
|
|
|
|
case unicode.IsSpace(rune):
|
|
|
|
|
if quoted || i > 0 {
|
|
|
|
|
quoted = false
|
|
|
|
|
args = append(args, string(arg[:i]))
|
|
|
|
|
i = 0
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
arg[i] = rune
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
if quoted || i > 0 {
|
|
|
|
|
args = append(args, string(arg[:i]))
|
|
|
|
|
}
|
|
|
|
|
if quote != 0 {
|
|
|
|
|
err = errors.New("unclosed quote")
|
|
|
|
|
} else if escaped {
|
|
|
|
|
err = errors.New("unfinished escaping")
|
|
|
|
|
}
|
|
|
|
|
return args, err
|
|
|
|
|
}
|
2024-08-05 12:57:33 -07:00
|
|
|
|
|
|
|
|
// replacePrefix is like strings.ReplaceAll, but only replaces instances of old
|
|
|
|
|
// that are preceded by ' ', '\t', or appear at the beginning of a line.
|
|
|
|
|
//
|
|
|
|
|
// This does the same kind of filename string replacement as cmd/go.
|
|
|
|
|
// Pilfered from src/cmd/go/internal/work/shell.go .
|
|
|
|
|
func replacePrefix(s, old, new string) string {
|
|
|
|
|
n := strings.Count(s, old)
|
|
|
|
|
if n == 0 {
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s = strings.ReplaceAll(s, " "+old, " "+new)
|
|
|
|
|
s = strings.ReplaceAll(s, "\n"+old, "\n"+new)
|
|
|
|
|
s = strings.ReplaceAll(s, "\n\t"+old, "\n\t"+new)
|
|
|
|
|
if strings.HasPrefix(s, old) {
|
|
|
|
|
s = new + s[len(old):]
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|