rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

allowlist with opaque_type on `typedef struct A {} A;` generates invalid code

Open vthib opened this issue 3 years ago • 0 comments

Input C/C++ Header

typedef struct Foo {} Foo;
Foo *foo_new(void);

Bindgen Invocation

bindgen input.h --no-layout-tests --allowlist-function foo_new --opaque-type Foo

Actual Results

/* automatically generated by rust-bindgen 0.59.1 */

extern "C" {
    pub fn foo_new() -> *mut Foo;
}

Expected Results

Without the opaque-type argument, this code is generated:

/* automatically generated by rust-bindgen 0.59.1 */

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Foo {}
extern "C" {
    pub fn foo_new() -> *mut Foo;
}

If --allowlist-type Foo is added as well, then the codegen is ok:

/* automatically generated by rust-bindgen 0.59.1 */

#[repr(C)]
#[repr(align(1))]
#[derive(Debug, Copy, Clone)]
pub struct Foo {
    pub _bindgen_opaque_blob: [u8; 0usize],
}
extern "C" {
    pub fn foo_new() -> *mut Foo;
}

It appears to be a bug with a typedef of a struct using the same name as the struct, because with this input:

typedef struct _Foo {} Foo;
Foo *foo_new(void);

Then all those commands work well:

bindgen input.h --no-layout-tests --allowlist-function 'foo_new' --opaque-type Foo
bindgen input.h --no-layout-tests --allowlist-function 'foo_new' --opaque-type _Foo
bindgen input.h --no-layout-tests --allowlist-function 'foo_new' --opaque-type Foo --opaque-type _Foo

vthib avatar Oct 11 '21 12:10 vthib