]> git.feebdaed.xyz Git - 0xmirror/go.git/commitdiff
cmd/cgo: don't emit C local if it is not used
authorIan Lance Taylor <iant@golang.org>
Wed, 17 Dec 2025 00:00:36 +0000 (16:00 -0800)
committerGopher Robot <gobot@golang.org>
Wed, 17 Dec 2025 05:28:13 +0000 (21:28 -0800)
Fixes #76861

Change-Id: Icc8452e48ed736e8240f8afea18637c33b8e3ef8
Reviewed-on: https://go-review.googlesource.com/c/go/+/730600
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Joe Richey <joerichey@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>

src/cmd/cgo/internal/test/issue76861.go [new file with mode: 0644]
src/cmd/cgo/internal/test/issue76861/a.go [new file with mode: 0644]
src/cmd/cgo/out.go

diff --git a/src/cmd/cgo/internal/test/issue76861.go b/src/cmd/cgo/internal/test/issue76861.go
new file mode 100644 (file)
index 0000000..225e2ac
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build cgo
+
+package cgotest
+
+// Issue 43639: No runtime test needed, make sure package
+// cmd/cgo/internal/test/issue76861 compiles without error.
+
+import _ "cmd/cgo/internal/test/issue76861"
diff --git a/src/cmd/cgo/internal/test/issue76861/a.go b/src/cmd/cgo/internal/test/issue76861/a.go
new file mode 100644 (file)
index 0000000..18a7bda
--- /dev/null
@@ -0,0 +1,13 @@
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package issue76861
+
+// #cgo CFLAGS: -Wall -Werror
+// void issue76861(void) {}
+import "C"
+
+func Issue76861() {
+       C.issue76861()
+}
index dc1e5b29e592484eed13a83dea3abeb63aaba580..ac2ce8fd0dce3b30415cf4c2171c40763a1069f1 100644 (file)
@@ -783,13 +783,13 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
        // We're trying to write a gcc struct that matches gc's layout.
        // Use packed attribute to force no padding in this struct in case
        // gcc has different packing requirements.
-       fmt.Fprintf(fgcc, "\t%s %v *_cgo_a = v;\n", ctype, p.packedAttribute())
-       if n.FuncType.Result != nil {
-               // Save the stack top for use below.
-               fmt.Fprintf(fgcc, "\tchar *_cgo_stktop = _cgo_topofstack();\n")
-       }
        tr := n.FuncType.Result
+       if (n.Kind != "macro" && len(n.FuncType.Params) > 0) || tr != nil {
+               fmt.Fprintf(fgcc, "\t%s %v *_cgo_a = v;\n", ctype, p.packedAttribute())
+       }
        if tr != nil {
+               // Save the stack top for use below.
+               fmt.Fprintf(fgcc, "\tchar *_cgo_stktop = _cgo_topofstack();\n")
                fmt.Fprintf(fgcc, "\t__typeof__(_cgo_a->r) _cgo_r;\n")
        }
        fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
@@ -819,7 +819,7 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
                fmt.Fprintf(fgcc, "\t_cgo_errno = errno;\n")
        }
        fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n")
-       if n.FuncType.Result != nil {
+       if tr != nil {
                // The cgo call may have caused a stack copy (via a callback).
                // Adjust the return value pointer appropriately.
                fmt.Fprintf(fgcc, "\t_cgo_a = (void*)((char*)_cgo_a + (_cgo_topofstack() - _cgo_stktop));\n")