Skip to content

Commit 04f0521

Browse files
ariel-anieligopherbot
authored andcommitted
cmd/cgo: skip escape checks if call site has no argument
This CL improves on CL 579955. When the call site has no argument, 1 package main 2 3 /* 4 #include <stdio.h> 5 void foo() {printf("Hello from C\n");} 6 */ 7 import "C" 8 9 func main() { 10 C.foo() 11 } Escape checks are not needed, $ go tool cgo -objdir dir cgonoargs.go $ cat -n dir/_cgo_gotypes.go | sed -n '43,$p' 43 func _Cfunc_foo() (r1 _Ctype_void) { 44 _cgo_runtime_cgocall(_cgo_c8ba2f813f11_Cfunc_foo, uintptr(unsafe.Pointer(&r1))) 45 if _Cgo_always_false { 46 } 47 return 48 } Skip escape checks if call site has no argument. $ cat -n dir/_cgo_gotypes.go | sed -n '43,$p' 43 func _Cfunc_foo() (r1 _Ctype_void) { 44 _cgo_runtime_cgocall(_cgo_c8ba2f813f11_Cfunc_foo, uintptr(unsafe.Pointer(&r1))) 45 return 46 } For #75856 Change-Id: I9aac0b6fb2985f6833976099e7eead1f28971bee GitHub-Last-Rev: 1aacde4 GitHub-Pull-Request: #76186 Reviewed-on: https://go-review.googlesource.com/c/go/+/718060 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Keith Randall <khr@google.com> Auto-Submit: Keith Randall <khr@golang.org>
1 parent 9f3a108 commit 04f0521

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/cmd/cgo/out.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,15 @@ func (p *Package) writeDefsFunc(fgo2 io.Writer, n *Name, callsMalloc *bool) {
649649
if p.noEscapes[n.C] && p.noCallbacks[n.C] {
650650
touchFunc = "_Cgo_keepalive"
651651
}
652-
fmt.Fprintf(fgo2, "\tif _Cgo_always_false {\n")
653-
if d.Type.Params != nil {
652+
653+
if len(paramnames) > 0 {
654+
fmt.Fprintf(fgo2, "\tif _Cgo_always_false {\n")
654655
for _, name := range paramnames {
655656
fmt.Fprintf(fgo2, "\t\t%s(%s)\n", touchFunc, name)
656657
}
658+
fmt.Fprintf(fgo2, "\t}\n")
657659
}
658-
fmt.Fprintf(fgo2, "\t}\n")
660+
659661
fmt.Fprintf(fgo2, "\treturn\n")
660662
fmt.Fprintf(fgo2, "}\n")
661663
}

0 commit comments

Comments
 (0)