From b07046d28a17970dd04426daa273eb09722f589a Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Fri, 10 Aug 2018 23:28:29 +0200 Subject: [PATCH] Fixed missing prefix for named consts in `[T; ]` types --- src/bindgen/cdecl.rs | 3 ++- src/bindgen/ir/ty.rs | 31 ++++++++++++++++++++++++++----- tests/expectations/both/prefix.c | 11 +++++++++++ tests/expectations/prefix.c | 11 +++++++++++ tests/expectations/prefix.cpp | 14 ++++++++++++++ tests/expectations/tag/prefix.c | 11 +++++++++++ tests/rust/prefix.rs | 7 +++++++ tests/rust/prefix.toml | 2 ++ 8 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 tests/expectations/both/prefix.c create mode 100644 tests/expectations/prefix.c create mode 100644 tests/expectations/prefix.cpp create mode 100644 tests/expectations/tag/prefix.c create mode 100644 tests/rust/prefix.rs create mode 100644 tests/rust/prefix.toml diff --git a/src/bindgen/cdecl.rs b/src/bindgen/cdecl.rs index e9e4adf..abe6c52 100644 --- a/src/bindgen/cdecl.rs +++ b/src/bindgen/cdecl.rs @@ -122,7 +122,8 @@ impl CDecl { self.build_type(t, false); } &Type::Array(ref t, ref constant) => { - self.declarators.push(CDeclarator::Array(constant.clone())); + let len = constant.as_str().to_owned(); + self.declarators.push(CDeclarator::Array(len)); self.build_type(t, false); } &Type::FuncPtr(ref ret, ref args) => { diff --git a/src/bindgen/ir/ty.rs b/src/bindgen/ir/ty.rs index 1221be0..02240fe 100644 --- a/src/bindgen/ir/ty.rs +++ b/src/bindgen/ir/ty.rs @@ -166,13 +166,28 @@ impl fmt::Display for PrimitiveType { } } +// The `U` part of `[T; U]` +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum ArrayLength { + Name(String), + Value(String), +} + +impl ArrayLength { + pub fn as_str(&self) -> &str { + match self { + ArrayLength::Name(ref string) | ArrayLength::Value(ref string) => string + } + } +} + #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum Type { ConstPtr(Box), Ptr(Box), Path(GenericPath), Primitive(PrimitiveType), - Array(Box, String), + Array(Box, ArrayLength), FuncPtr(Box, Vec), } @@ -242,8 +257,9 @@ impl Type { }; let path = GenericPath::load(&path.path)?; - - Type::Array(Box::new(converted), path.name) + let len = ArrayLength::Name(path.name); + // panic!("panic -> name: {:?}", len); + Type::Array(Box::new(converted), len) } &syn::Type::Array(syn::TypeArray { ref elem, @@ -261,7 +277,9 @@ impl Type { None => return Err("Cannot have an array of zero sized types.".to_owned()), }; - Type::Array(Box::new(converted), format!("{}", len.value())) + let len = ArrayLength::Value(format!("{}", len.value())); + // panic!("panic -> value: {:?}", len); + Type::Array(Box::new(converted), len) } &syn::Type::BareFn(ref function) => { let args = function.inputs.iter().try_skip_map(|x| Type::load(&x.ty))?; @@ -480,8 +498,11 @@ impl Type { config.export.rename(&mut path.name); } &mut Type::Primitive(_) => {} - &mut Type::Array(ref mut ty, _) => { + &mut Type::Array(ref mut ty, ref mut len) => { ty.rename_for_config(config); + if let ArrayLength::Name(ref mut name) = len { + config.export.rename(name); + } } &mut Type::FuncPtr(ref mut ret, ref mut args) => { ret.rename_for_config(config); diff --git a/tests/expectations/both/prefix.c b/tests/expectations/both/prefix.c new file mode 100644 index 0000000..72dc40e --- /dev/null +++ b/tests/expectations/both/prefix.c @@ -0,0 +1,11 @@ +#include +#include +#include + +#define PREFIX_LEN 42 + +typedef int32_t PREFIX_NamedLenArray[PREFIX_LEN]; + +typedef int32_t PREFIX_ValuedLenArray[42]; + +void root(PREFIX_NamedLenArray x, PREFIX_ValuedLenArray y); diff --git a/tests/expectations/prefix.c b/tests/expectations/prefix.c new file mode 100644 index 0000000..72dc40e --- /dev/null +++ b/tests/expectations/prefix.c @@ -0,0 +1,11 @@ +#include +#include +#include + +#define PREFIX_LEN 42 + +typedef int32_t PREFIX_NamedLenArray[PREFIX_LEN]; + +typedef int32_t PREFIX_ValuedLenArray[42]; + +void root(PREFIX_NamedLenArray x, PREFIX_ValuedLenArray y); diff --git a/tests/expectations/prefix.cpp b/tests/expectations/prefix.cpp new file mode 100644 index 0000000..f80c9a5 --- /dev/null +++ b/tests/expectations/prefix.cpp @@ -0,0 +1,14 @@ +#include +#include + +static const int32_t PREFIX_LEN = 42; + +using PREFIX_NamedLenArray = int32_t[PREFIX_LEN]; + +using PREFIX_ValuedLenArray = int32_t[42]; + +extern "C" { + +void root(PREFIX_NamedLenArray x, PREFIX_ValuedLenArray y); + +} // extern "C" diff --git a/tests/expectations/tag/prefix.c b/tests/expectations/tag/prefix.c new file mode 100644 index 0000000..72dc40e --- /dev/null +++ b/tests/expectations/tag/prefix.c @@ -0,0 +1,11 @@ +#include +#include +#include + +#define PREFIX_LEN 42 + +typedef int32_t PREFIX_NamedLenArray[PREFIX_LEN]; + +typedef int32_t PREFIX_ValuedLenArray[42]; + +void root(PREFIX_NamedLenArray x, PREFIX_ValuedLenArray y); diff --git a/tests/rust/prefix.rs b/tests/rust/prefix.rs new file mode 100644 index 0000000..5779a66 --- /dev/null +++ b/tests/rust/prefix.rs @@ -0,0 +1,7 @@ +const LEN: i32 = 42; + +pub type NamedLenArray = [i32; LEN]; +pub type ValuedLenArray = [i32; 42]; + +#[no_mangle] +pub extern "C" fn root(x: NamedLenArray, y: ValuedLenArray) { } diff --git a/tests/rust/prefix.toml b/tests/rust/prefix.toml new file mode 100644 index 0000000..648c142 --- /dev/null +++ b/tests/rust/prefix.toml @@ -0,0 +1,2 @@ +[export] +prefix = "PREFIX_"