]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: rewrite cgo names to "C." in compiler error messages
authorIan Lance Taylor <iant@golang.org>
Wed, 19 Nov 2025 00:24:04 +0000 (16:24 -0800)
committerGopher Robot <gobot@golang.org>
Wed, 4 Feb 2026 19:25:57 +0000 (11:25 -0800)
We used to do this but it broke in Go 1.10. This restores the rewrite,
but only applied to compiler output for packages that use cgo.
That is all that the original rewrite applied to anyhow.

Fixes #76339

Change-Id: Ife8ee858ddd0ff7bcc7423455b2eabf8381b7bde
Reviewed-on: https://go-review.googlesource.com/c/go/+/721821
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Roland Shoemaker <roland@golang.org>
src/cmd/cgo/internal/testerrors/errors_test.go
src/cmd/go/internal/work/exec.go
src/cmd/go/internal/work/shell.go
src/internal/lazyregexp/lazyre.go

index 80d2c402cec0c2be8f0b8cc0da0cf83d99b234ea..41a69d4e26be7b4ef381b0b3f1d40003a7eb0788 100644 (file)
@@ -72,7 +72,7 @@ func expect(t *testing.T, errors []*regexp.Regexp, files ...string) {
        defer os.RemoveAll(dir)
 
        dst := filepath.Join(dir, strings.TrimSuffix(files[0], ".go"))
-       args := []string{"build", "-gcflags=-L -e", "-o=" + dst} // TODO(gri) no need for -gcflags=-L if go tool is adjusted
+       args := []string{"build", "-gcflags=-e", "-o=" + dst}
        for _, file := range files {
                args = append(args, path(file))
        }
index c497135612ece6e14a1c74ade3485fcf30159caa..d1c0c4ea6361259a9c7226d176994b01fbf5d99e 100644 (file)
@@ -928,6 +928,12 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
        // Compile Go.
        objpkg := objdir + "_pkg_.a"
        ofile, out, err := BuildToolchain.gc(b, a, objpkg, icfg.Bytes(), embedcfg, symabis, len(sfiles) > 0, pgoProfile, gofiles)
+       if len(out) > 0 && (p.UsesCgo() || p.UsesSwig()) && !cfg.BuildX {
+               // Fix up output referring to cgo-generated code to be more readable.
+               // Replace *[100]_Ctype_foo with *[100]C.foo.
+               // If we're using -x, assume we're debugging and want the full dump, so disable the rewrite.
+               out = cgoTypeSigRe.ReplaceAll(out, []byte("C."))
+       }
        if err := sh.reportCmd("", "", out, err); err != nil {
                return err
        }
@@ -1033,6 +1039,8 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
        return nil
 }
 
+var cgoTypeSigRe = lazyregexp.New(`\b_C2?(type|func|var|macro)_\B`)
+
 func (b *Builder) checkDirectives(a *Action) error {
        var msg []byte
        p := a.Package
index ceff84d81f831f6181da7eb00d63a34061f24fa8..ffb8f27271d4633d5bcca76923fd0da904cd6ff8 100644 (file)
@@ -15,7 +15,6 @@ import (
        "cmd/internal/pathcache"
        "errors"
        "fmt"
-       "internal/lazyregexp"
        "io"
        "io/fs"
        "os"
@@ -511,15 +510,6 @@ func (sh *Shell) reportCmd(desc, dir string, cmdOut []byte, cmdErr error) error
                dir = dirP
        }
 
-       // Fix up output referring to cgo-generated code to be more readable.
-       // Replace x.go:19[/tmp/.../x.cgo1.go:18] with x.go:19.
-       // Replace *[100]_Ctype_foo with *[100]C.foo.
-       // If we're using -x, assume we're debugging and want the full dump, so disable the rewrite.
-       if !cfg.BuildX && cgoLine.MatchString(out) {
-               out = cgoLine.ReplaceAllString(out, "")
-               out = cgoTypeSigRe.ReplaceAllString(out, "C.")
-       }
-
        // Usually desc is already p.Desc(), but if not, signal cmdError.Error to
        // add a line explicitly mentioning the import path.
        needsPath := importPath != "" && p != nil && desc != p.Desc()
@@ -580,9 +570,6 @@ func (e *cmdError) ImportPath() string {
        return e.importPath
 }
 
-var cgoLine = lazyregexp.New(`\[[^\[\]]+\.(cgo1|cover)\.go:[0-9]+(:[0-9]+)?\]`)
-var cgoTypeSigRe = lazyregexp.New(`\b_C2?(type|func|var|macro)_\B`)
-
 // run runs the command given by cmdline in the directory dir.
 // If the command fails, run prints information about the failure
 // and returns a non-nil error.
index 2681af35af1954b1384eecc12bcea655462a8776..0562a854f7c8a51e093643763fde288a39e48722 100644 (file)
@@ -43,6 +43,10 @@ func (r *Regexp) FindStringSubmatchIndex(s string) []int {
        return r.re().FindStringSubmatchIndex(s)
 }
 
+func (r *Regexp) ReplaceAll(src, repl []byte) []byte {
+       return r.re().ReplaceAll(src, repl)
+}
+
 func (r *Regexp) ReplaceAllString(src, repl string) string {
        return r.re().ReplaceAllString(src, repl)
 }