Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy not derived when using whitelist_recursively(false) #1454

Closed
jethrogb opened this issue Nov 29, 2018 · 5 comments · Fixed by #2003 or #2007
Closed

Copy not derived when using whitelist_recursively(false) #1454

jethrogb opened this issue Nov 29, 2018 · 5 comments · Fixed by #2003 or #2007
Labels

Comments

@jethrogb
Copy link
Contributor

Input C/C++ Header

#include <stdint.h>
#include <stdlib.h>
typedef uint64_t mbedtls_mpi_uint;
typedef struct
{
    int s;              /*!<  integer sign      */
    size_t n;           /*!<  total # of limbs  */
    mbedtls_mpi_uint *p;          /*!<  pointer to limbs  */
}
mbedtls_mpi;

Bindgen Invocation

    let bindings = bindgen::builder()
        .header_contents("test.h", ...)
        .trust_clang_mangling(false)
        .derive_copy(true)
        .whitelist_type("mbedtls_.*")
        .whitelist_recursively(whitelist_recursively)
        .layout_tests(false)
        .generate()
        .expect("bindgen error");

Actual Results

// whitelist_recursively = false
/* automatically generated by rust-bindgen */

pub type mbedtls_mpi_uint = u64;
#[repr(C)]
pub struct mbedtls_mpi {
    ///<  integer sign
    pub s: ::std::os::raw::c_int,
    ///<  total # of limbs
    pub n: usize,
    ///<  pointer to limbs
    pub p: *mut mbedtls_mpi_uint,
}

Expected Results

// whitelist_recursively = true
/* automatically generated by rust-bindgen */

pub type mbedtls_mpi_uint = u64;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct mbedtls_mpi {
    ///<  integer sign
    pub s: ::std::os::raw::c_int,
    ///<  total # of limbs
    pub n: usize,
    ///<  pointer to limbs
    pub p: *mut mbedtls_mpi_uint,
}
@emilio
Copy link
Contributor

emilio commented Jan 16, 2019

So what's going on is that when not whitelisting recursively we're not seeing through the built-in types (since they're not whitelisted!) and then we think we cannot derive stuff for them. You can see this if you do whitelist_type(".*") where the result is the expected one.

I think we could auto-whitelist items that don't generate code to make this better.

@emilio
Copy link
Contributor

emilio commented Jan 16, 2019

#1492 should address this. You should also whitelist size_t|uint64_t on your test-case, since bindgen thinks it's an alias.

We could do the check of whether we're a known alias there as well, which would be more convenient too, though a bit more code and another list that we need to make sure doesn't go out of sync.

Thoughts @jethrogb? Does that patch would work for you?

@jethrogb
Copy link
Contributor Author

jethrogb commented Jan 17, 2019

Two more examples of things I'd like to work (I don't know how bindgen is supposed to figure out which types are ok to derive, maybe yet another addition to ParseCallbacks?):

Input C/C++ Header

#include <sys/resource.h>

typedef struct
{
    struct rlimit core;
    struct rlimit cpu;
    struct rlimit data;
    struct rlimit fsize;
}
my_rlimit_conf;

Bindgen Invocation

let bindings = bindgen::builder()
    .header_contents("test.h", ...)
    .trust_clang_mangling(false)
    .derive_copy(true)
    .whitelist_type("my_rlimit_conf")
    .whitelist_recursively(false)
    .layout_tests(false)
    .generate()
    .expect("bindgen error");
println!("
extern crate libc;
use libc::rlimit;
{}
    ", bindings.to_string());

Actual Results

extern crate libc;
use libc::rlimit;
/* automatically generated by rust-bindgen */

#[repr(C)]
pub struct my_rlimit_conf {
    pub core: rlimit,
    pub cpu: rlimit,
    pub data: rlimit,
    pub fsize: rlimit,
}

Expected Results

extern crate libc;
use libc::rlimit;
/* automatically generated by rust-bindgen */

#[repr(C)]
#[derive(Copy, Clone)]
pub struct my_rlimit_conf {
    pub core: rlimit,
    pub cpu: rlimit,
    pub data: rlimit,
    pub fsize: rlimit,
}

(maybe also derive(Debug) depending on which libc feature is set, see RFC 2235)

Input C/C++ Header

#include <zlib.h>

typedef struct
{
    z_streamp stream;
}
mystruct;

Bindgen Invocation

let bindings = bindgen::builder()
    .header_contents("test.h", ...)
    .trust_clang_mangling(false)
    .derive_copy(true)
    .whitelist_type("mystruct")
    .whitelist_recursively(false)
    .layout_tests(false)
    .generate()
    .expect("bindgen error");
println!("
extern crate libz_sys;
use libz_sys::z_streamp;
{}
    ", bindings.to_string());

Actual Results

extern crate libz_sys;
use libz_sys::z_streamp;
/* automatically generated by rust-bindgen */

#[repr(C)]
pub struct mystruct {
    pub stream: z_streamp,
}

Expected Results

extern crate libz_sys;
use libz_sys::z_streamp;
/* automatically generated by rust-bindgen */

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct mystruct {
    pub stream: z_streamp,
}

@jethrogb
Copy link
Contributor Author

Can we do something like if the non-whitelisted types of member fields were emitted and that would result in the particular derive, then we do the derive anyway without emitting the member type?

@jethrogb jethrogb mentioned this issue Mar 21, 2019
emilio added a commit to emilio/rust-bindgen that referenced this issue Mar 25, 2019
emilio added a commit to emilio/rust-bindgen that referenced this issue Mar 25, 2019
emilio added a commit to emilio/rust-bindgen that referenced this issue Mar 25, 2019
@jethrogb
Copy link
Contributor Author

Please reopen

@emilio emilio reopened this Mar 26, 2019
jethrogb pushed a commit to jethrogb/rust-bindgen that referenced this issue Mar 9, 2021
jethrogb pushed a commit to jethrogb/rust-bindgen that referenced this issue Mar 9, 2021
jethrogb pushed a commit to jethrogb/rust-bindgen that referenced this issue Mar 10, 2021
jethrogb pushed a commit to jethrogb/rust-bindgen that referenced this issue Mar 10, 2021
jethrogb pushed a commit to jethrogb/rust-bindgen that referenced this issue Mar 10, 2021
@emilio emilio reopened this Mar 11, 2021
jethrogb pushed a commit to jethrogb/rust-bindgen that referenced this issue Mar 16, 2021
jethrogb pushed a commit to jethrogb/rust-bindgen that referenced this issue Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants