Skip to content

Commit

Permalink
cgo: add C._Bool type
Browse files Browse the repository at this point in the history
This fixes #3926.

While working on this I've found another bug: if C.bool is referenced
from within Go, it isn't available anymore on the C side. This is an
existing bug that also applies to float and double, but may be less
likely to be triggered there.
This bug is something to be fixed at a later time (it has something to
do with preprocessor defines).
  • Loading branch information
aykevl committed Sep 22, 2023
1 parent ed2a98c commit dfe559e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cgo/cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ var cgoAliases = map[string]string{
"C.uintptr_t": "uintptr",
"C.float": "float32",
"C.double": "float64",
"C._Bool": "bool",
}

// builtinAliases are handled specially because they only exist on the Go side
Expand Down Expand Up @@ -311,10 +312,11 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
// Process CGo imports for each file.
for i, f := range files {
cf := p.newCGoFile(f, i)
// Float and double are aliased, meaning that C.float is the same thing
// as float32 in Go.
// These types are aliased with the corresponding types in C. For
// example, float in C is always float32 in Go.
cf.names["float"] = clangCursor{}
cf.names["double"] = clangCursor{}
cf.names["_Bool"] = clangCursor{}
// Now read all the names (identifies) that C defines in the header
// snippet.
cf.readNames(p.cgoHeaders[i], cflagsForCGo, filepath.Base(fset.File(f.Pos()).Name()), func(names map[string]clangCursor) {
Expand Down
4 changes: 4 additions & 0 deletions testdata/cgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ int mul(int, int);
#include <string.h>
#cgo CFLAGS: -DSOME_CONSTANT=17
#define someDefine -5 + 2 * 7
bool someBool;
*/
import "C"

Expand Down Expand Up @@ -56,6 +57,9 @@ func main() {
var goInt8 int8 = 5
var _ C.int8_t = goInt8

var _ bool = C.someBool
var _ C._Bool = C.someBool

// more globals
println("bool:", C.globalBool, C.globalBool2 == true)
println("float:", C.globalFloat)
Expand Down

0 comments on commit dfe559e

Please sign in to comment.