From bb00d1c4a8e53c56880586987f0e1512fb252b9a Mon Sep 17 00:00:00 2001 From: Hayes Neuman Date: Fri, 22 May 2020 14:36:07 -0400 Subject: [PATCH] Support renaming for constants with casts, and properly order them in the output. --- src/bindgen/bindings.rs | 4 +- src/bindgen/ir/constant.rs | 51 ++++++++++++++++++++++--- tests/expectations/both/rename.c | 2 + tests/expectations/both/rename.compat.c | 2 + tests/expectations/rename.c | 2 + tests/expectations/rename.compat.c | 2 + tests/expectations/rename.cpp | 2 + tests/expectations/tag/rename.c | 2 + tests/expectations/tag/rename.compat.c | 2 + tests/rust/rename.rs | 2 + 10 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/bindgen/bindings.rs b/src/bindgen/bindings.rs index 36d0076..89fb897 100644 --- a/src/bindgen/bindings.rs +++ b/src/bindgen/bindings.rs @@ -216,7 +216,7 @@ impl Bindings { self.open_namespaces(&mut out); for constant in &self.constants { - if constant.ty.is_primitive_or_ptr_primitive() { + if constant.uses_only_primitive_types() { out.new_line_if_not_start(); constant.write(&self.config, &mut out, None); out.new_line(); @@ -247,7 +247,7 @@ impl Bindings { } for constant in &self.constants { - if !constant.ty.is_primitive_or_ptr_primitive() { + if !constant.uses_only_primitive_types() { out.new_line_if_not_start(); constant.write(&self.config, &mut out, None); out.new_line(); diff --git a/src/bindgen/ir/constant.rs b/src/bindgen/ir/constant.rs index 1ae682d..c337c4b 100644 --- a/src/bindgen/ir/constant.rs +++ b/src/bindgen/ir/constant.rs @@ -46,11 +46,17 @@ pub enum Literal { impl Literal { fn replace_self_with(&mut self, self_ty: &Path) { match *self { - Literal::PostfixUnaryOp { .. } - | Literal::BinOp { .. } - | Literal::Expr(..) - | Literal::Path(..) - | Literal::Cast { .. } => {} + Literal::PostfixUnaryOp { ref mut value, .. } => { + value.replace_self_with(self_ty); + } + Literal::BinOp { + ref mut left, + ref mut right, + .. + } => { + left.replace_self_with(self_ty); + right.replace_self_with(self_ty); + } Literal::Struct { ref mut path, ref mut export_name, @@ -63,6 +69,14 @@ impl Literal { expr.replace_self_with(self_ty); } } + Literal::Cast { + ref mut ty, + ref mut value, + } => { + ty.replace_self_with(self_ty); + value.replace_self_with(self_ty); + } + Literal::Expr(..) | Literal::Path(..) => {} } } @@ -80,6 +94,23 @@ impl Literal { Literal::Cast { ref value, .. } => value.is_valid(bindings), } } + + pub fn uses_only_primitive_types(&self) -> bool { + match self { + Literal::Expr(..) => true, + Literal::Path(..) => true, + Literal::PostfixUnaryOp { ref value, .. } => value.uses_only_primitive_types(), + Literal::BinOp { + ref left, + ref right, + .. + } => left.uses_only_primitive_types() & right.uses_only_primitive_types(), + Literal::Struct { .. } => false, + Literal::Cast { ref value, ref ty } => { + value.uses_only_primitive_types() && ty.is_primitive_or_ptr_primitive() + } + } + } } impl Literal { @@ -110,7 +141,11 @@ impl Literal { right.rename_for_config(config); } Literal::Expr(_) => {} - Literal::Cast { ref mut value, .. } => { + Literal::Cast { + ref mut ty, + ref mut value, + } => { + ty.rename_for_config(config, &GenericParams::default()); value.rename_for_config(config); } } @@ -398,6 +433,10 @@ impl Constant { associated_to, } } + + pub fn uses_only_primitive_types(&self) -> bool { + self.value.uses_only_primitive_types() && self.ty.is_primitive_or_ptr_primitive() + } } impl Item for Constant { diff --git a/tests/expectations/both/rename.c b/tests/expectations/both/rename.c index c9c510f..d6e6fd0 100644 --- a/tests/expectations/both/rename.c +++ b/tests/expectations/both/rename.c @@ -27,6 +27,8 @@ typedef union C_D { typedef C_A C_F; +#define C_I (intptr_t)(C_F*)10 + extern const int32_t G; void root(const C_A *a, C_AwesomeB b, C_C c, C_D d, C_E e, C_F f); diff --git a/tests/expectations/both/rename.compat.c b/tests/expectations/both/rename.compat.c index c352311..f0a087f 100644 --- a/tests/expectations/both/rename.compat.c +++ b/tests/expectations/both/rename.compat.c @@ -33,6 +33,8 @@ typedef union C_D { typedef C_A C_F; +#define C_I (intptr_t)(C_F*)10 + #ifdef __cplusplus extern "C" { #endif // __cplusplus diff --git a/tests/expectations/rename.c b/tests/expectations/rename.c index 1c40c5d..738907d 100644 --- a/tests/expectations/rename.c +++ b/tests/expectations/rename.c @@ -27,6 +27,8 @@ typedef union { typedef C_A C_F; +#define C_I (intptr_t)(C_F*)10 + extern const int32_t G; void root(const C_A *a, C_AwesomeB b, C_C c, C_D d, C_E e, C_F f); diff --git a/tests/expectations/rename.compat.c b/tests/expectations/rename.compat.c index 1a808fd..1394860 100644 --- a/tests/expectations/rename.compat.c +++ b/tests/expectations/rename.compat.c @@ -33,6 +33,8 @@ typedef union { typedef C_A C_F; +#define C_I (intptr_t)(C_F*)10 + #ifdef __cplusplus extern "C" { #endif // __cplusplus diff --git a/tests/expectations/rename.cpp b/tests/expectations/rename.cpp index ac54da1..b62758f 100644 --- a/tests/expectations/rename.cpp +++ b/tests/expectations/rename.cpp @@ -26,6 +26,8 @@ union C_D { using C_F = C_A; +static const intptr_t C_I = (intptr_t)(C_F*)10; + extern "C" { extern const int32_t G; diff --git a/tests/expectations/tag/rename.c b/tests/expectations/tag/rename.c index 514f720..ca12718 100644 --- a/tests/expectations/tag/rename.c +++ b/tests/expectations/tag/rename.c @@ -27,6 +27,8 @@ union C_D { typedef struct C_A C_F; +#define C_I (intptr_t)(C_F*)10 + extern const int32_t G; void root(const struct C_A *a, struct C_AwesomeB b, struct C_C c, union C_D d, C_E e, C_F f); diff --git a/tests/expectations/tag/rename.compat.c b/tests/expectations/tag/rename.compat.c index 1ba82a3..d7cf1c8 100644 --- a/tests/expectations/tag/rename.compat.c +++ b/tests/expectations/tag/rename.compat.c @@ -33,6 +33,8 @@ union C_D { typedef struct C_A C_F; +#define C_I (intptr_t)(C_F*)10 + #ifdef __cplusplus extern "C" { #endif // __cplusplus diff --git a/tests/rust/rename.rs b/tests/rust/rename.rs index e89f0c3..ba10249 100644 --- a/tests/rust/rename.rs +++ b/tests/rust/rename.rs @@ -33,6 +33,8 @@ pub static G: i32 = 10; pub const H: i32 = 10; +pub const I: isize = 10 as *mut F as isize; + #[no_mangle] pub extern "C" fn root( a: *const A,