Skip to content

Commit c34848f

Browse files
committed
cgo: add support for #cgo noescape lines
Here is the proposal: golang/go#56378 They are documented here: https://pkg.go.dev/cmd/cgo@master#hdr-Optimizing_calls_of_C_code This would have been very useful to fix tinygo-org/bluetooth#176 in a nice way. That bug is now fixed in a different way using a wrapper function, but once this new noescape pragma gets included in TinyGo we could remove the workaround and use `#cgo noescape` instead.
1 parent dfa9eb7 commit c34848f

File tree

6 files changed

+83
-4
lines changed

6 files changed

+83
-4
lines changed

cgo/cgo.go

+53
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"go/scanner"
1919
"go/token"
2020
"path/filepath"
21+
"sort"
2122
"strconv"
2223
"strings"
2324

@@ -42,6 +43,7 @@ type cgoPackage struct {
4243
fset *token.FileSet
4344
tokenFiles map[string]*token.File
4445
definedGlobally map[string]ast.Node
46+
noescapingFuncs map[string]*noescapingFunc // #cgo noescape lines
4547
anonDecls map[interface{}]string
4648
cflags []string // CFlags from #cgo lines
4749
ldflags []string // LDFlags from #cgo lines
@@ -80,6 +82,13 @@ type bitfieldInfo struct {
8082
endBit int64 // may be 0 meaning "until the end of the field"
8183
}
8284

85+
// Information about a #cgo noescape line in the source code.
86+
type noescapingFunc struct {
87+
name string
88+
pos token.Pos
89+
used bool // true if used somewhere in the source (for proper error reporting)
90+
}
91+
8392
// cgoAliases list type aliases between Go and C, for types that are equivalent
8493
// in both languages. See addTypeAliases.
8594
var cgoAliases = map[string]string{
@@ -179,6 +188,7 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
179188
fset: fset,
180189
tokenFiles: map[string]*token.File{},
181190
definedGlobally: map[string]ast.Node{},
191+
noescapingFuncs: map[string]*noescapingFunc{},
182192
anonDecls: map[interface{}]string{},
183193
visitedFiles: map[string][]byte{},
184194
}
@@ -337,6 +347,22 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
337347
})
338348
}
339349

350+
// Show an error when a #cgo noescape line isn't used in practice.
351+
// This matches upstream Go. I think the goal is to avoid issues with
352+
// misspelled function names, which seems very useful.
353+
var unusedNoescapeLines []*noescapingFunc
354+
for _, value := range p.noescapingFuncs {
355+
if !value.used {
356+
unusedNoescapeLines = append(unusedNoescapeLines, value)
357+
}
358+
}
359+
sort.SliceStable(unusedNoescapeLines, func(i, j int) bool {
360+
return unusedNoescapeLines[i].pos < unusedNoescapeLines[j].pos
361+
})
362+
for _, value := range unusedNoescapeLines {
363+
p.addError(value.pos, fmt.Sprintf("function %#v not found in #cgo noescape line", value.name))
364+
}
365+
340366
// Print the newly generated in-memory AST, for debugging.
341367
//ast.Print(fset, p.generated)
342368

@@ -402,6 +428,33 @@ func (p *cgoPackage) parseCGoPreprocessorLines(text string, pos token.Pos) strin
402428
}
403429
text = text[:lineStart] + string(spaces) + text[lineEnd:]
404430

