diff --git a/src/bindgen/cdecl.rs b/src/bindgen/cdecl.rs index 3ba543ac..78be0f7f 100644 --- a/src/bindgen/cdecl.rs +++ b/src/bindgen/cdecl.rs @@ -273,7 +273,7 @@ impl CDecl { } // Write the right part of declarators after the identifier - let mut iter = self.declarators.iter(); + let mut iter = self.declarators.iter().peekable(); let mut last_was_pointer = false; #[allow(clippy::while_let_on_iterator)] @@ -286,7 +286,14 @@ impl CDecl { if last_was_pointer { out.write(")"); } - write!(out, "[{}]", constant); + + let is_fam = constant == "0" && iter.peek().is_none(); + if is_fam && !config.structure.gnu_flexible_array_members && config.language != Language::Cython { + write!(out, "[]"); + } + else { + write!(out, "[{}]", constant); + } last_was_pointer = false; } diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index 31316503..aa2164fe 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -499,6 +499,8 @@ pub struct StructConfig { pub deprecated: Option, /// The way to annotation this function as #[deprecated] with notes pub deprecated_with_note: Option, + /// The way we represent flexible array members, either GNU style or c99 style + pub gnu_flexible_array_members: bool, } impl StructConfig { diff --git a/tests/expectations/struct.c b/tests/expectations/struct.c index 207e9a05..ef9730c1 100644 --- a/tests/expectations/struct.c +++ b/tests/expectations/struct.c @@ -25,4 +25,14 @@ typedef struct { float y; } TupleNamed; -void root(Opaque *a, Normal b, NormalWithZST c, TupleRenamed d, TupleNamed e); +typedef struct { + int32_t x; + int8_t y[]; +} WithFlexibleArrayMember; + +void root(Opaque *a, + Normal b, + NormalWithZST c, + TupleRenamed d, + TupleNamed e, + WithFlexibleArrayMember f); diff --git a/tests/expectations/struct.compat.c b/tests/expectations/struct.compat.c index fd82fcd1..01e5d057 100644 --- a/tests/expectations/struct.compat.c +++ b/tests/expectations/struct.compat.c @@ -25,11 +25,21 @@ typedef struct { float y; } TupleNamed; +typedef struct { + int32_t x; + int8_t y[]; +} WithFlexibleArrayMember; + #ifdef __cplusplus extern "C" { #endif // __cplusplus -void root(Opaque *a, Normal b, NormalWithZST c, TupleRenamed d, TupleNamed e); +void root(Opaque *a, + Normal b, + NormalWithZST c, + TupleRenamed d, + TupleNamed e, + WithFlexibleArrayMember f); #ifdef __cplusplus } // extern "C" diff --git a/tests/expectations/struct.cpp b/tests/expectations/struct.cpp index 4fe03404..592299f2 100644 --- a/tests/expectations/struct.cpp +++ b/tests/expectations/struct.cpp @@ -26,8 +26,18 @@ struct TupleNamed { float y; }; +struct WithFlexibleArrayMember { + int32_t x; + int8_t y[]; +}; + extern "C" { -void root(Opaque *a, Normal b, NormalWithZST c, TupleRenamed d, TupleNamed e); +void root(Opaque *a, + Normal b, + NormalWithZST c, + TupleRenamed d, + TupleNamed e, + WithFlexibleArrayMember f); } // extern "C" diff --git a/tests/expectations/struct.pyx b/tests/expectations/struct.pyx index 3342101f..5d795e11 100644 --- a/tests/expectations/struct.pyx +++ b/tests/expectations/struct.pyx @@ -25,4 +25,13 @@ cdef extern from *: int32_t x; float y; - void root(Opaque *a, Normal b, NormalWithZST c, TupleRenamed d, TupleNamed e); + ctypedef struct WithFlexibleArrayMember: + int32_t x; + int8_t y[0]; + + void root(Opaque *a, + Normal b, + NormalWithZST c, + TupleRenamed d, + TupleNamed e, + WithFlexibleArrayMember f); diff --git a/tests/expectations/struct_both.c b/tests/expectations/struct_both.c index 5664185f..25d10bdc 100644 --- a/tests/expectations/struct_both.c +++ b/tests/expectations/struct_both.c @@ -25,8 +25,14 @@ typedef struct TupleNamed { float y; } TupleNamed; +typedef struct WithFlexibleArrayMember { + int32_t x; + int8_t y[]; +} WithFlexibleArrayMember; + void root(struct Opaque *a, struct Normal b, struct NormalWithZST c, struct TupleRenamed d, - struct TupleNamed e); + struct TupleNamed e, + struct WithFlexibleArrayMember f); diff --git a/tests/expectations/struct_both.compat.c b/tests/expectations/struct_both.compat.c index ee0f4c76..ab4c6aa0 100644 --- a/tests/expectations/struct_both.compat.c +++ b/tests/expectations/struct_both.compat.c @@ -25,6 +25,11 @@ typedef struct TupleNamed { float y; } TupleNamed; +typedef struct WithFlexibleArrayMember { + int32_t x; + int8_t y[]; +} WithFlexibleArrayMember; + #ifdef __cplusplus extern "C" { #endif // __cplusplus @@ -33,7 +38,8 @@ void root(struct Opaque *a, struct Normal b, struct NormalWithZST c, struct TupleRenamed d, - struct TupleNamed e); + struct TupleNamed e, + struct WithFlexibleArrayMember f); #ifdef __cplusplus } // extern "C" diff --git a/tests/expectations/struct_tag.c b/tests/expectations/struct_tag.c index cc5ddaa7..76ac9f21 100644 --- a/tests/expectations/struct_tag.c +++ b/tests/expectations/struct_tag.c @@ -25,8 +25,14 @@ struct TupleNamed { float y; }; +struct WithFlexibleArrayMember { + int32_t x; + int8_t y[]; +}; + void root(struct Opaque *a, struct Normal b, struct NormalWithZST c, struct TupleRenamed d, - struct TupleNamed e); + struct TupleNamed e, + struct WithFlexibleArrayMember f); diff --git a/tests/expectations/struct_tag.compat.c b/tests/expectations/struct_tag.compat.c index 092007c0..5fe2021a 100644 --- a/tests/expectations/struct_tag.compat.c +++ b/tests/expectations/struct_tag.compat.c @@ -25,6 +25,11 @@ struct TupleNamed { float y; }; +struct WithFlexibleArrayMember { + int32_t x; + int8_t y[]; +}; + #ifdef __cplusplus extern "C" { #endif // __cplusplus @@ -33,7 +38,8 @@ void root(struct Opaque *a, struct Normal b, struct NormalWithZST c, struct TupleRenamed d, - struct TupleNamed e); + struct TupleNamed e, + struct WithFlexibleArrayMember f); #ifdef __cplusplus } // extern "C" diff --git a/tests/expectations/struct_tag.pyx b/tests/expectations/struct_tag.pyx index 768f3752..f8fa226e 100644 --- a/tests/expectations/struct_tag.pyx +++ b/tests/expectations/struct_tag.pyx @@ -25,4 +25,13 @@ cdef extern from *: int32_t x; float y; - void root(Opaque *a, Normal b, NormalWithZST c, TupleRenamed d, TupleNamed e); + cdef struct WithFlexibleArrayMember: + int32_t x; + int8_t y[0]; + + void root(Opaque *a, + Normal b, + NormalWithZST c, + TupleRenamed d, + TupleNamed e, + WithFlexibleArrayMember f); diff --git a/tests/rust/struct.rs b/tests/rust/struct.rs index 7569901a..c443ced9 100644 --- a/tests/rust/struct.rs +++ b/tests/rust/struct.rs @@ -28,11 +28,18 @@ struct TupleRenamed(i32, f32); #[repr(C)] struct TupleNamed(i32, f32); +#[repr(C)] +struct WithFlexibleArrayMember { + x: i32, + y: [i8; 0], +} + #[no_mangle] pub extern "C" fn root( a: *mut Opaque, b: Normal, c: NormalWithZST, d: TupleRenamed, - e: TupleNamed + e: TupleNamed, + f: WithFlexibleArrayMember, ) { }