diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2018-10-20 07:07:25 +0200 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2018-10-22 09:10:47 +0200 |
commit | 870f2b86dfe389e7fa546686a2d9b8d70e52f043 (patch) | |
tree | 815aa4c8e1e891e14d883a849e699ff40fb9b125 | |
parent | d7a74baf5c822e0312aca539c3836e5d87c6091b (diff) |
codegen: Teach bindgen to respect constness of statics.
-rw-r--r-- | src/codegen/mod.rs | 8 | ||||
-rw-r--r-- | tests/expectations/tests/extern-const-struct.rs | 46 | ||||
-rw-r--r-- | tests/expectations/tests/issue-511.rs | 11 | ||||
-rw-r--r-- | tests/expectations/tests/libclang-3.8/auto.rs | 2 | ||||
-rw-r--r-- | tests/expectations/tests/libclang-3.8/const_bool.rs | 6 | ||||
-rw-r--r-- | tests/expectations/tests/libclang-3.8/constant-evaluate.rs | 12 | ||||
-rw-r--r-- | tests/expectations/tests/libclang-3.9/constant-evaluate.rs | 2 | ||||
-rw-r--r-- | tests/headers/extern-const-struct.h | 5 |
8 files changed, 77 insertions, 15 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index f29a3bc0..8cc55a0e 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -575,10 +575,16 @@ impl CodeGenerator for Var { attrs.push(attributes::link_name(self.name())); } + let maybe_mut = if self.is_const() { + quote! { } + } else { + quote! { mut } + }; + let mut tokens = quote!( extern "C" { #(#attrs)* - pub static mut #canonical_ident: #ty; + pub static #maybe_mut #canonical_ident: #ty; } ); diff --git a/tests/expectations/tests/extern-const-struct.rs b/tests/expectations/tests/extern-const-struct.rs new file mode 100644 index 00000000..2f9ade8b --- /dev/null +++ b/tests/expectations/tests/extern-const-struct.rs @@ -0,0 +1,46 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Copy, Clone)] +pub struct nsFoo { + pub details: [f32; 400usize], +} +#[test] +fn bindgen_test_layout_nsFoo() { + assert_eq!( + ::std::mem::size_of::<nsFoo>(), + 1600usize, + concat!("Size of: ", stringify!(nsFoo)) + ); + assert_eq!( + ::std::mem::align_of::<nsFoo>(), + 4usize, + concat!("Alignment of ", stringify!(nsFoo)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<nsFoo>())).details as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(nsFoo), + "::", + stringify!(details) + ) + ); +} +impl Default for nsFoo { + fn default() -> Self { + unsafe { ::std::mem::zeroed() } + } +} +extern "C" { + #[link_name = "\u{1}gDetails"] + pub static gDetails: nsFoo; +} diff --git a/tests/expectations/tests/issue-511.rs b/tests/expectations/tests/issue-511.rs index ff725b33..453fed09 100644 --- a/tests/expectations/tests/issue-511.rs +++ b/tests/expectations/tests/issue-511.rs @@ -1,6 +1,11 @@ /* automatically generated by rust-bindgen */ -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] extern "C" { #[link_name = "\u{1}a"] @@ -12,9 +17,9 @@ extern "C" { } extern "C" { #[link_name = "\u{1}c"] - pub static mut c: *mut ::std::os::raw::c_char; + pub static c: *mut ::std::os::raw::c_char; } extern "C" { #[link_name = "\u{1}d"] - pub static mut d: *const ::std::os::raw::c_char; + pub static d: *const ::std::os::raw::c_char; } diff --git a/tests/expectations/tests/libclang-3.8/auto.rs b/tests/expectations/tests/libclang-3.8/auto.rs index 8db72d0f..920023f3 100644 --- a/tests/expectations/tests/libclang-3.8/auto.rs +++ b/tests/expectations/tests/libclang-3.8/auto.rs @@ -11,7 +11,7 @@ pub struct Foo { } extern "C" { #[link_name = "\u{1}_ZN3Foo4kFooE"] - pub static mut Foo_kFoo: bool; + pub static Foo_kFoo: bool; } #[test] fn bindgen_test_layout_Foo() { diff --git a/tests/expectations/tests/libclang-3.8/const_bool.rs b/tests/expectations/tests/libclang-3.8/const_bool.rs index 35636842..af5adcaf 100644 --- a/tests/expectations/tests/libclang-3.8/const_bool.rs +++ b/tests/expectations/tests/libclang-3.8/const_bool.rs @@ -6,7 +6,7 @@ extern "C" { #[link_name = "\u{1}_ZL1k"] - pub static mut k: bool; + pub static k: bool; } #[repr(C)] #[derive(Debug, Default, Copy, Clone)] @@ -15,7 +15,7 @@ pub struct A { } extern "C" { #[link_name = "\u{1}_ZN1A1kE"] - pub static mut A_k: bool; + pub static A_k: bool; } #[test] fn bindgen_test_layout_A() { @@ -33,5 +33,5 @@ fn bindgen_test_layout_A() { pub type foo = bool; extern "C" { #[link_name = "\u{1}_ZL2k2"] - pub static mut k2: foo; + pub static k2: foo; } diff --git a/tests/expectations/tests/libclang-3.8/constant-evaluate.rs b/tests/expectations/tests/libclang-3.8/constant-evaluate.rs index 7df4e918..fa54df27 100644 --- a/tests/expectations/tests/libclang-3.8/constant-evaluate.rs +++ b/tests/expectations/tests/libclang-3.8/constant-evaluate.rs @@ -13,27 +13,27 @@ pub type EasyToOverflow = ::std::os::raw::c_ulonglong; pub const k: EasyToOverflow = 2147483648; extern "C" { #[link_name = "\u{1}k_expr"] - pub static mut k_expr: EasyToOverflow; + pub static k_expr: EasyToOverflow; } extern "C" { #[link_name = "\u{1}wow"] - pub static mut wow: EasyToOverflow; + pub static wow: EasyToOverflow; } extern "C" { #[link_name = "\u{1}BAZ"] - pub static mut BAZ: ::std::os::raw::c_longlong; + pub static BAZ: ::std::os::raw::c_longlong; } extern "C" { #[link_name = "\u{1}fuzz"] - pub static mut fuzz: f64; + pub static fuzz: f64; } extern "C" { #[link_name = "\u{1}BAZZ"] - pub static mut BAZZ: ::std::os::raw::c_char; + pub static BAZZ: ::std::os::raw::c_char; } extern "C" { #[link_name = "\u{1}WAT"] - pub static mut WAT: ::std::os::raw::c_char; + pub static WAT: ::std::os::raw::c_char; } extern "C" { #[link_name = "\u{1}bytestring"] diff --git a/tests/expectations/tests/libclang-3.9/constant-evaluate.rs b/tests/expectations/tests/libclang-3.9/constant-evaluate.rs index 69501c64..6a3c8a36 100644 --- a/tests/expectations/tests/libclang-3.9/constant-evaluate.rs +++ b/tests/expectations/tests/libclang-3.9/constant-evaluate.rs @@ -17,7 +17,7 @@ pub const k: EasyToOverflow = 2147483648; pub const k_expr: EasyToOverflow = 0; extern "C" { #[link_name = "\u{1}wow"] - pub static mut wow: EasyToOverflow; + pub static wow: EasyToOverflow; } pub const BAZ: ::std::os::raw::c_longlong = 24; pub const fuzz: f64 = 51.0; diff --git a/tests/headers/extern-const-struct.h b/tests/headers/extern-const-struct.h new file mode 100644 index 00000000..10006e82 --- /dev/null +++ b/tests/headers/extern-const-struct.h @@ -0,0 +1,5 @@ +struct nsFoo { + float details[400]; +}; + +extern const struct nsFoo gDetails; |