From: Ian Lance Taylor Date: Wed, 19 Nov 2025 00:24:04 +0000 (-0800) Subject: cmd/go: rewrite cgo names to "C." in compiler error messages X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1bbb78e7772e7530338e9f154936dc2479adafac;p=gostls13.git 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 LUCI-TryBot-Result: Go LUCI Auto-Submit: Ian Lance Taylor Reviewed-by: Roland Shoemaker --- diff --git a/src/cmd/cgo/internal/testerrors/errors_test.go b/src/cmd/cgo/internal/testerrors/errors_test.go index 80d2c402ce..41a69d4e26 100644 --- a/src/cmd/cgo/internal/testerrors/errors_test.go +++ b/src/cmd/cgo/internal/testerrors/errors_test.go @@ -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)) } diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go index c497135612..d1c0c4ea63 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -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 diff --git a/src/cmd/go/internal/work/shell.go b/src/cmd/go/internal/work/shell.go index ceff84d81f..ffb8f27271 100644 --- a/src/cmd/go/internal/work/shell.go +++ b/src/cmd/go/internal/work/shell.go @@ -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. diff --git a/src/internal/lazyregexp/lazyre.go b/src/internal/lazyregexp/lazyre.go index 2681af35af..0562a854f7 100644 --- a/src/internal/lazyregexp/lazyre.go +++ b/src/internal/lazyregexp/lazyre.go @@ -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) }