cmd/go: rewrite cgo names to "C." in compiler error messages

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>
This commit is contained in:
Ian Lance Taylor 2025-11-18 16:24:04 -08:00 committed by Gopher Robot
parent a3688ab13e
commit 1bbb78e777
4 changed files with 13 additions and 14 deletions

View 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))
}

View 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

View 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.

View 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)
}