From 01979bc0d9250dd3d1d635a0c120ddc604138bd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Mon, 1 Feb 2021 13:37:58 +0100 Subject: [PATCH 1/2] ir: Support pointers to zsts. Fixes #654 --- src/bindgen/ir/ty.rs | 12 ++---------- tests/expectations/zst.both.c | 11 +++++++++++ tests/expectations/zst.both.compat.c | 19 +++++++++++++++++++ tests/expectations/zst.c | 11 +++++++++++ tests/expectations/zst.compat.c | 19 +++++++++++++++++++ tests/expectations/zst.cpp | 16 ++++++++++++++++ tests/expectations/zst.pyx | 13 +++++++++++++ tests/expectations/zst.tag.c | 11 +++++++++++ tests/expectations/zst.tag.compat.c | 19 +++++++++++++++++++ tests/expectations/zst.tag.pyx | 13 +++++++++++++ tests/rust/zst.rs | 10 ++++++++++ 11 files changed, 144 insertions(+), 10 deletions(-) create mode 100644 tests/expectations/zst.both.c create mode 100644 tests/expectations/zst.both.compat.c create mode 100644 tests/expectations/zst.c create mode 100644 tests/expectations/zst.compat.c create mode 100644 tests/expectations/zst.cpp create mode 100644 tests/expectations/zst.pyx create mode 100644 tests/expectations/zst.tag.c create mode 100644 tests/expectations/zst.tag.compat.c create mode 100644 tests/expectations/zst.tag.pyx create mode 100644 tests/rust/zst.rs diff --git a/src/bindgen/ir/ty.rs b/src/bindgen/ir/ty.rs index 5574d2fdf..7d59f0190 100644 --- a/src/bindgen/ir/ty.rs +++ b/src/bindgen/ir/ty.rs @@ -367,11 +367,7 @@ impl Type { let converted = match converted { Some(converted) => converted, - None => { - return Err("Cannot have a pointer to a zero sized type. If you are \ - trying to represent `void*` use `c_void*`." - .to_owned()); - } + None => Type::Primitive(PrimitiveType::Void), }; // TODO(emilio): we could make these use is_ref: true. @@ -388,11 +384,7 @@ impl Type { let converted = match converted { Some(converted) => converted, - None => { - return Err("Cannot have a pointer to a zero sized type. If you are \ - trying to represent `void*` use `c_void*`." - .to_owned()); - } + None => Type::Primitive(PrimitiveType::Void), }; let is_const = pointer.mutability.is_none(); diff --git a/tests/expectations/zst.both.c b/tests/expectations/zst.both.c new file mode 100644 index 000000000..b03132f91 --- /dev/null +++ b/tests/expectations/zst.both.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +typedef struct TraitObject { + void *data; + void *vtable; +} TraitObject; + +void *root(const void *ptr, struct TraitObject t); diff --git a/tests/expectations/zst.both.compat.c b/tests/expectations/zst.both.compat.c new file mode 100644 index 000000000..154f14f1e --- /dev/null +++ b/tests/expectations/zst.both.compat.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +typedef struct TraitObject { + void *data; + void *vtable; +} TraitObject; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void *root(const void *ptr, struct TraitObject t); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/zst.c b/tests/expectations/zst.c new file mode 100644 index 000000000..04caa7d18 --- /dev/null +++ b/tests/expectations/zst.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +typedef struct { + void *data; + void *vtable; +} TraitObject; + +void *root(const void *ptr, TraitObject t); diff --git a/tests/expectations/zst.compat.c b/tests/expectations/zst.compat.c new file mode 100644 index 000000000..5dbc3ea71 --- /dev/null +++ b/tests/expectations/zst.compat.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +typedef struct { + void *data; + void *vtable; +} TraitObject; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void *root(const void *ptr, TraitObject t); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/zst.cpp b/tests/expectations/zst.cpp new file mode 100644 index 000000000..6d6a4e2e3 --- /dev/null +++ b/tests/expectations/zst.cpp @@ -0,0 +1,16 @@ +#include +#include +#include +#include +#include + +struct TraitObject { + void *data; + void *vtable; +}; + +extern "C" { + +void *root(const void *ptr, TraitObject t); + +} // extern "C" diff --git a/tests/expectations/zst.pyx b/tests/expectations/zst.pyx new file mode 100644 index 000000000..2e96545c8 --- /dev/null +++ b/tests/expectations/zst.pyx @@ -0,0 +1,13 @@ +from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t +from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t +cdef extern from *: + ctypedef bint bool + ctypedef struct va_list + +cdef extern from *: + + ctypedef struct TraitObject: + void *data; + void *vtable; + + void *root(const void *ptr, TraitObject t); diff --git a/tests/expectations/zst.tag.c b/tests/expectations/zst.tag.c new file mode 100644 index 000000000..0a999aff0 --- /dev/null +++ b/tests/expectations/zst.tag.c @@ -0,0 +1,11 @@ +#include +#include +#include +#include + +struct TraitObject { + void *data; + void *vtable; +}; + +void *root(const void *ptr, struct TraitObject t); diff --git a/tests/expectations/zst.tag.compat.c b/tests/expectations/zst.tag.compat.c new file mode 100644 index 000000000..8a115b727 --- /dev/null +++ b/tests/expectations/zst.tag.compat.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +struct TraitObject { + void *data; + void *vtable; +}; + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +void *root(const void *ptr, struct TraitObject t); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/zst.tag.pyx b/tests/expectations/zst.tag.pyx new file mode 100644 index 000000000..03235cc2d --- /dev/null +++ b/tests/expectations/zst.tag.pyx @@ -0,0 +1,13 @@ +from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t +from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t +cdef extern from *: + ctypedef bint bool + ctypedef struct va_list + +cdef extern from *: + + cdef struct TraitObject: + void *data; + void *vtable; + + void *root(const void *ptr, TraitObject t); diff --git a/tests/rust/zst.rs b/tests/rust/zst.rs new file mode 100644 index 000000000..8a3e46739 --- /dev/null +++ b/tests/rust/zst.rs @@ -0,0 +1,10 @@ +#[repr(C)] +pub struct TraitObject { + pub data: *mut (), + pub vtable: *mut (), +} + +#[no_mangle] +pub extern "C" fn root(ptr: *const (), t: TraitObject) -> *mut () { + std::ptr::null_mut() +} From a8c2f3972eb6145cc71cecdad34af46967b424f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Mon, 1 Feb 2021 13:45:22 +0100 Subject: [PATCH 2/2] config: Deal with clippy lints -.-. --- src/bindgen/config.rs | 1 + src/bindgen/ir/cfg.rs | 13 +++++-------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index f89fb6ee2..8702cb366 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -60,6 +60,7 @@ impl Language { /// Controls what type of line endings are used in the generated code. #[derive(Debug, Clone, Copy)] +#[allow(clippy::upper_case_acronyms)] pub enum LineEndingStyle { /// Use Unix-style linefeed characters LF, diff --git a/src/bindgen/ir/cfg.rs b/src/bindgen/ir/cfg.rs index 46bc7b1d6..a02145ee7 100644 --- a/src/bindgen/ir/cfg.rs +++ b/src/bindgen/ir/cfg.rs @@ -153,15 +153,12 @@ impl Cfg { } syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { ref path, - ref lit, + lit: syn::Lit::Str(ref value), .. - })) => match *lit { - syn::Lit::Str(ref value) => Cfg::Named( - format!("{}", path.segments.first().unwrap().ident), - value.value(), - ), - _ => return None, - }, + })) => Cfg::Named( + format!("{}", path.segments.first().unwrap().ident), + value.value(), + ), syn::NestedMeta::Meta(syn::Meta::List(syn::MetaList { ref path, ref nested,