431+
allFields := strings.Fields(line[4:])
432+
switch allFields[0] {
433+
case "noescape":
434+
// The code indicates that pointer parameters will not be captured
435+
// by the called C function.
436+
if len(allFields) < 2 {
437+
p.addErrorAfter(pos, text[:lineStart], "missing function name in #cgo noescape line")
438+
continue
439+
}
440+
if len(allFields) > 2 {
441+
p.addErrorAfter(pos, text[:lineStart], "multiple function names in #cgo noescape line")
442+
continue
443+
}
444+
name := allFields[1]
445+
p.noescapingFuncs[name] = &noescapingFunc{
446+
name: name,
447+
pos: pos,
448+
used: false,
449+
}
450+
continue
451+
case "nocallback":
452+
// We don't do anything special when calling a C function, so there
453+
// appears to be no optimization that we can do here.
454+
// Accept, but ignore the parameter for compatibility.
455+
continue
456+
}
457+
405458
// Get the text before the colon in the #cgo directive.
406459
colon := strings.IndexByte(line, ':')
407460
if colon < 0 {

cgo/libclang.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,18 @@ func (f *cgoFile) createASTNode(name string, c clangCursor) (ast.Node, any) {
254254
},
255255
},
256256
}
257+
var doc []string
257258
if C.clang_isFunctionTypeVariadic(cursorType) != 0 {
259+
doc = append(doc, "//go:variadic")
260+
}
261+
if _, ok := f.noescapingFuncs[name]; ok {
262+
doc = append(doc, "//go:noescape")
263+
f.noescapingFuncs[name].used = true
264+
}
265+
if len(doc) != 0 {
258266
decl.Doc.List = append(decl.Doc.List, &ast.Comment{
259267
Slash: pos - 1,
260-
Text: "//go:variadic",
268+
Text: strings.Join(doc, "\n"),
261269
})
262270
}
263271
for i := 0; i < numArgs; i++ {

cgo/testdata/errors.go

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ typedef struct {
1010
1111
typedef someType noType; // undefined type
1212
13+
// Some invalid noescape lines
14+
#cgo noescape
15+
#cgo noescape foo bar
16+
#cgo noescape unusedFunction
17+
1318
#define SOME_CONST_1 5) // invalid const syntax
1419
#define SOME_CONST_2 6) // const not used (so no error)
1520
#define SOME_CONST_3 1234 // const too large for byte

cgo/testdata/errors.out.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// CGo errors:
2+
// testdata/errors.go:14:1: missing function name in #cgo noescape line
3+
// testdata/errors.go:15:1: multiple function names in #cgo noescape line
24
// testdata/errors.go:4:2: warning: some warning
35
// testdata/errors.go:11:9: error: unknown type name 'someType'
4-
// testdata/errors.go:22:5: warning: another warning
5-
// testdata/errors.go:13:23: unexpected token ), expected end of expression
6-
// testdata/errors.go:19:26: unexpected token ), expected end of expression
6+
// testdata/errors.go:27:5: warning: another warning
7+
// testdata/errors.go:18:23: unexpected token ), expected end of expression
8+
// testdata/errors.go:24:26: unexpected token ), expected end of expression
9+
// testdata/errors.go:3:1: function "unusedFunction" not found in #cgo noescape line
710

811
// Type checking errors after CGo processing:
912
// testdata/errors.go:102: cannot use 2 << 10 (untyped int constant 2048) as C.char value in variable declaration (overflows)

cgo/testdata/symbols.go

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ static void staticfunc(int x);
99
1010
// Global variable signatures.
1111
extern int someValue;
12+
13+
void notEscapingFunction(int *a);
14+
15+
#cgo noescape notEscapingFunction
1216
*/
1317
import "C"
1418

@@ -18,6 +22,7 @@ func accessFunctions() {
1822
C.variadic0()
1923
C.variadic2(3, 5)
2024
C.staticfunc(3)
25+
C.notEscapingFunction(nil)
2126
}
2227

2328
func accessGlobals() {

cgo/testdata/symbols.out.go

+5
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@ func C.staticfunc!symbols.go(x C.int)
6060

6161
var C.staticfunc!symbols.go$funcaddr unsafe.Pointer
6262

63+
//export notEscapingFunction
64+
//go:noescape
65+
func C.notEscapingFunction(a *C.int)
66+
67+
var C.notEscapingFunction$funcaddr unsafe.Pointer
6368
//go:extern someValue
6469
var C.someValue C.int

0 commit comments

Comments
 (0)