Skip to content

Commit bbe3d24

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 d93a36c commit bbe3d24

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

@@ -36,6 +37,7 @@ type cgoPackage struct {
3637
fset *token.FileSet
3738
tokenFiles map[string]*token.File
3839
definedGlobally map[string]ast.Node
40+
noescapingFuncs map[string]*noescapingFunc // #cgo noescape lines
3941
anonDecls map[interface{}]string
4042
cflags []string // CFlags from #cgo lines
4143
ldflags []string // LDFlags from #cgo lines
@@ -74,6 +76,13 @@ type bitfieldInfo struct {
7476
endBit int64 // may be 0 meaning "until the end of the field"
7577
}
7678

79+
// Information about a #cgo noescape line in the source code.
80+
type noescapingFunc struct {
81+
name string
82+
pos token.Pos
83+
used bool // true if used somewhere in the source (for proper error reporting)
84+
}
85+
7786
// cgoAliases list type aliases between Go and C, for types that are equivalent
7887
// in both languages. See addTypeAliases.
7988
var cgoAliases = map[string]string{
@@ -171,6 +180,7 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
171180
fset: fset,
172181
tokenFiles: map[string]*token.File{},
173182
definedGlobally: map[string]ast.Node{},
183+
noescapingFuncs: map[string]*noescapingFunc{},
174184
anonDecls: map[interface{}]string{},
175185
visitedFiles: map[string][]byte{},
176186
}
@@ -330,6 +340,22 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
330340
})
331341
}
332342

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

@@ -395,6 +421,33 @@ func (p *cgoPackage) parseCGoPreprocessorLines(text string, pos token.Pos) strin
395421
}
396422
text = text[:lineStart] + string(spaces) + text[lineEnd:]
397423

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

cgo/libclang.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,18 @@ func (f *cgoFile) createASTNode(name string, c clangCursor) (ast.Node, any) {
253253
},
254254
},
255255
}
256+
var doc []string
256257
if C.clang_isFunctionTypeVariadic(cursorType) != 0 {
258+
doc = append(doc, "//go:variadic")
259+
}
260+
if _, ok := f.noescapingFuncs[name]; ok {
261+
doc = append(doc, "//go:noescape")
262+
f.noescapingFuncs[name].used = true
263+
}
264+
if len(doc) != 0 {
257265
decl.Doc.List = append(decl.Doc.List, &ast.Comment{
258266
Slash: pos - 1,
259-
Text: "//go:variadic",
267+
Text: strings.Join(doc, "\n"),
260268
})
261269
}
262270
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)