cgo: rename reserved field names like type

This commit renames reserved field names like `type` to `_type`, and in
turn renames those fields as well (recursively). This avoids name
clashes when a C struct contains a field named `type`, which is a
reserved keyword in Go.

For some details, see:
https://golang.org/cmd/cgo/#hdr-Go_references_to_C
This commit is contained in:
Ayke van Laethem
2019-11-06 14:02:18 +01:00
committed by Ron Evans
parent 0db403dc0c
commit b41e58bcb4
4 changed files with 49 additions and 0 deletions
+16
View File
@@ -15,6 +15,18 @@ typedef struct point3d {
int z;
} point3d_t;
// Structs with reserved field names.
struct type1 {
// All these fields should be renamed.
int type;
int _type;
int __type;
};
struct type2 {
// This field should not be renamed.
int _type;
};
// Enums. These define constant numbers. All these constants must be given the
// correct number.
typedef enum option {
@@ -66,6 +78,10 @@ var (
_ C.point3d_t
_ C.struct_point3d
// Structs with reserved field names.
_ C.struct_type1
_ C.struct_type2
// Enums (anonymous and named).
_ C.option_t
_ C.enum_option
+6
View File
@@ -72,4 +72,10 @@ type C.struct_point3d struct {
y C.int
z C.int
}
type C.struct_type1 struct {
_type C.int
__type C.int
___type C.int
}
type C.struct_type2 struct{ _type C.int }
type C.enum_option C.int