Skip to content

Commit a45b476

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 83ec9e8 commit a45b476

File tree

6 files changed

+87
-8
lines changed

6 files changed

+87
-8
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{
@@ -186,6 +195,7 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
186195
fset: fset,
187196
tokenFiles: map[string]*token.File{},
188197
definedGlobally: map[string]ast.Node{},
198+
noescapingFuncs: map[string]*noescapingFunc{},
189199
anonDecls: map[interface{}]string{},
190200
visitedFiles: map[string][]byte{},
191201
}
@@ -344,6 +354,22 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
344354
})
345355
}
346356

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

@@ -409,6 +435,33 @@ func (p *cgoPackage) parseCGoPreprocessorLines(text string, pos token.Pos) strin
409435
}
410436
text = text[:lineStart] + string(spaces) + text[lineEnd:]
411437

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

cgo/libclang.go

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
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:26:5: warning: another warning
5-
// testdata/errors.go:13:23: unexpected token ), expected end of expression
6-
// testdata/errors.go:21:26: unexpected token ), expected end of expression
7-
// testdata/errors.go:16:33: unexpected token ), expected end of expression
8-
// testdata/errors.go:17:34: unexpected token ), expected end of expression
6+
// testdata/errors.go:31:5: warning: another warning
7+
// testdata/errors.go:18:23: unexpected token ), expected end of expression
8+
// testdata/errors.go:26:26: unexpected token ), expected end of expression
9+
// testdata/errors.go:21:33: unexpected token ), expected end of expression
10+
// testdata/errors.go:22:34: unexpected token ), expected end of expression
911
// -: unexpected token INT, expected end of expression
10-
// testdata/errors.go:30:35: unexpected number of parameters: expected 2, got 3
11-
// testdata/errors.go:31:31: unexpected number of parameters: expected 2, got 1
12+
// testdata/errors.go:35:35: unexpected number of parameters: expected 2, got 3
13+
// testdata/errors.go:36:31: unexpected number of parameters: expected 2, got 1
14+
// testdata/errors.go:3:1: function "unusedFunction" in #cgo noescape line is not used
1215

1316
// Type checking errors after CGo processing:
1417
// 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
@@ -67,5 +67,10 @@ func C.staticfunc!symbols.go(x C.int)
6767

6868
var C.staticfunc!symbols.go$funcaddr unsafe.Pointer
6969

70+
//export notEscapingFunction
71+
//go:noescape
72+
func C.notEscapingFunction(a *C.int)
73+
74+
var C.notEscapingFunction$funcaddr unsafe.Pointer
7075
//go:extern someValue
7176
var C.someValue C.int

0 commit comments

Comments
 (0)